From 2f9ee1d02fb95c3e24bfd5543f2a96d32838e78a Mon Sep 17 00:00:00 2001 From: "dbaron%dbaron.org" Date: Wed, 9 Jul 2003 06:53:44 +0000 Subject: [PATCH] Make ns[C]String::AppendFloat locale-independent. b=209569 r=jag sr=bzbarsky --- string/obsolete/nsStr.cpp | 1244 ---------------------- string/obsolete/nsStrPrivate.h | 287 ----- string/obsolete/nsString.cpp | 1243 ---------------------- string/obsolete/nsString2.cpp | 1455 -------------------------- xpcom/string/obsolete/nsStr.cpp | 1244 ---------------------- xpcom/string/obsolete/nsStrPrivate.h | 287 ----- xpcom/string/obsolete/nsString.cpp | 6 +- xpcom/string/obsolete/nsString2.cpp | 6 +- 8 files changed, 6 insertions(+), 5766 deletions(-) diff --git a/string/obsolete/nsStr.cpp b/string/obsolete/nsStr.cpp index 0f8e1df92ed..e69de29bb2d 100644 --- a/string/obsolete/nsStr.cpp +++ b/string/obsolete/nsStr.cpp @@ -1,1244 +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 - -/****************************************************************************************** - 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; - } -} - -#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/string/obsolete/nsStrPrivate.h b/string/obsolete/nsStrPrivate.h index 124bf9c6743..e69de29bb2d 100644 --- a/string/obsolete/nsStrPrivate.h +++ b/string/obsolete/nsStrPrivate.h @@ -1,287 +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 __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); - -#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/string/obsolete/nsString.cpp b/string/obsolete/nsString.cpp index 12e3a301e52..e69de29bb2d 100644 --- a/string/obsolete/nsString.cpp +++ b/string/obsolete/nsString.cpp @@ -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]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), "%g", aFloat); - sprintf(buf,"%g",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/string/obsolete/nsString2.cpp b/string/obsolete/nsString2.cpp index a22e85ac54e..e69de29bb2d 100644 --- a/string/obsolete/nsString2.cpp +++ b/string/obsolete/nsString2.cpp @@ -1,1455 +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]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), "%g", aFloat); - sprintf(buf,"%g",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); -} - -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/xpcom/string/obsolete/nsStr.cpp b/xpcom/string/obsolete/nsStr.cpp index 0f8e1df92ed..e69de29bb2d 100644 --- a/xpcom/string/obsolete/nsStr.cpp +++ b/xpcom/string/obsolete/nsStr.cpp @@ -1,1244 +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 - -/****************************************************************************************** - 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; - } -} - -#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/xpcom/string/obsolete/nsStrPrivate.h b/xpcom/string/obsolete/nsStrPrivate.h index 124bf9c6743..e69de29bb2d 100644 --- a/xpcom/string/obsolete/nsStrPrivate.h +++ b/xpcom/string/obsolete/nsStrPrivate.h @@ -1,287 +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 __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); - -#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/xpcom/string/obsolete/nsString.cpp b/xpcom/string/obsolete/nsString.cpp index 12e3a301e52..72797f743f8 100644 --- a/xpcom/string/obsolete/nsString.cpp +++ b/xpcom/string/obsolete/nsString.cpp @@ -755,9 +755,9 @@ void nsCString::AppendInt(PRInt32 anInteger,PRInt32 aRadix) { */ void nsCString::AppendFloat( double aFloat ){ char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), "%g", aFloat); - sprintf(buf,"%g",aFloat); + // 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); } diff --git a/xpcom/string/obsolete/nsString2.cpp b/xpcom/string/obsolete/nsString2.cpp index a22e85ac54e..79c07fcad5e 100644 --- a/xpcom/string/obsolete/nsString2.cpp +++ b/xpcom/string/obsolete/nsString2.cpp @@ -773,9 +773,9 @@ void nsString::AppendInt(PRInt32 anInteger,PRInt32 aRadix) { */ void nsString::AppendFloat(double aFloat){ char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), "%g", aFloat); - sprintf(buf,"%g",aFloat); + // 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); }