From fbbac7b0786c662dedebb068454c2865193f003e Mon Sep 17 00:00:00 2001 From: "rickg%netscape.com" Date: Mon, 30 Aug 1999 06:55:13 +0000 Subject: [PATCH] improved docs, bug fixes, removed outdated APIs git-svn-id: svn://10.0.0.236/trunk@45129 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/string/obsolete/nsStr.cpp | 91 +----- mozilla/string/obsolete/nsStr.h | 40 ++- mozilla/string/obsolete/nsString.cpp | 304 ++++++++++++------- mozilla/string/obsolete/nsString.h | 47 ++- mozilla/string/obsolete/nsString2.cpp | 313 +++++++++++--------- mozilla/string/obsolete/nsString2.h | 68 ++--- mozilla/xpcom/ds/nsStr.cpp | 91 +----- mozilla/xpcom/ds/nsStr.h | 40 ++- mozilla/xpcom/ds/nsString.cpp | 304 ++++++++++++------- mozilla/xpcom/ds/nsString.h | 47 ++- mozilla/xpcom/ds/nsString2.cpp | 313 +++++++++++--------- mozilla/xpcom/ds/nsString2.h | 68 ++--- mozilla/xpcom/string/obsolete/nsStr.cpp | 91 +----- mozilla/xpcom/string/obsolete/nsStr.h | 40 ++- mozilla/xpcom/string/obsolete/nsString.cpp | 304 ++++++++++++------- mozilla/xpcom/string/obsolete/nsString.h | 47 ++- mozilla/xpcom/string/obsolete/nsString2.cpp | 313 +++++++++++--------- mozilla/xpcom/string/obsolete/nsString2.h | 68 ++--- 18 files changed, 1326 insertions(+), 1263 deletions(-) diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index 7e84a3b9705..c977a0125ac 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -39,96 +39,8 @@ static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; //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."; -//---------------------------------------------------------------------------------------- -// The following is a memory agent who knows how to recycled (pool) freed memory... -//---------------------------------------------------------------------------------------- - -/************************************************************** - Define the char* (pooled) deallocator class... - **************************************************************/ - -/* -class nsBufferDeallocator: public nsDequeFunctor{ -public: - virtual void* operator()(void* anObject) { - char* aCString= (char*)anObject; - delete [] aCString; - return 0; - } -}; - -class nsPoolingMemoryAgent : public nsMemoryAgent{ -public: - nsPoolingMemoryAgent() { - memset(mPools,0,sizeof(mPools)); - } - - virtual ~nsPoolingMemoryAgent() { - nsBufferDeallocator theDeallocator; - int i=0; - for(i=0;i<10;i++){ - if(mPools[i]){ - mPools[i]->ForEach(theDeallocator); //now delete the buffers - } - delete mPools[i]; - mPools[i]=0; - } - } - - virtual PRBool Alloc(nsStr& aDest,PRUint32 aCount) { - - //we're given the acount value in charunits; we have to scale up by the charsize. - int theShift=4; - PRUint32 theNewCapacity=eDefaultSize; - while(theNewCapacityPop(); - } - if(!aDest.mStr) { - //we're given the acount value in charunits; we have to scale up by the charsize. - size_t theSize=(theNewCapacity<Push(aDest.mStr); - } - else delete [] aDest.mStr; //it's too big. Just delete it. - } - aDest.mStr=0; - aDest.mOwnsBuffer=0; - return PR_TRUE; - } - return PR_FALSE; - } - nsDeque* mPools[16]; -}; - -*/ - static char* gCommonEmptyBuffer=0; + /** * * @update gess10/30/98 @@ -188,7 +100,6 @@ void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 a * @return */ nsIMemoryAgent* GetDefaultAgent(void){ -// static nsPoolingMemoryAgent gDefaultAgent; static nsMemoryAgent gDefaultAgent; return (nsIMemoryAgent*)&gDefaultAgent; } diff --git a/mozilla/string/obsolete/nsStr.h b/mozilla/string/obsolete/nsStr.h index 3f926f1807a..ef7d86974f1 100644 --- a/mozilla/string/obsolete/nsStr.h +++ b/mozilla/string/obsolete/nsStr.h @@ -38,6 +38,23 @@ ***********************************************************************/ +/*********************************************************************** + GENERAL STRING ISSUES: + + 1. nsStrings and nsAutoString are always null terminated. + 2. If you try to set a null char (via SetChar()) a new length is set + 3. nsCStrings can be upsampled into nsString without data loss + 4. Char searching is faster than string searching. Use char interfaces + if your needs will allow it. + 5. It's easy to use the stack for nsAutostring buffer storage (fast too!). + See the CBufDescriptor class in this file. + 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() + provided you specify a 0mCapacity) { @@ -203,9 +199,9 @@ void nsCString::SetCapacity(PRUint32 aLength) { /** - * + * Retrieves internal (1-byte) buffer ptr; * @update gess1/4/99 - * @return ptr to internal buffer (if 1-byte), otherwise NULL + * @return ptr to internal buffer */ const char* nsCString::GetBuffer(void) const { return mStr; @@ -218,18 +214,33 @@ 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 -#include "nsCRT.h" #include "nsStr.h" #include "nsIAtom.h" @@ -66,10 +49,7 @@ class NS_COM nsCString : public nsStr { public: /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsCString(nsIMemoryAgent* anAgent=0); @@ -196,7 +176,7 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); nsSubsumeCStr operator+(const nsCString& aString); /** - * create a new string by adding this to the given buffer. + * create a new string by adding this to the given char*. * @param aCString is a ptr to cstring to be added to this * @return newly created string */ @@ -313,7 +293,7 @@ nsCString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminat operator const char*() const {return (const char*)mStr;} /** - * This method constructs a new nsCString on the stack that is a copy + * This method constructs a new nsCString that is a clone * of this string. * */ @@ -321,18 +301,21 @@ nsCString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new isolatin1 string */ char* ToNewCString() const; /** * Creates a unicode clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new unicode string */ PRUnichar* ToNewUnicode() const; /** * Copies data from internal buffer onto given char* buffer + * NOTE: This only copies as many chars as will fit in given buffer (clips) * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer @@ -378,8 +361,9 @@ nsCString& SetString(const nsStr& aString,PRInt32 aLength=-1) {return Assign(aSt if you want me to determine its length * @return this */ -nsCString& Assign(const nsCString& aString,PRInt32 aCount=-1); +nsCString& Assign(const nsStr& aString,PRInt32 aCount=-1); nsCString& Assign(const char* aString,PRInt32 aCount=-1); +nsCString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); nsCString& Assign(PRUnichar aChar); nsCString& Assign(char aChar); @@ -389,9 +373,11 @@ nsCString& Assign(char aChar); * @return this */ nsCString& operator=(const nsCString& aString) {return Assign(aString);} +nsCString& operator=(const nsStr& aString) {return Assign(aString);} nsCString& operator=(PRUnichar aChar) {return Assign(aChar);} nsCString& operator=(char aChar) {return Assign(aChar);} nsCString& operator=(const char* aCString) {return Assign(aCString);} +nsCString& operator=(const PRUnichar* aString) {return Assign(aString);} #ifdef AIX nsCString& operator=(const nsSubsumeCStr& aSubsumeString); // AIX requires a const here #else @@ -399,8 +385,8 @@ nsCString& operator=(nsSubsumeCStr& aSubsumeString); #endif /** - * Here's a bunch of append mehtods for varying types... - * @param aString : string to be appended to this + * Here's a bunch of methods that append varying types... + * @param various... * @return this */ nsCString& operator+=(const nsCString& aString){return Append(aString,aString.mLength);} @@ -527,6 +513,7 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * Search for given substring within this string * * @param aString is substring to be sought in this + * @param aIgnoreCase selects case sensitivity * @param anOffset tells us where in this strig to start searching * @return offset in string, or -1 (kNotFound) */ @@ -547,7 +534,7 @@ PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1 /** * This method searches this string for the first character - * found in the given string + * found in the given charset * @param aString contains set of chars to be found * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this @@ -730,7 +717,7 @@ public: */ virtual void SizeOf(nsISizeOfHandler* aHandler) const; - char mBuffer[32]; + char mBuffer[kDefaultStringSize]; }; /*************************************************************** diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index ccf9deeec17..ed0933a44d1 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -23,7 +23,6 @@ #include "nsDebug.h" #include "nsDeque.h" - #ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" @@ -56,10 +55,7 @@ static void Subsume(nsStr& aDest,nsStr& aSource){ /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); @@ -69,27 +65,28 @@ nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent * 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 */ nsString::nsString(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aCString); - } /** - * This constructor accepts an ascii string + * This constructor accepts a unicode string * @update gess 1/4/99 - * @param aCString is a ptr to a 1-byte cstr + * @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,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { - nsStr::Initialize(*this,aCharSize); +nsString::nsString(const PRUnichar* aString,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eTwoByte); Assign(aString); } /** - * This is our copy constructor + * This constructor works for all other nsSTr derivatives * @update gess 1/4/99 - * @param reference to another nsString + * @param reference to another nsCString */ nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); @@ -102,12 +99,12 @@ nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAg * @param reference to another nsString */ nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { - nsStr::Initialize(*this,aString.mCharSize); + nsStr::Initialize(*this,eTwoByte); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } /** - * construct off a subsumeable string + * construct from subsumeable string * @update gess 1/4/99 * @param reference to a subsumeString */ @@ -218,18 +215,33 @@ PRUnichar nsString::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } +/** + * Get nth character. + */ PRUnichar nsString::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } +/** + * Get 1st character. + */ PRUnichar nsString::First(void) const{ return GetCharAt(*this,0); } +/** + * Get last character. + */ PRUnichar nsString::Last(void) const{ return 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 nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); - } /** * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr + * @param aLength tells us how many chars to copy from aCString */ -nsAutoString::nsAutoString(const char* aCString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); +nsAutoString::nsAutoString(const char* aCString,PRInt32 aLength) : nsString() { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); mAgent=0; AddNullTerminator(*this); Append(aCString,aLength); - - } /** * 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,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { +nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString,aLength); - } /** - * Copy construct from uni-string - * @param aString is a ptr to a unistr + * constructor that uses external buffer + * @param aBuffer describes the external buffer */ -nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { +nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString() { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); @@ -2035,40 +2076,36 @@ nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { /** - * Copy construct from an nsString + * Copy construct from an nsStr * @param */ -nsAutoString::nsAutoString(const nsStr& aString,eCharSize aCharSize) : nsString(aCharSize) { +nsAutoString::nsAutoString(const nsStr& aString) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString); - } /** - * Copy construct from an nsString - * @param + * Default copy constructor */ -nsAutoString::nsAutoString(const nsAutoString& aString,eCharSize aCharSize) : nsString(aCharSize) { +nsAutoString::nsAutoString(const nsAutoString& aString) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString); - } /** - * Copy construct from an nsString + * Copy construct from a unichar * @param */ -nsAutoString::nsAutoString(PRUnichar aChar,eCharSize aCharSize) : nsString(aCharSize){ +nsAutoString::nsAutoString(PRUnichar aChar) : nsString(){ mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aChar); - } /** diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index dce89bc0d82..f34a01bbe1a 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -17,34 +17,18 @@ */ -/*********************************************************************** - GENERAL STRING ISSUES: - - 1. nsStrings and nsAutoString are always null terminated. - 2. If you try to set a null char (via SetChar()) a new length is set - 3. nsCStrings can be upsampled into nsString without data loss - 4. Char searching is faster than string searching. Use char interfaces - if your needs will allow it. - 5. It's easy to use the stack for nsAutostring buffer storage (fast too!). - See the CBufDescriptor class in nsStr.h - 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() - provided you specify a 0 -#include "nsCRT.h" #include "nsString.h" #include "nsIAtom.h" #include "nsStr.h" +#include "nsCRT.h" class nsISizeOfHandler; @@ -72,10 +56,7 @@ class NS_COM nsString : public nsStr { public: /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsString(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); @@ -90,7 +71,7 @@ nsString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgen * This constructor accepts a unichar string * @param aCString is a ptr to a 2-byte cstr */ -nsString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const PRUnichar* aString,nsIMemoryAgent* anAgent=0); /** * This is a copy constructor that accepts an nsStr @@ -145,6 +126,7 @@ void SetLength(PRUint32 aLength) { * @return nada */ void SetCapacity(PRUint32 aLength); + /** * This method truncates this string to given length. * @@ -211,20 +193,20 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); /** * Create a new string by appending given string to this * @param aString -- 2nd string to be appended - * @return new string + * @return new subsumable string */ nsSubsumeStr operator+(const nsStr& aString); nsSubsumeStr operator+(const nsString& aString); /** - * create a new string by adding this to the given buffer. + * create a new string by adding this to the given cstring * @param aCString is a ptr to cstring to be added to this * @return newly created string */ nsSubsumeStr operator+(const char* aCString); /** - * create a new string by adding this to the given wide buffer. + * create a new string by adding this to the given prunichar*. * @param aString is a ptr to UC-string to be added to this * @return newly created string */ @@ -343,26 +325,28 @@ nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminate *********************************************************************/ /** - * This method constructs a new nsString on the stack that is a copy - * of this string. + * This method constructs a new nsString is a clone of this string. * */ nsString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new isolatin1 string */ char* ToNewCString() const; /** * Creates a unicode clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new unicode string */ PRUnichar* ToNewUnicode() const; /** * Copies data from internal buffer onto given char* buffer + * NOTE: This only copies as many chars as will fit in given buffer (clips) * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer @@ -433,8 +417,8 @@ nsString& operator=(nsSubsumeStr& aSubsumeString); #endif /** - * Here's a bunch of append mehtods for varying types... - * @param aString : string to be appended to this + * Here's a bunch of methods that append varying types... + * @param various... * @return this */ nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} @@ -567,6 +551,8 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * Search for given substring within this string * * @param aString is substring to be sought in this + * @param aIgnoreCase selects case sensitivity + * @param anOffset tells us where in this strig to start searching * @return offset in string, or -1 (kNotFound) */ PRInt32 Find(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; @@ -583,12 +569,12 @@ PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffs * @param aIgnoreCase selects case sensitivity * @return find pos in string, or -1 (kNotFound) */ -PRInt32 Find(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; +//PRInt32 Find(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** * This method searches this string for the first character - * found in the given string + * found in the given charset * @param aString contains set of chars to be found * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this @@ -618,7 +604,7 @@ PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOff * @param aIgnoreCase selects case sensitivity * @return find pos in string, or -1 (kNotFound) */ -PRInt32 RFind(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; +//PRInt32 RFind(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** @@ -787,19 +773,19 @@ extern NS_COM int fputs(const nsString& aString, FILE* out); class NS_COM nsAutoString : public nsString { public: - nsAutoString(eCharSize aCharSize=kDefaultCharSize); - nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); + nsAutoString(eCharSize aCharSize=eTwoByte); + nsAutoString(const char* aCString,PRInt32 aLength=-1); + nsAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsAutoString(CBufDescriptor& aBuffer); - nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const nsStr& aString); + nsAutoString(const nsAutoString& aString); #ifdef AIX nsAutoString(const nsSubsumeStr& aSubsumeStr); // AIX requires a const #else nsAutoString(nsSubsumeStr& aSubsumeStr); #endif // AIX - nsAutoString(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(PRUnichar aChar); virtual ~nsAutoString(); nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} @@ -815,7 +801,7 @@ public: */ virtual void SizeOf(nsISizeOfHandler* aHandler) const; - char mBuffer[32]; + char mBuffer[kDefaultStringSize<ForEach(theDeallocator); //now delete the buffers - } - delete mPools[i]; - mPools[i]=0; - } - } - - virtual PRBool Alloc(nsStr& aDest,PRUint32 aCount) { - - //we're given the acount value in charunits; we have to scale up by the charsize. - int theShift=4; - PRUint32 theNewCapacity=eDefaultSize; - while(theNewCapacityPop(); - } - if(!aDest.mStr) { - //we're given the acount value in charunits; we have to scale up by the charsize. - size_t theSize=(theNewCapacity<Push(aDest.mStr); - } - else delete [] aDest.mStr; //it's too big. Just delete it. - } - aDest.mStr=0; - aDest.mOwnsBuffer=0; - return PR_TRUE; - } - return PR_FALSE; - } - nsDeque* mPools[16]; -}; - -*/ - static char* gCommonEmptyBuffer=0; + /** * * @update gess10/30/98 @@ -188,7 +100,6 @@ void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 a * @return */ nsIMemoryAgent* GetDefaultAgent(void){ -// static nsPoolingMemoryAgent gDefaultAgent; static nsMemoryAgent gDefaultAgent; return (nsIMemoryAgent*)&gDefaultAgent; } diff --git a/mozilla/xpcom/ds/nsStr.h b/mozilla/xpcom/ds/nsStr.h index 3f926f1807a..ef7d86974f1 100644 --- a/mozilla/xpcom/ds/nsStr.h +++ b/mozilla/xpcom/ds/nsStr.h @@ -38,6 +38,23 @@ ***********************************************************************/ +/*********************************************************************** + GENERAL STRING ISSUES: + + 1. nsStrings and nsAutoString are always null terminated. + 2. If you try to set a null char (via SetChar()) a new length is set + 3. nsCStrings can be upsampled into nsString without data loss + 4. Char searching is faster than string searching. Use char interfaces + if your needs will allow it. + 5. It's easy to use the stack for nsAutostring buffer storage (fast too!). + See the CBufDescriptor class in this file. + 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() + provided you specify a 0mCapacity) { @@ -203,9 +199,9 @@ void nsCString::SetCapacity(PRUint32 aLength) { /** - * + * Retrieves internal (1-byte) buffer ptr; * @update gess1/4/99 - * @return ptr to internal buffer (if 1-byte), otherwise NULL + * @return ptr to internal buffer */ const char* nsCString::GetBuffer(void) const { return mStr; @@ -218,18 +214,33 @@ 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 -#include "nsCRT.h" #include "nsStr.h" #include "nsIAtom.h" @@ -66,10 +49,7 @@ class NS_COM nsCString : public nsStr { public: /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsCString(nsIMemoryAgent* anAgent=0); @@ -196,7 +176,7 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); nsSubsumeCStr operator+(const nsCString& aString); /** - * create a new string by adding this to the given buffer. + * create a new string by adding this to the given char*. * @param aCString is a ptr to cstring to be added to this * @return newly created string */ @@ -313,7 +293,7 @@ nsCString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminat operator const char*() const {return (const char*)mStr;} /** - * This method constructs a new nsCString on the stack that is a copy + * This method constructs a new nsCString that is a clone * of this string. * */ @@ -321,18 +301,21 @@ nsCString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new isolatin1 string */ char* ToNewCString() const; /** * Creates a unicode clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new unicode string */ PRUnichar* ToNewUnicode() const; /** * Copies data from internal buffer onto given char* buffer + * NOTE: This only copies as many chars as will fit in given buffer (clips) * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer @@ -378,8 +361,9 @@ nsCString& SetString(const nsStr& aString,PRInt32 aLength=-1) {return Assign(aSt if you want me to determine its length * @return this */ -nsCString& Assign(const nsCString& aString,PRInt32 aCount=-1); +nsCString& Assign(const nsStr& aString,PRInt32 aCount=-1); nsCString& Assign(const char* aString,PRInt32 aCount=-1); +nsCString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); nsCString& Assign(PRUnichar aChar); nsCString& Assign(char aChar); @@ -389,9 +373,11 @@ nsCString& Assign(char aChar); * @return this */ nsCString& operator=(const nsCString& aString) {return Assign(aString);} +nsCString& operator=(const nsStr& aString) {return Assign(aString);} nsCString& operator=(PRUnichar aChar) {return Assign(aChar);} nsCString& operator=(char aChar) {return Assign(aChar);} nsCString& operator=(const char* aCString) {return Assign(aCString);} +nsCString& operator=(const PRUnichar* aString) {return Assign(aString);} #ifdef AIX nsCString& operator=(const nsSubsumeCStr& aSubsumeString); // AIX requires a const here #else @@ -399,8 +385,8 @@ nsCString& operator=(nsSubsumeCStr& aSubsumeString); #endif /** - * Here's a bunch of append mehtods for varying types... - * @param aString : string to be appended to this + * Here's a bunch of methods that append varying types... + * @param various... * @return this */ nsCString& operator+=(const nsCString& aString){return Append(aString,aString.mLength);} @@ -527,6 +513,7 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * Search for given substring within this string * * @param aString is substring to be sought in this + * @param aIgnoreCase selects case sensitivity * @param anOffset tells us where in this strig to start searching * @return offset in string, or -1 (kNotFound) */ @@ -547,7 +534,7 @@ PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1 /** * This method searches this string for the first character - * found in the given string + * found in the given charset * @param aString contains set of chars to be found * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this @@ -730,7 +717,7 @@ public: */ virtual void SizeOf(nsISizeOfHandler* aHandler) const; - char mBuffer[32]; + char mBuffer[kDefaultStringSize]; }; /*************************************************************** diff --git a/mozilla/xpcom/ds/nsString2.cpp b/mozilla/xpcom/ds/nsString2.cpp index ccf9deeec17..ed0933a44d1 100644 --- a/mozilla/xpcom/ds/nsString2.cpp +++ b/mozilla/xpcom/ds/nsString2.cpp @@ -23,7 +23,6 @@ #include "nsDebug.h" #include "nsDeque.h" - #ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" @@ -56,10 +55,7 @@ static void Subsume(nsStr& aDest,nsStr& aSource){ /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); @@ -69,27 +65,28 @@ nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent * 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 */ nsString::nsString(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aCString); - } /** - * This constructor accepts an ascii string + * This constructor accepts a unicode string * @update gess 1/4/99 - * @param aCString is a ptr to a 1-byte cstr + * @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,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { - nsStr::Initialize(*this,aCharSize); +nsString::nsString(const PRUnichar* aString,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eTwoByte); Assign(aString); } /** - * This is our copy constructor + * This constructor works for all other nsSTr derivatives * @update gess 1/4/99 - * @param reference to another nsString + * @param reference to another nsCString */ nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); @@ -102,12 +99,12 @@ nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAg * @param reference to another nsString */ nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { - nsStr::Initialize(*this,aString.mCharSize); + nsStr::Initialize(*this,eTwoByte); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } /** - * construct off a subsumeable string + * construct from subsumeable string * @update gess 1/4/99 * @param reference to a subsumeString */ @@ -218,18 +215,33 @@ PRUnichar nsString::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } +/** + * Get nth character. + */ PRUnichar nsString::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } +/** + * Get 1st character. + */ PRUnichar nsString::First(void) const{ return GetCharAt(*this,0); } +/** + * Get last character. + */ PRUnichar nsString::Last(void) const{ return 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 nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); - } /** * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr + * @param aLength tells us how many chars to copy from aCString */ -nsAutoString::nsAutoString(const char* aCString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); +nsAutoString::nsAutoString(const char* aCString,PRInt32 aLength) : nsString() { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); mAgent=0; AddNullTerminator(*this); Append(aCString,aLength); - - } /** * 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,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { +nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString,aLength); - } /** - * Copy construct from uni-string - * @param aString is a ptr to a unistr + * constructor that uses external buffer + * @param aBuffer describes the external buffer */ -nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { +nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString() { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); @@ -2035,40 +2076,36 @@ nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { /** - * Copy construct from an nsString + * Copy construct from an nsStr * @param */ -nsAutoString::nsAutoString(const nsStr& aString,eCharSize aCharSize) : nsString(aCharSize) { +nsAutoString::nsAutoString(const nsStr& aString) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString); - } /** - * Copy construct from an nsString - * @param + * Default copy constructor */ -nsAutoString::nsAutoString(const nsAutoString& aString,eCharSize aCharSize) : nsString(aCharSize) { +nsAutoString::nsAutoString(const nsAutoString& aString) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString); - } /** - * Copy construct from an nsString + * Copy construct from a unichar * @param */ -nsAutoString::nsAutoString(PRUnichar aChar,eCharSize aCharSize) : nsString(aCharSize){ +nsAutoString::nsAutoString(PRUnichar aChar) : nsString(){ mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aChar); - } /** diff --git a/mozilla/xpcom/ds/nsString2.h b/mozilla/xpcom/ds/nsString2.h index dce89bc0d82..f34a01bbe1a 100644 --- a/mozilla/xpcom/ds/nsString2.h +++ b/mozilla/xpcom/ds/nsString2.h @@ -17,34 +17,18 @@ */ -/*********************************************************************** - GENERAL STRING ISSUES: - - 1. nsStrings and nsAutoString are always null terminated. - 2. If you try to set a null char (via SetChar()) a new length is set - 3. nsCStrings can be upsampled into nsString without data loss - 4. Char searching is faster than string searching. Use char interfaces - if your needs will allow it. - 5. It's easy to use the stack for nsAutostring buffer storage (fast too!). - See the CBufDescriptor class in nsStr.h - 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() - provided you specify a 0 -#include "nsCRT.h" #include "nsString.h" #include "nsIAtom.h" #include "nsStr.h" +#include "nsCRT.h" class nsISizeOfHandler; @@ -72,10 +56,7 @@ class NS_COM nsString : public nsStr { public: /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsString(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); @@ -90,7 +71,7 @@ nsString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgen * This constructor accepts a unichar string * @param aCString is a ptr to a 2-byte cstr */ -nsString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const PRUnichar* aString,nsIMemoryAgent* anAgent=0); /** * This is a copy constructor that accepts an nsStr @@ -145,6 +126,7 @@ void SetLength(PRUint32 aLength) { * @return nada */ void SetCapacity(PRUint32 aLength); + /** * This method truncates this string to given length. * @@ -211,20 +193,20 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); /** * Create a new string by appending given string to this * @param aString -- 2nd string to be appended - * @return new string + * @return new subsumable string */ nsSubsumeStr operator+(const nsStr& aString); nsSubsumeStr operator+(const nsString& aString); /** - * create a new string by adding this to the given buffer. + * create a new string by adding this to the given cstring * @param aCString is a ptr to cstring to be added to this * @return newly created string */ nsSubsumeStr operator+(const char* aCString); /** - * create a new string by adding this to the given wide buffer. + * create a new string by adding this to the given prunichar*. * @param aString is a ptr to UC-string to be added to this * @return newly created string */ @@ -343,26 +325,28 @@ nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminate *********************************************************************/ /** - * This method constructs a new nsString on the stack that is a copy - * of this string. + * This method constructs a new nsString is a clone of this string. * */ nsString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new isolatin1 string */ char* ToNewCString() const; /** * Creates a unicode clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new unicode string */ PRUnichar* ToNewUnicode() const; /** * Copies data from internal buffer onto given char* buffer + * NOTE: This only copies as many chars as will fit in given buffer (clips) * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer @@ -433,8 +417,8 @@ nsString& operator=(nsSubsumeStr& aSubsumeString); #endif /** - * Here's a bunch of append mehtods for varying types... - * @param aString : string to be appended to this + * Here's a bunch of methods that append varying types... + * @param various... * @return this */ nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} @@ -567,6 +551,8 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * Search for given substring within this string * * @param aString is substring to be sought in this + * @param aIgnoreCase selects case sensitivity + * @param anOffset tells us where in this strig to start searching * @return offset in string, or -1 (kNotFound) */ PRInt32 Find(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; @@ -583,12 +569,12 @@ PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffs * @param aIgnoreCase selects case sensitivity * @return find pos in string, or -1 (kNotFound) */ -PRInt32 Find(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; +//PRInt32 Find(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** * This method searches this string for the first character - * found in the given string + * found in the given charset * @param aString contains set of chars to be found * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this @@ -618,7 +604,7 @@ PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOff * @param aIgnoreCase selects case sensitivity * @return find pos in string, or -1 (kNotFound) */ -PRInt32 RFind(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; +//PRInt32 RFind(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** @@ -787,19 +773,19 @@ extern NS_COM int fputs(const nsString& aString, FILE* out); class NS_COM nsAutoString : public nsString { public: - nsAutoString(eCharSize aCharSize=kDefaultCharSize); - nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); + nsAutoString(eCharSize aCharSize=eTwoByte); + nsAutoString(const char* aCString,PRInt32 aLength=-1); + nsAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsAutoString(CBufDescriptor& aBuffer); - nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const nsStr& aString); + nsAutoString(const nsAutoString& aString); #ifdef AIX nsAutoString(const nsSubsumeStr& aSubsumeStr); // AIX requires a const #else nsAutoString(nsSubsumeStr& aSubsumeStr); #endif // AIX - nsAutoString(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(PRUnichar aChar); virtual ~nsAutoString(); nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} @@ -815,7 +801,7 @@ public: */ virtual void SizeOf(nsISizeOfHandler* aHandler) const; - char mBuffer[32]; + char mBuffer[kDefaultStringSize<ForEach(theDeallocator); //now delete the buffers - } - delete mPools[i]; - mPools[i]=0; - } - } - - virtual PRBool Alloc(nsStr& aDest,PRUint32 aCount) { - - //we're given the acount value in charunits; we have to scale up by the charsize. - int theShift=4; - PRUint32 theNewCapacity=eDefaultSize; - while(theNewCapacityPop(); - } - if(!aDest.mStr) { - //we're given the acount value in charunits; we have to scale up by the charsize. - size_t theSize=(theNewCapacity<Push(aDest.mStr); - } - else delete [] aDest.mStr; //it's too big. Just delete it. - } - aDest.mStr=0; - aDest.mOwnsBuffer=0; - return PR_TRUE; - } - return PR_FALSE; - } - nsDeque* mPools[16]; -}; - -*/ - static char* gCommonEmptyBuffer=0; + /** * * @update gess10/30/98 @@ -188,7 +100,6 @@ void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 a * @return */ nsIMemoryAgent* GetDefaultAgent(void){ -// static nsPoolingMemoryAgent gDefaultAgent; static nsMemoryAgent gDefaultAgent; return (nsIMemoryAgent*)&gDefaultAgent; } diff --git a/mozilla/xpcom/string/obsolete/nsStr.h b/mozilla/xpcom/string/obsolete/nsStr.h index 3f926f1807a..ef7d86974f1 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.h +++ b/mozilla/xpcom/string/obsolete/nsStr.h @@ -38,6 +38,23 @@ ***********************************************************************/ +/*********************************************************************** + GENERAL STRING ISSUES: + + 1. nsStrings and nsAutoString are always null terminated. + 2. If you try to set a null char (via SetChar()) a new length is set + 3. nsCStrings can be upsampled into nsString without data loss + 4. Char searching is faster than string searching. Use char interfaces + if your needs will allow it. + 5. It's easy to use the stack for nsAutostring buffer storage (fast too!). + See the CBufDescriptor class in this file. + 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() + provided you specify a 0mCapacity) { @@ -203,9 +199,9 @@ void nsCString::SetCapacity(PRUint32 aLength) { /** - * + * Retrieves internal (1-byte) buffer ptr; * @update gess1/4/99 - * @return ptr to internal buffer (if 1-byte), otherwise NULL + * @return ptr to internal buffer */ const char* nsCString::GetBuffer(void) const { return mStr; @@ -218,18 +214,33 @@ 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 -#include "nsCRT.h" #include "nsStr.h" #include "nsIAtom.h" @@ -66,10 +49,7 @@ class NS_COM nsCString : public nsStr { public: /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsCString(nsIMemoryAgent* anAgent=0); @@ -196,7 +176,7 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); nsSubsumeCStr operator+(const nsCString& aString); /** - * create a new string by adding this to the given buffer. + * create a new string by adding this to the given char*. * @param aCString is a ptr to cstring to be added to this * @return newly created string */ @@ -313,7 +293,7 @@ nsCString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminat operator const char*() const {return (const char*)mStr;} /** - * This method constructs a new nsCString on the stack that is a copy + * This method constructs a new nsCString that is a clone * of this string. * */ @@ -321,18 +301,21 @@ nsCString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new isolatin1 string */ char* ToNewCString() const; /** * Creates a unicode clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new unicode string */ PRUnichar* ToNewUnicode() const; /** * Copies data from internal buffer onto given char* buffer + * NOTE: This only copies as many chars as will fit in given buffer (clips) * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer @@ -378,8 +361,9 @@ nsCString& SetString(const nsStr& aString,PRInt32 aLength=-1) {return Assign(aSt if you want me to determine its length * @return this */ -nsCString& Assign(const nsCString& aString,PRInt32 aCount=-1); +nsCString& Assign(const nsStr& aString,PRInt32 aCount=-1); nsCString& Assign(const char* aString,PRInt32 aCount=-1); +nsCString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); nsCString& Assign(PRUnichar aChar); nsCString& Assign(char aChar); @@ -389,9 +373,11 @@ nsCString& Assign(char aChar); * @return this */ nsCString& operator=(const nsCString& aString) {return Assign(aString);} +nsCString& operator=(const nsStr& aString) {return Assign(aString);} nsCString& operator=(PRUnichar aChar) {return Assign(aChar);} nsCString& operator=(char aChar) {return Assign(aChar);} nsCString& operator=(const char* aCString) {return Assign(aCString);} +nsCString& operator=(const PRUnichar* aString) {return Assign(aString);} #ifdef AIX nsCString& operator=(const nsSubsumeCStr& aSubsumeString); // AIX requires a const here #else @@ -399,8 +385,8 @@ nsCString& operator=(nsSubsumeCStr& aSubsumeString); #endif /** - * Here's a bunch of append mehtods for varying types... - * @param aString : string to be appended to this + * Here's a bunch of methods that append varying types... + * @param various... * @return this */ nsCString& operator+=(const nsCString& aString){return Append(aString,aString.mLength);} @@ -527,6 +513,7 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * Search for given substring within this string * * @param aString is substring to be sought in this + * @param aIgnoreCase selects case sensitivity * @param anOffset tells us where in this strig to start searching * @return offset in string, or -1 (kNotFound) */ @@ -547,7 +534,7 @@ PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1 /** * This method searches this string for the first character - * found in the given string + * found in the given charset * @param aString contains set of chars to be found * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this @@ -730,7 +717,7 @@ public: */ virtual void SizeOf(nsISizeOfHandler* aHandler) const; - char mBuffer[32]; + char mBuffer[kDefaultStringSize]; }; /*************************************************************** diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index ccf9deeec17..ed0933a44d1 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -23,7 +23,6 @@ #include "nsDebug.h" #include "nsDeque.h" - #ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" @@ -56,10 +55,7 @@ static void Subsume(nsStr& aDest,nsStr& aSource){ /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); @@ -69,27 +65,28 @@ nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent * 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 */ nsString::nsString(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aCString); - } /** - * This constructor accepts an ascii string + * This constructor accepts a unicode string * @update gess 1/4/99 - * @param aCString is a ptr to a 1-byte cstr + * @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,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { - nsStr::Initialize(*this,aCharSize); +nsString::nsString(const PRUnichar* aString,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eTwoByte); Assign(aString); } /** - * This is our copy constructor + * This constructor works for all other nsSTr derivatives * @update gess 1/4/99 - * @param reference to another nsString + * @param reference to another nsCString */ nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); @@ -102,12 +99,12 @@ nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAg * @param reference to another nsString */ nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { - nsStr::Initialize(*this,aString.mCharSize); + nsStr::Initialize(*this,eTwoByte); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } /** - * construct off a subsumeable string + * construct from subsumeable string * @update gess 1/4/99 * @param reference to a subsumeString */ @@ -218,18 +215,33 @@ PRUnichar nsString::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } +/** + * Get nth character. + */ PRUnichar nsString::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } +/** + * Get 1st character. + */ PRUnichar nsString::First(void) const{ return GetCharAt(*this,0); } +/** + * Get last character. + */ PRUnichar nsString::Last(void) const{ return 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 nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); - } /** * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr + * @param aLength tells us how many chars to copy from aCString */ -nsAutoString::nsAutoString(const char* aCString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); +nsAutoString::nsAutoString(const char* aCString,PRInt32 aLength) : nsString() { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); mAgent=0; AddNullTerminator(*this); Append(aCString,aLength); - - } /** * 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,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { +nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString,aLength); - } /** - * Copy construct from uni-string - * @param aString is a ptr to a unistr + * constructor that uses external buffer + * @param aBuffer describes the external buffer */ -nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { +nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString() { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); @@ -2035,40 +2076,36 @@ nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { /** - * Copy construct from an nsString + * Copy construct from an nsStr * @param */ -nsAutoString::nsAutoString(const nsStr& aString,eCharSize aCharSize) : nsString(aCharSize) { +nsAutoString::nsAutoString(const nsStr& aString) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString); - } /** - * Copy construct from an nsString - * @param + * Default copy constructor */ -nsAutoString::nsAutoString(const nsAutoString& aString,eCharSize aCharSize) : nsString(aCharSize) { +nsAutoString::nsAutoString(const nsAutoString& aString) : nsString() { mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aString); - } /** - * Copy construct from an nsString + * Copy construct from a unichar * @param */ -nsAutoString::nsAutoString(PRUnichar aChar,eCharSize aCharSize) : nsString(aCharSize){ +nsAutoString::nsAutoString(PRUnichar aChar) : nsString(){ mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); AddNullTerminator(*this); Append(aChar); - } /** diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index dce89bc0d82..f34a01bbe1a 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -17,34 +17,18 @@ */ -/*********************************************************************** - GENERAL STRING ISSUES: - - 1. nsStrings and nsAutoString are always null terminated. - 2. If you try to set a null char (via SetChar()) a new length is set - 3. nsCStrings can be upsampled into nsString without data loss - 4. Char searching is faster than string searching. Use char interfaces - if your needs will allow it. - 5. It's easy to use the stack for nsAutostring buffer storage (fast too!). - See the CBufDescriptor class in nsStr.h - 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() - provided you specify a 0 -#include "nsCRT.h" #include "nsString.h" #include "nsIAtom.h" #include "nsStr.h" +#include "nsCRT.h" class nsISizeOfHandler; @@ -72,10 +56,7 @@ class NS_COM nsString : public nsStr { public: /** - * Default constructor. Note that we actually allocate a small buffer - * to begin with. This is because the "philosophy" of the string class - * was to allow developers direct access to the underlying buffer for - * performance reasons. + * Default constructor. */ nsString(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); @@ -90,7 +71,7 @@ nsString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgen * This constructor accepts a unichar string * @param aCString is a ptr to a 2-byte cstr */ -nsString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const PRUnichar* aString,nsIMemoryAgent* anAgent=0); /** * This is a copy constructor that accepts an nsStr @@ -145,6 +126,7 @@ void SetLength(PRUint32 aLength) { * @return nada */ void SetCapacity(PRUint32 aLength); + /** * This method truncates this string to given length. * @@ -211,20 +193,20 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); /** * Create a new string by appending given string to this * @param aString -- 2nd string to be appended - * @return new string + * @return new subsumable string */ nsSubsumeStr operator+(const nsStr& aString); nsSubsumeStr operator+(const nsString& aString); /** - * create a new string by adding this to the given buffer. + * create a new string by adding this to the given cstring * @param aCString is a ptr to cstring to be added to this * @return newly created string */ nsSubsumeStr operator+(const char* aCString); /** - * create a new string by adding this to the given wide buffer. + * create a new string by adding this to the given prunichar*. * @param aString is a ptr to UC-string to be added to this * @return newly created string */ @@ -343,26 +325,28 @@ nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminate *********************************************************************/ /** - * This method constructs a new nsString on the stack that is a copy - * of this string. + * This method constructs a new nsString is a clone of this string. * */ nsString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new isolatin1 string */ char* ToNewCString() const; /** * Creates a unicode clone of this string + * Note that calls to this method should be matched with calls to Recycle(). * @return ptr to new unicode string */ PRUnichar* ToNewUnicode() const; /** * Copies data from internal buffer onto given char* buffer + * NOTE: This only copies as many chars as will fit in given buffer (clips) * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer @@ -433,8 +417,8 @@ nsString& operator=(nsSubsumeStr& aSubsumeString); #endif /** - * Here's a bunch of append mehtods for varying types... - * @param aString : string to be appended to this + * Here's a bunch of methods that append varying types... + * @param various... * @return this */ nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} @@ -567,6 +551,8 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * Search for given substring within this string * * @param aString is substring to be sought in this + * @param aIgnoreCase selects case sensitivity + * @param anOffset tells us where in this strig to start searching * @return offset in string, or -1 (kNotFound) */ PRInt32 Find(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; @@ -583,12 +569,12 @@ PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffs * @param aIgnoreCase selects case sensitivity * @return find pos in string, or -1 (kNotFound) */ -PRInt32 Find(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; +//PRInt32 Find(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** * This method searches this string for the first character - * found in the given string + * found in the given charset * @param aString contains set of chars to be found * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this @@ -618,7 +604,7 @@ PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOff * @param aIgnoreCase selects case sensitivity * @return find pos in string, or -1 (kNotFound) */ -PRInt32 RFind(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; +//PRInt32 RFind(PRUnichar aChar,PRInt32 offset=-1,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** @@ -787,19 +773,19 @@ extern NS_COM int fputs(const nsString& aString, FILE* out); class NS_COM nsAutoString : public nsString { public: - nsAutoString(eCharSize aCharSize=kDefaultCharSize); - nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); + nsAutoString(eCharSize aCharSize=eTwoByte); + nsAutoString(const char* aCString,PRInt32 aLength=-1); + nsAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsAutoString(CBufDescriptor& aBuffer); - nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const nsStr& aString); + nsAutoString(const nsAutoString& aString); #ifdef AIX nsAutoString(const nsSubsumeStr& aSubsumeStr); // AIX requires a const #else nsAutoString(nsSubsumeStr& aSubsumeStr); #endif // AIX - nsAutoString(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(PRUnichar aChar); virtual ~nsAutoString(); nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} @@ -815,7 +801,7 @@ public: */ virtual void SizeOf(nsISizeOfHandler* aHandler) const; - char mBuffer[32]; + char mBuffer[kDefaultStringSize<