diff --git a/mozilla/xpcom/string/obsolete/Makefile.in b/mozilla/xpcom/string/obsolete/Makefile.in index a5f9f3ee570..227dc11fb6a 100644 --- a/mozilla/xpcom/string/obsolete/Makefile.in +++ b/mozilla/xpcom/string/obsolete/Makefile.in @@ -35,22 +35,14 @@ REQUIRES = \ xpcom \ $(NULL) -CPPSRCS = \ - nsTStringBaseObsolete.cpp \ +CPPSRCS = \ + nsStringObsolete.cpp \ $(NULL) -# nsStr.cpp \ -# nsString.cpp \ -# nsString2.cpp \ - EXPORTS = \ nsString2.h \ $(NULL) -# nsStr.h \ -# nsStrShared.h \ -# nsString.h \ - # we don't want the shared lib, but we want to force the creation of a # static lib. FORCE_STATIC_LIB = 1 diff --git a/mozilla/xpcom/string/obsolete/README.html b/mozilla/xpcom/string/obsolete/README.html deleted file mode 100644 index 145ef82872a..00000000000 --- a/mozilla/xpcom/string/obsolete/README.html +++ /dev/null @@ -1,35 +0,0 @@ - - - -

original string implementations soon to be replaced (the names you are using will still be good)

-

- These are the original string implementations by rickg and others. - Most of the code here will be made obsolete by the new shared-buffer string (see bug #53065). - For the most part, this change is intended to be transparent to clients. - The type names you are using for strings now, e.g., nsString, nsAutoString, nsXPIDLString, will still be good, - they will just refer to better, but compatible, implementations. - If you're interested in learning how strings work, you probably want to start with - nsAReadableString. -

- - diff --git a/mozilla/xpcom/string/obsolete/nsStr.cpp b/mozilla/xpcom/string/obsolete/nsStr.cpp deleted file mode 100644 index 357d34b06bf..00000000000 --- a/mozilla/xpcom/string/obsolete/nsStr.cpp +++ /dev/null @@ -1,1336 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * 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 the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Rick Gessner (original author) - * Scott Collins - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef nsCharTraits_h___ -#include "nsCharTraits.h" -#endif - -#include "nsStrPrivate.h" -#include "nsStr.h" -#include "bufferRoutines.h" -#include //only used for printf -#include "prdtoa.h" - -/****************************************************************************************** - MODULE NOTES: - - This file contains the nsStr data structure. - This general purpose buffer management class is used as the basis for our strings. - It's benefits include: - 1. An efficient set of library style functions for manipulating nsStrs - 2. Support for 1 and 2 byte character strings (which can easily be increased to n) - 3. Unicode awareness and interoperability. - -*******************************************************************************************/ - -//static const char* kCallFindChar = "For better performance, call FindChar() for targets whose length==1."; -//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 - * - * @update gess10/30/98 - * @param - * @return - */ -void nsStrPrivate::Initialize(nsStr& aDest,eCharSize aCharSize) { - aDest.mStr=(char*)gCommonEmptyBuffer; - aDest.mLength=0; - aDest.mCapacityAndFlags = 0; - // handled by mCapacityAndFlags - // aDest.SetInternalCapacity(0); - // aDest.SetOwnsBuffer(PR_FALSE); - aDest.SetCharSize(aCharSize); -} - -/** - * This method initializes all the members of the nsStr structure - * @update gess10/30/98 - * @param - * @return - */ -void nsStrPrivate::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){ - aDest.mStr=(aCString) ? aCString : (char*)gCommonEmptyBuffer; - aDest.mLength=aLength; - aDest.mCapacityAndFlags = 0; - 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 - * @param - * @return - */ -void nsStrPrivate::Destroy(nsStr& aDest) { - if((aDest.mStr) && (aDest.mStr!=(char*)gCommonEmptyBuffer)) { - Free(aDest); - } -} - - -/** - * This method gets called when the internal buffer needs - * to grow to a given size. The original contents are not preserved. - * @update gess 3/30/98 - * @param aNewLength -- new capacity of string in charSize units - * @return void - */ -PRBool nsStrPrivate::EnsureCapacity(nsStr& aString,PRUint32 aNewLength) { - PRBool result=PR_TRUE; - if(aNewLength>aString.GetCapacity()) { - result=Realloc(aString,aNewLength); - if(aString.mStr) - AddNullTerminator(aString); - } - return result; -} - -/** - * This method gets called when the internal buffer needs - * to grow to a given size. The original contents ARE preserved. - * @update gess 3/30/98 - * @param aNewLength -- new capacity of string in charSize units - * @return void - */ -PRBool nsStrPrivate::GrowCapacity(nsStr& aDest,PRUint32 aNewLength) { - PRBool result=PR_TRUE; - if(aNewLength>aDest.GetCapacity()) { - nsStr theTempStr; - nsStrPrivate::Initialize(theTempStr,eCharSize(aDest.GetCharSize())); - - // the new strategy is, allocate exact size, double on grows - if ( aDest.GetCapacity() ) { - PRUint32 newCapacity = aDest.GetCapacity(); - while ( newCapacity < aNewLength ) - newCapacity <<= 1; - aNewLength = newCapacity; - } - - result=EnsureCapacity(theTempStr,aNewLength); - if(result) { - if(aDest.mLength) { - StrAppend(theTempStr,aDest,0,aDest.mLength); - } - 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.mLength=theTempStr.mLength; - aDest.SetInternalCapacity(theTempStr.GetCapacity()); - aDest.SetOwnsBuffer(theTempStr.GetOwnsBuffer()); - } - } - return result; -} - -/** - * Replaces the contents of aDest with aSource, up to aCount of chars. - * @update gess10/30/98 - * @param aDest is the nsStr that gets changed. - * @param aSource is where chars are copied from - * @param aCount is the number of chars copied from aSource - */ -void nsStrPrivate::StrAssign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount){ - if(&aDest!=&aSource){ - StrTruncate(aDest,0); - StrAppend(aDest,aSource,anOffset,aCount); - } -} - -/** - * This method appends the given nsStr to this one. Note that we have to - * pay attention to the underlying char-size of both structs. - * @update gess10/30/98 - * @param aDest is the nsStr to be manipulated - * @param aSource is where char are copied from - * @aCount is the number of bytes to be copied - */ -void nsStrPrivate::StrAppend(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount){ - if(anOffset aDest.GetCapacity()) { - isBigEnough=GrowCapacity(aDest,aDest.mLength+theLength); - } - - if(isBigEnough) { - //now append new chars, starting at offset - (*gCopyChars[aSource.GetCharSize()][aDest.GetCharSize()])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); - - aDest.mLength+=theLength; - AddNullTerminator(aDest); - NSSTR_SEEN(aDest); - } - } - } -} - - -/** - * This method inserts up to "aCount" chars from a source nsStr into a dest nsStr. - * @update gess10/30/98 - * @param aDest is the nsStr that gets changed - * @param aDestOffset is where in aDest the insertion is to occur - * @param aSource is where chars are copied from - * @param aSrcOffset is where in aSource chars are copied from - * @param aCount is the number of chars from aSource to be inserted into aDest - */ - -PRInt32 nsStrPrivate::GetSegmentLength(const nsStr& aSource, - PRUint32 aSrcOffset, PRInt32 aCount) -{ - PRInt32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength); - PRInt32 theLength=(aSrcOffset+theRealLen aDest.GetCapacity()) - AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); - else { - //shift the chars right by theDelta... - ShiftCharsRight(aDest.mStr, aDest.mLength, aDestOffset, theLength); - - //now insert new chars, starting at offset - CopyChars1To1(aDest.mStr, aDestOffset, aSource.mStr, aSrcOffset, theLength); - } - - //finally, make sure to update the string length... - aDest.mLength+=theLength; - AddNullTerminator(aDest); - NSSTR_SEEN(aDest); - }//if - //else nothing to do! - } - else StrAppend(aDest,aSource,0,aCount); - } - else StrAppend(aDest,aSource,0,aCount); - } -} - -void nsStrPrivate::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - 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) - // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). - if(0 aDest.GetCapacity()) - AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); - else { - //shift the chars right by theDelta... - ShiftDoubleCharsRight(aDest.mUStr, aDest.mLength, aDestOffset, theLength); - - //now insert new chars, starting at offset - CopyChars1To2(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); - } - - //finally, make sure to update the string length... - aDest.mLength+=theLength; - AddNullTerminator(aDest); - NSSTR_SEEN(aDest); - }//if - //else nothing to do! - } - else StrAppend(aDest,aSource,0,aCount); - } - else StrAppend(aDest,aSource,0,aCount); - } -} - -void nsStrPrivate::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ - 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) - // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). - if(0 aDest.GetCapacity()) - AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); - else { - - //shift the chars right by theDelta... - ShiftDoubleCharsRight(aDest.mUStr, aDest.mLength, aDestOffset, theLength); - - //now insert new chars, starting at offset - CopyChars2To2(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); - } - - //finally, make sure to update the string length... - aDest.mLength+=theLength; - AddNullTerminator(aDest); - NSSTR_SEEN(aDest); - }//if - //else nothing to do! - } - else StrAppend(aDest,aSource,0,aCount); - } - else StrAppend(aDest,aSource,0,aCount); - } -} - - -/** - * This method deletes up to aCount chars from aDest - * @update gess10/30/98 - * @param aDest is the nsStr to be manipulated - * @param aDestOffset is where in aDest deletion is to occur - * @param aCount is the number of chars to be deleted in aDest - */ - - -void nsStrPrivate::Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){ - NS_ASSERTION(aDest.GetCharSize() == eOneByte, "Must be 1 byte"); - - if(aDestOffset0) && aSet){ - PRInt32 theIndex=-1; - PRInt32 theMax=aDest.mLength; - PRInt32 theSetLen=strlen(aSet); - - if(aEliminateLeading) { - while(++theIndex<=theMax) { - PRUnichar theChar=aDest.GetCharAt(theIndex); - PRInt32 thePos=::FindChar1(aSet,theSetLen,0,theChar,theSetLen); - if(kNotFound==thePos) - break; - } - - if(0=0) { - PRUnichar theChar=aDest.GetCharAt(theIndex); //read at end now... - PRInt32 thePos=::FindChar1(aSet,theSetLen,0,theChar,theSetLen); - if(kNotFoundtheMaxPos) || (aTarget.mLength==0)) - return kNotFound; - - if(aCount<0) - aCount = MaxInt(theMaxPos,1); - - if (aCount <= 0) - return kNotFound; - - const char* root = aDest.mStr; - const char* left = root + anOffset; - const char* last = left + aCount; - const char* max = root + theMaxPos; - - const char* right = (lasttheMaxPos) || (aTarget.mLength==0)) - return kNotFound; - - if(aCount<0) - aCount = MaxInt(theMaxPos,1); - - if (aCount <= 0) - return kNotFound; - - const PRUnichar* root = aDest.mUStr; - const PRUnichar* left = root+anOffset; - const PRUnichar* last = left+aCount; - const PRUnichar* max = root+theMaxPos; - const PRUnichar* right = (lasttheMaxPos) || (aTarget.mLength==0)) - return kNotFound; - - if(aCount<0) - aCount = MaxInt(theMaxPos,1); - - if (aCount <= 0) - return kNotFound; - - const PRUnichar* root = aDest.mUStr; - const PRUnichar* left = root+anOffset; - const PRUnichar* last = left+aCount; - const PRUnichar* max = root+theMaxPos; - const PRUnichar* right = (last=aDest.mLength) || (aTarget.mLength==0)) - return kNotFound; - - if (aCount<=0) - return kNotFound; - - const char* root = aDest.mStr; - const char* destLast = root+aDest.mLength; //pts to last char in aDest (likely null) - - const char* rightmost = root+anOffset; - const char* min = rightmost-aCount + 1; - - const char* leftmost = (min=aDest.mLength) || (aTarget.mLength==0)) - return kNotFound; - - if (aCount<=0) - return kNotFound; - - const PRUnichar* root = aDest.mUStr; - const PRUnichar* destLast = root+aDest.mLength; //pts to last char in aDest (likely null) - - const PRUnichar* rightmost = root+anOffset; - const PRUnichar* min = rightmost-aCount+1; - - const PRUnichar* leftmost = (min=aDest.mLength) || (aTarget.mLength==0)) - return kNotFound; - - if (aCount<=0) - return kNotFound; - - const PRUnichar* root = aDest.mUStr; - const PRUnichar* destLast = root+aDest.mLength; //pts to last char in aDest (likely null) - - const PRUnichar* rightmost = root+anOffset; - const PRUnichar* min = rightmost-aCount+1; - - const PRUnichar* leftmost = (minaSource=1 - */ -PRInt32 nsStrPrivate::StrCompare1To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - 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); - result = TranslateCompareResult(aDest.mLength, aSource.mLength, result, aCount); - return result; - } - - return 0; -} - -PRInt32 nsStrPrivate::StrCompare2To1(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - 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); - PRInt32 result = Compare2To1(aDest.mUStr, aSource.mStr, theCount, aIgnoreCase); - result = TranslateCompareResult(aDest.mLength, aSource.mLength, result, aCount); - return result; - } - - return 0; -} - -PRInt32 nsStrPrivate::StrCompare2To2(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount) { - 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); - PRInt32 result = Compare2To2(aDest.mUStr, aSource.mUStr, theCount); - result = TranslateCompareResult(aDest.mLength, aSource.mLength, result, aCount); - return result; - } - - return 0; -} - -/** - * Overwrites the contents of dest at offset with contents of aSource - * - * @param aDest is the first str to compare - * @param aSource is the second str to compare - * @param aDestOffset is the offset within aDest where source should be copied - * @return error code - */ -void nsStrPrivate::Overwrite(nsStr& aDest,const nsStr& aSource,PRInt32 aDestOffset) { - if(aDest.mLength && aSource.mLength) { - 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.GetCharSize()][aDest.GetCharSize()])(aDest.mStr,aDestOffset,aSource.mStr,0,aSource.mLength); - } - } -} - -//---------------------------------------------------------------------------------------- -// allocate the given bytes, not including the null terminator -PRBool nsStrPrivate::Alloc(nsStr& aDest,PRUint32 aCount) { - - // the new strategy is, allocate exact size, double on grows - aDest.SetInternalCapacity(aCount); - aDest.mStr = (char*)nsMemory::Alloc((aCount+1)<1) { - mCapacity=aCapacity-1; - mLength=(-1==aLength) ? nsCharTraits::length(aString) : aLength; - if(mLength>PRInt32(mCapacity)) - mLength=mCapacity; - } -} - -CBufDescriptor::CBufDescriptor(const char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { - mBuffer=(char*)aString; - mCharSize=eOneByte; - mStackBased=aStackBased; - mIsConst=PR_TRUE; - mLength=mCapacity=0; - if(aString && aCapacity>1) { - mCapacity=aCapacity-1; - mLength=(-1==aLength) ? nsCharTraits::length(aString) : aLength; - if(mLength>PRInt32(mCapacity)) - mLength=mCapacity; - } -} - - -CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { - mBuffer=(char*)aString; - mCharSize=eTwoByte; - mStackBased=aStackBased; - mLength=mCapacity=0; - mIsConst=PR_FALSE; - if(aString && aCapacity>1) { - mCapacity=aCapacity-1; - mLength=(-1==aLength) ? nsCharTraits::length(aString) : aLength; - if(mLength>PRInt32(mCapacity)) - mLength=mCapacity; - } -} - -CBufDescriptor::CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { - mBuffer=(char*)aString; - mCharSize=eTwoByte; - mStackBased=aStackBased; - mLength=mCapacity=0; - mIsConst=PR_TRUE; - if(aString && aCapacity>1) { - mCapacity=aCapacity-1; - mLength=(-1==aLength) ? nsCharTraits::length(aString) : aLength; - if(mLength>PRInt32(mCapacity)) - mLength=mCapacity; - } -} - -//---------------------------------------------------------------------------------------- - -PRUint32 -nsStrPrivate::HashCode(const nsStr& aDest) -{ - PRUint32 h = 0; - if (aDest.GetCharSize() == eTwoByte) { - const PRUnichar* s = aDest.mUStr; - - if (!s) return h; - - PRUnichar c; - while ( (c = *s++) ) - h = (h>>28) ^ (h<<4) ^ c; - return h; - } else { - const char* s = aDest.mStr; - - if (!s) return h; - - unsigned char c; - while ( (c = *s++) ) - h = (h>>28) ^ (h<<4) ^ c; - return h; - } -} - -/** - * This is a copy of |PR_cnvtf| with a bug fixed. (The second argument - * of PR_dtoa is 2 rather than 1.) - */ -void -nsStrPrivate::cnvtf(char *buf, int bufsz, int prcsn, double fval) -{ - PRIntn decpt, sign, numdigits; - char *num, *nump; - char *bufp = buf; - char *endnum; - - /* If anything fails, we store an empty string in 'buf' */ - num = (char*)PR_MALLOC(bufsz); - if (num == NULL) { - buf[0] = '\0'; - return; - } - if (PR_dtoa(fval, 2, prcsn, &decpt, &sign, &endnum, num, bufsz) - == PR_FAILURE) { - buf[0] = '\0'; - goto done; - } - numdigits = endnum - num; - nump = num; - - /* - * The NSPR code had a fancy way of checking that we weren't dealing - * with -0.0 or -NaN, but I'll just use < instead. - * XXX Should we check !isnan(fval) as well? Is it portable? We - * probably don't need to bother since NAN isn't portable. - */ - if (sign && fval < 0.0f) { - *bufp++ = '-'; - } - - if (decpt == 9999) { - while ((*bufp++ = *nump++) != 0) {} /* nothing to execute */ - goto done; - } - - if (decpt > (prcsn+1) || decpt < -(prcsn-1) || decpt < -5) { - *bufp++ = *nump++; - if (numdigits != 1) { - *bufp++ = '.'; - } - - while (*nump != '\0') { - *bufp++ = *nump++; - } - *bufp++ = 'e'; - PR_snprintf(bufp, bufsz - (bufp - buf), "%+d", decpt-1); - } - else if (decpt >= 0) { - if (decpt == 0) { - *bufp++ = '0'; - } - else { - while (decpt--) { - if (*nump != '\0') { - *bufp++ = *nump++; - } - else { - *bufp++ = '0'; - } - } - } - if (*nump != '\0') { - *bufp++ = '.'; - while (*nump != '\0') { - *bufp++ = *nump++; - } - } - *bufp++ = '\0'; - } - else if (decpt < 0) { - *bufp++ = '0'; - *bufp++ = '.'; - while (decpt++) { - *bufp++ = '0'; - } - - while (*nump != '\0') { - *bufp++ = *nump++; - } - *bufp++ = '\0'; - } -done: - PR_DELETE(num); -} - -#ifdef NS_STR_STATS - -#include - -#ifdef XP_MAC -#define isascii(c) ((unsigned)(c) < 0x80) -#endif - -void -nsStrPrivate::Print(const nsStr& aDest, FILE* out, PRBool truncate) -{ - PRInt32 printLen = (PRInt32)aDest.mLength; - - if (aDest.GetCharSize() == eOneByte) { - const char* chars = aDest.mStr; - while (printLen-- && (!truncate || *chars != '\n')) { - fputc(*chars++, out); - } - } - else { - const PRUnichar* chars = aDest.mUStr; - while (printLen-- && (!truncate || *chars != '\n')) { - if (isascii(*chars)) - fputc((char)(*chars++), out); - else - fputc('-', out); - } - } -} - -//////////////////////////////////////////////////////////////////////////////// -// String Usage Statistics Routines - -static PLHashTable* gStringInfo = nsnull; -PRLock* gStringInfoLock = nsnull; -PRBool gNoStringInfo = PR_FALSE; - -nsStringInfo::nsStringInfo(nsStr& str) - : mCount(0) -{ - nsStrPrivate::Initialize(mStr, str.GetCharSize()); - nsStrPrivate::StrAssign(mStr, str, 0, -1); -// nsStrPrivate::Print(mStr, stdout); -// fputc('\n', stdout); -} - -PR_EXTERN(PRHashNumber) -nsStr_Hash(const void* key) -{ - nsStr* str = (nsStr*)key; - return nsStrPrivate::HashCode(*str); -} - -nsStringInfo* -nsStringInfo::GetInfo(nsStr& str) -{ - if (gStringInfo == nsnull) { - gStringInfo = PL_NewHashTable(1024, - nsStr_Hash, - nsStr_Compare, - PL_CompareValues, - NULL, NULL); - gStringInfoLock = PR_NewLock(); - } - PR_Lock(gStringInfoLock); - nsStringInfo* info = - (nsStringInfo*)PL_HashTableLookup(gStringInfo, &str); - if (info == NULL) { - gNoStringInfo = PR_TRUE; - info = new nsStringInfo(str); - if (info) { - PLHashEntry* e = PL_HashTableAdd(gStringInfo, &info->mStr, info); - if (e == NULL) { - delete info; - info = NULL; - } - } - gNoStringInfo = PR_FALSE; - } - PR_Unlock(gStringInfoLock); - return info; -} - -void -nsStringInfo::Seen(nsStr& str) -{ - if (!gNoStringInfo) { - nsStringInfo* info = GetInfo(str); - info->mCount++; - } -} - -void -nsStringInfo::Report(FILE* out) -{ - if (gStringInfo) { - fprintf(out, "\n== String Stats\n"); - PL_HashTableEnumerateEntries(gStringInfo, nsStringInfo::ReportEntry, out); - } -} - -PRIntn -nsStringInfo::ReportEntry(PLHashEntry *he, PRIntn i, void *arg) -{ - nsStringInfo* entry = (nsStringInfo*)he->value; - FILE* out = (FILE*)arg; - - fprintf(out, "%d ==> (%d) ", entry->mCount, entry->mStr.mLength); - nsStrPrivate::Print(entry->mStr, out, PR_TRUE); - fputc('\n', out); - return HT_ENUMERATE_NEXT; -} - -#endif // NS_STR_STATS - -//////////////////////////////////////////////////////////////////////////////// diff --git a/mozilla/xpcom/string/obsolete/nsStr.h b/mozilla/xpcom/string/obsolete/nsStr.h deleted file mode 100644 index 481bfc3cc3d..00000000000 --- a/mozilla/xpcom/string/obsolete/nsStr.h +++ /dev/null @@ -1,390 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * 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 the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Rick Gessner (original author) - * Scott Collins - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -/* nsStr.h --- the underlying buffer for rickg's original string implementations; - these classes will be replaced by the new shared-buffer string (see bug #53065) - */ - -#ifndef _nsStr -#define _nsStr - -#ifndef nsStringDefines_h___ -#include "nsStringDefines.h" -#endif - - -/*********************************************************************** - MODULE NOTES: - - 1. There are two philosophies to building string classes: - A. Hide the underlying buffer & offer API's allow indirect iteration - B. Reveal underlying buffer, risk corruption, but gain performance - - We chose the option B for performance reasons. - - 2 Our internal buffer always holds capacity+1 bytes. - - The nsStr struct is a simple structure (no methods) that contains - the necessary info to be described as a string. This simple struct - is manipulated by the static methods provided in this class. - (Which effectively makes this a library that works on structs). - - There are also object-based versions called nsString and nsAutoString - which use nsStr but makes it look at feel like an object. - - ***********************************************************************/ - -/*********************************************************************** - ASSUMPTIONS: - - 1. nsStrings and nsAutoString are always null terminated. However, - since it maintains a length byte, you can store NULL's inside - the string. Just be careful passing such buffers to 3rd party - API's that assume that NULL always terminate the buffer. - - 2. nsCStrings can be upsampled into nsString without data loss - - 3. Char searching is faster than string searching. Use char interfaces - if your needs will allow it. - - 4. It's easy to use the stack for nsAutostring buffer storage (fast too!). - See the CBufDescriptor class in this file. - - 5. If you don't provide the optional count argument to Append() and Insert(), - the method will assume that the given buffer is terminated by the first - NULL it encounters. - - 6. Downsampling from nsString to nsCString can be lossy -- avoid it if possible! - - 7. Calls to ToNewCString() and ToNewUnicode() should be matched with calls to nsMemory::Free(). - - ***********************************************************************/ - - -/********************************************************************************** - - AND NOW FOR SOME GENERAL DOCUMENTATION ON STRING USAGE... - - The fundamental datatype in the string library is nsStr. It's a structure that - provides the buffer storage and meta-info. It also provides a C-style library - of functions for direct manipulation (for those of you who prefer K&R to Bjarne). - - Here's a diagram of the class hierarchy: - - nsStr - |___nsString - | | - | ------nsAutoString - | - |___nsCString - | - ------nsCAutoString - - Why so many string classes? The 4 variants give you the control you need to - determine the best class for your purpose. There are 2 dimensions to this - flexibility: 1) stack vs. heap; and 2) 1-byte chars vs. 2-byte chars. - - Note: While nsAutoString and nsCAutoString begin life using stack-based storage, - they may not stay that way. Like all nsString classes, autostrings will - automatically grow to contain the data you provide. When autostrings - grow beyond their intrinsic buffer, they switch to heap based allocations. - (We avoid alloca to avoid considerable platform difficulties; see the - GNU documentation for more details). - - I should also briefly mention that all the string classes use a "memory agent" - object to perform memory operations. This class proxies the standard nsMemory - for actual memory calls, but knows the structure of nsStr making heap operations - more localized. - - - CHOOSING A STRING CLASS: - - In order to choose a string class for you purpose, use this handy table: - - heap-based stack-based - ----------------------------------- - ascii data | nsCString nsCAutoString | - |---------------------------------- - unicode data | nsString nsAutoString | - ----------------------------------- - - - Note: The i18n folks will stenuously object if we get too carried away with the - use of nsCString's that pass interface boundaries. Try to limit your - use of these to external interfaces that demand them, or for your own - private purposes in cases where they'll never be seen by humans. - - - --- FAQ --- - - Q. When should I use nsCString instead of nsString? - - A. You should really try to stick with nsString, so that we stay as unicode - compliant as possible. But there are cases where an interface you use requires - a char*. In such cases, it's fair to use nsCString. - - Q. I know that my string is going to be a certain size. Can I pre-size my nsString? - - A. Yup, here's how: - - { - nsString mBuffer; - mBuffer.SetCapacity(aReasonableSize); - } - - Q. Should nsAutoString or nsCAutoString ever live on the heap? - - A. That would be counterproductive. The point of nsAutoStrings is to preallocate your - buffers, and to auto-destroy the string when it goes out of scope. - - Q. I already have a char*. Can I use the nsString functionality on that buffer? - - A. Yes you can -- by using an intermediate class called CBufDescriptor. - The CBufDescriptor class is used to tell nsString about an external buffer (heap or stack) to use - instead of it's own internal buffers. Here's an example: - - { - char theBuffer[256]; - CBufDescritor theBufDecriptor( theBuffer, PR_TRUE, sizeof(theBuffer), 0); - nsCAutoString s3( theBufDescriptor ); - s3="HELLO, my name is inigo montoya, you killed my father, prepare to die!."; - } - - The assignment statment to s3 will cause the given string to be written to your - stack-based buffer via the normal nsString/nsCString interfaces. Cool, huh? - Note however that just like any other nsStringXXX use, if you write more data - than will fit in the buffer, a visit to the heap manager will be in order. - - - Q. What is the simplest way to get from a char* to PRUnichar*? - - A. The simplest way is by construction (or assignment): - - { - char* theBuf = "hello there"; - nsAutoString foo(theBuf); - } - - If you don't want the char* to be copied into the nsAutoString, the use a - CBufDescriptor instead. - - - **********************************************************************************/ - - -#include "nscore.h" -#include "nsMemory.h" -#include -#include -#include "plhash.h" -#include "nsStrShared.h" - -//---------------------------------------------------------------------------------------- -#define kDefaultCharSize eTwoByte -#define kRadix10 (10) -#define kRadix16 (16) -#define kAutoDetect (100) -#define kRadixUnknown (kAutoDetect+1) -#define IGNORE_CASE (PR_TRUE) - -#define NSSTR_CHARSIZE_BIT (31) -#define NSSTR_OWNSBUFFER_BIT (30) - -#define NSSTR_CHARSIZE_MASK (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); - } - - inline PRUnichar GetCharAt(PRUint32 anIndex) const { - if(anIndex (original author) - * Scott Collins - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef __nsStrPrivate_h -#define __nsStrPrivate_h - -#include "nscore.h" -#include "nsStrShared.h" - -struct nsStr; - -// there are only static methods here! -class nsStrPrivate { - public: - /** - * This method initializes an nsStr for use - * - * @update gess 01/04/99 - * @param aString is the nsStr to be initialized - * @param aCharSize tells us the requested char size (1 or 2 bytes) - */ - static void Initialize(nsStr& aDest,eCharSize aCharSize); - - /** - * This method initializes an nsStr for use - * - * @update gess 01/04/99 - * @param aString is the nsStr to be initialized - * @param aCharSize tells us the requested char size (1 or 2 bytes) - */ - static void Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer); - /** - * This method destroys the given nsStr, and *MAY* - * deallocate it's memory depending on the setting - * of the internal mOwnsBUffer flag. - * - * @update gess 01/04/99 - * @param aString is the nsStr to be manipulated - */ - static void Destroy(nsStr& aDest); - - /** - * These methods are where memory allocation/reallocation occur. - * - * @update gess 01/04/99 - * @param aString is the nsStr to be manipulated - * @return - */ - static PRBool EnsureCapacity(nsStr& aString,PRUint32 aNewLength); - static PRBool GrowCapacity(nsStr& aString,PRUint32 aNewLength); - - /** - * These methods are used to append content to the given nsStr - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param aSource is the buffer to be copied from - * @param anOffset tells us where in source to start copying - * @param aCount tells us the (max) # of chars to copy - */ - static void StrAppend(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount); - - /** - * These methods are used to assign contents of a source string to dest string - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param aSource is the buffer to be copied from - * @param anOffset tells us where in source to start copying - * @param aCount tells us the (max) # of chars to copy - */ - static void StrAssign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount); - - /** - * These methods are used to insert content from source string to the dest nsStr - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param aDestOffset tells us where in dest to start insertion - * @param aSource is the buffer to be copied from - * @param aSrcOffset tells us where in source to start copying - * @param aCount tells us the (max) # of chars to insert - */ - static void StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount); - static void StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount); - static void StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount); - - - /** - * Helper routines for StrInsert1into1, etc - */ - static PRInt32 GetSegmentLength(const nsStr& aSource, - PRUint32 aSrcOffset, PRInt32 aCount); - static void AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSource, PRUint32 aSrcOffset, PRInt32 theLength); - - /** - * This method deletes chars from the given str. - * The given allocator may choose to resize the str as well. - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be deleted from - * @param aDestOffset tells us where in dest to start deleting - * @param aCount tells us the (max) # of chars to delete - */ - static void Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount); - static void Delete2(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount); - - /** - * helper routines for Delete1, Delete2 - */ - static PRInt32 GetDeleteLength(const nsStr& aDest, PRUint32 aDestOffset, PRUint32 aCount); - - /** - * This method is used to truncate the given string. - * The given allocator may choose to resize the str as well (but it's not likely). - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param aDestOffset tells us where in dest to start insertion - * @param aSource is the buffer to be copied from - * @param aSrcOffset tells us where in source to start copying - */ - static void StrTruncate(nsStr& aDest,PRUint32 aDestOffset); - - /** - * This method trims chars (given in aSet) from the edges of given buffer - * - * @update gess 01/04/99 - * @param aDest is the buffer to be manipulated - * @param aSet tells us which chars to remove from given buffer - * @param aEliminateLeading tells us whether to strip chars from the start of the buffer - * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer - */ - static void Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); - - /** - * This method compresses duplicate runs of a given char from the given buffer - * - * @update gess 01/04/99 - * @param aDest is the buffer to be manipulated - * @param aSet tells us which chars to compress from given buffer - * @param aChar is the replacement char - * @param aEliminateLeading tells us whether to strip chars from the start of the buffer - * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer - */ - static void CompressSet1(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); - static void CompressSet2(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); - - /** - * This method removes all occurances of chars in given set from aDest - * - * @update gess 01/04/99 - * @param aDest is the buffer to be manipulated - * @param aSet tells us which chars to compress from given buffer - * @param aChar is the replacement char - * @param aEliminateLeading tells us whether to strip chars from the start of the buffer - * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer - */ - static void StripChars1(nsStr& aDest,const char* aSet); - static void StripChars2(nsStr& aDest,const char* aSet); - - /** - * This method compares the data bewteen two nsStr's - * - * @update gess 01/04/99 - * @param aStr1 is the first buffer to be compared - * @param aStr2 is the 2nd buffer to be compared - * @param aCount is the number of chars to compare - * @param aIgnorecase tells us whether to use a case-sensitive comparison - * @return -1,0,1 depending on <,==,> - */ - static PRInt32 StrCompare1To1(const nsStr& aDest,const nsStr& aSource, - PRInt32 aCount,PRBool aIgnoreCase); - static PRInt32 StrCompare2To1(const nsStr& aDest, const nsStr& aSource, - PRInt32 aCount, PRBool aIgnoreCase); - static PRInt32 StrCompare2To2(const nsStr& aDest, const nsStr& aSource, - PRInt32 aCount); - - /** - * These methods scan the given string for 1 or more chars in a given direction - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be searched to - * @param aSource (or aChar) is the substr we're looking to find - * @param aIgnoreCase tells us whether to search in a case-sensitive manner - * @param anOffset tells us where in the dest string to start searching - * @return the index of the source (substr) in dest, or -1 (kNotFound) if not found. - */ - static PRInt32 FindSubstr1in1(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount); - static PRInt32 FindSubstr1in2(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount); - static PRInt32 FindSubstr2in2(const nsStr& aDest,const nsStr& aSource, PRInt32 anOffset,PRInt32 aCount); - - static PRInt32 FindChar1(const nsStr& aDest,PRUnichar aChar, PRInt32 anOffset,PRInt32 aCount); - static PRInt32 FindChar2(const nsStr& aDest,PRUnichar aChar, PRInt32 anOffset,PRInt32 aCount); - - static PRInt32 RFindSubstr1in1(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount); - static PRInt32 RFindSubstr1in2(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount); - static PRInt32 RFindSubstr2in2(const nsStr& aDest,const nsStr& aSource, PRInt32 anOffset,PRInt32 aCount); - - static PRInt32 RFindChar1(const nsStr& aDest,PRUnichar aChar, PRInt32 anOffset,PRInt32 aCount); - static PRInt32 RFindChar2(const nsStr& aDest,PRUnichar aChar, PRInt32 anOffset,PRInt32 aCount); - - static void Overwrite(nsStr& aDest,const nsStr& aSource,PRInt32 anOffset); - - static char GetFindInSetFilter(const char *set) - { - // Calculate filter - char filter = ~char(0); // All bits set - while (*set) { - filter &= ~(*set); - ++set; - } - - return filter; - } - static PRUnichar GetFindInSetFilter(const PRUnichar *set) - { - // Calculate filter - PRUnichar filter = ~PRUnichar(0); // All bits set - while (*set) { - filter &= ~(*set); - ++set; - } - return filter; - } - -#ifdef NS_STR_STATS - static PRBool DidAcquireMemory(void); -#endif - - /** - * Returns a hash code for the string for use in a PLHashTable. - */ - static PRUint32 HashCode(const nsStr& aDest); - - // A copy of PR_cnvtf with a bug fixed. - static void cnvtf(char *buf, int bufsz, int prcsn, double fval); - -#ifdef NS_STR_STATS - /** - * Prints an nsStr. If truncate is true, the string is only printed up to - * the first newline. (Note: The current implementation doesn't handle - * non-ascii unicode characters.) - */ - static void Print(const nsStr& aDest, FILE* out, PRBool truncate = PR_FALSE); -#endif - - static PRBool Alloc(nsStr& aString,PRUint32 aCount); - static PRBool Realloc(nsStr& aString,PRUint32 aCount); - static PRBool Free(nsStr& aString); - -}; - -#endif diff --git a/mozilla/xpcom/string/obsolete/nsStrShared.h b/mozilla/xpcom/string/obsolete/nsStrShared.h deleted file mode 100644 index 251c9bfd915..00000000000 --- a/mozilla/xpcom/string/obsolete/nsStrShared.h +++ /dev/null @@ -1,8 +0,0 @@ - -#ifndef __nsStrShared_h -#define __nsStrShared_h - - -enum eCharSize {eOneByte=0,eTwoByte=1}; - -#endif diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp deleted file mode 100644 index 72797f743f8..00000000000 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ /dev/null @@ -1,1243 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * 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 the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Rick Gessner - * Scott Collins - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include -#include -#include -#include -#include "nsStrPrivate.h" -#include "nsString.h" -#include "nsReadableUtils.h" -#include "nsDebug.h" -#include "nsUTF8Utils.h" - -#ifndef nsCharTraits_h___ -#include "nsCharTraits.h" -#endif - -#ifndef RICKG_TESTBED -#include "prdtoa.h" -#endif - -#ifdef DEBUG -static const char* kPossibleNull = "Error: possible unintended null in string"; -static const char* kNullPointerError = "Error: unexpected null ptr"; -#endif -static const char* kWhitespace="\b\t\r\n "; - -const nsBufferHandle* -nsCString::GetFlatBufferHandle() const - { - return NS_REINTERPRET_CAST(const nsBufferHandle*, 1); - } - -/** - * Default constructor. - */ -nsCString::nsCString() { - nsStrPrivate::Initialize(*this,eOneByte); -} - -nsCString::nsCString(const char* aCString) { - nsStrPrivate::Initialize(*this,eOneByte); - Assign(aCString); -} - -/** - * This constructor accepts an ascii string - * @update gess 1/4/99 - * @param aCString is a ptr to a 1-byte cstr - * @param aLength tells us how many chars to copy from given CString - */ -nsCString::nsCString(const char* aCString,PRInt32 aLength) { - nsStrPrivate::Initialize(*this,eOneByte); - Assign(aCString,aLength); -} - -/** - * This is our copy constructor - * @update gess 1/4/99 - * @param reference to another nsCString - */ -nsCString::nsCString(const nsCString& aString) { - nsStrPrivate::Initialize(*this,aString.GetCharSize()); - nsStrPrivate::StrAssign(*this,aString,0,aString.mLength); -} - -/** - * Destructor - */ -nsCString::~nsCString() { - nsStrPrivate::Destroy(*this); -} - -const char* nsCString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const { - switch ( aRequest ) { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mEnd = (aFragment.mStart = mStr) + mLength; - return aFragment.mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } -} - -char* nsCString::GetWritableFragment( nsWritableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) { - switch ( aRequest ) { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mEnd = (aFragment.mStart = mStr) + mLength; - return aFragment.mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } -} - -nsCString::nsCString( const nsACString& aReadable ) { - nsStrPrivate::Initialize(*this,eOneByte); - Assign(aReadable); -} - -/** - * This method truncates this string to given length. - * - * @update rickg 03.23.2000 - * @param anIndex -- new length of string - * @return nada - */ -void nsCString::SetLength(PRUint32 anIndex) { - if ( anIndex > GetCapacity() ) - SetCapacity(anIndex); - // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAString.h), - // we can only use it since our local implementation, |nsCString::SetCapacity|, is known to do what we want - - nsStrPrivate::StrTruncate(*this,anIndex); -} - - -/** - * Call this method if you want to force the string to a certain capacity; - * |SetCapacity(0)| discards associated storage. - * - * @param aNewCapacity -- desired minimum capacity - */ -void -nsCString::SetCapacity( PRUint32 aNewCapacity ) - { - if ( aNewCapacity ) - { - if( aNewCapacity > GetCapacity() ) - nsStrPrivate::GrowCapacity(*this,aNewCapacity); - AddNullTerminator(*this); - } - else - { - nsStrPrivate::Destroy(*this); - nsStrPrivate::Initialize(*this, eOneByte); - } - } - -/********************************************************************** - Accessor methods... - *********************************************************************/ - -/** - * set a char inside this string at given index - * @param aChar is the char you want to write into this string - * @param anIndex is the ofs where you want to write the given char - * @return TRUE if successful - */ -PRBool nsCString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ - PRBool result=PR_FALSE; - if(anIndex::length(aTarget); - if(0::length(aNewValue); - if(02)) { - theFirstChar=First(); - theLastChar=Last(); - if(theFirstChar==theLastChar) { - if(('\''==theFirstChar) || ('"'==theFirstChar)) { - Cut(0,1); - Truncate(mLength-1); - theQuotesAreNeeded=PR_TRUE; - } - else theFirstChar=0; - } - } - - nsStrPrivate::Trim(*this, aTrimSet, aEliminateLeading, aEliminateTrailing); - - if(aIgnoreQuotes && theQuotesAreNeeded) { - Insert(theFirstChar, 0); - Append(theLastChar); - } - - } -} - -/** - * This method strips chars in given set from string. - * You can control whether chars are yanked from - * start and end of string as well. - * - * @update gess 3/31/98 - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this - */ -static void -CompressSet(nsCString& aStr, const char* aSet, char aChar, - PRBool aEliminateLeading, PRBool aEliminateTrailing){ - if(aSet){ - aStr.ReplaceChar(aSet, aChar); - nsStrPrivate::CompressSet1(aStr, aSet, - aEliminateLeading, aEliminateTrailing); - } -} - -/** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. - * - * @update gess 3/31/98 - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this - */ -void -nsCString::CompressWhitespace( PRBool aEliminateLeading,PRBool aEliminateTrailing){ - CompressSet(*this, kWhitespace,' ',aEliminateLeading,aEliminateTrailing); -} - -/********************************************************************** - string conversion methods... - *********************************************************************/ - -/** - * Perform string to float conversion. - * @update gess 01/04/99 - * @param aErrorCode will contain error if one occurs - * @return float rep of string value - */ -float nsCString::ToFloat(PRInt32* aErrorCode) const { - float res = 0.0f; - if (mLength > 0) { - char *conv_stopped; - const char *str = get(); - // Use PR_strtod, not strtod, since we don't want locale involved. - res = (float)PR_strtod(str, &conv_stopped); - if (conv_stopped == str+mLength) { - *aErrorCode = (PRInt32) NS_OK; - } - else { - /* Not all the string was scanned */ - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - } - } - else { - /* The string was too short (0 characters) */ - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - } - return res; -} - -/** - * Perform decimal numeric string to int conversion. - * NOTE: In this version, we use the radix you give, even if it's wrong. - * @update gess 02/14/00 - * @param aErrorCode will contain error if one occurs - * @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix. - * @return int rep of string value - */ -PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { - char* cp=mStr; - PRInt32 theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect) - PRInt32 result=0; - PRBool negate=PR_FALSE; - char theChar=0; - - //initial value, override if we find an integer - *anErrorCode=NS_ERROR_ILLEGAL_VALUE; - - if(cp) { - - //begin by skipping over leading chars that shouldn't be part of the number... - - char* endcp=cp+mLength; - PRBool done=PR_FALSE; - - while((cp='A') && (theChar<='F')) { - if(10==theRadix) { - if(kAutoDetect==aRadix){ - theRadix=16; - cp=first; //backup - result=0; - haveValue = PR_FALSE; - } - else { - *anErrorCode=NS_ERROR_ILLEGAL_VALUE; - result=0; - break; - } - } - else { - result = (theRadix * result) + ((theChar-'A')+10); - haveValue = PR_TRUE; - } - } - else if((theChar>='a') && (theChar<='f')) { - if(10==theRadix) { - if(kAutoDetect==aRadix){ - theRadix=16; - cp=first; //backup - result=0; - haveValue = PR_FALSE; - } - else { - *anErrorCode=NS_ERROR_ILLEGAL_VALUE; - result=0; - break; - } - } - else { - result = (theRadix * result) + ((theChar-'a')+10); - haveValue = PR_TRUE; - } - } - else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) { - continue; - } - else if((('#'==theChar) || ('+'==theChar)) && !haveValue) { - continue; - } - else { - //we've encountered a char that's not a legal number or sign - break; - } - } //while - if(negate) - result=-result; - } //if - } - return result; -} - -/********************************************************************** - String manipulation methods... - *********************************************************************/ - - -/** - * assign given unichar* to this string - * @update gess 01/04/99 - * @param aCString: buffer to be assigned to this - * @param aCount -- length of given buffer or -1 if you want me to compute length. - * NOTE: IFF you pass -1 as aCount, then your buffer must be null terminated. - * - * @return this - */ -void nsCString::AssignWithConversion(const PRUnichar* aString,PRInt32 aCount) { - nsStrPrivate::StrTruncate(*this,0); - - if(aString && aCount){ - nsStr temp; - nsStrPrivate::Initialize(temp,eTwoByte); - temp.mUStr=(PRUnichar*)aString; - - if(0::length(aString); - - if(0::length(aBuffer); - - if ( aLength > 0 ) - { - temp.mLength = aLength; - nsStrPrivate::StrAppend(*this, temp, 0, aLength); - } - } - -/** - * Append the given integer to this string - * @update gess 01/04/99 - * @param aInteger: - * @param aRadix: - * @return - */ -void nsCString::AppendInt(PRInt32 anInteger,PRInt32 aRadix) { - - PRUint32 theInt=(PRUint32)anInteger; - - char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - - PRInt32 radices[] = {1000000000,268435456}; - PRInt32 mask1=radices[16==aRadix]; - - PRInt32 charpos=0; - if(anInteger<0) { - theInt*=-1; - if(10==aRadix) { - buf[charpos++]='-'; - } - else theInt=(int)~(theInt-1); - } - - PRBool isfirst=PR_TRUE; - while(mask1>=1) { - PRInt32 theDiv=theInt/mask1; - if((theDiv) || (!isfirst)) { - buf[charpos++]="0123456789abcdef"[theDiv]; - isfirst=PR_FALSE; - } - theInt-=theDiv*mask1; - mask1/=aRadix; - } - Append(buf); -} - - -/** - * Append the given float to this string - * @update gess 01/04/99 - * @param aFloat: - * @return - */ -void nsCString::AppendFloat( double aFloat ){ - char buf[40]; - // Use nsStrPrivate::cnvtf, which is locale-insensitive, instead of the - // locale-sensitive PR_snprintf or sprintf(3) - nsStrPrivate::cnvtf(buf, sizeof(buf), 6, aFloat); - Append(buf); -} - -/** - * append given string to this string; - * @update gess 01/04/99 - * @param aString : string to be appended to this - * @return this - */ -void nsCString::AppendWithConversion(const nsString& aString,PRInt32 aCount) { - - if(aCount<0) - aCount=aString.mLength; - else aCount=MinInt(aCount,aString.mLength); - - if(0::length(aPtr); - // We don't know the capacity, so we'll just have to assume - // capacity = length. - nsStrPrivate::Initialize(*this, aPtr, aLength, aLength, eOneByte, PR_TRUE); -} - -nsCString::size_type -nsCString::Mid( self_type& aResult, index_type aStartPos, size_type aLengthToCopy ) const - { - // If we're just assigning our entire self, give |aResult| the opportunity to share - if ( aStartPos == 0 && aLengthToCopy >= Length() ) - aResult = *this; - else - aResult = Substring(*this, aStartPos, aLengthToCopy); - - return aResult.Length(); - } - - -/********************************************************************** - Searching methods... - *********************************************************************/ - - -/** - * Search for given cstr within this string - * - * @update gess 3/25/98 - * @param aCString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsCString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{ - PRInt32 result=kNotFound; - if(aCString) { - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - temp.mLength = nsCharTraits::length(aCString); - temp.mStr=(char*)aCString; - result=nsStrPrivate::FindSubstr1in1(*this,temp,aIgnoreCase,anOffset,aCount); - } - return result; -} - - -/** - * Search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsCString::Find(const nsCString& aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{ - PRInt32 result=nsStrPrivate::FindSubstr1in1(*this,aString,aIgnoreCase,anOffset,aCount); - return result; -} - -/** - * This searches this string for a given character - * - * @update gess 2/04/00 - * @param char is the character you're trying to find. - * @param anOffset tells us where to start the search; -1 means start at 0. - * @param aCount tell us how many chars to search from offset; -1 means use full length. - * @return index in aDest where member of aSet occurs, or -1 if not found - */ -PRInt32 nsCString::FindChar(PRUnichar aChar,PRInt32 anOffset,PRInt32 aCount) const{ - if (anOffset < 0) - anOffset=0; - - if (aCount < 0) - aCount = (PRInt32)mLength; - - if ((aChar < 256) && (0 < mLength) && - ((PRUint32)anOffset < mLength) && (0 < aCount)) { - // We'll only search if the given aChar is 8-bit, - // since this string is 8-bit. - - PRUint32 last = anOffset + aCount; - PRUint32 end = (last < mLength) ? last : mLength; - PRUint32 searchLen = end - anOffset; // Will be > 0 by the conditions above - const char* leftp = mStr + anOffset; - unsigned char theChar = (unsigned char) aChar; - - const char* result = (const char*)memchr(leftp, (int)theChar, searchLen); - - if (result) - return result - mStr; - } - - return kNotFound; -} - -/** - * This method finds the offset of the first char in this string that is - * a member of the given charset, starting the search at anOffset - * - * @update gess 3/25/98 - * @param aCStringSet - * @param anOffset -- where in this string to start searching - * @return - */ -PRInt32 nsCString::FindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{ - if (anOffset < 0) - anOffset = 0; - - if(*aCStringSet && (PRUint32)anOffset < mLength) { - // Build filter that will be used to filter out characters with - // bits that none of the terminal chars have. This works very well - // because searches are often done for chars with only the last - // 4-6 bits set and normal ascii letters have bit 7 set. Other - // letters have even higher bits set. - - // Calculate filter - char filter = nsStrPrivate::GetFindInSetFilter(aCStringSet); - - const char* endChar = mStr + mLength; - for(char *charp = mStr + anOffset; charp < endChar; ++charp) { - char currentChar = *charp; - // Check if all bits are in the required area - if (currentChar & filter) { - // They were not. Go on with the next char. - continue; - } - - // Test all chars - const char *charInSet = aCStringSet; - char setChar = *charInSet; - while (setChar) { - if (setChar == currentChar) { - // Found it! - return charp - mStr; // The index of the found char - } - setChar = *(++charInSet); - } - } // end for all chars in the string - } - return kNotFound; -} - -PRInt32 nsCString::FindCharInSet(const nsCString& aSet,PRInt32 anOffset) const{ - // This assumes that the set doesn't contain the null char. - PRInt32 result = FindCharInSet(aSet.get(), anOffset); - return result; -} - -/** - * Reverse search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsCString::RFind(const nsCString& aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{ - PRInt32 result=nsStrPrivate::RFindSubstr1in1(*this,aString,aIgnoreCase,anOffset,aCount); - return result; -} - - -/** - * Reverse search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsCString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{ - NS_ASSERTION(0!=aString,kNullPointerError); - - PRInt32 result=kNotFound; - if(aString) { - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - temp.mLength=nsCharTraits::length(aString); - temp.mStr=(char*)aString; - result=nsStrPrivate::RFindSubstr1in1(*this,temp,aIgnoreCase,anOffset,aCount); - } - return result; -} - -/** - * This reverse searches this string for a given character - * - * @update gess 2/04/00 - * @param char is the character you're trying to find. - * @param aIgnorecase indicates case sensitivity of search - * @param anOffset tells us where to start the search; -1 means start at 0. - * @param aCount tell us how many chars to search from offset; -1 means use full length. - * @return index in aDest where member of aSet occurs, or -1 if not found - */ -PRInt32 nsCString::RFindChar(PRUnichar aChar,PRInt32 anOffset,PRInt32 aCount) const{ - PRInt32 result=nsStrPrivate::RFindChar1(*this,aChar,anOffset,aCount); - return result; -} - -/** - * Reverse search for char in this string that is also a member of given charset - * - * @update gess 3/25/98 - * @param aCStringSet - * @param anOffset - * @return offset of found char, or -1 (kNotFound) - */ -PRInt32 nsCString::RFindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{ - NS_ASSERTION(0!=aCStringSet,kNullPointerError); - - if (anOffset < 0 || (PRUint32)anOffset > mLength-1) - anOffset = mLength-1; - - if(*aCStringSet) { - // Build filter that will be used to filter out characters with - // bits that none of the terminal chars have. This works very well - // because searches are often done for chars with only the last - // 4-6 bits set and normal ascii letters have bit 7 set. Other - // letters have even higher bits set. - - // Calculate filter - char filter = nsStrPrivate::GetFindInSetFilter(aCStringSet); - - const char* end = mStr; - for(char *charp = mStr + anOffset; charp > end; --charp) { - char currentChar = *charp; - // Check if all bits are in the required area - if (currentChar & filter) { - // They were not. Go on with the next char. - continue; - } - - // Test all chars - const char *setp = aCStringSet; - char setChar = *setp; - while (setChar) { - if (setChar == currentChar) { - // Found it! - return charp - mStr; - } - setChar = *(++setp); - } - } // end for all chars in the string - } - return kNotFound; -} - -PRInt32 nsCString::RFindCharInSet(const nsCString& aSet,PRInt32 anOffset) const{ - // Assumes that the set doesn't contain any nulls. - PRInt32 result = RFindCharInSet(aSet.get(), anOffset); - return result; -} - -/************************************************************** - COMPARISON METHODS... - **************************************************************/ - -/** - * Compares given cstring to this string. - * @update gess 01/04/99 - * @param aCString points to a cstring - * @param aIgnoreCase tells us how to treat case - * @param aCount tells us how many chars to test; -1 implies full length - * @return -1,0,1 - */ -PRInt32 nsCString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount) const { - NS_ASSERTION(0!=aCString,kNullPointerError); - - if(aCString) { - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - temp.mLength=nsCharTraits::length(aCString); - temp.mStr=(char*)aCString; - return nsStrPrivate::StrCompare1To1(*this,temp,aCount,aIgnoreCase); - } - return 0; -} - -PRBool nsCString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { - return EqualsWithConversion(aString,PR_TRUE,aLength); -} - -/** - * Compare this to given string; note that we compare full strings here. - * - * @param aString is the CString to be compared - * @param aCount tells us how many chars you want to compare starting with start of string - * @param aIgnorecase tells us whether to be case sensitive - * @param aCount tells us how many chars to test; -1 implies full length - * @return TRUE if equal - */ -PRBool nsCString::EqualsWithConversion(const char* aCString,PRBool aIgnoreCase,PRInt32 aCount) const{ - PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); - PRBool result=PRBool(0==theAnswer); - return result; -} - -//---------------------------------------------------------------------- - -NS_ConvertUTF16toUTF8::NS_ConvertUTF16toUTF8( const PRUnichar* aString ) - { - if (!aString) - // Leave us as an uninitialized nsCAutoString. - return; - Init(aString, nsCharTraits::length(aString)); - } - -NS_ConvertUTF16toUTF8::NS_ConvertUTF16toUTF8( const PRUnichar* aString, PRUint32 aLength ) - { - if (!aString) - // Leave us as an uninitialized nsCAutoString. - return; - Init(aString, aLength); - } - -NS_ConvertUTF16toUTF8::NS_ConvertUTF16toUTF8( const nsASingleFragmentString& aString ) - { - nsASingleFragmentString::const_char_iterator start; - Init(aString.BeginReading(start), aString.Length()); - } - -NS_ConvertUTF16toUTF8::NS_ConvertUTF16toUTF8( const nsAString& aString ) - { - // Compute space required: do this once so we don't incur multiple - // allocations. This "optimization" is probably of dubious value... - - nsAString::const_iterator start, end; - CalculateUTF8Size calculator; - copy_string(aString.BeginReading(start), aString.EndReading(end), - calculator); - - PRUint32 count = calculator.Size(); - - if (count) - { - // Grow the buffer if we need to. - SetCapacity(count); - - // All ready? Time to convert - - ConvertUTF16toUTF8 converter(mStr); - copy_string(aString.BeginReading(start), aString.EndReading(end), - converter).write_terminator(); - mLength = converter.Size(); - if (mLength != count) - { - NS_ERROR("Input invalid or incorrect length was calculated"); - Truncate(); - } - } - } - -void NS_ConvertUTF16toUTF8::Init( const PRUnichar* aString, PRUint32 aLength ) - { - // Compute space required: do this once so we don't incur multiple - // allocations. This "optimization" is probably of dubious value... - - CalculateUTF8Size calculator; - calculator.write(aString, aLength); - - PRUint32 count = calculator.Size(); - - if (count) - { - // Grow the buffer if we need to. - SetCapacity(count); - - // All ready? Time to convert - - ConvertUTF16toUTF8 converter(mStr); - converter.write(aString, aLength); - mLength = converter.Size(); - mStr[mLength] = char_type(0); - if (mLength != count) - { - NS_ERROR("Input invalid or incorrect length was calculated"); - Truncate(); - } - } - } - -NS_LossyConvertUTF16toASCII::NS_LossyConvertUTF16toASCII( const nsAString& aString ) - { - SetCapacity(aString.Length()); - - nsAString::const_iterator start; aString.BeginReading(start); - nsAString::const_iterator end; aString.EndReading(end); - - while (start != end) { - nsReadableFragment frag(start.fragment()); - AppendWithConversion(frag.mStart, frag.mEnd - frag.mStart); - start.advance(start.size_forward()); - } - } - - -/*********************************************************************** - IMPLEMENTATION NOTES: AUTOSTRING... - ***********************************************************************/ - - -/** - * Default constructor - * - */ -nsCAutoString::nsCAutoString() : nsCString(){ - nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); - AddNullTerminator(*this); - -} - -nsCAutoString::nsCAutoString( const nsCString& aString ) : nsCString(){ - nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); - AddNullTerminator(*this); - Append(aString); -} - -nsCAutoString::nsCAutoString( const nsACString& aString ) : nsCString(){ - nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); - AddNullTerminator(*this); - Append(aString); -} - -nsCAutoString::nsCAutoString(const char* aCString) : nsCString() { - nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); - AddNullTerminator(*this); - Append(aCString); -} - -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() { - nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); - AddNullTerminator(*this); - Append(aCString,aLength); -} - -/** - * Copy construct using an external buffer descriptor - * @param aBuffer -- descibes external buffer - */ -nsCAutoString::nsCAutoString(const CBufDescriptor& aBuffer) : nsCString() { - if(!aBuffer.mBuffer) { - nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); - } - else { - nsStrPrivate::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); - } - if(!aBuffer.mIsConst) - AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. -} diff --git a/mozilla/xpcom/string/obsolete/nsString.h b/mozilla/xpcom/string/obsolete/nsString.h deleted file mode 100644 index ef13a352254..00000000000 --- a/mozilla/xpcom/string/obsolete/nsString.h +++ /dev/null @@ -1,480 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * 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 the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Rick Gessner (original author) - * Scott Collins - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -/* - * nsString.h --- rickg's original strings of 1-byte chars, |nsCString| - * and |nsCAutoString|; these classes will be replaced by the new - * shared-buffer string (see bug #53065) - * - * This file will one day be _only_ a compatibility header for clients - * using the names |ns[C]String| et al ... which we probably want to - * support forever. - */ - -#ifndef nsString_h__ -#define nsString_h__ - -#ifndef nsString2_h__ -#include "nsString2.h" -#endif - -class NS_COM nsCString : - public nsAFlatCString, - public nsStr { - -public: - friend class nsString; - friend NS_COM void ToUpperCase( nsCString& ); - friend NS_COM void ToLowerCase( nsCString& ); - -protected: - virtual const nsBufferHandle* GetFlatBufferHandle() const; - virtual const char* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; - virtual char* GetWritableFragment( nsWritableFragment&, nsFragmentRequest, PRUint32 ); - -public: - virtual const char* get() const { return mStr; } - - PRBool IsEmpty() const { return mLength == 0; } - -public: - /** - * Default constructor. - */ - nsCString(); - - /** - * This is our copy constructor - * @param reference to another nsCString - */ - nsCString(const nsCString& aString); - - explicit nsCString( const nsACString& ); - - explicit nsCString(const char*); - nsCString(const char*, PRInt32); - - /** - * Destructor - * - */ - virtual ~nsCString(); - - /** - * Retrieve the length of this string - * @return string length - */ - virtual PRUint32 Length() const { return mLength; } - - /** - * Call this method if you want to force a different string capacity - * @update gess7/30/98 - * @param aLength -- contains new length for mStr - * @return - */ - void SetLength(PRUint32 aLength); - - /** - * Sets the new length of the string. - * @param aLength is new string length. - * @return nada - */ - void SetCapacity(PRUint32 aLength); - - /********************************************************************** - Accessor methods... - *********************************************************************/ - - PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); - - /********************************************************************** - Lexomorphic transforms... - *********************************************************************/ - - - /** - * This method is used to remove all occurances of the - * characters found in aSet from this string. - * - * @param aSet -- characters to be cut from this - * @return *this - */ - void StripChars(const char* aSet); - void StripChar(char aChar,PRInt32 anOffset=0); - - /** - * This method strips whitespace throughout the string - * - * @return this - */ - void StripWhitespace(); - - /** - * swaps occurence of 1 string for another - * - * @return this - */ - void ReplaceChar(char aOldChar,char aNewChar); - void ReplaceChar(const char* aSet,char aNewChar); - - void ReplaceSubstring(const nsCString& aTarget,const nsCString& aNewValue); - void ReplaceSubstring(const char* aTarget,const char* aNewValue); - - /** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @param aTrimSet -- contains chars to be trimmed from - * both ends - * @param aEliminateLeading - * @param aEliminateTrailing - * @param aIgnoreQuotes - * @return this - */ - void Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE,PRBool aIgnoreQuotes=PR_FALSE); - - /** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. - * - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this - */ - void CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); - - /********************************************************************** - string conversion methods... - *********************************************************************/ - - /** - * Perform string to float conversion. - * @param aErrorCode will contain error if one occurs - * @return float rep of string value - */ - float ToFloat(PRInt32* aErrorCode) const; - - - /** - * Perform string to int conversion. - * @param aErrorCode will contain error if one occurs - * @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you. - * @return int rep of string value, and possible (out) error code - */ - PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const; - - - /********************************************************************** - String manipulation methods... - *********************************************************************/ - - /** - * assign given string to this string - * @param aStr: buffer to be assigned to this - * @param aCount is the length of the given str (or -1) if you want me to determine its length - * NOTE: IFF you pass -1 as aCount, then your buffer must be null terminated. - * - * @return this - */ - - nsCString& operator=( const nsCString& aString ) { Assign(aString); return *this; } - nsCString& operator=( const nsACString& aReadable ) { Assign(aReadable); return *this; } -//nsCString& operator=( const nsPromiseReadable& aReadable ) { Assign(aReadable); return *this; } - nsCString& operator=( const char* aPtr ) { Assign(aPtr); return *this; } - nsCString& operator=( char aChar ) { Assign(aChar); return *this; } - - void AssignWithConversion(const PRUnichar*,PRInt32=-1); - void AssignWithConversion( const nsString& aString ); - void AssignWithConversion( const nsAString& aString ); - - /* - * Appends n characters from given string to this, - * - * @param aString is the source to be appended to this - * @param aCount -- number of chars to copy; -1 tells us to compute the strlen for you - * NOTE: IFF you pass -1 as aCount, then your buffer must be null terminated. - * - * @return number of chars copied - */ - - void AppendWithConversion(const nsString&, PRInt32=-1); - void AppendWithConversion( const nsAString& aString ); - void AppendWithConversion(const PRUnichar*, PRInt32=-1); - // Why no |AppendWithConversion(const PRUnichar*, PRInt32)|? --- now I know, because implicit construction hid the need for this routine - void AppendInt(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 - void AppendFloat( double aFloat ); - - -#if 0 - virtual void do_AppendFromReadable( const nsACString& ); - virtual void do_InsertFromReadable( const nsACString&, PRUint32 ); -#endif - - - // Takes ownership of aPtr, sets the current length to aLength if specified. - void Adopt( char* aPtr, PRInt32 aLength = -1 ); - - /* - |Left|, |Mid|, and |Right| are annoying signatures that seem better almost - any _other_ way than they are now. Consider these alternatives - - aWritable = aReadable.Left(17); // ...a member function that returns a |Substring| - aWritable = Left(aReadable, 17); // ...a global function that returns a |Substring| - Left(aReadable, 17, aWritable); // ...a global function that does the assignment - - as opposed to the current signature - - aReadable.Left(aWritable, 17); // ...a member function that does the assignment - - or maybe just stamping them out in favor of |Substring|, they are just duplicate functionality - - aWritable = Substring(aReadable, 0, 17); - */ - - size_type Left( self_type&, size_type ) const; - size_type Mid( self_type&, PRUint32, PRUint32 ) const; - size_type Right( self_type&, size_type ) const; - - - /********************************************************************** - Searching methods... - *********************************************************************/ - - - /** - * Search for given substring within this string - * - * @param aString is substring to be sought in this - * @param aIgnoreCase selects case sensitivity - * @param anOffset tells us where in this strig to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ - PRInt32 Find(const nsCString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=0,PRInt32 aCount=-1) const; - PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=0,PRInt32 aCount=-1) const; - /** - * Search for given char within this string - * - * @param aString is substring to be sought in this - * @param anOffset tells us where in this strig to start searching - * @param aIgnoreCase selects case sensitivity - * @param aCount tells us how many iterations to make starting at the given offset - * @return find pos in string, or -1 (kNotFound) - */ - PRInt32 FindChar(PRUnichar aChar,PRInt32 anOffset=0,PRInt32 aCount=-1) const; - - /** - * This method searches this string for the first character - * found in the given charset - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ - PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; - PRInt32 FindCharInSet(const nsCString& aString,PRInt32 anOffset=0) const; - - - /** - * This methods scans the string backwards, looking for the given string - * @param aString is substring to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ - PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aCount=-1) const; - PRInt32 RFind(const nsCString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aCount=-1) const; - - /** - * Search for given char within this string - * - * @param aString is substring to be sought in this - * @param anOffset tells us where in this strig to start searching - * @param aIgnoreCase selects case sensitivity - * @return find pos in string, or -1 (kNotFound) - */ - PRInt32 RFindChar(PRUnichar aChar,PRInt32 anOffset=-1,PRInt32 aCount=-1) const; - - /** - * This method searches this string for the last character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ - PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const; - PRInt32 RFindCharInSet(const nsCString& aString,PRInt32 anOffset=-1) const; - - - - /********************************************************************** - Comparison methods... - *********************************************************************/ - - /** - * Compares a given string type to this string. - * @update gess 7/27/98 - * @param S is the string to be compared - * @param aIgnoreCase tells us how to treat case - * @param aCount tells us how many chars to compare - * @return -1,0,1 - */ - PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; - - PRBool EqualsWithConversion(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; - - PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; - -private: - // NOT TO BE IMPLEMENTED - // these signatures help clients not accidentally call the wrong thing helped by C++ automatic integral promotion - void operator=( PRUnichar ); - void AssignWithConversion( const char*, PRInt32=-1 ); - void InsertWithConversion( char, PRUint32 ); -}; - -inline -nsCString::size_type -nsCString::Left( nsACString& aResult, size_type aLengthToCopy ) const - { - return Mid(aResult, 0, aLengthToCopy); - } - -inline -nsCString::size_type -nsCString::Right( self_type& aResult, size_type aLengthToCopy ) const - { - size_type myLength = Length(); - aLengthToCopy = NS_MIN(myLength, aLengthToCopy); - return Mid(aResult, myLength-aLengthToCopy, aLengthToCopy); - } - -// NS_DEF_STRING_COMPARISON_OPERATORS(nsCString, char) -// NS_DEF_DERIVED_STRING_OPERATOR_PLUS(nsCString, char) - - -/************************************************************** - Here comes the AutoString class which uses internal memory - (typically found on the stack) for its default buffer. - If the buffer needs to grow, it gets reallocated on the heap. - **************************************************************/ - -class NS_COM nsCAutoString : public nsCString { -public: - - virtual ~nsCAutoString() {} - - nsCAutoString(); - explicit nsCAutoString(const nsCString& ); - explicit nsCAutoString(const nsACString& aString); - explicit nsCAutoString(const char* aString); - nsCAutoString(const char* aString,PRInt32 aLength); - explicit nsCAutoString(const CBufDescriptor& aBuffer); - - nsCAutoString& operator=( const nsCAutoString& aString ) { Assign(aString); return *this; } - private: - void operator=( PRUnichar ); // NOT TO BE IMPLEMENTED - public: - nsCAutoString& operator=( const nsACString& aReadable ) { Assign(aReadable); return *this; } -// nsCAutoString& operator=( const nsPromiseReadable& aReadable ) { Assign(aReadable); return *this; } - nsCAutoString& operator=( const char* aPtr ) { Assign(aPtr); return *this; } - nsCAutoString& operator=( char aChar ) { Assign(aChar); return *this; } - - char mBuffer[kDefaultStringSize]; -}; - -// NS_DEF_DERIVED_STRING_OPERATOR_PLUS(nsCAutoString, char) - -/** - * A helper class that converts a UTF-16 string to UTF-8 - */ -class NS_COM NS_ConvertUTF16toUTF8 - : public nsCAutoString - /* - ... - */ - { - public: - explicit NS_ConvertUTF16toUTF8( const PRUnichar* aString ); - NS_ConvertUTF16toUTF8( const PRUnichar* aString, PRUint32 aLength ); - explicit NS_ConvertUTF16toUTF8( const nsAString& aString ); - explicit NS_ConvertUTF16toUTF8( const nsASingleFragmentString& aString ); - - protected: - void Init( const PRUnichar* aString, PRUint32 aLength ); - - private: - // NOT TO BE IMPLEMENTED - NS_ConvertUTF16toUTF8( char ); - }; - - -/** - * A helper class that converts a UTF-16 string to ASCII in a lossy manner - */ -class NS_COM NS_LossyConvertUTF16toASCII - : public nsCAutoString - /* - ... - */ - { - public: - explicit - NS_LossyConvertUTF16toASCII( const PRUnichar* aString ) - { - AppendWithConversion( aString, ~PRUint32(0) /* MAXINT */); - } - - NS_LossyConvertUTF16toASCII( const PRUnichar* aString, PRUint32 aLength ) - { - AppendWithConversion( aString, aLength ); - } - - explicit NS_LossyConvertUTF16toASCII( const nsAString& aString ); - - private: - // NOT TO BE IMPLEMENTED - NS_LossyConvertUTF16toASCII( char ); - }; - -// Backward compatibility -typedef NS_ConvertUTF16toUTF8 NS_ConvertUCS2toUTF8; -typedef NS_LossyConvertUTF16toASCII NS_LossyConvertUCS2toASCII; - -#endif /* !defined(nsString_h__) */ diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp deleted file mode 100644 index 2b87c4d559e..00000000000 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ /dev/null @@ -1,1473 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: NPL 1.1/GPL 2.0/LGPL 2.1 - * - * 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 the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Rick Gessner (original author) - * Scott Collins - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the NPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the NPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include -#include -#include -#include "nsStrPrivate.h" -#include "nsString.h" -#include "nsReadableUtils.h" -#include "nsDebug.h" -#include "nsUTF8Utils.h" - -#ifndef nsCharTraits_h___ -#include "nsCharTraits.h" -#endif - -#ifndef RICKG_TESTBED -#include "prdtoa.h" -#endif - -#ifdef DEBUG -static const char* kPossibleNull = "Error: possible unintended null in string"; -static const char* kNullPointerError = "Error: unexpected null ptr"; -#endif -static const char* kWhitespace="\b\t\r\n "; - -const nsBufferHandle* -nsString::GetFlatBufferHandle() const - { - return NS_REINTERPRET_CAST(const nsBufferHandle*, 1); - } - -/** - * Default constructor. - */ -nsString::nsString() { - nsStrPrivate::Initialize(*this,eTwoByte); -} - -nsString::nsString(const PRUnichar* aString) { - nsStrPrivate::Initialize(*this,eTwoByte); - Assign(aString); -} - -/** - * This constructor accepts a unicode string - * @update gess 1/4/99 - * @param aString is a ptr to a unichar string - * @param aLength tells us how many chars to copy from given aString - */ -nsString::nsString(const PRUnichar* aString,PRInt32 aCount) { - nsStrPrivate::Initialize(*this,eTwoByte); - Assign(aString,aCount); -} - -/** - * This is our copy constructor - * @update gess 1/4/99 - * @param reference to another nsString - */ -nsString::nsString(const nsString& aString) { - nsStrPrivate::Initialize(*this,eTwoByte); - nsStrPrivate::StrAssign(*this,aString,0,aString.mLength); -} - -/** - * Destructor - * Make sure we call nsStrPrivate::Destroy. - */ -nsString::~nsString() { - nsStrPrivate::Destroy(*this); -} - -const PRUnichar* nsString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const { - switch ( aRequest ) { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mEnd = (aFragment.mStart = mUStr) + mLength; - return aFragment.mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } -} - -PRUnichar* nsString::GetWritableFragment( nsWritableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) { - switch ( aRequest ) { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mEnd = (aFragment.mStart = mUStr) + mLength; - return aFragment.mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } -} - - -void -nsString::do_AppendFromElement( PRUnichar inChar ) - { - PRUnichar buf[2] = { 0, 0 }; - buf[0] = inChar; - - nsStr temp; - nsStrPrivate::Initialize(temp, eTwoByte); - temp.mUStr = buf; - temp.mLength = 1; - nsStrPrivate::StrAppend(*this, temp, 0, 1); - } - - -nsString::nsString( const nsAString& aReadable ) { - nsStrPrivate::Initialize(*this,eTwoByte); - Assign(aReadable); -} - -/** - * This method truncates this string to given length. - * - * @update gess 01/04/99 - * @param anIndex -- new length of string - * @return nada - */ -void nsString::SetLength(PRUint32 anIndex) { - if ( anIndex > GetCapacity() ) - SetCapacity(anIndex); - // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAString.h), - // we can only use it since our local implementation, |nsString::SetCapacity|, is known to do what we want - nsStrPrivate::StrTruncate(*this,anIndex); -} - - -/** - * Call this method if you want to force the string to a certain capacity; - * |SetCapacity(0)| discards associated storage. - * - * @param aNewCapacity -- desired minimum capacity - */ -void -nsString::SetCapacity( PRUint32 aNewCapacity ) - { - if ( aNewCapacity ) - { - if( aNewCapacity > GetCapacity() ) - nsStrPrivate::GrowCapacity(*this, aNewCapacity); - AddNullTerminator(*this); - } - else - { - nsStrPrivate::Destroy(*this); - nsStrPrivate::Initialize(*this, eTwoByte); - } - } - -/********************************************************************** - Accessor methods... - *********************************************************************/ - - -/** - * set a char inside this string at given index - * @param aChar is the char you want to write into this string - * @param anIndex is the ofs where you want to write the given char - * @return TRUE if successful - */ -PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ - PRBool result=PR_FALSE; - if(anIndex::length(aTarget); - if(0::length(aNewValue); - if(02)) { - theFirstChar=First(); - theLastChar=Last(); - if(theFirstChar==theLastChar) { - if(('\''==theFirstChar) || ('"'==theFirstChar)) { - Cut(0,1); - Truncate(mLength-1); - theQuotesAreNeeded=PR_TRUE; - } - else theFirstChar=0; - } - } - - nsStrPrivate::Trim(*this,aTrimSet,aEliminateLeading,aEliminateTrailing); - - if(aIgnoreQuotes && theQuotesAreNeeded) { - Insert(theFirstChar,0); - Append(theLastChar); - } - - } -} - -/** - * This method strips chars in given set from string. - */ -static void -CompressSet(nsString& aStr, const char* aSet, PRUnichar aChar, - PRBool aEliminateLeading, PRBool aEliminateTrailing) { - if (aSet) { - aStr.ReplaceChar(aSet, aChar); - nsStrPrivate::CompressSet2(aStr, aSet, - aEliminateLeading, aEliminateTrailing); - } -} - -/** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. - * - * @update gess 3/31/98 - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this - */ -void -nsString::CompressWhitespace( PRBool aEliminateLeading,PRBool aEliminateTrailing){ - CompressSet(*this, kWhitespace,' ', aEliminateLeading, aEliminateTrailing); -} - -/********************************************************************** - string conversion methods... - *********************************************************************/ - -/** - * Copies contents of this string into he given buffer - * Note that if you provide me a buffer that is smaller than the length of - * this string, only the number of bytes that will fit are copied. - * - * @update gess 01/04/99 - * @param aBuf - * @param aBufLength -- size of your external buffer (including null) - * @param anOffset -- THIS IS NOT USED AT THIS TIME! - * @return - */ -char* nsString::ToCString(char* aBuf, PRUint32 aBufLength,PRUint32 anOffset) const{ - if(aBuf) { - - // NS_ASSERTION(mLength<=aBufLength,"buffer smaller than string"); - - CBufDescriptor theDescr(aBuf,PR_TRUE,aBufLength,0); - nsCAutoString temp(theDescr); - nsStrPrivate::StrAssign(temp, *this, anOffset, PR_MIN(mLength, aBufLength-1)); - temp.mStr=0; - } - return aBuf; -} - -/** - * Perform string to float conversion. - * @update gess 01/04/99 - * @param aErrorCode will contain error if one occurs - * @return float rep of string value - */ -float nsString::ToFloat(PRInt32* aErrorCode) const { - float res = 0.0f; - char buf[100]; - if (mLength > 0 && mLength < sizeof(buf)) { - char *conv_stopped; - const char *str = ToCString(buf, sizeof(buf)); - // Use PR_strtod, not strtod, since we don't want locale involved. - res = (float)PR_strtod(str, &conv_stopped); - if (*conv_stopped == '\0') { - *aErrorCode = (PRInt32) NS_OK; - } - else { - /* Not all the string was scanned */ - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - } - } - else { - /* The string was too short (0 characters) or too long (sizeof(buf)) */ - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - } - return res; -} - - -/** - * Perform decimal numeric string to int conversion. - * NOTE: In this version, we use the radix you give, even if it's wrong. - * @update gess 02/14/00 - * @param aErrorCode will contain error if one occurs - * @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix. - * @return int rep of string value - */ -PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { - PRUnichar* cp=mUStr; - PRInt32 theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect) - PRInt32 result=0; - PRBool negate=PR_FALSE; - PRUnichar theChar=0; - - //initial value, override if we find an integer - *anErrorCode=NS_ERROR_ILLEGAL_VALUE; - - if(cp) { - - //begin by skipping over leading chars that shouldn't be part of the number... - - PRUnichar* endcp=cp+mLength; - PRBool done=PR_FALSE; - - while((cp='A') && (theChar<='F')) { - if(10==theRadix) { - if(kAutoDetect==aRadix){ - theRadix=16; - cp=first; //backup - result=0; - haveValue = PR_FALSE; - } - else { - *anErrorCode=NS_ERROR_ILLEGAL_VALUE; - result=0; - break; - } - } - else { - result = (theRadix * result) + ((theChar-'A')+10); - haveValue = PR_TRUE; - } - } - else if((theChar>='a') && (theChar<='f')) { - if(10==theRadix) { - if(kAutoDetect==aRadix){ - theRadix=16; - cp=first; //backup - result=0; - haveValue = PR_FALSE; - } - else { - *anErrorCode=NS_ERROR_ILLEGAL_VALUE; - result=0; - break; - } - } - else { - result = (theRadix * result) + ((theChar-'a')+10); - haveValue = PR_TRUE; - } - } - else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) { - continue; - } - else if((('#'==theChar) || ('+'==theChar)) && !haveValue) { - continue; - } - else { - //we've encountered a char that's not a legal number or sign - break; - } - } //while - if(negate) - result=-result; - } //if - } - return result; -} - -/********************************************************************** - String manipulation methods... - *********************************************************************/ - - - -/** - * assign given char* to this string - * @update gess 01/04/99 - * @param aCString: buffer to be assigned to this - * @param aCount -- length of given buffer or -1 if you want me to compute length. - * NOTE: IFF you pass -1 as aCount, then your buffer must be null terminated. - * - * @return this - */ -void nsString::AssignWithConversion(const char* aCString,PRInt32 aCount) { - nsStrPrivate::StrTruncate(*this,0); - if(aCString){ - AppendWithConversion(aCString,aCount); - } -} - -void nsString::AssignWithConversion(const char* aCString) { - nsStrPrivate::StrTruncate(*this,0); - if(aCString){ - AppendWithConversion(aCString); - } -} - - -/** - * append given c-string to this string - * @update gess 01/04/99 - * @param aString : string to be appended to this - * @param aCount -- length of given buffer or -1 if you want me to compute length. - * NOTE: IFF you pass -1 as aCount, then your buffer must be null terminated. - * - * @return this - */ -void nsString::AppendWithConversion(const char* aCString,PRInt32 aCount) { - if(aCString && aCount){ //if astring is null or count==0 there's nothing to do - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - temp.mStr = NS_CONST_CAST(char*, aCString); - - if(0::length(aCString); - - if(0=1) { - PRInt32 theDiv=theInt/mask1; - if((theDiv) || (!isfirst)) { - buf[charpos++]="0123456789abcdef"[theDiv]; - isfirst=PR_FALSE; - } - theInt-=theDiv*mask1; - mask1/=aRadix; - } - AppendWithConversion(buf); -} - - -/** - * Append the given float to this string - * @update gess 01/04/99 - * @param aFloat: - * @return - */ -void nsString::AppendFloat(double aFloat){ - char buf[40]; - // Use nsStrPrivate::cnvtf, which is locale-insensitive, instead of the - // locale-sensitive PR_snprintf or sprintf(3) - nsStrPrivate::cnvtf(buf, sizeof(buf), 6, aFloat); - AppendWithConversion(buf); -} - - - -/** - * Insert a char* into this string at a specified offset. - * - * @update gess4/22/98 - * @param char* aCString to be inserted into this string - * @param anOffset is insert pos in str - * @param aCount -- length of given buffer or -1 if you want me to compute length. - * NOTE: IFF you pass -1 as aCount, then your buffer must be null terminated. - * - * @return this - */ -void nsString::InsertWithConversion(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ - if(aCString && aCount){ - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - temp.mStr = NS_CONST_CAST(char*, aCString); - - if(0::length(aCString); - - if(0::length(aPtr); - // We don't know the capacity, so we'll just have to assume - // capacity = length. - nsStrPrivate::Initialize(*this, (char*)aPtr, aLength, aLength, eTwoByte, PR_TRUE); -} - -nsAString::size_type -nsString::Mid( self_type& aResult, index_type aStartPos, size_type aLengthToCopy ) const - { - // If we're just assigning our entire self, give |aResult| the opportunity to share - if ( aStartPos == 0 && aLengthToCopy >= Length() ) - aResult = *this; - else - aResult = Substring(*this, aStartPos, aLengthToCopy); - - return aResult.Length(); - } - - -/********************************************************************** - Searching methods... - *********************************************************************/ - - /** - * Search for given character within this string - * - * @param aChar is the character to search for - * @param anOffset tells us where in this string to start searching - (optional parameter) - * @param aCount tells us how far from the offset we are to search. Use - -1 to search the whole string. (optional parameter) - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsString::FindChar(PRUnichar aChar, PRInt32 anOffset/*=0*/, PRInt32 aCount/*=-1*/) const{ - if (anOffset < 0) - anOffset=0; - - if (aCount < 0) - aCount = (PRInt32)mLength; - - if ((0 < mLength) && ((PRUint32)anOffset < mLength) && (0 < aCount)) { - PRUint32 last = anOffset + aCount; - PRUint32 end = (last < mLength) ? last : mLength; - const PRUnichar* charp = mUStr + anOffset; - const PRUnichar* endp = mUStr + end; - - while (charp < endp && *charp != aChar) { - ++charp; - } - - if (charp < endp) - return charp - mUStr; - } - return kNotFound; -} - -/** - * search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{ - NS_ASSERTION(0!=aCString,kNullPointerError); - - PRInt32 result=kNotFound; - if(aCString) { - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - temp.mLength=nsCharTraits::length(aCString); - temp.mStr = NS_CONST_CAST(char*, aCString); - result=nsStrPrivate::FindSubstr1in2(*this,temp,aIgnoreCase,anOffset,aCount); - } - return result; -} - -/** - * search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsString::Find(const PRUnichar* aString,PRInt32 anOffset,PRInt32 aCount) const{ - NS_ASSERTION(0!=aString,kNullPointerError); - - PRInt32 result=kNotFound; - if(aString) { - nsStr temp; - nsStrPrivate::Initialize(temp,eTwoByte); - temp.mLength = nsCharTraits::length(aString); - temp.mUStr = NS_CONST_CAST(PRUnichar*, aString); - result=nsStrPrivate::FindSubstr2in2(*this,temp,anOffset,aCount); - } - return result; -} - -/** - * search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsString::Find(const nsAFlatString& aString,PRInt32 anOffset,PRInt32 aCount) const{ - - nsStr temp; - nsStrPrivate::Initialize(temp, eTwoByte); - temp.mLength = aString.Length(); - temp.mUStr = NS_CONST_CAST(PRUnichar*, aString.get()); - - PRInt32 result=nsStrPrivate::FindSubstr2in2(*this,temp,anOffset,aCount); - return result; -} - -/** - * This method finds the offset of the first char in this string that is - * a member of the given charset, starting the search at anOffset - * - * @update gess 3/25/98 - * @param aCStringSet - * @param anOffset -- where in this string to start searching - * @return - */ -PRInt32 nsString::FindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{ - NS_ASSERTION(0!=aCStringSet,kNullPointerError); - - if (anOffset < 0) - anOffset = 0; - - if(*aCStringSet && (PRUint32)anOffset < mLength) { - // Build filter that will be used to filter out characters with - // bits that none of the terminal chars have. This works very well - // because searches are often done for chars with only the last - // 4-6 bits set and normal ascii letters have bit 7 set. Other - // letters have even higher bits set. - - // Calculate filter - PRUnichar filter = (~PRUnichar(0)^~char(0)) | - nsStrPrivate::GetFindInSetFilter(aCStringSet); - - const PRUnichar* endChar = mUStr + mLength; - for(PRUnichar *charp = mUStr + anOffset; charp < endChar; ++charp) { - PRUnichar currentChar = *charp; - // Check if all bits are in the required area - if (currentChar & filter) { - // They were not. Go on with the next char. - continue; - } - - // Test all chars - const char *setp = aCStringSet; - PRUnichar setChar = PRUnichar(*setp); - while (setChar) { - if (setChar == currentChar) { - // Found it! - return charp - mUStr; // The index of the found char - } - setChar = PRUnichar(*(++setp)); - } - } // end for all chars in the string - } - return kNotFound; -} - -/** - * This method finds the offset of the first char in this string that is - * a member of the given charset, starting the search at anOffset - * - * @update gess 3/25/98 - * @param aCStringSet - * @param anOffset -- where in this string to start searching - * @return - */ -PRInt32 nsString::FindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{ - NS_ASSERTION(0!=aStringSet,kNullPointerError); - - if (anOffset < 0) - anOffset = 0; - - if (*aStringSet && (PRUint32)anOffset < mLength) { - // Build filter that will be used to filter out characters with - // bits that none of the terminal chars have. This works very well - // because searches are often done for chars with only the last - // 4-6 bits set and normal ascii letters have bit 7 set. Other - // letters have even higher bits set. - - // Calculate filter - PRUnichar filter = nsStrPrivate::GetFindInSetFilter(aStringSet); - - const PRUnichar* endChar = mUStr + mLength; - for(PRUnichar *charp = mUStr + anOffset;charp < endChar; ++charp) { - PRUnichar currentChar = *charp; - // Check if all bits are in the required area - if (currentChar & filter) { - // They were not. Go on with the next char. - continue; - } - - // Test all chars - const PRUnichar *setp = aStringSet; - PRUnichar setChar = *setp; - while (setChar) { - if (setChar == currentChar) { - // Found it! - return charp - mUStr; // The index of the found char - } - setChar = *(++setp); - } - } // end for all chars in the string - } - return kNotFound; -} - -/** - * Reverse search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsString::RFind(const nsAFlatString& aString,PRInt32 anOffset,PRInt32 aCount) const -{ - nsStr temp; - nsStrPrivate::Initialize(temp, eTwoByte); - temp.mLength = aString.Length(); - temp.mUStr = NS_CONST_CAST(PRUnichar*, aString.get()); - PRInt32 result=nsStrPrivate::RFindSubstr2in2(*this,temp,anOffset,aCount); - return result; -} - -PRInt32 nsString::RFind(const PRUnichar* aString, PRInt32 anOffset, PRInt32 aCount) const -{ - PRInt32 result=kNotFound; - if (aString) { - nsStr temp; - nsStrPrivate::Initialize(temp, eTwoByte); - temp.mLength = nsCharTraits::length(aString); - temp.mUStr = NS_CONST_CAST(PRUnichar*, aString); - result=nsStrPrivate::RFindSubstr2in2(*this,temp,anOffset,aCount); - } - return result; -} - -/** - * Reverse search for given string within this string - * - * @update gess 3/25/98 - * @param aString - substr to be found - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param anOffset tells us where in this string to start searching - * @param aCount tells us how many iterations to make starting at the given offset - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 nsString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{ - NS_ASSERTION(0!=aString,kNullPointerError); - - PRInt32 result=kNotFound; - if(aString) { - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - temp.mLength=nsCharTraits::length(aString); - temp.mStr = NS_CONST_CAST(char*, aString); - result=nsStrPrivate::RFindSubstr1in2(*this,temp,aIgnoreCase,anOffset,aCount); - } - return result; -} - -/** - * Reverse search for a given char, starting at given offset - * - * @update gess 3/25/98 - * @param aChar - * @param aIgnoreCase - * @param anOffset - * @return offset of found char, or -1 (kNotFound) - */ -PRInt32 nsString::RFindChar(PRUnichar aChar,PRInt32 anOffset,PRInt32 aCount) const{ - PRInt32 result=nsStrPrivate::RFindChar2(*this,aChar,anOffset,aCount); - return result; -} - -/** - * Reverse search for a first char in this string that is a - * member of the given char set - * - * @update gess 3/25/98 - * @param aSet - * @param aIgnoreCase - * @param anOffset - * @return offset of found char, or -1 (kNotFound) - */ -PRInt32 nsString::RFindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{ - NS_ASSERTION(0!=aStringSet,kNullPointerError); - - if (anOffset < 0 || (PRUint32)anOffset >= mLength) - anOffset = mLength - 1; - - if(*aStringSet) { - // Build filter that will be used to filter out characters with - // bits that none of the terminal chars have. This works very well - // because searches are often done for chars with only the last - // 4-6 bits set and normal ascii letters have bit 7 set. Other - // letters have even higher bits set. - - // Calculate filter - PRUnichar filter = nsStrPrivate::GetFindInSetFilter(aStringSet); - - const PRUnichar* endp = mUStr - 1; - for(PRUnichar *charp = mUStr + anOffset; charp > endp; --charp) { - PRUnichar currentChar = *charp; - // Check if all bits are in the required area - if (currentChar & filter) { - // They were not. Go on with the next char. - continue; - } - - // Test all chars - const PRUnichar* setp = aStringSet; - PRUnichar setChar = *setp; - while (setChar) { - if (setChar == currentChar) { - // Found it! - return charp - mUStr; - } - setChar = *(++setp); - } - } // end for all chars in the string - } - return kNotFound; -} - - -/************************************************************** - COMPARISON METHODS... - **************************************************************/ - -/** - * Compares given cstring to this string. - * @update gess 01/04/99 - * @param aCString pts to a cstring - * @param aIgnoreCase tells us how to treat case - * @param aCount tells us how many chars to test; -1 implies full length - * @return -1,0,1 - */ -PRInt32 nsString::CompareWithConversion(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount) const { - NS_ASSERTION(0!=aCString,kNullPointerError); - - if(aCString) { - nsStr temp; - nsStrPrivate::Initialize(temp,eOneByte); - - temp.mLength= (0::length(aCString); - - temp.mStr = NS_CONST_CAST(char*, aCString); - return nsStrPrivate::StrCompare2To1(*this,temp,aCount,aIgnoreCase); - } - - return 0; -} - -PRBool nsString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { - return EqualsWithConversion(aString,PR_TRUE,aLength); -} - -/** - * Compare this to given c-string; note that we compare full strings here. - * - * @param aString is the CString to be compared - * @param aIgnorecase tells us whether to be case sensitive - * @param aCount tells us how many chars to test; -1 implies full length - * @return TRUE if equal - */ -PRBool nsString::EqualsWithConversion(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const { - PRInt32 theAnswer=CompareWithConversion(aString,aIgnoreCase,aCount); - PRBool result=PRBool(0==theAnswer); - return result; -} - -PRInt32 Compare2To2(const PRUnichar* aStr1,const PRUnichar* aStr2,PRUint32 aCount); - -/** - * Determine if given char is a valid space character - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -PRBool nsString::IsSpace(PRUnichar aChar) { - // XXX i18n - if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given buffer contains plain ascii - * - * @param aBuffer -- if null, then we test *this, otherwise we test given buffer - * @return TRUE if is all ascii chars, or if strlen==0 - */ -PRBool nsString::IsASCII(const PRUnichar* aBuffer) { - - if(!aBuffer) { - if(eOneByte==GetCharSize()) { - char* aByte = mStr; - while(*aByte) { - if(*aByte & 0x80) { // don't use (*aByte > 0x7F) since char is signed - return PR_FALSE; - } - aByte++; - } - return PR_TRUE; - } else { - aBuffer=mUStr; // let the following code handle it - } - } - if(aBuffer) { - while(*aBuffer) { - if(*aBuffer>0x007F){ - return PR_FALSE; - } - aBuffer++; - } - } - return PR_TRUE; -} - - -/*********************************************************************** - IMPLEMENTATION NOTES: AUTOSTRING... - ***********************************************************************/ - - -/** - * Default constructor - * - */ -nsAutoString::nsAutoString() : nsString() { - nsStrPrivate::Initialize(*this, (char*)mBuffer, (sizeof(mBuffer)/sizeof(mBuffer[0]))-1, 0, eTwoByte, PR_FALSE); - AddNullTerminator(*this); -} - -nsAutoString::nsAutoString(const PRUnichar* aString) : nsString() { - nsStrPrivate::Initialize(*this, (char*)mBuffer, (sizeof(mBuffer)/sizeof(mBuffer[0]))-1, 0, eTwoByte, PR_FALSE); - AddNullTerminator(*this); - Append(aString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - * @param aLength tells us how many chars to copy from aString - */ -nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString() { - nsStrPrivate::Initialize(*this, (char*)mBuffer, (sizeof(mBuffer)/sizeof(mBuffer[0]))-1, 0, eTwoByte, PR_FALSE); - AddNullTerminator(*this); - Append(aString,aLength); -} - -nsAutoString::nsAutoString( const nsString& aString ) - : nsString() -{ - nsStrPrivate::Initialize(*this, (char*)mBuffer, (sizeof(mBuffer)/sizeof(mBuffer[0]))-1, 0, eTwoByte, PR_FALSE); - AddNullTerminator(*this); - Append(aString); -} - -nsAutoString::nsAutoString( const nsAString& aString ) - : nsString() -{ - nsStrPrivate::Initialize(*this, (char*)mBuffer, (sizeof(mBuffer)/sizeof(mBuffer[0]))-1, 0, eTwoByte, PR_FALSE); - AddNullTerminator(*this); - Append(aString); -} - - - -/** - * constructor that uses external buffer - * @param aBuffer describes the external buffer - */ -nsAutoString::nsAutoString(const CBufDescriptor& aBuffer) : nsString() { - if(!aBuffer.mBuffer) { - nsStrPrivate::Initialize(*this, (char*)mBuffer, (sizeof(mBuffer)/sizeof(mBuffer[0]))-1, 0, eTwoByte, PR_FALSE); - } - else { - nsStrPrivate::Initialize(*this, aBuffer.mBuffer, aBuffer.mCapacity, aBuffer.mLength, aBuffer.mCharSize, !aBuffer.mStackBased); - } - if(!aBuffer.mIsConst) - AddNullTerminator(*this); -} - -PRUnichar* nsVoidableString::GetWritableFragment(nsWritableFragment& aFragment,nsFragmentRequest aRequest,PRUint32 aOffset) { - mIsVoid = PR_FALSE; - - return nsAutoString::GetWritableFragment(aFragment, aRequest, aOffset); -} - -PRBool nsVoidableString::IsVoid() const { - return mIsVoid; -} - -void nsVoidableString::SetIsVoid(PRBool aVoid) { - if (aVoid && !mIsVoid) { - Truncate(); - } - - mIsVoid = aVoid; -} - -void -NS_ConvertASCIItoUTF16::Init( const char* aCString, PRUint32 aLength ) - { - AppendWithConversion(aCString,aLength); - } - -NS_ConvertASCIItoUTF16::NS_ConvertASCIItoUTF16( const nsACString& aCString ) - { - SetCapacity(aCString.Length()); - - nsACString::const_iterator start; aCString.BeginReading(start); - nsACString::const_iterator end; aCString.EndReading(end); - - while (start != end) - { - const nsReadableFragment& frag = start.fragment(); - AppendWithConversion(frag.mStart, frag.mEnd - frag.mStart); - start.advance(start.size_forward()); - } - } - -NS_ConvertUTF8toUTF16::NS_ConvertUTF8toUTF16( const nsACString& aCString ) - { - // Compute space required: do this once so we don't incur multiple - // allocations. This "optimization" is probably of dubious value... - - nsACString::const_iterator start, end; - CalculateUTF8Length calculator; - copy_string(aCString.BeginReading(start), aCString.EndReading(end), - calculator); - - PRUint32 count = calculator.Length(); - - if (count) - { - // Grow the buffer if we need to. - SetCapacity(count); - - // All ready? Time to convert - - ConvertUTF8toUTF16 converter(mUStr); - copy_string(aCString.BeginReading(start), aCString.EndReading(end), - converter).write_terminator(); - mLength = converter.Length(); - if (mLength != count) - { - NS_ERROR("Input wasn't UTF-8 or incorrect length was calculated"); - Truncate(); - } - } - } - -NS_ConvertUTF8toUTF16::NS_ConvertUTF8toUTF16( const nsASingleFragmentCString& aCString ) - { - nsASingleFragmentCString::const_char_iterator start; - Init(aCString.BeginReading(start), aCString.Length()); - } - -NS_ConvertUTF8toUTF16::NS_ConvertUTF8toUTF16( const char* aCString ) - { - if (!aCString) - // Leave us as an uninitialized nsAutoString. - return; - Init(aCString, nsCharTraits::length(aCString)); - } - -NS_ConvertUTF8toUTF16::NS_ConvertUTF8toUTF16( const char* aCString, PRUint32 aLength ) - { - if (!aCString) - // Leave us as an uninitialized nsAutoString. - return; - Init(aCString, aLength); - } - -void -NS_ConvertUTF8toUTF16::Init( const char* aCString, PRUint32 aLength ) - { - // Compute space required: do this once so we don't incur multiple - // allocations. This "optimization" is probably of dubious value... - - CalculateUTF8Length calculator; - calculator.write(aCString, aLength); - - PRUint32 count = calculator.Length(); - - if (count) - { - // Grow the buffer if we need to. - SetCapacity(count); - - // All ready? Time to convert - - ConvertUTF8toUTF16 converter(mUStr); - converter.write(aCString, aLength); - mLength = converter.Length(); - mUStr[mLength] = char_type(0); - if (mLength != count) - { - NS_ERROR("Input invalid or incorrect length was calculated"); - Truncate(); - } - } - } - -/** - * Default copy constructor - */ -nsAutoString::nsAutoString(const nsAutoString& aString) : nsString() { - nsStrPrivate::Initialize(*this,(char*)mBuffer,(sizeof(mBuffer)/sizeof(mBuffer[0]))-1,0,eTwoByte,PR_FALSE); - AddNullTerminator(*this); - Append(aString); -} - - -/** - * Copy construct from a unichar - * @param - */ -nsAutoString::nsAutoString(PRUnichar aChar) : nsString(){ - nsStrPrivate::Initialize(*this,(char*)mBuffer,(sizeof(mBuffer)/sizeof(mBuffer[0]))-1,0,eTwoByte,PR_FALSE); - AddNullTerminator(*this); - Append(aChar); -} diff --git a/mozilla/xpcom/string/public/Makefile.in b/mozilla/xpcom/string/public/Makefile.in index 5a47663b6ae..34961c76260 100644 --- a/mozilla/xpcom/string/public/Makefile.in +++ b/mozilla/xpcom/string/public/Makefile.in @@ -62,6 +62,12 @@ EXPORTS = \ nsTString.h \ nsTStringBase.h \ nsTStringTuple.h \ + nsObsoleteAString.h \ + nsStringBase.h \ + nsStringTuple.h \ + string-template-def-unichar.h \ + string-template-def-char.h \ + string-template-undef.h \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/mozilla/xpcom/string/public/nsAFlatString.h b/mozilla/xpcom/string/public/nsAFlatString.h index 1b0fc883d6e..7c4644edc6c 100644 --- a/mozilla/xpcom/string/public/nsAFlatString.h +++ b/mozilla/xpcom/string/public/nsAFlatString.h @@ -26,8 +26,8 @@ #ifndef nsAFlatString_h___ #define nsAFlatString_h___ -#ifndef nsTString_h___ -#include "nsTString.h" +#ifndef nsString_h___ +#include "nsString.h" #endif #endif /* !defined(nsAFlatString_h___) */ diff --git a/mozilla/xpcom/string/public/nsASingleFragmentString.h b/mozilla/xpcom/string/public/nsASingleFragmentString.h index 19962335d6d..cf73261168d 100644 --- a/mozilla/xpcom/string/public/nsASingleFragmentString.h +++ b/mozilla/xpcom/string/public/nsASingleFragmentString.h @@ -26,8 +26,8 @@ #ifndef nsASingleFragmentString_h___ #define nsASingleFragmentString_h___ -#ifndef nsTStringBase_h___ -#include "nsTStringBase.h" +#ifndef nsStringBase_h___ +#include "nsStringBase.h" #endif #endif /* !defined(nsASingleFragmentString_h___) */ diff --git a/mozilla/xpcom/string/public/nsAString.h b/mozilla/xpcom/string/public/nsAString.h index c5a196d66b6..6251dc1f218 100644 --- a/mozilla/xpcom/string/public/nsAString.h +++ b/mozilla/xpcom/string/public/nsAString.h @@ -24,8 +24,49 @@ #ifndef nsAString_h___ #define nsAString_h___ -#ifndef nsTAString_h___ +#ifndef nsStringFwd_h___ +#include "nsStringFwd.h" +#endif + +#ifndef nsStringIterator_h___ +#include "nsStringIterator.h" +#endif + +#ifndef nsObsoleteAString_h___ +#include "nsObsoleteAString.h" +#endif + + + // declare nsAString +#include "string-template-def-unichar.h" #include "nsTAString.h" +#include "string-template-undef.h" + + + // declare nsACString +#include "string-template-def-char.h" +#include "nsTAString.h" +#include "string-template-undef.h" + + + /** + * ASCII case-insensitive comparator. (for Unicode case-insensitive + * comparision, see nsUnicharUtils.h) + */ +class NS_COM nsCaseInsensitiveCStringComparator + : public nsCStringComparator + { + public: + typedef char char_type; + + virtual int operator()( const char_type*, const char_type*, PRUint32 length ) const; + virtual int operator()( char_type, char_type ) const; + }; + + + // included here for backwards compatibility +#ifndef nsStringTuple_h___ +#include "nsStringTuple.h" #endif #endif // !defined(nsAString_h___) diff --git a/mozilla/xpcom/string/public/nsDependentConcatenation.h b/mozilla/xpcom/string/public/nsDependentConcatenation.h index 44603b5392a..92795340221 100644 --- a/mozilla/xpcom/string/public/nsDependentConcatenation.h +++ b/mozilla/xpcom/string/public/nsDependentConcatenation.h @@ -27,8 +27,8 @@ #ifndef nsDependentConcatenation_h___ #define nsDependentConcatenation_h___ -#ifndef nsTStringTuple_h___ -#include "nsTStringTuple.h" +#ifndef nsStringTuple_h___ +#include "nsStringTuple.h" #endif #endif /* !defined(nsDependentConcatenation_h___) */ diff --git a/mozilla/xpcom/string/public/nsDependentString.h b/mozilla/xpcom/string/public/nsDependentString.h index 279d877eef4..9750732c374 100644 --- a/mozilla/xpcom/string/public/nsDependentString.h +++ b/mozilla/xpcom/string/public/nsDependentString.h @@ -24,8 +24,22 @@ #ifndef nsDependentString_h___ #define nsDependentString_h___ -#ifndef nsTDependentString_h___ -#include "nsTDependentString.h" +#ifndef nsString_h___ +#include "nsString.h" #endif +#ifndef nsDebug_h___ +#include "nsDebug.h" +#endif + + // declare nsDependentString +#include "string-template-def-unichar.h" +#include "nsTDependentString.h" +#include "string-template-undef.h" + + // declare nsDependentCString +#include "string-template-def-char.h" +#include "nsTDependentString.h" +#include "string-template-undef.h" + #endif /* !defined(nsDependentString_h___) */ diff --git a/mozilla/xpcom/string/public/nsDependentSubstring.h b/mozilla/xpcom/string/public/nsDependentSubstring.h index 3b367944010..abe10bf33a1 100644 --- a/mozilla/xpcom/string/public/nsDependentSubstring.h +++ b/mozilla/xpcom/string/public/nsDependentSubstring.h @@ -24,8 +24,18 @@ #ifndef nsDependentSubstring_h___ #define nsDependentSubstring_h___ -#ifndef nsTDependentSubstring_h___ -#include "nsTDependentSubstring.h" +#ifndef nsStringBase_h___ +#include "nsStringBase.h" #endif + // declare nsDependentSubstring +#include "string-template-def-unichar.h" +#include "nsTDependentSubstring.h" +#include "string-template-undef.h" + + // declare nsDependentCSubstring +#include "string-template-def-char.h" +#include "nsTDependentSubstring.h" +#include "string-template-undef.h" + #endif /* !defined(nsDependentSubstring_h___) */ diff --git a/mozilla/xpcom/string/public/nsObsoleteAString.h b/mozilla/xpcom/string/public/nsObsoleteAString.h new file mode 100644 index 00000000000..de842ff85d9 --- /dev/null +++ b/mozilla/xpcom/string/public/nsObsoleteAString.h @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsObsoleteAString_h___ +#define nsObsoleteAString_h___ + +/**************************************************************************** + + THIS FILE IS NOT FOR HUMAN CONSUMPTION. See nsAString instead. + + ****************************************************************************/ + +#ifndef nsStringFwd_h___ +#include "nsStringFwd.h" +#endif + +#ifndef nscore_h___ +#include "nscore.h" +#endif + + // declare nsObsoleteAString +#include "string-template-def-unichar.h" +#include "nsTObsoleteAString.h" +#include "string-template-undef.h" + + // declare nsObsoleteACString +#include "string-template-def-char.h" +#include "nsTObsoleteAString.h" +#include "string-template-undef.h" + +#endif // !defined(nsObsoleteAString_h___) diff --git a/mozilla/xpcom/string/public/nsPrintfCString.h b/mozilla/xpcom/string/public/nsPrintfCString.h index 058c32e35c4..4bba696f157 100755 --- a/mozilla/xpcom/string/public/nsPrintfCString.h +++ b/mozilla/xpcom/string/public/nsPrintfCString.h @@ -38,8 +38,8 @@ #ifndef nsPrintfCString_h___ #define nsPrintfCString_h___ -#ifndef nsTString_h___ -#include "nsTString.h" +#ifndef nsString_h___ +#include "nsString.h" #endif @@ -67,9 +67,9 @@ * much more efficiently handled with |NS_LITERAL_[C]STRING| and |nsLiteral[C]String|. */ -class NS_COM nsPrintfCString : public nsTString +class NS_COM nsPrintfCString : public nsCString { - typedef nsTString string_type; + typedef nsCString string_type; enum { kLocalBufferSize=15 }; // ought to be large enough for most things ... a |long long| needs at most 20 (so you'd better ask) diff --git a/mozilla/xpcom/string/public/nsPromiseFlatString.h b/mozilla/xpcom/string/public/nsPromiseFlatString.h index dbba1814856..8106d244055 100644 --- a/mozilla/xpcom/string/public/nsPromiseFlatString.h +++ b/mozilla/xpcom/string/public/nsPromiseFlatString.h @@ -24,8 +24,66 @@ #ifndef nsPromiseFlatString_h___ #define nsPromiseFlatString_h___ -#ifndef nsTPromiseFlatString_h___ -#include "nsTPromiseFlatString.h" +#ifndef nsString_h___ +#include "nsString.h" #endif + // declare nsPromiseFlatString +#include "string-template-def-unichar.h" +#include "nsTPromiseFlatString.h" +#include "string-template-undef.h" + + // declare nsPromiseFlatCString +#include "string-template-def-char.h" +#include "nsTPromiseFlatString.h" +#include "string-template-undef.h" + + +inline +const nsPromiseFlatString +PromiseFlatString( const nsAString& str ) + { + return nsPromiseFlatString(str); + } + + // e.g., PromiseFlatString(Substring(s)) +inline +const nsPromiseFlatString +PromiseFlatString( const nsStringBase& frag ) + { + return nsPromiseFlatString(frag); + } + + // e.g., PromiseFlatString(a + b) +inline +const nsPromiseFlatString +PromiseFlatString( const nsStringTuple& tuple ) + { + return nsPromiseFlatString(tuple); + } + + +inline +const nsPromiseFlatCString +PromiseFlatCString( const nsACString& str ) + { + return nsPromiseFlatCString(str); + } + + // e.g., PromiseFlatCString(Substring(s)) +inline +const nsPromiseFlatCString +PromiseFlatCString( const nsCStringBase& frag ) + { + return nsPromiseFlatCString(frag); + } + + // e.g., PromiseFlatCString(a + b) +inline +const nsPromiseFlatCString +PromiseFlatCString( const nsCStringTuple& tuple ) + { + return nsPromiseFlatCString(tuple); + } + #endif /* !defined(nsPromiseFlatString_h___) */ diff --git a/mozilla/xpcom/string/public/nsSharableString.h b/mozilla/xpcom/string/public/nsSharableString.h index 6dbf7a778ec..d9775112200 100644 --- a/mozilla/xpcom/string/public/nsSharableString.h +++ b/mozilla/xpcom/string/public/nsSharableString.h @@ -27,8 +27,8 @@ #ifndef nsSharableString_h___ #define nsSharableString_h___ -#ifndef nsTString_h___ -#include "nsTString.h" +#ifndef nsString_h___ +#include "nsString.h" #endif // need to include this for compatibility diff --git a/mozilla/xpcom/string/public/nsString.h b/mozilla/xpcom/string/public/nsString.h index 54958ca1387..6cd1e2643ed 100644 --- a/mozilla/xpcom/string/public/nsString.h +++ b/mozilla/xpcom/string/public/nsString.h @@ -39,28 +39,243 @@ #ifndef nsString_h___ #define nsString_h___ -#ifndef nsTString_h___ -#include "nsTString.h" + +#ifndef nsStringBase_h___ +#include "nsStringBase.h" #endif -#ifndef nsTDependentString_h___ -#include "nsTDependentString.h" +#ifndef nsDependentSubstring_h___ +#include "nsDependentSubstring.h" +#endif + +#ifndef nsReadableUtils_h___ +#include "nsReadableUtils.h" +#endif + + + // enable support for the obsolete string API if not explicitly disabled +#ifndef MOZ_STRING_WITH_OBSOLETE_API +#define MOZ_STRING_WITH_OBSOLETE_API 1 +#endif + +#if MOZ_STRING_WITH_OBSOLETE_API + // radix values for ToInteger/AppendInt +#define kRadix10 (10) +#define kRadix16 (16) +#define kAutoDetect (100) +#define kRadixUnknown (kAutoDetect+1) +#endif + +class CBufDescriptor; + + + // declare nsString, et. al. +#include "string-template-def-unichar.h" +#include "nsTString.h" +#include "string-template-undef.h" + + // declare nsCString, et. al. +#include "string-template-def-char.h" +#include "nsTString.h" +#include "string-template-undef.h" + + + /** + * CBufDescriptor + * + * Allows a nsC?AutoString to be configured to use a custom buffer. + */ +class CBufDescriptor + { + public: + + CBufDescriptor(char* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) + { + mStr = aString; + mCapacity = aCapacity; + mLength = aLength; + mFlags = F_SINGLE_BYTE | (aStackBased ? F_STACK_BASED : 0); + } + + CBufDescriptor(const char* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) + { + mStr = NS_CONST_CAST(char*, aString); + mCapacity = aCapacity; + mLength = aLength; + mFlags = F_SINGLE_BYTE | F_CONST | (aStackBased ? F_STACK_BASED : 0); + } + + CBufDescriptor(PRUnichar* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) + { + mUStr = aString; + mCapacity = aCapacity; + mLength = aLength; + mFlags = F_DOUBLE_BYTE | (aStackBased ? F_STACK_BASED : 0); + } + + CBufDescriptor(const PRUnichar* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) + { + mUStr = NS_CONST_CAST(PRUnichar*, aString); + mCapacity = aCapacity; + mLength = aLength; + mFlags = F_DOUBLE_BYTE | F_CONST | (aStackBased ? F_STACK_BASED : 0); + } + + union + { + char* mStr; + PRUnichar* mUStr; + }; + + PRUint32 mCapacity; + PRInt32 mLength; + + enum + { + F_CONST = (1 << 0), + F_STACK_BASED = (1 << 1), + F_SINGLE_BYTE = (1 << 2), + F_DOUBLE_BYTE = (1 << 3) + }; + + PRUint32 mFlags; + }; + + + /** + * A helper class that converts a UTF-16 string to ASCII in a lossy manner + */ +class NS_LossyConvertUTF16toASCII : public nsCAutoString + { + public: + explicit + NS_LossyConvertUTF16toASCII( const PRUnichar* aString ) + { + LossyAppendUTF16toASCII(aString, *this); + } + + NS_LossyConvertUTF16toASCII( const PRUnichar* aString, PRUint32 aLength ) + { + LossyCopyUTF16toASCII(nsDependentSubstring(aString, aString + aLength), *this); + } + + explicit + NS_LossyConvertUTF16toASCII( const nsAString& aString ) + { + LossyCopyUTF16toASCII(aString, *this); + } + + private: + // NOT TO BE IMPLEMENTED + NS_LossyConvertUTF16toASCII( char ); + }; + + +class NS_ConvertASCIItoUTF16 : public nsAutoString + { + public: + explicit + NS_ConvertASCIItoUTF16( const char* aCString ) + { + AppendASCIItoUTF16(aCString, *this); + } + + NS_ConvertASCIItoUTF16( const char* aCString, PRUint32 aLength ) + { + CopyASCIItoUTF16(nsDependentCSubstring(aCString, aCString + aLength), *this); + } + + explicit + NS_ConvertASCIItoUTF16( const nsACString& aCString ) + { + CopyASCIItoUTF16(aCString, *this); + } + + private: + // NOT TO BE IMPLEMENTED + NS_ConvertASCIItoUTF16( PRUnichar ); + }; + + + /** + * A helper class that converts a UTF-16 string to UTF-8 + */ +class NS_ConvertUTF16toUTF8 : public nsCAutoString + { + public: + explicit + NS_ConvertUTF16toUTF8( const PRUnichar* aString ) + { + CopyUTF16toUTF8(aString, *this); + } + + NS_ConvertUTF16toUTF8( const PRUnichar* aString, PRUint32 aLength ) + { + CopyUTF16toUTF8(nsDependentSubstring(aString, aString + aLength), *this); + } + + explicit + NS_ConvertUTF16toUTF8( const nsAString& aString ) + { + CopyUTF16toUTF8(aString, *this); + } + + private: + // NOT TO BE IMPLEMENTED + NS_ConvertUTF16toUTF8( char ); + }; + + +class NS_ConvertUTF8toUTF16 : public nsAutoString + { + public: + explicit + NS_ConvertUTF8toUTF16( const char* aCString ) + { + CopyUTF8toUTF16(aCString, *this); + } + + NS_ConvertUTF8toUTF16( const char* aCString, PRUint32 aLength ) + { + CopyUTF8toUTF16(nsDependentCSubstring(aCString, aCString + aLength), *this); + } + + explicit + NS_ConvertUTF8toUTF16( const nsACString& aCString ) + { + CopyUTF8toUTF16(aCString, *this); + } + + private: + NS_ConvertUTF8toUTF16( PRUnichar ); + }; + + +// the following are included/declared for backwards compatibility + +typedef NS_ConvertUTF16toUTF8 NS_ConvertUCS2toUTF8; +typedef NS_LossyConvertUTF16toASCII NS_LossyConvertUCS2toASCII; +typedef NS_ConvertASCIItoUTF16 NS_ConvertASCIItoUCS2; +typedef NS_ConvertUTF8toUTF16 NS_ConvertUTF8toUCS2; +typedef nsAutoString nsVoidableString; + +#ifndef nsDependentString_h___ +#include "nsDependentString.h" #endif #ifndef nsLiteralString_h___ #include "nsLiteralString.h" #endif -#ifndef nsTDependentSubstring_h___ -#include "nsTDependentSubstring.h" +#ifndef nsPromiseFlatString_h___ +#include "nsPromiseFlatString.h" #endif -#ifndef nsTPromiseFlatString_h___ -#include "nsTPromiseFlatString.h" -#endif - -// need to include this for compatibility +// need to include these for backwards compatibility #include "nsMemory.h" +#include +#include +#include "plhash.h" #include NEW_H inline PRInt32 MinInt(PRInt32 x, PRInt32 y) diff --git a/mozilla/xpcom/string/public/nsStringBase.h b/mozilla/xpcom/string/public/nsStringBase.h new file mode 100644 index 00000000000..513de14f16f --- /dev/null +++ b/mozilla/xpcom/string/public/nsStringBase.h @@ -0,0 +1,65 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsStringBase_h___ +#define nsStringBase_h___ + +#ifndef nsAString_h___ +#include "nsAString.h" +#endif + +static const PRInt32 kNotFound = -1; + + + // declare nsStringBase +#include "string-template-def-unichar.h" +#include "nsTStringBase.h" +#include "string-template-undef.h" + + + // declare nsCStringBase +#include "string-template-def-char.h" +#include "nsTStringBase.h" +#include "string-template-undef.h" + + +#ifndef nsStringTuple_h___ +#include "nsStringTuple.h" +#endif + +#endif // !defined(nsStringBase_h___) diff --git a/mozilla/xpcom/string/public/nsStringFwd.h b/mozilla/xpcom/string/public/nsStringFwd.h index c17748c7878..2566ab4f363 100644 --- a/mozilla/xpcom/string/public/nsStringFwd.h +++ b/mozilla/xpcom/string/public/nsStringFwd.h @@ -36,55 +36,56 @@ /** - * template types: + * double-byte (PRUnichar) string types */ -template class nsTAString; -template class nsTObsoleteAString; -template class nsTStringBase; -template class nsTStringTuple; -template class nsTString; -template class nsTAutoString; -template class nsTDependentString; -template class nsTDependentSubstring; -template class nsTPromiseFlatString; -template class nsTStringComparator; -template class nsTDefaultStringComparator; -template class nsTXPIDLString; +class nsAString; +class nsObsoleteAString; +class nsStringBase; +class nsStringTuple; +class nsString; +class nsAutoString; +class nsDependentString; +class nsDependentSubstring; +class nsPromiseFlatString; +class nsStringComparator; +class nsDefaultStringComparator; +class nsXPIDLString; + + + /** + * single-byte (char) string types + */ + +class nsACString; +class nsObsoleteACString; +class nsCStringBase; +class nsCStringTuple; +class nsCString; +class nsCAutoString; +class nsDependentCString; +class nsDependentCSubstring; +class nsPromiseFlatCString; +class nsCStringComparator; +class nsDefaultCStringComparator; +class nsXPIDLCString; + + + /** + * typedefs for backwards compatibility + */ + +typedef nsString nsSharableString; +typedef nsString nsAFlatString; +typedef nsStringBase nsASingleFragmentString; +typedef nsStringTuple nsDependentConcatenation; +typedef nsDependentSubstring nsDependentSingleFragmentSubstring; + +typedef nsCString nsSharableCString; +typedef nsCString nsAFlatCString; +typedef nsCStringBase nsASingleFragmentCString; +typedef nsCStringTuple nsDependentCConcatenation; +typedef nsDependentCSubstring nsDependentSingleFragmentCSubstring; - /** - * for everything else, we typedef :-) - */ - -typedef nsTAString nsACString; -typedef nsTStringBase nsASingleFragmentCString; -typedef nsTString nsAFlatCString; -typedef nsTStringTuple nsDependentCConcatenation; -typedef nsTDependentSubstring nsDependentSingleFragmentCSubstring; -typedef nsTDependentSubstring nsDependentCSubstring; -typedef nsTDependentString nsDependentCString; -typedef nsTPromiseFlatString nsPromiseFlatCString; -typedef nsTString nsCString; -typedef nsTAutoString nsCAutoString; -typedef nsTXPIDLString nsXPIDLCString; -typedef nsTString nsSharableCString; -typedef nsTStringComparator nsCStringComparator; -typedef nsTDefaultStringComparator nsDefaultCStringComparator; - -typedef nsTAString nsAString; -typedef nsTStringBase nsASingleFragmentString; -typedef nsTString nsAFlatString; -typedef nsTStringTuple nsDependentConcatenation; -typedef nsTDependentSubstring nsDependentSingleFragmentSubstring; -typedef nsTDependentSubstring nsDependentSubstring; -typedef nsTDependentString nsDependentString; -typedef nsTPromiseFlatString nsPromiseFlatString; -typedef nsTString nsString; -typedef nsTAutoString nsAutoString; -typedef nsTXPIDLString nsXPIDLString; -typedef nsTString nsSharableString; -typedef nsTStringComparator nsStringComparator; -typedef nsTDefaultStringComparator nsDefaultStringComparator; - #endif /* !defined(nsStringFwd_h___) */ diff --git a/mozilla/xpcom/string/public/nsStringIterator.h b/mozilla/xpcom/string/public/nsStringIterator.h index 5d3e8cea36b..7fd6c0df558 100644 --- a/mozilla/xpcom/string/public/nsStringIterator.h +++ b/mozilla/xpcom/string/public/nsStringIterator.h @@ -51,8 +51,10 @@ class nsReadingIterator typedef const CharT& reference; private: - friend class nsTAString; - friend class nsTStringBase; + friend class nsAString; + friend class nsACString; + friend class nsStringBase; + friend class nsCStringBase; // unfortunately, the API for nsReadingIterator requires that the // iterator know its start and end positions. this was needed when @@ -187,8 +189,10 @@ class nsWritingIterator typedef CharT& reference; private: - friend class nsTAString; - friend class nsTStringBase; + friend class nsAString; + friend class nsACString; + friend class nsStringBase; + friend class nsCStringBase; // unfortunately, the API for nsWritingIterator requires that the // iterator know its start and end positions. this was needed when diff --git a/mozilla/xpcom/string/public/nsStringTuple.h b/mozilla/xpcom/string/public/nsStringTuple.h new file mode 100644 index 00000000000..94113ee890c --- /dev/null +++ b/mozilla/xpcom/string/public/nsStringTuple.h @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsStringTuple_h___ +#define nsStringTuple_h___ + +#ifndef nsStringBase_h___ +#include "nsStringBase.h" +#endif + + // declare nsStringTuple +#include "string-template-def-unichar.h" +#include "nsTStringTuple.h" +#include "string-template-undef.h" + + // declare nsCStringTuple +#include "string-template-def-char.h" +#include "nsTStringTuple.h" +#include "string-template-undef.h" + +#endif // !defined(nsStringTuple_h___) diff --git a/mozilla/xpcom/string/public/nsTAString.h b/mozilla/xpcom/string/public/nsTAString.h index 0bcb07a1b11..a879885a082 100644 --- a/mozilla/xpcom/string/public/nsTAString.h +++ b/mozilla/xpcom/string/public/nsTAString.h @@ -36,55 +36,32 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTAString_h___ -#define nsTAString_h___ - -#ifndef nsStringFwd_h___ -#include "nsStringFwd.h" -#endif - -#ifndef nsStringIterator_h___ -#include "nsStringIterator.h" -#endif - -#ifndef nsTObsoleteAString_h___ -#include "nsTObsoleteAString.h" -#endif - /** * The base for string comparators */ -template -class NS_COM nsTStringComparator +class NS_COM nsTStringComparator_CharT { public: typedef CharT char_type; - nsTStringComparator() {} + nsTStringComparator_CharT() {} virtual int operator()( const char_type*, const char_type*, PRUint32 length ) const = 0; virtual int operator()( char_type, char_type ) const = 0; }; -template -class NS_COM nsTDefaultStringComparator - : public nsTStringComparator + + /** + * The default string comparator (case-sensitive comparision) + */ +class NS_COM nsTDefaultStringComparator_CharT + : public nsTStringComparator_CharT { public: typedef CharT char_type; - nsTDefaultStringComparator() {} - - virtual int operator()( const char_type*, const char_type*, PRUint32 length ) const; - virtual int operator()( char_type, char_type ) const; - }; - -class NS_COM nsCaseInsensitiveCStringComparator - : public nsCStringComparator - { - public: - typedef char char_type; + nsTDefaultStringComparator_CharT() {} virtual int operator()( const char_type*, const char_type*, PRUint32 length ) const; virtual int operator()( char_type, char_type ) const; @@ -103,26 +80,25 @@ class NS_COM nsCaseInsensitiveCStringComparator * other main abstract classes in the string hierarchy. */ -template -class NS_COM nsTAString +class NS_COM nsTAString_CharT { public: typedef CharT char_type; typedef nsCharTraits char_traits; - typedef typename char_traits::incompatible_char_type incompatible_char_type; + typedef char_traits::incompatible_char_type incompatible_char_type; - typedef nsTAString self_type; - typedef nsTAString abstract_string_type; - typedef nsTObsoleteAString obsolete_string_type; - typedef nsTStringBase string_base_type; - typedef nsTStringTuple string_tuple_type; + typedef nsTAString_CharT self_type; + typedef nsTAString_CharT abstract_string_type; + typedef nsTObsoleteAString_CharT obsolete_string_type; + typedef nsTStringBase_CharT string_base_type; + typedef nsTStringTuple_CharT string_tuple_type; typedef nsReadingIterator const_iterator; typedef nsWritingIterator iterator; - typedef nsTStringComparator comparator_type; + typedef nsTStringComparator_CharT comparator_type; typedef PRUint32 size_type; typedef PRUint32 index_type; @@ -130,7 +106,7 @@ class NS_COM nsTAString public: // this acts like a virtual destructor - ~nsTAString(); + ~nsTAString_CharT(); inline const_iterator& BeginReading( const_iterator& iter ) const { @@ -178,7 +154,7 @@ class NS_COM nsTAString PRBool IsTerminated() const; /** - * these are contant time since nsTAString uses flat storage + * these are contant time since nsTAString_CharT uses flat storage */ char_type First() const; char_type Last() const; @@ -288,12 +264,13 @@ class NS_COM nsTAString protected: - friend class nsTStringTuple; + friend class nsTStringTuple_CharT; + // XXX still needed now that these aren't template types?? // GCC 3.2 erroneously needs these (they are subclasses!) - friend class nsTStringBase; - friend class nsTDependentSubstring; - friend class nsTPromiseFlatString; + friend class nsTStringBase_CharT; + friend class nsTDependentSubstring_CharT; + friend class nsTPromiseFlatString_CharT; /** * the address of our virtual function table. required for backwards @@ -310,10 +287,10 @@ class NS_COM nsTAString PRUint32 mFlags; /** - * nsTAString must be subclassed before it can be instantiated. + * nsTAString_CharT must be subclassed before it can be instantiated. */ - nsTAString(char_type* data, size_type length, PRUint32 flags) - : mVTable(nsTObsoleteAString::sCanonicalVTable) + nsTAString_CharT(char_type* data, size_type length, PRUint32 flags) + : mVTable(obsolete_string_type::sCanonicalVTable) , mData(data) , mLength(length) , mFlags(flags) @@ -324,8 +301,9 @@ class NS_COM nsTAString * * mData and mLength are intentionally left uninitialized. */ - nsTAString(PRUint32 flags) - : mVTable(nsTObsoleteAString::sCanonicalVTable) + explicit + nsTAString_CharT(PRUint32 flags) + : mVTable(obsolete_string_type::sCanonicalVTable) , mFlags(flags) {} @@ -335,8 +313,8 @@ class NS_COM nsTAString * this is public to support automatic conversion of tuple to abstract * string, which is necessary to support our API. */ - nsTAString(const string_tuple_type& tuple) - : mVTable(nsTObsoleteAString::sCanonicalVTable) + nsTAString_CharT(const string_tuple_type& tuple) + : mVTable(obsolete_string_type::sCanonicalVTable) , mData(nsnull) , mLength(0) , mFlags(0) @@ -346,12 +324,6 @@ class NS_COM nsTAString protected: - //nsTAString( const self_type& readable ); - /* - : mVTable(readable.mVTable) - {} - */ - /** * get pointer to internal string buffer (may not be null terminated). * return length of buffer. @@ -368,12 +340,7 @@ class NS_COM nsTAString /** * we can be converted to a const nsTStringBase (dependent on this) */ - const string_base_type ToString() const - { - const char_type* data; - size_type length = GetReadableBuffer(&data); - return string_base_type(NS_CONST_CAST(char_type*, data), length, 0); - } + inline const string_base_type ToString() const; private: @@ -403,57 +370,42 @@ class NS_COM nsTAString }; -template NS_COM -int Compare( const nsTAString& lhs, const nsTAString& rhs, const nsTStringComparator& = nsTDefaultStringComparator() ); +int Compare( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs, const nsTStringComparator_CharT& = nsTDefaultStringComparator_CharT() ); -template inline -PRBool operator!=( const nsTAString& lhs, const nsTAString& rhs ) +PRBool operator!=( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs ) { return !lhs.Equals(rhs); } -template inline -PRBool operator< ( const nsTAString& lhs, const nsTAString& rhs ) +PRBool operator< ( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs ) { return Compare(lhs, rhs)< 0; } -template inline -PRBool operator<=( const nsTAString& lhs, const nsTAString& rhs ) +PRBool operator<=( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs ) { return Compare(lhs, rhs)<=0; } -template inline -PRBool operator==( const nsTAString& lhs, const nsTAString& rhs ) +PRBool operator==( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs ) { return lhs.Equals(rhs); } -template inline -PRBool operator>=( const nsTAString& lhs, const nsTAString& rhs ) +PRBool operator>=( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs ) { return Compare(lhs, rhs)>=0; } -template inline -PRBool operator> ( const nsTAString& lhs, const nsTAString& rhs ) +PRBool operator> ( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs ) { return Compare(lhs, rhs)> 0; } - - -#ifndef nsTStringTuple_h___ -#include "nsTStringTuple.h" -#endif - - -#endif // !defined(nsTAString_h___) diff --git a/mozilla/xpcom/string/public/nsTDependentString.h b/mozilla/xpcom/string/public/nsTDependentString.h index 8e3915bb2bb..9fee7153d98 100644 --- a/mozilla/xpcom/string/public/nsTDependentString.h +++ b/mozilla/xpcom/string/public/nsTDependentString.h @@ -36,20 +36,9 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTDependentString_h___ -#define nsTDependentString_h___ - -#ifndef nsTString_h___ -#include "nsTString.h" -#endif - -#ifndef nsDebug_h___ -#include "nsDebug.h" -#endif - /** - * nsTDependentString + * nsTDependentString_CharT * * Stores a null-terminated, immutable sequence of characters. * @@ -59,21 +48,20 @@ * nsTDependentString continues to reference valid memory for the * duration of its use. */ -template -class nsTDependentString : public nsTString +class nsTDependentString_CharT : public nsTString_CharT { public: - typedef CharT char_type; + typedef CharT char_type; - typedef nsTDependentString self_type; - typedef nsTString string_type; + typedef nsTDependentString_CharT self_type; + typedef nsTString_CharT string_type; - typedef typename string_type::char_traits char_traits; - typedef typename string_type::string_base_type string_base_type; - typedef typename string_type::string_tuple_type string_tuple_type; - typedef typename string_type::abstract_string_type abstract_string_type; - typedef typename string_type::size_type size_type; + typedef string_type::char_traits char_traits; + typedef string_type::string_base_type string_base_type; + typedef string_type::string_tuple_type string_tuple_type; + typedef string_type::abstract_string_type abstract_string_type; + typedef string_type::size_type size_type; public: @@ -92,27 +80,27 @@ class nsTDependentString : public nsTString * constructors */ - nsTDependentString( const char_type* start, const char_type* end ) + nsTDependentString_CharT( const char_type* start, const char_type* end ) : string_type(NS_CONST_CAST(char_type*, start), end - start, F_TERMINATED) { AssertValid(); } - nsTDependentString( const char_type* data, PRUint32 length ) + nsTDependentString_CharT( const char_type* data, PRUint32 length ) : string_type(NS_CONST_CAST(char_type*, data), length, F_TERMINATED) { AssertValid(); } explicit - nsTDependentString( const char_type* data ) + nsTDependentString_CharT( const char_type* data ) : string_type(NS_CONST_CAST(char_type*, data), char_traits::length(data), F_TERMINATED) { AssertValid(); } explicit - nsTDependentString( const string_base_type& str ) + nsTDependentString_CharT( const string_base_type& str ) : string_type(NS_CONST_CAST(char_type*, str.Data()), str.Length(), F_TERMINATED) { AssertValid(); @@ -150,8 +138,6 @@ class nsTDependentString : public nsTString private: // NOT USED - nsTDependentString( const string_tuple_type& ); - nsTDependentString( const abstract_string_type& ); + nsTDependentString_CharT( const string_tuple_type& ); + nsTDependentString_CharT( const abstract_string_type& ); }; - -#endif // !defined(nsTDependentString_h___) diff --git a/mozilla/xpcom/string/public/nsTDependentSubstring.h b/mozilla/xpcom/string/public/nsTDependentSubstring.h index ebc0a5c05dd..9d6bd402334 100644 --- a/mozilla/xpcom/string/public/nsTDependentSubstring.h +++ b/mozilla/xpcom/string/public/nsTDependentSubstring.h @@ -36,30 +36,22 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTDependentSubstring_h___ -#define nsTDependentSubstring_h___ - -#ifndef nsTStringBase_h___ -#include "nsTStringBase.h" -#endif - /** - * nsTDependentSubstring + * nsTDependentSubstring_CharT */ -template -class nsTDependentSubstring : public nsTStringBase +class nsTDependentSubstring_CharT : public nsTStringBase_CharT { public: - typedef CharT char_type; + typedef CharT char_type; - typedef nsTDependentSubstring self_type; - typedef nsTStringBase string_base_type; + typedef nsTDependentSubstring_CharT self_type; + typedef nsTStringBase_CharT string_base_type; - typedef typename string_base_type::abstract_string_type abstract_string_type; - typedef typename string_base_type::const_iterator const_iterator; - typedef typename string_base_type::size_type size_type; + typedef string_base_type::abstract_string_type abstract_string_type; + typedef string_base_type::const_iterator const_iterator; + typedef string_base_type::size_type size_type; public: @@ -69,26 +61,26 @@ class nsTDependentSubstring : public nsTStringBase void Rebind( const char_type* start, const char_type* end ) { NS_ASSERTION(start && end, "nsTDependentSubstring must wrap a non-NULL buffer"); - mData = start; + mData = NS_CONST_CAST(char_type*, start); mLength = end - start; } - nsTDependentSubstring( const abstract_string_type& str, PRUint32 startPos, PRUint32 length = size_type(-1) ) + nsTDependentSubstring_CharT( const abstract_string_type& str, PRUint32 startPos, PRUint32 length = size_type(-1) ) : string_base_type(F_NONE) { Rebind(str, startPos, length); } - nsTDependentSubstring( const string_base_type& str, PRUint32 startPos, PRUint32 length = size_type(-1) ) + nsTDependentSubstring_CharT( const string_base_type& str, PRUint32 startPos, PRUint32 length = size_type(-1) ) : string_base_type(F_NONE) { Rebind(str, startPos, length); } - nsTDependentSubstring( const char_type* start, const char_type* end ) + nsTDependentSubstring_CharT( const char_type* start, const char_type* end ) : string_base_type(NS_CONST_CAST(char_type*, start), end - start, F_NONE) {} - nsTDependentSubstring( const const_iterator& start, const const_iterator& end ) + nsTDependentSubstring_CharT( const const_iterator& start, const const_iterator& end ) : string_base_type(NS_CONST_CAST(char_type*, start.get()), end.get() - start.get(), F_NONE) {} // auto-generated copy-constructor OK (XXX really?? what about base class copy-ctor?) @@ -98,60 +90,58 @@ class nsTDependentSubstring : public nsTStringBase void operator=( const self_type& ) {} // we're immutable, you can't assign into a substring }; -template -const nsTDependentSubstring -Substring( const nsTAString& str, PRUint32 startPos, PRUint32 length = PRUint32(-1) ) +inline +const nsTDependentSubstring_CharT +Substring( const nsTAString_CharT& str, PRUint32 startPos, PRUint32 length = PRUint32(-1) ) { - return nsTDependentSubstring(str, startPos, length); + return nsTDependentSubstring_CharT(str, startPos, length); } -template -const nsTDependentSubstring -Substring( const nsTStringBase& str, PRUint32 startPos, PRUint32 length = PRUint32(-1) ) +inline +const nsTDependentSubstring_CharT +Substring( const nsTStringBase_CharT& str, PRUint32 startPos, PRUint32 length = PRUint32(-1) ) { - return nsTDependentSubstring(str, startPos, length); + return nsTDependentSubstring_CharT(str, startPos, length); } -template -const nsTDependentSubstring +inline +const nsTDependentSubstring_CharT Substring( const nsReadingIterator& start, const nsReadingIterator& end ) { - return nsTDependentSubstring(start.get(), end.get()); + return nsTDependentSubstring_CharT(start.get(), end.get()); } -template -const nsTDependentSubstring +inline +const nsTDependentSubstring_CharT Substring( const CharT* start, const CharT* end ) { - return nsTDependentSubstring(start, end); + return nsTDependentSubstring_CharT(start, end); } -template -const nsTDependentSubstring -StringHead( const nsTAString& str, PRUint32 count ) +inline +const nsTDependentSubstring_CharT +StringHead( const nsTAString_CharT& str, PRUint32 count ) { - return nsTDependentSubstring(str, 0, count); + return nsTDependentSubstring_CharT(str, 0, count); } -template -const nsTDependentSubstring -StringHead( const nsTStringBase& str, PRUint32 count ) +inline +const nsTDependentSubstring_CharT +StringHead( const nsTStringBase_CharT& str, PRUint32 count ) { - return nsTDependentSubstring(str, 0, count); + return nsTDependentSubstring_CharT(str, 0, count); } -template -const nsTDependentSubstring -StringTail( const nsTAString& str, PRUint32 count ) +inline +const nsTDependentSubstring_CharT +StringTail( const nsTAString_CharT& str, PRUint32 count ) { - return nsTDependentSubstring(str, str.Length() - count, count); + return nsTDependentSubstring_CharT(str, str.Length() - count, count); } -template -const nsTDependentSubstring -StringTail( const nsTStringBase& str, PRUint32 count ) +inline +const nsTDependentSubstring_CharT +StringTail( const nsTStringBase_CharT& str, PRUint32 count ) { - return nsTDependentSubstring(str, str.Length() - count, count); + return nsTDependentSubstring_CharT(str, str.Length() - count, count); } - -#endif // !defined(nsTDependentSubstring_h___) diff --git a/mozilla/xpcom/string/public/nsTObsoleteAString.h b/mozilla/xpcom/string/public/nsTObsoleteAString.h index 86523ea0efb..9c93129c411 100644 --- a/mozilla/xpcom/string/public/nsTObsoleteAString.h +++ b/mozilla/xpcom/string/public/nsTObsoleteAString.h @@ -36,87 +36,13 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTObsoleteAString_h___ -#define nsTObsoleteAString_h___ - -/**************************************************************************** - - THIS FILE IS NOT FOR HUMAN CONSUMPTION. See nsAString instead. - - ****************************************************************************/ - -#ifndef nsStringFwd_h___ -#include "nsStringFwd.h" -#endif - -#ifndef nscore_h___ -#include "nscore.h" -#endif - - /** - * An |nsFragmentRequest| is used to tell |GetReadableFragment| and - * |GetWritableFragment| what to do. - * - * @see GetReadableFragment - */ -enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt }; - - - /** - * A |nsReadableFragment| provides |const| access to a contiguous hunk of - * string of homogenous units, e.g., bytes (|char|). This doesn't mean it - * represents a flat hunk. It could be a variable length encoding, for - * instance UTF-8. And the fragment itself need not be zero-terminated. - * - * An |nsReadableFragment| is the underlying machinery that lets - * |nsReadingIterator|s work. - * - * @see nsReadingIterator - * @status FROZEN - */ -template -struct nsTObsoleteReadableFragment - { - const CharT* mStart; - const CharT* mEnd; - const void* mFragmentIdentifier; - - nsTObsoleteReadableFragment() : mStart(0), mEnd(0), mFragmentIdentifier(0) {} - }; - - - /** - * A |nsWritableFragment| provides non-|const| access to a contiguous hunk of - * string of homogenous units, e.g., bytes (|char|). This doesn't mean it - * represents a flat hunk. It could be a variable length encoding, for - * instance UTF-8. And the fragment itself need not be zero-terminated. - * - * An |nsWritableFragment| is the underlying machinery that lets - * |nsWritingIterator|s work. - * - * @see nsWritingIterator - * @status FROZEN - */ -template -struct nsTObsoleteWritableFragment - { - CharT* mStart; - CharT* mEnd; - void* mFragmentIdentifier; - - nsTObsoleteWritableFragment() : mStart(0), mEnd(0), mFragmentIdentifier(0) {} - }; - - - /** - * nsTObsoleteAString : binary compatible with old nsAString vtable + * nsTObsoleteAString_CharT : binary compatible with old nsAC?String vtable * * @status FROZEN */ -template -class NS_COM nsTObsoleteAString +class NS_COM nsTObsoleteAString_CharT { public: /** @@ -125,27 +51,78 @@ class NS_COM nsTObsoleteAString */ static const void *sCanonicalVTable; - protected: + /** + * An |nsFragmentRequest| is used to tell |GetReadableFragment| and + * |GetWritableFragment| what to do. + * + * @see GetReadableFragment + */ + enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt }; - typedef CharT char_type; + /** + * A |nsReadableFragment| provides |const| access to a contiguous hunk of + * string of homogenous units, e.g., bytes (|char|). This doesn't mean it + * represents a flat hunk. It could be a variable length encoding, for + * instance UTF-8. And the fragment itself need not be zero-terminated. + * + * An |nsReadableFragment| is the underlying machinery that lets + * |nsReadingIterator|s work. + * + * @see nsReadingIterator + * @status FROZEN + */ + struct nsReadableFragment + { + const CharT* mStart; + const CharT* mEnd; + const void* mFragmentIdentifier; - typedef void buffer_handle_type; - typedef void shared_buffer_handle_type; - typedef nsTObsoleteReadableFragment const_fragment_type; - typedef nsTObsoleteWritableFragment fragment_type; + nsReadableFragment() : mStart(0), mEnd(0), mFragmentIdentifier(0) {} + }; - typedef nsTObsoleteAString self_type; - typedef nsTObsoleteAString obsolete_string_type; - typedef PRUint32 size_type; - typedef PRUint32 index_type; + /** + * A |nsWritableFragment| provides non-|const| access to a contiguous hunk of + * string of homogenous units, e.g., bytes (|char|). This doesn't mean it + * represents a flat hunk. It could be a variable length encoding, for + * instance UTF-8. And the fragment itself need not be zero-terminated. + * + * An |nsWritableFragment| is the underlying machinery that lets + * |nsWritingIterator|s work. + * + * @see nsWritingIterator + * @status FROZEN + */ + struct nsWritableFragment + { + CharT* mStart; + CharT* mEnd; + void* mFragmentIdentifier; + + nsWritableFragment() : mStart(0), mEnd(0), mFragmentIdentifier(0) {} + }; protected: - friend class nsTAString; - friend class nsTStringBase; + typedef CharT char_type; - virtual ~nsTObsoleteAString() { } + typedef void buffer_handle_type; + typedef void shared_buffer_handle_type; + typedef nsReadableFragment const_fragment_type; + typedef nsWritableFragment fragment_type; + + typedef nsTObsoleteAString_CharT self_type; + typedef nsTObsoleteAString_CharT obsolete_string_type; + + typedef PRUint32 size_type; + typedef PRUint32 index_type; + + protected: + + friend class nsTAString_CharT; + friend class nsTStringBase_CharT; + + virtual ~nsTObsoleteAString_CharT() { } virtual PRUint32 GetImplementationFlags() const = 0; virtual const buffer_handle_type* GetFlatBufferHandle() const = 0; @@ -183,10 +160,5 @@ class NS_COM nsTObsoleteAString virtual char_type* GetWritableFragment( fragment_type&, nsFragmentRequest, PRUint32 = 0 ) = 0; }; -typedef nsTObsoleteAString nsObsoleteAString; -typedef nsTObsoleteAString nsObsoleteACString; - // forward declare implementation -template class nsTObsoleteAStringThunk; - -#endif // !defined(nsTObsoleteAString_h___) +class nsTObsoleteAStringThunk_CharT; diff --git a/mozilla/xpcom/string/public/nsTPromiseFlatString.h b/mozilla/xpcom/string/public/nsTPromiseFlatString.h index 05cb8efd656..e0e42b2670e 100644 --- a/mozilla/xpcom/string/public/nsTPromiseFlatString.h +++ b/mozilla/xpcom/string/public/nsTPromiseFlatString.h @@ -36,12 +36,6 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTPromiseFlatString_h___ -#define nsTPromiseFlatString_h___ - -#ifndef nsTString_h___ -#include "nsTString.h" -#endif /** * XXX fix docs XXX @@ -97,19 +91,18 @@ * the right thing happens. */ -template -class NS_COM nsTPromiseFlatString : public nsTString +class NS_COM nsTPromiseFlatString_CharT : public nsTString_CharT { public: - typedef CharT char_type; + typedef CharT char_type; - typedef nsTPromiseFlatString self_type; - typedef nsTString string_type; - typedef nsTStringBase string_base_type; + typedef nsTPromiseFlatString_CharT self_type; + typedef nsTString_CharT string_type; + typedef nsTStringBase_CharT string_base_type; - typedef typename string_base_type::string_tuple_type string_tuple_type; - typedef typename string_base_type::abstract_string_type abstract_string_type; + typedef string_base_type::string_tuple_type string_tuple_type; + typedef string_base_type::abstract_string_type abstract_string_type; private: @@ -122,21 +115,21 @@ class NS_COM nsTPromiseFlatString : public nsTString public: explicit - nsTPromiseFlatString( const string_base_type& str ) + nsTPromiseFlatString_CharT( const string_base_type& str ) : string_type() { Init(str); } explicit - nsTPromiseFlatString( const abstract_string_type& readable ) + nsTPromiseFlatString_CharT( const abstract_string_type& readable ) : string_type() { Init(readable); } explicit - nsTPromiseFlatString( const string_tuple_type& tuple ) + nsTPromiseFlatString_CharT( const string_tuple_type& tuple ) : string_type() { // nothing else to do here except assign the value of the tuple @@ -144,54 +137,3 @@ class NS_COM nsTPromiseFlatString : public nsTString Assign(tuple); } }; - - -inline -const nsTPromiseFlatString -PromiseFlatString( const nsTAString& str ) - { - return nsTPromiseFlatString(str); - } - - // e.g., PromiseFlatString(Substring(s)) -inline -const nsTPromiseFlatString -PromiseFlatString( const nsTStringBase& frag ) - { - return nsTPromiseFlatString(frag); - } - - // e.g., PromiseFlatString(a + b) -inline -const nsTPromiseFlatString -PromiseFlatString( const nsTStringTuple& tuple ) - { - return nsTPromiseFlatString(tuple); - } - - -inline -const nsTPromiseFlatString -PromiseFlatCString( const nsTAString& str ) - { - return nsTPromiseFlatString(str); - } - - // e.g., PromiseFlatString(Substring(s)) -inline -const nsTPromiseFlatString -PromiseFlatCString( const nsTStringBase& str ) - { - return nsTPromiseFlatString(str); - } - - // e.g., PromiseFlatString(a + b) -inline -const nsTPromiseFlatString -PromiseFlatCString( const nsTStringTuple& tuple ) - { - return nsTPromiseFlatString(tuple); - } - - -#endif /* !defined(nsTPromiseFlatString_h___) */ diff --git a/mozilla/xpcom/string/public/nsTString.h b/mozilla/xpcom/string/public/nsTString.h index 400a82bace9..239664b7b6f 100644 --- a/mozilla/xpcom/string/public/nsTString.h +++ b/mozilla/xpcom/string/public/nsTString.h @@ -36,40 +36,24 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTString_h___ -#define nsTString_h___ - -#ifndef nsTStringBase_h___ -#include "nsTStringBase.h" -#endif - -#ifndef nsTDependentSubstring_h___ -#include "nsTDependentSubstring.h" -#endif - -#ifndef nsReadableUtils_h___ -#include "nsReadableUtils.h" -#endif - /** * This is the canonical null-terminated string class. All subclasses * promise null-terminated storage. Instances of this class allocate * strings on the heap. */ -template -class nsTString : public nsTStringBase +class nsTString_CharT : public nsTStringBase_CharT { public: - typedef CharT char_type; + typedef CharT char_type; - typedef nsTString self_type; - typedef nsTStringBase string_base_type; + typedef nsTString_CharT self_type; + typedef nsTStringBase_CharT string_base_type; - typedef typename string_base_type::string_tuple_type string_tuple_type; - typedef typename string_base_type::abstract_string_type abstract_string_type; - typedef typename string_base_type::size_type size_type; + typedef string_base_type::string_tuple_type string_tuple_type; + typedef string_base_type::abstract_string_type abstract_string_type; + typedef string_base_type::size_type size_type; public: @@ -77,37 +61,37 @@ class nsTString : public nsTStringBase * constructors */ - nsTString() + nsTString_CharT() : string_base_type() {} explicit - nsTString( char_type c ) + nsTString_CharT( char_type c ) : string_base_type() { Assign(c); } explicit - nsTString( const char_type* data, size_type length = size_type(-1) ) + nsTString_CharT( const char_type* data, size_type length = size_type(-1) ) : string_base_type() { Assign(data, length); } - nsTString( const self_type& str ) + nsTString_CharT( const self_type& str ) : string_base_type() { Assign(str); } - nsTString( const string_tuple_type& tuple ) + nsTString_CharT( const string_tuple_type& tuple ) : string_base_type() { Assign(tuple); } explicit - nsTString( const abstract_string_type& readable ) + nsTString_CharT( const abstract_string_type& readable ) : string_base_type() { Assign(readable); @@ -127,99 +111,350 @@ class nsTString : public nsTStringBase return mData; } + +#if MOZ_STRING_WITH_OBSOLETE_API + + + /** + * Search for the given substring within this string. + * + * @param aString is substring to be sought in this + * @param aIgnoreCase selects case sensitivity + * @param aOffset tells us where in this string to start searching + * @param aCount tells us how far from the offset we are to search. Use + * -1 to search the whole string. + * @return offset in string, or kNotFound + */ + + PRInt32 Find( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; + PRInt32 Find( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; + +#ifdef CharT_is_PRUnichar + PRInt32 Find( const nsAFlatString& aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; + PRInt32 Find( const PRUnichar* aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; +#endif + + + /** + * This methods scans the string backwards, looking for the given string + * + * @param aString is substring to be sought in this + * @param aIgnoreCase tells us whether or not to do caseless compare + * @param aOffset tells us where in this string to start searching. + * Use -1 to search from the end of the string. + * @param aCount tells us how many iterations to make starting at the + * given offset. + * @return offset in string, or kNotFound + */ + + PRInt32 RFind( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; + PRInt32 RFind( const char* aCString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; + +#ifdef CharT_is_PRUnichar + PRInt32 RFind( const nsAFlatString& aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; + PRInt32 RFind( const PRUnichar* aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; +#endif + + + /** + * Search for given char within this string + * + * @param aChar is the character to search for + * @param aOffset tells us where in this strig to start searching + * @param aCount tells us how far from the offset we are to search. + * Use -1 to search the whole string. + * @return offset in string, or kNotFound + */ + + // PRInt32 FindChar( PRUnichar aChar, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; + PRInt32 RFindChar( PRUnichar aChar, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; + + + /** + * This method searches this string for the first character found in + * the given string. + * + * @param aString contains set of chars to be found + * @param aOffset tells us where in this string to start searching + * (counting from left) + * @return offset in string, or kNotFound + */ + + PRInt32 FindCharInSet( const char* aString, PRInt32 aOffset=0 ) const; + PRInt32 FindCharInSet( const self_type& aString, PRInt32 aOffset=0 ) const + { + return FindCharInSet(aString.get(), aOffset); + } + +#ifdef CharT_is_PRUnichar + PRInt32 FindCharInSet( const PRUnichar* aString, PRInt32 aOffset=0 ) const; +#endif + + + /** + * This method searches this string for the last character found in + * the given string. + * + * @param aString contains set of chars to be found + * @param aOffset tells us where in this string to start searching + * (counting from left) + * @return offset in string, or kNotFound + */ + + PRInt32 RFindCharInSet( const char_type* aString, PRInt32 aOffset=-1 ) const; + PRInt32 RFindCharInSet( const self_type& aString, PRInt32 aOffset=-1 ) const + { + return RFindCharInSet(aString.get(), aOffset); + } + + + /** + * Compares a given string to this string. + * + * @param aString is the string to be compared + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to compare + * @return -1,0,1 + */ + +#ifdef CharT_is_char + PRInt32 Compare( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const; +#else + PRInt32 CompareWithConversion( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const; +#endif + + + /** + * Equality check between given string and this string. + * + * @param aString is the string to check + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to compare + * @return boolean + */ + + PRBool EqualsWithConversion( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const; + PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const + { + return EqualsWithConversion(aString, PR_TRUE, aCount); + } + + +#ifdef CharT_is_PRUnichar + + /** + * Determine if given buffer is plain ascii + * + * @param aBuffer -- if null, then we test *this, otherwise we test given buffer + * @return TRUE if is all ascii chars or if strlen==0 + */ + + PRBool IsASCII(const PRUnichar* aBuffer=0); + + + /** + * Determine if given char is a valid space character + * + * @param aChar is character to be tested + * @return TRUE if is valid space char + */ + + static PRBool IsSpace(PRUnichar ch); + + + /** + * Copies data from internal buffer onto given char* buffer + * + * NOTE: This only copies as many chars as will fit in given buffer (clips) + * @param aBuf is the buffer where data is stored + * @param aBuflength is the max # of chars to move to buffer + * @param aOffset is the offset to copy from + * @return ptr to given buffer + */ + + char* ToCString( char* aBuf, PRUint32 aBufLength, PRUint32 aOffset=0 ) const; + +#endif // !CharT_is_PRUnichar + + /** + * Perform string to float conversion. + * + * @param aErrorCode will contain error if one occurs + * @return float rep of string value + */ + float ToFloat( PRInt32* aErrorCode ) const; + + + /** + * Perform string to int conversion. + * @param aErrorCode will contain error if one occurs + * @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you. + * @return int rep of string value, and possible (out) error code + */ + PRInt32 ToInteger( PRInt32* aErrorCode, PRUint32 aRadix=kRadix10 ) const; + + + /** + * |Left|, |Mid|, and |Right| are annoying signatures that seem better almost + * any _other_ way than they are now. Consider these alternatives + * + * aWritable = aReadable.Left(17); // ...a member function that returns a |Substring| + * aWritable = Left(aReadable, 17); // ...a global function that returns a |Substring| + * Left(aReadable, 17, aWritable); // ...a global function that does the assignment + * + * as opposed to the current signature + * + * aReadable.Left(aWritable, 17); // ...a member function that does the assignment + * + * or maybe just stamping them out in favor of |Substring|, they are just duplicate functionality + * + * aWritable = Substring(aReadable, 0, 17); + */ + + size_type Mid( self_type& aResult, PRUint32 aStartPos, PRUint32 aCount ) const; + + size_type Left( self_type& aResult, size_type aCount ) const + { + return Mid(aResult, 0, aCount); + } + + size_type Right( self_type& aResult, size_type aCount ) const + { + aCount = NS_MIN(mLength, aCount); + return Mid(aResult, mLength - aCount, aCount); + } + + + /** + * Set a char inside this string at given index + * + * @param aChar is the char you want to write into this string + * @param anIndex is the ofs where you want to write the given char + * @return TRUE if successful + */ + + PRBool SetCharAt( PRUnichar aChar, PRUint32 aIndex ); + + + /** + * These methods are used to remove all occurances of the + * characters found in aSet from this string. + * + * @param aSet -- characters to be cut from this + */ + void StripChars( const char* aSet ); + + + /** + * This method is used to remove all occurances of aChar from this + * string. + * + * @param aChar -- char to be stripped + * @param aOffset -- where in this string to start stripping chars + */ + + void StripChar( char_type aChar, PRInt32 aOffset=0 ); + + + /** + * This method strips whitespace throughout the string. + */ + void StripWhitespace(); + + + /** + * swaps occurence of 1 string for another + */ + + void ReplaceChar( char_type aOldChar, char_type aNewChar ); + void ReplaceChar( const char* aSet, char_type aNewChar ); + void ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue); + void ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue); + + + /** + * This method trims characters found in aTrimSet from + * either end of the underlying string. + * + * @param aSet -- contains chars to be trimmed from both ends + * @param aEliminateLeading + * @param aEliminateTrailing + * @param aIgnoreQuotes -- if true, causes surrounding quotes to be ignored + * @return this + */ + void Trim( const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE, PRBool aIgnoreQuotes=PR_FALSE ); + + /** + * This method strips whitespace from string. + * You can control whether whitespace is yanked from start and end of + * string as well. + * + * @param aEliminateLeading controls stripping of leading ws + * @param aEliminateTrailing controls stripping of trailing ws + */ + void CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE ); + + + /** + * assign/append/insert with _LOSSY_ conversion + */ + + void AssignWithConversion( const nsTAString_IncompatibleCharT& aString ); + void AssignWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 ); + + void AppendWithConversion( const nsTAString_IncompatibleCharT& aString ); + void AppendWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 ); + +#ifdef CharT_is_PRUnichar + void InsertWithConversion( const incompatible_char_type* aData, PRUint32 aOffset, PRInt32 aCount=-1 ); +#endif + + /** + * Append the given integer to this string + */ + + void AppendInt( PRInt32 aInteger, PRInt32 aRadix=kRadix10 ); //radix=8,10 or 16 + + /** + * Append the given float to this string + */ + + void AppendFloat( double aFloat ); + + +#endif // !MOZ_STRING_WITH_OBSOLETE_API + + protected: - nsTString( PRUint32 flags ) + nsTString_CharT( PRUint32 flags ) : string_base_type(flags) {} // allow subclasses to initialize fields directly - nsTString( char_type* data, size_type length, PRUint32 flags ) + nsTString_CharT( char_type* data, size_type length, PRUint32 flags ) : string_base_type(data, length, flags) {} }; - /** - * CBufDescriptor - * - * Allows a nsTAutoString to be configured to use a custom buffer. - */ -class CBufDescriptor - { - public: - - CBufDescriptor(char* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) - { - mStr = aString; - mCapacity = aCapacity; - mLength = aLength; - mFlags = F_SINGLE_BYTE | (aStackBased ? F_STACK_BASED : 0); - } - - CBufDescriptor(const char* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) - { - mStr = NS_CONST_CAST(char*, aString); - mCapacity = aCapacity; - mLength = aLength; - mFlags = F_SINGLE_BYTE | F_CONST | (aStackBased ? F_STACK_BASED : 0); - } - - CBufDescriptor(PRUnichar* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) - { - mUStr = aString; - mCapacity = aCapacity; - mLength = aLength; - mFlags = F_DOUBLE_BYTE | (aStackBased ? F_STACK_BASED : 0); - } - - CBufDescriptor(const PRUnichar* aString, PRBool aStackBased, PRUint32 aCapacity, PRInt32 aLength=-1) - { - mUStr = NS_CONST_CAST(PRUnichar*, aString); - mCapacity = aCapacity; - mLength = aLength; - mFlags = F_DOUBLE_BYTE | F_CONST | (aStackBased ? F_STACK_BASED : 0); - } - - union - { - char* mStr; - PRUnichar* mUStr; - }; - - PRUint32 mCapacity; - PRInt32 mLength; - - enum - { - F_CONST = (1 << 0), - F_STACK_BASED = (1 << 1), - F_SINGLE_BYTE = (1 << 2), - F_DOUBLE_BYTE = (1 << 3) - }; - - PRUint32 mFlags; - }; - - /** * nsTAutoString * - * Subclass of nsTString that adds support for stack-based string allocation. + * Subclass of nsTString_CharT that adds support for stack-based string allocation. * Do not allocate this class on the heap! ;-) */ -template -class nsTAutoString : public nsTString +class nsTAutoString_CharT : public nsTString_CharT { public: - typedef CharT char_type; + typedef CharT char_type; - typedef nsTAutoString self_type; - typedef nsTString string_type; + typedef nsTAutoString_CharT self_type; + typedef nsTString_CharT string_type; - typedef typename string_type::string_base_type string_base_type; - typedef typename string_type::string_tuple_type string_tuple_type; - typedef typename string_type::abstract_string_type abstract_string_type; - typedef typename string_type::size_type size_type; + typedef string_type::string_base_type string_base_type; + typedef string_type::string_tuple_type string_tuple_type; + typedef string_type::abstract_string_type abstract_string_type; + typedef string_type::size_type size_type; public: @@ -227,54 +462,54 @@ class nsTAutoString : public nsTString * constructors */ - nsTAutoString() + nsTAutoString_CharT() : string_type(mFixedBuf, 0, F_TERMINATED | F_FIXED), mFixedCapacity(kDefaultStringSize - 1) { mFixedBuf[0] = char_type(0); } explicit - nsTAutoString( char_type c ) + nsTAutoString_CharT( char_type c ) : string_type(mFixedBuf, 0, F_TERMINATED | F_FIXED), mFixedCapacity(kDefaultStringSize - 1) { Assign(c); } explicit - nsTAutoString( const char_type* data, size_type length = size_type(-1) ) + nsTAutoString_CharT( const char_type* data, size_type length = size_type(-1) ) : string_type(mFixedBuf, 0, F_TERMINATED | F_FIXED), mFixedCapacity(kDefaultStringSize - 1) { Assign(data, length); } - nsTAutoString( const self_type& str ) + nsTAutoString_CharT( const self_type& str ) : string_type(mFixedBuf, 0, F_TERMINATED | F_FIXED), mFixedCapacity(kDefaultStringSize - 1) { Assign(str); } explicit - nsTAutoString( const string_base_type& str ) + nsTAutoString_CharT( const string_base_type& str ) : string_type(mFixedBuf, 0, F_TERMINATED | F_FIXED), mFixedCapacity(kDefaultStringSize - 1) { Assign(str); } - nsTAutoString( const string_tuple_type& tuple ) + nsTAutoString_CharT( const string_tuple_type& tuple ) : string_type(mFixedBuf, 0, F_TERMINATED | F_FIXED), mFixedCapacity(kDefaultStringSize - 1) { Assign(tuple); } explicit - nsTAutoString( const abstract_string_type& readable ) + nsTAutoString_CharT( const abstract_string_type& readable ) : string_type(mFixedBuf, 0, F_TERMINATED | F_FIXED), mFixedCapacity(kDefaultStringSize - 1) { Assign(readable); } explicit - nsTAutoString( const CBufDescriptor& aBufDesc ) + nsTAutoString_CharT( const CBufDescriptor& aBufDesc ) : string_type(PRUint32(F_TERMINATED)) { Init(aBufDesc); @@ -292,7 +527,7 @@ class nsTAutoString : public nsTString private: - friend class nsTStringBase; + friend class nsTStringBase_CharT; void Init( const CBufDescriptor& aBufDesc ); @@ -301,29 +536,28 @@ class nsTAutoString : public nsTString }; -template -class nsTXPIDLString : public nsTString +class nsTXPIDLString_CharT : public nsTString_CharT { public: - typedef CharT char_type; + typedef CharT char_type; - typedef nsTXPIDLString self_type; - typedef nsTString string_type; + typedef nsTXPIDLString_CharT self_type; + typedef nsTString_CharT string_type; - typedef typename string_type::string_base_type string_base_type; - typedef typename string_type::string_tuple_type string_tuple_type; - typedef typename string_type::abstract_string_type abstract_string_type; - typedef typename string_type::size_type size_type; - typedef typename string_type::index_type index_type; + typedef string_type::string_base_type string_base_type; + typedef string_type::string_tuple_type string_tuple_type; + typedef string_type::abstract_string_type abstract_string_type; + typedef string_type::size_type size_type; + typedef string_type::index_type index_type; public: - nsTXPIDLString() + nsTXPIDLString_CharT() : string_type(nsnull, 0, 0) {} // copy-constructor required to avoid default - nsTXPIDLString( const self_type& str ) + nsTXPIDLString_CharT( const self_type& str ) : string_type(str) {} // this case operator is the reason why this class cannot just be a @@ -348,6 +582,7 @@ class nsTXPIDLString : public nsTString self_type& operator=( const abstract_string_type& readable ) { Assign(readable); return *this; } }; + /** * getter_Copies support for use with raw string out params: * @@ -355,21 +590,20 @@ class nsTXPIDLString : public nsTString * * void some_function() * { - * nsTString blah; + * nsXPIDLCString blah; * GetBlah(getter_Copies(blah)); * // ... * } */ -template -class getter_Copies_t +class nsTGetterCopies_CharT { public: typedef CharT char_type; - getter_Copies_t(nsTXPIDLString& str) + nsTGetterCopies_CharT(nsTXPIDLString_CharT& str) : mString(str), mData(nsnull) {} - ~getter_Copies_t() + ~nsTGetterCopies_CharT() { mString.Adopt(mData); // OK if mData is null } @@ -380,132 +614,13 @@ class getter_Copies_t } private: - nsTXPIDLString& mString; - char_type* mData; + nsTXPIDLString_CharT& mString; + char_type* mData; }; -template inline -getter_Copies_t -getter_Copies( nsTXPIDLString& aString ) +nsTGetterCopies_CharT +getter_Copies( nsTXPIDLString_CharT& aString ) { - return getter_Copies_t(aString); + return nsTGetterCopies_CharT(aString); } - - - /** - * A helper class that converts a UTF-16 string to ASCII in a lossy manner - */ -class NS_COM NS_LossyConvertUTF16toASCII : public nsCAutoString - { - public: - explicit - NS_LossyConvertUTF16toASCII( const PRUnichar* aString ) - { - LossyAppendUTF16toASCII(aString, *this); - } - - NS_LossyConvertUTF16toASCII( const PRUnichar* aString, PRUint32 aLength ) - { - LossyCopyUTF16toASCII(nsDependentSubstring(aString, aString + aLength), *this); - } - - explicit - NS_LossyConvertUTF16toASCII( const nsAString& aString ) - { - LossyCopyUTF16toASCII(aString, *this); - } - - private: - // NOT TO BE IMPLEMENTED - NS_LossyConvertUTF16toASCII( char ); - }; - - -class NS_COM NS_ConvertASCIItoUTF16 : public nsAutoString - { - public: - explicit - NS_ConvertASCIItoUTF16( const char* aCString ) - { - AppendASCIItoUTF16(aCString, *this); - } - - NS_ConvertASCIItoUTF16( const char* aCString, PRUint32 aLength ) - { - CopyASCIItoUTF16(nsDependentCSubstring(aCString, aCString + aLength), *this); - } - - explicit - NS_ConvertASCIItoUTF16( const nsACString& aCString ) - { - CopyASCIItoUTF16(aCString, *this); - } - - private: - // NOT TO BE IMPLEMENTED - NS_ConvertASCIItoUTF16( PRUnichar ); - }; - - - /** - * A helper class that converts a UTF-16 string to UTF-8 - */ -class NS_COM NS_ConvertUTF16toUTF8 : public nsCAutoString - { - public: - explicit - NS_ConvertUTF16toUTF8( const PRUnichar* aString ) - { - CopyUTF16toUTF8(aString, *this); - } - - NS_ConvertUTF16toUTF8( const PRUnichar* aString, PRUint32 aLength ) - { - CopyUTF16toUTF8(nsDependentSubstring(aString, aString + aLength), *this); - } - - explicit - NS_ConvertUTF16toUTF8( const nsAString& aString ) - { - CopyUTF16toUTF8(aString, *this); - } - - private: - // NOT TO BE IMPLEMENTED - NS_ConvertUTF16toUTF8( char ); - }; - - -class NS_COM NS_ConvertUTF8toUTF16 : public nsAutoString - { - public: - explicit - NS_ConvertUTF8toUTF16( const char* aCString ) - { - CopyUTF8toUTF16(aCString, *this); - } - - NS_ConvertUTF8toUTF16( const char* aCString, PRUint32 aLength ) - { - CopyUTF8toUTF16(nsDependentCSubstring(aCString, aCString + aLength), *this); - } - - explicit - NS_ConvertUTF8toUTF16( const nsACString& aCString ) - { - CopyUTF8toUTF16(aCString, *this); - } - - private: - NS_ConvertUTF8toUTF16( PRUnichar ); - }; - -// Backward compatibility -typedef NS_ConvertUTF16toUTF8 NS_ConvertUCS2toUTF8; -typedef NS_LossyConvertUTF16toASCII NS_LossyConvertUCS2toASCII; -typedef NS_ConvertASCIItoUTF16 NS_ConvertASCIItoUCS2; -typedef NS_ConvertUTF8toUTF16 NS_ConvertUTF8toUCS2; -typedef nsAutoString nsVoidableString; - -#endif // !defined(nsTString_h___) diff --git a/mozilla/xpcom/string/public/nsTStringBase.h b/mozilla/xpcom/string/public/nsTStringBase.h index 367c9e0ab15..57f317dbe35 100644 --- a/mozilla/xpcom/string/public/nsTStringBase.h +++ b/mozilla/xpcom/string/public/nsTStringBase.h @@ -36,61 +36,36 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTStringBase_h___ -#define nsTStringBase_h___ - - // enable support for the obsolete string API if not explicitly disabled -#ifndef MOZ_STRING_WITH_OBSOLETE_API -#define MOZ_STRING_WITH_OBSOLETE_API 1 - // radix values for ToInteger/AppendInt -#define kRadix10 (10) -#define kRadix16 (16) -#define kAutoDetect (100) -#define kRadixUnknown (kAutoDetect+1) -#endif - -#ifndef nsTAString_h___ -#include "nsTAString.h" -#endif - -#ifndef nsTStringTuple_h___ -#include "nsTStringTuple.h" -#endif - - -static const PRInt32 kNotFound = -1; - /** - * nsTStringBase + * nsTStringBase_CharT * * The base string type. This type is not instantiated directly. A sub- * class is instantiated instead. For example, see nsTString. */ -template -class NS_COM nsTStringBase : public nsTAString +class NS_COM nsTStringBase_CharT : public nsTAString_CharT { public: - typedef CharT char_type; - typedef nsCharTraits char_traits; + typedef CharT char_type; + typedef nsCharTraits char_traits; - typedef typename char_traits::incompatible_char_type incompatible_char_type; + typedef char_traits::incompatible_char_type incompatible_char_type; - typedef nsTStringBase self_type; - typedef nsTString string_type; - typedef nsTStringTuple string_tuple_type; - typedef nsTAString abstract_string_type; + typedef nsTStringBase_CharT self_type; + typedef nsTString_CharT string_type; + typedef nsTStringTuple_CharT string_tuple_type; + typedef nsTAString_CharT abstract_string_type; - typedef nsReadingIterator const_iterator; - typedef nsWritingIterator iterator; - typedef char_type* char_iterator; - typedef const char_type* const_char_iterator; + typedef nsReadingIterator const_iterator; + typedef nsWritingIterator iterator; + typedef char_type* char_iterator; + typedef const char_type* const_char_iterator; - typedef nsTStringComparator comparator_type; + typedef nsTStringComparator_CharT comparator_type; - typedef PRUint32 size_type; - typedef PRUint32 index_type; + typedef PRUint32 size_type; + typedef PRUint32 index_type; public: @@ -300,327 +275,19 @@ class NS_COM nsTStringBase : public nsTAString /** * string data is never null, but can be marked void. if true, the - * string will be truncated. @see nsTStringBase::IsVoid + * string will be truncated. @see nsTStringBase_CharT::IsVoid */ void SetIsVoid( PRBool ); -#if MOZ_STRING_WITH_OBSOLETE_API - - - /** - * Search for the given substring within this string. - * - * @param aString is substring to be sought in this - * @param aIgnoreCase selects case sensitivity - * @param aOffset tells us where in this string to start searching - * @param aCount tells us how far from the offset we are to search. Use - * -1 to search the whole string. - * @return offset in string, or kNotFound - */ - - PRInt32 Find( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; - PRInt32 Find( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; - - // NOTE: these two variants are only implemented for nsTStringBase - PRInt32 Find( const nsAFlatString& aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; - PRInt32 Find( const PRUnichar* aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; - - - /** - * This methods scans the string backwards, looking for the given string - * - * @param aString is substring to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @param aOffset tells us where in this string to start searching. - * Use -1 to search from the end of the string. - * @param aCount tells us how many iterations to make starting at the - * given offset. - * @return offset in string, or kNotFound - */ - - PRInt32 RFind( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; - PRInt32 RFind( const char* aCString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; - - // NOTE: these two variants are only implemented for nsTStringBase - PRInt32 RFind( const nsAFlatString& aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; - PRInt32 RFind( const PRUnichar* aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; - - - /** - * Search for given char within this string - * - * @param aChar is the character to search for - * @param aOffset tells us where in this strig to start searching - * @param aCount tells us how far from the offset we are to search. - * Use -1 to search the whole string. - * @return offset in string, or kNotFound - */ - - // PRInt32 FindChar( PRUnichar aChar, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const; - PRInt32 RFindChar( PRUnichar aChar, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const; - - - /** - * This method searches this string for the first character found in - * the given string. - * - * @param aString contains set of chars to be found - * @param aOffset tells us where in this string to start searching - * (counting from left) - * @return offset in string, or kNotFound - */ - - PRInt32 FindCharInSet( const char* aString, PRInt32 aOffset=0 ) const; - PRInt32 FindCharInSet( const nsCString& aString, PRInt32 aOffset=0 ) const - { - return FindCharInSet(aString.get(), aOffset); - } - - // NOTE: this variant is only implemented for nsTStringBase - PRInt32 FindCharInSet( const PRUnichar* aString, PRInt32 aOffset=0 ) const; - - - /** - * This method searches this string for the last character found in - * the given string. - * - * @param aString contains set of chars to be found - * @param aOffset tells us where in this string to start searching - * (counting from left) - * @return offset in string, or kNotFound - */ - - PRInt32 RFindCharInSet( const char_type* aString, PRInt32 aOffset=-1 ) const; - PRInt32 RFindCharInSet( const nsCString& aString, PRInt32 aOffset=-1 ) const - { - return RFindCharInSet(aString.get(), aOffset); - } - - - /** - * Compares a given string to this string. - * - * @param aString is the string to be compared - * @param aIgnoreCase tells us how to treat case - * @param aCount tells us how many chars to compare - * @return -1,0,1 - */ - - // NOTE: this method is only implemented for nsTStringBase - PRInt32 Compare( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const; - - // NOTE: this method is only implemented for nsTStringBase - PRInt32 CompareWithConversion( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const; - - - /** - * Equality check between given string and this string. - * - * @param aString is the string to check - * @param aIgnoreCase tells us how to treat case - * @param aCount tells us how many chars to compare - * @return boolean - */ - - PRBool EqualsWithConversion( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const; - PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const - { - return EqualsWithConversion(aString, PR_TRUE, aCount); - } - - - /** - * Determine if given buffer is plain ascii - * - * @param aBuffer -- if null, then we test *this, otherwise we test given buffer - * @return TRUE if is all ascii chars or if strlen==0 - */ - - // NOTE: this method is only implemented for nsTStringBase - PRBool IsASCII(const PRUnichar* aBuffer=0); - - - /** - * Determine if given char is a valid space character - * - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ - - // NOTE: this method is only implemented for nsTStringBase - static PRBool IsSpace(PRUnichar ch); - - - /** - * Copies data from internal buffer onto given char* buffer - * - * NOTE: This only copies as many chars as will fit in given buffer (clips) - * @param aBuf is the buffer where data is stored - * @param aBuflength is the max # of chars to move to buffer - * @param aOffset is the offset to copy from - * @return ptr to given buffer - */ - - // NOTE: this method is only implemented for nsTStringBase - char* ToCString( char* aBuf, PRUint32 aBufLength, PRUint32 aOffset=0 ) const; - - - /** - * Perform string to float conversion. - * - * @param aErrorCode will contain error if one occurs - * @return float rep of string value - */ - float ToFloat( PRInt32* aErrorCode ) const; - - - /** - * Perform string to int conversion. - * @param aErrorCode will contain error if one occurs - * @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you. - * @return int rep of string value, and possible (out) error code - */ - PRInt32 ToInteger( PRInt32* aErrorCode, PRUint32 aRadix=kRadix10 ) const; - - - /** - * |Left|, |Mid|, and |Right| are annoying signatures that seem better almost - * any _other_ way than they are now. Consider these alternatives - * - * aWritable = aReadable.Left(17); // ...a member function that returns a |Substring| - * aWritable = Left(aReadable, 17); // ...a global function that returns a |Substring| - * Left(aReadable, 17, aWritable); // ...a global function that does the assignment - * - * as opposed to the current signature - * - * aReadable.Left(aWritable, 17); // ...a member function that does the assignment - * - * or maybe just stamping them out in favor of |Substring|, they are just duplicate functionality - * - * aWritable = Substring(aReadable, 0, 17); - */ - - size_type Mid( self_type& aResult, PRUint32 aStartPos, PRUint32 aCount ) const; - - size_type Left( self_type& aResult, size_type aCount ) const - { - return Mid(aResult, 0, aCount); - } - - size_type Right( self_type& aResult, size_type aCount ) const - { - aCount = NS_MIN(mLength, aCount); - return Mid(aResult, mLength - aCount, aCount); - } - - - /** - * Set a char inside this string at given index - * - * @param aChar is the char you want to write into this string - * @param anIndex is the ofs where you want to write the given char - * @return TRUE if successful - */ - - PRBool SetCharAt( PRUnichar aChar, PRUint32 aIndex ); - - - /** - * These methods are used to remove all occurances of the - * characters found in aSet from this string. - * - * @param aSet -- characters to be cut from this - */ - void StripChars( const char* aSet ); - - - /** - * This method is used to remove all occurances of aChar from this - * string. - * - * @param aChar -- char to be stripped - * @param aOffset -- where in this string to start stripping chars - */ - - void StripChar( char_type aChar, PRInt32 aOffset=0 ); - - - /** - * This method strips whitespace throughout the string. - */ - void StripWhitespace(); - - - /** - * swaps occurence of 1 string for another - */ - - void ReplaceChar( char_type aOldChar, char_type aNewChar ); - void ReplaceChar( const char* aSet, char_type aNewChar ); - void ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue); - void ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue); - - - /** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @param aSet -- contains chars to be trimmed from both ends - * @param aEliminateLeading - * @param aEliminateTrailing - * @param aIgnoreQuotes -- if true, causes surrounding quotes to be ignored - * @return this - */ - void Trim( const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE, PRBool aIgnoreQuotes=PR_FALSE ); - - /** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from start and end of - * string as well. - * - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - */ - void CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE ); - - - /** - * assign/append/insert with _LOSSY_ conversion - */ - - void AssignWithConversion( const nsTAString& aString ); - void AssignWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 ); - - void AppendWithConversion( const nsTAString& aString ); - void AppendWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 ); - - // NOTE: this method is only implemented for nsTStringBase - void InsertWithConversion( const incompatible_char_type* aData, PRUint32 aOffset, PRInt32 aCount=-1 ); - - /** - * Append the given integer to this string - */ - - void AppendInt( PRInt32 aInteger, PRInt32 aRadix=kRadix10 ); //radix=8,10 or 16 - - /** - * Append the given float to this string - */ - - void AppendFloat( double aFloat ); - - -#endif // !MOZ_STRING_WITH_OBSOLETE_API - public: /** * this is public to support automatic conversion of tuple to string * base type, which helps avoid converting to nsTAString. */ - nsTStringBase(const string_tuple_type& tuple) + nsTStringBase_CharT(const string_tuple_type& tuple) : abstract_string_type(nsnull, 0, F_NONE) { Assign(tuple); @@ -628,26 +295,26 @@ class NS_COM nsTStringBase : public nsTAString protected: - friend class nsTObsoleteAStringThunk; - friend class nsTAString; - friend class nsTStringTuple; + friend class nsTObsoleteAStringThunk_CharT; + friend class nsTAString_CharT; + friend class nsTStringTuple_CharT; // default initialization - nsTStringBase() + nsTStringBase_CharT() : abstract_string_type( NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer), 0, F_TERMINATED) {} // allow subclasses to initialize fields directly - nsTStringBase( char_type *data, size_type length, PRUint32 flags ) + nsTStringBase_CharT( char_type *data, size_type length, PRUint32 flags ) : abstract_string_type(data, length, flags) {} // version of constructor that leaves mData and mLength uninitialized - nsTStringBase( PRUint32 flags ) + nsTStringBase_CharT( PRUint32 flags ) : abstract_string_type(flags) {} // copy-constructor, constructs as dependent on given object // (NOTE: this is for internal use only) - nsTStringBase( const self_type& str ) + nsTStringBase_CharT( const self_type& str ) : abstract_string_type( str.mData, str.mLength, str.mFlags & (F_TERMINATED | F_VOIDED)) {} @@ -702,4 +369,19 @@ class NS_COM nsTStringBase : public nsTAString }; }; -#endif // !defined(nsTStringBase_h___) + + + /** + * nsTAString::ToString + * + * defined here since it depends on nsStringBase class definition. + */ + +inline +const nsTAString_CharT::string_base_type +nsTAString_CharT::ToString() const + { + const char_type* data; + size_type length = GetReadableBuffer(&data); + return string_base_type(NS_CONST_CAST(char_type*, data), length, 0); + } diff --git a/mozilla/xpcom/string/public/nsTStringTuple.h b/mozilla/xpcom/string/public/nsTStringTuple.h index f64efff8e5d..d73314719be 100644 --- a/mozilla/xpcom/string/public/nsTStringTuple.h +++ b/mozilla/xpcom/string/public/nsTStringTuple.h @@ -36,41 +36,33 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsTStringTuple_h___ -#define nsTStringTuple_h___ - -#ifndef nsTStringBase_h___ -#include "nsTStringBase.h" -#endif - /** - * nsTStringTuple + * nsTStringTuple_CharT * * Represents a tuple of string fragments. Built as a recursive binary tree. */ -template -class NS_COM nsTStringTuple +class NS_COM nsTStringTuple_CharT { public: - typedef CharT char_type; + typedef CharT char_type; + typedef nsCharTraits char_traits; - typedef nsTStringTuple self_type; - typedef nsTStringBase string_base_type; - typedef nsTString string_type; + typedef nsTStringTuple_CharT self_type; + typedef nsTStringBase_CharT string_base_type; + typedef nsTString_CharT string_type; + typedef nsTAString_CharT abstract_string_type; - typedef typename string_base_type::abstract_string_type abstract_string_type; - typedef typename string_base_type::char_traits char_traits; - typedef typename string_base_type::size_type size_type; + typedef PRUint32 size_type; public: - nsTStringTuple(const void* a, const void* b) + nsTStringTuple_CharT(const void* a, const void* b) : mHead(nsnull) , mFragA(a) , mFragB(b) {} - nsTStringTuple(const self_type& head, const void* frag) + nsTStringTuple_CharT(const self_type& head, const void* frag) : mHead(&head) , mFragA(nsnull) // this fragment is ignored when head != nsnull , mFragB(frag) {} @@ -93,12 +85,6 @@ class NS_COM nsTStringTuple */ PRBool IsDependentOn(const char_type *start, const char_type *end) const; - /** - * allow automatic flattening (XXX would be better to fix callsites to avoid this) - * but, at least our string type uses a shared buffer :) - */ -//XXX operator const string_type() const { return string_type(*this); } - private: const self_type* mHead; @@ -106,60 +92,52 @@ class NS_COM nsTStringTuple const void* mFragB; // type of mFrag? is given by the low-order bit. if set, the type - // is nsTAString, else it is nsTStringBase. + // is nsTAString_CharT, else it is nsTStringBase_CharT. }; #define NS_FLAG_READABLE(_r) ((void*)( ((unsigned long) _r) | 0x1 )) -template inline -const nsTStringTuple -operator+(const nsTStringBase& a, const nsTStringBase& b) +const nsTStringTuple_CharT +operator+(const nsTStringBase_CharT& a, const nsTStringBase_CharT& b) { - return nsTStringTuple(&a, &b); + return nsTStringTuple_CharT(&a, &b); } -template inline -const nsTStringTuple -operator+(const nsTStringTuple& tuple, const nsTStringBase& str) +const nsTStringTuple_CharT +operator+(const nsTStringTuple_CharT& tuple, const nsTStringBase_CharT& str) { - return nsTStringTuple(tuple, &str); + return nsTStringTuple_CharT(tuple, &str); } -template inline -const nsTStringTuple -operator+(const nsTAString& a, const nsTAString& b) +const nsTStringTuple_CharT +operator+(const nsTAString_CharT& a, const nsTAString_CharT& b) { - return nsTStringTuple(NS_FLAG_READABLE(&a), NS_FLAG_READABLE(&b)); + return nsTStringTuple_CharT(NS_FLAG_READABLE(&a), NS_FLAG_READABLE(&b)); } -template inline -const nsTStringTuple -operator+(const nsTStringTuple& tuple, const nsTAString& readable) +const nsTStringTuple_CharT +operator+(const nsTStringTuple_CharT& tuple, const nsTAString_CharT& readable) { - return nsTStringTuple(tuple, NS_FLAG_READABLE(&readable)); + return nsTStringTuple_CharT(tuple, NS_FLAG_READABLE(&readable)); } -template inline -const nsTStringTuple -operator+(const nsTStringBase& str, const nsTAString& readable) +const nsTStringTuple_CharT +operator+(const nsTStringBase_CharT& str, const nsTAString_CharT& readable) { - return nsTStringTuple(&str, NS_FLAG_READABLE(&readable)); + return nsTStringTuple_CharT(&str, NS_FLAG_READABLE(&readable)); } -template inline -const nsTStringTuple -operator+(const nsTAString& readable, const nsTStringBase& str) +const nsTStringTuple_CharT +operator+(const nsTAString_CharT& readable, const nsTStringBase_CharT& str) { - return nsTStringTuple(NS_FLAG_READABLE(&readable), &str); + return nsTStringTuple_CharT(NS_FLAG_READABLE(&readable), &str); } #undef NS_FLAG_READABLE - -#endif // !defined(nsTStringTuple_h___) diff --git a/mozilla/xpcom/string/public/string-template-def-char.h b/mozilla/xpcom/string/public/string-template-def-char.h new file mode 100644 index 00000000000..9cf58f82108 --- /dev/null +++ b/mozilla/xpcom/string/public/string-template-def-char.h @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#define CharT char +#define CharT_is_char 1 +#define nsTObsoleteAString_CharT nsObsoleteACString +#define nsTObsoleteAStringThunk_CharT nsObsoleteACStringThunk +#define nsTAString_CharT nsACString +#define nsTAString_IncompatibleCharT nsAString +#define nsTString_CharT nsCString +#define nsTAutoString_CharT nsCAutoString +#define nsTStringBase_CharT nsCStringBase +#define nsTStringTuple_CharT nsCStringTuple +#define nsTStringComparator_CharT nsCStringComparator +#define nsTDefaultStringComparator_CharT nsDefaultCStringComparator +#define nsTPromiseFlatString_CharT nsPromiseFlatCString +#define nsTDependentString_CharT nsDependentCString +#define nsTDependentSubstring_CharT nsDependentCSubstring +#define nsTXPIDLString_CharT nsXPIDLCString +#define nsTGetterCopies_CharT nsCGetterCopies diff --git a/mozilla/xpcom/string/public/string-template-def-unichar.h b/mozilla/xpcom/string/public/string-template-def-unichar.h new file mode 100644 index 00000000000..be343076fd1 --- /dev/null +++ b/mozilla/xpcom/string/public/string-template-def-unichar.h @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#define CharT PRUnichar +#define CharT_is_PRUnichar 1 +#define nsTObsoleteAString_CharT nsObsoleteAString +#define nsTObsoleteAStringThunk_CharT nsObsoleteAStringThunk +#define nsTAString_CharT nsAString +#define nsTAString_IncompatibleCharT nsACString +#define nsTString_CharT nsString +#define nsTAutoString_CharT nsAutoString +#define nsTStringBase_CharT nsStringBase +#define nsTStringTuple_CharT nsStringTuple +#define nsTStringComparator_CharT nsStringComparator +#define nsTDefaultStringComparator_CharT nsDefaultStringComparator +#define nsTPromiseFlatString_CharT nsPromiseFlatString +#define nsTDependentString_CharT nsDependentString +#define nsTDependentSubstring_CharT nsDependentSubstring +#define nsTXPIDLString_CharT nsXPIDLString +#define nsTGetterCopies_CharT nsGetterCopies diff --git a/mozilla/xpcom/string/public/string-template-undef.h b/mozilla/xpcom/string/public/string-template-undef.h new file mode 100644 index 00000000000..d8b158f802d --- /dev/null +++ b/mozilla/xpcom/string/public/string-template-undef.h @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#undef CharT +#undef CharT_is_PRUnichar +#undef CharT_is_char +#undef nsTObsoleteAString_CharT +#undef nsTObsoleteAStringThunk_CharT +#undef nsTAString_CharT +#undef nsTAString_IncompatibleCharT +#undef nsTString_CharT +#undef nsTAutoString_CharT +#undef nsTStringBase_CharT +#undef nsTStringTuple_CharT +#undef nsTStringComparator_CharT +#undef nsTDefaultStringComparator_CharT +#undef nsTPromiseFlatString_CharT +#undef nsTDependentString_CharT +#undef nsTDependentSubstring_CharT +#undef nsTXPIDLString_CharT +#undef nsTGetterCopies_CharT diff --git a/mozilla/xpcom/string/src/Makefile.in b/mozilla/xpcom/string/src/Makefile.in index 3097dba5977..1325691e4b6 100644 --- a/mozilla/xpcom/string/src/Makefile.in +++ b/mozilla/xpcom/string/src/Makefile.in @@ -38,29 +38,16 @@ REQUIRES = xpcom \ CPPSRCS = \ nsPrintfCString.cpp \ nsReadableUtils.cpp \ - nsTAString.cpp \ - nsTDependentSubstring.cpp \ - nsTObsoleteAStringThunk.cpp \ - nsTPromiseFlatString.cpp \ - nsTString.cpp \ - nsTStringBase.cpp \ - nsTStringComparator.cpp \ - nsTStringTuple.cpp \ + nsAString.cpp \ + nsDependentSubstring.cpp \ + nsObsoleteAStringThunk.cpp \ + nsPromiseFlatString.cpp \ + nsString.cpp \ + nsStringBase.cpp \ + nsStringComparator.cpp \ + nsStringTuple.cpp \ $(NULL) -#CPPSRCS = \ -# nsASingleFragmentString.cpp \ -# nsAString.cpp \ -# nsDependentConcatenation.cpp \ -# nsDependentSubstring.cpp \ -# nsFragmentedString.cpp \ -# nsPromiseFlatString.cpp \ -# nsSharableString.cpp \ -# nsSharedBufferList.cpp \ -# nsSlidingString.cpp \ -# nsXPIDLString.cpp \ -# $(NULL) - # we don't want the shared lib, but we want to force the creation of a # static lib. FORCE_STATIC_LIB = 1 diff --git a/mozilla/xpcom/string/src/nsASingleFragmentString.cpp b/mozilla/xpcom/string/src/nsASingleFragmentString.cpp deleted file mode 100644 index c9f99624010..00000000000 --- a/mozilla/xpcom/string/src/nsASingleFragmentString.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * - * The Initial Developer of the Original Code is Netscape - * Communications. Portions created by Netscape Communications are - * Copyright (C) 2001 by Netscape Communications. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - */ - -#include "nsASingleFragmentString.h" - -const nsASingleFragmentString::char_type* -nsASingleFragmentString::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - { - const buffer_handle_type* buffer = GetBufferHandle(); - NS_ASSERTION(buffer, "trouble: no buffer!"); - if (!buffer) - return 0; - - aFragment.mEnd = buffer->DataEnd(); - return (aFragment.mStart = buffer->DataStart()) + aOffset; - } - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - -nsASingleFragmentString::char_type* -nsASingleFragmentString::GetWritableFragment( fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - { - buffer_handle_type* buffer = NS_CONST_CAST(buffer_handle_type*, GetBufferHandle()); - NS_ASSERTION(buffer, "trouble: no buffer!"); - - aFragment.mEnd = buffer->DataEnd(); - return (aFragment.mStart = buffer->DataStart()) + aOffset; - } - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - -const nsASingleFragmentCString::char_type* -nsASingleFragmentCString::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - { - const buffer_handle_type* buffer = GetBufferHandle(); - NS_ASSERTION(buffer, "trouble: no buffer!"); - if (!buffer) - return 0; - - aFragment.mEnd = buffer->DataEnd(); - return (aFragment.mStart = buffer->DataStart()) + aOffset; - } - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - -nsASingleFragmentCString::char_type* -nsASingleFragmentCString::GetWritableFragment( fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - { - buffer_handle_type* buffer = NS_CONST_CAST(buffer_handle_type*, GetBufferHandle()); - NS_ASSERTION(buffer, "trouble: no buffer!"); - - aFragment.mEnd = buffer->DataEnd(); - return (aFragment.mStart = buffer->DataStart()) + aOffset; - } - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - -PRUint32 -nsASingleFragmentString::Length() const - { - const buffer_handle_type* handle = GetBufferHandle(); - return PRUint32(handle ? handle->DataLength() : 0); - } - -PRUint32 -nsASingleFragmentCString::Length() const - { - const buffer_handle_type* handle = GetBufferHandle(); - return PRUint32(handle ? handle->DataLength() : 0); - } diff --git a/mozilla/xpcom/string/src/nsAString.cpp b/mozilla/xpcom/string/src/nsAString.cpp index de7c4b848d6..5b481662cc1 100644 --- a/mozilla/xpcom/string/src/nsAString.cpp +++ b/mozilla/xpcom/string/src/nsAString.cpp @@ -1,1006 +1,51 @@ /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. - * - * The Initial Developer of the Original Code is Netscape - * Communications. Portions created by Netscape Communications are - * Copyright (C) 2001 by Netscape Communications. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - */ + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ #include "nsAString.h" -#include "nsDependentSubstring.h" -#include "nsDependentString.h" -#include "plstr.h" - - -int -nsDefaultStringComparator::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const - { - return nsCharTraits::compare(lhs, rhs, aLength); - } - -int -nsDefaultStringComparator::operator()( char_type lhs, char_type rhs) const - { - return lhs - rhs; - } - -NS_COM -int -Compare( const nsAString& lhs, const nsAString& rhs, const nsStringComparator& aComparator ) - { - typedef nsAString::size_type size_type; - - if ( &lhs == &rhs ) - return 0; - - size_type lLength = lhs.Length(); - size_type rLength = rhs.Length(); - size_type lengthToCompare = NS_MIN(lLength, rLength); - - nsAString::const_iterator leftIter, rightIter; - lhs.BeginReading(leftIter); - rhs.BeginReading(rightIter); - - for (;;) - { - size_type lengthAvailable = size_type( NS_MIN(leftIter.size_forward(), rightIter.size_forward()) ); - - if ( lengthAvailable > lengthToCompare ) - lengthAvailable = lengthToCompare; - - { - int result; - // Note: |result| should be declared in this |if| expression, but some compilers don't like that - if ( (result = aComparator(leftIter.get(), rightIter.get(), lengthAvailable)) != 0 ) - return result; - } - - if ( !(lengthToCompare -= lengthAvailable) ) - break; - - leftIter.advance( PRInt32(lengthAvailable) ); - rightIter.advance( PRInt32(lengthAvailable) ); - } - - if ( lLength < rLength ) - return -1; - else if ( rLength < lLength ) - return 1; - else - return 0; - } - -const nsAString::shared_buffer_handle_type* -nsAString::GetSharedBufferHandle() const - { - return 0; - } - -const nsAString::buffer_handle_type* -nsAString::GetFlatBufferHandle() const - { - return GetSharedBufferHandle(); - } - -const nsAString::buffer_handle_type* -nsAString::GetBufferHandle() const - { - return GetSharedBufferHandle(); - } - -PRUint32 -nsAString::GetImplementationFlags() const - { - return 0; - } - - -PRBool -nsAString::IsVoid() const - { - return PR_FALSE; - } - -void -nsAString::SetIsVoid( PRBool ) - { - // |SetIsVoid| is ignored by default - } - -PRBool -nsAString::Equals( const char_type* rhs, const nsStringComparator& aComparator ) const - { - return Equals(nsDependentString(rhs), aComparator); - } - - - -nsAString::char_type -nsAString::First() const - { - NS_ASSERTION(Length()>0, "|First()| on an empty string"); - - const_iterator iter; - return *BeginReading(iter); - } - -nsAString::char_type -nsAString::Last() const - { - NS_ASSERTION(Length()>0, "|Last()| on an empty string"); - - const_iterator iter; - - if ( !IsEmpty() ) - { - EndReading(iter); - iter.advance(-1); - } - - return *iter; // Note: this has undefined results if |IsEmpty()| - } - -nsAString::size_type -nsAString::CountChar( char_type c ) const - { - /* - re-write this to use a counting sink - */ - - size_type result = 0; - size_type lengthToExamine = Length(); - - const_iterator iter; - for ( BeginReading(iter); ; ) - { - PRInt32 lengthToExamineInThisFragment = iter.size_forward(); - const char_type* fromBegin = iter.get(); - result += size_type(NS_COUNT(fromBegin, fromBegin+lengthToExamineInThisFragment, c)); - if ( !(lengthToExamine -= lengthToExamineInThisFragment) ) - return result; - iter.advance(lengthToExamineInThisFragment); - } - // never reached; quiets warnings - return 0; - } - -PRInt32 -nsAString::FindChar( char_type aChar, PRUint32 aOffset ) const - { - const_iterator iter, done_searching; - BeginReading(iter).advance( PRInt32(aOffset) ); - EndReading(done_searching); - - size_type lengthSearched = 0; - while ( iter != done_searching ) - { - PRInt32 fragmentLength = iter.size_forward(); - const char_type* charFoundAt = nsCharTraits::find(iter.get(), fragmentLength, aChar); - if ( charFoundAt ) - return lengthSearched + (charFoundAt-iter.get()) + aOffset; - - lengthSearched += fragmentLength; - iter.advance(fragmentLength); - } - - return -1; - } - -PRBool -nsAString::IsDependentOn( const self_type& aString ) const - { - const_fragment_type f1; - const char_type* s1 = GetReadableFragment(f1, kFirstFragment); - while ( s1 ) - { - const_fragment_type f2; - const char_type* s2 = aString.GetReadableFragment(f2, kFirstFragment); - while ( s2 ) - { - // if it _isn't_ the case that - // one fragment starts after the other ends, - // or ends before the other starts, - // then, they conflict: - // !(f2.mStart>=f1.mEnd || f2.mEnd<=f1.mStart) - // - // Simplified, that gives us: - if ( f2.mStart < f1.mEnd && f2.mEnd > f1.mStart ) - return PR_TRUE; - - s2 = aString.GetReadableFragment(f2, kNextFragment); - } - s1 = GetReadableFragment(f1, kNextFragment); - } - return PR_FALSE; - } - - // - // |Assign()| - // - -void -nsAString::do_AssignFromReadable( const self_type& aReadable ) - /* - ...we need to check whether the string that's being assigned into |this| somehow references |this|. - E.g., - - ... writable& w ... - ... readable& r ... - - w = r + w; - - In this example, you can see that unless the characters promised by |w| in |r+w| are resolved before - anything starts getting copied into |w|, there will be trouble. They will be overritten by the contents - of |r| before being retrieved to be appended. - - We could have a really tricky solution where we tell the promise to resolve _just_ the data promised - by |this|, but this should be a rare case, since clients with more local knowledge will know that, e.g., - in the case above, |Insert| could have special behavior with significantly better performance. Since - it's a rare case anyway, we should just do the simplest thing that could possibly work, resolve the - entire promise. If we measure and this turns out to show up on performance radar, we then have the - option to fix either the callers or this mechanism. - */ - { - // self-assign is a no-op - if ( this == &aReadable) - return; - - if ( !aReadable.IsDependentOn(*this) ) - UncheckedAssignFromReadable(aReadable); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - // Note: not exception safe. We need something to manage temporary buffers like this - - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedAssignFromReadable(Substring(buffer, buffer + length)); - delete[] buffer; - } - // else assert? - } - } - -void -nsAString::UncheckedAssignFromReadable( const self_type& aReadable ) - { - SetLength(0); - if ( !aReadable.IsEmpty() ) - { - SetLength(aReadable.Length()); - // first setting the length to |0| avoids copying characters only to be overwritten later - // in the case where the implementation decides to re-allocate - - const_iterator fromBegin, fromEnd; - iterator toBegin; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), BeginWriting(toBegin)); - } - } - -void -nsAString::do_AssignFromElementPtr( const char_type* aPtr ) - { - do_AssignFromReadable(nsDependentString(aPtr)); - } - -void -nsAString::do_AssignFromElementPtrLength( const char_type* aPtr, size_type aLength ) - { - do_AssignFromReadable(Substring(aPtr, aPtr+aLength)); - } - -void -nsAString::do_AssignFromElement( char_type aChar ) - { - UncheckedAssignFromReadable(Substring(&aChar, &aChar+1)); - } - - - - // - // |Append()| - // - -void -nsAString::do_AppendFromReadable( const self_type& aReadable ) - { - if ( !aReadable.IsDependentOn(*this) ) - UncheckedAppendFromReadable(aReadable); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedAppendFromReadable(Substring(buffer, buffer + length)); - delete[] buffer; - } - // else assert? - } - } - -void -nsAString::UncheckedAppendFromReadable( const self_type& aReadable) - { - size_type oldLength = this->Length(); - SetLength(oldLength + aReadable.Length()); - - const_iterator fromBegin, fromEnd; - iterator toBegin; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), BeginWriting(toBegin).advance( PRInt32(oldLength) ) ); - } - - -void -nsAString::do_AppendFromElementPtr( const char_type* aPtr ) - { - do_AppendFromReadable(nsDependentString(aPtr)); - } - -void -nsAString::do_AppendFromElementPtrLength( const char_type* aPtr, size_type aLength ) - { - do_AppendFromReadable(Substring(aPtr, aPtr+aLength)); - } - -void -nsAString::do_AppendFromElement( char_type aChar ) - { - UncheckedAppendFromReadable(Substring(&aChar, &aChar + 1)); - } - - - - // - // |Insert()| - // - -void -nsAString::do_InsertFromReadable( const self_type& aReadable, index_type atPosition ) - { - if ( !aReadable.IsDependentOn(*this) ) - UncheckedInsertFromReadable(aReadable, atPosition); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedInsertFromReadable(Substring(buffer, buffer + length), atPosition); - delete[] buffer; - } - // else assert - } - } - -void -nsAString::UncheckedInsertFromReadable( const self_type& aReadable, index_type atPosition ) - { - size_type oldLength = this->Length(); - SetLength(oldLength + aReadable.Length()); - - const_iterator fromBegin, fromEnd; - iterator toBegin; - if ( atPosition < oldLength ) - copy_string_backward(this->BeginReading(fromBegin).advance(PRInt32(atPosition)), this->BeginReading(fromEnd).advance(PRInt32(oldLength)), EndWriting(toBegin)); - else - atPosition = oldLength; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(atPosition))); - } - -void -nsAString::do_InsertFromElementPtr( const char_type* aPtr, index_type atPosition ) - { - do_InsertFromReadable(nsDependentString(aPtr), atPosition); - } - -void -nsAString::do_InsertFromElementPtrLength( const char_type* aPtr, index_type atPosition, size_type aLength ) - { - do_InsertFromReadable(Substring(aPtr, aPtr+aLength), atPosition); - } - -void -nsAString::do_InsertFromElement( char_type aChar, index_type atPosition ) - { - UncheckedInsertFromReadable(Substring(&aChar, &aChar+1), atPosition); - } - - - - // - // |Cut()| - // - -void -nsAString::Cut( index_type cutStart, size_type cutLength ) - { - size_type myLength = this->Length(); - cutLength = NS_MIN(cutLength, myLength-cutStart); - index_type cutEnd = cutStart + cutLength; - - if ( cutEnd < myLength ) - { - const_iterator fromBegin, fromEnd; - iterator toBegin; - copy_string(this->BeginReading(fromBegin).advance(PRInt32(cutEnd)), this->EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(cutStart))); - } - SetLength(myLength-cutLength); - } - - - - // - // |Replace()| - // - -void -nsAString::do_ReplaceFromReadable( index_type cutStart, size_type cutLength, const self_type& aReadable ) - { - if ( !aReadable.IsDependentOn(*this) ) - UncheckedReplaceFromReadable(cutStart, cutLength, aReadable); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedReplaceFromReadable(cutStart, cutLength, Substring(buffer, buffer + length)); - delete[] buffer; - } - // else assert? - } - } - -void -nsAString::UncheckedReplaceFromReadable( index_type cutStart, size_type cutLength, const self_type& aReplacement ) - { - size_type oldLength = this->Length(); - - cutStart = NS_MIN(cutStart, oldLength); - cutLength = NS_MIN(cutLength, oldLength-cutStart); - index_type cutEnd = cutStart + cutLength; - - size_type replacementLength = aReplacement.Length(); - index_type replacementEnd = cutStart + replacementLength; - - size_type newLength = oldLength - cutLength + replacementLength; - - const_iterator fromBegin, fromEnd; - iterator toBegin; - if ( cutLength > replacementLength ) - copy_string(this->BeginReading(fromBegin).advance(PRInt32(cutEnd)), this->EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(replacementEnd))); - SetLength(newLength); - if ( cutLength < replacementLength ) - copy_string_backward(this->BeginReading(fromBegin).advance(PRInt32(cutEnd)), this->BeginReading(fromEnd).advance(PRInt32(oldLength)), EndWriting(toBegin)); - - copy_string(aReplacement.BeginReading(fromBegin), aReplacement.EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(cutStart))); - } - - - -int -nsDefaultCStringComparator::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const - { - return nsCharTraits::compare(lhs, rhs, aLength); - } - -PRBool -nsDefaultCStringComparator::operator()( char_type lhs, char_type rhs ) const - { - return lhs - rhs; - } - -int -nsCaseInsensitiveCStringComparator::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const - { - PRInt32 result=PRInt32(PL_strncasecmp(lhs, rhs, aLength)); - //Egads. PL_strncasecmp is returning *very* negative numbers. - //Some folks expect -1,0,1, so let's temper its enthusiasm. - if (result<0) - result=-1; - return result; - } - -PRBool -nsCaseInsensitiveCStringComparator::operator()( char lhs, char rhs ) const - { - if (lhs == rhs) return 0; - - lhs = tolower(lhs); - rhs = tolower(rhs); - - return lhs - rhs; - } - -NS_COM -int -Compare( const nsACString& lhs, const nsACString& rhs, const nsCStringComparator& aComparator ) - { - typedef nsACString::size_type size_type; - - if ( &lhs == &rhs ) - return 0; - - size_type lLength = lhs.Length(); - size_type rLength = rhs.Length(); - size_type lengthToCompare = NS_MIN(lLength, rLength); - - nsACString::const_iterator leftIter, rightIter; - lhs.BeginReading(leftIter); - rhs.BeginReading(rightIter); - - for (;;) - { - size_type lengthAvailable = size_type( NS_MIN(leftIter.size_forward(), rightIter.size_forward()) ); - - if ( lengthAvailable > lengthToCompare ) - lengthAvailable = lengthToCompare; - - { - int result; - // Note: |result| should be declared in this |if| expression, but some compilers don't like that - if ( (result = aComparator(leftIter.get(), rightIter.get(), lengthAvailable)) != 0 ) - return result; - } - - if ( !(lengthToCompare -= lengthAvailable) ) - break; - - leftIter.advance( PRInt32(lengthAvailable) ); - rightIter.advance( PRInt32(lengthAvailable) ); - } - - if ( lLength < rLength ) - return -1; - else if ( rLength < lLength ) - return 1; - else - return 0; - } - -const nsACString::shared_buffer_handle_type* -nsACString::GetSharedBufferHandle() const - { - return 0; - } - -const nsACString::buffer_handle_type* -nsACString::GetFlatBufferHandle() const - { - return GetSharedBufferHandle(); - } - -const nsACString::buffer_handle_type* -nsACString::GetBufferHandle() const - { - return GetSharedBufferHandle(); - } - -PRUint32 -nsACString::GetImplementationFlags() const - { - return 0; - } - - -PRBool -nsACString::IsVoid() const - { - return PR_FALSE; - } - -void -nsACString::SetIsVoid( PRBool ) - { - // |SetIsVoid| is ignored by default - } - -PRBool -nsACString::Equals( const char_type* rhs, const nsCStringComparator& aComparator ) const - { - return Equals(nsDependentCString(rhs), aComparator); - } - -nsACString::char_type -nsACString::First() const - { - NS_ASSERTION(Length()>0, "|First()| on an empty string"); - - const_iterator iter; - return *BeginReading(iter); - } - -nsACString::char_type -nsACString::Last() const - { - NS_ASSERTION(Length()>0, "|Last()| on an empty string"); - - const_iterator iter; - - if ( !IsEmpty() ) - { - EndReading(iter); - iter.advance(-1); - } - - return *iter; // Note: this has undefined results if |IsEmpty()| - } - -nsACString::size_type -nsACString::CountChar( char_type c ) const - { - /* - re-write this to use a counting sink - */ - - size_type result = 0; - size_type lengthToExamine = Length(); - - const_iterator iter; - for ( BeginReading(iter); ; ) - { - PRInt32 lengthToExamineInThisFragment = iter.size_forward(); - const char_type* fromBegin = iter.get(); - result += size_type(NS_COUNT(fromBegin, fromBegin+lengthToExamineInThisFragment, c)); - if ( !(lengthToExamine -= lengthToExamineInThisFragment) ) - return result; - iter.advance(lengthToExamineInThisFragment); - } - // never reached; quiets warnings - return 0; - } - -PRInt32 -nsACString::FindChar( char_type aChar, PRUint32 aOffset ) const - { - const_iterator iter, done_searching; - BeginReading(iter).advance( PRInt32(aOffset) ); - EndReading(done_searching); - - size_type lengthSearched = 0; - while ( iter != done_searching ) - { - PRInt32 fragmentLength = iter.size_forward(); - const char_type* charFoundAt = nsCharTraits::find(iter.get(), fragmentLength, aChar); - if ( charFoundAt ) - return lengthSearched + (charFoundAt-iter.get()) + aOffset; - - lengthSearched += fragmentLength; - iter.advance(fragmentLength); - } - - return -1; - } - -PRBool -nsACString::IsDependentOn( const self_type& aString ) const - { - const_fragment_type f1; - const char_type* s1 = GetReadableFragment(f1, kFirstFragment); - while ( s1 ) - { - const_fragment_type f2; - const char_type* s2 = aString.GetReadableFragment(f2, kFirstFragment); - while ( s2 ) - { - // if it _isn't_ the case that - // one fragment starts after the other ends, - // or ends before the other starts, - // then, they conflict: - // !(f2.mStart>=f1.mEnd || f2.mEnd<=f1.mStart) - // - // Simplified, that gives us: - if ( f2.mStart < f1.mEnd && f2.mEnd > f1.mStart ) - return PR_TRUE; - - s2 = aString.GetReadableFragment(f2, kNextFragment); - } - s1 = GetReadableFragment(f1, kNextFragment); - } - return PR_FALSE; - } - - // - // |Assign()| - // - -void -nsACString::do_AssignFromReadable( const self_type& aReadable ) - /* - ...we need to check whether the string that's being assigned into |this| somehow references |this|. - E.g., - - ... writable& w ... - ... readable& r ... - - w = r + w; - - In this example, you can see that unless the characters promised by |w| in |r+w| are resolved before - anything starts getting copied into |w|, there will be trouble. They will be overritten by the contents - of |r| before being retrieved to be appended. - - We could have a really tricky solution where we tell the promise to resolve _just_ the data promised - by |this|, but this should be a rare case, since clients with more local knowledge will know that, e.g., - in the case above, |Insert| could have special behavior with significantly better performance. Since - it's a rare case anyway, we should just do the simplest thing that could possibly work, resolve the - entire promise. If we measure and this turns out to show up on performance radar, we then have the - option to fix either the callers or this mechanism. - */ - { - // self-assign is a no-op - if (this == &aReadable) - return; - - if ( !aReadable.IsDependentOn(*this) ) - UncheckedAssignFromReadable(aReadable); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - // Note: not exception safe. We need something to manage temporary buffers like this - - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedAssignFromReadable(Substring(buffer, buffer + length)); - delete[] buffer; - } - // else assert? - } - } - -void -nsACString::UncheckedAssignFromReadable( const self_type& aReadable ) - { - SetLength(0); - if ( !aReadable.IsEmpty() ) - { - SetLength(aReadable.Length()); - // first setting the length to |0| avoids copying characters only to be overwritten later - // in the case where the implementation decides to re-allocate - - const_iterator fromBegin, fromEnd; - iterator toBegin; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), BeginWriting(toBegin)); - } - } - -void -nsACString::do_AssignFromElementPtr( const char_type* aPtr ) - { - do_AssignFromReadable(nsDependentCString(aPtr)); - } - -void -nsACString::do_AssignFromElementPtrLength( const char_type* aPtr, size_type aLength ) - { - do_AssignFromReadable(Substring(aPtr, aPtr+aLength)); - } - -void -nsACString::do_AssignFromElement( char_type aChar ) - { - UncheckedAssignFromReadable(Substring(&aChar, &aChar+1)); - } - - - - // - // |Append()| - // - -void -nsACString::do_AppendFromReadable( const self_type& aReadable ) - { - if ( !aReadable.IsDependentOn(*this) ) - UncheckedAppendFromReadable(aReadable); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedAppendFromReadable(Substring(buffer, buffer + length)); - delete[] buffer; - } - // else assert? - } - } - -void -nsACString::UncheckedAppendFromReadable( const self_type& aReadable ) - { - size_type oldLength = this->Length(); - SetLength(oldLength + aReadable.Length()); - - const_iterator fromBegin, fromEnd; - iterator toBegin; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), BeginWriting(toBegin).advance( PRInt32(oldLength) ) ); - } - -void -nsACString::do_AppendFromElementPtr( const char_type* aPtr ) - { - do_AppendFromReadable(nsDependentCString(aPtr)); - } - -void -nsACString::do_AppendFromElementPtrLength( const char_type* aPtr, size_type aLength ) - { - do_AppendFromReadable(Substring(aPtr, aPtr+aLength)); - } - -void -nsACString::do_AppendFromElement( char_type aChar ) - { - UncheckedAppendFromReadable(Substring(&aChar, &aChar + 1)); - } - - - - // - // |Insert()| - // - -void -nsACString::do_InsertFromReadable( const self_type& aReadable, index_type atPosition ) - { - if ( !aReadable.IsDependentOn(*this) ) - UncheckedInsertFromReadable(aReadable, atPosition); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedInsertFromReadable(Substring(buffer, buffer + length), atPosition); - delete[] buffer; - } - // else assert - } - } - -void -nsACString::UncheckedInsertFromReadable( const self_type& aReadable, index_type atPosition ) - { - size_type oldLength = this->Length(); - SetLength(oldLength + aReadable.Length()); - - const_iterator fromBegin, fromEnd; - iterator toBegin; - if ( atPosition < oldLength ) - copy_string_backward(this->BeginReading(fromBegin).advance(PRInt32(atPosition)), this->BeginReading(fromEnd).advance(PRInt32(oldLength)), EndWriting(toBegin)); - else - atPosition = oldLength; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(atPosition))); - } - -void -nsACString::do_InsertFromElementPtr( const char_type* aPtr, index_type atPosition ) - { - do_InsertFromReadable(nsDependentCString(aPtr), atPosition); - } - -void -nsACString::do_InsertFromElementPtrLength( const char_type* aPtr, index_type atPosition, size_type aLength ) - { - do_InsertFromReadable(Substring(aPtr, aPtr+aLength), atPosition); - } - -void -nsACString::do_InsertFromElement( char_type aChar, index_type atPosition ) - { - UncheckedInsertFromReadable(Substring(&aChar, &aChar+1), atPosition); - } - - - - // - // |Cut()| - // - -void -nsACString::Cut( index_type cutStart, size_type cutLength ) - { - size_type myLength = this->Length(); - cutLength = NS_MIN(cutLength, myLength-cutStart); - index_type cutEnd = cutStart + cutLength; - - if ( cutEnd < myLength ) - { - const_iterator fromBegin, fromEnd; - iterator toBegin; - copy_string(this->BeginReading(fromBegin).advance(PRInt32(cutEnd)), this->EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(cutStart))); - } - SetLength(myLength-cutLength); - } - - - - // - // |Replace()| - // - -void -nsACString::do_ReplaceFromReadable( index_type cutStart, size_type cutLength, const self_type& aReadable ) - { - if ( !aReadable.IsDependentOn(*this) ) - UncheckedReplaceFromReadable(cutStart, cutLength, aReadable); - else - { - size_type length = aReadable.Length(); - char_type* buffer = new char_type[length]; - if ( buffer ) - { - const_iterator fromBegin, fromEnd; - char_type* toBegin = buffer; - copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - UncheckedReplaceFromReadable(cutStart, cutLength, Substring(buffer, buffer + length)); - delete[] buffer; - } - // else assert? - } - } - -void -nsACString::UncheckedReplaceFromReadable( index_type cutStart, size_type cutLength, const self_type& aReplacement ) - { - size_type oldLength = this->Length(); - - cutStart = NS_MIN(cutStart, oldLength); - cutLength = NS_MIN(cutLength, oldLength-cutStart); - index_type cutEnd = cutStart + cutLength; - - size_type replacementLength = aReplacement.Length(); - index_type replacementEnd = cutStart + replacementLength; - - size_type newLength = oldLength - cutLength + replacementLength; - - const_iterator fromBegin, fromEnd; - iterator toBegin; - if ( cutLength > replacementLength ) - copy_string(this->BeginReading(fromBegin).advance(PRInt32(cutEnd)), this->EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(replacementEnd))); - SetLength(newLength); - if ( cutLength < replacementLength ) - copy_string_backward(this->BeginReading(fromBegin).advance(PRInt32(cutEnd)), this->BeginReading(fromEnd).advance(PRInt32(oldLength)), EndWriting(toBegin)); - - copy_string(aReplacement.BeginReading(fromBegin), aReplacement.EndReading(fromEnd), BeginWriting(toBegin).advance(PRInt32(cutStart))); - } - +#include "nsObsoleteAString.h" +#include "nsString.h" + + // define nsAString +#include "string-template-def-unichar.h" +#include "nsTAString.cpp" +#include "string-template-undef.h" + + // define nsACString +#include "string-template-def-char.h" +#include "nsTAString.cpp" +#include "string-template-undef.h" diff --git a/mozilla/xpcom/string/src/nsDependentConcatenation.cpp b/mozilla/xpcom/string/src/nsDependentConcatenation.cpp deleted file mode 100644 index 3a9fd92dfb3..00000000000 --- a/mozilla/xpcom/string/src/nsDependentConcatenation.cpp +++ /dev/null @@ -1,190 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * - * The Initial Developer of the Original Code is Netscape - * Communications. Portions created by Netscape Communications are - * Copyright (C) 2001 by Netscape Communications. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - */ - -//-------1---------2---------3---------4---------5---------6---------7---------8 - -#include "nsAString.h" - // remember, no one should include "nsDependentConcatenation.h" themselves - // one always gets it through "nsAString.h" - -#ifndef nsDependentConcatenation_h___ -#include "nsDependentConcatenation.h" -#endif - - -PRUint32 -nsDependentConcatenation::Length() const - { - return mStrings[kFirstString]->Length() + mStrings[kLastString]->Length(); - } - -#if 0 -PRBool -nsDependentConcatenation::PromisesExactly( const abstract_string_type& aString ) const - { - // Not really like this, test for the empty string, etc - return mStrings[kFirstString] == &aString && !mStrings[kLastString] || !mStrings[kFirstString] && mStrings[kLastString] == &aString; - } -#endif - -const nsDependentConcatenation::char_type* -nsDependentConcatenation::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aPosition ) const - { - int whichString; - - // based on the request, pick which string we will forward the |GetReadableFragment()| call into - - switch ( aRequest ) - { - case kPrevFragment: - case kNextFragment: - whichString = GetCurrentStringFromFragment(aFragment); - break; - - case kFirstFragment: - whichString = SetFirstStringInFragment(aFragment); - break; - - case kLastFragment: - whichString = SetLastStringInFragment(aFragment); - break; - - case kFragmentAt: - PRUint32 leftLength = mStrings[kFirstString]->Length(); - if ( aPosition < leftLength ) - whichString = SetFirstStringInFragment(aFragment); - else - { - whichString = SetLastStringInFragment(aFragment); - aPosition -= leftLength; - } - break; - - } - - const char_type* result; - PRBool done; - do - { - done = PR_TRUE; - result = mStrings[whichString]->GetReadableFragment(aFragment, aRequest, aPosition); - - if ( !result ) - { - done = PR_FALSE; - if ( aRequest == kNextFragment && whichString == kFirstString ) - { - aRequest = kFirstFragment; - whichString = SetLastStringInFragment(aFragment); - } - else if ( aRequest == kPrevFragment && whichString == kLastString ) - { - aRequest = kLastFragment; - whichString = SetFirstStringInFragment(aFragment); - } - else - done = PR_TRUE; - } - } - while ( !done ); - return result; - } - - -PRUint32 -nsDependentCConcatenation::Length() const - { - return mStrings[kFirstString]->Length() + mStrings[kLastString]->Length(); - } - -#if 0 -PRBool -nsDependentCConcatenation::PromisesExactly( const abstract_string_type& aString ) const - { - // Not really like this, test for the empty string, etc - return mStrings[kFirstString] == &aString && !mStrings[kLastString] || !mStrings[kFirstString] && mStrings[kLastString] == &aString; - } -#endif - -const nsDependentCConcatenation::char_type* -nsDependentCConcatenation::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aPosition ) const - { - int whichString; - - // based on the request, pick which string we will forward the |GetReadableFragment()| call into - - switch ( aRequest ) - { - case kPrevFragment: - case kNextFragment: - whichString = GetCurrentStringFromFragment(aFragment); - break; - - case kFirstFragment: - whichString = SetFirstStringInFragment(aFragment); - break; - - case kLastFragment: - whichString = SetLastStringInFragment(aFragment); - break; - - case kFragmentAt: - PRUint32 leftLength = mStrings[kFirstString]->Length(); - if ( aPosition < leftLength ) - whichString = SetFirstStringInFragment(aFragment); - else - { - whichString = SetLastStringInFragment(aFragment); - aPosition -= leftLength; - } - break; - - } - - const char_type* result; - PRBool done; - do - { - done = PR_TRUE; - result = mStrings[whichString]->GetReadableFragment(aFragment, aRequest, aPosition); - - if ( !result ) - { - done = PR_FALSE; - if ( aRequest == kNextFragment && whichString == kFirstString ) - { - aRequest = kFirstFragment; - whichString = SetLastStringInFragment(aFragment); - } - else if ( aRequest == kPrevFragment && whichString == kLastString ) - { - aRequest = kLastFragment; - whichString = SetFirstStringInFragment(aFragment); - } - else - done = PR_TRUE; - } - } - while ( !done ); - return result; - } diff --git a/mozilla/xpcom/string/src/nsDependentSubstring.cpp b/mozilla/xpcom/string/src/nsDependentSubstring.cpp index 43935ef8eaf..971defc8be7 100644 --- a/mozilla/xpcom/string/src/nsDependentSubstring.cpp +++ b/mozilla/xpcom/string/src/nsDependentSubstring.cpp @@ -1,186 +1,50 @@ /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. - * - * The Initial Developer of the Original Code is Netscape - * Communications. Portions created by Netscape Communications are - * Copyright (C) 2001 by Netscape Communications. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - */ + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ #include "nsDependentSubstring.h" +#include "nsAlgorithm.h" -nsDependentSubstring::nsDependentSubstring( const abstract_string_type& aString, PRUint32 aStartPos, PRUint32 aLength ) - : mString(aString), - mStartPos( NS_MIN(aStartPos, aString.Length()) ), - mLength( NS_MIN(aLength, aString.Length()-mStartPos) ) - { - // nothing else to do here - } + // define nsDependentSubstring +#include "string-template-def-unichar.h" +#include "nsTDependentSubstring.cpp" +#include "string-template-undef.h" -nsDependentSubstring::nsDependentSubstring( const const_iterator& aStart, const const_iterator& aEnd ) - : mString(aStart.string()) - { - const_iterator zeroPoint; - mString.BeginReading(zeroPoint); - mStartPos = Distance(zeroPoint, aStart); - mLength = Distance(aStart, aEnd); - } - -PRUint32 -nsDependentSubstring::Length() const - { - return mLength; - } - -const nsDependentSubstring::char_type* -nsDependentSubstring::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aPosition ) const - { - // Offset any request for a specific position (First, Last, At) by our - // substrings startpos within the owning string - - if ( aRequest == kFirstFragment ) - { - aPosition = mStartPos; - aRequest = kFragmentAt; - } - else if ( aRequest == kLastFragment ) - { - aPosition = mStartPos + mLength; - aRequest = kFragmentAt; - } - else if ( aRequest == kFragmentAt ) - aPosition += mStartPos; - - // requests for |kNextFragment| or |kPrevFragment| are just relayed down into the string we're slicing - - const char_type* position_ptr = mString.GetReadableFragment(aFragment, aRequest, aPosition); - - // If |GetReadableFragment| returns |0|, then we are off the string, the contents of the - // fragment are garbage. - - // Therefore, only need to fix up the fragment boundaries when |position_ptr| is not null - if ( position_ptr ) - { - // if there's more physical data in the returned fragment than I logically have left... - size_t logical_size_backward = aPosition - mStartPos; - if ( size_t(position_ptr - aFragment.mStart) > logical_size_backward ) - aFragment.mStart = position_ptr - logical_size_backward; - - size_t logical_size_forward = mLength - logical_size_backward; - if ( size_t(aFragment.mEnd - position_ptr) > logical_size_forward ) - aFragment.mEnd = position_ptr + logical_size_forward; - } - - return position_ptr; - } - -nsDependentCSubstring::nsDependentCSubstring( const abstract_string_type& aString, PRUint32 aStartPos, PRUint32 aLength ) - : mString(aString), - mStartPos( NS_MIN(aStartPos, aString.Length()) ), - mLength( NS_MIN(aLength, aString.Length()-mStartPos) ) - { - // nothing else to do here - } - -nsDependentCSubstring::nsDependentCSubstring( const const_iterator& aStart, const const_iterator& aEnd ) - : mString(aStart.string()) - { - const_iterator zeroPoint; - mString.BeginReading(zeroPoint); - mStartPos = Distance(zeroPoint, aStart); - mLength = Distance(aStart, aEnd); - } - -PRUint32 -nsDependentCSubstring::Length() const - { - return mLength; - } - -const nsDependentCSubstring::char_type* -nsDependentCSubstring::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aPosition ) const - { - // Offset any request for a specific position (First, Last, At) by our - // substrings startpos within the owning string - - if ( aRequest == kFirstFragment ) - { - aPosition = mStartPos; - aRequest = kFragmentAt; - } - else if ( aRequest == kLastFragment ) - { - aPosition = mStartPos + mLength; - aRequest = kFragmentAt; - } - else if ( aRequest == kFragmentAt ) - aPosition += mStartPos; - - // requests for |kNextFragment| or |kPrevFragment| are just relayed down into the string we're slicing - - const char_type* position_ptr = mString.GetReadableFragment(aFragment, aRequest, aPosition); - - // If |GetReadableFragment| returns |0|, then we are off the string, the contents of the - // fragment are garbage. - - // Therefore, only need to fix up the fragment boundaries when |position_ptr| is not null - if ( position_ptr ) - { - // if there's more physical data in the returned fragment than I logically have left... - size_t logical_size_backward = aPosition - mStartPos; - if ( size_t(position_ptr - aFragment.mStart) > logical_size_backward ) - aFragment.mStart = position_ptr - logical_size_backward; - - size_t logical_size_forward = mLength - logical_size_backward; - if ( size_t(aFragment.mEnd - position_ptr) > logical_size_forward ) - aFragment.mEnd = position_ptr + logical_size_forward; - } - - return position_ptr; - } - -void -nsDependentSingleFragmentSubstring::Rebind( const char_type* aStartPtr, const char_type* aEndPtr ) - { - NS_ASSERTION(aStartPtr && aEndPtr, "nsDependentSingleFragmentString must wrap a non-NULL buffer"); - mHandle.DataStart(aStartPtr); - mHandle.DataEnd(aEndPtr); - } - -void -nsDependentSingleFragmentSubstring::Rebind( const abstract_single_fragment_type& aString, const PRUint32 aStartPos, const PRUint32 aLength ) - { - const_char_iterator iter; - mHandle.DataStart(aString.BeginReading(iter) + NS_MIN(aStartPos, aString.Length())); - mHandle.DataEnd( NS_MIN(mHandle.DataStart() + aLength, aString.EndReading(iter)) ); - } - -void -nsDependentSingleFragmentCSubstring::Rebind( const char_type* aStartPtr, const char_type* aEndPtr ) - { - NS_ASSERTION(aStartPtr && aEndPtr, "nsDependentSingleFragmentCString must wrap a non-NULL buffer"); - mHandle.DataStart(aStartPtr); - mHandle.DataEnd(aEndPtr); - } - -void -nsDependentSingleFragmentCSubstring::Rebind( const abstract_single_fragment_type& aString, const PRUint32 aStartPos, const PRUint32 aLength ) - { - const_char_iterator iter; - mHandle.DataStart(aString.BeginReading(iter) + NS_MIN(aStartPos, aString.Length())); - mHandle.DataEnd( NS_MIN(mHandle.DataStart() + aLength, aString.EndReading(iter)) ); - } + // define nsDependentCSubstring +#include "string-template-def-char.h" +#include "nsTDependentSubstring.cpp" +#include "string-template-undef.h" diff --git a/mozilla/xpcom/string/src/nsFragmentedString.cpp b/mozilla/xpcom/string/src/nsFragmentedString.cpp deleted file mode 100755 index 38e1d04bd42..00000000000 --- a/mozilla/xpcom/string/src/nsFragmentedString.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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 XPCOM. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Original Author: - * Scott Collins - * - * Contributor(s): - */ - -#include "nsFragmentedString.h" - - -const nsFragmentedString::char_type* -nsFragmentedString::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - const nsSharedBufferList::Buffer* buffer = 0; - switch ( aRequest ) - { - case kPrevFragment: - buffer = NS_STATIC_CAST(const nsSharedBufferList::Buffer*, aFragment.mFragmentIdentifier)->mPrev; - break; - - case kFirstFragment: - buffer = mBufferList.GetFirstBuffer(); - break; - - case kLastFragment: - buffer = mBufferList.GetLastBuffer(); - break; - - case kNextFragment: - buffer = NS_STATIC_CAST(const nsSharedBufferList::Buffer*, aFragment.mFragmentIdentifier)->mNext; - break; - - case kFragmentAt: - // ...work... - break; - } - - if ( buffer ) - { - aFragment.mStart = buffer->DataStart(); - aFragment.mEnd = buffer->DataEnd(); - aFragment.mFragmentIdentifier = buffer; - return aFragment.mStart + aOffset; - } - - return 0; - } - - -nsFragmentedString::char_type* -nsFragmentedString::GetWritableFragment( fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) - { - nsSharedBufferList::Buffer* buffer = 0; - switch ( aRequest ) - { - case kPrevFragment: - buffer = NS_STATIC_CAST(nsSharedBufferList::Buffer*, aFragment.mFragmentIdentifier)->mPrev; - break; - - case kFirstFragment: - buffer = mBufferList.GetFirstBuffer(); - break; - - case kLastFragment: - buffer = mBufferList.GetLastBuffer(); - break; - - case kNextFragment: - buffer = NS_STATIC_CAST(nsSharedBufferList::Buffer*, aFragment.mFragmentIdentifier)->mNext; - break; - - case kFragmentAt: - // ...work... - break; - } - - if ( buffer ) - { - aFragment.mStart = buffer->DataStart(); - aFragment.mEnd = buffer->DataEnd(); - aFragment.mFragmentIdentifier = buffer; - return aFragment.mStart + aOffset; - } - - return 0; - } - - /** - * ... - */ -PRUint32 -nsFragmentedString::Length() const - { - return PRUint32(mBufferList.GetDataLength()); - } - - /** - * |SetLength| - */ -void -nsFragmentedString::SetLength( PRUint32 aNewLength ) - { - // according to the current interpretation of |SetLength|, - // cut off characters from the end, or else add unitialized space to fill - - if ( aNewLength < PRUint32(mBufferList.GetDataLength()) ) - { -// if ( aNewLength ) - mBufferList.DiscardSuffix(mBufferList.GetDataLength()-aNewLength); -// else -// mBufferList.DestroyBuffers(); - } - -// temporarily... eliminate as soon as our munging routines don't need this form of |SetLength| - else if ( aNewLength > PRUint32(mBufferList.GetDataLength()) ) - { - size_t empty_space_to_add = aNewLength - mBufferList.GetDataLength(); - nsSharedBufferList::Buffer* new_buffer = nsSharedBufferList::NewSingleAllocationBuffer(0, 0, empty_space_to_add); - new_buffer->DataEnd(new_buffer->DataStart()+empty_space_to_add); - mBufferList.LinkBuffer(mBufferList.GetLastBuffer(), new_buffer, 0); - } - } - - -#if 0 - /** - * |SetCapacity|. - * - * If a client tries to increase the capacity of multi-fragment string, perhaps a single - * empty fragment of the appropriate size should be appended. - */ -void -nsFragmentedString::SetCapacity( PRUint32 aNewCapacity ) - { - if ( !aNewCapacity ) - { - // |SetCapacity(0)| is special and means ``release all storage''. - } - else if ( aNewCapacity > ... ) - { - - } - } -#endif diff --git a/mozilla/xpcom/string/src/nsObsoleteAStringThunk.cpp b/mozilla/xpcom/string/src/nsObsoleteAStringThunk.cpp new file mode 100644 index 00000000000..82be222e1a0 --- /dev/null +++ b/mozilla/xpcom/string/src/nsObsoleteAStringThunk.cpp @@ -0,0 +1,78 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsObsoleteAString.h" +#include "nsString.h" + + + // define nsObsoleteAStringThunk +#include "string-template-def-unichar.h" +#include "nsTObsoleteAStringThunk.cpp" +#include "string-template-undef.h" + + + // define nsObsoleteACStringThunk +#include "string-template-def-char.h" +#include "nsTObsoleteAStringThunk.cpp" +#include "string-template-undef.h" + + +/** + * get pointers to canonical vtables... + */ + +inline void *operator new(size_t size, const void *loc) { return (void *) loc; } + +static const void * +get_nsObsoleteAStringThunk_vptr() +{ + const void *result; + new (&result) nsObsoleteAStringThunk(); + return result; +} + +static const void * +get_nsObsoleteACStringThunk_vptr() +{ + const void *result; + new (&result) nsObsoleteACStringThunk(); + return result; +} + +const void *nsObsoleteAString::sCanonicalVTable = get_nsObsoleteAStringThunk_vptr(); +const void *nsObsoleteACString::sCanonicalVTable = get_nsObsoleteACStringThunk_vptr(); diff --git a/mozilla/xpcom/string/src/nsPromiseFlatString.cpp b/mozilla/xpcom/string/src/nsPromiseFlatString.cpp index f16395f65b2..aab4f209180 100644 --- a/mozilla/xpcom/string/src/nsPromiseFlatString.cpp +++ b/mozilla/xpcom/string/src/nsPromiseFlatString.cpp @@ -1,139 +1,49 @@ /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. - * - * The Initial Developer of the Original Code is Netscape - * Communications. Portions created by Netscape Communications are - * Copyright (C) 2001 by Netscape Communications. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - */ + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ #include "nsPromiseFlatString.h" + // define nsPromiseFlatString +#include "string-template-def-unichar.h" +#include "nsTPromiseFlatString.cpp" +#include "string-template-undef.h" -nsPromiseFlatString::nsPromiseFlatString( const self_type& aOther ) - : mFlattenedString(aOther.mFlattenedString) - { - if ( aOther.mPromisedString == &aOther.mFlattenedString ) - mPromisedString = &mFlattenedString; - else - mPromisedString = aOther.mPromisedString; - } - -nsPromiseFlatString::nsPromiseFlatString( const abstract_string_type& aString ) - { - if ( aString.GetFlatBufferHandle() ) - mPromisedString = NS_STATIC_CAST(const nsAFlatString*, &aString); - else - { - mFlattenedString = aString; // flatten |aString| - mPromisedString = &mFlattenedString; - } - } - -const nsPromiseFlatString::buffer_handle_type* -nsPromiseFlatString::GetFlatBufferHandle() const - { - return mPromisedString->GetFlatBufferHandle(); - } - -const nsPromiseFlatString::buffer_handle_type* -nsPromiseFlatString::GetBufferHandle() const - { - return mPromisedString->GetBufferHandle(); - } - -const nsPromiseFlatString::shared_buffer_handle_type* -nsPromiseFlatString::GetSharedBufferHandle() const - { - return mPromisedString->GetSharedBufferHandle(); - } - -const nsPromiseFlatString::char_type* -nsPromiseFlatString::get() const - { - return mPromisedString->get(); - } - -PRUint32 -nsPromiseFlatString::Length() const - { - return mPromisedString->Length(); - } - -const nsPromiseFlatString::char_type* -nsPromiseFlatString::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - return mPromisedString->GetReadableFragment(aFragment, aRequest, aOffset); - } - - - -nsPromiseFlatCString::nsPromiseFlatCString( const self_type& aOther ) - : mFlattenedString(aOther.mFlattenedString) - { - if ( aOther.mPromisedString == &aOther.mFlattenedString ) - mPromisedString = &mFlattenedString; - else - mPromisedString = aOther.mPromisedString; - } - -nsPromiseFlatCString::nsPromiseFlatCString( const abstract_string_type& aString ) - { - if ( aString.GetFlatBufferHandle() ) - mPromisedString = NS_STATIC_CAST(const nsAFlatCString*, &aString); - else - { - mFlattenedString = aString; // flatten |aString| - mPromisedString = &mFlattenedString; - } - } - -const nsPromiseFlatCString::buffer_handle_type* -nsPromiseFlatCString::GetFlatBufferHandle() const - { - return mPromisedString->GetFlatBufferHandle(); - } - -const nsPromiseFlatCString::buffer_handle_type* -nsPromiseFlatCString::GetBufferHandle() const - { - return mPromisedString->GetBufferHandle(); - } - -const nsPromiseFlatCString::shared_buffer_handle_type* -nsPromiseFlatCString::GetSharedBufferHandle() const - { - return mPromisedString->GetSharedBufferHandle(); - } - -const nsPromiseFlatCString::char_type* -nsPromiseFlatCString::get() const - { - return mPromisedString->get(); - } - -PRUint32 -nsPromiseFlatCString::Length() const - { - return mPromisedString->Length(); - } - -const nsPromiseFlatCString::char_type* -nsPromiseFlatCString::GetReadableFragment( const_fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - return mPromisedString->GetReadableFragment(aFragment, aRequest, aOffset); - } + // define nsPromiseFlatCString +#include "string-template-def-char.h" +#include "nsTPromiseFlatString.cpp" +#include "string-template-undef.h" diff --git a/mozilla/xpcom/string/src/nsSharableString.cpp b/mozilla/xpcom/string/src/nsSharableString.cpp deleted file mode 100644 index c0e55174294..00000000000 --- a/mozilla/xpcom/string/src/nsSharableString.cpp +++ /dev/null @@ -1,356 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * - * The Initial Developer of the Original Code is Netscape - * Communications. Portions created by Netscape Communications are - * Copyright (C) 2001 by Netscape Communications. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - */ - -//-------1---------2---------3---------4---------5---------6---------7---------8 - -#include "nsSharableString.h" -// #include "nsBufferHandleUtils.h" -#include "nsDependentSubstring.h" - -void -nsSharableString::SetCapacity( size_type aNewCapacity ) - { - // Note: Capacity numbers do not include room for a terminating - // NULL. However, StorageLength numbers do, since this string type - // requires a terminating null so we include it in the storage of - // our buffer handle. - - if ( aNewCapacity ) - { - // SetCapacity wouldn't be called if the caller didn't intend to - // mutate the string. - // - // If the buffer is shared, we want to allocate a new buffer - // unconditionally. If we do not, and the caller plans to do an - // assign and a series of appends, the assign will lead to a - // small buffer that will then be grown in steps, defeating the - // point of |SetCapacity|. - // - // Since the caller is planning to mutate the string, we don't - // want to make the new buffer any larger than the requested - // capacity since that would be a waste of space. This means - // that, when the string is shared, we may want to give a string - // a buffer shorter than its current length. - // - // Since sharing should be transparent to the caller, we should - // therefore *unconditionally* truncate the current length of - // the string to the requested capacity. - - if ( !mBuffer->IsMutable() ) - { - if ( aNewCapacity > mBuffer->DataLength() ) - mBuffer = NS_AllocateContiguousHandleWithData(mBuffer.get(), - *this, PRUint32(aNewCapacity - mBuffer->DataLength() + 1)); - else - mBuffer = NS_AllocateContiguousHandleWithData(mBuffer.get(), - Substring(*this, 0, aNewCapacity), PRUint32(1)); - } - else - { - if ( aNewCapacity >= mBuffer->StorageLength() ) - { - // This is where we implement the "exact size on assign, - // double on fault" allocation strategy. We don't do it - // exactly since we don't double on a fault when the - // buffer is shared, but we'll double soon enough. - size_type doubledCapacity = (mBuffer->StorageLength() - 1) * 2; - if ( doubledCapacity > aNewCapacity ) - aNewCapacity = doubledCapacity; - - // XXX We should be able to use |realloc| under certain - // conditions (contiguous buffer handle, not - // kIsImmutable (,etc.)?) - mBuffer = NS_AllocateContiguousHandleWithData(mBuffer.get(), - *this, PRUint32(aNewCapacity - mBuffer->DataLength() + 1)); - } - else if ( aNewCapacity < mBuffer->DataLength() ) - { - // Ensure we always have the same effect on the length - // whether or not the buffer is shared, as mentioned - // above. - mBuffer->DataEnd(mBuffer->DataStart() + aNewCapacity); - *mBuffer->DataEnd() = char_type('\0'); - } - } - } - else - mBuffer = GetSharedEmptyBufferHandle(); - } - -void -nsSharableString::SetLength( size_type aNewLength ) - { - if ( !mBuffer->IsMutable() || aNewLength >= mBuffer->StorageLength() ) - SetCapacity(aNewLength); - mBuffer->DataEnd( mBuffer->DataStart() + aNewLength ); - - // This is needed for |Truncate| callers and also for some callers - // (such as nsAString::do_AppendFromReadable) that are manipulating - // the internals of the string. It also makes sense to do this - // since this class implements |nsAFlatString|, so the buffer must - // always be null-terminated at its length. Callers using writing - // iterators can't be expected to null-terminate themselves since - // they don't know if they're dealing with a string that has a - // buffer big enough for null-termination. - *mBuffer->DataEnd() = char_type(0); - } - -void -nsSharableString::do_AssignFromReadable( const abstract_string_type& aReadable ) - { - const shared_buffer_handle_type* handle = aReadable.GetSharedBufferHandle(); - if ( !handle ) - { - // null-check |mBuffer.get()| here only for the constructor - // taking |const abstract_string_type&| - if ( mBuffer.get() && mBuffer->IsMutable() && - mBuffer->StorageLength() > aReadable.Length() && - !aReadable.IsDependentOn(*this) ) - { - abstract_string_type::const_iterator fromBegin, fromEnd; - char_type *storage_start = mBuffer->DataStart(); - *copy_string( aReadable.BeginReading(fromBegin), - aReadable.EndReading(fromEnd), - storage_start ) = char_type(0); - mBuffer->DataEnd(storage_start); // modified by |copy_string| - return; // don't want to assign to |mBuffer| below - } - else - handle = NS_AllocateContiguousHandleWithData(handle, - aReadable, PRUint32(1)); - } - mBuffer = handle; - } - -const nsSharableString::shared_buffer_handle_type* -nsSharableString::GetSharedBufferHandle() const - { - return mBuffer.get(); - } - -void -nsSharableString::Adopt( char_type* aNewValue ) - { - size_type length = nsCharTraits::length(aNewValue); - mBuffer = new nsSharedBufferHandle(aNewValue, aNewValue+length, - length, PR_FALSE); - } - -/* static */ -nsSharableString::shared_buffer_handle_type* -nsSharableString::GetSharedEmptyBufferHandle() - { - static shared_buffer_handle_type* sBufferHandle = nsnull; - static char_type null_char = char_type(0); - - if (!sBufferHandle) { - sBufferHandle = new nsNonDestructingSharedBufferHandle(&null_char, &null_char, 1); - sBufferHandle->AcquireReference(); // To avoid the |Destroy| - // mechanism unless threads - // race to set the refcount, in - // which case we'll pull the - // same trick in |Destroy|. - } - return sBufferHandle; - } - -// The need to override GetWritableFragment may be temporary, depending -// on the protocol we choose for callers who want to mutate strings -// using iterators. See -// -nsSharableString::char_type* -nsSharableString::GetWritableFragment( fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) - { - // This makes writing iterators safe to use, but it imposes a - // double-malloc performance penalty on users who intend to modify - // the length of the string but call |BeginWriting| before they call - // |SetLength|. - if ( mBuffer->IsMutable() ) - SetCapacity( mBuffer->DataLength() ); - return nsAFlatString::GetWritableFragment( aFragment, aRequest, aOffset ); - } - - -void -nsSharableCString::SetCapacity( size_type aNewCapacity ) - { - // Note: Capacity numbers do not include room for a terminating - // NULL. However, StorageLength numbers do, since this string type - // requires a terminating null so we include it in the storage of - // our buffer handle. - - if ( aNewCapacity ) - { - // SetCapacity wouldn't be called if the caller didn't intend to - // mutate the string. - // - // If the buffer is shared, we want to allocate a new buffer - // unconditionally. If we do not, and the caller plans to do an - // assign and a series of appends, the assign will lead to a - // small buffer that will then be grown in steps, defeating the - // point of |SetCapacity|. - // - // Since the caller is planning to mutate the string, we don't - // want to make the new buffer any larger than the requested - // capacity since that would be a waste of space. This means - // that, when the string is shared, we may want to give a string - // a buffer shorter than its current length. - // - // Since sharing should be transparent to the caller, we should - // therefore *unconditionally* truncate the current length of - // the string to the requested capacity. - - if ( !mBuffer->IsMutable() ) - { - if ( aNewCapacity > mBuffer->DataLength() ) - mBuffer = NS_AllocateContiguousHandleWithData(mBuffer.get(), - *this, PRUint32(aNewCapacity - mBuffer->DataLength() + 1)); - else - mBuffer = NS_AllocateContiguousHandleWithData(mBuffer.get(), - Substring(*this, 0, aNewCapacity), PRUint32(1)); - } - else - { - if ( aNewCapacity >= mBuffer->StorageLength() ) - { - // This is where we implement the "exact size on assign, - // double on fault" allocation strategy. We don't do it - // exactly since we don't double on a fault when the - // buffer is shared, but we'll double soon enough. - size_type doubledCapacity = (mBuffer->StorageLength() - 1) * 2; - if ( doubledCapacity > aNewCapacity ) - aNewCapacity = doubledCapacity; - - // XXX We should be able to use |realloc| under certain - // conditions (contiguous buffer handle, not - // kIsImmutable (,etc.)?) - mBuffer = NS_AllocateContiguousHandleWithData(mBuffer.get(), - *this, PRUint32(aNewCapacity - mBuffer->DataLength() + 1)); - } - else if ( aNewCapacity < mBuffer->DataLength() ) - { - // Ensure we always have the same effect on the length - // whether or not the buffer is shared, as mentioned - // above. - mBuffer->DataEnd(mBuffer->DataStart() + aNewCapacity); - *mBuffer->DataEnd() = char_type('\0'); - } - } - } - else - mBuffer = GetSharedEmptyBufferHandle(); - } - -void -nsSharableCString::SetLength( size_type aNewLength ) - { - if ( !mBuffer->IsMutable() || aNewLength >= mBuffer->StorageLength() ) - SetCapacity(aNewLength); - mBuffer->DataEnd( mBuffer->DataStart() + aNewLength ); - - // This is needed for |Truncate| callers and also for some callers - // (such as nsACString::do_AppendFromReadable) that are manipulating - // the internals of the string. It also makes sense to do this - // since this class implements |nsAFlatCString|, so the buffer must - // always be null-terminated at its length. Callers using writing - // iterators can't be expected to null-terminate themselves since - // they don't know if they're dealing with a string that has a - // buffer big enough for null-termination. - *mBuffer->DataEnd() = char_type(0); - } - -void -nsSharableCString::do_AssignFromReadable( const abstract_string_type& aReadable ) - { - const shared_buffer_handle_type* handle = aReadable.GetSharedBufferHandle(); - if ( !handle ) - { - // null-check |mBuffer.get()| here only for the constructor - // taking |const abstract_string_type&| - if ( mBuffer.get() && mBuffer->IsMutable() && - mBuffer->StorageLength() > aReadable.Length() && - !aReadable.IsDependentOn(*this) ) - { - abstract_string_type::const_iterator fromBegin, fromEnd; - char_type *storage_start = mBuffer->DataStart(); - *copy_string( aReadable.BeginReading(fromBegin), - aReadable.EndReading(fromEnd), - storage_start ) = char_type(0); - mBuffer->DataEnd(storage_start); // modified by |copy_string| - return; // don't want to assign to |mBuffer| below - } - else - handle = NS_AllocateContiguousHandleWithData(handle, - aReadable, PRUint32(1)); - } - mBuffer = handle; - } - -const nsSharableCString::shared_buffer_handle_type* -nsSharableCString::GetSharedBufferHandle() const - { - return mBuffer.get(); - } - -void -nsSharableCString::Adopt( char_type* aNewValue ) - { - size_type length = nsCharTraits::length(aNewValue); - mBuffer = new nsSharedBufferHandle(aNewValue, aNewValue+length, - length, PR_FALSE); - } - -/* static */ -nsSharableCString::shared_buffer_handle_type* -nsSharableCString::GetSharedEmptyBufferHandle() - { - static shared_buffer_handle_type* sBufferHandle = nsnull; - static char_type null_char = char_type(0); - - if (!sBufferHandle) { - sBufferHandle = new nsNonDestructingSharedBufferHandle(&null_char, &null_char, 1); - sBufferHandle->AcquireReference(); // To avoid the |Destroy| - // mechanism unless threads - // race to set the refcount, in - // which case we'll pull the - // same trick in |Destroy|. - } - return sBufferHandle; - } - -// The need to override GetWritableFragment may be temporary, depending -// on the protocol we choose for callers who want to mutate strings -// using iterators. See -// -nsSharableCString::char_type* -nsSharableCString::GetWritableFragment( fragment_type& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) - { - // This makes writing iterators safe to use, but it imposes a - // double-malloc performance penalty on users who intend to modify - // the length of the string but call |BeginWriting| before they call - // |SetLength|. - if ( mBuffer->IsMutable() ) - SetCapacity( mBuffer->DataLength() ); - return nsAFlatCString::GetWritableFragment( aFragment, aRequest, aOffset ); - } - diff --git a/mozilla/xpcom/string/src/nsSharedBufferList.cpp b/mozilla/xpcom/string/src/nsSharedBufferList.cpp deleted file mode 100755 index 684f75c36f2..00000000000 --- a/mozilla/xpcom/string/src/nsSharedBufferList.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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 strings. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - * - */ - -#include "nsSharedBufferList.h" - -#ifndef nsAlgorithm_h___ -#include "nsAlgorithm.h" - // for |copy_string| -#endif - - -ptrdiff_t -nsSharedBufferList::Position::Distance( const Position& aStart, const Position& aEnd ) - { - ptrdiff_t result = 0; - if ( aStart.mBuffer == aEnd.mBuffer ) - result = aEnd.mPosInBuffer - aStart.mPosInBuffer; - else - { - result = aStart.mBuffer->DataEnd() - aStart.mPosInBuffer; - for ( Buffer* b = aStart.mBuffer->mNext; b != aEnd.mBuffer; b = b->mNext ) - result += b->DataLength(); - result += aEnd.mPosInBuffer - aEnd.mBuffer->DataStart(); - } - - return result; - } - - -void -nsSharedBufferList::DestroyBuffers() - { - // destroy the entire list of buffers, without bothering to manage their links - Buffer* next_buffer; - for ( Buffer* cur_buffer=mFirstBuffer; cur_buffer; cur_buffer=next_buffer ) - { - next_buffer = cur_buffer->mNext; - operator delete(cur_buffer); - } - mFirstBuffer = mLastBuffer = 0; - mTotalDataLength = 0; - } - -nsSharedBufferList::~nsSharedBufferList() - { - DestroyBuffers(); - } - - - -void -nsSharedBufferList::LinkBuffer( Buffer* aPrevBuffer, Buffer* aNewBuffer, Buffer* aNextBuffer ) - { - NS_ASSERTION(aNewBuffer, "aNewBuffer"); - NS_ASSERTION(aPrevBuffer || mFirstBuffer == aNextBuffer, "aPrevBuffer || mFirstBuffer == aNextBuffer"); - NS_ASSERTION(!aPrevBuffer || aPrevBuffer->mNext == aNextBuffer, "!aPrevBuffer || aPrevBuffer->mNext == aNextBuffer"); - NS_ASSERTION(aNextBuffer || mLastBuffer == aPrevBuffer, "aNextBuffer || mLastBuffer == aPrevBuffer"); - NS_ASSERTION(!aNextBuffer || aNextBuffer->mPrev == aPrevBuffer, "!aNextBuffer || aNextBuffer->mPrev == aPrevBuffer"); - - if ( (aNewBuffer->mPrev = aPrevBuffer) ) - aPrevBuffer->mNext = aNewBuffer; - else - mFirstBuffer = aNewBuffer; - - if ( (aNewBuffer->mNext = aNextBuffer) ) - aNextBuffer->mPrev = aNewBuffer; - else - mLastBuffer = aNewBuffer; - - mTotalDataLength += aNewBuffer->DataLength(); - } - -void -nsSharedBufferList::SplitBuffer( const Position& aSplitPosition, SplitDisposition aSplitDirection ) - { - Buffer* bufferToSplit = aSplitPosition.mBuffer; - - NS_ASSERTION(bufferToSplit, "bufferToSplit"); - - Buffer::size_type splitOffset = - aSplitPosition.mPosInBuffer - bufferToSplit->DataStart(); - - NS_ASSERTION(aSplitPosition.mPosInBuffer >= bufferToSplit->DataStart() && - splitOffset <= bufferToSplit->DataLength(), - "|splitOffset| within buffer"); - - // if the caller specifically asked to split off the right side of - // the buffer-to-be-split or else if they asked for the minimum - // amount of work, and that turned out to be the right side... - ptrdiff_t savedLength = mTotalDataLength; - if ( aSplitDirection==kSplitCopyRightData || - ( aSplitDirection==kSplitCopyLeastData && - ((bufferToSplit->DataLength() >> 1) <= splitOffset) ) ) - { - // ...then allocate a new buffer initializing it by copying all the data _after_ the split in the source buffer (i.e., `split right') - Buffer* new_buffer = NewSingleAllocationBuffer(bufferToSplit->DataStart()+splitOffset, PRUint32(bufferToSplit->DataLength()-splitOffset)); - LinkBuffer(bufferToSplit, new_buffer, bufferToSplit->mNext); - bufferToSplit->DataEnd(aSplitPosition.mPosInBuffer); - } - else - { - // ...else move the data _before_ the split point (i.e., `split left') - Buffer* new_buffer = NewSingleAllocationBuffer(bufferToSplit->DataStart(), PRUint32(splitOffset)); - LinkBuffer(bufferToSplit->mPrev, new_buffer, bufferToSplit); - bufferToSplit->DataStart(aSplitPosition.mPosInBuffer); - } - mTotalDataLength = savedLength; - // duh! splitting a buffer doesn't change the length. - } - - -nsSharedBufferList::Buffer* -nsSharedBufferList::UnlinkBuffer( Buffer* aBufferToUnlink ) - { - NS_ASSERTION(aBufferToUnlink, "aBufferToUnlink"); - - Buffer* prev_buffer = aBufferToUnlink->mPrev; - Buffer* next_buffer = aBufferToUnlink->mNext; - - if ( prev_buffer ) - prev_buffer->mNext = next_buffer; - else - mFirstBuffer = next_buffer; - - if ( next_buffer ) - next_buffer->mPrev = prev_buffer; - else - mLastBuffer = prev_buffer; - - mTotalDataLength -= aBufferToUnlink->DataLength(); - - return aBufferToUnlink; - } - - -void -nsSharedBufferList::DiscardSuffix( PRUint32 /* aLengthToDiscard */ ) - { - // XXX - } - - - -#if 0 -template -void -nsChunkList::CutTrailingData( PRUint32 aLengthToCut ) - { - Chunk* chunk = mLastChunk; - while ( chunk && aLengthToCut ) - { - Chunk* prev_chunk = chunk->mPrev; - if ( aLengthToCut < chunk->mDataLength ) - { - chunk->mDataLength -= aLengthToCut; - aLengthToCut = 0; - } - else - { - RemoveChunk(chunk); - aLengthToCut -= chunk->mDataLength; - operator delete(chunk); - } - - chunk = prev_chunk; - } - } -#endif diff --git a/mozilla/xpcom/string/src/nsSlidingString.cpp b/mozilla/xpcom/string/src/nsSlidingString.cpp deleted file mode 100755 index d45e7ea1086..00000000000 --- a/mozilla/xpcom/string/src/nsSlidingString.cpp +++ /dev/null @@ -1,352 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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 strings. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - * - */ - -#include "nsSlidingString.h" - -#ifndef nsBufferHandleUtils_h___ -#include "nsBufferHandleUtils.h" -#endif - -#include NEW_H - - - - /** - * |nsSlidingSharedBufferList| - */ - -void -nsSlidingSharedBufferList::DiscardUnreferencedPrefix( Buffer* aRecentlyReleasedBuffer ) - { - if ( aRecentlyReleasedBuffer == mFirstBuffer ) - { - while ( mFirstBuffer && !mFirstBuffer->IsReferenced() ) { - Buffer *buffer = UnlinkBuffer(mFirstBuffer); - if (mFreeProc && !buffer->IsSingleAllocationWithBuffer()) { - (*mFreeProc)(buffer->DataStart(), mClientData); - buffer->DataStart(nsnull); - } - delete buffer; - } - } - } - - - - /** - * |nsSlidingSubstring| - */ - - // copy constructor -nsSlidingSubstring::nsSlidingSubstring( const nsSlidingSubstring& aString ) - : mStart(aString.mStart), - mEnd(aString.mEnd), - mBufferList(aString.mBufferList), - mLength(aString.mLength) - { - acquire_ownership_of_buffer_list(); - } - -nsSlidingSubstring::nsSlidingSubstring( const nsSlidingSubstring& aString, const nsAString::const_iterator& aStart, const nsAString::const_iterator& aEnd ) - : mStart(aStart), - mEnd(aEnd), - mBufferList(aString.mBufferList), - mLength(PRUint32(Position::Distance(mStart, mEnd))) - { - acquire_ownership_of_buffer_list(); - } - -nsSlidingSubstring::nsSlidingSubstring( const nsSlidingString& aString ) - : mStart(aString.mStart), - mEnd(aString.mEnd), - mBufferList(aString.mBufferList), - mLength(aString.mLength) - { - acquire_ownership_of_buffer_list(); - } - -nsSlidingSubstring::nsSlidingSubstring( const nsSlidingString& aString, const nsAString::const_iterator& aStart, const nsAString::const_iterator& aEnd ) - : mStart(aStart), - mEnd(aEnd), - mBufferList(aString.mBufferList), - mLength(PRUint32(Position::Distance(mStart, mEnd))) - { - acquire_ownership_of_buffer_list(); - } - -nsSlidingSubstring::nsSlidingSubstring( nsSlidingSharedBufferList* aBufferList ) - : mBufferList(aBufferList) - { - init_range_from_buffer_list(); - acquire_ownership_of_buffer_list(); - } - -typedef const nsSharedBufferList::Buffer* Buffer_ptr; - -static nsSharedBufferList::Buffer* -AllocateContiguousHandleWithData( Buffer_ptr aDummyHandlePtr, const nsAString& aDataSource ) - { - typedef const PRUnichar* PRUnichar_ptr; - - // figure out the number of bytes needed the |HandleT| part, including padding to correctly align the data part - size_t handle_size = NS_AlignedHandleSize(aDummyHandlePtr, PRUnichar_ptr(0)); - - // figure out how many |CharT|s wee need to fit in the data part - size_t string_length = aDataSource.Length(); - - // how many bytes is that (including a zero-terminator so we can claim to be flat)? - size_t string_size = (string_length+1) * sizeof(PRUnichar); - - - nsSharedBufferList::Buffer* result = 0; - void* handle_ptr = ::operator new(handle_size + string_size); - - if ( handle_ptr ) - { - typedef PRUnichar* PRUnichar_ptr; - PRUnichar* string_start_ptr = PRUnichar_ptr(NS_STATIC_CAST(unsigned char*, handle_ptr) + handle_size); - PRUnichar* string_end_ptr = string_start_ptr + string_length; - - nsAString::const_iterator fromBegin, fromEnd; - PRUnichar* toBegin = string_start_ptr; - copy_string(aDataSource.BeginReading(fromBegin), aDataSource.EndReading(fromEnd), toBegin); - result = new (handle_ptr) nsSharedBufferList::Buffer(string_start_ptr, string_end_ptr, string_end_ptr-string_start_ptr+1, PR_TRUE); - } - - return result; - } - -nsSlidingSubstring::nsSlidingSubstring( const nsAString& aSourceString ) - : mBufferList(new nsSlidingSharedBufferList(AllocateContiguousHandleWithData(Buffer_ptr(0), aSourceString))) - { - init_range_from_buffer_list(); - acquire_ownership_of_buffer_list(); - } - -void -nsSlidingSubstring::Rebind( const nsSlidingSubstring& aString ) - { - aString.acquire_ownership_of_buffer_list(); - release_ownership_of_buffer_list(); - - mStart = aString.mStart; - mEnd = aString.mEnd; - mBufferList = aString.mBufferList; - mLength = aString.mLength; - } - -void -nsSlidingSubstring::Rebind( const nsSlidingSubstring& aString, const nsAString::const_iterator& aStart, const nsAString::const_iterator& aEnd ) - { - release_ownership_of_buffer_list(); - - mStart = aStart; - mEnd = aEnd; - mBufferList = aString.mBufferList; - mLength = PRUint32(Position::Distance(mStart, mEnd)); - - acquire_ownership_of_buffer_list(); - } - -void -nsSlidingSubstring::Rebind( const nsSlidingString& aString ) - { - aString.acquire_ownership_of_buffer_list(); - release_ownership_of_buffer_list(); - - mStart = aString.mStart; - mEnd = aString.mEnd; - mBufferList = aString.mBufferList; - mLength = aString.mLength; - } - -void -nsSlidingSubstring::Rebind( const nsSlidingString& aString, const nsAString::const_iterator& aStart, const nsAString::const_iterator& aEnd ) - { - release_ownership_of_buffer_list(); - - mStart = aStart; - mEnd = aEnd; - mBufferList = aString.mBufferList; - mLength = PRUint32(Position::Distance(mStart, mEnd)); - - acquire_ownership_of_buffer_list(); - } - -void -nsSlidingSubstring::Rebind( const nsAString& aSourceString ) - { - release_ownership_of_buffer_list(); - mBufferList = new nsSlidingSharedBufferList(AllocateContiguousHandleWithData(Buffer_ptr(0), aSourceString)); - init_range_from_buffer_list(); - acquire_ownership_of_buffer_list(); - } - -nsSlidingSubstring::~nsSlidingSubstring() - { - release_ownership_of_buffer_list(); - } - -const PRUnichar* -nsSlidingSubstring::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - const Buffer* result_buffer = 0; - switch ( aRequest ) - { - case kPrevFragment: - { - const Buffer* current_buffer = NS_STATIC_CAST(const Buffer*, aFragment.mFragmentIdentifier); - if ( current_buffer != mStart.mBuffer ) - result_buffer = current_buffer->mPrev; - } - break; - - case kFirstFragment: - result_buffer = mStart.mBuffer; - break; - - case kLastFragment: - result_buffer = mEnd.mBuffer; - break; - - case kNextFragment: - { - const Buffer* current_buffer = NS_STATIC_CAST(const Buffer*, aFragment.mFragmentIdentifier); - if ( current_buffer != mEnd.mBuffer ) - result_buffer = current_buffer->mNext; - } - break; - - case kFragmentAt: - { - // kFragmentAt is going away; we hate this linear search - - PRUint32 N; - result_buffer = mStart.mBuffer; - - while ( result_buffer && (N = PRUint32(result_buffer->DataLength())) < aOffset ) - { - aOffset -= N; - result_buffer = result_buffer->mNext; - } - } - break; - } - - if ( result_buffer ) - { - if ( result_buffer == mStart.mBuffer ) - aFragment.mStart = mStart.mPosInBuffer; - else - aFragment.mStart = result_buffer->DataStart(); - - if ( result_buffer == mEnd.mBuffer ) - aFragment.mEnd = mEnd.mPosInBuffer; - else - aFragment.mEnd = result_buffer->DataEnd(); - - aFragment.mFragmentIdentifier = result_buffer; - return aFragment.mStart + aOffset; - } - - return 0; - } - - - - /** - * |nsSlidingString| - */ - -nsSlidingString::nsSlidingString( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd ) - : nsSlidingSubstring(new nsSlidingSharedBufferList(nsSlidingSharedBufferList::NewWrappingBuffer(aStorageStart, aDataEnd, aStorageEnd))) - { - // nothing else to do here - } - -nsSlidingString::nsSlidingString( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd, - nsFreeProc *aFreeProc, void *aClientData ) - : nsSlidingSubstring(new nsSlidingSharedBufferList(nsSlidingSharedBufferList::NewWrappingBuffer(aStorageStart, aDataEnd, aStorageEnd), aFreeProc, aClientData)) - { - // nothing else to do here - } - -void -nsSlidingString::AppendBuffer( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd ) - { - Buffer* new_buffer = - new Buffer(aStorageStart, aDataEnd, aStorageEnd - aStorageStart); - Buffer* old_last_buffer = mBufferList->GetLastBuffer(); - mBufferList->LinkBuffer(old_last_buffer, new_buffer, 0); - mLength += new_buffer->DataLength(); - - mEnd.PointAfter(new_buffer); - } - -void -nsSlidingString::InsertReadable( const nsAString& aReadable, const nsAString::const_iterator& aInsertPoint ) - /* - * Warning: this routine manipulates the shared buffer list in an unexpected way. - * The original design did not really allow for insertions, but this call promises - * that if called for a point after the end of all extant token strings, that no token string - * or the work string will be invalidated. - * - * This routine is protected because it is the responsibility of the derived class to keep those promises. - */ - { - Position insertPos(aInsertPoint); - - mBufferList->SplitBuffer(insertPos, nsSharedBufferList::kSplitCopyRightData); - // splitting to the right keeps the work string and any extant token pointing to and - // holding a reference count on the same buffer - - Buffer* new_buffer = nsSharedBufferList::NewSingleAllocationBuffer(aReadable, 0); - // make a new buffer with all the data to insert... - // BULLSHIT ALERT: we may have empty space to re-use in the split buffer, measure the cost - // of this and decide if we should do the work to fill it - - Buffer* buffer_to_split = insertPos.mBuffer; - mBufferList->LinkBuffer(buffer_to_split, new_buffer, buffer_to_split->mNext); - mLength += aReadable.Length(); - mEnd.PointAfter(mBufferList->GetLastBuffer()); - } - -void -nsSlidingString::DiscardPrefix( const nsAString::const_iterator& aIter ) - { - Position old_start(mStart); - mStart = aIter; - mLength -= Position::Distance(old_start, mStart); - - mStart.mBuffer->AcquireNonOwningReference(); - old_start.mBuffer->ReleaseNonOwningReference(); - - mBufferList->DiscardUnreferencedPrefix(old_start.mBuffer); - } - -const PRUnichar* -nsSlidingString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - return nsSlidingSubstring::GetReadableFragment(aFragment, aRequest, aOffset); - } diff --git a/mozilla/xpcom/string/src/nsString.cpp b/mozilla/xpcom/string/src/nsString.cpp new file mode 100644 index 00000000000..558a079eff6 --- /dev/null +++ b/mozilla/xpcom/string/src/nsString.cpp @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include +#include "nsString.h" + +template class CBufDescriptorReader {}; + +NS_SPECIALIZE_TEMPLATE +class CBufDescriptorReader + { + public: + char* read( const CBufDescriptor& desc ) const + { + NS_ASSERTION(desc.mFlags & CBufDescriptor::F_SINGLE_BYTE, "wrong string type"); + return desc.mStr; + } + }; + +NS_SPECIALIZE_TEMPLATE +class CBufDescriptorReader + { + public: + PRUnichar* read( const CBufDescriptor& desc ) const + { + NS_ASSERTION(desc.mFlags & CBufDescriptor::F_DOUBLE_BYTE, "wrong string type"); + return desc.mUStr; + } + }; + + // define nsString +#include "string-template-def-unichar.h" +#include "nsTString.cpp" +#include "string-template-undef.h" + + // define nsCString +#include "string-template-def-char.h" +#include "nsTString.cpp" +#include "string-template-undef.h" diff --git a/mozilla/xpcom/string/src/nsStringBase.cpp b/mozilla/xpcom/string/src/nsStringBase.cpp new file mode 100644 index 00000000000..e57b7f2b652 --- /dev/null +++ b/mozilla/xpcom/string/src/nsStringBase.cpp @@ -0,0 +1,214 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifdef DEBUG +#define ENABLE_STRING_STATS +#endif + +#ifdef ENABLE_STRING_STATS +#include +#endif + +#include +#include "nsStringBase.h" +#include "nsString.h" +#include "nsDependentString.h" +#include "nsMemory.h" +#include "pratom.h" + +// --------------------------------------------------------------------------- + +static PRUnichar gNullChar = 0; + +const char* nsCharTraits ::sEmptyBuffer = (const char*) &gNullChar; +const PRUnichar* nsCharTraits::sEmptyBuffer = &gNullChar; + +// --------------------------------------------------------------------------- + +#ifdef ENABLE_STRING_STATS +class nsStringStats + { + public: + nsStringStats() + : mAllocCount(0), mReallocCount(0), mFreeCount(0), mShareCount(0) {} + + ~nsStringStats() + { + printf("nsStringStats\n"); + printf(" => mAllocCount: %d\n", mAllocCount); + printf(" => mReallocCount: %d\n", mReallocCount); + printf(" => mFreeCount: %d\n", mFreeCount); + printf(" => mShareCount: %d\n", mShareCount); + printf(" => mAdoptCount: %d\n", mAdoptCount); + printf(" => mAdoptFreeCount: %d\n", mAdoptFreeCount); + } + + PRInt32 mAllocCount; + PRInt32 mReallocCount; + PRInt32 mFreeCount; + PRInt32 mShareCount; + PRInt32 mAdoptCount; + PRInt32 mAdoptFreeCount; + }; +static nsStringStats gStringStats; +#define STRING_STAT_INCREMENT(_s) PR_AtomicIncrement(&gStringStats.m ## _s ## Count) +#else +#define STRING_STAT_INCREMENT(_s) +#endif + +// --------------------------------------------------------------------------- + + /** + * This structure preceeds the string buffers "we" allocate. It may be the + * case that nsTStringBase::mData does not point to one of these special + * buffers. The mFlags member variable distinguishes the buffer type. + * + * When this header is in use, it enables reference counting, and capacity + * tracking. NOTE: A string buffer can be modified only if its reference + * count is 1. + */ +class nsStringHeader + { + private: + + PRInt32 mRefCount; + PRUint32 mStorageSize; + + public: + + void AddRef() + { + PR_AtomicIncrement(&mRefCount); + STRING_STAT_INCREMENT(Share); + } + + void Release() + { + if (PR_AtomicDecrement(&mRefCount) == 0) + { + STRING_STAT_INCREMENT(Free); + free(this); // we were allocated with |malloc| + } + } + + /** + * Alloc returns a pointer to a new string header with set capacity. + */ + static nsStringHeader* Alloc(size_t size) + { + STRING_STAT_INCREMENT(Alloc); + + nsStringHeader *hdr = + (nsStringHeader *) malloc(sizeof(nsStringHeader) + size); + if (hdr) + { + hdr->mRefCount = 1; + hdr->mStorageSize = size; + } + return hdr; + } + + static nsStringHeader* Realloc(nsStringHeader* hdr, size_t size) + { + STRING_STAT_INCREMENT(Realloc); + + // no point in trying to save ourselves if we hit this assertion + NS_ASSERTION(!hdr->IsReadonly(), "|Realloc| attempted on readonly string"); + + hdr = (nsStringHeader*) realloc(hdr, sizeof(nsStringHeader) + size); + if (hdr) + hdr->mStorageSize = size; + + return hdr; + } + + static nsStringHeader* FromData(void* data) + { + return (nsStringHeader*) ( ((char*) data) - sizeof(nsStringHeader) ); + } + + void* Data() const + { + return (void*) ( ((char*) this) + sizeof(nsStringHeader) ); + } + + PRUint32 StorageSize() const + { + return mStorageSize; + } + + /** + * Because nsTStringBase allows only single threaded access, if this + * method returns FALSE, then the caller can be sure that it has + * exclusive access to the nsStringHeader and associated data. + * However, if this function returns TRUE, then there is no telling + * how many other threads may be accessing this object simultaneously. + */ + PRBool IsReadonly() const + { + return mRefCount > 1; + } + }; + +// --------------------------------------------------------------------------- + +inline void +ReleaseData( void* data, PRUint32 flags ) + { + if (flags & nsStringBase::F_SHARED) + { + nsStringHeader::FromData(data)->Release(); + } + else if (flags & nsStringBase::F_OWNED) + { + nsMemory::Free(data); + STRING_STAT_INCREMENT(AdoptFree); + } + // otherwise, nothing to do. + } + + + // define nsStringBase +#include "string-template-def-unichar.h" +#include "nsTStringBase.cpp" +#include "string-template-undef.h" + + // define nsCStringBase +#include "string-template-def-char.h" +#include "nsTStringBase.cpp" +#include "string-template-undef.h" diff --git a/mozilla/xpcom/string/src/nsStringComparator.cpp b/mozilla/xpcom/string/src/nsStringComparator.cpp new file mode 100644 index 00000000000..ced75c73250 --- /dev/null +++ b/mozilla/xpcom/string/src/nsStringComparator.cpp @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include +#include "nsAString.h" +#include "plstr.h" + + + // define nsStringComparator +#include "string-template-def-unichar.h" +#include "nsTStringComparator.cpp" +#include "string-template-undef.h" + + // define nsCStringComparator +#include "string-template-def-char.h" +#include "nsTStringComparator.cpp" +#include "string-template-undef.h" + + +int +nsCaseInsensitiveCStringComparator::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const + { + PRInt32 result=PRInt32(PL_strncasecmp(lhs, rhs, aLength)); + //Egads. PL_strncasecmp is returning *very* negative numbers. + //Some folks expect -1,0,1, so let's temper its enthusiasm. + if (result<0) + result=-1; + return result; + } + +int +nsCaseInsensitiveCStringComparator::operator()( char lhs, char rhs ) const + { + if (lhs == rhs) return 0; + + lhs = tolower(lhs); + rhs = tolower(rhs); + + return lhs - rhs; + } diff --git a/mozilla/xpcom/string/src/nsStringTuple.cpp b/mozilla/xpcom/string/src/nsStringTuple.cpp new file mode 100644 index 00000000000..6a0a6f588f2 --- /dev/null +++ b/mozilla/xpcom/string/src/nsStringTuple.cpp @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by IBM Corporation are Copyright (C) 2003 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsStringTuple.h" + + // convert fragment to string +#define TO_STRING(_v) \ + ( (ptrdiff_t(_v) & 0x1) \ + ? NS_REINTERPRET_CAST(const abstract_string_type*, \ + ((unsigned long)_v & ~0x1))->ToString() \ + : *NS_REINTERPRET_CAST(const string_base_type*, (_v)) ) + + // define nsStringTuple +#include "string-template-def-unichar.h" +#include "nsTStringTuple.cpp" +#include "string-template-undef.h" + + // define nsCStringTuple +#include "string-template-def-char.h" +#include "nsTStringTuple.cpp" +#include "string-template-undef.h" diff --git a/mozilla/xpcom/string/src/nsTAString.cpp b/mozilla/xpcom/string/src/nsTAString.cpp index f0f90cca962..19240e849ed 100644 --- a/mozilla/xpcom/string/src/nsTAString.cpp +++ b/mozilla/xpcom/string/src/nsTAString.cpp @@ -36,9 +36,6 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsTAString.h" -#include "nsTObsoleteAString.h" -#include "nsTString.h" /** * Some comments on this implementation... @@ -54,54 +51,17 @@ */ -#ifdef _MSC_VER - - /** - * MSVC6 does not support template instantiation of destructors, so we - * need to specialize the destructors manually. - * - * Unfortunately, GCC 2.96 cannot compile this code, so we use the - * template instantation approach for non-MSVC compilers. - */ - -NS_SPECIALIZE_TEMPLATE -nsTAString::~nsTAString() +nsTAString_CharT::~nsTAString_CharT() { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->ReleaseData(); else - AsObsoleteString()->~nsTObsoleteAString(); - } -NS_SPECIALIZE_TEMPLATE -nsTAString::~nsTAString() - { - if (mVTable == obsolete_string_type::sCanonicalVTable) - AsString()->ReleaseData(); - else - AsObsoleteString()->~nsTObsoleteAString(); + AsObsoleteString()->~nsTObsoleteAString_CharT(); } -#else // !_MSC_VER -template -nsTAString::~nsTAString() - { - if (mVTable == obsolete_string_type::sCanonicalVTable) - AsString()->ReleaseData(); - else - AsObsoleteString()->~nsTObsoleteAString(); - } - -template nsTAString::~nsTAString(); -template nsTAString::~nsTAString(); - -#endif - - -template -typename -nsTAString::size_type -nsTAString::Length() const +nsTAString_CharT::size_type +nsTAString_CharT::Length() const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->Length(); @@ -109,9 +69,8 @@ nsTAString::Length() const return AsObsoleteString()->Length(); } -template PRBool -nsTAString::Equals( const self_type& readable ) const +nsTAString_CharT::Equals( const self_type& readable ) const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->Equals(readable); @@ -119,9 +78,8 @@ nsTAString::Equals( const self_type& readable ) const return ToString().Equals(readable); } -template PRBool -nsTAString::Equals( const self_type& readable, const comparator_type& comparator ) const +nsTAString_CharT::Equals( const self_type& readable, const comparator_type& comparator ) const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->Equals(readable, comparator); @@ -129,9 +87,8 @@ nsTAString::Equals( const self_type& readable, const comparator_type& com return ToString().Equals(readable, comparator); } -template PRBool -nsTAString::Equals( const char_type* data ) const +nsTAString_CharT::Equals( const char_type* data ) const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->Equals(data); @@ -139,9 +96,8 @@ nsTAString::Equals( const char_type* data ) const return ToString().Equals(data); } -template PRBool -nsTAString::Equals( const char_type* data, const comparator_type& comparator ) const +nsTAString_CharT::Equals( const char_type* data, const comparator_type& comparator ) const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->Equals(data, comparator); @@ -149,9 +105,8 @@ nsTAString::Equals( const char_type* data, const comparator_type& compara return ToString().Equals(data, comparator); } -template PRBool -nsTAString::IsVoid() const +nsTAString_CharT::IsVoid() const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->IsVoid(); @@ -159,9 +114,8 @@ nsTAString::IsVoid() const return AsObsoleteString()->IsVoid(); } -template void -nsTAString::SetIsVoid( PRBool val ) +nsTAString_CharT::SetIsVoid( PRBool val ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->SetIsVoid(val); @@ -169,9 +123,8 @@ nsTAString::SetIsVoid( PRBool val ) AsObsoleteString()->SetIsVoid(val); } -template PRBool -nsTAString::IsTerminated() const +nsTAString_CharT::IsTerminated() const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->IsTerminated(); @@ -179,9 +132,8 @@ nsTAString::IsTerminated() const return AsObsoleteString()->GetFlatBufferHandle() != nsnull; } -template CharT -nsTAString::First() const +nsTAString_CharT::First() const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->First(); @@ -189,9 +141,8 @@ nsTAString::First() const return ToString().First(); } -template CharT -nsTAString::Last() const +nsTAString_CharT::Last() const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->Last(); @@ -199,10 +150,8 @@ nsTAString::Last() const return ToString().Last(); } -template -typename -nsTAString::size_type -nsTAString::CountChar( char_type c ) const +nsTAString_CharT::size_type +nsTAString_CharT::CountChar( char_type c ) const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->CountChar(c); @@ -210,9 +159,8 @@ nsTAString::CountChar( char_type c ) const return ToString().CountChar(c); } -template PRInt32 -nsTAString::FindChar( char_type c, index_type offset ) const +nsTAString_CharT::FindChar( char_type c, index_type offset ) const { if (mVTable == obsolete_string_type::sCanonicalVTable) return AsString()->FindChar(c, offset); @@ -220,9 +168,8 @@ nsTAString::FindChar( char_type c, index_type offset ) const return ToString().FindChar(c, offset); } -template void -nsTAString::SetCapacity( size_type size ) +nsTAString_CharT::SetCapacity( size_type size ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->SetCapacity(size); @@ -230,9 +177,8 @@ nsTAString::SetCapacity( size_type size ) AsObsoleteString()->SetCapacity(size); } -template void -nsTAString::SetLength( size_type size ) +nsTAString_CharT::SetLength( size_type size ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->SetLength(size); @@ -240,9 +186,8 @@ nsTAString::SetLength( size_type size ) AsObsoleteString()->SetLength(size); } -template void -nsTAString::Assign( const self_type& readable ) +nsTAString_CharT::Assign( const self_type& readable ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Assign(readable); @@ -250,19 +195,17 @@ nsTAString::Assign( const self_type& readable ) AsObsoleteString()->do_AssignFromReadable(*readable.AsObsoleteString()); } -template void -nsTAString::Assign( const string_tuple_type& tuple ) +nsTAString_CharT::Assign( const string_tuple_type& tuple ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Assign(tuple); else - AsObsoleteString()->do_AssignFromReadable(*nsTAutoString(tuple).AsObsoleteString()); + AsObsoleteString()->do_AssignFromReadable(*nsTAutoString_CharT(tuple).AsObsoleteString()); } -template void -nsTAString::Assign( const char_type* data ) +nsTAString_CharT::Assign( const char_type* data ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Assign(data); @@ -270,9 +213,8 @@ nsTAString::Assign( const char_type* data ) AsObsoleteString()->do_AssignFromElementPtr(data); } -template void -nsTAString::Assign( const char_type* data, size_type length ) +nsTAString_CharT::Assign( const char_type* data, size_type length ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Assign(data, length); @@ -280,9 +222,8 @@ nsTAString::Assign( const char_type* data, size_type length ) AsObsoleteString()->do_AssignFromElementPtrLength(data, length); } -template void -nsTAString::Assign( char_type c ) +nsTAString_CharT::Assign( char_type c ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Assign(c); @@ -290,9 +231,8 @@ nsTAString::Assign( char_type c ) AsObsoleteString()->do_AssignFromElement(c); } -template void -nsTAString::Append( const self_type& readable ) +nsTAString_CharT::Append( const self_type& readable ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Append(readable); @@ -300,19 +240,17 @@ nsTAString::Append( const self_type& readable ) AsObsoleteString()->do_AppendFromReadable(*readable.AsObsoleteString()); } -template void -nsTAString::Append( const string_tuple_type& tuple ) +nsTAString_CharT::Append( const string_tuple_type& tuple ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Append(tuple); else - AsObsoleteString()->do_AppendFromReadable(*nsTAutoString(tuple).AsObsoleteString()); + AsObsoleteString()->do_AppendFromReadable(*nsTAutoString_CharT(tuple).AsObsoleteString()); } -template void -nsTAString::Append( const char_type* data ) +nsTAString_CharT::Append( const char_type* data ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Append(data); @@ -320,9 +258,8 @@ nsTAString::Append( const char_type* data ) AsObsoleteString()->do_AppendFromElementPtr(data); } -template void -nsTAString::Append( const char_type* data, size_type length ) +nsTAString_CharT::Append( const char_type* data, size_type length ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Append(data, length); @@ -330,9 +267,8 @@ nsTAString::Append( const char_type* data, size_type length ) AsObsoleteString()->do_AppendFromElementPtrLength(data, length); } -template void -nsTAString::Append( char_type c ) +nsTAString_CharT::Append( char_type c ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Append(c); @@ -340,9 +276,8 @@ nsTAString::Append( char_type c ) AsObsoleteString()->do_AppendFromElement(c); } -template void -nsTAString::Insert( const self_type& readable, index_type pos ) +nsTAString_CharT::Insert( const self_type& readable, index_type pos ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Insert(readable, pos); @@ -350,19 +285,17 @@ nsTAString::Insert( const self_type& readable, index_type pos ) AsObsoleteString()->do_InsertFromReadable(*readable.AsObsoleteString(), pos); } -template void -nsTAString::Insert( const string_tuple_type& tuple, index_type pos ) +nsTAString_CharT::Insert( const string_tuple_type& tuple, index_type pos ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Insert(tuple, pos); else - AsObsoleteString()->do_InsertFromReadable(*nsTAutoString(tuple).AsObsoleteString(), pos); + AsObsoleteString()->do_InsertFromReadable(*nsTAutoString_CharT(tuple).AsObsoleteString(), pos); } -template void -nsTAString::Insert( const char_type* data, index_type pos ) +nsTAString_CharT::Insert( const char_type* data, index_type pos ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Insert(data, pos); @@ -370,9 +303,8 @@ nsTAString::Insert( const char_type* data, index_type pos ) AsObsoleteString()->do_InsertFromElementPtr(data, pos); } -template void -nsTAString::Insert( const char_type* data, index_type pos, size_type length ) +nsTAString_CharT::Insert( const char_type* data, index_type pos, size_type length ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Insert(data, pos, length); @@ -380,9 +312,8 @@ nsTAString::Insert( const char_type* data, index_type pos, size_type leng AsObsoleteString()->do_InsertFromElementPtrLength(data, pos, length); } -template void -nsTAString::Insert( char_type c, index_type pos ) +nsTAString_CharT::Insert( char_type c, index_type pos ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Insert(c, pos); @@ -390,9 +321,8 @@ nsTAString::Insert( char_type c, index_type pos ) AsObsoleteString()->do_InsertFromElement(c, pos); } -template void -nsTAString::Cut( index_type cutStart, size_type cutLength ) +nsTAString_CharT::Cut( index_type cutStart, size_type cutLength ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Cut(cutStart, cutLength); @@ -400,9 +330,8 @@ nsTAString::Cut( index_type cutStart, size_type cutLength ) AsObsoleteString()->Cut(cutStart, cutLength); } -template void -nsTAString::Replace( index_type cutStart, size_type cutLength, const self_type& readable ) +nsTAString_CharT::Replace( index_type cutStart, size_type cutLength, const self_type& readable ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Replace(cutStart, cutLength, readable); @@ -410,20 +339,17 @@ nsTAString::Replace( index_type cutStart, size_type cutLength, const self AsObsoleteString()->do_ReplaceFromReadable(cutStart, cutLength, *readable.AsObsoleteString()); } -template void -nsTAString::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ) +nsTAString_CharT::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ) { if (mVTable == obsolete_string_type::sCanonicalVTable) AsString()->Replace(cutStart, cutLength, tuple); else - AsObsoleteString()->do_ReplaceFromReadable(cutStart, cutLength, *nsTAutoString(tuple).AsObsoleteString()); + AsObsoleteString()->do_ReplaceFromReadable(cutStart, cutLength, *nsTAutoString_CharT(tuple).AsObsoleteString()); } -template -typename -nsTAString::size_type -nsTAString::GetReadableBuffer( const char_type **data ) const +nsTAString_CharT::size_type +nsTAString_CharT::GetReadableBuffer( const char_type **data ) const { if (mVTable == obsolete_string_type::sCanonicalVTable) { @@ -432,16 +358,14 @@ nsTAString::GetReadableBuffer( const char_type **data ) const return str->mLength; } - typename obsolete_string_type::const_fragment_type frag; - AsObsoleteString()->GetReadableFragment(frag, kFirstFragment, 0); + obsolete_string_type::const_fragment_type frag; + AsObsoleteString()->GetReadableFragment(frag, obsolete_string_type::kFirstFragment, 0); *data = frag.mStart; return (frag.mEnd - frag.mStart); } -template -typename -nsTAString::size_type -nsTAString::GetWritableBuffer(char_type **data) +nsTAString_CharT::size_type +nsTAString_CharT::GetWritableBuffer(char_type **data) { if (mVTable == obsolete_string_type::sCanonicalVTable) { @@ -450,15 +374,14 @@ nsTAString::GetWritableBuffer(char_type **data) return str->Length(); } - typename obsolete_string_type::fragment_type frag; - AsObsoleteString()->GetWritableFragment(frag, kFirstFragment, 0); + obsolete_string_type::fragment_type frag; + AsObsoleteString()->GetWritableFragment(frag, obsolete_string_type::kFirstFragment, 0); *data = frag.mStart; return (frag.mEnd - frag.mStart); } -template PRBool -nsTAString::IsDependentOn(const char_type* start, const char_type *end) const +nsTAString_CharT::IsDependentOn(const char_type* start, const char_type *end) const { // this is an optimization... if (mVTable == obsolete_string_type::sCanonicalVTable) @@ -466,80 +389,3 @@ nsTAString::IsDependentOn(const char_type* start, const char_type *end) c return ToString().IsDependentOn(start, end); } - - - /** - * explicit template instantiation - */ - -template PRUint32 nsTAString::Length() const; -template PRBool nsTAString::Equals( const self_type& readable ) const; -template PRBool nsTAString::Equals( const self_type& readable, const comparator_type& comparator ) const; -template PRBool nsTAString::Equals( const char_type* data ) const; -template PRBool nsTAString::Equals( const char_type* data, const comparator_type& comparator ) const; -template PRBool nsTAString::IsVoid() const; -template void nsTAString::SetIsVoid( PRBool val ); -template PRBool nsTAString::IsTerminated() const; -template char nsTAString::First() const; -template char nsTAString::Last() const; -template PRUint32 nsTAString::CountChar( char_type c ) const; -template PRInt32 nsTAString::FindChar( char_type c, index_type offset ) const; -template void nsTAString::SetCapacity( size_type size ); -template void nsTAString::SetLength( size_type size ); -template void nsTAString::Assign( const self_type& readable ); -template void nsTAString::Assign( const string_tuple_type& tuple ); -template void nsTAString::Assign( const char_type* data ); -template void nsTAString::Assign( const char_type* data, size_type length ); -template void nsTAString::Assign( char_type c ); -template void nsTAString::Append( const self_type& readable ); -template void nsTAString::Append( const string_tuple_type& tuple ); -template void nsTAString::Append( const char_type* data ); -template void nsTAString::Append( const char_type* data, size_type length ); -template void nsTAString::Append( char_type c ); -template void nsTAString::Insert( const self_type& readable, index_type pos ); -template void nsTAString::Insert( const string_tuple_type& tuple, index_type pos ); -template void nsTAString::Insert( const char_type* data, index_type pos ); -template void nsTAString::Insert( const char_type* data, index_type pos, size_type length ); -template void nsTAString::Insert( char_type c, index_type pos ); -template void nsTAString::Cut( index_type cutStart, size_type cutLength ); -template void nsTAString::Replace( index_type cutStart, size_type cutLength, const self_type& readable ); -template void nsTAString::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ); -template PRUint32 nsTAString::GetReadableBuffer( const char_type **data ) const; -template PRUint32 nsTAString::GetWritableBuffer(char_type **data); -template PRBool nsTAString::IsDependentOn(const char_type* start, const char_type *end) const; - -template PRUint32 nsTAString::Length() const; -template PRBool nsTAString::Equals( const self_type& readable ) const; -template PRBool nsTAString::Equals( const self_type& readable, const comparator_type& comparator ) const; -template PRBool nsTAString::Equals( const char_type* data ) const; -template PRBool nsTAString::Equals( const char_type* data, const comparator_type& comparator ) const; -template PRBool nsTAString::IsVoid() const; -template void nsTAString::SetIsVoid( PRBool val ); -template PRBool nsTAString::IsTerminated() const; -template PRUnichar nsTAString::First() const; -template PRUnichar nsTAString::Last() const; -template PRUint32 nsTAString::CountChar( char_type c ) const; -template PRInt32 nsTAString::FindChar( char_type c, index_type offset ) const; -template void nsTAString::SetCapacity( size_type size ); -template void nsTAString::SetLength( size_type size ); -template void nsTAString::Assign( const self_type& readable ); -template void nsTAString::Assign( const string_tuple_type& tuple ); -template void nsTAString::Assign( const char_type* data ); -template void nsTAString::Assign( const char_type* data, size_type length ); -template void nsTAString::Assign( char_type c ); -template void nsTAString::Append( const self_type& readable ); -template void nsTAString::Append( const string_tuple_type& tuple ); -template void nsTAString::Append( const char_type* data ); -template void nsTAString::Append( const char_type* data, size_type length ); -template void nsTAString::Append( char_type c ); -template void nsTAString::Insert( const self_type& readable, index_type pos ); -template void nsTAString::Insert( const string_tuple_type& tuple, index_type pos ); -template void nsTAString::Insert( const char_type* data, index_type pos ); -template void nsTAString::Insert( const char_type* data, index_type pos, size_type length ); -template void nsTAString::Insert( char_type c, index_type pos ); -template void nsTAString::Cut( index_type cutStart, size_type cutLength ); -template void nsTAString::Replace( index_type cutStart, size_type cutLength, const self_type& readable ); -template void nsTAString::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ); -template PRUint32 nsTAString::GetReadableBuffer( const char_type **data ) const; -template PRUint32 nsTAString::GetWritableBuffer(char_type **data); -template PRBool nsTAString::IsDependentOn(const char_type* start, const char_type *end) const; diff --git a/mozilla/xpcom/string/src/nsTDependentSubstring.cpp b/mozilla/xpcom/string/src/nsTDependentSubstring.cpp index 429496036d9..0ba96cd3945 100644 --- a/mozilla/xpcom/string/src/nsTDependentSubstring.cpp +++ b/mozilla/xpcom/string/src/nsTDependentSubstring.cpp @@ -36,12 +36,8 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsTDependentSubstring.h" -#include "nsAlgorithm.h" - -template void -nsTDependentSubstring::Rebind( const abstract_string_type& readable, PRUint32 startPos, PRUint32 length ) +nsTDependentSubstring_CharT::Rebind( const abstract_string_type& readable, PRUint32 startPos, PRUint32 length ) { size_type strLength = readable.GetReadableBuffer((const char_type**) &mData); @@ -52,9 +48,8 @@ nsTDependentSubstring::Rebind( const abstract_string_type& readable, PRUi mLength = NS_MIN(length, strLength - startPos); } -template void -nsTDependentSubstring::Rebind( const string_base_type& str, PRUint32 startPos, PRUint32 length ) +nsTDependentSubstring_CharT::Rebind( const string_base_type& str, PRUint32 startPos, PRUint32 length ) { size_type strLength = str.Length(); @@ -64,14 +59,3 @@ nsTDependentSubstring::Rebind( const string_base_type& str, PRUint32 star mData = NS_CONST_CAST(char_type*, str.Data()) + startPos; mLength = NS_MIN(length, strLength - startPos); } - - - /** - * explicit template instantiation - */ - -template void nsTDependentSubstring::Rebind( const abstract_string_type&, PRUint32, PRUint32 ); -template void nsTDependentSubstring::Rebind( const string_base_type&, PRUint32, PRUint32 ); - -template void nsTDependentSubstring::Rebind( const abstract_string_type&, PRUint32, PRUint32 ); -template void nsTDependentSubstring::Rebind( const string_base_type&, PRUint32, PRUint32 ); diff --git a/mozilla/xpcom/string/src/nsTObsoleteAStringThunk.cpp b/mozilla/xpcom/string/src/nsTObsoleteAStringThunk.cpp index 87726ed0ed3..bbae189c667 100644 --- a/mozilla/xpcom/string/src/nsTObsoleteAStringThunk.cpp +++ b/mozilla/xpcom/string/src/nsTObsoleteAStringThunk.cpp @@ -36,33 +36,28 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsTObsoleteAString.h" -#include "nsTString.h" -extern const void *nsObsoleteAStringThunk_vptr; - -template -class nsTObsoleteAStringThunk : public nsTObsoleteAString +class nsTObsoleteAStringThunk_CharT : public nsTObsoleteAString_CharT { public: - typedef CharT char_type; + typedef CharT char_type; - typedef nsTObsoleteAString obsolete_string_type; + typedef nsTObsoleteAString_CharT obsolete_string_type; - typedef typename obsolete_string_type::buffer_handle_type buffer_handle_type; - typedef typename obsolete_string_type::shared_buffer_handle_type shared_buffer_handle_type; - typedef typename obsolete_string_type::const_fragment_type const_fragment_type; - typedef typename obsolete_string_type::fragment_type fragment_type; - typedef typename obsolete_string_type::size_type size_type; - typedef typename obsolete_string_type::index_type index_type; + typedef obsolete_string_type::buffer_handle_type buffer_handle_type; + typedef obsolete_string_type::shared_buffer_handle_type shared_buffer_handle_type; + typedef obsolete_string_type::const_fragment_type const_fragment_type; + typedef obsolete_string_type::fragment_type fragment_type; + typedef obsolete_string_type::size_type size_type; + typedef obsolete_string_type::index_type index_type; - typedef nsTObsoleteAStringThunk self_type; - typedef nsTStringBase string_base_type; - typedef nsTAString abstract_string_type; + typedef nsTObsoleteAStringThunk_CharT self_type; + typedef nsTStringBase_CharT string_base_type; + typedef nsTAString_CharT abstract_string_type; public: - nsTObsoleteAStringThunk() {} + nsTObsoleteAStringThunk_CharT() {} /** @@ -77,7 +72,7 @@ class nsTObsoleteAStringThunk : public nsTObsoleteAString * all virtual methods need to be redirected to appropriate nsString methods */ - virtual ~nsTObsoleteAStringThunk() + virtual ~nsTObsoleteAStringThunk_CharT() { concrete_self()->ReleaseData(); } @@ -238,34 +233,3 @@ class nsTObsoleteAStringThunk : public nsTObsoleteAString } } }; - - -/** - * explicit template instantiation should not be necessary given the code below. - */ - - -/** - * get pointers to canonical vtables... - */ - -inline void *operator new(size_t size, const void *loc) { return (void *) loc; } - -static const void * -get_nsTObsoleteAStringThunk_PRUnichar_vptr() -{ - const void *result; - new (&result) nsTObsoleteAStringThunk(); - return result; -} - -static const void * -get_nsTObsoleteAStringThunk_char_vptr() -{ - const void *result; - new (&result) nsTObsoleteAStringThunk(); - return result; -} - -const void *nsTObsoleteAString::sCanonicalVTable = get_nsTObsoleteAStringThunk_PRUnichar_vptr(); -const void *nsTObsoleteAString ::sCanonicalVTable = get_nsTObsoleteAStringThunk_char_vptr(); diff --git a/mozilla/xpcom/string/src/nsTPromiseFlatString.cpp b/mozilla/xpcom/string/src/nsTPromiseFlatString.cpp index 8bf53a15a78..f7013d4d1c1 100644 --- a/mozilla/xpcom/string/src/nsTPromiseFlatString.cpp +++ b/mozilla/xpcom/string/src/nsTPromiseFlatString.cpp @@ -36,15 +36,12 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsTPromiseFlatString.h" - -template void -nsTPromiseFlatString::Init(const string_base_type& str) +nsTPromiseFlatString_CharT::Init(const string_base_type& str) { // we have to manually set this here since we are being called on an // unitialized object. - mVTable = nsTObsoleteAString::sCanonicalVTable; + mVTable = nsTObsoleteAString_CharT::sCanonicalVTable; if (str.IsTerminated()) { @@ -59,22 +56,11 @@ nsTPromiseFlatString::Init(const string_base_type& str) } // this function is non-inline to minimize codesize -template void -nsTPromiseFlatString::Init(const abstract_string_type& readable) +nsTPromiseFlatString_CharT::Init(const abstract_string_type& readable) { - if (readable.mVTable == nsTObsoleteAString::sCanonicalVTable) + if (readable.mVTable == nsTObsoleteAString_CharT::sCanonicalVTable) Init(*readable.AsString()); else Init(readable.ToString()); } - - - /** - * explicit template instantiation - */ - -template void nsTPromiseFlatString ::Init( const string_base_type& ); -template void nsTPromiseFlatString ::Init( const abstract_string_type& ); -template void nsTPromiseFlatString::Init( const string_base_type& ); -template void nsTPromiseFlatString::Init( const abstract_string_type& ); diff --git a/mozilla/xpcom/string/src/nsTString.cpp b/mozilla/xpcom/string/src/nsTString.cpp index dcbc184276c..bcf8a589b18 100644 --- a/mozilla/xpcom/string/src/nsTString.cpp +++ b/mozilla/xpcom/string/src/nsTString.cpp @@ -36,36 +36,8 @@ * * ***** END LICENSE BLOCK ***** */ -#include -#include "nsTString.h" - -template class CBufDescriptorReader {}; - -NS_SPECIALIZE_TEMPLATE -class CBufDescriptorReader - { - public: - char* read( const CBufDescriptor& desc ) const - { - NS_ASSERTION(desc.mFlags & CBufDescriptor::F_SINGLE_BYTE, "wrong string type"); - return desc.mStr; - } - }; - -NS_SPECIALIZE_TEMPLATE -class CBufDescriptorReader - { - public: - PRUnichar* read( const CBufDescriptor& desc ) const - { - NS_ASSERTION(desc.mFlags & CBufDescriptor::F_DOUBLE_BYTE, "wrong string type"); - return desc.mUStr; - } - }; - -template void -nsTAutoString::Init( const CBufDescriptor& desc ) +nsTAutoString_CharT::Init( const CBufDescriptor& desc ) { mData = CBufDescriptorReader().read(desc); mLength = desc.mLength; @@ -87,11 +59,3 @@ nsTAutoString::Init( const CBufDescriptor& desc ) // mFixedCapacity = 0; // this member will be ignored } } - - - /** - * explicit template instantiation - */ - -template void nsTAutoString ::Init( const CBufDescriptor& ); -template void nsTAutoString::Init( const CBufDescriptor& ); diff --git a/mozilla/xpcom/string/src/nsTStringBase.cpp b/mozilla/xpcom/string/src/nsTStringBase.cpp index c88e23f842e..7c6ae597e74 100644 --- a/mozilla/xpcom/string/src/nsTStringBase.cpp +++ b/mozilla/xpcom/string/src/nsTStringBase.cpp @@ -36,173 +36,6 @@ * * ***** END LICENSE BLOCK ***** */ -#ifdef DEBUG -#define ENABLE_STRING_STATS -#endif - -#ifdef ENABLE_STRING_STATS -#include -#endif - -#include -#include "nsTStringBase.h" -#include "nsTString.h" -#include "nsTDependentString.h" -#include "nsMemory.h" -#include "pratom.h" - -// --------------------------------------------------------------------------- - -static PRUnichar gNullChar = 0; - -const char* nsCharTraits ::sEmptyBuffer = (const char*) &gNullChar; -const PRUnichar* nsCharTraits::sEmptyBuffer = &gNullChar; - -// --------------------------------------------------------------------------- - -#ifdef ENABLE_STRING_STATS -class nsStringStats - { - public: - nsStringStats() - : mAllocCount(0), mReallocCount(0), mFreeCount(0), mShareCount(0) {} - - ~nsStringStats() - { - printf("nsStringStats\n"); - printf(" => mAllocCount: %d\n", mAllocCount); - printf(" => mReallocCount: %d\n", mReallocCount); - printf(" => mFreeCount: %d\n", mFreeCount); - printf(" => mShareCount: %d\n", mShareCount); - printf(" => mAdoptCount: %d\n", mAdoptCount); - printf(" => mAdoptFreeCount: %d\n", mAdoptFreeCount); - } - - PRInt32 mAllocCount; - PRInt32 mReallocCount; - PRInt32 mFreeCount; - PRInt32 mShareCount; - PRInt32 mAdoptCount; - PRInt32 mAdoptFreeCount; - }; -static nsStringStats gStringStats; -#define STRING_STAT_INCREMENT(_s) PR_AtomicIncrement(&gStringStats.m ## _s ## Count) -#else -#define STRING_STAT_INCREMENT(_s) -#endif - -// --------------------------------------------------------------------------- - - /** - * This structure preceeds the string buffers "we" allocate. It may be the - * case that nsTStringBase::mData does not point to one of these special - * buffers. The mFlags member variable distinguishes the buffer type. - * - * When this header is in use, it enables reference counting, and capacity - * tracking. NOTE: A string buffer can be modified only if its reference - * count is 1. - */ -class nsStringHeader - { - private: - - PRInt32 mRefCount; - PRUint32 mStorageSize; - - public: - - void AddRef() - { - PR_AtomicIncrement(&mRefCount); - STRING_STAT_INCREMENT(Share); - } - - void Release() - { - if (PR_AtomicDecrement(&mRefCount) == 0) - { - STRING_STAT_INCREMENT(Free); - free(this); // we were allocated with |malloc| - } - } - - /** - * Alloc returns a pointer to a new string header with set capacity. - */ - static nsStringHeader* Alloc(size_t size) - { - STRING_STAT_INCREMENT(Alloc); - - nsStringHeader *hdr = - (nsStringHeader *) malloc(sizeof(nsStringHeader) + size); - if (hdr) - { - hdr->mRefCount = 1; - hdr->mStorageSize = size; - } - return hdr; - } - - static nsStringHeader* Realloc(nsStringHeader* hdr, size_t size) - { - STRING_STAT_INCREMENT(Realloc); - - // no point in trying to save ourselves if we hit this assertion - NS_ASSERTION(!hdr->IsReadonly(), "|Realloc| attempted on readonly string"); - - hdr = (nsStringHeader*) realloc(hdr, sizeof(nsStringHeader) + size); - if (hdr) - hdr->mStorageSize = size; - - return hdr; - } - - static nsStringHeader* FromData(void* data) - { - return (nsStringHeader*) ( ((char*) data) - sizeof(nsStringHeader) ); - } - - void* Data() const - { - return (void*) ( ((char*) this) + sizeof(nsStringHeader) ); - } - - PRUint32 StorageSize() const - { - return mStorageSize; - } - - /** - * Because nsTStringBase allows only single threaded access, if this - * method returns FALSE, then the caller can be sure that it has - * exclusive access to the nsStringHeader and associated data. - * However, if this function returns TRUE, then there is no telling - * how many other threads may be accessing this object simultaneously. - */ - PRBool IsReadonly() const - { - return mRefCount > 1; - } - }; - -// --------------------------------------------------------------------------- - -inline void -ReleaseData( void* data, PRUint32 flags ) - { - if (flags & nsTStringBase::F_SHARED) - { - nsStringHeader::FromData(data)->Release(); - } - else if (flags & nsTStringBase::F_OWNED) - { - nsMemory::Free(data); - STRING_STAT_INCREMENT(AdoptFree); - } - // otherwise, nothing to do. - } - -// --------------------------------------------------------------------------- /** * this function is called to prepare mData for writing. the given capacity @@ -211,9 +44,8 @@ ReleaseData( void* data, PRUint32 flags ) * returns the old data and old flags members if mData is newly allocated. * the old data must be released by the caller. */ -template PRBool -nsTStringBase::MutatePrep( size_type capacity, char_type** oldData, PRUint32* oldFlags ) +nsTStringBase_CharT::MutatePrep( size_type capacity, char_type** oldData, PRUint32* oldFlags ) { // initialize to no old data *oldData = nsnull; @@ -295,17 +127,15 @@ nsTStringBase::MutatePrep( size_type capacity, char_type** oldData, PRUin return PR_TRUE; } -template void -nsTStringBase::ReleaseData() +nsTStringBase_CharT::ReleaseData() { ::ReleaseData(mData, mFlags); // mData, mLength, and mFlags are purposefully left dangling } -template void -nsTStringBase::ReplacePrep( index_type cutStart, size_type cutLen, size_type fragLen ) +nsTStringBase_CharT::ReplacePrep( index_type cutStart, size_type cutLen, size_type fragLen ) { // bound cut length cutLen = NS_MIN(cutLen, cutStart + mLength); @@ -359,10 +189,8 @@ nsTStringBase::ReplacePrep( index_type cutStart, size_type cutLen, size_t mLength = newLen; } -template -typename -nsTStringBase::size_type -nsTStringBase::Capacity() const +nsTStringBase_CharT::size_type +nsTStringBase_CharT::Capacity() const { size_type capacity; if (mFlags & F_SHARED) @@ -376,7 +204,7 @@ nsTStringBase::Capacity() const } else if (mFlags & F_FIXED) { - capacity = NS_STATIC_CAST(const nsTAutoString*, this)->mFixedCapacity; + capacity = NS_STATIC_CAST(const nsTAutoString_CharT*, this)->mFixedCapacity; } else if (mFlags & F_OWNED) { @@ -394,9 +222,8 @@ nsTStringBase::Capacity() const } #if 0 -template PRBool -nsTStringBase::EnsureCapacity( size_type capacity, PRBool preserveData ) +nsTStringBase_CharT::EnsureCapacity( size_type capacity, PRBool preserveData ) { size_type curCapacity = Capacity(); @@ -474,9 +301,8 @@ nsTStringBase::EnsureCapacity( size_type capacity, PRBool preserveData ) } #endif -template void -nsTStringBase::EnsureMutable() +nsTStringBase_CharT::EnsureMutable() { if (mFlags & F_SHARED) { @@ -491,9 +317,8 @@ nsTStringBase::EnsureMutable() // --------------------------------------------------------------------------- -template void -nsTStringBase::Assign( const char_type* data, size_type length ) +nsTStringBase_CharT::Assign( const char_type* data, size_type length ) { // unfortunately, people do pass null sometimes :( if (!data) @@ -516,9 +341,8 @@ nsTStringBase::Assign( const char_type* data, size_type length ) char_traits::copy(mData, data, length); } -template void -nsTStringBase::Assign( const self_type& str ) +nsTStringBase_CharT::Assign( const self_type& str ) { // |str| could be sharable. we need to check its flags to know how to // deal with it. @@ -549,9 +373,8 @@ nsTStringBase::Assign( const self_type& str ) } } -template void -nsTStringBase::Assign( const string_tuple_type& tuple ) +nsTStringBase_CharT::Assign( const string_tuple_type& tuple ) { if (tuple.IsDependentOn(mData, mData + mLength)) { @@ -568,21 +391,19 @@ nsTStringBase::Assign( const string_tuple_type& tuple ) } // this is non-inline to reduce codesize at the callsite -template void -nsTStringBase::Assign( const abstract_string_type& readable ) +nsTStringBase_CharT::Assign( const abstract_string_type& readable ) { // promote to string if possible to take advantage of sharing - if (readable.mVTable == nsTObsoleteAString::sCanonicalVTable) + if (readable.mVTable == nsTObsoleteAString_CharT::sCanonicalVTable) Assign(*readable.AsString()); else Assign(readable.ToString()); } -template void -nsTStringBase::Adopt( char_type* data, size_type length ) +nsTStringBase_CharT::Adopt( char_type* data, size_type length ) { ::ReleaseData(mData, mFlags); mData = data; @@ -603,16 +424,15 @@ nsTStringBase::Adopt( char_type* data, size_type length ) } -template void -nsTStringBase::Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length ) +nsTStringBase_CharT::Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length ) { if (length == size_type(-1)) length = char_traits::length(data); if (IsDependentOn(data, data + length)) { - nsTAutoString temp(data, length); + nsTAutoString_CharT temp(data, length); Replace(cutStart, cutLength, temp); return; } @@ -623,13 +443,12 @@ nsTStringBase::Replace( index_type cutStart, size_type cutLength, const c char_traits::copy(mData + cutStart, data, length); } -template void -nsTStringBase::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ) +nsTStringBase_CharT::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ) { if (tuple.IsDependentOn(mData, mData + mLength)) { - nsTAutoString temp(tuple); + nsTAutoString_CharT temp(tuple); Replace(cutStart, cutLength, temp); return; } @@ -642,16 +461,14 @@ nsTStringBase::Replace( index_type cutStart, size_type cutLength, const s tuple.WriteTo(mData + cutStart, length); } -template void -nsTStringBase::Replace( index_type cutStart, size_type cutLength, const abstract_string_type& readable ) +nsTStringBase_CharT::Replace( index_type cutStart, size_type cutLength, const abstract_string_type& readable ) { Replace(cutStart, cutLength, readable.ToString()); } -template void -nsTStringBase::SetCapacity( size_type capacity ) +nsTStringBase_CharT::SetCapacity( size_type capacity ) { // capacity does not include room for the terminating null char @@ -682,9 +499,8 @@ nsTStringBase::SetCapacity( size_type capacity ) } } -template void -nsTStringBase::SetLength( size_type length ) +nsTStringBase_CharT::SetLength( size_type length ) { if (length == 0) { @@ -702,9 +518,8 @@ nsTStringBase::SetLength( size_type length ) } } -template void -nsTStringBase::SetIsVoid( PRBool val ) +nsTStringBase_CharT::SetIsVoid( PRBool val ) { if (val) { @@ -717,23 +532,20 @@ nsTStringBase::SetIsVoid( PRBool val ) } } -template PRBool -nsTStringBase::Equals( const self_type& str ) const +nsTStringBase_CharT::Equals( const self_type& str ) const { return mLength == str.mLength && char_traits::compare(mData, str.mData, mLength) == 0; } -template PRBool -nsTStringBase::Equals( const self_type& str, const comparator_type& comp ) const +nsTStringBase_CharT::Equals( const self_type& str, const comparator_type& comp ) const { return mLength == str.mLength && comp(mData, str.mData, mLength) == 0; } -template PRBool -nsTStringBase::Equals( const abstract_string_type& readable ) const +nsTStringBase_CharT::Equals( const abstract_string_type& readable ) const { const char_type* data; size_type length = readable.GetReadableBuffer(&data); @@ -741,9 +553,8 @@ nsTStringBase::Equals( const abstract_string_type& readable ) const return mLength == length && char_traits::compare(mData, data, mLength) == 0; } -template PRBool -nsTStringBase::Equals( const abstract_string_type& readable, const comparator_type& comp ) const +nsTStringBase_CharT::Equals( const abstract_string_type& readable, const comparator_type& comp ) const { const char_type* data; size_type length = readable.GetReadableBuffer(&data); @@ -751,24 +562,20 @@ nsTStringBase::Equals( const abstract_string_type& readable, const compar return mLength == length && comp(mData, data, mLength) == 0; } -template PRBool -nsTStringBase::Equals( const char_type* data ) const +nsTStringBase_CharT::Equals( const char_type* data ) const { - return Equals(nsTDependentString(data)); + return Equals(nsTDependentString_CharT(data)); } -template PRBool -nsTStringBase::Equals( const char_type* data, const comparator_type& comp ) const +nsTStringBase_CharT::Equals( const char_type* data, const comparator_type& comp ) const { - return Equals(nsTDependentString(data), comp); + return Equals(nsTDependentString_CharT(data), comp); } -template -typename -nsTStringBase::size_type -nsTStringBase::CountChar( char_type c ) const +nsTStringBase_CharT::size_type +nsTStringBase_CharT::CountChar( char_type c ) const { const char_type *start = mData; const char_type *end = mData + mLength; @@ -776,9 +583,8 @@ nsTStringBase::CountChar( char_type c ) const return NS_COUNT(start, end, c); } -template PRInt32 -nsTStringBase::FindChar( char_type c, index_type offset ) const +nsTStringBase_CharT::FindChar( char_type c, index_type offset ) const { if (offset < mLength) { @@ -788,56 +594,3 @@ nsTStringBase::FindChar( char_type c, index_type offset ) const } return -1; } - - - /** - * explicit template instantiation - */ - -template void nsTStringBase::ReleaseData(); -template PRBool nsTStringBase::MutatePrep( size_type capacity, char_type** oldData, PRUint32* oldFlags ); -template void nsTStringBase::ReplacePrep( index_type cutStart, size_type cutLen, size_type fragLen ); -template PRUint32 nsTStringBase::Capacity() const; -template void nsTStringBase::EnsureMutable(); -template void nsTStringBase::Assign( const char_type* data, size_type length ); -template void nsTStringBase::Assign( const self_type& str ); -template void nsTStringBase::Assign( const string_tuple_type& tuple ); -template void nsTStringBase::Assign( const abstract_string_type& readable ); -template void nsTStringBase::Adopt( char_type* data, size_type length ); -template void nsTStringBase::Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length ); -template void nsTStringBase::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ); -template void nsTStringBase::Replace( index_type cutStart, size_type cutLength, const abstract_string_type& readable ); -template void nsTStringBase::SetLength( size_type length ); -template void nsTStringBase::SetIsVoid( PRBool val ); -template PRBool nsTStringBase::Equals( const self_type& ) const; -template PRBool nsTStringBase::Equals( const self_type&, const comparator_type& ) const; -template PRBool nsTStringBase::Equals( const abstract_string_type& ) const; -template PRBool nsTStringBase::Equals( const abstract_string_type&, const comparator_type& ) const; -template PRBool nsTStringBase::Equals( const char_type* ) const; -template PRBool nsTStringBase::Equals( const char_type*, const comparator_type& ) const; -template PRUint32 nsTStringBase::CountChar( char_type ) const; -template PRInt32 nsTStringBase::FindChar( char_type, index_type ) const; - -template void nsTStringBase::ReleaseData(); -template PRBool nsTStringBase::MutatePrep( size_type capacity, char_type** oldData, PRUint32* oldFlags ); -template void nsTStringBase::ReplacePrep( index_type cutStart, size_type cutLen, size_type fragLen ); -template PRUint32 nsTStringBase::Capacity() const; -template void nsTStringBase::EnsureMutable(); -template void nsTStringBase::Assign( const char_type* data, size_type length ); -template void nsTStringBase::Assign( const self_type& str ); -template void nsTStringBase::Assign( const string_tuple_type& tuple ); -template void nsTStringBase::Assign( const abstract_string_type& readable ); -template void nsTStringBase::Adopt( char_type* data, size_type length ); -template void nsTStringBase::Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length ); -template void nsTStringBase::Replace( index_type cutStart, size_type cutLength, const string_tuple_type& tuple ); -template void nsTStringBase::Replace( index_type cutStart, size_type cutLength, const abstract_string_type& readable ); -template void nsTStringBase::SetLength( size_type length ); -template void nsTStringBase::SetIsVoid( PRBool val ); -template PRBool nsTStringBase::Equals( const self_type& ) const; -template PRBool nsTStringBase::Equals( const self_type&, const comparator_type& ) const; -template PRBool nsTStringBase::Equals( const abstract_string_type& ) const; -template PRBool nsTStringBase::Equals( const abstract_string_type&, const comparator_type& ) const; -template PRBool nsTStringBase::Equals( const char_type* ) const; -template PRBool nsTStringBase::Equals( const char_type*, const comparator_type& ) const; -template PRUint32 nsTStringBase::CountChar( char_type ) const; -template PRInt32 nsTStringBase::FindChar( char_type, index_type ) const; diff --git a/mozilla/xpcom/string/src/nsTStringComparator.cpp b/mozilla/xpcom/string/src/nsTStringComparator.cpp index 24a1d2f3be1..6d2645a1d41 100644 --- a/mozilla/xpcom/string/src/nsTStringComparator.cpp +++ b/mozilla/xpcom/string/src/nsTStringComparator.cpp @@ -36,21 +36,15 @@ * * ***** END LICENSE BLOCK ***** */ -#include -#include "nsTAString.h" -#include "plstr.h" - - -template NS_COM int -Compare( const nsTAString& lhs, const nsTAString& rhs, const nsTStringComparator& comp ) +Compare( const nsTAString_CharT& lhs, const nsTAString_CharT& rhs, const nsTStringComparator_CharT& comp ) { - typedef typename nsTAString::size_type size_type; + typedef nsTAString_CharT::size_type size_type; if ( &lhs == &rhs ) return 0; - typename nsTAString::const_iterator leftIter, rightIter; + nsTAString_CharT::const_iterator leftIter, rightIter; lhs.BeginReading(leftIter); rhs.BeginReading(rightIter); @@ -72,61 +66,14 @@ Compare( const nsTAString& lhs, const nsTAString& rhs, const nsTSt return result; } -template NS_COM int Compare( const nsTAString& lhs, const nsTAString& rhs, const nsTStringComparator& comp ); -template NS_COM int Compare( const nsTAString& lhs, const nsTAString& rhs, const nsTStringComparator& comp ); - - - /** - * MSVC cannot handle template instantion of operators :-( - */ - -NS_SPECIALIZE_TEMPLATE int -nsTDefaultStringComparator::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const +nsTDefaultStringComparator_CharT::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const { - return nsCharTraits::compare(lhs, rhs, aLength); + return nsCharTraits::compare(lhs, rhs, aLength); } -NS_SPECIALIZE_TEMPLATE int -nsTDefaultStringComparator::operator()( char_type lhs, char_type rhs) const +nsTDefaultStringComparator_CharT::operator()( char_type lhs, char_type rhs) const { return lhs - rhs; } - -NS_SPECIALIZE_TEMPLATE -int -nsTDefaultStringComparator::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const - { - return nsCharTraits::compare(lhs, rhs, aLength); - } - -NS_SPECIALIZE_TEMPLATE -int -nsTDefaultStringComparator::operator()( char_type lhs, char_type rhs) const - { - return lhs - rhs; - } - - -int -nsCaseInsensitiveCStringComparator::operator()( const char_type* lhs, const char_type* rhs, PRUint32 aLength ) const - { - PRInt32 result=PRInt32(PL_strncasecmp(lhs, rhs, aLength)); - //Egads. PL_strncasecmp is returning *very* negative numbers. - //Some folks expect -1,0,1, so let's temper its enthusiasm. - if (result<0) - result=-1; - return result; - } - -int -nsCaseInsensitiveCStringComparator::operator()( char lhs, char rhs ) const - { - if (lhs == rhs) return 0; - - lhs = tolower(lhs); - rhs = tolower(rhs); - - return lhs - rhs; - } diff --git a/mozilla/xpcom/string/src/nsTStringFragment.cpp b/mozilla/xpcom/string/src/nsTStringFragment.cpp deleted file mode 100644 index aeca79718d7..00000000000 --- a/mozilla/xpcom/string/src/nsTStringFragment.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et cindent: */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * - * The Initial Developer of the Original Code is IBM Corporation. - * Portions created by IBM Corporation are Copyright (C) 2003 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Darin Fisher - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include "nsTStringFragment.h" -#include "nsAlgorithm.h" - -// --------------------------------------------------------------------------- - -static PRUnichar gNullChar = 0; - -const char* nsCharTraits ::sEmptyBuffer = (const char*) &gNullChar; -const PRUnichar* nsCharTraits::sEmptyBuffer = &gNullChar; - -// --------------------------------------------------------------------------- - -template -void -nsTStringFragment::InitFromAString( const abstract_string_type& readable ) - { - mVTable = nsTObsoleteAString::sCanonicalVTable; - - if (readable.mVTable == mVTable) - { - *this = readable.AsStringFragment(); - } - else - { - mLength = readable.GetReadableBuffer((const char_type**) &mData); - - // test if string is flat... - if (readable.AsObsoleteString().GetFlatBufferHandle()) - mFlags = F_TERMINATED; - else - mFlags = 0; - } - } - -template -PRBool -nsTStringFragment::Equals( const self_type& frag ) const - { - return mLength == frag.mLength && char_traits::compare(mData, frag.mData, mLength) == 0; - } - -template -PRBool -nsTStringFragment::Equals( const self_type& frag, const comparator_type& comp ) const - { - return mLength == frag.mLength && comp(mData, frag.mData, mLength) == 0; - } - -template -typename -nsTStringFragment::size_type -nsTStringFragment::CountChar( char_type c ) const - { - const char_type *start = mData; - const char_type *end = mData + mLength; - - return NS_COUNT(start, end, c); - } - -template -PRInt32 -nsTStringFragment::FindChar( char_type c, index_type offset ) const - { - if (offset < mLength) - { - const char_type* result = char_traits::find(mData + offset, mLength, c); - if (result) - return result - mData; - } - return -1; - } - - - /** - * explicit template instantiation - */ - -template void nsTStringFragment::InitFromAString( const abstract_string_type& ); -template PRBool nsTStringFragment::Equals ( const self_type& ) const; -template PRBool nsTStringFragment::Equals ( const self_type&, const comparator_type& ) const; -template PRUint32 nsTStringFragment::CountChar ( char_type ) const; -template PRInt32 nsTStringFragment::FindChar ( char_type, index_type ) const; - -template void nsTStringFragment::InitFromAString( const abstract_string_type& ); -template PRBool nsTStringFragment::Equals ( const self_type& ) const; -template PRBool nsTStringFragment::Equals ( const self_type&, const comparator_type& ) const; -template PRUint32 nsTStringFragment::CountChar ( char_type ) const; -template PRInt32 nsTStringFragment::FindChar ( char_type, index_type ) const; diff --git a/mozilla/xpcom/string/src/nsTStringTuple.cpp b/mozilla/xpcom/string/src/nsTStringTuple.cpp index f83dccaf4a7..0cb4e342320 100644 --- a/mozilla/xpcom/string/src/nsTStringTuple.cpp +++ b/mozilla/xpcom/string/src/nsTStringTuple.cpp @@ -36,23 +36,13 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsTStringTuple.h" - -#define TO_STRING(_v) \ - ( (ptrdiff_t(_v) & 0x1) \ - ? NS_REINTERPRET_CAST(const abstract_string_type*, \ - ((unsigned long)_v & ~0x1))->ToString() \ - : *NS_REINTERPRET_CAST(const string_base_type*, (_v)) ) - /** * computes the aggregate string length */ -template -typename -nsTStringTuple::size_type -nsTStringTuple::Length() const +nsTStringTuple_CharT::size_type +nsTStringTuple_CharT::Length() const { // fragments are enumerated right to left @@ -78,9 +68,8 @@ nsTStringTuple::Length() const * method. the string written to |buf| is not null-terminated. */ -template void -nsTStringTuple::WriteTo( char_type *buf, PRUint32 bufLen ) const +nsTStringTuple_CharT::WriteTo( char_type *buf, PRUint32 bufLen ) const { // we need to write out data into buf, ending at end. so our data // needs to preceed |end| exactly. we trust that the buffer was @@ -109,9 +98,8 @@ nsTStringTuple::WriteTo( char_type *buf, PRUint32 bufLen ) const * the given char sequence. */ -template PRBool -nsTStringTuple::IsDependentOn( const char_type *start, const char_type *end ) const +nsTStringTuple_CharT::IsDependentOn( const char_type *start, const char_type *end ) const { // fragments are enumerated right to left @@ -132,16 +120,3 @@ nsTStringTuple::IsDependentOn( const char_type *start, const char_type *e } return dependent; } - - - /** - * explicit template instantiation - */ - -template PRUint32 nsTStringTuple::Length() const; -template void nsTStringTuple::WriteTo( char_type *buf, PRUint32 bufLen ) const; -template PRBool nsTStringTuple::IsDependentOn( const char_type *start, const char_type *end ) const; - -template PRUint32 nsTStringTuple::Length() const; -template void nsTStringTuple::WriteTo( char_type *buf, PRUint32 bufLen ) const; -template PRBool nsTStringTuple::IsDependentOn( const char_type *start, const char_type *end ) const; diff --git a/mozilla/xpcom/string/src/nsXPIDLString.cpp b/mozilla/xpcom/string/src/nsXPIDLString.cpp deleted file mode 100644 index f3c7b9c3422..00000000000 --- a/mozilla/xpcom/string/src/nsXPIDLString.cpp +++ /dev/null @@ -1,245 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * The contents of this file are subject to the Mozilla 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/MPL/ - * - * 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. - * - * The Initial Developer of the Original Code is Netscape - * Communications. Portions created by Netscape Communications are - * Copyright (C) 2001 by Netscape Communications. All - * Rights Reserved. - * - * Contributor(s): - * Scott Collins (original author) - */ - -// XXX TODO: -// -// nsSharableString will need to be careful to use GetSharedBufferHandle -// where necessary so that an nsXPIDLString can be passed as an -// nsSharableString&. We must be careful to ensure that an -// nsXPIDLString can be used as an nsSharableString& and that an -// nsXPIDLString and an nsSharableString can share buffers created by -// the other (and buffers created by nsSharableString::Adopt rather than -// its other assignment methods). - -#include "nsXPIDLString.h" - -#if DEBUG_STRING_STATS -size_t nsXPIDLString::sCreatedCount = 0; -size_t nsXPIDLString::sAliveCount = 0; -size_t nsXPIDLString::sHighWaterCount = 0; -size_t nsXPIDLString::sAssignCount = 0; -size_t nsXPIDLString::sShareCount = 0; - -size_t nsXPIDLCString::sCreatedCount = 0; -size_t nsXPIDLCString::sAliveCount = 0; -size_t nsXPIDLCString::sHighWaterCount = 0; -size_t nsXPIDLCString::sAssignCount = 0; -size_t nsXPIDLCString::sShareCount = 0; -#endif - - -template -class nsImportedStringHandle - : public nsSharedBufferHandle - { - public: - nsImportedStringHandle() : nsSharedBufferHandle(0, 0, 0, PR_FALSE) { } - - CharT** AddressOfStorageStart() { return &(this->mDataStart); } - void RecalculateBoundaries() const; - }; - - -template -void -nsImportedStringHandle::RecalculateBoundaries() const - { - size_t data_length = 0; - - CharT* storage_start = NS_CONST_CAST(CharT*, this->DataStart()); - if ( storage_start ) - { - data_length = nsCharTraits::length(storage_start); - } - - nsImportedStringHandle* mutable_this = NS_CONST_CAST(nsImportedStringHandle*, this); - mutable_this->DataStart(storage_start); - mutable_this->DataEnd(storage_start + data_length); - mutable_this->StorageLength(data_length + 1); - } - - -#if DEBUG_STRING_STATS -const nsXPIDLString::buffer_handle_type* -nsXPIDLString::GetFlatBufferHandle() const - { - --sShareCount; - return GetSharedBufferHandle(); - } - - -const nsXPIDLString::buffer_handle_type* -nsXPIDLString::GetBufferHandle() const - { - --sShareCount; - return GetSharedBufferHandle(); - } - - -void -nsXPIDLString::DebugPrintStats( FILE* aOutFile ) - { - fprintf(aOutFile, "nsXPIDLString stats: %ld alive now [%ld max] of %ld created; %ld getter_Copies, %ld attempts to share\n", - sAliveCount, sHighWaterCount, sCreatedCount, sAssignCount, sShareCount); - } -#endif - - -const nsXPIDLString::shared_buffer_handle_type* -nsXPIDLString::GetSharedBufferHandle() const - { - self_type* mutable_this = NS_CONST_CAST(self_type*, this); - if ( !mBuffer->DataStart() ) - // XXXldb This isn't any good. What if we just called - // PrepareForUseAsOutParam and it hasn't been filled in yet? - mutable_this->mBuffer = GetSharedEmptyBufferHandle(); - else if ( !mBuffer->DataEnd() ) - { - // Our handle may not be an nsImportedStringHandle. However, if it - // is not, this cast will still be safe since no other handle will - // be in this state. - const nsImportedStringHandle* handle = NS_STATIC_CAST(const nsImportedStringHandle*, mBuffer.get()); - handle->RecalculateBoundaries(); - } - -#if DEBUG_STRING_STATS - ++sShareCount; -#endif - return mBuffer.get(); - } - - -nsXPIDLString::char_type** -nsXPIDLString::PrepareForUseAsOutParam() - { - nsImportedStringHandle* handle = new nsImportedStringHandle(); - NS_ASSERTION(handle, "Trouble! We couldn't get a new handle during |getter_Copies|."); - - mBuffer = handle; -#if DEBUG_STRING_STATS - ++sAssignCount; -#endif - return handle->AddressOfStorageStart(); - } - -/* static */ -nsXPIDLString::shared_buffer_handle_type* -nsXPIDLString::GetSharedEmptyBufferHandle() - { - static shared_buffer_handle_type* sBufferHandle = nsnull; - static char_type null_char = char_type(0); - - if (!sBufferHandle) { - sBufferHandle = new nsNonDestructingSharedBufferHandle(&null_char, &null_char, 1); - sBufferHandle->AcquireReference(); // To avoid the |Destroy| - // mechanism unless threads - // race to set the refcount, in - // which case we'll pull the - // same trick in |Destroy|. - sBufferHandle->SetImplementationFlags(sBufferHandle->GetImplementationFlags() | shared_buffer_handle_type::kIsNULL); - } - return sBufferHandle; - } - - -#if DEBUG_STRING_STATS -const nsXPIDLCString::buffer_handle_type* -nsXPIDLCString::GetFlatBufferHandle() const - { - --sShareCount; - return GetSharedBufferHandle(); - } - - -const nsXPIDLCString::buffer_handle_type* -nsXPIDLCString::GetBufferHandle() const - { - --sShareCount; - return GetSharedBufferHandle(); - } - - -void -nsXPIDLCString::DebugPrintStats( FILE* aOutFile ) - { - fprintf(aOutFile, "nsXPIDLCString stats: %ld alive now [%ld max] of %ld created; %ld getter_Copies, %ld attempts to share\n", - sAliveCount, sHighWaterCount, sCreatedCount, sAssignCount, sShareCount); - } -#endif - - -const nsXPIDLCString::shared_buffer_handle_type* -nsXPIDLCString::GetSharedBufferHandle() const - { - self_type* mutable_this = NS_CONST_CAST(self_type*, this); - if ( !mBuffer->DataStart() ) - // XXXldb This isn't any good. What if we just called - // PrepareForUseAsOutParam and it hasn't been filled in yet? - mutable_this->mBuffer = GetSharedEmptyBufferHandle(); - else if ( !mBuffer->DataEnd() ) - { - // Our handle may not be an nsImportedStringHandle. However, if it - // is not, this cast will still be safe since no other handle will - // be in this state. - const nsImportedStringHandle* handle = NS_STATIC_CAST(const nsImportedStringHandle*, mBuffer.get()); - handle->RecalculateBoundaries(); - } - -#if DEBUG_STRING_STATS - ++sShareCount; -#endif - return mBuffer.get(); - } - - -nsXPIDLCString::char_type** -nsXPIDLCString::PrepareForUseAsOutParam() - { - nsImportedStringHandle* handle = new nsImportedStringHandle(); - NS_ASSERTION(handle, "Trouble! We couldn't get a new handle during |getter_Copies|."); - - mBuffer = handle; -#if DEBUG_STRING_STATS - ++sAssignCount; -#endif - return handle->AddressOfStorageStart(); - } - -/* static */ -nsXPIDLCString::shared_buffer_handle_type* -nsXPIDLCString::GetSharedEmptyBufferHandle() - { - static shared_buffer_handle_type* sBufferHandle = nsnull; - static char_type null_char = char_type(0); - - if (!sBufferHandle) { - sBufferHandle = new nsNonDestructingSharedBufferHandle(&null_char, &null_char, 1); - sBufferHandle->AcquireReference(); // To avoid the |Destroy| - // mechanism unless threads - // race to set the refcount, in - // which case we'll pull the - // same trick in |Destroy|. - sBufferHandle->SetImplementationFlags(sBufferHandle->GetImplementationFlags() | shared_buffer_handle_type::kIsNULL); - } - return sBufferHandle; - }