From 42847ba510061b530a96697ec3ba947c3a4e877b Mon Sep 17 00:00:00 2001 From: "rickg%netscape.com" Date: Sun, 25 Jul 1999 17:21:14 +0000 Subject: [PATCH] added optional arg validation to improve safety git-svn-id: svn://10.0.0.236/trunk@41052 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/string/obsolete/nsString2.cpp | 193 +++++++++----------- mozilla/string/obsolete/nsString2.h | 45 +++-- mozilla/xpcom/ds/nsString2.cpp | 193 +++++++++----------- mozilla/xpcom/ds/nsString2.h | 45 +++-- mozilla/xpcom/string/obsolete/nsString2.cpp | 193 +++++++++----------- mozilla/xpcom/string/obsolete/nsString2.h | 45 +++-- 6 files changed, 348 insertions(+), 366 deletions(-) diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index 003ab4be9c2..15a7b39081f 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -34,61 +34,6 @@ static const char* kNullPointerError = "Error: unexpected null ptr"; 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 Subsume(nsStr& aDest,nsStr& aSource){ if(aSource.mStr && aSource.mLength) { @@ -291,6 +236,9 @@ PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ if(eOneByte==mCharSize) mStr[anIndex]=char(aChar); else mUStr[anIndex]=aChar; + +// SOON! if(0==aChar) mLength=anIndex; + result=PR_TRUE; } return result; @@ -379,23 +327,6 @@ nsSubsumeStr nsString::operator+(PRUnichar aChar) { Lexomorphic transforms... *********************************************************************/ -/** - * Converts all chars in given string to UCS2 - */ -void nsString::ToUCS2(PRUint32 aStartOffset){ - static CTableConstructor gTableConstructor; - if(aStartOffset0, we don't double check the actual string length! * @return this */ nsString& nsString::Append(const nsStr& aString,PRInt32 aCount) { @@ -971,8 +897,6 @@ nsString& nsString::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! * @return this */ nsString& nsString::Append(const nsString& aString,PRInt32 aCount) { @@ -989,19 +913,25 @@ nsString& nsString::Append(const nsString& aString,PRInt32 aCount) { * @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! * @return this */ nsString& nsString::Append(const char* aCString,PRInt32 aCount) { - if(aCString){ + if(aCString && aCount){ //if astring is null or count==0 there's nothing to do nsStr temp; 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 */ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aCount) { - if(aString){ + if(aString && aCount){ //if astring is null or count==0 there's nothing to do nsStr temp; Initialize(temp,eTwoByte); temp.mUStr=(PRUnichar*)aString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; - + if(00, 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. */ nsString& nsString::Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) { @@ -1184,21 +1119,27 @@ nsString& nsString::Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCoun * @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! + * @param aCounttells us how many chars to insert * @return the number of chars inserted into this string */ nsString& nsString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ - if(aCString){ + if(aCString && aCount){ nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; + if(0255){ + return PR_FALSE; + } + aBuffer++; + } + } + return PR_TRUE; +} + + /** * Determine if given char is valid digit * @@ -2036,7 +2009,7 @@ nsAutoString::nsAutoString(const PRUnichar* aString,eCharSize aCharSize,PRInt32 * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString::nsAutoString(CSharedStrBuffer& aBuffer) : nsString(eTwoByte) { +nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index 583fab9d5ec..10b62236b2a 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -17,6 +17,24 @@ */ +/*********************************************************************** + 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 */ nsString& Append(const nsStr& aString,PRInt32 aCount); @@ -720,6 +732,14 @@ PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; +/** + * Determine if given buffer is plain ascii + * + * @param aBuffer -- if null, then we test *this, otherwise we test given buffer + * @return TRUE if is all ascii chars or if strlen==0 + */ +PRBool IsASCII(const PRUnichar* aBuffer=0); + /** * Determine if given char is a valid space character @@ -771,7 +791,7 @@ public: nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - nsAutoString(CSharedStrBuffer& aBuffer); + nsAutoString(CBufDescriptor& aBuffer); nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); #ifdef AIX @@ -783,11 +803,11 @@ public: virtual ~nsAutoString(); nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} - nsAutoString& operator=(const nsAutoString& aString) {nsString::operator=(aString); return *this;} - nsAutoString& operator=(const char* aCString) {nsString::operator=(aCString); return *this;} - nsAutoString& operator=(char aChar) {nsString::operator=(aChar); return *this;} - nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::operator=(aBuffer); return *this;} - nsAutoString& operator=(PRUnichar aChar) {nsString::operator=(aChar); return *this;} + nsAutoString& operator=(const nsAutoString& aString) {nsString::Assign(aString); return *this;} + nsAutoString& operator=(const char* aCString) {nsString::Assign(aCString); return *this;} + nsAutoString& operator=(char aChar) {nsString::Assign(aChar); return *this;} + nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::Assign(aBuffer); return *this;} + nsAutoString& operator=(PRUnichar aChar) {nsString::Assign(aChar); return *this;} /** * Retrieve the size of this string @@ -799,6 +819,7 @@ public: }; + /*************************************************************** The subsumestr class is very unusual. It differs from a normal string in that it doesn't use normal diff --git a/mozilla/xpcom/ds/nsString2.cpp b/mozilla/xpcom/ds/nsString2.cpp index 003ab4be9c2..15a7b39081f 100644 --- a/mozilla/xpcom/ds/nsString2.cpp +++ b/mozilla/xpcom/ds/nsString2.cpp @@ -34,61 +34,6 @@ static const char* kNullPointerError = "Error: unexpected null ptr"; 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 Subsume(nsStr& aDest,nsStr& aSource){ if(aSource.mStr && aSource.mLength) { @@ -291,6 +236,9 @@ PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ if(eOneByte==mCharSize) mStr[anIndex]=char(aChar); else mUStr[anIndex]=aChar; + +// SOON! if(0==aChar) mLength=anIndex; + result=PR_TRUE; } return result; @@ -379,23 +327,6 @@ nsSubsumeStr nsString::operator+(PRUnichar aChar) { Lexomorphic transforms... *********************************************************************/ -/** - * Converts all chars in given string to UCS2 - */ -void nsString::ToUCS2(PRUint32 aStartOffset){ - static CTableConstructor gTableConstructor; - if(aStartOffset0, we don't double check the actual string length! * @return this */ nsString& nsString::Append(const nsStr& aString,PRInt32 aCount) { @@ -971,8 +897,6 @@ nsString& nsString::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! * @return this */ nsString& nsString::Append(const nsString& aString,PRInt32 aCount) { @@ -989,19 +913,25 @@ nsString& nsString::Append(const nsString& aString,PRInt32 aCount) { * @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! * @return this */ nsString& nsString::Append(const char* aCString,PRInt32 aCount) { - if(aCString){ + if(aCString && aCount){ //if astring is null or count==0 there's nothing to do nsStr temp; 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 */ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aCount) { - if(aString){ + if(aString && aCount){ //if astring is null or count==0 there's nothing to do nsStr temp; Initialize(temp,eTwoByte); temp.mUStr=(PRUnichar*)aString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; - + if(00, 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. */ nsString& nsString::Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) { @@ -1184,21 +1119,27 @@ nsString& nsString::Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCoun * @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! + * @param aCounttells us how many chars to insert * @return the number of chars inserted into this string */ nsString& nsString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ - if(aCString){ + if(aCString && aCount){ nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; + if(0255){ + return PR_FALSE; + } + aBuffer++; + } + } + return PR_TRUE; +} + + /** * Determine if given char is valid digit * @@ -2036,7 +2009,7 @@ nsAutoString::nsAutoString(const PRUnichar* aString,eCharSize aCharSize,PRInt32 * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString::nsAutoString(CSharedStrBuffer& aBuffer) : nsString(eTwoByte) { +nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); diff --git a/mozilla/xpcom/ds/nsString2.h b/mozilla/xpcom/ds/nsString2.h index 583fab9d5ec..10b62236b2a 100644 --- a/mozilla/xpcom/ds/nsString2.h +++ b/mozilla/xpcom/ds/nsString2.h @@ -17,6 +17,24 @@ */ +/*********************************************************************** + 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 */ nsString& Append(const nsStr& aString,PRInt32 aCount); @@ -720,6 +732,14 @@ PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; +/** + * Determine if given buffer is plain ascii + * + * @param aBuffer -- if null, then we test *this, otherwise we test given buffer + * @return TRUE if is all ascii chars or if strlen==0 + */ +PRBool IsASCII(const PRUnichar* aBuffer=0); + /** * Determine if given char is a valid space character @@ -771,7 +791,7 @@ public: nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - nsAutoString(CSharedStrBuffer& aBuffer); + nsAutoString(CBufDescriptor& aBuffer); nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); #ifdef AIX @@ -783,11 +803,11 @@ public: virtual ~nsAutoString(); nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} - nsAutoString& operator=(const nsAutoString& aString) {nsString::operator=(aString); return *this;} - nsAutoString& operator=(const char* aCString) {nsString::operator=(aCString); return *this;} - nsAutoString& operator=(char aChar) {nsString::operator=(aChar); return *this;} - nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::operator=(aBuffer); return *this;} - nsAutoString& operator=(PRUnichar aChar) {nsString::operator=(aChar); return *this;} + nsAutoString& operator=(const nsAutoString& aString) {nsString::Assign(aString); return *this;} + nsAutoString& operator=(const char* aCString) {nsString::Assign(aCString); return *this;} + nsAutoString& operator=(char aChar) {nsString::Assign(aChar); return *this;} + nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::Assign(aBuffer); return *this;} + nsAutoString& operator=(PRUnichar aChar) {nsString::Assign(aChar); return *this;} /** * Retrieve the size of this string @@ -799,6 +819,7 @@ public: }; + /*************************************************************** The subsumestr class is very unusual. It differs from a normal string in that it doesn't use normal diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index 003ab4be9c2..15a7b39081f 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -34,61 +34,6 @@ static const char* kNullPointerError = "Error: unexpected null ptr"; 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 Subsume(nsStr& aDest,nsStr& aSource){ if(aSource.mStr && aSource.mLength) { @@ -291,6 +236,9 @@ PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ if(eOneByte==mCharSize) mStr[anIndex]=char(aChar); else mUStr[anIndex]=aChar; + +// SOON! if(0==aChar) mLength=anIndex; + result=PR_TRUE; } return result; @@ -379,23 +327,6 @@ nsSubsumeStr nsString::operator+(PRUnichar aChar) { Lexomorphic transforms... *********************************************************************/ -/** - * Converts all chars in given string to UCS2 - */ -void nsString::ToUCS2(PRUint32 aStartOffset){ - static CTableConstructor gTableConstructor; - if(aStartOffset0, we don't double check the actual string length! * @return this */ nsString& nsString::Append(const nsStr& aString,PRInt32 aCount) { @@ -971,8 +897,6 @@ nsString& nsString::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! * @return this */ nsString& nsString::Append(const nsString& aString,PRInt32 aCount) { @@ -989,19 +913,25 @@ nsString& nsString::Append(const nsString& aString,PRInt32 aCount) { * @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! * @return this */ nsString& nsString::Append(const char* aCString,PRInt32 aCount) { - if(aCString){ + if(aCString && aCount){ //if astring is null or count==0 there's nothing to do nsStr temp; 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 */ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aCount) { - if(aString){ + if(aString && aCount){ //if astring is null or count==0 there's nothing to do nsStr temp; Initialize(temp,eTwoByte); temp.mUStr=(PRUnichar*)aString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aString); - else temp.mLength=aCount; - + if(00, 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. */ nsString& nsString::Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) { @@ -1184,21 +1119,27 @@ nsString& nsString::Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCoun * @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! + * @param aCounttells us how many chars to insert * @return the number of chars inserted into this string */ nsString& nsString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ - if(aCString){ + if(aCString && aCount){ nsStr temp; nsStr::Initialize(temp,eOneByte); temp.mStr=(char*)aCString; - if(aCount<0) - aCount=temp.mLength=nsCRT::strlen(aCString); - else temp.mLength=aCount; + if(0255){ + return PR_FALSE; + } + aBuffer++; + } + } + return PR_TRUE; +} + + /** * Determine if given char is valid digit * @@ -2036,7 +2009,7 @@ nsAutoString::nsAutoString(const PRUnichar* aString,eCharSize aCharSize,PRInt32 * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString::nsAutoString(CSharedStrBuffer& aBuffer) : nsString(eTwoByte) { +nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) { mAgent=0; if(!aBuffer.mBuffer) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index 583fab9d5ec..10b62236b2a 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -17,6 +17,24 @@ */ +/*********************************************************************** + 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 */ nsString& Append(const nsStr& aString,PRInt32 aCount); @@ -720,6 +732,14 @@ PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; +/** + * Determine if given buffer is plain ascii + * + * @param aBuffer -- if null, then we test *this, otherwise we test given buffer + * @return TRUE if is all ascii chars or if strlen==0 + */ +PRBool IsASCII(const PRUnichar* aBuffer=0); + /** * Determine if given char is a valid space character @@ -771,7 +791,7 @@ public: nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - nsAutoString(CSharedStrBuffer& aBuffer); + nsAutoString(CBufDescriptor& aBuffer); nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); #ifdef AIX @@ -783,11 +803,11 @@ public: virtual ~nsAutoString(); nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} - nsAutoString& operator=(const nsAutoString& aString) {nsString::operator=(aString); return *this;} - nsAutoString& operator=(const char* aCString) {nsString::operator=(aCString); return *this;} - nsAutoString& operator=(char aChar) {nsString::operator=(aChar); return *this;} - nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::operator=(aBuffer); return *this;} - nsAutoString& operator=(PRUnichar aChar) {nsString::operator=(aChar); return *this;} + nsAutoString& operator=(const nsAutoString& aString) {nsString::Assign(aString); return *this;} + nsAutoString& operator=(const char* aCString) {nsString::Assign(aCString); return *this;} + nsAutoString& operator=(char aChar) {nsString::Assign(aChar); return *this;} + nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::Assign(aBuffer); return *this;} + nsAutoString& operator=(PRUnichar aChar) {nsString::Assign(aChar); return *this;} /** * Retrieve the size of this string @@ -799,6 +819,7 @@ public: }; + /*************************************************************** The subsumestr class is very unusual. It differs from a normal string in that it doesn't use normal