This commit is contained in:
rickg%netscape.com 1998-09-04 17:26:37 +00:00
Родитель 9beefbebbe
Коммит 7cb4b0c39d
8 изменённых файлов: 3152 добавлений и 908 удалений

Просмотреть файл

@ -15,6 +15,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <iostream.h>
#include <ctype.h>
#include <string.h>
@ -39,15 +40,11 @@ PRBool nsString::mSelfTested = PR_FALSE;
/***********************************************************************
MODULE NOTES:
IMPLEMENTATION NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
Man I hate writing string classes.
You'd think after about a qintrillion lines of code have been written,
that no poor soul would ever have to do this again. Sigh.
***********************************************************************/
@ -129,6 +126,21 @@ nsString::~nsString()
mCapacity=mLength=0;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
void nsString::SizeOf(nsISizeOfHandler* aHandler) const
{
aHandler->Add(sizeof(*this));
@ -211,19 +223,6 @@ void nsString::SetLength(PRInt32 aLength) {
mLength=aLength;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
/*********************************************************
ACCESSOR METHODS....
@ -591,39 +590,6 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode) const {
return rv;
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given PRUnichar* to this string
* @update gess 7/27/98
@ -684,6 +650,37 @@ nsString& nsString::operator=(const PRUnichar* aStr) {
return this->SetString(aStr);
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given char to this string
@ -930,38 +927,41 @@ PRInt32 nsString::Right(nsString& aCopy,PRInt32 aCount) {
*/
PRInt32 nsString::Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) {
aCount=(aCount>aCopy.mLength) ? aCopy.mLength : aCount; //don't try to copy more than you are given
if(aCount>0) {
if(0<anOffset) {
if(aCount>0) {
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
while (++first<last) {
*(++next)=*first;
while (++first<last) {
*(++next)=*first;
}
mLength+=aCount;
}
mLength+=aCount;
else aCount=0;
}
return aCount;
}
@ -1243,8 +1243,9 @@ PRInt32 nsString::Find(const char* anISOLatin1Buf) const{
PRInt32 result=kNotFound;
if(anISOLatin1Buf) {
PRInt32 len=strlen(anISOLatin1Buf);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],anISOLatin1Buf,len))
return offset; //in this case, 0 means they match
}
@ -1264,8 +1265,9 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{
PRInt32 result=kNotFound;
if(aString) {
PRInt32 len=nsCRT::strlen(aString);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],aString,len))
return offset; //in this case, 0 means they match
}
@ -1285,8 +1287,10 @@ PRInt32 nsString::Find(const nsString& aString) const{
PRInt32 result=kNotFound;
PRInt32 len=aString.mLength;
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++) {
PRInt32 offset=0;
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(offset=0;offset<=max;offset++) {
if(0==nsCRT::strncmp(&mStr[offset],aString.mStr,len)) {
return offset; //in this case, 0 means they match
}
@ -1319,7 +1323,7 @@ PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && (strlen(anISOLatin1Set))) {
for(PRInt32 i=anOffset;i<mLength;i++){
@ -1338,7 +1342,7 @@ PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=anOffset;i<mLength;i++){
PRInt32 pos=aSet.Find(mStr[i]);
@ -1356,7 +1360,7 @@ PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && strlen(anISOLatin1Set)) {
for(PRInt32 i=mLength-1;i>0;i--){
@ -1375,7 +1379,7 @@ PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset)
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=mLength-1;i>0;i--){
PRInt32 pos=aSet.Find(mStr[i]);
@ -2038,6 +2042,18 @@ void nsString::SelfTest(void) {
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString sourceString("Hello how are you");
nsString subString("you");
nsString replacementStr("xxx");
PRInt32 offset = sourceString.Find(subString);
sourceString.Cut(offset, subString.Length());
sourceString.Insert(replacementStr, offset, replacementStr.Length()); // Offset isn't checked in Insert either
//**********************************************
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString temp8("aaaa");
nsString temp8a("AAAA");
nsString temp9("bbbb");
@ -2183,10 +2199,10 @@ void nsString::SelfTest(void) {
pos=temp15.Find(PRUnichar('r'));
NS_ASSERTION(pos==17,"Error: Find char routine");
pos=temp15.FindFirstCharInSet("12k");
pos=temp15.FindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindFirstInChar routine");
pos=temp15.FindLastCharInSet("12k");
pos=temp15.RFindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindLastInChar routine");
pos=temp15.RFind("abc");

Просмотреть файл

@ -16,15 +16,19 @@
* Reserved.
*/
/**
* MODULE NOTES:
* LAST MODS: gess 28Feb98
*
* This very simple string class that knows how to do
* efficient (dynamic) resizing. It offers almost no
* i18n support, and will undoubtedly have to be replaced.
*
*/
/***********************************************************************
MODULE NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
***********************************************************************/
#ifndef _NSSTRING
#define _NSSTRING
@ -40,156 +44,697 @@ class nsISizeOfHandler;
class NS_BASE nsString {
public:
nsString();
nsString(const char* anISOLatin1);
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
/**
* Default constructor. Note that we actually allocate a small buffer
* to begin with. This is because the "philosophy" of the string class
* was to allow developers direct access to the underlying buffer for
* performance reasons.
*/
nsString();
public:
virtual ~nsString();
/**
* This constructor accepts an isolatin string
* @param anISOLatin1 is a ptr to a 1-byte cstr
*/
nsString(const char* anISOLatin1);
PRInt32 Length() const { return mLength; }
/**
* This is our copy constructor
* @param reference to another nsString
*/
nsString(const nsString&);
void SetLength(PRInt32 aLength);
void Truncate(PRInt32 anIndex=0);
virtual void EnsureCapacityFor(PRInt32 aNewLength);
/**
* Constructor from a unicode string
* @param anicodestr pts to a unicode string
*/
nsString(const PRUnichar* aUnicode);
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
PRBool IsOrdered(void) const;
/**
* Virtual Destructor
*/
virtual ~nsString();
///accessor methods
//@{
const PRUnichar* GetUnicode(void) const;
operator PRUnichar*() const;
PRUnichar operator()(PRInt32 i) const;
PRUnichar& operator[](PRInt32 i) const;
PRUnichar& CharAt(PRInt32 anIndex) const;
PRUnichar& First() const;
PRUnichar& Last() const;
/**
* Retrieve the length of this string
* @return string length
*/
PRInt32 Length() const { return mLength; }
//string creation methods...
nsString operator+(const nsString& aString);
nsString operator+(const char* anISOLatin1);
nsString operator+(char aChar);
nsString operator+(const PRUnichar* aBuffer);
nsString operator+(PRUnichar aChar);
void ToLowerCase();
void ToLowerCase(nsString& aString) const;
void ToUpperCase();
void ToUpperCase(nsString& aString) const;
/**
* Sets the new length of the string.
* @param aLength is new string length.
* @return nada
*/
void SetLength(PRInt32 aLength);
nsString* ToNewString() const;
char* ToNewCString() const;
/**
* This method truncates this string to given length.
*
* @param anIndex -- new length of string
* @return nada
*/
void Truncate(PRInt32 anIndex=0);
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
void Copy(nsString& aString) const;
PRUnichar* ToNewUnicode() const;
float ToFloat(PRInt32* aErrorCode) const;
PRInt32 ToInteger(PRInt32* aErrorCode) const;
//@}
/**
* This method gets called when the internal buffer needs
* to grow to a given size.
* @param aNewLength -- new capacity of string
* @return void
*/
virtual void EnsureCapacityFor(PRInt32 aNewLength);
///string manipulation methods...
//@{
/**
*
* @param
*/
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* Determine whether or not the characters in this
* string are in sorted order.
*
* @return TRUE if ordered.
*/
PRBool IsOrdered(void) const;
nsString& operator=(const nsString& aString);
nsString& operator=(const char* anISOLatin1);
nsString& operator=(char aChar);
nsString& operator=(const PRUnichar* aBuffer);
nsString& operator=(PRUnichar aChar);
nsString& operator+=(const nsString& aString);
nsString& operator+=(const char* anISOLatin1);
nsString& operator+=(const PRUnichar* aBuffer);
nsString& operator+=(PRUnichar aChar);
/**********************************************************************
Accessor methods...
*********************************************************************/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
nsString& Append(char aChar);
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
nsString& Append(PRUnichar aChar);
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
nsString& Append(float aFloat);
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/**
* Retrieve pointer to internal string value
* @return PRUnichar* to internal string
*/
const PRUnichar* GetUnicode(void) const;
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
nsString& StripChars(const char* aSet);
nsString& StripWhitespace();
nsString& Trim( const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
static PRBool IsSpace(PRUnichar ch);
static PRBool IsAlpha(PRUnichar ch);
static PRBool IsDigit(PRUnichar ch);
/**
*
* @param
* @return
*/
operator PRUnichar*() const;
//@}
/**
* Retrieve unicode char at given index
* @param offset into string
* @return PRUnichar* to internal string
*/
PRUnichar operator()(PRInt32 anIndex) const;
///searching methods...
//@{
PRInt32 BinarySearch(PRUnichar aChar) const;
PRInt32 Find(const char* anISOLatin1) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
PRInt32 Find(const nsString& aString) const;
PRInt32 FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindFirstCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
//@}
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& operator[](PRInt32 anIndex) const;
///comparision methods...
//@{
virtual PRInt32 Compare(const nsString &S,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *anISOLatin1,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& CharAt(PRInt32 anIndex) const;
PRBool operator==(const nsString &S) const;
PRBool operator==(const char *anISOLatin1) const;
PRBool operator==(const PRUnichar* aString) const;
PRBool operator!=(const nsString &S) const;
PRBool operator!=(const char *anISOLatin1) const;
PRBool operator!=(const PRUnichar* aString) const;
PRBool operator<(const nsString &S) const;
PRBool operator<(const char *anISOLatin1) const;
PRBool operator<(const PRUnichar* aString) const;
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Retrieve reference to first unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& First() const;
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Retrieve reference to last unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& Last() const;
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
//@}
/**********************************************************************
String creation methods...
*********************************************************************/
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
/**
* Create a new string by appending given string to this
* @param aString -- 2nd string to be appended
* @return new string
*/
nsString operator+(const nsString& aString);
/**
* create a new string by adding this to the given buffer.
* @param anISOLatin1 is a ptr to cstring to be added to this
* @return newly created string
*/
nsString operator+(const char* anISOLatin1);
/**
* create a new string by adding this to the given char.
* @param aChar is a char to be added to this
* @return newly created string
*/
nsString operator+(char aChar);
/**
* create a new string by adding this to the given buffer.
* @param aStr unichar buffer to be added to this
* @return newly created string
*/
nsString operator+(const PRUnichar* aBuffer);
/**
* create a new string by adding this to the given char.
* @param aChar is a unichar to be added to this
* @return newly created string
*/
nsString operator+(PRUnichar aChar);
/**
* Converts all chars in internal string to lower
*/
void ToLowerCase();
/**
* Converts all chars in given string to lower
*/
void ToLowerCase(nsString& aString) const;
/**
* Converts all chars in given string to upper
*/
void ToUpperCase();
/**
* Converts all chars in internal string to upper
*/
void ToUpperCase(nsString& aString) const;
/**
* Creates a duplicate clone (ptr) of this string.
* @return ptr to clone of this string
*/
nsString* ToNewString() const;
/**
* Creates an ISOLatin1 clone of this string
* @return ptr to new isolatin1 string
*/
char* ToNewCString() const;
/**
* Copies data from internal buffer onto given char* buffer
* @param aBuf is the buffer where data is stored
* @param aBuflength is the max # of chars to move to buffer
* @return ptr to given buffer
*/
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
/**
* Copies contents of this onto given string.
* @param aString to hold copy of this
* @return nada.
*/
void Copy(nsString& aString) const;
/**
* Creates an unichar clone of this string
* @return ptr to new unichar string
*/
PRUnichar* ToNewUnicode() const;
/**
* Perform string to float conversion.
* @param aErrorCode will contain error if one occurs
* @return float rep of string value
*/
float ToFloat(PRInt32* aErrorCode) const;
/**
* Perform string to int conversion.
* @param aErrorCode will contain error if one occurs
* @return int rep of string value
*/
PRInt32 ToInteger(PRInt32* aErrorCode) const;
/**********************************************************************
String manipulation methods...
*********************************************************************/
/**
* assign given PRUnichar* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* assign given string to this one
* @param aString: string to be added to this
* @return this
*/
nsString& operator=(const nsString& aString);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& operator=(const char* anISOLatin1);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(char aChar);
/**
* assign given unichar* to this string
* @param aBuffer: unichar buffer to be assigned to this
* @return this
*/
nsString& operator=(const PRUnichar* aBuffer);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& operator+=(const nsString& aString);
/**
* append given buffer to this string
* @param anISOLatin1: buffer to be appended to this
* @return this
*/
nsString& operator+=(const char* anISOLatin1);
/**
* append given buffer to this string
* @param aBuffer: buffer to be appended to this
* @return this
*/
nsString& operator+=(const PRUnichar* aBuffer);
/**
* append given char to this string
* @param aChar: char to be appended to this
* @return this
*/
nsString& operator+=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& Append(char aChar);
/**
* append given unichar buffer to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
/**
* append given unichar character to this string
* @param aChar is the char to be appended to this
* @return this
*/
nsString& Append(PRUnichar aChar);
/**
* Append an integer onto this string
* @param aInteger is the int to be appended
* @param aRadix specifies 8,10,16
* @return this
*/
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
/**
* Append a float value onto this string
* @param aFloat is the float to be appended
* @return this
*/
nsString& Append(float aFloat);
/*
* Copies n characters from this string to given string,
* starting at the leftmost offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at the given offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @param anOffset -- position where copying begins
* @return number of chars copied
*/
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at rightmost char.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
/*
* This method inserts n chars from given string into this
* string at str[anOffset].
*
* @param aCopy -- String to be inserted into this
* @param anOffset -- insertion position within this str
* @param aCount -- number of chars to be copied from aCopy
* @return number of chars inserted into this.
*/
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
/**
* Insert a single unicode char into this string at
* a specified offset.
*
* @param aChar char to be inserted into this string
* @param anOffset is insert pos in str
* @return the number of chars inserted into this string
*/
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/*
* This method is used to cut characters in this string
* starting at anOffset, continuing for aCount chars.
*
* @param anOffset -- start pos for cut operation
* @param aCount -- number of chars to be cut
* @return *this
*/
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
/**
* This method is used to remove all occurances of the
* characters found in aSet from this string.
*
* @param aSet -- characters to be cut from this
* @return *this
*/
nsString& StripChars(const char* aSet);
/**
* This method strips whitespace throughout the string
*
* @return this
*/
nsString& StripWhitespace();
/**
* This method trims characters found in aTrimSet from
* either end of the underlying string.
*
* @param aTrimSet -- contains chars to be trimmed from
* both ends
* @return this
*/
nsString& Trim(const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* This method strips whitespace from string.
* You can control whether whitespace is yanked from
* start and end of string as well.
*
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* 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);
/**********************************************************************
Searching methods...
*********************************************************************/
/**
* Search for given character within this string.
* This method does so by using a binary search,
* so your string HAD BETTER BE ORDERED!
*
* @param aChar is the unicode char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 BinarySearch(PRUnichar aChar) const;
/**
* Search for given substring within this string
*
* @param aString is substring to be sought in this
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(const char* aString) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(const nsString& aString) const;
/**
* Search for given char within this string
*
* @param aChar - char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
/**
* This method searches this string for the first character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This method searches this string for the last character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This methods scans the string backwards, looking for the given string
* @param aString is substring to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
/**
* This methods scans the string backwards, looking for the given char
* @param char is the char to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
/**********************************************************************
Comparison methods...
*********************************************************************/
/**
* Compares a given string type to this string.
* @update gess 7/27/98
* @param S is the string to be compared
* @param aIgnoreCase tells us how to treat case
* @return -1,0,1
*/
virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* These methods compare a given string type to this one
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator==(const nsString &aString) const;
PRBool operator==(const char *aString) const;
PRBool operator==(const PRUnichar* aString) const;
/**
* These methods perform a !compare of a given string type to this
* @param aString is the string to be compared to this
* @return TRUE
*/
PRBool operator!=(const nsString &aString) const;
PRBool operator!=(const char *aString) const;
PRBool operator!=(const PRUnichar* aString) const;
/**
* These methods test if a given string is < than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<(const nsString &aString) const;
PRBool operator<(const char *aString) const;
PRBool operator<(const PRUnichar* aString) const;
/**
* These methods test if a given string is > than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
/**
* These methods test if a given string is <= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
/**
* These methods test if a given string is >= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* aString,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other without respect to case
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
protected:

Просмотреть файл

@ -15,6 +15,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <iostream.h>
#include <ctype.h>
#include <string.h>
@ -39,15 +40,11 @@ PRBool nsString::mSelfTested = PR_FALSE;
/***********************************************************************
MODULE NOTES:
IMPLEMENTATION NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
Man I hate writing string classes.
You'd think after about a qintrillion lines of code have been written,
that no poor soul would ever have to do this again. Sigh.
***********************************************************************/
@ -129,6 +126,21 @@ nsString::~nsString()
mCapacity=mLength=0;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
void nsString::SizeOf(nsISizeOfHandler* aHandler) const
{
aHandler->Add(sizeof(*this));
@ -211,19 +223,6 @@ void nsString::SetLength(PRInt32 aLength) {
mLength=aLength;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
/*********************************************************
ACCESSOR METHODS....
@ -591,39 +590,6 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode) const {
return rv;
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given PRUnichar* to this string
* @update gess 7/27/98
@ -684,6 +650,37 @@ nsString& nsString::operator=(const PRUnichar* aStr) {
return this->SetString(aStr);
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given char to this string
@ -930,38 +927,41 @@ PRInt32 nsString::Right(nsString& aCopy,PRInt32 aCount) {
*/
PRInt32 nsString::Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) {
aCount=(aCount>aCopy.mLength) ? aCopy.mLength : aCount; //don't try to copy more than you are given
if(aCount>0) {
if(0<anOffset) {
if(aCount>0) {
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
while (++first<last) {
*(++next)=*first;
while (++first<last) {
*(++next)=*first;
}
mLength+=aCount;
}
mLength+=aCount;
else aCount=0;
}
return aCount;
}
@ -1243,8 +1243,9 @@ PRInt32 nsString::Find(const char* anISOLatin1Buf) const{
PRInt32 result=kNotFound;
if(anISOLatin1Buf) {
PRInt32 len=strlen(anISOLatin1Buf);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],anISOLatin1Buf,len))
return offset; //in this case, 0 means they match
}
@ -1264,8 +1265,9 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{
PRInt32 result=kNotFound;
if(aString) {
PRInt32 len=nsCRT::strlen(aString);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],aString,len))
return offset; //in this case, 0 means they match
}
@ -1285,8 +1287,10 @@ PRInt32 nsString::Find(const nsString& aString) const{
PRInt32 result=kNotFound;
PRInt32 len=aString.mLength;
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++) {
PRInt32 offset=0;
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(offset=0;offset<=max;offset++) {
if(0==nsCRT::strncmp(&mStr[offset],aString.mStr,len)) {
return offset; //in this case, 0 means they match
}
@ -1319,7 +1323,7 @@ PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && (strlen(anISOLatin1Set))) {
for(PRInt32 i=anOffset;i<mLength;i++){
@ -1338,7 +1342,7 @@ PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=anOffset;i<mLength;i++){
PRInt32 pos=aSet.Find(mStr[i]);
@ -1356,7 +1360,7 @@ PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && strlen(anISOLatin1Set)) {
for(PRInt32 i=mLength-1;i>0;i--){
@ -1375,7 +1379,7 @@ PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset)
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=mLength-1;i>0;i--){
PRInt32 pos=aSet.Find(mStr[i]);
@ -2038,6 +2042,18 @@ void nsString::SelfTest(void) {
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString sourceString("Hello how are you");
nsString subString("you");
nsString replacementStr("xxx");
PRInt32 offset = sourceString.Find(subString);
sourceString.Cut(offset, subString.Length());
sourceString.Insert(replacementStr, offset, replacementStr.Length()); // Offset isn't checked in Insert either
//**********************************************
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString temp8("aaaa");
nsString temp8a("AAAA");
nsString temp9("bbbb");
@ -2183,10 +2199,10 @@ void nsString::SelfTest(void) {
pos=temp15.Find(PRUnichar('r'));
NS_ASSERTION(pos==17,"Error: Find char routine");
pos=temp15.FindFirstCharInSet("12k");
pos=temp15.FindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindFirstInChar routine");
pos=temp15.FindLastCharInSet("12k");
pos=temp15.RFindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindLastInChar routine");
pos=temp15.RFind("abc");

Просмотреть файл

@ -16,15 +16,19 @@
* Reserved.
*/
/**
* MODULE NOTES:
* LAST MODS: gess 28Feb98
*
* This very simple string class that knows how to do
* efficient (dynamic) resizing. It offers almost no
* i18n support, and will undoubtedly have to be replaced.
*
*/
/***********************************************************************
MODULE NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
***********************************************************************/
#ifndef _NSSTRING
#define _NSSTRING
@ -40,156 +44,697 @@ class nsISizeOfHandler;
class NS_BASE nsString {
public:
nsString();
nsString(const char* anISOLatin1);
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
/**
* Default constructor. Note that we actually allocate a small buffer
* to begin with. This is because the "philosophy" of the string class
* was to allow developers direct access to the underlying buffer for
* performance reasons.
*/
nsString();
public:
virtual ~nsString();
/**
* This constructor accepts an isolatin string
* @param anISOLatin1 is a ptr to a 1-byte cstr
*/
nsString(const char* anISOLatin1);
PRInt32 Length() const { return mLength; }
/**
* This is our copy constructor
* @param reference to another nsString
*/
nsString(const nsString&);
void SetLength(PRInt32 aLength);
void Truncate(PRInt32 anIndex=0);
virtual void EnsureCapacityFor(PRInt32 aNewLength);
/**
* Constructor from a unicode string
* @param anicodestr pts to a unicode string
*/
nsString(const PRUnichar* aUnicode);
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
PRBool IsOrdered(void) const;
/**
* Virtual Destructor
*/
virtual ~nsString();
///accessor methods
//@{
const PRUnichar* GetUnicode(void) const;
operator PRUnichar*() const;
PRUnichar operator()(PRInt32 i) const;
PRUnichar& operator[](PRInt32 i) const;
PRUnichar& CharAt(PRInt32 anIndex) const;
PRUnichar& First() const;
PRUnichar& Last() const;
/**
* Retrieve the length of this string
* @return string length
*/
PRInt32 Length() const { return mLength; }
//string creation methods...
nsString operator+(const nsString& aString);
nsString operator+(const char* anISOLatin1);
nsString operator+(char aChar);
nsString operator+(const PRUnichar* aBuffer);
nsString operator+(PRUnichar aChar);
void ToLowerCase();
void ToLowerCase(nsString& aString) const;
void ToUpperCase();
void ToUpperCase(nsString& aString) const;
/**
* Sets the new length of the string.
* @param aLength is new string length.
* @return nada
*/
void SetLength(PRInt32 aLength);
nsString* ToNewString() const;
char* ToNewCString() const;
/**
* This method truncates this string to given length.
*
* @param anIndex -- new length of string
* @return nada
*/
void Truncate(PRInt32 anIndex=0);
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
void Copy(nsString& aString) const;
PRUnichar* ToNewUnicode() const;
float ToFloat(PRInt32* aErrorCode) const;
PRInt32 ToInteger(PRInt32* aErrorCode) const;
//@}
/**
* This method gets called when the internal buffer needs
* to grow to a given size.
* @param aNewLength -- new capacity of string
* @return void
*/
virtual void EnsureCapacityFor(PRInt32 aNewLength);
///string manipulation methods...
//@{
/**
*
* @param
*/
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* Determine whether or not the characters in this
* string are in sorted order.
*
* @return TRUE if ordered.
*/
PRBool IsOrdered(void) const;
nsString& operator=(const nsString& aString);
nsString& operator=(const char* anISOLatin1);
nsString& operator=(char aChar);
nsString& operator=(const PRUnichar* aBuffer);
nsString& operator=(PRUnichar aChar);
nsString& operator+=(const nsString& aString);
nsString& operator+=(const char* anISOLatin1);
nsString& operator+=(const PRUnichar* aBuffer);
nsString& operator+=(PRUnichar aChar);
/**********************************************************************
Accessor methods...
*********************************************************************/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
nsString& Append(char aChar);
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
nsString& Append(PRUnichar aChar);
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
nsString& Append(float aFloat);
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/**
* Retrieve pointer to internal string value
* @return PRUnichar* to internal string
*/
const PRUnichar* GetUnicode(void) const;
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
nsString& StripChars(const char* aSet);
nsString& StripWhitespace();
nsString& Trim( const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
static PRBool IsSpace(PRUnichar ch);
static PRBool IsAlpha(PRUnichar ch);
static PRBool IsDigit(PRUnichar ch);
/**
*
* @param
* @return
*/
operator PRUnichar*() const;
//@}
/**
* Retrieve unicode char at given index
* @param offset into string
* @return PRUnichar* to internal string
*/
PRUnichar operator()(PRInt32 anIndex) const;
///searching methods...
//@{
PRInt32 BinarySearch(PRUnichar aChar) const;
PRInt32 Find(const char* anISOLatin1) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
PRInt32 Find(const nsString& aString) const;
PRInt32 FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindFirstCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
//@}
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& operator[](PRInt32 anIndex) const;
///comparision methods...
//@{
virtual PRInt32 Compare(const nsString &S,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *anISOLatin1,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& CharAt(PRInt32 anIndex) const;
PRBool operator==(const nsString &S) const;
PRBool operator==(const char *anISOLatin1) const;
PRBool operator==(const PRUnichar* aString) const;
PRBool operator!=(const nsString &S) const;
PRBool operator!=(const char *anISOLatin1) const;
PRBool operator!=(const PRUnichar* aString) const;
PRBool operator<(const nsString &S) const;
PRBool operator<(const char *anISOLatin1) const;
PRBool operator<(const PRUnichar* aString) const;
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Retrieve reference to first unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& First() const;
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Retrieve reference to last unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& Last() const;
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
//@}
/**********************************************************************
String creation methods...
*********************************************************************/
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
/**
* Create a new string by appending given string to this
* @param aString -- 2nd string to be appended
* @return new string
*/
nsString operator+(const nsString& aString);
/**
* create a new string by adding this to the given buffer.
* @param anISOLatin1 is a ptr to cstring to be added to this
* @return newly created string
*/
nsString operator+(const char* anISOLatin1);
/**
* create a new string by adding this to the given char.
* @param aChar is a char to be added to this
* @return newly created string
*/
nsString operator+(char aChar);
/**
* create a new string by adding this to the given buffer.
* @param aStr unichar buffer to be added to this
* @return newly created string
*/
nsString operator+(const PRUnichar* aBuffer);
/**
* create a new string by adding this to the given char.
* @param aChar is a unichar to be added to this
* @return newly created string
*/
nsString operator+(PRUnichar aChar);
/**
* Converts all chars in internal string to lower
*/
void ToLowerCase();
/**
* Converts all chars in given string to lower
*/
void ToLowerCase(nsString& aString) const;
/**
* Converts all chars in given string to upper
*/
void ToUpperCase();
/**
* Converts all chars in internal string to upper
*/
void ToUpperCase(nsString& aString) const;
/**
* Creates a duplicate clone (ptr) of this string.
* @return ptr to clone of this string
*/
nsString* ToNewString() const;
/**
* Creates an ISOLatin1 clone of this string
* @return ptr to new isolatin1 string
*/
char* ToNewCString() const;
/**
* Copies data from internal buffer onto given char* buffer
* @param aBuf is the buffer where data is stored
* @param aBuflength is the max # of chars to move to buffer
* @return ptr to given buffer
*/
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
/**
* Copies contents of this onto given string.
* @param aString to hold copy of this
* @return nada.
*/
void Copy(nsString& aString) const;
/**
* Creates an unichar clone of this string
* @return ptr to new unichar string
*/
PRUnichar* ToNewUnicode() const;
/**
* Perform string to float conversion.
* @param aErrorCode will contain error if one occurs
* @return float rep of string value
*/
float ToFloat(PRInt32* aErrorCode) const;
/**
* Perform string to int conversion.
* @param aErrorCode will contain error if one occurs
* @return int rep of string value
*/
PRInt32 ToInteger(PRInt32* aErrorCode) const;
/**********************************************************************
String manipulation methods...
*********************************************************************/
/**
* assign given PRUnichar* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* assign given string to this one
* @param aString: string to be added to this
* @return this
*/
nsString& operator=(const nsString& aString);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& operator=(const char* anISOLatin1);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(char aChar);
/**
* assign given unichar* to this string
* @param aBuffer: unichar buffer to be assigned to this
* @return this
*/
nsString& operator=(const PRUnichar* aBuffer);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& operator+=(const nsString& aString);
/**
* append given buffer to this string
* @param anISOLatin1: buffer to be appended to this
* @return this
*/
nsString& operator+=(const char* anISOLatin1);
/**
* append given buffer to this string
* @param aBuffer: buffer to be appended to this
* @return this
*/
nsString& operator+=(const PRUnichar* aBuffer);
/**
* append given char to this string
* @param aChar: char to be appended to this
* @return this
*/
nsString& operator+=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& Append(char aChar);
/**
* append given unichar buffer to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
/**
* append given unichar character to this string
* @param aChar is the char to be appended to this
* @return this
*/
nsString& Append(PRUnichar aChar);
/**
* Append an integer onto this string
* @param aInteger is the int to be appended
* @param aRadix specifies 8,10,16
* @return this
*/
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
/**
* Append a float value onto this string
* @param aFloat is the float to be appended
* @return this
*/
nsString& Append(float aFloat);
/*
* Copies n characters from this string to given string,
* starting at the leftmost offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at the given offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @param anOffset -- position where copying begins
* @return number of chars copied
*/
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at rightmost char.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
/*
* This method inserts n chars from given string into this
* string at str[anOffset].
*
* @param aCopy -- String to be inserted into this
* @param anOffset -- insertion position within this str
* @param aCount -- number of chars to be copied from aCopy
* @return number of chars inserted into this.
*/
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
/**
* Insert a single unicode char into this string at
* a specified offset.
*
* @param aChar char to be inserted into this string
* @param anOffset is insert pos in str
* @return the number of chars inserted into this string
*/
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/*
* This method is used to cut characters in this string
* starting at anOffset, continuing for aCount chars.
*
* @param anOffset -- start pos for cut operation
* @param aCount -- number of chars to be cut
* @return *this
*/
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
/**
* This method is used to remove all occurances of the
* characters found in aSet from this string.
*
* @param aSet -- characters to be cut from this
* @return *this
*/
nsString& StripChars(const char* aSet);
/**
* This method strips whitespace throughout the string
*
* @return this
*/
nsString& StripWhitespace();
/**
* This method trims characters found in aTrimSet from
* either end of the underlying string.
*
* @param aTrimSet -- contains chars to be trimmed from
* both ends
* @return this
*/
nsString& Trim(const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* This method strips whitespace from string.
* You can control whether whitespace is yanked from
* start and end of string as well.
*
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* 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);
/**********************************************************************
Searching methods...
*********************************************************************/
/**
* Search for given character within this string.
* This method does so by using a binary search,
* so your string HAD BETTER BE ORDERED!
*
* @param aChar is the unicode char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 BinarySearch(PRUnichar aChar) const;
/**
* Search for given substring within this string
*
* @param aString is substring to be sought in this
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(const char* aString) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(const nsString& aString) const;
/**
* Search for given char within this string
*
* @param aChar - char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
/**
* This method searches this string for the first character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This method searches this string for the last character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This methods scans the string backwards, looking for the given string
* @param aString is substring to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
/**
* This methods scans the string backwards, looking for the given char
* @param char is the char to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
/**********************************************************************
Comparison methods...
*********************************************************************/
/**
* Compares a given string type to this string.
* @update gess 7/27/98
* @param S is the string to be compared
* @param aIgnoreCase tells us how to treat case
* @return -1,0,1
*/
virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* These methods compare a given string type to this one
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator==(const nsString &aString) const;
PRBool operator==(const char *aString) const;
PRBool operator==(const PRUnichar* aString) const;
/**
* These methods perform a !compare of a given string type to this
* @param aString is the string to be compared to this
* @return TRUE
*/
PRBool operator!=(const nsString &aString) const;
PRBool operator!=(const char *aString) const;
PRBool operator!=(const PRUnichar* aString) const;
/**
* These methods test if a given string is < than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<(const nsString &aString) const;
PRBool operator<(const char *aString) const;
PRBool operator<(const PRUnichar* aString) const;
/**
* These methods test if a given string is > than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
/**
* These methods test if a given string is <= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
/**
* These methods test if a given string is >= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* aString,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other without respect to case
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
protected:

Просмотреть файл

@ -15,6 +15,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <iostream.h>
#include <ctype.h>
#include <string.h>
@ -39,15 +40,11 @@ PRBool nsString::mSelfTested = PR_FALSE;
/***********************************************************************
MODULE NOTES:
IMPLEMENTATION NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
Man I hate writing string classes.
You'd think after about a qintrillion lines of code have been written,
that no poor soul would ever have to do this again. Sigh.
***********************************************************************/
@ -129,6 +126,21 @@ nsString::~nsString()
mCapacity=mLength=0;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
void nsString::SizeOf(nsISizeOfHandler* aHandler) const
{
aHandler->Add(sizeof(*this));
@ -211,19 +223,6 @@ void nsString::SetLength(PRInt32 aLength) {
mLength=aLength;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
/*********************************************************
ACCESSOR METHODS....
@ -591,39 +590,6 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode) const {
return rv;
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given PRUnichar* to this string
* @update gess 7/27/98
@ -684,6 +650,37 @@ nsString& nsString::operator=(const PRUnichar* aStr) {
return this->SetString(aStr);
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given char to this string
@ -930,38 +927,41 @@ PRInt32 nsString::Right(nsString& aCopy,PRInt32 aCount) {
*/
PRInt32 nsString::Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) {
aCount=(aCount>aCopy.mLength) ? aCopy.mLength : aCount; //don't try to copy more than you are given
if(aCount>0) {
if(0<anOffset) {
if(aCount>0) {
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
while (++first<last) {
*(++next)=*first;
while (++first<last) {
*(++next)=*first;
}
mLength+=aCount;
}
mLength+=aCount;
else aCount=0;
}
return aCount;
}
@ -1243,8 +1243,9 @@ PRInt32 nsString::Find(const char* anISOLatin1Buf) const{
PRInt32 result=kNotFound;
if(anISOLatin1Buf) {
PRInt32 len=strlen(anISOLatin1Buf);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],anISOLatin1Buf,len))
return offset; //in this case, 0 means they match
}
@ -1264,8 +1265,9 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{
PRInt32 result=kNotFound;
if(aString) {
PRInt32 len=nsCRT::strlen(aString);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],aString,len))
return offset; //in this case, 0 means they match
}
@ -1285,8 +1287,10 @@ PRInt32 nsString::Find(const nsString& aString) const{
PRInt32 result=kNotFound;
PRInt32 len=aString.mLength;
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++) {
PRInt32 offset=0;
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(offset=0;offset<=max;offset++) {
if(0==nsCRT::strncmp(&mStr[offset],aString.mStr,len)) {
return offset; //in this case, 0 means they match
}
@ -1319,7 +1323,7 @@ PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && (strlen(anISOLatin1Set))) {
for(PRInt32 i=anOffset;i<mLength;i++){
@ -1338,7 +1342,7 @@ PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=anOffset;i<mLength;i++){
PRInt32 pos=aSet.Find(mStr[i]);
@ -1356,7 +1360,7 @@ PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && strlen(anISOLatin1Set)) {
for(PRInt32 i=mLength-1;i>0;i--){
@ -1375,7 +1379,7 @@ PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset)
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=mLength-1;i>0;i--){
PRInt32 pos=aSet.Find(mStr[i]);
@ -2038,6 +2042,18 @@ void nsString::SelfTest(void) {
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString sourceString("Hello how are you");
nsString subString("you");
nsString replacementStr("xxx");
PRInt32 offset = sourceString.Find(subString);
sourceString.Cut(offset, subString.Length());
sourceString.Insert(replacementStr, offset, replacementStr.Length()); // Offset isn't checked in Insert either
//**********************************************
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString temp8("aaaa");
nsString temp8a("AAAA");
nsString temp9("bbbb");
@ -2183,10 +2199,10 @@ void nsString::SelfTest(void) {
pos=temp15.Find(PRUnichar('r'));
NS_ASSERTION(pos==17,"Error: Find char routine");
pos=temp15.FindFirstCharInSet("12k");
pos=temp15.FindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindFirstInChar routine");
pos=temp15.FindLastCharInSet("12k");
pos=temp15.RFindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindLastInChar routine");
pos=temp15.RFind("abc");

Просмотреть файл

@ -16,15 +16,19 @@
* Reserved.
*/
/**
* MODULE NOTES:
* LAST MODS: gess 28Feb98
*
* This very simple string class that knows how to do
* efficient (dynamic) resizing. It offers almost no
* i18n support, and will undoubtedly have to be replaced.
*
*/
/***********************************************************************
MODULE NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
***********************************************************************/
#ifndef _NSSTRING
#define _NSSTRING
@ -40,156 +44,697 @@ class nsISizeOfHandler;
class NS_BASE nsString {
public:
nsString();
nsString(const char* anISOLatin1);
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
/**
* Default constructor. Note that we actually allocate a small buffer
* to begin with. This is because the "philosophy" of the string class
* was to allow developers direct access to the underlying buffer for
* performance reasons.
*/
nsString();
public:
virtual ~nsString();
/**
* This constructor accepts an isolatin string
* @param anISOLatin1 is a ptr to a 1-byte cstr
*/
nsString(const char* anISOLatin1);
PRInt32 Length() const { return mLength; }
/**
* This is our copy constructor
* @param reference to another nsString
*/
nsString(const nsString&);
void SetLength(PRInt32 aLength);
void Truncate(PRInt32 anIndex=0);
virtual void EnsureCapacityFor(PRInt32 aNewLength);
/**
* Constructor from a unicode string
* @param anicodestr pts to a unicode string
*/
nsString(const PRUnichar* aUnicode);
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
PRBool IsOrdered(void) const;
/**
* Virtual Destructor
*/
virtual ~nsString();
///accessor methods
//@{
const PRUnichar* GetUnicode(void) const;
operator PRUnichar*() const;
PRUnichar operator()(PRInt32 i) const;
PRUnichar& operator[](PRInt32 i) const;
PRUnichar& CharAt(PRInt32 anIndex) const;
PRUnichar& First() const;
PRUnichar& Last() const;
/**
* Retrieve the length of this string
* @return string length
*/
PRInt32 Length() const { return mLength; }
//string creation methods...
nsString operator+(const nsString& aString);
nsString operator+(const char* anISOLatin1);
nsString operator+(char aChar);
nsString operator+(const PRUnichar* aBuffer);
nsString operator+(PRUnichar aChar);
void ToLowerCase();
void ToLowerCase(nsString& aString) const;
void ToUpperCase();
void ToUpperCase(nsString& aString) const;
/**
* Sets the new length of the string.
* @param aLength is new string length.
* @return nada
*/
void SetLength(PRInt32 aLength);
nsString* ToNewString() const;
char* ToNewCString() const;
/**
* This method truncates this string to given length.
*
* @param anIndex -- new length of string
* @return nada
*/
void Truncate(PRInt32 anIndex=0);
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
void Copy(nsString& aString) const;
PRUnichar* ToNewUnicode() const;
float ToFloat(PRInt32* aErrorCode) const;
PRInt32 ToInteger(PRInt32* aErrorCode) const;
//@}
/**
* This method gets called when the internal buffer needs
* to grow to a given size.
* @param aNewLength -- new capacity of string
* @return void
*/
virtual void EnsureCapacityFor(PRInt32 aNewLength);
///string manipulation methods...
//@{
/**
*
* @param
*/
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* Determine whether or not the characters in this
* string are in sorted order.
*
* @return TRUE if ordered.
*/
PRBool IsOrdered(void) const;
nsString& operator=(const nsString& aString);
nsString& operator=(const char* anISOLatin1);
nsString& operator=(char aChar);
nsString& operator=(const PRUnichar* aBuffer);
nsString& operator=(PRUnichar aChar);
nsString& operator+=(const nsString& aString);
nsString& operator+=(const char* anISOLatin1);
nsString& operator+=(const PRUnichar* aBuffer);
nsString& operator+=(PRUnichar aChar);
/**********************************************************************
Accessor methods...
*********************************************************************/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
nsString& Append(char aChar);
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
nsString& Append(PRUnichar aChar);
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
nsString& Append(float aFloat);
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/**
* Retrieve pointer to internal string value
* @return PRUnichar* to internal string
*/
const PRUnichar* GetUnicode(void) const;
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
nsString& StripChars(const char* aSet);
nsString& StripWhitespace();
nsString& Trim( const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
static PRBool IsSpace(PRUnichar ch);
static PRBool IsAlpha(PRUnichar ch);
static PRBool IsDigit(PRUnichar ch);
/**
*
* @param
* @return
*/
operator PRUnichar*() const;
//@}
/**
* Retrieve unicode char at given index
* @param offset into string
* @return PRUnichar* to internal string
*/
PRUnichar operator()(PRInt32 anIndex) const;
///searching methods...
//@{
PRInt32 BinarySearch(PRUnichar aChar) const;
PRInt32 Find(const char* anISOLatin1) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
PRInt32 Find(const nsString& aString) const;
PRInt32 FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindFirstCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
//@}
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& operator[](PRInt32 anIndex) const;
///comparision methods...
//@{
virtual PRInt32 Compare(const nsString &S,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *anISOLatin1,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& CharAt(PRInt32 anIndex) const;
PRBool operator==(const nsString &S) const;
PRBool operator==(const char *anISOLatin1) const;
PRBool operator==(const PRUnichar* aString) const;
PRBool operator!=(const nsString &S) const;
PRBool operator!=(const char *anISOLatin1) const;
PRBool operator!=(const PRUnichar* aString) const;
PRBool operator<(const nsString &S) const;
PRBool operator<(const char *anISOLatin1) const;
PRBool operator<(const PRUnichar* aString) const;
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Retrieve reference to first unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& First() const;
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Retrieve reference to last unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& Last() const;
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
//@}
/**********************************************************************
String creation methods...
*********************************************************************/
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
/**
* Create a new string by appending given string to this
* @param aString -- 2nd string to be appended
* @return new string
*/
nsString operator+(const nsString& aString);
/**
* create a new string by adding this to the given buffer.
* @param anISOLatin1 is a ptr to cstring to be added to this
* @return newly created string
*/
nsString operator+(const char* anISOLatin1);
/**
* create a new string by adding this to the given char.
* @param aChar is a char to be added to this
* @return newly created string
*/
nsString operator+(char aChar);
/**
* create a new string by adding this to the given buffer.
* @param aStr unichar buffer to be added to this
* @return newly created string
*/
nsString operator+(const PRUnichar* aBuffer);
/**
* create a new string by adding this to the given char.
* @param aChar is a unichar to be added to this
* @return newly created string
*/
nsString operator+(PRUnichar aChar);
/**
* Converts all chars in internal string to lower
*/
void ToLowerCase();
/**
* Converts all chars in given string to lower
*/
void ToLowerCase(nsString& aString) const;
/**
* Converts all chars in given string to upper
*/
void ToUpperCase();
/**
* Converts all chars in internal string to upper
*/
void ToUpperCase(nsString& aString) const;
/**
* Creates a duplicate clone (ptr) of this string.
* @return ptr to clone of this string
*/
nsString* ToNewString() const;
/**
* Creates an ISOLatin1 clone of this string
* @return ptr to new isolatin1 string
*/
char* ToNewCString() const;
/**
* Copies data from internal buffer onto given char* buffer
* @param aBuf is the buffer where data is stored
* @param aBuflength is the max # of chars to move to buffer
* @return ptr to given buffer
*/
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
/**
* Copies contents of this onto given string.
* @param aString to hold copy of this
* @return nada.
*/
void Copy(nsString& aString) const;
/**
* Creates an unichar clone of this string
* @return ptr to new unichar string
*/
PRUnichar* ToNewUnicode() const;
/**
* Perform string to float conversion.
* @param aErrorCode will contain error if one occurs
* @return float rep of string value
*/
float ToFloat(PRInt32* aErrorCode) const;
/**
* Perform string to int conversion.
* @param aErrorCode will contain error if one occurs
* @return int rep of string value
*/
PRInt32 ToInteger(PRInt32* aErrorCode) const;
/**********************************************************************
String manipulation methods...
*********************************************************************/
/**
* assign given PRUnichar* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* assign given string to this one
* @param aString: string to be added to this
* @return this
*/
nsString& operator=(const nsString& aString);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& operator=(const char* anISOLatin1);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(char aChar);
/**
* assign given unichar* to this string
* @param aBuffer: unichar buffer to be assigned to this
* @return this
*/
nsString& operator=(const PRUnichar* aBuffer);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& operator+=(const nsString& aString);
/**
* append given buffer to this string
* @param anISOLatin1: buffer to be appended to this
* @return this
*/
nsString& operator+=(const char* anISOLatin1);
/**
* append given buffer to this string
* @param aBuffer: buffer to be appended to this
* @return this
*/
nsString& operator+=(const PRUnichar* aBuffer);
/**
* append given char to this string
* @param aChar: char to be appended to this
* @return this
*/
nsString& operator+=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& Append(char aChar);
/**
* append given unichar buffer to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
/**
* append given unichar character to this string
* @param aChar is the char to be appended to this
* @return this
*/
nsString& Append(PRUnichar aChar);
/**
* Append an integer onto this string
* @param aInteger is the int to be appended
* @param aRadix specifies 8,10,16
* @return this
*/
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
/**
* Append a float value onto this string
* @param aFloat is the float to be appended
* @return this
*/
nsString& Append(float aFloat);
/*
* Copies n characters from this string to given string,
* starting at the leftmost offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at the given offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @param anOffset -- position where copying begins
* @return number of chars copied
*/
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at rightmost char.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
/*
* This method inserts n chars from given string into this
* string at str[anOffset].
*
* @param aCopy -- String to be inserted into this
* @param anOffset -- insertion position within this str
* @param aCount -- number of chars to be copied from aCopy
* @return number of chars inserted into this.
*/
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
/**
* Insert a single unicode char into this string at
* a specified offset.
*
* @param aChar char to be inserted into this string
* @param anOffset is insert pos in str
* @return the number of chars inserted into this string
*/
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/*
* This method is used to cut characters in this string
* starting at anOffset, continuing for aCount chars.
*
* @param anOffset -- start pos for cut operation
* @param aCount -- number of chars to be cut
* @return *this
*/
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
/**
* This method is used to remove all occurances of the
* characters found in aSet from this string.
*
* @param aSet -- characters to be cut from this
* @return *this
*/
nsString& StripChars(const char* aSet);
/**
* This method strips whitespace throughout the string
*
* @return this
*/
nsString& StripWhitespace();
/**
* This method trims characters found in aTrimSet from
* either end of the underlying string.
*
* @param aTrimSet -- contains chars to be trimmed from
* both ends
* @return this
*/
nsString& Trim(const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* This method strips whitespace from string.
* You can control whether whitespace is yanked from
* start and end of string as well.
*
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* 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);
/**********************************************************************
Searching methods...
*********************************************************************/
/**
* Search for given character within this string.
* This method does so by using a binary search,
* so your string HAD BETTER BE ORDERED!
*
* @param aChar is the unicode char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 BinarySearch(PRUnichar aChar) const;
/**
* Search for given substring within this string
*
* @param aString is substring to be sought in this
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(const char* aString) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(const nsString& aString) const;
/**
* Search for given char within this string
*
* @param aChar - char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
/**
* This method searches this string for the first character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This method searches this string for the last character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This methods scans the string backwards, looking for the given string
* @param aString is substring to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
/**
* This methods scans the string backwards, looking for the given char
* @param char is the char to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
/**********************************************************************
Comparison methods...
*********************************************************************/
/**
* Compares a given string type to this string.
* @update gess 7/27/98
* @param S is the string to be compared
* @param aIgnoreCase tells us how to treat case
* @return -1,0,1
*/
virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* These methods compare a given string type to this one
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator==(const nsString &aString) const;
PRBool operator==(const char *aString) const;
PRBool operator==(const PRUnichar* aString) const;
/**
* These methods perform a !compare of a given string type to this
* @param aString is the string to be compared to this
* @return TRUE
*/
PRBool operator!=(const nsString &aString) const;
PRBool operator!=(const char *aString) const;
PRBool operator!=(const PRUnichar* aString) const;
/**
* These methods test if a given string is < than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<(const nsString &aString) const;
PRBool operator<(const char *aString) const;
PRBool operator<(const PRUnichar* aString) const;
/**
* These methods test if a given string is > than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
/**
* These methods test if a given string is <= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
/**
* These methods test if a given string is >= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* aString,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other without respect to case
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
protected:

Просмотреть файл

@ -15,6 +15,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <iostream.h>
#include <ctype.h>
#include <string.h>
@ -39,15 +40,11 @@ PRBool nsString::mSelfTested = PR_FALSE;
/***********************************************************************
MODULE NOTES:
IMPLEMENTATION NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
Man I hate writing string classes.
You'd think after about a qintrillion lines of code have been written,
that no poor soul would ever have to do this again. Sigh.
***********************************************************************/
@ -129,6 +126,21 @@ nsString::~nsString()
mCapacity=mLength=0;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
void nsString::SizeOf(nsISizeOfHandler* aHandler) const
{
aHandler->Add(sizeof(*this));
@ -211,19 +223,6 @@ void nsString::SetLength(PRInt32 aLength) {
mLength=aLength;
}
/**
* This method truncates this string to given length.
*
* @update gess 7/27/98
* @param anIndex -- new length of string
* @return nada
*/
void nsString::Truncate(PRInt32 anIndex) {
if((anIndex>-1) && (anIndex<mLength)) {
mLength=anIndex;
mStr[mLength]=0;
}
}
/*********************************************************
ACCESSOR METHODS....
@ -591,39 +590,6 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode) const {
return rv;
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given PRUnichar* to this string
* @update gess 7/27/98
@ -684,6 +650,37 @@ nsString& nsString::operator=(const PRUnichar* aStr) {
return this->SetString(aStr);
}
/**
* assign given string to this one
* @update gess 7/27/98
* @param aString: string to be added to this
* @return this
*/
nsString& nsString::operator=(const nsString& aString) {
return this->SetString(aString.mStr,aString.mLength);
}
/**
* assign given char* to this string
* @update gess 7/27/98
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& nsString::operator=(const char* anISOLatin1) {
return SetString(anISOLatin1);
}
/**
* assign given char to this string
* @update gess 7/27/98
* @param aChar: char to be assignd to this
* @return this
*/
nsString& nsString::operator=(char aChar) {
return this->operator=(PRUnichar(aChar));
}
/**
* assign given char to this string
@ -930,38 +927,41 @@ PRInt32 nsString::Right(nsString& aCopy,PRInt32 aCount) {
*/
PRInt32 nsString::Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) {
aCount=(aCount>aCopy.mLength) ? aCopy.mLength : aCount; //don't try to copy more than you are given
if(aCount>0) {
if(0<anOffset) {
if(aCount>0) {
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
//1st optimization: If you're inserting at end, then simply append!
if(anOffset>=mLength){
Append(aCopy,aCopy.mLength);
return aCopy.mLength;
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
if(mLength+aCount >= mCapacity) {
EnsureCapacityFor(mLength+aCount);
}
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
PRUnichar* last = mStr + mLength;
PRUnichar* first = mStr + anOffset-1;
PRUnichar* next = mStr + mLength + aCount;
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//Copy rightmost chars, up to offset+aCount...
while(first<last) {
*next=*last;
next--;
last--;
}
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
//now insert new chars, starting at offset
next = last;
first = aCopy.mStr - 1;
last = aCopy.mStr + aCount;
while (++first<last) {
*(++next)=*first;
while (++first<last) {
*(++next)=*first;
}
mLength+=aCount;
}
mLength+=aCount;
else aCount=0;
}
return aCount;
}
@ -1243,8 +1243,9 @@ PRInt32 nsString::Find(const char* anISOLatin1Buf) const{
PRInt32 result=kNotFound;
if(anISOLatin1Buf) {
PRInt32 len=strlen(anISOLatin1Buf);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],anISOLatin1Buf,len))
return offset; //in this case, 0 means they match
}
@ -1264,8 +1265,9 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{
PRInt32 result=kNotFound;
if(aString) {
PRInt32 len=nsCRT::strlen(aString);
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++)
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(PRInt32 offset=0;offset<=max;offset++)
if(0==nsCRT::strncmp(&mStr[offset],aString,len))
return offset; //in this case, 0 means they match
}
@ -1285,8 +1287,10 @@ PRInt32 nsString::Find(const nsString& aString) const{
PRInt32 result=kNotFound;
PRInt32 len=aString.mLength;
if(len<=mLength) { //only enter if abuffer length is <= mStr length.
for(PRInt32 offset=0;offset<mLength-len;offset++) {
PRInt32 offset=0;
if((0<len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length.
PRInt32 max=mLength-len;
for(offset=0;offset<=max;offset++) {
if(0==nsCRT::strncmp(&mStr[offset],aString.mStr,len)) {
return offset; //in this case, 0 means they match
}
@ -1319,7 +1323,7 @@ PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && (strlen(anISOLatin1Set))) {
for(PRInt32 i=anOffset;i<mLength;i++){
@ -1338,7 +1342,7 @@ PRInt32 nsString::FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 anOffset
* @param
* @return
*/
PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::FindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=anOffset;i<mLength;i++){
PRInt32 pos=aSet.Find(mStr[i]);
@ -1356,7 +1360,7 @@ PRInt32 nsString::FindFirstCharInSet(nsString& aSet,PRInt32 anOffset) const{
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(const char* anISOLatin1Set,PRInt32 anOffset) const{
NS_ASSERTION(0!=anISOLatin1Set,kNullPointerError);
if(anISOLatin1Set && strlen(anISOLatin1Set)) {
for(PRInt32 i=mLength-1;i>0;i--){
@ -1375,7 +1379,7 @@ PRInt32 nsString::FindLastCharInSet(const char* anISOLatin1Set,PRInt32 anOffset)
* @param
* @return
*/
PRInt32 nsString::FindLastCharInSet(nsString& aSet,PRInt32 anOffset) const{
PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{
if(aSet.Length()) {
for(PRInt32 i=mLength-1;i>0;i--){
PRInt32 pos=aSet.Find(mStr[i]);
@ -2038,6 +2042,18 @@ void nsString::SelfTest(void) {
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString sourceString("Hello how are you");
nsString subString("you");
nsString replacementStr("xxx");
PRInt32 offset = sourceString.Find(subString);
sourceString.Cut(offset, subString.Length());
sourceString.Insert(replacementStr, offset, replacementStr.Length()); // Offset isn't checked in Insert either
//**********************************************
//Now let's test a few string COMPARISION ops...
//**********************************************
nsString temp8("aaaa");
nsString temp8a("AAAA");
nsString temp9("bbbb");
@ -2183,10 +2199,10 @@ void nsString::SelfTest(void) {
pos=temp15.Find(PRUnichar('r'));
NS_ASSERTION(pos==17,"Error: Find char routine");
pos=temp15.FindFirstCharInSet("12k");
pos=temp15.FindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindFirstInChar routine");
pos=temp15.FindLastCharInSet("12k");
pos=temp15.RFindCharInSet("12k");
NS_ASSERTION(pos==10,"Error: FindLastInChar routine");
pos=temp15.RFind("abc");

Просмотреть файл

@ -16,15 +16,19 @@
* Reserved.
*/
/**
* MODULE NOTES:
* LAST MODS: gess 28Feb98
*
* This very simple string class that knows how to do
* efficient (dynamic) resizing. It offers almost no
* i18n support, and will undoubtedly have to be replaced.
*
*/
/***********************************************************************
MODULE NOTES:
A. There are two philosophies to building string classes:
1. Hide the underlying buffer & offer API's allow indirect iteration
2. Reveal underlying buffer, risk corruption, but gain performance
We chose the second option for performance reasons.
B Our internal buffer always holds capacity+1 bytes.
***********************************************************************/
#ifndef _NSSTRING
#define _NSSTRING
@ -40,156 +44,697 @@ class nsISizeOfHandler;
class NS_BASE nsString {
public:
nsString();
nsString(const char* anISOLatin1);
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
/**
* Default constructor. Note that we actually allocate a small buffer
* to begin with. This is because the "philosophy" of the string class
* was to allow developers direct access to the underlying buffer for
* performance reasons.
*/
nsString();
public:
virtual ~nsString();
/**
* This constructor accepts an isolatin string
* @param anISOLatin1 is a ptr to a 1-byte cstr
*/
nsString(const char* anISOLatin1);
PRInt32 Length() const { return mLength; }
/**
* This is our copy constructor
* @param reference to another nsString
*/
nsString(const nsString&);
void SetLength(PRInt32 aLength);
void Truncate(PRInt32 anIndex=0);
virtual void EnsureCapacityFor(PRInt32 aNewLength);
/**
* Constructor from a unicode string
* @param anicodestr pts to a unicode string
*/
nsString(const PRUnichar* aUnicode);
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
PRBool IsOrdered(void) const;
/**
* Virtual Destructor
*/
virtual ~nsString();
///accessor methods
//@{
const PRUnichar* GetUnicode(void) const;
operator PRUnichar*() const;
PRUnichar operator()(PRInt32 i) const;
PRUnichar& operator[](PRInt32 i) const;
PRUnichar& CharAt(PRInt32 anIndex) const;
PRUnichar& First() const;
PRUnichar& Last() const;
/**
* Retrieve the length of this string
* @return string length
*/
PRInt32 Length() const { return mLength; }
//string creation methods...
nsString operator+(const nsString& aString);
nsString operator+(const char* anISOLatin1);
nsString operator+(char aChar);
nsString operator+(const PRUnichar* aBuffer);
nsString operator+(PRUnichar aChar);
void ToLowerCase();
void ToLowerCase(nsString& aString) const;
void ToUpperCase();
void ToUpperCase(nsString& aString) const;
/**
* Sets the new length of the string.
* @param aLength is new string length.
* @return nada
*/
void SetLength(PRInt32 aLength);
nsString* ToNewString() const;
char* ToNewCString() const;
/**
* This method truncates this string to given length.
*
* @param anIndex -- new length of string
* @return nada
*/
void Truncate(PRInt32 anIndex=0);
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
void Copy(nsString& aString) const;
PRUnichar* ToNewUnicode() const;
float ToFloat(PRInt32* aErrorCode) const;
PRInt32 ToInteger(PRInt32* aErrorCode) const;
//@}
/**
* This method gets called when the internal buffer needs
* to grow to a given size.
* @param aNewLength -- new capacity of string
* @return void
*/
virtual void EnsureCapacityFor(PRInt32 aNewLength);
///string manipulation methods...
//@{
/**
*
* @param
*/
virtual void SizeOf(nsISizeOfHandler* aHandler) const;
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* Determine whether or not the characters in this
* string are in sorted order.
*
* @return TRUE if ordered.
*/
PRBool IsOrdered(void) const;
nsString& operator=(const nsString& aString);
nsString& operator=(const char* anISOLatin1);
nsString& operator=(char aChar);
nsString& operator=(const PRUnichar* aBuffer);
nsString& operator=(PRUnichar aChar);
nsString& operator+=(const nsString& aString);
nsString& operator+=(const char* anISOLatin1);
nsString& operator+=(const PRUnichar* aBuffer);
nsString& operator+=(PRUnichar aChar);
/**********************************************************************
Accessor methods...
*********************************************************************/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
nsString& Append(char aChar);
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
nsString& Append(PRUnichar aChar);
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
nsString& Append(float aFloat);
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/**
* Retrieve pointer to internal string value
* @return PRUnichar* to internal string
*/
const PRUnichar* GetUnicode(void) const;
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
nsString& StripChars(const char* aSet);
nsString& StripWhitespace();
nsString& Trim( const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
static PRBool IsSpace(PRUnichar ch);
static PRBool IsAlpha(PRUnichar ch);
static PRBool IsDigit(PRUnichar ch);
/**
*
* @param
* @return
*/
operator PRUnichar*() const;
//@}
/**
* Retrieve unicode char at given index
* @param offset into string
* @return PRUnichar* to internal string
*/
PRUnichar operator()(PRInt32 anIndex) const;
///searching methods...
//@{
PRInt32 BinarySearch(PRUnichar aChar) const;
PRInt32 Find(const char* anISOLatin1) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
PRInt32 Find(const nsString& aString) const;
PRInt32 FindFirstCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindFirstCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(const char* anISOLatin1Set,PRInt32 offset=0) const;
PRInt32 FindLastCharInSet(nsString& aString,PRInt32 offset=0) const;
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
//@}
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& operator[](PRInt32 anIndex) const;
///comparision methods...
//@{
virtual PRInt32 Compare(const nsString &S,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *anISOLatin1,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* Retrieve reference to unicode char at given index
* @param offset into string
* @return PRUnichar& from internal string
*/
PRUnichar& CharAt(PRInt32 anIndex) const;
PRBool operator==(const nsString &S) const;
PRBool operator==(const char *anISOLatin1) const;
PRBool operator==(const PRUnichar* aString) const;
PRBool operator!=(const nsString &S) const;
PRBool operator!=(const char *anISOLatin1) const;
PRBool operator!=(const PRUnichar* aString) const;
PRBool operator<(const nsString &S) const;
PRBool operator<(const char *anISOLatin1) const;
PRBool operator<(const PRUnichar* aString) const;
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Retrieve reference to first unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& First() const;
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Retrieve reference to last unicode char in string
* @return PRUnichar from internal string
*/
PRUnichar& Last() const;
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* anISOLatin1,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
//@}
/**********************************************************************
String creation methods...
*********************************************************************/
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
/**
* Create a new string by appending given string to this
* @param aString -- 2nd string to be appended
* @return new string
*/
nsString operator+(const nsString& aString);
/**
* create a new string by adding this to the given buffer.
* @param anISOLatin1 is a ptr to cstring to be added to this
* @return newly created string
*/
nsString operator+(const char* anISOLatin1);
/**
* create a new string by adding this to the given char.
* @param aChar is a char to be added to this
* @return newly created string
*/
nsString operator+(char aChar);
/**
* create a new string by adding this to the given buffer.
* @param aStr unichar buffer to be added to this
* @return newly created string
*/
nsString operator+(const PRUnichar* aBuffer);
/**
* create a new string by adding this to the given char.
* @param aChar is a unichar to be added to this
* @return newly created string
*/
nsString operator+(PRUnichar aChar);
/**
* Converts all chars in internal string to lower
*/
void ToLowerCase();
/**
* Converts all chars in given string to lower
*/
void ToLowerCase(nsString& aString) const;
/**
* Converts all chars in given string to upper
*/
void ToUpperCase();
/**
* Converts all chars in internal string to upper
*/
void ToUpperCase(nsString& aString) const;
/**
* Creates a duplicate clone (ptr) of this string.
* @return ptr to clone of this string
*/
nsString* ToNewString() const;
/**
* Creates an ISOLatin1 clone of this string
* @return ptr to new isolatin1 string
*/
char* ToNewCString() const;
/**
* Copies data from internal buffer onto given char* buffer
* @param aBuf is the buffer where data is stored
* @param aBuflength is the max # of chars to move to buffer
* @return ptr to given buffer
*/
char* ToCString(char* aBuf,PRInt32 aBufLength) const;
/**
* Copies contents of this onto given string.
* @param aString to hold copy of this
* @return nada.
*/
void Copy(nsString& aString) const;
/**
* Creates an unichar clone of this string
* @return ptr to new unichar string
*/
PRUnichar* ToNewUnicode() const;
/**
* Perform string to float conversion.
* @param aErrorCode will contain error if one occurs
* @return float rep of string value
*/
float ToFloat(PRInt32* aErrorCode) const;
/**
* Perform string to int conversion.
* @param aErrorCode will contain error if one occurs
* @return int rep of string value
*/
PRInt32 ToInteger(PRInt32* aErrorCode) const;
/**********************************************************************
String manipulation methods...
*********************************************************************/
/**
* assign given PRUnichar* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& SetString(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* assign given string to this one
* @param aString: string to be added to this
* @return this
*/
nsString& operator=(const nsString& aString);
/**
* assign given char* to this string
* @param anISOLatin1: buffer to be assigned to this
* @return this
*/
nsString& operator=(const char* anISOLatin1);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(char aChar);
/**
* assign given unichar* to this string
* @param aBuffer: unichar buffer to be assigned to this
* @return this
*/
nsString& operator=(const PRUnichar* aBuffer);
/**
* assign given char to this string
* @param aChar: char to be assignd to this
* @return this
*/
nsString& operator=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& operator+=(const nsString& aString);
/**
* append given buffer to this string
* @param anISOLatin1: buffer to be appended to this
* @return this
*/
nsString& operator+=(const char* anISOLatin1);
/**
* append given buffer to this string
* @param aBuffer: buffer to be appended to this
* @return this
*/
nsString& operator+=(const PRUnichar* aBuffer);
/**
* append given char to this string
* @param aChar: char to be appended to this
* @return this
*/
nsString& operator+=(PRUnichar aChar);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const nsString& aString,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const char* anISOLatin1,PRInt32 aLength=-1);
/**
* append given string to this string
* @param aString : string to be appended to this
* @return this
*/
nsString& Append(char aChar);
/**
* append given unichar buffer to this string
* @param aString : string to be appended to this
* @param alength is the length of the given str (or -1)
if you want me to determine its length
* @return this
*/
nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1);
/**
* append given unichar character to this string
* @param aChar is the char to be appended to this
* @return this
*/
nsString& Append(PRUnichar aChar);
/**
* Append an integer onto this string
* @param aInteger is the int to be appended
* @param aRadix specifies 8,10,16
* @return this
*/
nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16
/**
* Append a float value onto this string
* @param aFloat is the float to be appended
* @return this
*/
nsString& Append(float aFloat);
/*
* Copies n characters from this string to given string,
* starting at the leftmost offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Left(nsString& aCopy,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at the given offset.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @param anOffset -- position where copying begins
* @return number of chars copied
*/
PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount);
/*
* Copies n characters from this string to given string,
* starting at rightmost char.
*
*
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
PRInt32 Right(nsString& aCopy,PRInt32 aCount);
/*
* This method inserts n chars from given string into this
* string at str[anOffset].
*
* @param aCopy -- String to be inserted into this
* @param anOffset -- insertion position within this str
* @param aCount -- number of chars to be copied from aCopy
* @return number of chars inserted into this.
*/
PRInt32 Insert(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1);
/**
* Insert a single unicode char into this string at
* a specified offset.
*
* @param aChar char to be inserted into this string
* @param anOffset is insert pos in str
* @return the number of chars inserted into this string
*/
PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset);
/*
* This method is used to cut characters in this string
* starting at anOffset, continuing for aCount chars.
*
* @param anOffset -- start pos for cut operation
* @param aCount -- number of chars to be cut
* @return *this
*/
nsString& Cut(PRInt32 anOffset,PRInt32 aCount);
/**
* This method is used to remove all occurances of the
* characters found in aSet from this string.
*
* @param aSet -- characters to be cut from this
* @return *this
*/
nsString& StripChars(const char* aSet);
/**
* This method strips whitespace throughout the string
*
* @return this
*/
nsString& StripWhitespace();
/**
* This method trims characters found in aTrimSet from
* either end of the underlying string.
*
* @param aTrimSet -- contains chars to be trimmed from
* both ends
* @return this
*/
nsString& Trim(const char* aSet,
PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* This method strips whitespace from string.
* You can control whether whitespace is yanked from
* start and end of string as well.
*
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,
PRBool aEliminateTrailing=PR_TRUE);
/**
* 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);
/**********************************************************************
Searching methods...
*********************************************************************/
/**
* Search for given character within this string.
* This method does so by using a binary search,
* so your string HAD BETTER BE ORDERED!
*
* @param aChar is the unicode char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 BinarySearch(PRUnichar aChar) const;
/**
* Search for given substring within this string
*
* @param aString is substring to be sought in this
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(const char* aString) const;
PRInt32 Find(const PRUnichar* aString) const;
PRInt32 Find(const nsString& aString) const;
/**
* Search for given char within this string
*
* @param aChar - char to be found
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const;
/**
* This method searches this string for the first character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This method searches this string for the last character
* found in the given string
* @param aString contains set of chars to be found
* @param anOffset tells us where to start searching in this
* @return -1 if not found, else the offset in this
*/
PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const;
PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const;
/**
* This methods scans the string backwards, looking for the given string
* @param aString is substring to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(const char* anISOLatin1,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE) const;
/**
* This methods scans the string backwards, looking for the given char
* @param char is the char to be sought in this
* @param aIgnoreCase tells us whether or not to do caseless compare
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const;
/**********************************************************************
Comparison methods...
*********************************************************************/
/**
* Compares a given string type to this string.
* @update gess 7/27/98
* @param S is the string to be compared
* @param aIgnoreCase tells us how to treat case
* @return -1,0,1
*/
virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const;
virtual PRInt32 Compare(const char *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const;
/**
* These methods compare a given string type to this one
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator==(const nsString &aString) const;
PRBool operator==(const char *aString) const;
PRBool operator==(const PRUnichar* aString) const;
/**
* These methods perform a !compare of a given string type to this
* @param aString is the string to be compared to this
* @return TRUE
*/
PRBool operator!=(const nsString &aString) const;
PRBool operator!=(const char *aString) const;
PRBool operator!=(const PRUnichar* aString) const;
/**
* These methods test if a given string is < than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<(const nsString &aString) const;
PRBool operator<(const char *aString) const;
PRBool operator<(const PRUnichar* aString) const;
/**
* These methods test if a given string is > than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>(const nsString &S) const;
PRBool operator>(const char *anISOLatin1) const;
PRBool operator>(const PRUnichar* aString) const;
/**
* These methods test if a given string is <= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator<=(const nsString &S) const;
PRBool operator<=(const char *anISOLatin1) const;
PRBool operator<=(const PRUnichar* aString) const;
/**
* These methods test if a given string is >= than this
* @param aString is the string to be compared to this
* @return TRUE or FALSE
*/
PRBool operator>=(const nsString &S) const;
PRBool operator>=(const char *anISOLatin1) const;
PRBool operator>=(const PRUnichar* aString) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool Equals(const nsString& aString) const;
PRBool Equals(const char* aString,PRInt32 aLength=-1) const;
PRBool Equals(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const;
/**
* Compare this to given string; note that we compare full strings here.
* The optional length argument just lets us know how long the given string is.
* If you provide a length, it is compared to length of this string as an
* optimization.
*
* @param aString -- the string to compare to this
* @param aLength -- optional length of given string.
* @return TRUE if equal
*/
PRBool EqualsIgnoreCase(const nsString& aString) const;
PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const;
PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const;
/**
* Compares to unichar string ptrs to each other without respect to case
* @param s1 is a ptr to a unichar buffer
* @param s2 is a ptr to a unichar buffer
* @return TRUE if they match
*/
PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const;
static void SelfTest();
virtual void DebugDump(ostream& aStream) const;
protected: