bug18337 and fixes for embedded nulls; r=harishd

This commit is contained in:
rickg%netscape.com 1999-11-14 06:22:52 +00:00
parent 1826780418
commit bd46d8d5f0
20 changed files with 420 additions and 329 deletions

View File

@ -43,6 +43,7 @@
//static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1.";
static const PRUnichar gCommonEmptyBuffer[1] = {0};
static nsresult gStringAcquiredMemory = NS_OK;
/**
* This method initializes all the members of the nsStr structure
@ -303,10 +304,15 @@ void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) {
}
/**
*
* @update gess1/7/99
* @param
* @return
* This method removes characters from the given set from this string.
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
*
* @update gess 11/7/99
* @param aDest
* @param aSet
* @param aEliminateLeading
* @param aEliminateTrailing
* @return nothing
*/
void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
@ -622,12 +628,12 @@ PRBool nsStr::Alloc(nsStr& aDest,PRUint32 aCount) {
PRUint32 theSize=(theNewCapacity<<aDest.mCharSize);
aDest.mStr = (char*)nsAllocator::Alloc(theSize);
PRBool result=PR_FALSE;
if(aDest.mStr) {
aDest.mOwnsBuffer=1;
result=PR_TRUE;
gStringAcquiredMemory=PR_TRUE;
}
return result;
else gStringAcquiredMemory=PR_FALSE;
return gStringAcquiredMemory;
}
PRBool nsStr::Free(nsStr& aDest){
@ -657,6 +663,16 @@ PRBool nsStr::Realloc(nsStr& aDest,PRUint32 aCount){
return result;
}
/**
* Retrieve last memory error
*
* @update gess 10/11/99
* @return memory error (usually returns NS_OK)
*/
PRBool nsStr::DidAcquireMemory(void) {
return gStringAcquiredMemory;
}
//----------------------------------------------------------------------------------------
CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) {

View File

@ -45,17 +45,26 @@
/***********************************************************************
ASSUMPTIONS:
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
1. nsStrings and nsAutoString are always null terminated. However,
since it maintains a length byte, you can store NULL's inside
the string. Just be careful passing such buffers to 3rd party
API's that assume that NULL always terminate the buffer.
2. nsCStrings can be upsampled into nsString without data loss
3. 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!).
4. 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 0<n value for the optional count argument.
7. Downsampling from nsString to nsCString is lossy -- avoid it if possible!
8. Calls to ToNewCString() and ToNewUnicode() should be matched with calls to Recycle().
5. If you don't provide the optional count argument to Append() and Insert(),
the method will assume that the given buffer is terminated by the first
NULL it encounters.
6. Downsampling from nsString to nsCString can be lossy -- avoid it if possible!
7. Calls to ToNewCString() and ToNewUnicode() should be matched with calls to Recycle().
***********************************************************************/
@ -399,6 +408,8 @@ struct NS_COM nsStr {
static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset);
static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset);
static PRBool DidAcquireMemory(void);
PRUint32 mLength;
PRUint32 mCapacity;

View File

@ -87,26 +87,7 @@ nsCString::nsCString(const char* aCString,PRInt32 aLength) {
*/
nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength) {
nsStr::Initialize(*this,eOneByte);
if(aString && aLength){
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aLength) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aLength;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<(PRInt32)mLength)) {
aLength=temp.mLength=pos;
}
}
else aLength=temp.mLength=nsCRT::strlen(aString);
if(0<aLength)
nsStr::Append(*this,temp,0,aLength);
}
Assign(aString,aLength);
}
/**
@ -248,12 +229,12 @@ PRBool nsCString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
PRBool result=PR_FALSE;
if(anIndex<mLength){
mStr[anIndex]=(char)aChar;
// SOON! if(0==aChar) mLength=anIndex;
result=PR_TRUE;
}
return result;
}
/*********************************************************
append (operator+) METHODS....
*********************************************************/
@ -803,19 +784,21 @@ nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) {
nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) {
nsStr::Truncate(*this,0);
if(aString){
if(aString && aCount){
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in Assign(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -825,6 +808,7 @@ nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) {
return *this;
}
/**
* assign given unichar to this string
* @update gess 01/04/99
@ -913,13 +897,15 @@ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) {
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Append(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1099,13 +1085,15 @@ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCou
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Insert(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);

View File

@ -398,8 +398,9 @@ public:
*/
nsCString& operator+=(const nsCString& aString){return Append(aString,aString.mLength);}
nsCString& operator+=(const char* aCString) {return Append(aCString);}
nsCString& operator+=(PRUnichar aChar){return Append(aChar);}
nsCString& operator+=(char aChar){return Append(aChar);}
nsCString& operator+=(const PRUnichar aChar){return Append(aChar);}
nsCString& operator+=(const char aChar){return Append(aChar);}
nsCString& operator+=(const int anInt){return Append(anInt,10);}
/*
* Appends n characters from given string to this,

View File

@ -72,9 +72,9 @@ nsString::nsString() {
* @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){
nsString::nsString(const char* aCString,PRInt32 aCount){
nsStr::Initialize(*this,eTwoByte);
Assign(aCString);
Assign(aCString,aCount);
}
/**
@ -83,9 +83,9 @@ nsString::nsString(const char* aCString){
* @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) {
nsString::nsString(const PRUnichar* aString,PRInt32 aCount) {
nsStr::Initialize(*this,eTwoByte);
Assign(aString);
Assign(aString,aCount);
}
/**
@ -270,6 +270,7 @@ PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
return result;
}
/*********************************************************
append (operator+) METHODS....
*********************************************************/
@ -1037,13 +1038,15 @@ nsString& nsString::Append(const char* aCString,PRInt32 aCount) {
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in append(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1067,13 +1070,15 @@ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aCount) {
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in append(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -1255,13 +1260,15 @@ nsString& nsString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Insert(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1288,13 +1295,15 @@ nsString& nsString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aC
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in Insert(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -1699,7 +1708,9 @@ PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount
if(aCString) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aCString);
temp.mLength= (0<aCount) ? aCount : nsCRT::strlen(aCString);
temp.mStr=(char*)aCString;
return nsStr::Compare(*this,temp,aCount,aIgnoreCase);
}
@ -1792,6 +1803,7 @@ PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) cons
return Equals(s1,s2,PR_TRUE);
}
/**
* Compare this to given string; note that we compare full strings here.
*

View File

@ -69,13 +69,13 @@ public:
* This constructor accepts an isolatin string
* @param aCString is a ptr to a 1-byte cstr
*/
nsString(const char* aCString);
nsString(const char* aCString,PRInt32 aCount=-1);
/**
* This constructor accepts a unichar string
* @param aCString is a ptr to a 2-byte cstr
*/
nsString(const PRUnichar* aString);
nsString(const PRUnichar* aString,PRInt32 aCount=-1);
/**
* This is a copy constructor that accepts an nsStr
@ -442,9 +442,10 @@ public:
nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);}
nsString& operator+=(const nsString& aString){return Append(aString,aString.mLength);}
nsString& operator+=(const char* aCString) {return Append(aCString);}
//nsString& operator+=(char aChar){return Append(aChar);}
nsString& operator+=(const char aChar) {return Append(PRUnichar(unsigned char(aChar)));}
nsString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);}
nsString& operator+=(PRUnichar aChar){return Append(aChar);}
nsString& operator+=(const PRUnichar aChar){return Append(aChar);}
nsString& operator+=(const int anInt){return Append(anInt,10);}
/*
* Appends n characters from given string to this,

View File

@ -400,7 +400,7 @@ PRInt32 Compare1To1(const char* aStr1,const char* aStr2,PRUint32 aCount,PRBool a
PRInt32 result=0;
if(aIgnoreCase)
result=nsCRT::strncasecmp(aStr1,aStr2,aCount);
else result=strncmp(aStr1,aStr2,aCount);
else result=memcmp(aStr1,aStr2,aCount);
return result;
}
@ -618,17 +618,16 @@ CaseConverters gCaseConverters[]={&ConvertCase1,&ConvertCase2};
*/
PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet){
typedef char chartype;
chartype* from = aString;
chartype* end = aString + aLength-1;
chartype* to = from;
char* from = aString;
char* end = aString + aLength-1;
char* to = from;
//this code converts /n, /t, /r into normal space ' ';
//it also compresses runs of whitespace down to a single char...
if(aSet && aString && (0 < aLength)){
PRUint32 aSetLen=strlen(aSet);
while (from <= end) {
chartype theChar = *from++;
char theChar = *from++;
if(kNotFound!=FindChar1(aSet,aSetLen,0,theChar,PR_FALSE)){
*to++=theChar;
while (from <= end) {
@ -644,7 +643,7 @@ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet){
}
*to = 0;
}
return to - (chartype*)aString;
return to - (char*)aString;
}
@ -661,18 +660,17 @@ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet){
*/
PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet){
typedef PRUnichar chartype;
chartype* from = (chartype*)aString;
chartype* end = from + aLength-1;
chartype* to = from;
PRUnichar* from = (PRUnichar*)aString;
PRUnichar* end = from + aLength-1;
PRUnichar* to = from;
//this code converts /n, /t, /r into normal space ' ';
//it also compresses runs of whitespace down to a single char...
if(aSet && aString && (0 < aLength)){
PRUint32 aSetLen=strlen(aSet);
while (from <= end) {
chartype theChar = *from++;
if(kNotFound!=FindChar1(aSet,aSetLen,0,theChar,PR_FALSE)){
PRUnichar theChar = *from++;
if((255<theChar) || (kNotFound!=FindChar1(aSet,aSetLen,0,theChar,PR_FALSE))){
*to++=theChar;
while (from <= end) {
theChar = *from++;
@ -687,7 +685,7 @@ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet){
}
*to = 0;
}
return to - (chartype*)aString;
return to - (PRUnichar*)aString;
}
typedef PRInt32 (*CompressChars)(char* aString,PRUint32 aCount,const char* aSet);
@ -706,22 +704,21 @@ CompressChars gCompressChars[]={&CompressChars1,&CompressChars2};
*/
PRInt32 StripChars1(char* aString,PRUint32 aLength,const char* aSet){
typedef char chartype;
chartype* to = aString;
chartype* from = aString-1;
chartype* end = aString + aLength;
char* to = aString;
char* from = aString-1;
char* end = aString + aLength;
if(aSet && aString && (0 < aLength)){
PRUint32 aSetLen=strlen(aSet);
while (++from < end) {
chartype theChar = *from;
char theChar = *from;
if(kNotFound==FindChar1(aSet,aSetLen,0,theChar,PR_FALSE)){
*to++ = theChar;
}
}
*to = 0;
}
return to - (chartype*)aString;
return to - (char*)aString;
}
@ -738,22 +735,24 @@ PRInt32 StripChars1(char* aString,PRUint32 aLength,const char* aSet){
*/
PRInt32 StripChars2(char* aString,PRUint32 aLength,const char* aSet){
typedef PRUnichar chartype;
chartype* to = (chartype*)aString;
chartype* from = (chartype*)aString-1;
chartype* end = to + aLength;
PRUnichar* to = (PRUnichar*)aString;
PRUnichar* from = (PRUnichar*)aString-1;
PRUnichar* end = to + aLength;
if(aSet && aString && (0 < aLength)){
PRUint32 aSetLen=strlen(aSet);
while (++from < end) {
chartype theChar = *from;
if(kNotFound==FindChar1(aSet,aSetLen,0,theChar,PR_FALSE)){
PRUnichar theChar = *from;
//Note the test for ascii range below. If you have a real unicode char,
//and you're searching for chars in the (given) ascii string, there's no
//point in doing the real search since it's out of the ascii range.
if((255<theChar) || (kNotFound==FindChar1(aSet,aSetLen,0,theChar,PR_FALSE))){
*to++ = theChar;
}
}
*to = 0;
}
return to - (chartype*)aString;
return to - (PRUnichar*)aString;
}
typedef PRInt32 (*StripChars)(char* aString,PRUint32 aCount,const char* aSet);

View File

@ -236,12 +236,13 @@ PRUint32 nsCRT::strlen(const PRUnichar* s)
/**
* Compare unichar string ptrs, stopping at the 1st null
* NOTE: If both are null, we return 0.
* @update gess7/30/98
* NOTE: We terminate the search upon encountering a NULL
*
* @update gess 11/10/99
* @param s1 and s2 both point to unichar strings
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strcmp(const PRUnichar* s1, const PRUnichar* s2)
{
PRInt32 nsCRT::strcmp(const PRUnichar* s1, const PRUnichar* s2) {
if(s1 && s2) {
for (;;) {
PRUnichar c1 = *s1++;
@ -259,12 +260,13 @@ PRInt32 nsCRT::strcmp(const PRUnichar* s1, const PRUnichar* s2)
/**
* Compare unichar string ptrs, stopping at the 1st null or nth char.
* NOTE: If either is null, we return 0.
* @update gess7/30/98
* NOTE: We DO NOT terminate the search upon encountering NULL's before N
*
* @update gess 11/10/99
* @param s1 and s2 both point to unichar strings
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strncmp(const PRUnichar* s1, const PRUnichar* s2, PRUint32 n)
{
PRInt32 nsCRT::strncmp(const PRUnichar* s1, const PRUnichar* s2, PRUint32 n) {
if(s1 && s2) {
if(n != 0) {
do {
@ -274,23 +276,22 @@ PRInt32 nsCRT::strncmp(const PRUnichar* s1, const PRUnichar* s2, PRUint32 n)
if (c1 < c2) return -1;
return 1;
}
if ((0==c1) || (0==c2)) break;
} while (--n != 0);
}
}
return 0;
}
/**
* Compare unichar string ptrs without regard to case
* NOTE: If both are null, we return 0.
* @update gess7/30/98
* NOTE: We terminate the search upon encountering NULL
*
* @update gess 11/10/99
* @param s1 and s2 both point to unichar strings
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strcasecmp(const PRUnichar* s1, const PRUnichar* s2)
{
PRInt32 nsCRT::strcasecmp(const PRUnichar* s1, const PRUnichar* s2) {
if(s1 && s2) {
for (;;) {
PRUnichar c1 = *s1++;
@ -313,12 +314,13 @@ PRInt32 nsCRT::strcasecmp(const PRUnichar* s1, const PRUnichar* s2)
* Compare unichar string ptrs, stopping at the 1st null or nth char;
* also ignoring the case of characters.
* NOTE: If both are null, we return 0.
* @update gess7/30/98
* NOTE: We DO NOT terminate the search upon encountering NULL's before N
*
* @update gess 11/10/99
* @param s1 and s2 both point to unichar strings
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strncasecmp(const PRUnichar* s1, const PRUnichar* s2, PRUint32 n)
{
PRInt32 nsCRT::strncasecmp(const PRUnichar* s1, const PRUnichar* s2, PRUint32 n) {
if(s1 && s2) {
if(n != 0){
do {
@ -332,7 +334,6 @@ PRInt32 nsCRT::strncasecmp(const PRUnichar* s1, const PRUnichar* s2, PRUint32 n)
return 1;
}
}
if ((0==c1) || (0==c2)) break;
} while (--n != 0);
}
}
@ -343,13 +344,14 @@ PRInt32 nsCRT::strncasecmp(const PRUnichar* s1, const PRUnichar* s2, PRUint32 n)
/**
* Compare a unichar string ptr to cstring.
* NOTE: If both are null, we return 0.
* NOTE: We terminate the search upon encountering NULL's
*
* @update gess7/30/98
* @param s1 points to unichar string
* @param s2 points to cstring
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strcmp(const PRUnichar* s1, const char* s2)
{
PRInt32 nsCRT::strcmp(const PRUnichar* s1, const char* s2) {
if(s1 && s2) {
for (;;) {
PRUnichar c1 = *s1++;
@ -368,15 +370,16 @@ PRInt32 nsCRT::strcmp(const PRUnichar* s1, const char* s2)
/**
* Compare a unichar string ptr to cstring, up to N chars.
* NOTE: If both are null, we return 0.
* @update gess7/30/98
* NOTE: We DO NOT terminate the search upon encountering NULL's before N
*
* @update gess 11/10/99
* @param s1 points to unichar string
* @param s2 points to cstring
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strncmp(const PRUnichar* s1, const char* s2, PRUint32 n)
{
PRInt32 nsCRT::strncmp(const PRUnichar* s1, const char* s2, PRUint32 n) {
if(s1 && s2) {
if(n != 0){
if(0<n){
do {
PRUnichar c1 = *s1++;
PRUnichar c2 = kIsoLatin1ToUCS2[*(const unsigned char*)s2++];
@ -384,23 +387,24 @@ PRInt32 nsCRT::strncmp(const PRUnichar* s1, const char* s2, PRUint32 n)
if (c1 < c2) return -1;
return 1;
}
if ((0==c1) || (0==c2)) break;
} while (--n != 0);
}
}
return 0;
}
/**
* Compare a unichar string ptr to cstring without regard to case
* NOTE: If both are null, we return 0.
* @update gess7/30/98
* NOTE: We terminate the search upon encountering NULL's
*
* @update gess 11/10/99
* @param s1 points to unichar string
* @param s2 points to cstring
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strcasecmp(const PRUnichar* s1, const char* s2)
{
PRInt32 nsCRT::strcasecmp(const PRUnichar* s1, const char* s2) {
if(s1 && s2) {
for (;;) {
PRUnichar c1 = *s1++;
@ -422,13 +426,14 @@ PRInt32 nsCRT::strcasecmp(const PRUnichar* s1, const char* s2)
/**
* Caseless compare up to N chars between unichar string ptr to cstring.
* NOTE: If both are null, we return 0.
* @update gess7/30/98
* NOTE: We DO NOT terminate the search upon encountering NULL's before N
*
* @update gess 11/10/99
* @param s1 points to unichar string
* @param s2 points to cstring
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
*/
PRInt32 nsCRT::strncasecmp(const PRUnichar* s1, const char* s2, PRUint32 n)
{
PRInt32 nsCRT::strncasecmp(const PRUnichar* s1, const char* s2, PRUint32 n) {
if(s1 && s2){
if(n != 0){
do {
@ -442,13 +447,13 @@ PRInt32 nsCRT::strncasecmp(const PRUnichar* s1, const char* s2, PRUint32 n)
return 1;
}
}
if (c1 == 0) break;
} while (--n != 0);
}
}
return 0;
}
PRUnichar* nsCRT::strdup(const PRUnichar* str)
{
PRUint32 len = nsCRT::strlen(str) + 1; // add one for null

View File

@ -43,6 +43,7 @@
//static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1.";
static const PRUnichar gCommonEmptyBuffer[1] = {0};
static nsresult gStringAcquiredMemory = NS_OK;
/**
* This method initializes all the members of the nsStr structure
@ -303,10 +304,15 @@ void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) {
}
/**
*
* @update gess1/7/99
* @param
* @return
* This method removes characters from the given set from this string.
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
*
* @update gess 11/7/99
* @param aDest
* @param aSet
* @param aEliminateLeading
* @param aEliminateTrailing
* @return nothing
*/
void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
@ -622,12 +628,12 @@ PRBool nsStr::Alloc(nsStr& aDest,PRUint32 aCount) {
PRUint32 theSize=(theNewCapacity<<aDest.mCharSize);
aDest.mStr = (char*)nsAllocator::Alloc(theSize);
PRBool result=PR_FALSE;
if(aDest.mStr) {
aDest.mOwnsBuffer=1;
result=PR_TRUE;
gStringAcquiredMemory=PR_TRUE;
}
return result;
else gStringAcquiredMemory=PR_FALSE;
return gStringAcquiredMemory;
}
PRBool nsStr::Free(nsStr& aDest){
@ -657,6 +663,16 @@ PRBool nsStr::Realloc(nsStr& aDest,PRUint32 aCount){
return result;
}
/**
* Retrieve last memory error
*
* @update gess 10/11/99
* @return memory error (usually returns NS_OK)
*/
PRBool nsStr::DidAcquireMemory(void) {
return gStringAcquiredMemory;
}
//----------------------------------------------------------------------------------------
CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) {

View File

@ -45,17 +45,26 @@
/***********************************************************************
ASSUMPTIONS:
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
1. nsStrings and nsAutoString are always null terminated. However,
since it maintains a length byte, you can store NULL's inside
the string. Just be careful passing such buffers to 3rd party
API's that assume that NULL always terminate the buffer.
2. nsCStrings can be upsampled into nsString without data loss
3. 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!).
4. 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 0<n value for the optional count argument.
7. Downsampling from nsString to nsCString is lossy -- avoid it if possible!
8. Calls to ToNewCString() and ToNewUnicode() should be matched with calls to Recycle().
5. If you don't provide the optional count argument to Append() and Insert(),
the method will assume that the given buffer is terminated by the first
NULL it encounters.
6. Downsampling from nsString to nsCString can be lossy -- avoid it if possible!
7. Calls to ToNewCString() and ToNewUnicode() should be matched with calls to Recycle().
***********************************************************************/
@ -399,6 +408,8 @@ struct NS_COM nsStr {
static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset);
static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset);
static PRBool DidAcquireMemory(void);
PRUint32 mLength;
PRUint32 mCapacity;

View File

@ -87,26 +87,7 @@ nsCString::nsCString(const char* aCString,PRInt32 aLength) {
*/
nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength) {
nsStr::Initialize(*this,eOneByte);
if(aString && aLength){
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aLength) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aLength;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<(PRInt32)mLength)) {
aLength=temp.mLength=pos;
}
}
else aLength=temp.mLength=nsCRT::strlen(aString);
if(0<aLength)
nsStr::Append(*this,temp,0,aLength);
}
Assign(aString,aLength);
}
/**
@ -248,12 +229,12 @@ PRBool nsCString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
PRBool result=PR_FALSE;
if(anIndex<mLength){
mStr[anIndex]=(char)aChar;
// SOON! if(0==aChar) mLength=anIndex;
result=PR_TRUE;
}
return result;
}
/*********************************************************
append (operator+) METHODS....
*********************************************************/
@ -803,19 +784,21 @@ nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) {
nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) {
nsStr::Truncate(*this,0);
if(aString){
if(aString && aCount){
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in Assign(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -825,6 +808,7 @@ nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) {
return *this;
}
/**
* assign given unichar to this string
* @update gess 01/04/99
@ -913,13 +897,15 @@ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) {
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Append(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1099,13 +1085,15 @@ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCou
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Insert(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);

View File

@ -398,8 +398,9 @@ public:
*/
nsCString& operator+=(const nsCString& aString){return Append(aString,aString.mLength);}
nsCString& operator+=(const char* aCString) {return Append(aCString);}
nsCString& operator+=(PRUnichar aChar){return Append(aChar);}
nsCString& operator+=(char aChar){return Append(aChar);}
nsCString& operator+=(const PRUnichar aChar){return Append(aChar);}
nsCString& operator+=(const char aChar){return Append(aChar);}
nsCString& operator+=(const int anInt){return Append(anInt,10);}
/*
* Appends n characters from given string to this,

View File

@ -72,9 +72,9 @@ nsString::nsString() {
* @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){
nsString::nsString(const char* aCString,PRInt32 aCount){
nsStr::Initialize(*this,eTwoByte);
Assign(aCString);
Assign(aCString,aCount);
}
/**
@ -83,9 +83,9 @@ nsString::nsString(const char* aCString){
* @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) {
nsString::nsString(const PRUnichar* aString,PRInt32 aCount) {
nsStr::Initialize(*this,eTwoByte);
Assign(aString);
Assign(aString,aCount);
}
/**
@ -270,6 +270,7 @@ PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
return result;
}
/*********************************************************
append (operator+) METHODS....
*********************************************************/
@ -1037,13 +1038,15 @@ nsString& nsString::Append(const char* aCString,PRInt32 aCount) {
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in append(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1067,13 +1070,15 @@ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aCount) {
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in append(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -1255,13 +1260,15 @@ nsString& nsString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Insert(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1288,13 +1295,15 @@ nsString& nsString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aC
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in Insert(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -1699,7 +1708,9 @@ PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount
if(aCString) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aCString);
temp.mLength= (0<aCount) ? aCount : nsCRT::strlen(aCString);
temp.mStr=(char*)aCString;
return nsStr::Compare(*this,temp,aCount,aIgnoreCase);
}
@ -1792,6 +1803,7 @@ PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) cons
return Equals(s1,s2,PR_TRUE);
}
/**
* Compare this to given string; note that we compare full strings here.
*

View File

@ -69,13 +69,13 @@ public:
* This constructor accepts an isolatin string
* @param aCString is a ptr to a 1-byte cstr
*/
nsString(const char* aCString);
nsString(const char* aCString,PRInt32 aCount=-1);
/**
* This constructor accepts a unichar string
* @param aCString is a ptr to a 2-byte cstr
*/
nsString(const PRUnichar* aString);
nsString(const PRUnichar* aString,PRInt32 aCount=-1);
/**
* This is a copy constructor that accepts an nsStr
@ -442,9 +442,10 @@ public:
nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);}
nsString& operator+=(const nsString& aString){return Append(aString,aString.mLength);}
nsString& operator+=(const char* aCString) {return Append(aCString);}
//nsString& operator+=(char aChar){return Append(aChar);}
nsString& operator+=(const char aChar) {return Append(PRUnichar(unsigned char(aChar)));}
nsString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);}
nsString& operator+=(PRUnichar aChar){return Append(aChar);}
nsString& operator+=(const PRUnichar aChar){return Append(aChar);}
nsString& operator+=(const int anInt){return Append(anInt,10);}
/*
* Appends n characters from given string to this,

View File

@ -43,6 +43,7 @@
//static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1.";
static const PRUnichar gCommonEmptyBuffer[1] = {0};
static nsresult gStringAcquiredMemory = NS_OK;
/**
* This method initializes all the members of the nsStr structure
@ -303,10 +304,15 @@ void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) {
}
/**
*
* @update gess1/7/99
* @param
* @return
* This method removes characters from the given set from this string.
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
*
* @update gess 11/7/99
* @param aDest
* @param aSet
* @param aEliminateLeading
* @param aEliminateTrailing
* @return nothing
*/
void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
@ -622,12 +628,12 @@ PRBool nsStr::Alloc(nsStr& aDest,PRUint32 aCount) {
PRUint32 theSize=(theNewCapacity<<aDest.mCharSize);
aDest.mStr = (char*)nsAllocator::Alloc(theSize);
PRBool result=PR_FALSE;
if(aDest.mStr) {
aDest.mOwnsBuffer=1;
result=PR_TRUE;
gStringAcquiredMemory=PR_TRUE;
}
return result;
else gStringAcquiredMemory=PR_FALSE;
return gStringAcquiredMemory;
}
PRBool nsStr::Free(nsStr& aDest){
@ -657,6 +663,16 @@ PRBool nsStr::Realloc(nsStr& aDest,PRUint32 aCount){
return result;
}
/**
* Retrieve last memory error
*
* @update gess 10/11/99
* @return memory error (usually returns NS_OK)
*/
PRBool nsStr::DidAcquireMemory(void) {
return gStringAcquiredMemory;
}
//----------------------------------------------------------------------------------------
CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) {

View File

@ -45,17 +45,26 @@
/***********************************************************************
ASSUMPTIONS:
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
1. nsStrings and nsAutoString are always null terminated. However,
since it maintains a length byte, you can store NULL's inside
the string. Just be careful passing such buffers to 3rd party
API's that assume that NULL always terminate the buffer.
2. nsCStrings can be upsampled into nsString without data loss
3. 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!).
4. 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 0<n value for the optional count argument.
7. Downsampling from nsString to nsCString is lossy -- avoid it if possible!
8. Calls to ToNewCString() and ToNewUnicode() should be matched with calls to Recycle().
5. If you don't provide the optional count argument to Append() and Insert(),
the method will assume that the given buffer is terminated by the first
NULL it encounters.
6. Downsampling from nsString to nsCString can be lossy -- avoid it if possible!
7. Calls to ToNewCString() and ToNewUnicode() should be matched with calls to Recycle().
***********************************************************************/
@ -399,6 +408,8 @@ struct NS_COM nsStr {
static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset);
static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset);
static PRBool DidAcquireMemory(void);
PRUint32 mLength;
PRUint32 mCapacity;

View File

@ -87,26 +87,7 @@ nsCString::nsCString(const char* aCString,PRInt32 aLength) {
*/
nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength) {
nsStr::Initialize(*this,eOneByte);
if(aString && aLength){
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aLength) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aLength;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<(PRInt32)mLength)) {
aLength=temp.mLength=pos;
}
}
else aLength=temp.mLength=nsCRT::strlen(aString);
if(0<aLength)
nsStr::Append(*this,temp,0,aLength);
}
Assign(aString,aLength);
}
/**
@ -248,12 +229,12 @@ PRBool nsCString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
PRBool result=PR_FALSE;
if(anIndex<mLength){
mStr[anIndex]=(char)aChar;
// SOON! if(0==aChar) mLength=anIndex;
result=PR_TRUE;
}
return result;
}
/*********************************************************
append (operator+) METHODS....
*********************************************************/
@ -803,19 +784,21 @@ nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) {
nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) {
nsStr::Truncate(*this,0);
if(aString){
if(aString && aCount){
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in Assign(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -825,6 +808,7 @@ nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) {
return *this;
}
/**
* assign given unichar to this string
* @update gess 01/04/99
@ -913,13 +897,15 @@ nsCString& nsCString::Append(const char* aCString,PRInt32 aCount) {
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Append(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1099,13 +1085,15 @@ nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCou
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Insert(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);

View File

@ -398,8 +398,9 @@ public:
*/
nsCString& operator+=(const nsCString& aString){return Append(aString,aString.mLength);}
nsCString& operator+=(const char* aCString) {return Append(aCString);}
nsCString& operator+=(PRUnichar aChar){return Append(aChar);}
nsCString& operator+=(char aChar){return Append(aChar);}
nsCString& operator+=(const PRUnichar aChar){return Append(aChar);}
nsCString& operator+=(const char aChar){return Append(aChar);}
nsCString& operator+=(const int anInt){return Append(anInt,10);}
/*
* Appends n characters from given string to this,

View File

@ -72,9 +72,9 @@ nsString::nsString() {
* @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){
nsString::nsString(const char* aCString,PRInt32 aCount){
nsStr::Initialize(*this,eTwoByte);
Assign(aCString);
Assign(aCString,aCount);
}
/**
@ -83,9 +83,9 @@ nsString::nsString(const char* aCString){
* @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) {
nsString::nsString(const PRUnichar* aString,PRInt32 aCount) {
nsStr::Initialize(*this,eTwoByte);
Assign(aString);
Assign(aString,aCount);
}
/**
@ -270,6 +270,7 @@ PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
return result;
}
/*********************************************************
append (operator+) METHODS....
*********************************************************/
@ -1037,13 +1038,15 @@ nsString& nsString::Append(const char* aCString,PRInt32 aCount) {
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in append(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1067,13 +1070,15 @@ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aCount) {
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in append(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -1255,13 +1260,15 @@ nsString& nsString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=strlen(aCString);
NS_ASSERTION(aCount <= len, "potential error in Insert(char*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
@ -1288,13 +1295,15 @@ nsString& nsString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aC
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
// If this assertion fires, the caller is probably lying about the length of
// the passed-in string. File a bug on the caller.
#ifdef NS_DEBUG
PRInt32 len=nsCRT::strlen(aString);
NS_ASSERTION(aCount <= len, "potential error in Insert(PRUnichar*)");
#endif
}
else aCount=temp.mLength=nsCRT::strlen(aString);
@ -1699,7 +1708,9 @@ PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount
if(aCString) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aCString);
temp.mLength= (0<aCount) ? aCount : nsCRT::strlen(aCString);
temp.mStr=(char*)aCString;
return nsStr::Compare(*this,temp,aCount,aIgnoreCase);
}
@ -1792,6 +1803,7 @@ PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) cons
return Equals(s1,s2,PR_TRUE);
}
/**
* Compare this to given string; note that we compare full strings here.
*

View File

@ -69,13 +69,13 @@ public:
* This constructor accepts an isolatin string
* @param aCString is a ptr to a 1-byte cstr
*/
nsString(const char* aCString);
nsString(const char* aCString,PRInt32 aCount=-1);
/**
* This constructor accepts a unichar string
* @param aCString is a ptr to a 2-byte cstr
*/
nsString(const PRUnichar* aString);
nsString(const PRUnichar* aString,PRInt32 aCount=-1);
/**
* This is a copy constructor that accepts an nsStr
@ -442,9 +442,10 @@ public:
nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);}
nsString& operator+=(const nsString& aString){return Append(aString,aString.mLength);}
nsString& operator+=(const char* aCString) {return Append(aCString);}
//nsString& operator+=(char aChar){return Append(aChar);}
nsString& operator+=(const char aChar) {return Append(PRUnichar(unsigned char(aChar)));}
nsString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);}
nsString& operator+=(PRUnichar aChar){return Append(aChar);}
nsString& operator+=(const PRUnichar aChar){return Append(aChar);}
nsString& operator+=(const int anInt){return Append(anInt,10);}
/*
* Appends n characters from given string to this,