/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): */ #include #include #include #include #include "nsString.h" #include "nsDebug.h" #include "nsCRT.h" #include "nsDeque.h" #ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" #endif static const char* kNullPointerError = "Error: unexpected null ptr"; static const char* kWhitespace="\b\t\r\n "; static void CSubsume(nsStr& aDest,nsStr& aSource){ if(aSource.mStr && aSource.mLength) { if(aSource.mOwnsBuffer){ nsStr::Destroy(aDest); aDest.mStr=aSource.mStr; aDest.mLength=aSource.mLength; aDest.mCharSize=aSource.mCharSize; aDest.mCapacity=aSource.mCapacity; aDest.mOwnsBuffer=aSource.mOwnsBuffer; aSource.mOwnsBuffer=PR_FALSE; aSource.mStr=0; } else{ nsStr::Assign(aDest,aSource,0,aSource.mLength); } } else nsStr::Truncate(aDest,0); } /** * Default constructor. */ nsCString::nsCString() { nsStr::Initialize(*this,eOneByte); } /** * 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) { nsStr::Initialize(*this,eOneByte); Assign(aCString,aLength); } /** * 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 */ nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength) { nsStr::Initialize(*this,eOneByte); Assign(aString,aLength); } /** * This constructor works for all other nsSTr derivatives * @update gess 1/4/99 * @param reference to another nsCString */ nsCString::nsCString(const nsStr &aString) { nsStr::Initialize(*this,eOneByte); nsStr::Assign(*this,aString,0,aString.mLength); } /** * This is our copy constructor * @update gess 1/4/99 * @param reference to another nsCString */ nsCString::nsCString(const nsCString& aString) { nsStr::Initialize(*this,aString.mCharSize); nsStr::Assign(*this,aString,0,aString.mLength); } /** * construct from subsumeable string * @update gess 1/4/99 * @param reference to a subsumeString */ nsCString::nsCString(nsSubsumeCStr& aSubsumeStr) { CSubsume(*this,aSubsumeStr); } /** * Destructor */ nsCString::~nsCString() { nsStr::Destroy(*this); } void nsCString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { *aResult = sizeof(*this) + mCapacity * mCharSize; } } /** * This method truncates this string to given length. * * @update gess 01/04/99 * @param anIndex -- new length of string * @return nada */ void nsCString::Truncate(PRInt32 anIndex) { nsStr::Truncate(*this,anIndex); } /** * Determine whether or not the characters in this * string are in sorted order. * * @update gess 8/25/98 * @return TRUE if ordered. */ PRBool nsCString::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRUint32 theIndex; for(theIndex=1;theIndexmStr[theIndex]) { result=PR_FALSE; break; } } } return result; } /** * Call this method if you want to force the string to a certain capacity * @update gess 1/4/99 * @param aLength -- contains new length for mStr */ void nsCString::SetCapacity(PRUint32 aLength) { if(aLength>mCapacity) { GrowCapacity(*this,aLength); } AddNullTerminator(*this); } /********************************************************************** Accessor methods... *********************************************************************/ /** * Retrieves internal (1-byte) buffer ptr; * @update gess1/4/99 * @return ptr to internal buffer */ const char* nsCString::GetBuffer(void) const { return mStr; } /** * Get nth character. */ PRUnichar nsCString::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } /** * Get nth character. */ PRUnichar nsCString::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } /** * Get 1st character. */ PRUnichar nsCString::First(void) const{ return GetCharAt(*this,0); } /** * Get last character. */ PRUnichar nsCString::Last(void) const{ return (char)GetCharAt(*this,mLength-1); } /** * 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 PRInt32(sizeof(buf)-1)) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; return 0.0f; } char* cp = ToCString(buf, sizeof(buf)); float f = (float) PR_strtod(cp, &cp); if (*cp != 0) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; } *aErrorCode = (PRInt32) NS_OK; return f; } /** * Perform numeric string to int conversion with given radix. * NOTE: 1. This method mandates that the string is well formed and uppercased. * 2. This method will return an error if the string you give contains chars outside the range for the specified radix. * @update gess 10/01/98 * @param aErrorCode will contain error if one occurs * @param aRadix tells us what base to expect the string in. * @return int rep of string value */ PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { //copy chars to local buffer -- step down from 2 bytes to 1 if necessary... PRInt32 result=0; char* cp = aString.mStr + aString.mLength; PRInt32 theMult=1; *anErrorCode = NS_OK; //now iterate the numeric chars and build our result char theChar=0; char theDigit=0; while(--cp>=aString.mStr){ theChar=*cp; if((theChar>='0') && (theChar<='9')){ theDigit=theChar-'0'; } else if((theChar>='A') && (theChar<='F')) { if(10==aRadix){ *anErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } theDigit=(theChar-'A')+10; } else if((theChar>='a') && (theChar<='f')) { if(10==aRadix){ *anErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } theDigit=(theChar-'a')+10; } else if('-'==theChar) { result=-result; break; } else if(('+'==theChar) || (' '==theChar) || ('X'==theChar) || ('x'==theChar)) { //stop in a good state if you see this... break; } else { //we've encountered a char that's not a legal number or sign *anErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } result+=theDigit*theMult; theMult*=aRadix; } return result; } /** * Call this method to extract the rightmost numeric value from the given * 1-byte input string, and simultaneously determine the radix. * NOTE: This method mandates that the string is well formed. * Leading and trailing gunk should be removed, and upper-cased. * @update gess 10/01/98 * @param anInputString contains orig string * @param anOutString contains numeric portion copy of input string * @param aRadix (an out parm) tells the caller what base we think the string is in. * @return non-zero error code if this string is non-numeric */ static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { const char* cp=aString.GetBuffer(); PRInt32 result=NS_ERROR_ILLEGAL_VALUE; if(cp) { //begin by skipping over leading chars that shouldn't be part of the number... char* to=(char*)cp; const char* endcp=cp+aString.mLength; PRBool done=PR_FALSE; while(!done){ switch(*cp) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case '-': case '+': case '#': done=PR_TRUE; break; default: cp++; done=cp=theChar) { aRadix=16; *to++=theChar; } else if('X'==theChar) { if('-'==aString.mStr[0]) to=&aString.mStr[1]; else to=aString.mStr; aRadix=16; } else if('a'<=theChar) { if('f'>=theChar) { aRadix=16; *to++='A'+(theChar-'a'); } else if('x'==theChar) { if('-'==aString.mStr[0]) to=&aString.mStr[1]; else to=aString.mStr; aRadix=16; } } else break; //bad char } else if((theChar>='0') && (theChar<='9')) { *to++=theChar; } else if('-'==theChar) { *to++=theChar; } else if(('#'!=theChar) && ('+'!=theChar)){ break; //terminate on invalid char! } cp++; } aString.Truncate(to-aString.mStr); result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; } return result; } /** * This method tries to autodetect that radix given a string * @update gess 10/01/98 * @return 10,16,or 0 (meaning I don't know) */ PRUint32 nsCString::DetermineRadix(void) { PRUint32 result=kRadixUnknown; if(0=1) { PRInt32 theDiv=theInt/mask1; if((theDiv) || (!isfirst)) { buf[charpos++]="0123456789abcdef"[theDiv]; isfirst=PR_FALSE; } theInt-=theDiv*mask1; mask1/=aRadix; } return Append(buf); } /** * Append the given float to this string * @update gess 01/04/99 * @param aFloat: * @return */ nsCString& nsCString::Append(float aFloat){ char buf[40]; // *** XX UNCOMMENT THIS LINE //PR_snprintf(buf, sizeof(buf), "%g", aFloat); sprintf(buf,"%g",aFloat); return Append(buf); } /* * Copies n characters from this left of this string to given string, * * @update gess 4/1/98 * @param aDest -- Receiving string * @param aCount -- number of chars to copy * @return number of chars copied */ PRUint32 nsCString::Left(nsCString& aDest,PRInt32 aCount) const{ if(aCount<0) aCount=mLength; else aCount=MinInt(aCount,mLength); nsStr::Assign(aDest,*this,0,aCount); return aDest.mLength; } /* * Copies n characters from this string to from given offset * * @update gess 4/1/98 * @param aDest -- Receiving string * @param anOffset -- where copying should begin * @param aCount -- number of chars to copy * @return number of chars copied */ PRUint32 nsCString::Mid(nsCString& aDest,PRUint32 anOffset,PRInt32 aCount) const{ if(aCount<0) aCount=mLength; else aCount=MinInt(aCount,mLength); nsStr::Assign(aDest,*this,anOffset,aCount); return aDest.mLength; } /* * Copies last n characters from this string to given string, * * @update gess 4/1/98 * @param aDest -- Receiving string * @param aCount -- number of chars to copy * @return number of chars copied */ PRUint32 nsCString::Right(nsCString& aDest,PRInt32 aCount) const{ PRInt32 offset=MaxInt(mLength-aCount,0); return Mid(aDest,offset,aCount); } /* * This method inserts n chars from given string into this * string at str[anOffset]. * * @update gess 4/1/98 * @param aString -- source String to be inserted into this * @param anOffset -- insertion position within this str * @param aCount -- number of chars to be copied from aCopy * @return this */ nsCString& nsCString::Insert(const nsCString& aString,PRUint32 anOffset,PRInt32 aCount) { nsStr::Insert(*this,anOffset,aString,0,aCount); return *this; } /** * 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 aCounttells us how many chars to insert * @return this */ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ if(aCString){ nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mStr=(char*)aCString; if(0> 1; PRUnichar theChar=GetCharAt(*this,middle); if (theChar==aChar) return middle; if (theChar>aChar) high = middle - 1; else low = middle + 1; } return kNotFound; } /** * Search for given cstr within this string * * @update gess 3/25/98 * @param aCString - substr to be found * @param aIgnoreCase * @param anOffset * @return offset in string, or -1 (kNotFound) */ PRInt32 nsCString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=kNotFound; if(aCString) { nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mLength=nsCRT::strlen(aCString); temp.mStr=(char*)aCString; result=nsStr::FindSubstr(*this,temp,aIgnoreCase,anOffset); } return result; } /** * Search for given buffer within this string * * @update gess 3/25/98 * @param aString unichar* to be found * @param aIgnoreCase * @param anOffset * @return offset in string, or -1 (kNotFound) */ PRInt32 nsCString::Find(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 anOffset) const{ NS_ASSERTION(0!=aString,kNullPointerError); PRInt32 result=kNotFound; if(aString) { nsStr temp; nsStr::Initialize(temp,eTwoByte); temp.mLength=nsCRT::strlen(aString); temp.mUStr=(PRUnichar*)aString; result=nsStr::FindSubstr(*this,temp,aIgnoreCase,anOffset); } return result; } /** * Search for given string within this string * * @update gess 3/25/98 * @param aSTring -- buffer to be found * @param aIgnoreCase * @param anOffset * @return offset in string, or -1 (kNotFound) */ PRInt32 nsCString::Find(const nsStr& aString,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=nsStr::FindSubstr(*this,aString,aIgnoreCase,anOffset); return result; } /** * Search for a given char, starting at given offset * * @update gess 3/25/98 * @param aChar the unichar to be sought * @param aIgnoreCase * @param anOffset * @return offset of found char, or -1 (kNotFound) */ PRInt32 nsCString::FindChar(PRUnichar aChar,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=nsStr::FindChar(*this,aChar,aIgnoreCase,anOffset); 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 nsCString::FindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{ NS_ASSERTION(0!=aCStringSet,kNullPointerError); PRInt32 result=kNotFound; if(aCStringSet) { nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mLength=nsCRT::strlen(aCStringSet); temp.mStr=(char*)aCStringSet; result=nsStr::FindCharInSet(*this,temp,PR_FALSE,anOffset); } 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 aStringSet * @param anOffset -- where in this string to start searching * @return */ PRInt32 nsCString::FindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{ NS_ASSERTION(0!=aStringSet,kNullPointerError); PRInt32 result=kNotFound; if(aStringSet) { nsStr temp; nsStr::Initialize(temp,eTwoByte); temp.mLength=nsCRT::strlen(aStringSet); temp.mStr=(char*)aStringSet; result=nsStr::FindCharInSet(*this,temp,PR_FALSE,anOffset); } 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 nsCString::FindCharInSet(const nsStr& aSet,PRInt32 anOffset) const{ PRInt32 result=nsStr::FindCharInSet(*this,aSet,PR_FALSE,anOffset); return result; } /** * Reverse search for substring * * @update gess 3/25/98 * @param aString * @param aIgnoreCase * @param anOffset - tells us where to begin the search * @return offset of substring or -1 */ PRInt32 nsCString::RFind(const nsStr& aString,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=nsStr::RFindSubstr(*this,aString,aIgnoreCase,anOffset); return result; } /** * Reverse search for substring * * @update gess 3/25/98 * @param aString * @param aIgnoreCase * @param anOffset - tells us where to begin the search * @return offset of substring or -1 */ PRInt32 nsCString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset) const{ NS_ASSERTION(0!=aString,kNullPointerError); PRInt32 result=kNotFound; if(aString) { nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mLength=nsCRT::strlen(aString); temp.mStr=(char*)aString; result=nsStr::RFindSubstr(*this,temp,aIgnoreCase,anOffset); } return result; } /** * Reverse search for char * * @update gess 3/25/98 * @param aChar * @param aIgnoreCase * @param anOffset - tells us where to begin the search * @return offset of substring or -1 */ PRInt32 nsCString::RFindChar(PRUnichar aChar,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset); 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); PRInt32 result=kNotFound; if(aCStringSet) { nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mLength=nsCRT::strlen(aCStringSet); temp.mStr=(char*)aCStringSet; result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } return result; } /** * Reverse search for char in this string that is also a member of given charset * * @update gess 3/25/98 * @param aStringSet * @param anOffset * @return offset of found char, or -1 (kNotFound) */ PRInt32 nsCString::RFindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{ NS_ASSERTION(0!=aStringSet,kNullPointerError); PRInt32 result=kNotFound; if(aStringSet) { nsStr temp; nsStr::Initialize(temp,eTwoByte); temp.mLength=nsCRT::strlen(aStringSet); temp.mUStr=(PRUnichar*)aStringSet; result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } 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 nsStr& aSet,PRInt32 anOffset) const{ PRInt32 result=nsStr::RFindCharInSet(*this,aSet,PR_FALSE,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; nsStr::Initialize(temp,eOneByte); temp.mLength=nsCRT::strlen(aCString); temp.mStr=(char*)aCString; return nsStr::Compare(*this,temp,aCount,aIgnoreCase); } return 0; } /** * Compares given unistring to this string. * @update gess 01/04/99 * @param aString pts to a uni-string * @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 PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { NS_ASSERTION(0!=aString,kNullPointerError); if(aString) { nsStr temp; nsStr::Initialize(temp,eTwoByte); temp.mLength=nsCRT::strlen(aString); temp.mUStr=(PRUnichar*)aString; return nsStr::Compare(*this,temp,aCount,aIgnoreCase); } return 0; } /** * Compare given nsStr with this cstring. * * @param aString is an nsStr instance to be compared * @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 nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { return nsStr::Compare(*this,aString,aCount,aIgnoreCase); } /** * Here come a whole bunch of operator functions that are self-explanatory... */ PRBool nsCString::operator==(const nsStr& S) const {return Equals(S);} PRBool nsCString::operator==(const char* s) const {return Equals(s);} PRBool nsCString::operator==(const PRUnichar* s) const {return Equals(s);} PRBool nsCString::operator!=(const nsStr& S) const {return PRBool(Compare(S)!=0);} PRBool nsCString::operator!=(const char* s) const {return PRBool(Compare(s)!=0);} PRBool nsCString::operator!=(const PRUnichar* s) const {return PRBool(Compare(s)!=0);} PRBool nsCString::operator<(const nsStr& S) const {return PRBool(Compare(S)<0);} PRBool nsCString::operator<(const char* s) const {return PRBool(Compare(s)<0);} PRBool nsCString::operator<(const PRUnichar* s) const {return PRBool(Compare(s)<0);} PRBool nsCString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} PRBool nsCString::operator>(const char* s) const {return PRBool(Compare(s)>0);} PRBool nsCString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} PRBool nsCString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} PRBool nsCString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} PRBool nsCString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} PRBool nsCString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} PRBool nsCString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} PRBool nsCString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} PRBool nsCString::EqualsIgnoreCase(const nsStr& aString) const { return Equals(aString,PR_TRUE); } PRBool nsCString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { return Equals(aString,PR_TRUE,aLength); } PRBool nsCString::EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aLength) const { return Equals(aString,PR_TRUE,aLength); } /** * Compare this to given string; note that we compare full strings here. * * @update gess 01/04/99 * @param aString is the other nsCString to be compared to * @param aIgnoreCase tells us how to treat case * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ PRBool nsCString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); PRBool result=PRBool(0==theAnswer); return result; } /** * 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::Equals(const char* aCString,PRBool aIgnoreCase,PRInt32 aCount) const{ PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); PRBool result=PRBool(0==theAnswer); return result; } PRBool nsCString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); PRBool result=PRBool(0==theAnswer); return result; } /************************************************************** Define the string deallocator class... **************************************************************/ #ifndef RICKG_TESTBED class nsCStringDeallocator: public nsDequeFunctor{ public: virtual void* operator()(void* anObject) { nsCString* aString= (nsCString*)anObject; if(aString){ delete aString; } return 0; } }; #endif /**************************************************************************** * This class, appropriately enough, creates and recycles nsCString objects.. ****************************************************************************/ #ifndef RICKG_TESTBED class nsCStringRecycler { public: nsCStringRecycler() : mDeque(0) { } ~nsCStringRecycler() { nsCStringDeallocator theDeallocator; mDeque.ForEach(theDeallocator); //now delete the strings } void Recycle(nsCString* aString) { mDeque.Push(aString); } nsCString* CreateString(void){ nsCString* result=(nsCString*)mDeque.Pop(); if(!result) result=new nsCString(); return result; } nsDeque mDeque; }; static nsCStringRecycler& GetRecycler(void); /** * * @update gess 01/04/99 * @param * @return */ nsCStringRecycler& GetRecycler(void){ static nsCStringRecycler gCRecycler; return gCRecycler; } #endif /** * Call this mehod when you're done * @update gess 01/04/99 * @param * @return */ nsCString* nsCString::CreateString(void){ nsCString* result=0; #ifndef RICKG_TESTBED result=GetRecycler().CreateString(); #endif return result; } /** * Call this mehod when you're done * @update gess 01/04/99 * @param * @return */ void nsCString::Recycle(nsCString* aString){ #ifndef RICKG_TESTBED GetRecycler().Recycle(aString); #endif } #if 0 /** * * @update gess8/8/98 * @param * @return */ ostream& operator<<(ostream& aStream,const nsCString& aString){ if(eOneByte==aString.mCharSize) { aStream<= PRInt32(sizeof(buf))) { cp = aString.ToNewCString(); } else { aString.ToCString(cp, len + 1); } if(len>0) ::fwrite(cp, 1, len, out); if (cp != buf) { delete[] cp; } return (int) len; } /** * Dumps the contents of the string to stdout * @update gess 11/15/99 */ void nsCString::DebugDump(void) const { if(mStr && (eOneByte==mCharSize)) { printf("\n%s",mStr); } } /*********************************************************************** IMPLEMENTATION NOTES: AUTOSTRING... ***********************************************************************/ /** * Default constructor * */ nsCAutoString::nsCAutoString() : nsCString(){ nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); AddNullTerminator(*this); } /** * Default constructor */ nsCAutoString::nsCAutoString(const nsCAutoString& aString) : nsCString() { nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); AddNullTerminator(*this); Append(aString); } /** * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr */ nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() { nsStr::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) { nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); } else { nsStr::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. } /** * Copy construct from uni-string * @param aString is a ptr to a unistr */ nsCAutoString::nsCAutoString(const PRUnichar* aString,PRInt32 aLength) : nsCString() { nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); AddNullTerminator(*this); Append(aString,aLength); } /** * construct from an nsStr * @param */ nsCAutoString::nsCAutoString(const nsStr& aString) : nsCString() { nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); AddNullTerminator(*this); Append(aString); } /** * construct from a char * @param */ nsCAutoString::nsCAutoString(PRUnichar aChar) : nsCString(){ nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); AddNullTerminator(*this); Append(aChar); } /** * construct from a subsumeable string * @update gess 1/4/99 * @param reference to a subsumeString */ #ifdef AIX nsCAutoString::nsCAutoString(const nsSubsumeCStr& aSubsumeStr) :nsCString() { nsSubsumeCStr temp(aSubsumeStr); // a temp is needed for the AIX compiler CSubsume(*this,temp); #else nsCAutoString::nsCAutoString( nsSubsumeCStr& aSubsumeStr) :nsCString() { CSubsume(*this,aSubsumeStr); #endif // AIX } /** * deconstructor * @param */ nsCAutoString::~nsCAutoString(){ } void nsCAutoString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const { if (aResult) { *aResult = sizeof(*this) + mCapacity * mCharSize; } } nsSubsumeCStr::nsSubsumeCStr(nsStr& aString) : nsCString() { CSubsume(*this,aString); } nsSubsumeCStr::nsSubsumeCStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { mUStr=aString; mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; mOwnsBuffer=assumeOwnership; } nsSubsumeCStr::nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { mStr=aString; mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength; mOwnsBuffer=assumeOwnership; }