From 3d36bf2da15a04d3e70b840bc2611772bc8bdae0 Mon Sep 17 00:00:00 2001 From: "darin%meer.net" Date: Thu, 5 Feb 2004 23:41:37 +0000 Subject: [PATCH] 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 --- mozilla/xpcom/string/public/nsTAString.h | 2 +- mozilla/xpcom/string/public/nsTString.h | 20 ++++++++- mozilla/xpcom/string/public/nsTSubstring.h | 2 +- mozilla/xpcom/string/src/nsTSubstring.cpp | 24 ++++++---- mozilla/xpcom/tests/TestStrings.cpp | 52 ++++++++++++++++++++++ 5 files changed, 87 insertions(+), 13 deletions(-) diff --git a/mozilla/xpcom/string/public/nsTAString.h b/mozilla/xpcom/string/public/nsTAString.h index 8305cccdfc1..5c5f9bc4655 100644 --- a/mozilla/xpcom/string/public/nsTAString.h +++ b/mozilla/xpcom/string/public/nsTAString.h @@ -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) diff --git a/mozilla/xpcom/string/public/nsTString.h b/mozilla/xpcom/string/public/nsTString.h index 10708ef5a88..80d80a40027 100644 --- a/mozilla/xpcom/string/public/nsTString.h +++ b/mozilla/xpcom/string/public/nsTString.h @@ -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 diff --git a/mozilla/xpcom/string/public/nsTSubstring.h b/mozilla/xpcom/string/public/nsTSubstring.h index c7530272819..1cc61253b8b 100644 --- a/mozilla/xpcom/string/public/nsTSubstring.h +++ b/mozilla/xpcom/string/public/nsTSubstring.h @@ -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 }; }; diff --git a/mozilla/xpcom/string/src/nsTSubstring.cpp b/mozilla/xpcom/string/src/nsTSubstring.cpp index e4764af8199..3c0fbda8140 100644 --- a/mozilla/xpcom/string/src/nsTSubstring.cpp +++ b/mozilla/xpcom/string/src/nsTSubstring.cpp @@ -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); } } diff --git a/mozilla/xpcom/tests/TestStrings.cpp b/mozilla/xpcom/tests/TestStrings.cpp index ead4b158a5c..10310a371db 100644 --- a/mozilla/xpcom/tests/TestStrings.cpp +++ b/mozilla/xpcom/tests/TestStrings.cpp @@ -37,6 +37,7 @@ #include #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 } };