diff --git a/string/obsolete/nsString.h b/string/obsolete/nsString.h index 40024b58fa4..df7da3cbb42 100644 --- a/string/obsolete/nsString.h +++ b/string/obsolete/nsString.h @@ -34,6 +34,9 @@ 4. Subsumable strings ***********************************************************************/ +#ifndef NEW_STRING_APIS +//#define NEW_STRING_APIS 1 +#endif //NEW_STRING_APIS #ifndef _nsCString_ #define _nsCString_ diff --git a/string/obsolete/nsString2.cpp b/string/obsolete/nsString2.cpp index ab155aa18bb..954f0f8d008 100644 --- a/string/obsolete/nsString2.cpp +++ b/string/obsolete/nsString2.cpp @@ -2286,6 +2286,15 @@ nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString( Append(aString,aLength); } +nsAutoString::nsAutoString( const nsString& aString ) + : nsString() +{ + Initialize(*this, mBuffer, (sizeof(mBuffer)>>eTwoByte)-1, 0, eTwoByte, PR_FALSE); + AddNullTerminator(*this); + Append(aString); +} + + /** * constructor that uses external buffer * @param aBuffer describes the external buffer diff --git a/string/obsolete/nsString2.h b/string/obsolete/nsString2.h index 835d808bfb2..e586a9f0d8b 100644 --- a/string/obsolete/nsString2.h +++ b/string/obsolete/nsString2.h @@ -35,6 +35,10 @@ 4. Subsumable strings ***********************************************************************/ +#ifndef NEW_STRING_APIS +//#define NEW_STRING_APIS 1 +#endif //NEW_STRING_APIS + #ifndef _nsString_ #define _nsString_ @@ -898,6 +902,7 @@ public: virtual ~nsAutoString(); nsAutoString(); nsAutoString(const nsAutoString& aString); + nsAutoString(const nsString& aString); nsAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsAutoString(PRUnichar aChar); nsAutoString(const CBufDescriptor& aBuffer); diff --git a/string/public/nsAReadableString.h b/string/public/nsAReadableString.h index 211cbe8f37e..15a98c9cf2f 100644 --- a/string/public/nsAReadableString.h +++ b/string/public/nsAReadableString.h @@ -504,7 +504,7 @@ template PRUint32 basic_nsAReadableString::CountChar( CharT c ) const { -#if 1 +#if 0 return PRUint32(count(BeginReading(), EndReading(), c)); #else PRUint32 result = 0; @@ -679,7 +679,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs ) const { - return ::Compare(*this, NS_STATIC_CAST(basic_nsAReadableString, basic_nsLiteralString(rhs))); + return ::Compare(*this, NS_STATIC_CAST(const basic_nsAReadableString&, basic_nsLiteralString(rhs))); } template @@ -687,7 +687,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs, PRUint32 rhs_length ) const { - return ::Compare(*this, NS_STATIC_CAST(basic_nsAReadableString, basic_nsLiteralString(rhs, rhs_length))); + return ::Compare(*this, NS_STATIC_CAST(const basic_nsAReadableString&, basic_nsLiteralString(rhs, rhs_length))); } diff --git a/string/public/nsCharTraits.h b/string/public/nsCharTraits.h index e52103dbbf1..c5ee480dc72 100644 --- a/string/public/nsCharTraits.h +++ b/string/public/nsCharTraits.h @@ -487,6 +487,8 @@ struct nsCharSourceTraits } }; +#ifdef HAVE_CPP_PARTIAL_SPECIALIZATION + template struct nsCharSourceTraits { @@ -513,6 +515,37 @@ struct nsCharSourceTraits } }; +#else + +NS_SPECIALIZE_TEMPLATE +struct nsCharSourceTraits + { + static + PRUint32 + readable_size( const char* s ) + { + return PRUint32(nsCharTraits::length(s)); +// return numeric_limits::max(); + } + + static + PRUint32 + readable_size( const char* first, const char* last ) + { + return PRUint32(last-first); + } + + static + const char* + read( const char* s ) + { + return s; + } + }; + +// Add specialization for |PRUnichar| only if it is needed + +#endif template @@ -526,6 +559,8 @@ struct nsCharSinkTraits } }; +#ifdef HAVE_CPP_PARTIAL_SPECIALIZATION + template struct nsCharSinkTraits { @@ -539,5 +574,34 @@ struct nsCharSinkTraits } }; +#else + +NS_SPECIALIZE_TEMPLATE +struct nsCharSinkTraits + { + static + PRUint32 + write( char*& iter, const char* s, PRUint32 n ) + { + nsCharTraits::copy(iter, s, n); + iter += n; + return n; + } + }; + +NS_SPECIALIZE_TEMPLATE +struct nsCharSinkTraits + { + static + PRUint32 + write( PRUnichar*& iter, const PRUnichar* s, PRUint32 n ) + { + nsCharTraits::copy(iter, s, n); + iter += n; + return n; + } + }; + +#endif #endif // !defined(_nsCharTraits_h__) diff --git a/xpcom/base/nscore.h b/xpcom/base/nscore.h index 58a25a49dc5..e451ef4f0de 100644 --- a/xpcom/base/nscore.h +++ b/xpcom/base/nscore.h @@ -162,6 +162,7 @@ typedef PRUint16 PRUnichar; /* under Metrowerks (Mac), we don't have autoconf yet */ #ifdef __MWERKS__ #define HAVE_CPP_SPECIALIZATION + #define HAVE_CPP_PARTIAL_SPECIALIZATION #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX #define HAVE_ACCESS_CHANGING_CPP_USING diff --git a/xpcom/ds/nsAReadableString.h b/xpcom/ds/nsAReadableString.h index 211cbe8f37e..15a98c9cf2f 100644 --- a/xpcom/ds/nsAReadableString.h +++ b/xpcom/ds/nsAReadableString.h @@ -504,7 +504,7 @@ template PRUint32 basic_nsAReadableString::CountChar( CharT c ) const { -#if 1 +#if 0 return PRUint32(count(BeginReading(), EndReading(), c)); #else PRUint32 result = 0; @@ -679,7 +679,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs ) const { - return ::Compare(*this, NS_STATIC_CAST(basic_nsAReadableString, basic_nsLiteralString(rhs))); + return ::Compare(*this, NS_STATIC_CAST(const basic_nsAReadableString&, basic_nsLiteralString(rhs))); } template @@ -687,7 +687,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs, PRUint32 rhs_length ) const { - return ::Compare(*this, NS_STATIC_CAST(basic_nsAReadableString, basic_nsLiteralString(rhs, rhs_length))); + return ::Compare(*this, NS_STATIC_CAST(const basic_nsAReadableString&, basic_nsLiteralString(rhs, rhs_length))); } diff --git a/xpcom/ds/nsCharTraits.h b/xpcom/ds/nsCharTraits.h index e52103dbbf1..c5ee480dc72 100644 --- a/xpcom/ds/nsCharTraits.h +++ b/xpcom/ds/nsCharTraits.h @@ -487,6 +487,8 @@ struct nsCharSourceTraits } }; +#ifdef HAVE_CPP_PARTIAL_SPECIALIZATION + template struct nsCharSourceTraits { @@ -513,6 +515,37 @@ struct nsCharSourceTraits } }; +#else + +NS_SPECIALIZE_TEMPLATE +struct nsCharSourceTraits + { + static + PRUint32 + readable_size( const char* s ) + { + return PRUint32(nsCharTraits::length(s)); +// return numeric_limits::max(); + } + + static + PRUint32 + readable_size( const char* first, const char* last ) + { + return PRUint32(last-first); + } + + static + const char* + read( const char* s ) + { + return s; + } + }; + +// Add specialization for |PRUnichar| only if it is needed + +#endif template @@ -526,6 +559,8 @@ struct nsCharSinkTraits } }; +#ifdef HAVE_CPP_PARTIAL_SPECIALIZATION + template struct nsCharSinkTraits { @@ -539,5 +574,34 @@ struct nsCharSinkTraits } }; +#else + +NS_SPECIALIZE_TEMPLATE +struct nsCharSinkTraits + { + static + PRUint32 + write( char*& iter, const char* s, PRUint32 n ) + { + nsCharTraits::copy(iter, s, n); + iter += n; + return n; + } + }; + +NS_SPECIALIZE_TEMPLATE +struct nsCharSinkTraits + { + static + PRUint32 + write( PRUnichar*& iter, const PRUnichar* s, PRUint32 n ) + { + nsCharTraits::copy(iter, s, n); + iter += n; + return n; + } + }; + +#endif #endif // !defined(_nsCharTraits_h__) diff --git a/xpcom/ds/nsString.h b/xpcom/ds/nsString.h index 40024b58fa4..df7da3cbb42 100644 --- a/xpcom/ds/nsString.h +++ b/xpcom/ds/nsString.h @@ -34,6 +34,9 @@ 4. Subsumable strings ***********************************************************************/ +#ifndef NEW_STRING_APIS +//#define NEW_STRING_APIS 1 +#endif //NEW_STRING_APIS #ifndef _nsCString_ #define _nsCString_ diff --git a/xpcom/ds/nsString2.cpp b/xpcom/ds/nsString2.cpp index ab155aa18bb..954f0f8d008 100644 --- a/xpcom/ds/nsString2.cpp +++ b/xpcom/ds/nsString2.cpp @@ -2286,6 +2286,15 @@ nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString( Append(aString,aLength); } +nsAutoString::nsAutoString( const nsString& aString ) + : nsString() +{ + Initialize(*this, mBuffer, (sizeof(mBuffer)>>eTwoByte)-1, 0, eTwoByte, PR_FALSE); + AddNullTerminator(*this); + Append(aString); +} + + /** * constructor that uses external buffer * @param aBuffer describes the external buffer diff --git a/xpcom/ds/nsString2.h b/xpcom/ds/nsString2.h index 835d808bfb2..e586a9f0d8b 100644 --- a/xpcom/ds/nsString2.h +++ b/xpcom/ds/nsString2.h @@ -35,6 +35,10 @@ 4. Subsumable strings ***********************************************************************/ +#ifndef NEW_STRING_APIS +//#define NEW_STRING_APIS 1 +#endif //NEW_STRING_APIS + #ifndef _nsString_ #define _nsString_ @@ -898,6 +902,7 @@ public: virtual ~nsAutoString(); nsAutoString(); nsAutoString(const nsAutoString& aString); + nsAutoString(const nsString& aString); nsAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsAutoString(PRUnichar aChar); nsAutoString(const CBufDescriptor& aBuffer); diff --git a/xpcom/string/obsolete/nsString.h b/xpcom/string/obsolete/nsString.h index 40024b58fa4..df7da3cbb42 100644 --- a/xpcom/string/obsolete/nsString.h +++ b/xpcom/string/obsolete/nsString.h @@ -34,6 +34,9 @@ 4. Subsumable strings ***********************************************************************/ +#ifndef NEW_STRING_APIS +//#define NEW_STRING_APIS 1 +#endif //NEW_STRING_APIS #ifndef _nsCString_ #define _nsCString_ diff --git a/xpcom/string/obsolete/nsString2.cpp b/xpcom/string/obsolete/nsString2.cpp index ab155aa18bb..954f0f8d008 100644 --- a/xpcom/string/obsolete/nsString2.cpp +++ b/xpcom/string/obsolete/nsString2.cpp @@ -2286,6 +2286,15 @@ nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString( Append(aString,aLength); } +nsAutoString::nsAutoString( const nsString& aString ) + : nsString() +{ + Initialize(*this, mBuffer, (sizeof(mBuffer)>>eTwoByte)-1, 0, eTwoByte, PR_FALSE); + AddNullTerminator(*this); + Append(aString); +} + + /** * constructor that uses external buffer * @param aBuffer describes the external buffer diff --git a/xpcom/string/obsolete/nsString2.h b/xpcom/string/obsolete/nsString2.h index 835d808bfb2..e586a9f0d8b 100644 --- a/xpcom/string/obsolete/nsString2.h +++ b/xpcom/string/obsolete/nsString2.h @@ -35,6 +35,10 @@ 4. Subsumable strings ***********************************************************************/ +#ifndef NEW_STRING_APIS +//#define NEW_STRING_APIS 1 +#endif //NEW_STRING_APIS + #ifndef _nsString_ #define _nsString_ @@ -898,6 +902,7 @@ public: virtual ~nsAutoString(); nsAutoString(); nsAutoString(const nsAutoString& aString); + nsAutoString(const nsString& aString); nsAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsAutoString(PRUnichar aChar); nsAutoString(const CBufDescriptor& aBuffer); diff --git a/xpcom/string/public/nsAReadableString.h b/xpcom/string/public/nsAReadableString.h index 211cbe8f37e..15a98c9cf2f 100644 --- a/xpcom/string/public/nsAReadableString.h +++ b/xpcom/string/public/nsAReadableString.h @@ -504,7 +504,7 @@ template PRUint32 basic_nsAReadableString::CountChar( CharT c ) const { -#if 1 +#if 0 return PRUint32(count(BeginReading(), EndReading(), c)); #else PRUint32 result = 0; @@ -679,7 +679,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs ) const { - return ::Compare(*this, NS_STATIC_CAST(basic_nsAReadableString, basic_nsLiteralString(rhs))); + return ::Compare(*this, NS_STATIC_CAST(const basic_nsAReadableString&, basic_nsLiteralString(rhs))); } template @@ -687,7 +687,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs, PRUint32 rhs_length ) const { - return ::Compare(*this, NS_STATIC_CAST(basic_nsAReadableString, basic_nsLiteralString(rhs, rhs_length))); + return ::Compare(*this, NS_STATIC_CAST(const basic_nsAReadableString&, basic_nsLiteralString(rhs, rhs_length))); } diff --git a/xpcom/string/public/nsCharTraits.h b/xpcom/string/public/nsCharTraits.h index e52103dbbf1..c5ee480dc72 100644 --- a/xpcom/string/public/nsCharTraits.h +++ b/xpcom/string/public/nsCharTraits.h @@ -487,6 +487,8 @@ struct nsCharSourceTraits } }; +#ifdef HAVE_CPP_PARTIAL_SPECIALIZATION + template struct nsCharSourceTraits { @@ -513,6 +515,37 @@ struct nsCharSourceTraits } }; +#else + +NS_SPECIALIZE_TEMPLATE +struct nsCharSourceTraits + { + static + PRUint32 + readable_size( const char* s ) + { + return PRUint32(nsCharTraits::length(s)); +// return numeric_limits::max(); + } + + static + PRUint32 + readable_size( const char* first, const char* last ) + { + return PRUint32(last-first); + } + + static + const char* + read( const char* s ) + { + return s; + } + }; + +// Add specialization for |PRUnichar| only if it is needed + +#endif template @@ -526,6 +559,8 @@ struct nsCharSinkTraits } }; +#ifdef HAVE_CPP_PARTIAL_SPECIALIZATION + template struct nsCharSinkTraits { @@ -539,5 +574,34 @@ struct nsCharSinkTraits } }; +#else + +NS_SPECIALIZE_TEMPLATE +struct nsCharSinkTraits + { + static + PRUint32 + write( char*& iter, const char* s, PRUint32 n ) + { + nsCharTraits::copy(iter, s, n); + iter += n; + return n; + } + }; + +NS_SPECIALIZE_TEMPLATE +struct nsCharSinkTraits + { + static + PRUint32 + write( PRUnichar*& iter, const PRUnichar* s, PRUint32 n ) + { + nsCharTraits::copy(iter, s, n); + iter += n; + return n; + } + }; + +#endif #endif // !defined(_nsCharTraits_h__) diff --git a/xpcom/tests/PropertiesTest.cpp b/xpcom/tests/PropertiesTest.cpp index fa7636a80c5..9a85b705333 100644 --- a/xpcom/tests/PropertiesTest.cpp +++ b/xpcom/tests/PropertiesTest.cpp @@ -136,8 +136,8 @@ main(int argc, char* argv[]) char name[16]; name[0] = 0; sprintf(name, "%d", i); - nsAutoString v(""); - ret = props->GetStringProperty(name, v); + nsAutoString v; + ret = props->GetStringProperty(NS_ConvertASCIItoUCS2(name), v); if (NS_FAILED(ret) || (!v.Length())) { break; } diff --git a/xpcom/tests/StringFactoringTests/makefile.win b/xpcom/tests/StringFactoringTests/makefile.win new file mode 100644 index 00000000000..60e63ca1e56 --- /dev/null +++ b/xpcom/tests/StringFactoringTests/makefile.win @@ -0,0 +1,64 @@ +#!nmake +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): + +DEPTH=..\..\.. + +MAKE_OBJ_TYPE = EXE +PROGRAM = .\$(OBJDIR)\test_main.exe + +OBJS = \ + .\$(OBJDIR)\test_main.obj \ + .\$(OBJDIR)\nsString2.obj \ + .\$(OBJDIR)\nsStr.obj \ + .\$(OBJDIR)\nsString.obj \ + .\$(OBJDIR)\nsCRT.obj \ + .\$(OBJDIR)\nsAllocator.obj \ + .\$(OBJDIR)\nsDeque.obj \ + $(NULL) + +CPPSRCS = \ + test_main.cpp \ + $(DEPTH)\xpcom\nsString2.cpp \ + $(DEPTH)\xpcom\ds\nsStr.cpp \ + $(DEPTH)\xpcom\ds\nsString.cpp \ + $(DEPTH)\xpcom\ds\nsCRT.cpp \ + $(DEPTH)\xpcom\ds\nsAllocator.cpp \ + $(DEPTH)\xpcom\ds\nsDeque.cpp \ + $(NULL) + +LCFLAGS = \ + -DNEW_STRING_APIS -DSTANDALONE_STRING_TESTS -DNDEBUG -GX\ + $(DEFINES) \ + $(NULL) + +LINCS=-I$(PUBLIC)\xpcom \ + -I$(MOZ_TOOLS)\include \ + $(NULL) + +include <$(DEPTH)\config\rules.mak> + +export:: $(PROGRAM) + $(MAKE_INSTALL) $(PROGRAM) $(DIST)\bin + +clobber:: + rm -f $(DIST)\bin\test_main.exe + +$(PROGRAM):: $(OBJS) $(MYLIBS) diff --git a/xpcom/tests/StringFactoringTests/test_main.cpp b/xpcom/tests/StringFactoringTests/test_main.cpp index 55c89dbe27e..f653507fef6 100644 --- a/xpcom/tests/StringFactoringTests/test_main.cpp +++ b/xpcom/tests/StringFactoringTests/test_main.cpp @@ -1,6 +1,6 @@ #include -#define TEST_STD_STRING +//#define TEST_STD_STRING #include "nsString.h" diff --git a/xpcom/tests/TestAtoms.cpp b/xpcom/tests/TestAtoms.cpp index 3a865745905..0aa11bde836 100644 --- a/xpcom/tests/TestAtoms.cpp +++ b/xpcom/tests/TestAtoms.cpp @@ -47,7 +47,8 @@ int main(int argc, char** argv) if (nsnull == s) { break; } - nsAutoString sb(buf); + nsAutoString sb; + sb.AssignWithConversion(buf); strings[count++] = sb.ToNewUnicode(); sb.ToUpperCase(); strings[count++] = sb.ToNewUnicode(); diff --git a/xpcom/tests/TestCRT.cpp b/xpcom/tests/TestCRT.cpp index ff5b86e3757..d366b15c3c1 100644 Binary files a/xpcom/tests/TestCRT.cpp and b/xpcom/tests/TestCRT.cpp differ diff --git a/xpcom/tests/TestObserverService.cpp b/xpcom/tests/TestObserverService.cpp index 92d26f7f095..8e1b4c251e3 100644 --- a/xpcom/tests/TestObserverService.cpp +++ b/xpcom/tests/TestObserverService.cpp @@ -51,7 +51,7 @@ extern ostream &operator<<( ostream &s, nsString &str ) { class TestObserver : public nsIObserver, public nsSupportsWeakReference { public: - TestObserver( const nsString &name = "unknown" ) + TestObserver( const nsString &name = NS_ConvertASCIItoUCS2("unknown") ) : mName( name ) { NS_INIT_REFCNT(); } @@ -82,8 +82,8 @@ TestObserver::Observe( nsISupports *aSubject, int main(int argc, char *argv[]) { - nsString topicA( "topic-A" ); - nsString topicB( "topic-B" ); + nsString topicA; topicA.AssignWithConversion( "topic-A" ); + nsString topicB; topicB.AssignWithConversion( "topic-B" ); nsresult rv; nsresult res = nsComponentManager::CreateInstance(NS_OBSERVERSERVICE_PROGID, @@ -94,9 +94,9 @@ int main(int argc, char *argv[]) if (res == NS_OK) { nsIObserver *anObserver; - nsIObserver *aObserver = new TestObserver("Observer-A"); + nsIObserver *aObserver = new TestObserver(NS_ConvertASCIItoUCS2("Observer-A")); aObserver->AddRef(); - nsIObserver *bObserver = new TestObserver("Observer-B"); + nsIObserver *bObserver = new TestObserver(NS_ConvertASCIItoUCS2("Observer-B")); bObserver->AddRef(); cout << "Adding Observer-A as observer of topic-A..." << endl; @@ -114,13 +114,13 @@ int main(int argc, char *argv[]) cout << "Testing Notify(observer-A, topic-A)..." << endl; rv = anObserverService->Notify( aObserver, topicA.GetUnicode(), - nsString("Testing Notify(observer-A, topic-A)").GetUnicode() ); + NS_ConvertASCIItoUCS2("Testing Notify(observer-A, topic-A)").GetUnicode() ); testResult(rv); cout << "Testing Notify(observer-B, topic-B)..." << endl; rv = anObserverService->Notify( bObserver, topicB.GetUnicode(), - nsString("Testing Notify(observer-B, topic-B)").GetUnicode() ); + NS_ConvertASCIItoUCS2("Testing Notify(observer-B, topic-B)").GetUnicode() ); testResult(rv); cout << "Testing EnumerateObserverList (for topic-A)..." << endl; @@ -138,7 +138,7 @@ int main(int argc, char *argv[]) rv = inst->QueryInterface(NS_GET_IID(nsIObserver),(void**)&anObserver); cout << "Calling observe on enumerated observer " << NS_REINTERPRET_CAST(TestObserver*, NS_REINTERPRET_CAST(void*, inst))->mName << "..." << endl; - rv = anObserver->Observe( inst, topicA.GetUnicode(), nsString("during enumeration").GetUnicode() ); + rv = anObserver->Observe( inst, topicA.GetUnicode(), NS_ConvertASCIItoUCS2("during enumeration").GetUnicode() ); testResult(rv); } }