making nsXPIDLString's NULL property equate to nsAString's IsVoid property,

discussed this change with dbaron and jst.
adding testcases for nsXPIDLString


git-svn-id: svn://10.0.0.236/branches/STRING_20040119_BRANCH@152401 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
darin%meer.net
2004-02-05 23:41:37 +00:00
parent de85c6e5c4
commit 3d36bf2da1
5 changed files with 87 additions and 13 deletions

View File

@@ -298,7 +298,7 @@ class nsTAString_CharT
/**
* optional ctor for use by subclasses.
*
* mData and mLength are intentionally left uninitialized.
* NOTE: mData and mLength are intentionally left uninitialized.
*/
explicit
nsTAString_CharT(PRUint32 flags)

View File

@@ -525,6 +525,13 @@ class nsTAutoString_CharT : public nsTString_CharT
};
/**
* nsTXPIDLString extends nsTString such that:
*
* (1) mData can be null
* (2) objects of this type can be automatically cast to |const CharT*|
* (3) getter_Copies method is supported to adopt data
*/
class nsTXPIDLString_CharT : public nsTString_CharT
{
public:
@@ -534,11 +541,20 @@ class nsTXPIDLString_CharT : public nsTString_CharT
public:
nsTXPIDLString_CharT()
: string_type(nsnull, 0, 0) {}
: string_type(NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer), 0, F_TERMINATED | F_VOIDED) {}
// copy-constructor required to avoid default
nsTXPIDLString_CharT( const self_type& str )
: string_type(str) {}
: string_type(NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer), 0, F_TERMINATED | F_VOIDED)
{
Assign(str);
}
// return nsnull if we are voided
const char_type* get() const
{
return (mFlags & F_VOIDED) ? nsnull : mData;
}
// this case operator is the reason why this class cannot just be a
// typedef for nsTString

View File

@@ -362,6 +362,6 @@ class nsTSubstring_CharT : public nsTAString_CharT
F_VOIDED = 1 << 1, // IsVoid returns true
F_SHARED = 1 << 2, // mData[0] is prefixed with additional fields
F_OWNED = 1 << 3, // mData is owned by this class
F_FIXED = 1 << 4 // mData is pointing at nsTAutoString::mFixedBuf
F_FIXED = 1 << 4 // mData is pointing at a fixed-size writable buffer
};
};

View File

@@ -117,7 +117,7 @@ nsTSubstring_CharT::MutatePrep( size_type capacity, char_type** oldData, PRUint3
*oldFlags = mFlags;
mData = newData;
mFlags = (F_TERMINATED | F_SHARED);
mFlags = F_TERMINATED | F_SHARED;
// mLength does not change
@@ -240,7 +240,7 @@ nsTSubstring_CharT::EnsureMutable()
void
nsTSubstring_CharT::Assign( const char_type* data, size_type length )
{
// unfortunately, people do pass null sometimes :(
// unfortunately, some callers pass null :-(
if (!data)
{
Truncate();
@@ -281,11 +281,16 @@ nsTSubstring_CharT::Assign( const self_type& str )
mData = str.mData;
mLength = str.mLength;
mFlags = (F_TERMINATED | F_SHARED);
mFlags = F_TERMINATED | F_SHARED;
// get an owning reference to the mData
nsStringHeader::FromData(mData)->AddRef();
}
else if (str.mFlags & F_VOIDED)
{
// inherit the F_VOIDED attribute
SetIsVoid(PR_TRUE);
}
else
{
// else, treat this like an ordinary assignment.
@@ -325,21 +330,22 @@ nsTSubstring_CharT::Assign( const abstract_string_type& readable )
void
nsTSubstring_CharT::Adopt( char_type* data, size_type length )
{
::ReleaseData(mData, mFlags);
mData = data;
if (mData)
if (data)
{
::ReleaseData(mData, mFlags);
if (length == size_type(-1))
length = char_traits::length(data);
mData = data;
mLength = length;
mFlags = (F_TERMINATED | F_OWNED);
mFlags = F_TERMINATED | F_OWNED;
STRING_STAT_INCREMENT(Adopt);
}
else
{
mLength = 0;
mFlags = 0;
SetIsVoid(PR_TRUE);
}
}

View File

@@ -37,6 +37,7 @@
#include <stdio.h>
#include "nsString.h"
#include "nsCRT.h"
void test_assign_helper(const nsACString& in, nsACString &_retval)
{
@@ -256,6 +257,56 @@ PRBool test_concat_2()
return PR_FALSE;
}
PRBool test_xpidl_string()
{
nsXPIDLCString a, b;
a = b;
if (a != b)
return PR_FALSE;
a.Adopt(0);
if (a != b)
return PR_FALSE;
a.Append("foopy");
a.Assign(b);
if (a != b)
return PR_FALSE;
a.Insert("", 0);
a.Assign(b);
if (a != b)
return PR_FALSE;
const char text[] = "hello world";
*getter_Copies(a) = nsCRT::strdup(text);
if (strcmp(a, text) != 0)
return PR_FALSE;
b = a;
if (strcmp(a, b) != 0)
return PR_FALSE;
a.Adopt(0);
nsACString::const_iterator begin, end;
a.BeginReading(begin);
a.EndReading(end);
char *r = ToNewCString(Substring(begin, end));
if (strcmp(r, "") != 0)
return PR_FALSE;
nsMemory::Free(r);
a.Adopt(0);
if (a != (const char*) 0)
return PR_FALSE;
PRInt32 index = a.FindCharInSet("xyz");
if (index != kNotFound)
return PR_FALSE;
return PR_TRUE;
}
//----
typedef PRBool (*TestFunc)();
@@ -284,6 +335,7 @@ tests[] =
{ "test_cbufdesc", test_cbufdesc },
{ "test_concat", test_concat },
{ "test_concat_2", test_concat_2 },
{ "test_xpidl_string", test_xpidl_string },
{ nsnull, nsnull }
};