From 9f0f12ed0289bd12ca54c61406e167b28e336a1c Mon Sep 17 00:00:00 2001 From: "rickg%netscape.com" Date: Sun, 25 Jul 1999 17:20:37 +0000 Subject: [PATCH] narrowed the nsCString API's for safety git-svn-id: svn://10.0.0.236/trunk@41051 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/string/obsolete/nsString.cpp | 306 ++++++--------------- mozilla/string/obsolete/nsString.h | 119 +++----- mozilla/xpcom/ds/nsString.cpp | 306 ++++++--------------- mozilla/xpcom/ds/nsString.h | 119 +++----- mozilla/xpcom/string/obsolete/nsString.cpp | 306 ++++++--------------- mozilla/xpcom/string/obsolete/nsString.h | 119 +++----- 6 files changed, 375 insertions(+), 900 deletions(-) diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index b1643b24934..abc2cf62e98 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -35,60 +35,6 @@ static const char* kWhitespace="\b\t\r\n "; -#define NOT_USED 0xfffd - -static PRUint16 PA_HackTable[] = { - NOT_USED, - NOT_USED, - 0x201a, /* SINGLE LOW-9 QUOTATION MARK */ - 0x0192, /* LATIN SMALL LETTER F WITH HOOK */ - 0x201e, /* DOUBLE LOW-9 QUOTATION MARK */ - 0x2026, /* HORIZONTAL ELLIPSIS */ - 0x2020, /* DAGGER */ - 0x2021, /* DOUBLE DAGGER */ - 0x02c6, /* MODIFIER LETTER CIRCUMFLEX ACCENT */ - 0x2030, /* PER MILLE SIGN */ - 0x0160, /* LATIN CAPITAL LETTER S WITH CARON */ - 0x2039, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */ - 0x0152, /* LATIN CAPITAL LIGATURE OE */ - NOT_USED, - NOT_USED, - NOT_USED, - - NOT_USED, - 0x2018, /* LEFT SINGLE QUOTATION MARK */ - 0x2019, /* RIGHT SINGLE QUOTATION MARK */ - 0x201c, /* LEFT DOUBLE QUOTATION MARK */ - 0x201d, /* RIGHT DOUBLE QUOTATION MARK */ - 0x2022, /* BULLET */ - 0x2013, /* EN DASH */ - 0x2014, /* EM DASH */ - 0x02dc, /* SMALL TILDE */ - 0x2122, /* TRADE MARK SIGN */ - 0x0161, /* LATIN SMALL LETTER S WITH CARON */ - 0x203a, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */ - 0x0153, /* LATIN SMALL LIGATURE OE */ - NOT_USED, - NOT_USED, - 0x0178 /* LATIN CAPITAL LETTER Y WITH DIAERESIS */ -}; - -static PRUnichar gToUCS2[256]; - -class CTableConstructor { -public: - CTableConstructor(){ - PRUnichar* cp = gToUCS2; - PRInt32 i; - for (i = 0; i < 256; i++) { - *cp++ = PRUnichar(i); - } - cp = gToUCS2; - for (i = 0; i < 32; i++) { - cp[0x80 + i] = PA_HackTable[i]; - } - } -}; static void CSubsume(nsStr& aDest,nsStr& aSource){ @@ -138,7 +84,27 @@ nsCString::nsCString(const char* aCString,PRInt32 aLength,nsIMemoryAgent* anAgen */ nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,eOneByte); - Assign(aString,aLength); + + if(aString && aLength){ + nsStr temp; + Initialize(temp,eTwoByte); + temp.mUStr=(PRUnichar*)aString; + + if(0=aString.mStr){ - theChar=*cp; + char theChar=*cp; if((theChar>='0') && (theChar<='9')){ theDigit=theChar-'0'; } @@ -797,7 +742,7 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { * @param aString: string to be added to this * @return this */ -nsCString& nsCString::Assign(const nsStr& aString,PRInt32 aCount) { +nsCString& nsCString::Assign(const nsCString& aString,PRInt32 aCount) { if(this!=&aString){ nsStr::Truncate(*this,0,0); @@ -824,27 +769,13 @@ nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) { return *this; } -/** - * assign given unichar* to this string - * @update gess 01/04/99 - * @param aString: buffer to be assigned to this - * @return this - */ -nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) { - nsStr::Truncate(*this,0,0); - if(aString){ - Append(aString,aCount); - } - return *this; -} - /** * assign given char to this string * @update gess 01/04/99 * @param aChar: char to be assignd to this * @return this */ -nsCString& nsCString::Assign(char aChar) { +nsCString& nsCString::Assign(PRUnichar aChar) { nsStr::Truncate(*this,0,0); return Append(aChar); } @@ -855,7 +786,7 @@ nsCString& nsCString::Assign(char aChar) { * @param aChar: char to be assignd to this * @return this */ -nsCString& nsCString::Assign(PRUnichar aChar) { +nsCString& nsCString::Assign(char aChar) { nsStr::Truncate(*this,0,0); return Append(aChar); } @@ -886,7 +817,7 @@ nsCString& nsCString::operator=(nsSubsumeCStr& aSubsumeString) { * @param aString : string to be appended to this * @return this */ -nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { +nsCString& nsCString::Append(const nsCString& aString,PRInt32 aCount) { if(aCount<0) aCount=aString.mLength; else aCount=MinInt(aCount,aString.mLength); @@ -899,8 +830,7 @@ nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { * append given string to this string * @update gess 01/04/99 * @param aString : string to be appended to this - * @param aCount -- number of chars to copy; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! + * @param aCount: #of chars to be copied; -1 means to copy the whole thing * @return this */ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) { @@ -909,9 +839,16 @@ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) { Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; + if(00, we don't double check the actual string length! * @return this */ -nsCString& nsCString::Append(const PRUnichar* aString,PRInt32 aCount) { - if(aString){ - nsStr temp; - Initialize(temp,eTwoByte); - temp.mUStr=(PRUnichar*)aString; +nsCString& nsCString::Append(PRUnichar aChar) { + PRUnichar buf[2]={0,0}; + buf[0]=aChar; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; - - if(0=0) && (aChar<128)) { - return Append((char)aChar); - } - return *this; -} - /** * * @update gess 01/04/99 @@ -1073,7 +992,7 @@ PRUint32 nsCString::Right(nsCString& aCopy,PRInt32 aCount) const{ * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsCString& nsCString::Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount) { +nsCString& nsCString::Insert(const nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount) { nsStr::Insert(*this,anOffset,aCopy,0,aCount,mAgent); return *this; @@ -1086,8 +1005,6 @@ nsCString& nsCString::Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount * @update gess4/22/98 * @param aChar char to be inserted into this string * @param anOffset is insert pos in str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! * @return the number of chars inserted into this string */ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ @@ -1096,39 +1013,16 @@ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCou nsStr::Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; - - if(temp.mLength && (00, we don't double check the actual string length! - * @return the number of chars inserted into this string - */ -nsCString& nsCString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aCount){ - if(aString){ - nsStr temp; - nsStr::Initialize(temp,eTwoByte); - temp.mUStr=(PRUnichar*)aString; - - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; + else aCount=temp.mLength=nsCRT::strlen(aCString); if(temp.mLength && (0= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is a valid space character - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -PRBool nsCString::IsSpace(PRUnichar aChar) { - // XXX i18n - if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is valid digit - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */ -PRBool nsCString::IsDigit(PRUnichar aChar) { - // XXX i18n - return PRBool((aChar >= '0') && (aChar <= '9')); -} - /************************************************************** Define the string deallocator class... @@ -1832,7 +1704,7 @@ nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr */ -nsCAutoString::nsCAutoString(CSharedStrBuffer& aBuffer) : nsCString() { +nsCAutoString::nsCAutoString(CBufDescriptor& aBuffer) : nsCString() { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); diff --git a/mozilla/string/obsolete/nsString.h b/mozilla/string/obsolete/nsString.h index ae208123ea5..3d1bedb67ca 100644 --- a/mozilla/string/obsolete/nsString.h +++ b/mozilla/string/obsolete/nsString.h @@ -17,6 +17,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 nsStr.h + 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() + provided you specify a 00, we don't double check the actual string length! * @return number of chars copied */ -nsCString& Append(const nsStr& aString,PRInt32 aCount); +nsCString& Append(const nsCString& aString,PRInt32 aCount); nsCString& Append(const char* aString,PRInt32 aCount=-1); -nsCString& Append(const PRUnichar* aString,PRInt32 aCount=-1); -nsCString& Append(char aChar); nsCString& Append(PRUnichar aChar); +nsCString& Append(char aChar); nsCString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 nsCString& Append(float aFloat); @@ -491,11 +472,10 @@ PRUint32 Right(nsCString& aCopy,PRInt32 aCount) const; * * @param aCopy -- String to be inserted into this * @param anOffset -- insertion position within this str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! + * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -503,12 +483,9 @@ nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); * * @param aString* to be inserted into this string * @param anOffset is insert pos in str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! * @return the number of chars inserted into this string */ nsCString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); -nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a single char into this string at @@ -519,6 +496,7 @@ nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); * @return the number of chars inserted into this string */ nsCString& Insert(PRUnichar aChar,PRUint32 anOffset); +nsCString& Insert(char aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -636,7 +614,7 @@ virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRI * @return TRUE or FALSE */ PRBool operator==(const nsStr &aString) const; -PRBool operator==(const char *aString) const; +PRBool operator==(const char* aString) const; PRBool operator==(const PRUnichar* aString) const; /** @@ -704,30 +682,6 @@ PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const; -/** - * Determine if given char is a valid space character - * - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -static PRBool IsSpace(PRUnichar ch); - -/** - * Determine if given char in valid alpha range - * - * @param aChar is character to be tested - * @return TRUE if in alpha range - */ -static PRBool IsAlpha(PRUnichar ch); - -/** - * Determine if given char is valid digit - * - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */ -static PRBool IsDigit(PRUnichar ch); - static void Recycle(nsCString* aString); static nsCString* CreateString(void); @@ -752,7 +706,7 @@ public: nsCAutoString(); nsCAutoString(const char* aString,PRInt32 aLength=-1); - nsCAutoString(CSharedStrBuffer& aBuffer); + nsCAutoString(CBufDescriptor& aBuffer); nsCAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsCAutoString(const nsStr& aString); nsCAutoString(const nsCAutoString& aString); @@ -765,11 +719,10 @@ public: nsCAutoString(PRUnichar aChar); virtual ~nsCAutoString(); - nsCAutoString& operator=(const nsStr& aString) {nsCString::Assign(aString); return *this;} - nsCAutoString& operator=(const char* aCString) {nsCString::operator=(aCString); return *this;} - nsCAutoString& operator=(char aChar) {nsCString::operator=(aChar); return *this;} - nsCAutoString& operator=(const PRUnichar* aBuffer) {nsCString::operator=(aBuffer); return *this;} - nsCAutoString& operator=(PRUnichar aChar) {nsCString::operator=(aChar); return *this;} + nsCAutoString& operator=(const nsCString& aString) {nsCString::Assign(aString); return *this;} + nsCAutoString& operator=(const char* aCString) {nsCString::Assign(aCString); return *this;} + nsCAutoString& operator=(PRUnichar aChar) {nsCString::Assign(aChar); return *this;} + nsCAutoString& operator=(char aChar) {nsCString::Assign(aChar); return *this;} /** * Retrieve the size of this string diff --git a/mozilla/xpcom/ds/nsString.cpp b/mozilla/xpcom/ds/nsString.cpp index b1643b24934..abc2cf62e98 100644 --- a/mozilla/xpcom/ds/nsString.cpp +++ b/mozilla/xpcom/ds/nsString.cpp @@ -35,60 +35,6 @@ static const char* kWhitespace="\b\t\r\n "; -#define NOT_USED 0xfffd - -static PRUint16 PA_HackTable[] = { - NOT_USED, - NOT_USED, - 0x201a, /* SINGLE LOW-9 QUOTATION MARK */ - 0x0192, /* LATIN SMALL LETTER F WITH HOOK */ - 0x201e, /* DOUBLE LOW-9 QUOTATION MARK */ - 0x2026, /* HORIZONTAL ELLIPSIS */ - 0x2020, /* DAGGER */ - 0x2021, /* DOUBLE DAGGER */ - 0x02c6, /* MODIFIER LETTER CIRCUMFLEX ACCENT */ - 0x2030, /* PER MILLE SIGN */ - 0x0160, /* LATIN CAPITAL LETTER S WITH CARON */ - 0x2039, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */ - 0x0152, /* LATIN CAPITAL LIGATURE OE */ - NOT_USED, - NOT_USED, - NOT_USED, - - NOT_USED, - 0x2018, /* LEFT SINGLE QUOTATION MARK */ - 0x2019, /* RIGHT SINGLE QUOTATION MARK */ - 0x201c, /* LEFT DOUBLE QUOTATION MARK */ - 0x201d, /* RIGHT DOUBLE QUOTATION MARK */ - 0x2022, /* BULLET */ - 0x2013, /* EN DASH */ - 0x2014, /* EM DASH */ - 0x02dc, /* SMALL TILDE */ - 0x2122, /* TRADE MARK SIGN */ - 0x0161, /* LATIN SMALL LETTER S WITH CARON */ - 0x203a, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */ - 0x0153, /* LATIN SMALL LIGATURE OE */ - NOT_USED, - NOT_USED, - 0x0178 /* LATIN CAPITAL LETTER Y WITH DIAERESIS */ -}; - -static PRUnichar gToUCS2[256]; - -class CTableConstructor { -public: - CTableConstructor(){ - PRUnichar* cp = gToUCS2; - PRInt32 i; - for (i = 0; i < 256; i++) { - *cp++ = PRUnichar(i); - } - cp = gToUCS2; - for (i = 0; i < 32; i++) { - cp[0x80 + i] = PA_HackTable[i]; - } - } -}; static void CSubsume(nsStr& aDest,nsStr& aSource){ @@ -138,7 +84,27 @@ nsCString::nsCString(const char* aCString,PRInt32 aLength,nsIMemoryAgent* anAgen */ nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,eOneByte); - Assign(aString,aLength); + + if(aString && aLength){ + nsStr temp; + Initialize(temp,eTwoByte); + temp.mUStr=(PRUnichar*)aString; + + if(0=aString.mStr){ - theChar=*cp; + char theChar=*cp; if((theChar>='0') && (theChar<='9')){ theDigit=theChar-'0'; } @@ -797,7 +742,7 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { * @param aString: string to be added to this * @return this */ -nsCString& nsCString::Assign(const nsStr& aString,PRInt32 aCount) { +nsCString& nsCString::Assign(const nsCString& aString,PRInt32 aCount) { if(this!=&aString){ nsStr::Truncate(*this,0,0); @@ -824,27 +769,13 @@ nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) { return *this; } -/** - * assign given unichar* to this string - * @update gess 01/04/99 - * @param aString: buffer to be assigned to this - * @return this - */ -nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) { - nsStr::Truncate(*this,0,0); - if(aString){ - Append(aString,aCount); - } - return *this; -} - /** * assign given char to this string * @update gess 01/04/99 * @param aChar: char to be assignd to this * @return this */ -nsCString& nsCString::Assign(char aChar) { +nsCString& nsCString::Assign(PRUnichar aChar) { nsStr::Truncate(*this,0,0); return Append(aChar); } @@ -855,7 +786,7 @@ nsCString& nsCString::Assign(char aChar) { * @param aChar: char to be assignd to this * @return this */ -nsCString& nsCString::Assign(PRUnichar aChar) { +nsCString& nsCString::Assign(char aChar) { nsStr::Truncate(*this,0,0); return Append(aChar); } @@ -886,7 +817,7 @@ nsCString& nsCString::operator=(nsSubsumeCStr& aSubsumeString) { * @param aString : string to be appended to this * @return this */ -nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { +nsCString& nsCString::Append(const nsCString& aString,PRInt32 aCount) { if(aCount<0) aCount=aString.mLength; else aCount=MinInt(aCount,aString.mLength); @@ -899,8 +830,7 @@ nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { * append given string to this string * @update gess 01/04/99 * @param aString : string to be appended to this - * @param aCount -- number of chars to copy; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! + * @param aCount: #of chars to be copied; -1 means to copy the whole thing * @return this */ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) { @@ -909,9 +839,16 @@ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) { Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; + if(00, we don't double check the actual string length! * @return this */ -nsCString& nsCString::Append(const PRUnichar* aString,PRInt32 aCount) { - if(aString){ - nsStr temp; - Initialize(temp,eTwoByte); - temp.mUStr=(PRUnichar*)aString; +nsCString& nsCString::Append(PRUnichar aChar) { + PRUnichar buf[2]={0,0}; + buf[0]=aChar; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; - - if(0=0) && (aChar<128)) { - return Append((char)aChar); - } - return *this; -} - /** * * @update gess 01/04/99 @@ -1073,7 +992,7 @@ PRUint32 nsCString::Right(nsCString& aCopy,PRInt32 aCount) const{ * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsCString& nsCString::Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount) { +nsCString& nsCString::Insert(const nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount) { nsStr::Insert(*this,anOffset,aCopy,0,aCount,mAgent); return *this; @@ -1086,8 +1005,6 @@ nsCString& nsCString::Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount * @update gess4/22/98 * @param aChar char to be inserted into this string * @param anOffset is insert pos in str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! * @return the number of chars inserted into this string */ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ @@ -1096,39 +1013,16 @@ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCou nsStr::Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; - - if(temp.mLength && (00, we don't double check the actual string length! - * @return the number of chars inserted into this string - */ -nsCString& nsCString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aCount){ - if(aString){ - nsStr temp; - nsStr::Initialize(temp,eTwoByte); - temp.mUStr=(PRUnichar*)aString; - - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; + else aCount=temp.mLength=nsCRT::strlen(aCString); if(temp.mLength && (0= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is a valid space character - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -PRBool nsCString::IsSpace(PRUnichar aChar) { - // XXX i18n - if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is valid digit - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */ -PRBool nsCString::IsDigit(PRUnichar aChar) { - // XXX i18n - return PRBool((aChar >= '0') && (aChar <= '9')); -} - /************************************************************** Define the string deallocator class... @@ -1832,7 +1704,7 @@ nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr */ -nsCAutoString::nsCAutoString(CSharedStrBuffer& aBuffer) : nsCString() { +nsCAutoString::nsCAutoString(CBufDescriptor& aBuffer) : nsCString() { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); diff --git a/mozilla/xpcom/ds/nsString.h b/mozilla/xpcom/ds/nsString.h index ae208123ea5..3d1bedb67ca 100644 --- a/mozilla/xpcom/ds/nsString.h +++ b/mozilla/xpcom/ds/nsString.h @@ -17,6 +17,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 nsStr.h + 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() + provided you specify a 00, we don't double check the actual string length! * @return number of chars copied */ -nsCString& Append(const nsStr& aString,PRInt32 aCount); +nsCString& Append(const nsCString& aString,PRInt32 aCount); nsCString& Append(const char* aString,PRInt32 aCount=-1); -nsCString& Append(const PRUnichar* aString,PRInt32 aCount=-1); -nsCString& Append(char aChar); nsCString& Append(PRUnichar aChar); +nsCString& Append(char aChar); nsCString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 nsCString& Append(float aFloat); @@ -491,11 +472,10 @@ PRUint32 Right(nsCString& aCopy,PRInt32 aCount) const; * * @param aCopy -- String to be inserted into this * @param anOffset -- insertion position within this str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! + * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -503,12 +483,9 @@ nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); * * @param aString* to be inserted into this string * @param anOffset is insert pos in str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! * @return the number of chars inserted into this string */ nsCString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); -nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a single char into this string at @@ -519,6 +496,7 @@ nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); * @return the number of chars inserted into this string */ nsCString& Insert(PRUnichar aChar,PRUint32 anOffset); +nsCString& Insert(char aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -636,7 +614,7 @@ virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRI * @return TRUE or FALSE */ PRBool operator==(const nsStr &aString) const; -PRBool operator==(const char *aString) const; +PRBool operator==(const char* aString) const; PRBool operator==(const PRUnichar* aString) const; /** @@ -704,30 +682,6 @@ PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const; -/** - * Determine if given char is a valid space character - * - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -static PRBool IsSpace(PRUnichar ch); - -/** - * Determine if given char in valid alpha range - * - * @param aChar is character to be tested - * @return TRUE if in alpha range - */ -static PRBool IsAlpha(PRUnichar ch); - -/** - * Determine if given char is valid digit - * - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */ -static PRBool IsDigit(PRUnichar ch); - static void Recycle(nsCString* aString); static nsCString* CreateString(void); @@ -752,7 +706,7 @@ public: nsCAutoString(); nsCAutoString(const char* aString,PRInt32 aLength=-1); - nsCAutoString(CSharedStrBuffer& aBuffer); + nsCAutoString(CBufDescriptor& aBuffer); nsCAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsCAutoString(const nsStr& aString); nsCAutoString(const nsCAutoString& aString); @@ -765,11 +719,10 @@ public: nsCAutoString(PRUnichar aChar); virtual ~nsCAutoString(); - nsCAutoString& operator=(const nsStr& aString) {nsCString::Assign(aString); return *this;} - nsCAutoString& operator=(const char* aCString) {nsCString::operator=(aCString); return *this;} - nsCAutoString& operator=(char aChar) {nsCString::operator=(aChar); return *this;} - nsCAutoString& operator=(const PRUnichar* aBuffer) {nsCString::operator=(aBuffer); return *this;} - nsCAutoString& operator=(PRUnichar aChar) {nsCString::operator=(aChar); return *this;} + nsCAutoString& operator=(const nsCString& aString) {nsCString::Assign(aString); return *this;} + nsCAutoString& operator=(const char* aCString) {nsCString::Assign(aCString); return *this;} + nsCAutoString& operator=(PRUnichar aChar) {nsCString::Assign(aChar); return *this;} + nsCAutoString& operator=(char aChar) {nsCString::Assign(aChar); return *this;} /** * Retrieve the size of this string diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index b1643b24934..abc2cf62e98 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -35,60 +35,6 @@ static const char* kWhitespace="\b\t\r\n "; -#define NOT_USED 0xfffd - -static PRUint16 PA_HackTable[] = { - NOT_USED, - NOT_USED, - 0x201a, /* SINGLE LOW-9 QUOTATION MARK */ - 0x0192, /* LATIN SMALL LETTER F WITH HOOK */ - 0x201e, /* DOUBLE LOW-9 QUOTATION MARK */ - 0x2026, /* HORIZONTAL ELLIPSIS */ - 0x2020, /* DAGGER */ - 0x2021, /* DOUBLE DAGGER */ - 0x02c6, /* MODIFIER LETTER CIRCUMFLEX ACCENT */ - 0x2030, /* PER MILLE SIGN */ - 0x0160, /* LATIN CAPITAL LETTER S WITH CARON */ - 0x2039, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */ - 0x0152, /* LATIN CAPITAL LIGATURE OE */ - NOT_USED, - NOT_USED, - NOT_USED, - - NOT_USED, - 0x2018, /* LEFT SINGLE QUOTATION MARK */ - 0x2019, /* RIGHT SINGLE QUOTATION MARK */ - 0x201c, /* LEFT DOUBLE QUOTATION MARK */ - 0x201d, /* RIGHT DOUBLE QUOTATION MARK */ - 0x2022, /* BULLET */ - 0x2013, /* EN DASH */ - 0x2014, /* EM DASH */ - 0x02dc, /* SMALL TILDE */ - 0x2122, /* TRADE MARK SIGN */ - 0x0161, /* LATIN SMALL LETTER S WITH CARON */ - 0x203a, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */ - 0x0153, /* LATIN SMALL LIGATURE OE */ - NOT_USED, - NOT_USED, - 0x0178 /* LATIN CAPITAL LETTER Y WITH DIAERESIS */ -}; - -static PRUnichar gToUCS2[256]; - -class CTableConstructor { -public: - CTableConstructor(){ - PRUnichar* cp = gToUCS2; - PRInt32 i; - for (i = 0; i < 256; i++) { - *cp++ = PRUnichar(i); - } - cp = gToUCS2; - for (i = 0; i < 32; i++) { - cp[0x80 + i] = PA_HackTable[i]; - } - } -}; static void CSubsume(nsStr& aDest,nsStr& aSource){ @@ -138,7 +84,27 @@ nsCString::nsCString(const char* aCString,PRInt32 aLength,nsIMemoryAgent* anAgen */ nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,eOneByte); - Assign(aString,aLength); + + if(aString && aLength){ + nsStr temp; + Initialize(temp,eTwoByte); + temp.mUStr=(PRUnichar*)aString; + + if(0=aString.mStr){ - theChar=*cp; + char theChar=*cp; if((theChar>='0') && (theChar<='9')){ theDigit=theChar-'0'; } @@ -797,7 +742,7 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { * @param aString: string to be added to this * @return this */ -nsCString& nsCString::Assign(const nsStr& aString,PRInt32 aCount) { +nsCString& nsCString::Assign(const nsCString& aString,PRInt32 aCount) { if(this!=&aString){ nsStr::Truncate(*this,0,0); @@ -824,27 +769,13 @@ nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) { return *this; } -/** - * assign given unichar* to this string - * @update gess 01/04/99 - * @param aString: buffer to be assigned to this - * @return this - */ -nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) { - nsStr::Truncate(*this,0,0); - if(aString){ - Append(aString,aCount); - } - return *this; -} - /** * assign given char to this string * @update gess 01/04/99 * @param aChar: char to be assignd to this * @return this */ -nsCString& nsCString::Assign(char aChar) { +nsCString& nsCString::Assign(PRUnichar aChar) { nsStr::Truncate(*this,0,0); return Append(aChar); } @@ -855,7 +786,7 @@ nsCString& nsCString::Assign(char aChar) { * @param aChar: char to be assignd to this * @return this */ -nsCString& nsCString::Assign(PRUnichar aChar) { +nsCString& nsCString::Assign(char aChar) { nsStr::Truncate(*this,0,0); return Append(aChar); } @@ -886,7 +817,7 @@ nsCString& nsCString::operator=(nsSubsumeCStr& aSubsumeString) { * @param aString : string to be appended to this * @return this */ -nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { +nsCString& nsCString::Append(const nsCString& aString,PRInt32 aCount) { if(aCount<0) aCount=aString.mLength; else aCount=MinInt(aCount,aString.mLength); @@ -899,8 +830,7 @@ nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { * append given string to this string * @update gess 01/04/99 * @param aString : string to be appended to this - * @param aCount -- number of chars to copy; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! + * @param aCount: #of chars to be copied; -1 means to copy the whole thing * @return this */ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) { @@ -909,9 +839,16 @@ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) { Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; + if(00, we don't double check the actual string length! * @return this */ -nsCString& nsCString::Append(const PRUnichar* aString,PRInt32 aCount) { - if(aString){ - nsStr temp; - Initialize(temp,eTwoByte); - temp.mUStr=(PRUnichar*)aString; +nsCString& nsCString::Append(PRUnichar aChar) { + PRUnichar buf[2]={0,0}; + buf[0]=aChar; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; - - if(0=0) && (aChar<128)) { - return Append((char)aChar); - } - return *this; -} - /** * * @update gess 01/04/99 @@ -1073,7 +992,7 @@ PRUint32 nsCString::Right(nsCString& aCopy,PRInt32 aCount) const{ * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsCString& nsCString::Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount) { +nsCString& nsCString::Insert(const nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount) { nsStr::Insert(*this,anOffset,aCopy,0,aCount,mAgent); return *this; @@ -1086,8 +1005,6 @@ nsCString& nsCString::Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount * @update gess4/22/98 * @param aChar char to be inserted into this string * @param anOffset is insert pos in str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! * @return the number of chars inserted into this string */ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ @@ -1096,39 +1013,16 @@ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCou nsStr::Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; - - if(temp.mLength && (00, we don't double check the actual string length! - * @return the number of chars inserted into this string - */ -nsCString& nsCString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aCount){ - if(aString){ - nsStr temp; - nsStr::Initialize(temp,eTwoByte); - temp.mUStr=(PRUnichar*)aString; - - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; + else aCount=temp.mLength=nsCRT::strlen(aCString); if(temp.mLength && (0= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is a valid space character - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -PRBool nsCString::IsSpace(PRUnichar aChar) { - // XXX i18n - if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is valid digit - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */ -PRBool nsCString::IsDigit(PRUnichar aChar) { - // XXX i18n - return PRBool((aChar >= '0') && (aChar <= '9')); -} - /************************************************************** Define the string deallocator class... @@ -1832,7 +1704,7 @@ nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr */ -nsCAutoString::nsCAutoString(CSharedStrBuffer& aBuffer) : nsCString() { +nsCAutoString::nsCAutoString(CBufDescriptor& aBuffer) : nsCString() { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); diff --git a/mozilla/xpcom/string/obsolete/nsString.h b/mozilla/xpcom/string/obsolete/nsString.h index ae208123ea5..3d1bedb67ca 100644 --- a/mozilla/xpcom/string/obsolete/nsString.h +++ b/mozilla/xpcom/string/obsolete/nsString.h @@ -17,6 +17,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 nsStr.h + 6. It's ONLY ok to provide non-null-terminated buffers to Append() and Insert() + provided you specify a 00, we don't double check the actual string length! * @return number of chars copied */ -nsCString& Append(const nsStr& aString,PRInt32 aCount); +nsCString& Append(const nsCString& aString,PRInt32 aCount); nsCString& Append(const char* aString,PRInt32 aCount=-1); -nsCString& Append(const PRUnichar* aString,PRInt32 aCount=-1); -nsCString& Append(char aChar); nsCString& Append(PRUnichar aChar); +nsCString& Append(char aChar); nsCString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 nsCString& Append(float aFloat); @@ -491,11 +472,10 @@ PRUint32 Right(nsCString& aCopy,PRInt32 aCount) const; * * @param aCopy -- String to be inserted into this * @param anOffset -- insertion position within this str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! + * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -503,12 +483,9 @@ nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); * * @param aString* to be inserted into this string * @param anOffset is insert pos in str - * @param aCount -- number of chars to insert; -1 tells us to compute the strlen for you - * WARNING: If you provide a count>0, we don't double check the actual string length! * @return the number of chars inserted into this string */ nsCString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); -nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a single char into this string at @@ -519,6 +496,7 @@ nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); * @return the number of chars inserted into this string */ nsCString& Insert(PRUnichar aChar,PRUint32 anOffset); +nsCString& Insert(char aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -636,7 +614,7 @@ virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRI * @return TRUE or FALSE */ PRBool operator==(const nsStr &aString) const; -PRBool operator==(const char *aString) const; +PRBool operator==(const char* aString) const; PRBool operator==(const PRUnichar* aString) const; /** @@ -704,30 +682,6 @@ PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const; -/** - * Determine if given char is a valid space character - * - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -static PRBool IsSpace(PRUnichar ch); - -/** - * Determine if given char in valid alpha range - * - * @param aChar is character to be tested - * @return TRUE if in alpha range - */ -static PRBool IsAlpha(PRUnichar ch); - -/** - * Determine if given char is valid digit - * - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */ -static PRBool IsDigit(PRUnichar ch); - static void Recycle(nsCString* aString); static nsCString* CreateString(void); @@ -752,7 +706,7 @@ public: nsCAutoString(); nsCAutoString(const char* aString,PRInt32 aLength=-1); - nsCAutoString(CSharedStrBuffer& aBuffer); + nsCAutoString(CBufDescriptor& aBuffer); nsCAutoString(const PRUnichar* aString,PRInt32 aLength=-1); nsCAutoString(const nsStr& aString); nsCAutoString(const nsCAutoString& aString); @@ -765,11 +719,10 @@ public: nsCAutoString(PRUnichar aChar); virtual ~nsCAutoString(); - nsCAutoString& operator=(const nsStr& aString) {nsCString::Assign(aString); return *this;} - nsCAutoString& operator=(const char* aCString) {nsCString::operator=(aCString); return *this;} - nsCAutoString& operator=(char aChar) {nsCString::operator=(aChar); return *this;} - nsCAutoString& operator=(const PRUnichar* aBuffer) {nsCString::operator=(aBuffer); return *this;} - nsCAutoString& operator=(PRUnichar aChar) {nsCString::operator=(aChar); return *this;} + nsCAutoString& operator=(const nsCString& aString) {nsCString::Assign(aString); return *this;} + nsCAutoString& operator=(const char* aCString) {nsCString::Assign(aCString); return *this;} + nsCAutoString& operator=(PRUnichar aChar) {nsCString::Assign(aChar); return *this;} + nsCAutoString& operator=(char aChar) {nsCString::Assign(aChar); return *this;} /** * Retrieve the size of this string