diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index a8e6f461115..55961a934bc 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -59,7 +59,10 @@ //static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1."; static const PRUnichar gCommonEmptyBuffer[1] = {0}; + +#ifdef NS_STR_STATS static PRBool gStringAcquiredMemory = PR_TRUE; +#endif /** * This method initializes all the members of the nsStr structure @@ -71,9 +74,9 @@ static PRBool gStringAcquiredMemory = PR_TRUE; void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { aDest.mStr=(char*)gCommonEmptyBuffer; aDest.mLength=0; - aDest.mCapacity=0; - aDest.mCharSize=aCharSize; - aDest.mOwnsBuffer=0; + aDest.SetInternalCapacity(0); + aDest.SetCharSize(aCharSize); + aDest.SetOwnsBuffer(PR_FALSE); } /** @@ -85,12 +88,11 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){ aDest.mStr=(aCString) ? aCString : (char*)gCommonEmptyBuffer; aDest.mLength=aLength; - aDest.mCapacity=aCapacity; - aDest.mCharSize=aCharSize; - aDest.mOwnsBuffer=aOwnsBuffer; + aDest.SetInternalCapacity(aCapacity); + aDest.SetCharSize(aCharSize); + aDest.SetOwnsBuffer(aOwnsBuffer); } - /** * This member destroys the memory buffer owned by an nsStr object (if it actually owns it) * @update gess10/30/98 @@ -113,7 +115,7 @@ void nsStr::Destroy(nsStr& aDest) { */ PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength) { PRBool result=PR_TRUE; - if(aNewLength>aString.mCapacity) { + if(aNewLength>aString.GetCapacity()) { result=Realloc(aString,aNewLength); if(aString.mStr) AddNullTerminator(aString); @@ -130,19 +132,17 @@ PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength) { */ PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength) { PRBool result=PR_TRUE; - if(aNewLength>aDest.mCapacity) { + if(aNewLength>aDest.GetCapacity()) { nsStr theTempStr; - nsStr::Initialize(theTempStr,eCharSize(aDest.mCharSize)); + nsStr::Initialize(theTempStr,eCharSize(aDest.GetCharSize())); -#ifndef NS_USE_OLD_STRING_ALLOCATION_STRATEGY // the new strategy is, allocate exact size, double on grows - if ( aDest.mCapacity ) { - PRUint32 newCapacity = aDest.mCapacity; + if ( aDest.GetCapacity() ) { + PRUint32 newCapacity = aDest.GetCapacity(); while ( newCapacity < aNewLength ) newCapacity <<= 1; aNewLength = newCapacity; } -#endif result=EnsureCapacity(theTempStr,aNewLength); if(result) { @@ -153,8 +153,8 @@ PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength) { aDest.mStr = theTempStr.mStr; theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... aDest.mLength=theTempStr.mLength; - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + aDest.SetInternalCapacity(theTempStr.GetCapacity()); + aDest.SetOwnsBuffer(theTempStr.GetOwnsBuffer()); } } return result; @@ -189,13 +189,13 @@ void nsStr::StrAppend(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt3 if(0 aDest.mCapacity) { + if(aDest.mLength+theLength > aDest.GetCapacity()) { isBigEnough=GrowCapacity(aDest,aDest.mLength+theLength); } if(isBigEnough) { //now append new chars, starting at offset - (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); + (*gCopyChars[aSource.GetCharSize()][aDest.GetCharSize()])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); aDest.mLength+=theLength; AddNullTerminator(aDest); @@ -227,7 +227,7 @@ PRInt32 nsStr::GetSegmentLength(const nsStr& aSource, void nsStr::AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSource, PRUint32 aSrcOffset, PRInt32 theLength) { nsStr theTempStr; - nsStr::Initialize(theTempStr,eCharSize(aDest.mCharSize)); + nsStr::Initialize(theTempStr,eCharSize(aDest.GetCharSize())); PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength); //grow the temp buffer to the right size @@ -246,14 +246,14 @@ void nsStr::AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSo Free(aDest); aDest.mStr = theTempStr.mStr; theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + aDest.SetInternalCapacity(theTempStr.GetCapacity()); + aDest.SetOwnsBuffer(theTempStr.GetOwnsBuffer()); } } void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -267,7 +267,7 @@ void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { //shift the chars right by theDelta... @@ -291,8 +291,8 @@ void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou } void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -306,7 +306,7 @@ void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { //shift the chars right by theDelta... @@ -330,8 +330,8 @@ void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou } void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -345,7 +345,7 @@ void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { //shift the chars right by theDelta... @@ -369,8 +369,8 @@ void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou } void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 1 byte"); - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -384,7 +384,7 @@ void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { @@ -419,7 +419,7 @@ void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou void nsStr::Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){ - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); if(aDestOffsetaSource=1 */ PRInt32 nsStr::StrCompare1To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); PRInt32 result = Compare1To1(aDest.mStr, aSource.mStr, theCount, aIgnoreCase); @@ -1149,8 +1149,8 @@ PRInt32 nsStr::StrCompare1To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aC PRInt32 nsStr::StrCompare1To2(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 2 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); PRInt32 result = Compare1To2(aDest.mStr, aSource.mUStr, theCount, aIgnoreCase); @@ -1162,8 +1162,8 @@ PRInt32 nsStr::StrCompare1To2(const nsStr& aDest,const nsStr& aSource,PRInt32 aC } PRInt32 nsStr::StrCompare2To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); @@ -1176,8 +1176,8 @@ PRInt32 nsStr::StrCompare2To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aC } PRInt32 nsStr::StrCompare2To2(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 2 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); @@ -1202,49 +1202,36 @@ void nsStr::Overwrite(nsStr& aDest,const nsStr& aSource,PRInt32 aDestOffset) { if((aDest.mLength-aDestOffset)>=aSource.mLength) { //if you're here, then both dest and source have valid lengths //and there's enough room in dest (at offset) to contain source. - (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDestOffset,aSource.mStr,0,aSource.mLength); + (*gCopyChars[aSource.GetCharSize()][aDest.GetCharSize()])(aDest.mStr,aDestOffset,aSource.mStr,0,aSource.mLength); } } } //---------------------------------------------------------------------------------------- - +// allocate the given bytes, not including the null terminator PRBool nsStr::Alloc(nsStr& aDest,PRUint32 aCount) { - static int mAllocCount=0; - mAllocCount++; + // the new strategy is, allocate exact size, double on grows + aDest.SetInternalCapacity(aCount); + aDest.mStr = (char*)nsMemory::Alloc((aCount+1)<> NSSTR_CHARSIZE_BIT); + } private: + + inline void SetInternalCapacity(PRUint32 aCapacity) { + mCapacityAndFlags = + ((mCapacityAndFlags & ~NSSTR_CAPACITY_MASK) | + (aCapacity & NSSTR_CAPACITY_MASK)); + } + + inline void SetCharSize(eCharSize aCharSize) { + mCapacityAndFlags = + ((mCapacityAndFlags & ~NSSTR_CHARSIZE_MASK) | + (PRUint32(aCharSize) << NSSTR_CHARSIZE_BIT)); + } + + inline void SetOwnsBuffer(PRBool aOwnsBuffer) { + mCapacityAndFlags = + (mCapacityAndFlags & ~NSSTR_OWNSBUFFER_MASK | + (aOwnsBuffer ? 1 : 0) << NSSTR_OWNSBUFFER_BIT); + } + static PRBool Alloc(nsStr& aString,PRUint32 aCount); static PRBool Realloc(nsStr& aString,PRUint32 aCount); static PRBool Free(nsStr& aString); public: + friend NS_COM + char* + ToNewUTF8String( const nsAString& aSource ); friend inline void AddNullTerminator(nsStr& aDest); friend inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex); friend class nsString; @@ -523,7 +560,7 @@ inline PRInt32 MaxInt(PRInt32 anInt1,PRInt32 anInt2){ } inline void AddNullTerminator(nsStr& aDest) { - if(eTwoByte==aDest.mCharSize) + if(eTwoByte==aDest.GetCharSize()) aDest.mUStr[aDest.mLength]=0; else aDest.mStr[aDest.mLength]=0; } @@ -547,7 +584,7 @@ inline void Recycle( PRUnichar* aBuffer) { nsMemory::Free(aBuffer); } */ inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex) { if(anIndex mCapacity ) + if ( anIndex > GetCapacity() ) SetCapacity(anIndex); // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAWritableString.h), // we can only use it since our local implementation, |nsCString::SetCapacity|, is known to do what we want @@ -173,7 +173,7 @@ nsCString::SetCapacity( PRUint32 aNewCapacity ) { if ( aNewCapacity ) { - if( aNewCapacity > mCapacity ) + if( aNewCapacity > GetCapacity() ) GrowCapacity(*this,aNewCapacity); AddNullTerminator(*this); } @@ -1374,7 +1374,7 @@ nsCAutoString::~nsCAutoString(){ #ifdef DEBUG void nsCAutoString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { - *aResult = sizeof(*this) + mCapacity * mCharSize; + *aResult = sizeof(*this) + GetCapacity() * GetCharSize(); } } #endif diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index 86252a70beb..0d3db8eb706 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -155,7 +155,7 @@ nsString::nsString( const nsAString& aReadable ) { #ifdef DEBUG void nsString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { - *aResult = sizeof(*this) + mCapacity * mCharSize; + *aResult = sizeof(*this) + GetCapacity() * GetCharSize(); } } #endif @@ -168,7 +168,7 @@ void nsString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { * @return nada */ void nsString::SetLength(PRUint32 anIndex) { - if ( anIndex > mCapacity ) + if ( anIndex > GetCapacity() ) SetCapacity(anIndex); // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAWritableString.h), // we can only use it since our local implementation, |nsString::SetCapacity|, is known to do what we want @@ -187,7 +187,7 @@ nsString::SetCapacity( PRUint32 aNewCapacity ) { if ( aNewCapacity ) { - if( aNewCapacity > mCapacity ) + if( aNewCapacity > GetCapacity() ) GrowCapacity(*this, aNewCapacity); AddNullTerminator(*this); } @@ -212,7 +212,7 @@ nsString::SetCapacity( PRUint32 aNewCapacity ) * @return ptr to internal (2-byte) buffer; */ const PRUnichar* nsString::get() const { - const PRUnichar* result=(eOneByte==mCharSize) ? 0 : mUStr; + const PRUnichar* result=(eOneByte==GetCharSize()) ? 0 : mUStr; return result; } @@ -225,7 +225,7 @@ const PRUnichar* nsString::get() const { PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex 0x7F) since char is signed @@ -1525,125 +1525,6 @@ class CalculateUTF8Length PRBool mErrorEncountered; }; -class ConvertUTF8toUCS2 - { - public: - typedef nsACString::char_type value_type; - typedef nsAString::char_type buffer_type; - - ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {} - - size_t Length() const { return mBuffer - mStart; } - - PRUint32 write( const value_type* start, PRUint32 N ) - { - // algorithm assumes utf8 units won't - // be spread across fragments - const value_type* p = start; - const value_type* end = start + N; - for ( ; p != end /* && *p */; ) - { - char c = *p++; - - if ( UTF8traits::isASCII(c) ) - { - *mBuffer++ = buffer_type(c); - continue; - } - - PRUint32 ucs4; - PRUint32 minUcs4; - PRInt32 state = 0; - - if ( UTF8traits::is2byte(c) ) - { - ucs4 = (PRUint32(c) << 6) & 0x000007C0L; - state = 1; - minUcs4 = 0x00000080; - } - else if ( UTF8traits::is3byte(c) ) - { - ucs4 = (PRUint32(c) << 12) & 0x0000F000L; - state = 2; - minUcs4 = 0x00000800; - } - else if ( UTF8traits::is4byte(c) ) - { - ucs4 = (PRUint32(c) << 18) & 0x001F0000L; - state = 3; - minUcs4 = 0x00010000; - } - else if ( UTF8traits::is5byte(c) ) - { - ucs4 = (PRUint32(c) << 24) & 0x03000000L; - state = 4; - minUcs4 = 0x00200000; - } - else if ( UTF8traits::is6byte(c) ) - { - ucs4 = (PRUint32(c) << 30) & 0x40000000L; - state = 5; - minUcs4 = 0x04000000; - } - else - { - NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings."); - break; - } - - while ( state-- ) - { - c = *p++; - - if ( UTF8traits::isInSeq(c) ) - { - PRInt32 shift = state * 6; - ucs4 |= (PRUint32(c) & 0x3F) << shift; - } - else - { - NS_ERROR("not a UTF8 string"); - return p - start; - } - } - - if ( ucs4 < minUcs4 ) - { - // Overlong sequence - *mBuffer++ = 0xFFFD; - } - else if ( ucs4 <= 0xD7FF ) - { - *mBuffer++ = ucs4; - } - else if ( /* ucs4 >= 0xD800 && */ ucs4 <= 0xDFFF ) - { - // Surrogates - *mBuffer++ = 0xFFFD; - } - else if ( ucs4 == 0xFFFE || ucs4 == 0xFFFF ) - { - // Prohibited characters - *mBuffer++ = 0xFFFD; - } - else if ( ucs4 >= 0x00010000 ) - { - *mBuffer++ = 0xFFFD; - } - else - { - if ( ucs4 != 0xFEFF ) // ignore BOM - *mBuffer++ = ucs4; - } - } - return p - start; - } - - private: - buffer_type* mStart; - buffer_type* mBuffer; - }; - void NS_ConvertUTF8toUCS2::Init( const nsACString& aCString ) { @@ -1668,7 +1549,7 @@ NS_ConvertUTF8toUCS2::Init( const nsACString& aCString ) ConvertUTF8toUCS2 converter(mUStr); copy_string(aCString.BeginReading(start), aCString.EndReading(end), converter); mLength = converter.Length(); - if (mCapacity) + if (GetCapacity()) mUStr[mLength] = '\0'; // null terminate } @@ -1705,7 +1586,7 @@ nsAutoString::~nsAutoString(){ #ifdef DEBUG void nsAutoString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { - *aResult = sizeof(*this) + mCapacity * mCharSize; + *aResult = sizeof(*this) + GetCapacity() * GetCharSize(); } } #endif diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index e00a19f8148..4f8306f4099 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -602,6 +602,125 @@ class NS_COM NS_ConvertUTF8toUCS2 NS_ConvertUTF8toUCS2( PRUnichar ); }; +class ConvertUTF8toUCS2 + { + public: + typedef nsACString::char_type value_type; + typedef nsAString::char_type buffer_type; + + ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {} + + size_t Length() const { return mBuffer - mStart; } + + PRUint32 write( const value_type* start, PRUint32 N ) + { + // algorithm assumes utf8 units won't + // be spread across fragments + const value_type* p = start; + const value_type* end = start + N; + for ( ; p != end /* && *p */; ) + { + char c = *p++; + + if ( UTF8traits::isASCII(c) ) + { + *mBuffer++ = buffer_type(c); + continue; + } + + PRUint32 ucs4; + PRUint32 minUcs4; + PRInt32 state = 0; + + if ( UTF8traits::is2byte(c) ) + { + ucs4 = (PRUint32(c) << 6) & 0x000007C0L; + state = 1; + minUcs4 = 0x00000080; + } + else if ( UTF8traits::is3byte(c) ) + { + ucs4 = (PRUint32(c) << 12) & 0x0000F000L; + state = 2; + minUcs4 = 0x00000800; + } + else if ( UTF8traits::is4byte(c) ) + { + ucs4 = (PRUint32(c) << 18) & 0x001F0000L; + state = 3; + minUcs4 = 0x00010000; + } + else if ( UTF8traits::is5byte(c) ) + { + ucs4 = (PRUint32(c) << 24) & 0x03000000L; + state = 4; + minUcs4 = 0x00200000; + } + else if ( UTF8traits::is6byte(c) ) + { + ucs4 = (PRUint32(c) << 30) & 0x40000000L; + state = 5; + minUcs4 = 0x04000000; + } + else + { + NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings."); + break; + } + + while ( state-- ) + { + c = *p++; + + if ( UTF8traits::isInSeq(c) ) + { + PRInt32 shift = state * 6; + ucs4 |= (PRUint32(c) & 0x3F) << shift; + } + else + { + NS_ERROR("not a UTF8 string"); + return p - start; + } + } + + if ( ucs4 < minUcs4 ) + { + // Overlong sequence + *mBuffer++ = 0xFFFD; + } + else if ( ucs4 <= 0xD7FF ) + { + *mBuffer++ = ucs4; + } + else if ( /* ucs4 >= 0xD800 && */ ucs4 <= 0xDFFF ) + { + // Surrogates + *mBuffer++ = 0xFFFD; + } + else if ( ucs4 == 0xFFFE || ucs4 == 0xFFFF ) + { + // Prohibited characters + *mBuffer++ = 0xFFFD; + } + else if ( ucs4 >= 0x00010000 ) + { + *mBuffer++ = 0xFFFD; + } + else + { + if ( ucs4 != 0xFEFF ) // ignore BOM + *mBuffer++ = ucs4; + } + } + return p - start; + } + + private: + buffer_type* mStart; + buffer_type* mBuffer; + }; + #endif diff --git a/mozilla/string/src/nsReadableUtils.cpp b/mozilla/string/src/nsReadableUtils.cpp index 46b796a4892..c2c6cd2333d 100755 --- a/mozilla/string/src/nsReadableUtils.cpp +++ b/mozilla/string/src/nsReadableUtils.cpp @@ -211,13 +211,13 @@ ToNewUTF8String( const nsAString& aSource ) NS_ConvertUCS2toUTF8 temp(aSource); char* result; - if (temp.mOwnsBuffer) { + if (temp.GetOwnsBuffer()) { // We allocated. Trick the string into not freeing its buffer to // avoid an extra allocation. result = temp.mStr; temp.mStr=0; - temp.mOwnsBuffer = PR_FALSE; + temp.SetOwnsBuffer(PR_FALSE); } else { // We didn't allocate a buffer, so we need to copy it out of the @@ -385,7 +385,7 @@ void ToUpperCase( nsCString& aCString ) { ConvertToUpperCase converter; - converter.write(aCString.mStr, aCString.mLength); + converter.write(aCString.mStr, aCString.Length()); } /** @@ -479,7 +479,7 @@ void ToLowerCase( nsCString& aCString ) { ConvertToLowerCase converter; - converter.write(aCString.mStr, aCString.mLength); + converter.write(aCString.mStr, aCString.Length()); } /** diff --git a/mozilla/xpcom/string/obsolete/nsStr.cpp b/mozilla/xpcom/string/obsolete/nsStr.cpp index a8e6f461115..55961a934bc 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.cpp +++ b/mozilla/xpcom/string/obsolete/nsStr.cpp @@ -59,7 +59,10 @@ //static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1."; static const PRUnichar gCommonEmptyBuffer[1] = {0}; + +#ifdef NS_STR_STATS static PRBool gStringAcquiredMemory = PR_TRUE; +#endif /** * This method initializes all the members of the nsStr structure @@ -71,9 +74,9 @@ static PRBool gStringAcquiredMemory = PR_TRUE; void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { aDest.mStr=(char*)gCommonEmptyBuffer; aDest.mLength=0; - aDest.mCapacity=0; - aDest.mCharSize=aCharSize; - aDest.mOwnsBuffer=0; + aDest.SetInternalCapacity(0); + aDest.SetCharSize(aCharSize); + aDest.SetOwnsBuffer(PR_FALSE); } /** @@ -85,12 +88,11 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){ aDest.mStr=(aCString) ? aCString : (char*)gCommonEmptyBuffer; aDest.mLength=aLength; - aDest.mCapacity=aCapacity; - aDest.mCharSize=aCharSize; - aDest.mOwnsBuffer=aOwnsBuffer; + aDest.SetInternalCapacity(aCapacity); + aDest.SetCharSize(aCharSize); + aDest.SetOwnsBuffer(aOwnsBuffer); } - /** * This member destroys the memory buffer owned by an nsStr object (if it actually owns it) * @update gess10/30/98 @@ -113,7 +115,7 @@ void nsStr::Destroy(nsStr& aDest) { */ PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength) { PRBool result=PR_TRUE; - if(aNewLength>aString.mCapacity) { + if(aNewLength>aString.GetCapacity()) { result=Realloc(aString,aNewLength); if(aString.mStr) AddNullTerminator(aString); @@ -130,19 +132,17 @@ PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength) { */ PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength) { PRBool result=PR_TRUE; - if(aNewLength>aDest.mCapacity) { + if(aNewLength>aDest.GetCapacity()) { nsStr theTempStr; - nsStr::Initialize(theTempStr,eCharSize(aDest.mCharSize)); + nsStr::Initialize(theTempStr,eCharSize(aDest.GetCharSize())); -#ifndef NS_USE_OLD_STRING_ALLOCATION_STRATEGY // the new strategy is, allocate exact size, double on grows - if ( aDest.mCapacity ) { - PRUint32 newCapacity = aDest.mCapacity; + if ( aDest.GetCapacity() ) { + PRUint32 newCapacity = aDest.GetCapacity(); while ( newCapacity < aNewLength ) newCapacity <<= 1; aNewLength = newCapacity; } -#endif result=EnsureCapacity(theTempStr,aNewLength); if(result) { @@ -153,8 +153,8 @@ PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength) { aDest.mStr = theTempStr.mStr; theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... aDest.mLength=theTempStr.mLength; - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + aDest.SetInternalCapacity(theTempStr.GetCapacity()); + aDest.SetOwnsBuffer(theTempStr.GetOwnsBuffer()); } } return result; @@ -189,13 +189,13 @@ void nsStr::StrAppend(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt3 if(0 aDest.mCapacity) { + if(aDest.mLength+theLength > aDest.GetCapacity()) { isBigEnough=GrowCapacity(aDest,aDest.mLength+theLength); } if(isBigEnough) { //now append new chars, starting at offset - (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); + (*gCopyChars[aSource.GetCharSize()][aDest.GetCharSize()])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); aDest.mLength+=theLength; AddNullTerminator(aDest); @@ -227,7 +227,7 @@ PRInt32 nsStr::GetSegmentLength(const nsStr& aSource, void nsStr::AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSource, PRUint32 aSrcOffset, PRInt32 theLength) { nsStr theTempStr; - nsStr::Initialize(theTempStr,eCharSize(aDest.mCharSize)); + nsStr::Initialize(theTempStr,eCharSize(aDest.GetCharSize())); PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength); //grow the temp buffer to the right size @@ -246,14 +246,14 @@ void nsStr::AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSo Free(aDest); aDest.mStr = theTempStr.mStr; theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + aDest.SetInternalCapacity(theTempStr.GetCapacity()); + aDest.SetOwnsBuffer(theTempStr.GetOwnsBuffer()); } } void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -267,7 +267,7 @@ void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { //shift the chars right by theDelta... @@ -291,8 +291,8 @@ void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou } void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -306,7 +306,7 @@ void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { //shift the chars right by theDelta... @@ -330,8 +330,8 @@ void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou } void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -345,7 +345,7 @@ void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { //shift the chars right by theDelta... @@ -369,8 +369,8 @@ void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou } void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 1 byte"); - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); //there are a few cases for insert: // 1. You're inserting chars into an empty string (assign) // 2. You're inserting onto the end of a string (append) @@ -384,7 +384,7 @@ void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou //here's the only new case we have to handle. //chars are really being inserted into our buffer... - if(aDest.mLength+theLength > aDest.mCapacity) + if(aDest.mLength+theLength > aDest.GetCapacity()) AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { @@ -419,7 +419,7 @@ void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSou void nsStr::Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){ - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); if(aDestOffsetaSource=1 */ PRInt32 nsStr::StrCompare1To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); PRInt32 result = Compare1To1(aDest.mStr, aSource.mStr, theCount, aIgnoreCase); @@ -1149,8 +1149,8 @@ PRInt32 nsStr::StrCompare1To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aC PRInt32 nsStr::StrCompare1To2(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 2 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); PRInt32 result = Compare1To2(aDest.mStr, aSource.mUStr, theCount, aIgnoreCase); @@ -1162,8 +1162,8 @@ PRInt32 nsStr::StrCompare1To2(const nsStr& aDest,const nsStr& aSource,PRInt32 aC } PRInt32 nsStr::StrCompare2To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); - NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eOneByte, "Must be 1 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); @@ -1176,8 +1176,8 @@ PRInt32 nsStr::StrCompare2To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aC } PRInt32 nsStr::StrCompare2To2(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); - NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.GetCharSize() == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aSource.GetCharSize() == eTwoByte, "Must be 2 byte"); if (aCount) { PRInt32 theCount = GetCompareCount(aDest.mLength, aSource.mLength, aCount); @@ -1202,49 +1202,36 @@ void nsStr::Overwrite(nsStr& aDest,const nsStr& aSource,PRInt32 aDestOffset) { if((aDest.mLength-aDestOffset)>=aSource.mLength) { //if you're here, then both dest and source have valid lengths //and there's enough room in dest (at offset) to contain source. - (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDestOffset,aSource.mStr,0,aSource.mLength); + (*gCopyChars[aSource.GetCharSize()][aDest.GetCharSize()])(aDest.mStr,aDestOffset,aSource.mStr,0,aSource.mLength); } } } //---------------------------------------------------------------------------------------- - +// allocate the given bytes, not including the null terminator PRBool nsStr::Alloc(nsStr& aDest,PRUint32 aCount) { - static int mAllocCount=0; - mAllocCount++; + // the new strategy is, allocate exact size, double on grows + aDest.SetInternalCapacity(aCount); + aDest.mStr = (char*)nsMemory::Alloc((aCount+1)<> NSSTR_CHARSIZE_BIT); + } private: + + inline void SetInternalCapacity(PRUint32 aCapacity) { + mCapacityAndFlags = + ((mCapacityAndFlags & ~NSSTR_CAPACITY_MASK) | + (aCapacity & NSSTR_CAPACITY_MASK)); + } + + inline void SetCharSize(eCharSize aCharSize) { + mCapacityAndFlags = + ((mCapacityAndFlags & ~NSSTR_CHARSIZE_MASK) | + (PRUint32(aCharSize) << NSSTR_CHARSIZE_BIT)); + } + + inline void SetOwnsBuffer(PRBool aOwnsBuffer) { + mCapacityAndFlags = + (mCapacityAndFlags & ~NSSTR_OWNSBUFFER_MASK | + (aOwnsBuffer ? 1 : 0) << NSSTR_OWNSBUFFER_BIT); + } + static PRBool Alloc(nsStr& aString,PRUint32 aCount); static PRBool Realloc(nsStr& aString,PRUint32 aCount); static PRBool Free(nsStr& aString); public: + friend NS_COM + char* + ToNewUTF8String( const nsAString& aSource ); friend inline void AddNullTerminator(nsStr& aDest); friend inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex); friend class nsString; @@ -523,7 +560,7 @@ inline PRInt32 MaxInt(PRInt32 anInt1,PRInt32 anInt2){ } inline void AddNullTerminator(nsStr& aDest) { - if(eTwoByte==aDest.mCharSize) + if(eTwoByte==aDest.GetCharSize()) aDest.mUStr[aDest.mLength]=0; else aDest.mStr[aDest.mLength]=0; } @@ -547,7 +584,7 @@ inline void Recycle( PRUnichar* aBuffer) { nsMemory::Free(aBuffer); } */ inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex) { if(anIndex mCapacity ) + if ( anIndex > GetCapacity() ) SetCapacity(anIndex); // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAWritableString.h), // we can only use it since our local implementation, |nsCString::SetCapacity|, is known to do what we want @@ -173,7 +173,7 @@ nsCString::SetCapacity( PRUint32 aNewCapacity ) { if ( aNewCapacity ) { - if( aNewCapacity > mCapacity ) + if( aNewCapacity > GetCapacity() ) GrowCapacity(*this,aNewCapacity); AddNullTerminator(*this); } @@ -1374,7 +1374,7 @@ nsCAutoString::~nsCAutoString(){ #ifdef DEBUG void nsCAutoString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { - *aResult = sizeof(*this) + mCapacity * mCharSize; + *aResult = sizeof(*this) + GetCapacity() * GetCharSize(); } } #endif diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index 86252a70beb..0d3db8eb706 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -155,7 +155,7 @@ nsString::nsString( const nsAString& aReadable ) { #ifdef DEBUG void nsString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { - *aResult = sizeof(*this) + mCapacity * mCharSize; + *aResult = sizeof(*this) + GetCapacity() * GetCharSize(); } } #endif @@ -168,7 +168,7 @@ void nsString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { * @return nada */ void nsString::SetLength(PRUint32 anIndex) { - if ( anIndex > mCapacity ) + if ( anIndex > GetCapacity() ) SetCapacity(anIndex); // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAWritableString.h), // we can only use it since our local implementation, |nsString::SetCapacity|, is known to do what we want @@ -187,7 +187,7 @@ nsString::SetCapacity( PRUint32 aNewCapacity ) { if ( aNewCapacity ) { - if( aNewCapacity > mCapacity ) + if( aNewCapacity > GetCapacity() ) GrowCapacity(*this, aNewCapacity); AddNullTerminator(*this); } @@ -212,7 +212,7 @@ nsString::SetCapacity( PRUint32 aNewCapacity ) * @return ptr to internal (2-byte) buffer; */ const PRUnichar* nsString::get() const { - const PRUnichar* result=(eOneByte==mCharSize) ? 0 : mUStr; + const PRUnichar* result=(eOneByte==GetCharSize()) ? 0 : mUStr; return result; } @@ -225,7 +225,7 @@ const PRUnichar* nsString::get() const { PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex 0x7F) since char is signed @@ -1525,125 +1525,6 @@ class CalculateUTF8Length PRBool mErrorEncountered; }; -class ConvertUTF8toUCS2 - { - public: - typedef nsACString::char_type value_type; - typedef nsAString::char_type buffer_type; - - ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {} - - size_t Length() const { return mBuffer - mStart; } - - PRUint32 write( const value_type* start, PRUint32 N ) - { - // algorithm assumes utf8 units won't - // be spread across fragments - const value_type* p = start; - const value_type* end = start + N; - for ( ; p != end /* && *p */; ) - { - char c = *p++; - - if ( UTF8traits::isASCII(c) ) - { - *mBuffer++ = buffer_type(c); - continue; - } - - PRUint32 ucs4; - PRUint32 minUcs4; - PRInt32 state = 0; - - if ( UTF8traits::is2byte(c) ) - { - ucs4 = (PRUint32(c) << 6) & 0x000007C0L; - state = 1; - minUcs4 = 0x00000080; - } - else if ( UTF8traits::is3byte(c) ) - { - ucs4 = (PRUint32(c) << 12) & 0x0000F000L; - state = 2; - minUcs4 = 0x00000800; - } - else if ( UTF8traits::is4byte(c) ) - { - ucs4 = (PRUint32(c) << 18) & 0x001F0000L; - state = 3; - minUcs4 = 0x00010000; - } - else if ( UTF8traits::is5byte(c) ) - { - ucs4 = (PRUint32(c) << 24) & 0x03000000L; - state = 4; - minUcs4 = 0x00200000; - } - else if ( UTF8traits::is6byte(c) ) - { - ucs4 = (PRUint32(c) << 30) & 0x40000000L; - state = 5; - minUcs4 = 0x04000000; - } - else - { - NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings."); - break; - } - - while ( state-- ) - { - c = *p++; - - if ( UTF8traits::isInSeq(c) ) - { - PRInt32 shift = state * 6; - ucs4 |= (PRUint32(c) & 0x3F) << shift; - } - else - { - NS_ERROR("not a UTF8 string"); - return p - start; - } - } - - if ( ucs4 < minUcs4 ) - { - // Overlong sequence - *mBuffer++ = 0xFFFD; - } - else if ( ucs4 <= 0xD7FF ) - { - *mBuffer++ = ucs4; - } - else if ( /* ucs4 >= 0xD800 && */ ucs4 <= 0xDFFF ) - { - // Surrogates - *mBuffer++ = 0xFFFD; - } - else if ( ucs4 == 0xFFFE || ucs4 == 0xFFFF ) - { - // Prohibited characters - *mBuffer++ = 0xFFFD; - } - else if ( ucs4 >= 0x00010000 ) - { - *mBuffer++ = 0xFFFD; - } - else - { - if ( ucs4 != 0xFEFF ) // ignore BOM - *mBuffer++ = ucs4; - } - } - return p - start; - } - - private: - buffer_type* mStart; - buffer_type* mBuffer; - }; - void NS_ConvertUTF8toUCS2::Init( const nsACString& aCString ) { @@ -1668,7 +1549,7 @@ NS_ConvertUTF8toUCS2::Init( const nsACString& aCString ) ConvertUTF8toUCS2 converter(mUStr); copy_string(aCString.BeginReading(start), aCString.EndReading(end), converter); mLength = converter.Length(); - if (mCapacity) + if (GetCapacity()) mUStr[mLength] = '\0'; // null terminate } @@ -1705,7 +1586,7 @@ nsAutoString::~nsAutoString(){ #ifdef DEBUG void nsAutoString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { - *aResult = sizeof(*this) + mCapacity * mCharSize; + *aResult = sizeof(*this) + GetCapacity() * GetCharSize(); } } #endif diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index e00a19f8148..4f8306f4099 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -602,6 +602,125 @@ class NS_COM NS_ConvertUTF8toUCS2 NS_ConvertUTF8toUCS2( PRUnichar ); }; +class ConvertUTF8toUCS2 + { + public: + typedef nsACString::char_type value_type; + typedef nsAString::char_type buffer_type; + + ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {} + + size_t Length() const { return mBuffer - mStart; } + + PRUint32 write( const value_type* start, PRUint32 N ) + { + // algorithm assumes utf8 units won't + // be spread across fragments + const value_type* p = start; + const value_type* end = start + N; + for ( ; p != end /* && *p */; ) + { + char c = *p++; + + if ( UTF8traits::isASCII(c) ) + { + *mBuffer++ = buffer_type(c); + continue; + } + + PRUint32 ucs4; + PRUint32 minUcs4; + PRInt32 state = 0; + + if ( UTF8traits::is2byte(c) ) + { + ucs4 = (PRUint32(c) << 6) & 0x000007C0L; + state = 1; + minUcs4 = 0x00000080; + } + else if ( UTF8traits::is3byte(c) ) + { + ucs4 = (PRUint32(c) << 12) & 0x0000F000L; + state = 2; + minUcs4 = 0x00000800; + } + else if ( UTF8traits::is4byte(c) ) + { + ucs4 = (PRUint32(c) << 18) & 0x001F0000L; + state = 3; + minUcs4 = 0x00010000; + } + else if ( UTF8traits::is5byte(c) ) + { + ucs4 = (PRUint32(c) << 24) & 0x03000000L; + state = 4; + minUcs4 = 0x00200000; + } + else if ( UTF8traits::is6byte(c) ) + { + ucs4 = (PRUint32(c) << 30) & 0x40000000L; + state = 5; + minUcs4 = 0x04000000; + } + else + { + NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings."); + break; + } + + while ( state-- ) + { + c = *p++; + + if ( UTF8traits::isInSeq(c) ) + { + PRInt32 shift = state * 6; + ucs4 |= (PRUint32(c) & 0x3F) << shift; + } + else + { + NS_ERROR("not a UTF8 string"); + return p - start; + } + } + + if ( ucs4 < minUcs4 ) + { + // Overlong sequence + *mBuffer++ = 0xFFFD; + } + else if ( ucs4 <= 0xD7FF ) + { + *mBuffer++ = ucs4; + } + else if ( /* ucs4 >= 0xD800 && */ ucs4 <= 0xDFFF ) + { + // Surrogates + *mBuffer++ = 0xFFFD; + } + else if ( ucs4 == 0xFFFE || ucs4 == 0xFFFF ) + { + // Prohibited characters + *mBuffer++ = 0xFFFD; + } + else if ( ucs4 >= 0x00010000 ) + { + *mBuffer++ = 0xFFFD; + } + else + { + if ( ucs4 != 0xFEFF ) // ignore BOM + *mBuffer++ = ucs4; + } + } + return p - start; + } + + private: + buffer_type* mStart; + buffer_type* mBuffer; + }; + #endif diff --git a/mozilla/xpcom/string/src/nsReadableUtils.cpp b/mozilla/xpcom/string/src/nsReadableUtils.cpp index 46b796a4892..c2c6cd2333d 100755 --- a/mozilla/xpcom/string/src/nsReadableUtils.cpp +++ b/mozilla/xpcom/string/src/nsReadableUtils.cpp @@ -211,13 +211,13 @@ ToNewUTF8String( const nsAString& aSource ) NS_ConvertUCS2toUTF8 temp(aSource); char* result; - if (temp.mOwnsBuffer) { + if (temp.GetOwnsBuffer()) { // We allocated. Trick the string into not freeing its buffer to // avoid an extra allocation. result = temp.mStr; temp.mStr=0; - temp.mOwnsBuffer = PR_FALSE; + temp.SetOwnsBuffer(PR_FALSE); } else { // We didn't allocate a buffer, so we need to copy it out of the @@ -385,7 +385,7 @@ void ToUpperCase( nsCString& aCString ) { ConvertToUpperCase converter; - converter.write(aCString.mStr, aCString.mLength); + converter.write(aCString.mStr, aCString.Length()); } /** @@ -479,7 +479,7 @@ void ToLowerCase( nsCString& aCString ) { ConvertToLowerCase converter; - converter.write(aCString.mStr, aCString.mLength); + converter.write(aCString.mStr, aCString.Length()); } /**