update nsStringX classes; not part of build

This commit is contained in:
rickg%netscape.com 2000-04-05 06:11:03 +00:00
Родитель 8a75ea3668
Коммит 359508091b
6 изменённых файлов: 236 добавлений и 1493 удалений

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

@ -15,6 +15,13 @@
* 03.10.2000: Fixed off-by-1 error in StripChar() (rickg)
* 03.20.2000: Ran regressions by comparing operations against original nsString (rickg)
*
* To Do:
*
* 1. Use shared buffer for empty strings
* 2. Test refcounting
* 3. Implement copy semantics between autostring and nsCSubsumeString to ensure
* that actual character copying occurs if necessary.
*
*************************************************************************************/
#ifndef NS_BUFFERMANAGER_
@ -198,14 +205,15 @@ inline nsresult SVRealloc(nsStringValueImpl<CharType>&aDest,PRUint32 aCount){
nsresult result=SVAlloc(temp,aCount);
if(NS_SUCCEEDED(result)) {
if(0<aDest.mLength) {
memcpy(temp.mBuffer,aDest.mBuffer,aDest.mLength*sizeof(CharType));
temp.mLength=aDest.mLength;
temp.mBuffer[temp.mLength]=0;
if(aDest.mBuffer) {
if(0<aDest.mLength) {
memcpy(temp.mBuffer,aDest.mBuffer,aDest.mLength*sizeof(CharType));
temp.mLength=aDest.mLength;
}
SVFree(aDest);
}
SVFree(aDest);
aDest=temp;
aDest.mBuffer[aDest.mLength]=0;
}
return result;
}

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

@ -1,5 +1,4 @@
#include "nsString2.h"
#include "nsString.h"
#include "nsBufferManager.h"
#include <stdio.h>
@ -13,32 +12,49 @@ static const char* kWhitespace="\b\t\r\n ";
// Ctor's
//******************************************
void Subsume(nsStringValueImpl<PRUnichar> &aDest,nsStringValueImpl<PRUnichar> &aSource){
if(aSource.mBuffer && aSource.mLength) {
#if 0
if(aSource.mOwnsBuffer){
nsStr::Destroy(aDest);
aDest.mStr=aSource.mStr;
aDest.mLength=aSource.mLength;
aDest.mCapacity=aSource.mCapacity;
aDest.mOwnsBuffer=aSource.mOwnsBuffer;
aSource.mOwnsBuffer=PR_FALSE;
aSource.mStr=0;
}
else{
nsStr::StrAssign(aDest,aSource,0,aSource.mLength);
}
#endif
}
else SVTruncate(aDest,0);
}
nsString::nsString() : mStringValue() {
}
//call this version for nsString,nsString and the autostrings
nsString::nsString(const nsString& aString,PRInt32 aLength) : mStringValue() {
Assign(aString,aLength,0);
SVAppend(mStringValue,aString.mStringValue,aLength,0);
}
//call this version with nsCString
nsString::nsString(const nsCString &aString,PRInt32 aLength) : mStringValue() {
Assign(aString,aLength);
SVAppend(mStringValue,aString.mStringValue,aLength,0);
}
//call this version for a single char of type char...
nsString::nsString(const nsSubsumeStr &aSubsumeString) : mStringValue(aSubsumeString.mLHS) {
SVAppend(mStringValue,aSubsumeString.mRHS,aSubsumeString.mRHS.mLength,0);
}
//call this version for a single char of type char...
nsString::nsString(nsSubsumeStr &aSubsumeString) : mStringValue(aSubsumeString.mLHS) {
SVAppend(mStringValue,aSubsumeString.mRHS,aSubsumeString.mRHS.mLength,0);
nsString::nsString(nsSubsumeStr &aSubsumeString) : mStringValue() {
mStringValue=aSubsumeString.mStringValue;
memset(&aSubsumeString.mStringValue,0,sizeof(mStringValue));
}
//call this version for char*'s....
nsString::nsString(const PRUnichar* aString,PRInt32 aLength) : mStringValue() {
Assign(aString,aLength,0);
Append(aString,aLength,0);
}
//call this version for a single char of type char...
@ -49,8 +65,8 @@ nsString::nsString(const char aChar) : mStringValue() {
//call this version for PRUnichar*'s....
nsString::nsString(const char* aString,PRInt32 aLength) : mStringValue() {
nsStringValueImpl<char> theString(CONST_CAST(char*,aString),aLength);
SVAssign(mStringValue,theString,theString.mLength,0);
nsStringValueImpl<char> theString(NS_CONST_CAST(char*,aString),aLength);
SVAppend(mStringValue,theString,theString.mLength,0);
}
//call this version for all other ABT versions of readable strings
@ -58,12 +74,10 @@ nsString::nsString(const nsAReadableString &aString) : mStringValue() {
Assign(aString);
}
//call this version for stack-based string buffers...
nsString::nsString(const nsUStackBuffer &aBuffer) : mStringValue(aBuffer) {
nsString::~nsString() {
SVFree(mStringValue);
}
nsString::~nsString() { }
void nsString::Reinitialize(PRUnichar* aBuffer,PRUint32 aCapacity,PRInt32 aLength) {
mStringValue.mBuffer=aBuffer;
mStringValue.mCapacity=aCapacity;
@ -88,21 +102,22 @@ nsString& nsString::operator=(const nsCString &aString) {
}
nsString& nsString::operator=(const nsSubsumeStr &aSubsumeString) {
//XXX NOT IMPLEMENTED
mStringValue=aSubsumeString.mStringValue;
memset((void*)&aSubsumeString.mStringValue,0,sizeof(mStringValue));
return *this;
}
nsString& nsString::operator=(const PRUnichar *aString) {
if(mStringValue.mBuffer!=aString) {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString));
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString));
Assign(aString);
}
return *this;
}
nsString& nsString::operator=(const char *aString) {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
SVAssign(mStringValue,theStringValue,theStringValue.mLength,0);
return *this;
}
@ -234,7 +249,7 @@ nsresult nsString::Append(const char* aString,PRInt32 aLength,PRUint32 aSrcOffse
nsresult result=NS_OK;
if(aString) {
if(aLength<0) aLength=stringlen(aString);
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aLength);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aLength);
result=SVAppend(mStringValue,theStringValue,aLength,aSrcOffset);
}
return result;
@ -276,7 +291,7 @@ nsresult nsString::Append(const PRUnichar aChar) {
nsresult nsString::Append(const PRUnichar* aString,PRInt32 aLength,PRUint32 aSrcOffset) {
nsresult result=NS_OK;
if(aString) {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString),aLength);
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString),aLength);
result=SVAppend (mStringValue,theStringValue,aLength,aSrcOffset);
}
return result;
@ -416,7 +431,7 @@ nsresult nsString::Insert(const char* aString,PRUint32 anOffset,PRInt32 aLength)
nsresult result=NS_OK;
if(aString){
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aLength);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aLength);
if(0<theStringValue.mLength){
result=SVInsert(mStringValue,anOffset,theStringValue,theStringValue.mLength,0);
@ -440,7 +455,7 @@ nsresult nsString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aLe
nsresult result=NS_OK;
if(aString){
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString),aLength);
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString),aLength);
if(0<theStringValue.mLength){
result=SVInsert(mStringValue,anOffset,theStringValue,theStringValue.mLength,0);
@ -500,8 +515,8 @@ nsresult nsString::ReplaceSubstring( const nsString& aTarget,const nsString& aNe
nsresult nsString::ReplaceSubstring(const char* aTarget,const char* aNewValue) {
nsresult result=NS_OK;
const nsStringValueImpl<char> theTarget(CONST_CAST(char*,aTarget));
const nsStringValueImpl<char> theNewValue(CONST_CAST(char*,aNewValue));
const nsStringValueImpl<char> theTarget(NS_CONST_CAST(char*,aTarget));
const nsStringValueImpl<char> theNewValue(NS_CONST_CAST(char*,aNewValue));
return SVReplace(mStringValue,theTarget,theNewValue,mStringValue.mLength,0);
}
@ -545,7 +560,7 @@ nsresult nsString::StripChar(PRInt32 anInt,PRUint32 anOffset){
* @return *this
*/
nsresult nsString::StripChars(const char* aSet,PRInt32 aLength){
nsStringValueImpl<char> theSet(CONST_CAST(char*,aSet),aLength);
nsStringValueImpl<char> theSet(NS_CONST_CAST(char*,aSet),aLength);
return SVStripCharsInSet(mStringValue,aSet,mStringValue.mLength,0);
}
@ -585,23 +600,29 @@ nsresult nsString::Right(nsString& aCopy,PRInt32 aCount) const {
//******************************************
nsSubsumeStr nsString::operator+(const nsString &aString) {
nsSubsumeStr result(mStringValue,aString.mStringValue);
return result;
nsString theString(*this);
SVAppend(theString.mStringValue,aString.mStringValue);
return nsSubsumeStr(theString);
}
nsSubsumeStr nsString::operator+(const PRUnichar* aString) {
nsSubsumeStr result; //NOT IMPLEMENTED
return result;
nsString theTempString(*this);
nsStringValueImpl<PRUnichar> theTempStrValue(NS_CONST_CAST(PRUnichar*,aString));
SVAppend(theTempString.mStringValue,theTempStrValue);
return nsSubsumeStr(theTempString);
}
nsSubsumeStr nsString::operator+(const char* aCString) {
nsSubsumeStr result; //NOT IMPLEMENTED
return result;
nsSubsumeStr nsString::operator+(const char* aString) {
nsString theTempString(*this);
nsStringValueImpl<char> theTempStrValue(NS_CONST_CAST(char*,aString));
SVAppend(theTempString.mStringValue,theTempStrValue);
return nsSubsumeStr(theTempString);
}
nsSubsumeStr nsString::operator+(PRUnichar aChar) {
nsSubsumeStr result; //NOT IMPLEMENTED
return result;
nsString theTempString(*this);
theTempString.Append(aChar);
return nsSubsumeStr(theTempString);
}
//*******************************************
// Here come the operator+=() methods...
@ -667,12 +688,12 @@ PRInt32 nsString::Compare(const nsCString &aString,PRBool aIgnoreCase,PRInt32 a
}
PRInt32 nsString::Compare(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aCount);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aCount);
return SVCompare(mStringValue,theStringValue,aIgnoreCase,aCount);
}
PRInt32 nsString::Compare(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString),aCount);
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString),aCount);
return SVCompareChars(mStringValue,theStringValue,aIgnoreCase,aCount);
}
@ -685,13 +706,13 @@ PRBool nsString::Equals(const nsCString &aString,PRBool aIgnoreCase,PRInt32 aCo
}
PRBool nsString::Equals(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aCount);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aCount);
return PRBool(0==SVCompare(mStringValue,theStringValue,aIgnoreCase,aCount));
}
PRBool nsString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString),aCount);
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString),aCount);
return PRBool(0==SVCompareChars(mStringValue,theStringValue,aIgnoreCase,aCount));
}
@ -776,7 +797,7 @@ PRInt32 nsString::Find(const nsCString &aString,PRBool aIgnoreCase,PRUint32 anOf
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 nsString::Find(const char* aString,PRBool aIgnoreCase,PRUint32 anOffset,PRInt32 aRepCount) const {
nsStringValueImpl<char> theString(CONST_CAST(char*,aString));
nsStringValueImpl<char> theString(NS_CONST_CAST(char*,aString));
return SVFind(mStringValue,theString,aIgnoreCase,aRepCount,anOffset);
}
@ -792,7 +813,7 @@ PRInt32 nsString::Find(const char* aString,PRBool aIgnoreCase,PRUint32 anOffset,
*/
PRInt32 nsString::Find(const PRUnichar* aString,PRBool aIgnoreCase,PRUint32 anOffset,PRInt32 aRepCount) const {
nsStringValueImpl<PRUnichar> theString(CONST_CAST(PRUnichar*,aString));
nsStringValueImpl<PRUnichar> theString(NS_CONST_CAST(PRUnichar*,aString));
return SVFind(mStringValue,theString,aIgnoreCase,aRepCount,anOffset);
}
@ -821,7 +842,7 @@ PRInt32 nsString::FindCharInSet(const nsString& aString,PRUint32 anOffset) const
*/
PRInt32 nsString::FindCharInSet(const char *aString,PRUint32 anOffset) const{
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
return SVFindCharInSet(mStringValue,theStringValue,PR_FALSE,mStringValue.mLength,anOffset);
}
@ -836,7 +857,7 @@ PRInt32 nsString::FindCharInSet(const char *aString,PRUint32 anOffset) const{
*/
PRInt32 nsString::FindCharInSet(const PRUnichar *aString,PRUint32 anOffset) const{
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString));
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString));
return SVFindCharInSet(mStringValue,theStringValue,PR_FALSE,mStringValue.mLength,anOffset);
}
@ -889,13 +910,13 @@ PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase,PRInt32 anOff
}
PRInt32 nsString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aRepCount) const{
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
return SVRFind(mStringValue,theStringValue,aIgnoreCase,aRepCount,anOffset);
}
PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aRepCount) const{
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString));
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString));
return SVRFind(mStringValue,theStringValue,aIgnoreCase,aRepCount,anOffset);
}
/**
@ -921,7 +942,7 @@ PRInt32 nsString::RFindCharInSet(const nsString& aString,PRInt32 anOffset) const
* @return
*/
PRInt32 nsString::RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset) const{
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString));
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString));
return SVRFindCharInSet(mStringValue,theStringValue,PR_FALSE,mStringValue.mLength,anOffset);
}
@ -935,7 +956,7 @@ PRInt32 nsString::RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset) cons
* @return
*/
PRInt32 nsString::RFindCharInSet(const char* aString,PRInt32 anOffset) const{
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
return SVRFindCharInSet(mStringValue,theStringValue,PR_FALSE,mStringValue.mLength,anOffset);
}
@ -973,9 +994,7 @@ PRUnichar* nsString::ToNewUnicode() const {
char* nsString::ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset) const {
int len=(mStringValue.mLength<aBufLength) ? mStringValue.mLength : aBufLength;
for(int i=0;i<len;i++){
aBuf[i]=(char)mStringValue.mBuffer[i];
}
SVCopyBuffer_(aBuf,mStringValue.mBuffer,len);
aBuf[len]=0;
return aBuf;
}
@ -1031,54 +1050,17 @@ nsresult nsString::Append(const nsStringValueImpl<char> &aString,PRInt32 aLength
}
/*****************************************************************
Now we declare the nsCSubsumeString class
Now we define the nsSubsumeStr class
*****************************************************************/
nsSubsumeStr::nsSubsumeStr() {
nsSubsumeStr::nsSubsumeStr(nsString& aString) : nsString() {
Subsume(mStringValue,aString.mStringValue);
}
nsSubsumeStr::nsSubsumeStr(const nsString& aString) : mLHS(aString.mStringValue), mRHS() {
}
nsSubsumeStr::nsSubsumeStr(const nsSubsumeStr& aSubsumeString) :
mLHS(aSubsumeString.mLHS),
mRHS(aSubsumeString.mRHS) {
}
nsSubsumeStr::nsSubsumeStr(const nsStringValueImpl<PRUnichar> &aLHS,const nsSubsumeStr& aSubsumeString) :
mLHS(aLHS),
mRHS(aSubsumeString.mLHS) {
}
nsSubsumeStr::nsSubsumeStr(const nsStringValueImpl<PRUnichar> &aLHS,const nsStringValueImpl<PRUnichar> &aRHS) :
mLHS(aLHS),
mRHS(aRHS) {
}
nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) {
}
nsSubsumeStr nsSubsumeStr::operator+(const nsSubsumeStr &aSubsumeString) {
nsSubsumeStr result(*this);
return result;
}
nsSubsumeStr nsSubsumeStr::operator+(const nsString &aString) {
SVAppend(mLHS,mRHS,mRHS.mLength,0);
memcpy(&mRHS,&aString.mStringValue,sizeof(mRHS));
return *this;
}
int nsSubsumeStr::Subsume(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) {
//XXX NOT IMPLEMENTED
int result=0;
return result;
nsSubsumeStr::nsSubsumeStr(PRUnichar * aString,PRBool assumeOwnership,PRInt32 aLength) : nsString() {
// mStringValue.mBuffer=aString;
// mStringValue.mCapacity=mStringValue.mLength=(-1==aLength) ? strlen(aString) : aLength;
// mOwnsBuffer=assumeOwnership;
}
/**
@ -1136,7 +1118,7 @@ nsAutoString::nsAutoString() : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
mStringValue.mRefCount=2;
}
//call this version nsAutoString derivatives...
@ -1144,59 +1126,55 @@ nsAutoString::nsAutoString(const nsAutoString& aString,PRInt32 aLength) : nsStri
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
Assign(aString,aLength);
mStringValue.mRefCount=2;
nsString::Assign(aString,aLength,0);
}
//call this version for nsString and the autostrings
nsAutoString::nsAutoString(const nsString& aString,PRInt32 aLength) : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
Assign(aString,aLength);
mStringValue.mRefCount=2;
nsString::Assign(aString,aLength,0);
}
//call this version with nsCString
nsAutoString::nsAutoString(const nsCString& aString) : nsString(aString) {
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
nsAutoString::nsAutoString(const nsCString& aString) : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount=2;
nsString::Assign(aString);
}
//call this version for char*'s....
nsAutoString::nsAutoString(const char* aString,PRInt32 aLength) : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
nsStringValueImpl<char> theString(CONST_CAST(char*,aString),aLength);
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
SVAssign(mStringValue,theString,aLength,0);
mStringValue.mRefCount=2;; //so we don't try to delete our internal buffer
nsString::Assign(aString,aLength,0);
}
//call this version for a single char of type char...
nsAutoString::nsAutoString(char aChar) : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
PRUnichar theBuffer[]={aChar,0};
Assign(theBuffer,1,0);
mStringValue.mRefCount=2; //so we don't try to delete our internal buffer
nsString::Assign(aChar);
}
//call this version for a single char of type char...
nsAutoString::nsAutoString(PRUnichar aChar) : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
PRUnichar theBuffer[]={aChar,0};
Assign(theBuffer,1,0);
mStringValue.mRefCount=2; //so we don't try to delete our internal buffer
nsString::Assign(aChar);
}
@ -1205,9 +1183,8 @@ nsAutoString::nsAutoString(const PRUnichar* aString,PRInt32 aLength) : nsString(
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
nsStringValueImpl<PRUnichar> theString(CONST_CAST(PRUnichar*,aString),aLength);
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
SVAssign(mStringValue,theString,theString.mLength,0);
mStringValue.mRefCount=2; //so we don't try to delete our internal buffer
nsString::Assign(aString,aLength,0);
}
@ -1216,28 +1193,19 @@ nsAutoString::nsAutoString(const nsAReadableString &aString) : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
Assign(aString);
mStringValue.mRefCount=2; //so we don't try to delete our internal buffer
nsString::Assign(aString);
}
nsAutoString::nsAutoString(const nsUStackBuffer &aBuffer) : nsString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mLength=aBuffer.mLength;
mStringValue.mCapacity=aBuffer.mCapacity;
nsAutoString::nsAutoString(const UBufDescriptor &aBuffer) : nsString() {
mStringValue.mBuffer=aBuffer.mBuffer;
mStringValue.mRefCount++; //so we don't try to delete our internal buffer
}
nsAutoString::nsAutoString(const CBufDescriptor& aBuffer) : nsString() {
mStringValue.mBuffer=(PRUnichar*)aBuffer.mBuffer;
mStringValue.mCapacity=aBuffer.mCapacity;
mStringValue.mLength=aBuffer.mLength;
mStringValue.mRefCount=(aBuffer.mStackBased) ? 2 : 1;
mStringValue.mRefCount=(aBuffer.mOwnsBuffer) ? 1 : 2;
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
}
nsAutoString::nsAutoString(const nsSubsumeStr& aSubsumeStringX) : nsString() {
}
@ -1245,39 +1213,33 @@ nsAutoString::nsAutoString(const nsSubsumeStr& aSubsumeStringX) : nsString() {
nsAutoString::~nsAutoString() { }
nsAutoString& nsAutoString::operator=(const nsAutoString& aCopy) {
if(aCopy.mStringValue.mBuffer!=mStringValue.mBuffer) {
Assign(aCopy);
}
nsAutoString& nsAutoString::operator=(const nsAutoString& aString) {
nsString::operator=(aString);
return *this;
}
nsAutoString& nsAutoString::operator=(const nsString& aString) {
Assign(aString);
nsString::operator=(aString);
return *this;
}
nsAutoString& nsAutoString::operator=(const nsCString& aString) {
Assign(aString);
nsString::operator=(aString);
return *this;
}
nsAutoString& nsAutoString::operator=(const PRUnichar* aString) {
if(mStringValue.mBuffer!=aString) {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString));
Assign(aString);
}
nsString::operator=(aString);
return *this;
}
nsAutoString& nsAutoString::operator=(const char* aString) {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
SVAssign(mStringValue,theStringValue,theStringValue.mLength,0);
nsString::operator=(aString);
return *this;
}
nsAutoString& nsAutoString::operator=(const PRUnichar aChar) {
Assign(aChar);
nsString::operator=(aChar);
return *this;
}

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

@ -1,587 +0,0 @@
/*****************************************************************************************************************
*
* This template provides the basic implementation for nsString.
*
* Contributor(s):
* Rick Gessner <rickg@netscape.com>
*
* History:
*
* 02.29.2000: Original files (rickg)
* 03.02.2000: Expand the API's to be a bit more realistic (rickg):
* 1. Flesh out the interface to be compatible with old library
* 2. Enable both nsString-based interfaces and nsAReadableString interfaces
* 3. Build enough infrastructure to test nsString(s) and nsImmutableString constructions.
* 03.03.2000: Finished mapping original nsString functionality to template form (rickg)
*
*****************************************************************************************************************/
#ifndef _NS_STRING_
#define _NS_STRING_
#include "nsStringValue.h"
typedef nsStackBuffer<PRUnichar> nsUStackBuffer;
class NS_COM nsCString;
class NS_COM nsSubsumeStr;
class NS_COM nsString {
public:
//******************************************
// Ctor's
//******************************************
nsString();
//copy constructor
nsString(const nsString& aString,PRInt32 aLength=-1);
//call this version for nsCString
nsString(const nsCString& aString,PRInt32 aLength=-1);
//call this version for prunichar*'s....
nsString(const PRUnichar* aString,PRInt32 aLength=-1) ;
//call this version for a single char of type char...
nsString(const char aChar);
//call this version for a single char of type prunichar...
nsString(const PRUnichar aChar);
//call this version for stack-based string buffers...
nsString(const nsSubsumeStr &aSubsumeString);
//call this version for stack-based string buffers...
nsString(nsSubsumeStr &aSubsumeString);
//call this version for char's....
nsString(const char* aString,PRInt32 aLength=-1) ;
//call this version for all other ABT versions of readable strings
nsString(const nsAReadableString &aString) ;
//call this version for stack-based string buffers...
nsString(const nsUStackBuffer &aBuffer);
virtual ~nsString();
void Reinitialize(PRUnichar* aBuffer,PRUint32 aCapacity,PRInt32 aLength=-1);
//******************************************
// Concat operators
//******************************************
nsSubsumeStr operator+(const nsString &aString);
nsSubsumeStr operator+(const PRUnichar* aString);
nsSubsumeStr operator+(const char* aString);
nsSubsumeStr operator+(PRUnichar aChar);
//******************************************
// Assigment operators
//******************************************
nsString& operator=(const nsString& aString);
nsString& operator=(const nsCString& aString);
nsString& operator=(const nsSubsumeStr &aSubsumeString);
nsString& operator=(const PRUnichar* aString);
nsString& operator=(const char* aString);
nsString& operator=(const char aChar);
nsString& operator=(const PRUnichar aChar);
//******************************************
// Here are the simple accessor methods...
//******************************************
virtual nsresult SetLength(PRUint32 aLength) {
if(aLength>mStringValue.mLength)
SetCapacity(aLength);
Truncate(aLength);
return NS_OK;
}
virtual nsresult SetCapacity(PRUint32 aCapacity);
PRUnichar* GetUnicode() const { return mStringValue.mBuffer;}
PRUnichar operator[](PRUint32 aOffset) const {return CharAt(aOffset);}
// operator const char* const() {return mStringValue.mBuffer;}
PRUint32 Length() const {return mStringValue.mLength;}
PRUint32 Capacity() const {return mStringValue.mCapacity;}
size_t GetCharSize() const {return sizeof(char);}
PRBool IsUnicode() const {return PRBool(sizeof(PRUnichar)==sizeof(char));}
PRBool IsEmpty(void) const {return PRBool(mStringValue.mLength==0);}
PRUnichar CharAt(PRUint32 anIndex) const {
return (anIndex<mStringValue.mLength) ? mStringValue.mBuffer[anIndex] : 0;
}
PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex);
PRUnichar First(void) const {
return CharAt(0);
}
PRUnichar Last(void) const {
return CharAt(mStringValue.mLength-1);
}
virtual void SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const {
if (aResult) {
*aResult = sizeof(*this) + Capacity();
}
}
//******************************************
// Here are the Assignment methods,
// and other mutators...
//******************************************
virtual nsresult Truncate(PRUint32 anOffset=0);
/*
* This method assign from a stringimpl
* string at aString[anOffset].
*
* @update rickg 03.01.2000
* @param aString -- source String to be inserted into this
* @param aLength -- number of chars to be copied from aCopy
* @param aSrcOffset -- insertion position within this str
* @return this
*/
virtual nsresult Assign(const nsString& aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(const PRUnichar* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(const char* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
//assign from a stringValueImpl of a compatible type
virtual nsresult Assign(const nsCString& aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(const nsAReadableString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(char aChar);
virtual nsresult Assign(PRUnichar aChar);
nsString& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {Assign(aString, aLength); return *this;}
nsString& SetString(const nsString& aString,PRInt32 aLength=-1) {Assign(aString, aLength); return *this;}
//***************************************
// Here come the append methods...
//***************************************
/*
* This method appends a stringimpl starting at aString[aSrcOffset]
*
* @update rickg 03.01.2000
* @param aString -- source String to be inserted into this
* @param aLength -- number of chars to be copied from aCopy
* @param aSrcOffset -- insertion position within this str
* @return this
*/
virtual nsresult Append(const nsString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const nsCString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const PRUnichar* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const char* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const PRUnichar aChar) ;
virtual nsresult Append(const char aChar);
virtual nsresult Append(const nsAReadableString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(PRInt32 anInteger,PRInt32 aRadix=10);
virtual nsresult Append(float aFloat);
//***************************************
// Here come a few deletion methods...
//***************************************
nsresult Cut(PRUint32 anOffset,PRInt32 aCount);
nsresult Trim(const char* aTrimSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE,PRBool aIgnoreQuotes=PR_FALSE);
/**
* This method compresses multiple chars in a row into a single instance
* You can control whether chars are yanked from ends too.
*
* @update rickg 03.01.2000
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsresult CompressSet(const char* aSet, PRUnichar aChar,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.
*
* @update rickg 03.01.2000
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsresult CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE);
//***************************************
// Here come a wad of insert methods...
//***************************************
/*
* This method inserts n chars from given string into this
* string at str[anOffset].
*
* @update rickg 03.01.2000
* @param aString -- source String to be inserted into this
* @param anOffset -- insertion position within this str
* @param aCount -- number of chars to be copied from aCopy
* @return this
*/
nsresult Insert(const nsString& aString,PRUint32 anOffset=0,PRInt32 aCount=-1);
nsresult Insert(const char* aString,PRUint32 anOffset=0,PRInt32 aLength=-1);
nsresult Insert(const PRUnichar* aString,PRUint32 anOffset=0,PRInt32 aLength=-1);
nsresult Insert(char aChar,PRUint32 anOffset=0);
//*******************************************
// Here come inplace replacement methods...
//*******************************************
/**
* swaps occurence of 1 char for another
*
* @return this
*/
nsresult ReplaceChar(char anOldChar,char aNewChar);
nsresult ReplaceChar(const char* aSet,char aNewChar);
nsresult ReplaceSubstring( const nsString& aTarget,const nsString& aNewValue);
nsresult ReplaceSubstring(const char* aTarget,const char* aNewValue);
//*******************************************
// Here come the stripchar methods...
//*******************************************
/**
* This method is used to remove all occurances of the
* given character from this string.
*
* @update rickg 03.01.2000
* @param aChar -- char to be stripped
* @param anOffset -- where in this string to start stripping chars
* @return *this
*/
nsresult StripChar(PRUnichar aChar,PRUint32 anOffset=0);
nsresult StripChar(PRInt32 anInt,PRUint32 anOffset=0);
nsresult StripChars(const char* aSet,PRInt32 aLength=-1);
nsresult StripWhitespace();
//**************************************************
// Here are some methods that extract substrings...
//**************************************************
nsresult Left(nsString& aCopy,PRInt32 aCount) const;
nsresult Mid(nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) const;
nsresult Right(nsString& aCopy,PRInt32 aCount) const;
//*******************************************
// Here come the operator+=() methods...
//*******************************************
nsString& operator+=(const nsString& aString);
nsString& operator+=(const char* aString);
nsString& operator+=(const PRUnichar* aString);
nsString& operator+=(const char aChar);
nsString& operator+=(const PRUnichar aChar);
nsString& operator+=(const int anInt);
nsString& operator+=(const nsSubsumeStr &aSubsumeString) {
//XXX NOT IMPLEMENTED
return *this;
}
/***********************************
Comparison methods...
***********************************/
PRBool operator==(const nsString& aString) const {return Equals(aString);}
PRBool operator==(const nsCString& aString) const {return Equals(aString);}
PRBool operator==(const char* aString) const {return Equals(aString);}
PRBool operator==(char* aString) const {return Equals(aString);}
PRBool operator==(const PRUnichar* aString) const {return Equals(aString);}
PRBool operator==(PRUnichar* aString) const {return Equals(aString);}
PRBool operator!=(const nsString& aString) const {return PRBool(Compare(aString)!=0);}
PRBool operator!=(const nsCString& aString) const {return PRBool(Compare(aString)!=0);}
PRBool operator!=(const char* aString) const {return PRBool(Compare(aString)!=0);}
PRBool operator!=(const PRUnichar* aString) const {return PRBool(Compare(aString)!=0);}
PRBool operator<(const nsString& aString) const {return PRBool(Compare(aString)<0);}
PRBool operator<(const nsCString& aString) const {return PRBool(Compare(aString)<0);}
PRBool operator<(const char* aString) const {return PRBool(Compare(aString)<0);}
PRBool operator<(const PRUnichar* aString) const {return PRBool(Compare(aString)<0);}
PRBool operator>(const nsString& aString) const {return PRBool(Compare(aString)>0);}
PRBool operator>(const nsCString& aString) const {return PRBool(Compare(aString)>0);}
PRBool operator>(const char* aString) const {return PRBool(Compare(aString)>0);}
PRBool operator>(const PRUnichar* aString) const {return PRBool(Compare(aString)>0);}
PRBool operator<=(const nsString& aString) const {return PRBool(Compare(aString)<=0);}
PRBool operator<=(const nsCString& aString) const {return PRBool(Compare(aString)<=0);}
PRBool operator<=(const char* aString) const {return PRBool(Compare(aString)<=0);}
PRBool operator<=(const PRUnichar* aString) const {return PRBool(Compare(aString)<=0);}
PRBool operator>=(const nsString& aString) const {return PRBool(Compare(aString)>=0);}
PRBool operator>=(const nsCString& aString) const {return PRBool(Compare(aString)>=0);}
PRBool operator>=(const char* aString) const {return PRBool(Compare(aString)>=0);}
PRBool operator>=(const PRUnichar* aString) const {return PRBool(Compare(aString)>=0);}
PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRInt32 Compare(const nsCString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const ;
PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(const nsCString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(/*FIX: const */nsIAtom* anAtom,PRBool aIgnoreCase) const;
PRBool Equals(const PRUnichar* aLHS,const PRUnichar* aRHS,PRBool aIgnoreCase=PR_FALSE) const;
PRBool EqualsIgnoreCase(const nsString &aString) const {return Equals(aString,PR_TRUE);}
PRBool EqualsIgnoreCase(const nsCString &aString) const {return Equals(aString,PR_TRUE);}
PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const {return Equals(aString,PR_TRUE,aCount);}
PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const {return Equals(aString,PR_TRUE,aCount);}
PRBool EqualsIgnoreCase(const PRUnichar* aLHS, const PRUnichar* aRHS) const {return Equals(aLHS,aRHS,PR_TRUE);}
PRBool EqualsIgnoreCase(/*FIX: const */nsIAtom *aAtom) const {return Equals(aAtom,PR_TRUE);}
/***************************************
These are string searching methods...
***************************************/
/**
* search for given string within this string
*
* @update rickg 03.01.2000
* @param aString - substr to be found
* @param aIgnoreCase tells us whether or not to do caseless compare
* @param anOffset tells us where in this string to start searching
* @param aRepCount tells us how many iterations to make starting at the given offset
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(const nsString& aTarget,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 Find(const nsCString &aString,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 FindChar(char aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=0,PRInt32 aRepCount=-1) const ;
PRInt32 FindCharInSet(const nsString& aString,PRUint32 anOffset=0) const;
PRInt32 FindCharInSet(const char *aString,PRUint32 anOffset=0) const;
PRInt32 FindCharInSet(const PRUnichar *aString,PRUint32 anOffset=0) const;
PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFind(const nsCString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFind(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFindChar(char aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFindCharInSet(const nsString& aString,PRInt32 anOffset=-1) const ;
PRInt32 RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const;
PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const;
/***************************************
These convert to a different type
***************************************/
static void Recycle(nsString* aString);
static nsString* CreateString(void);
nsString* ToNewString() const;
char* ToNewCString() const;
char* ToNewUTF8String() const;
PRUnichar* ToNewUnicode() const;
char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const;
//******************************************
// Utility methods
//******************************************
PRInt32 CountChar(PRUnichar aChar);
//This will not work correctly for any unicode set other than ascii
void DebugDump(void) 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
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
* @return int rep of string value, and possible (out) error code
*/
PRInt32 ToInteger(PRInt32* anErrorCode,PRUint32 aRadix=kAutoDetect) const;
void ToLowerCase();
void ToLowerCase(nsString &aString) const;
void ToUpperCase();
void ToUpperCase(nsString &aString) const;
protected:
nsresult Append(const nsStringValueImpl<char> &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
nsStringValueImpl<PRUnichar> mStringValue;
friend class nsCString;
friend class nsSubsumeStr;
};
/*****************************************************************
Now we declare the nsSubsumeStr class
*****************************************************************/
class NS_COM nsSubsumeStr {
public:
nsSubsumeStr();
nsSubsumeStr(const nsString& aString);
nsSubsumeStr(const nsSubsumeStr& aSubsumeString);
nsSubsumeStr(const nsStringValueImpl<PRUnichar> &aLHS,const nsSubsumeStr& aSubsumeString);
nsSubsumeStr(const nsStringValueImpl<PRUnichar> &aLHS,const nsStringValueImpl<PRUnichar> &aSubsumeString);
nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1);
nsSubsumeStr operator+(const nsSubsumeStr &aSubsumeString);
nsSubsumeStr operator+(const nsString &aString);
operator const PRUnichar*() {return 0;}
int Subsume(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1);
nsStringValueImpl<PRUnichar> mLHS;
nsStringValueImpl<PRUnichar> mRHS;
};
/*****************************************************************
Now we declare the nsAutoString class
*****************************************************************/
class NS_COM nsAutoString : public nsString {
public:
nsAutoString();
//call this version nsAutoString derivatives...
nsAutoString(const nsAutoString& aString,PRInt32 aLength=-1);
//call this version for nsString and the autostrings
nsAutoString(const nsString& aString,PRInt32 aLength=-1);
//call this version with nsCString (start of COW)
nsAutoString(const nsCString& aString);
//call this version for char*'s....
nsAutoString(const char* aString,PRInt32 aLength=-1);
//call this version for a single char of type char...
nsAutoString(PRUnichar aChar);
nsAutoString(char aChar);
//call this version for PRUnichar*'s....
nsAutoString(const PRUnichar* aString,PRInt32 aLength=-1);
//call this version for all other ABT versions of readable strings
nsAutoString(const nsAReadableString &aString);
nsAutoString(const nsUStackBuffer &aBuffer) ;
nsAutoString(const CBufDescriptor& aBuffer) ;
nsAutoString(const nsSubsumeStr& aSubsumeStringX) ;
virtual ~nsAutoString();
nsAutoString& operator=(const nsAutoString& aCopy);
nsAutoString& operator=(const nsString& aString);
nsAutoString& operator=(const nsCString& aString);
nsAutoString& operator=(const PRUnichar* aString);
nsAutoString& operator=(const char* aString) ;
nsAutoString& operator=(const PRUnichar aChar);
nsAutoString& operator=(const nsSubsumeStr &aSubsumeString);
protected:
PRUnichar mInternalBuffer[kDefaultStringSize+1];
};
extern NS_COM int fputs(const nsString& aString, FILE* out);
extern PRUint32 HashCode(const nsString& aDest);
extern NS_COM void Recycle( PRUnichar* aBuffer);
#endif

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

@ -25,7 +25,6 @@
#include "nscore.h"
#include "nsIAllocator.h"
#include <string.h>
#include "nsCRT.h"
#if 0
typedef int PRInt32;
@ -97,74 +96,6 @@ inline size_t stringlen(const char* theString) {
return ::strlen(theString);
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
class NS_COM CBufDescriptor {
public:
CBufDescriptor(char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1) {
mBuffer=aString;
mCharSize=1;
mStackBased=aStackBased;
mIsConst=PR_FALSE;
mLength=mCapacity=0;
if(aString && aCapacity>1) {
mCapacity=aCapacity-1;
mLength=(-1==aLength) ? strlen(aString) : aLength;
if(mLength>PRInt32(mCapacity))
mLength=mCapacity;
}
}
CBufDescriptor(const char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1) {
mBuffer=(char*)aString;
mCharSize=1;
mStackBased=aStackBased;
mIsConst=PR_TRUE;
mLength=mCapacity=0;
if(aString && aCapacity>1) {
mCapacity=aCapacity-1;
mLength=(-1==aLength) ? stringlen(aString) : aLength;
if(mLength>PRInt32(mCapacity))
mLength=mCapacity;
}
}
CBufDescriptor(PRUnichar* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1) {
mBuffer=(char*)aString;
mCharSize=2;
mStackBased=aStackBased;
mLength=mCapacity=0;
mIsConst=PR_FALSE;
if(aString && aCapacity>1) {
mCapacity=aCapacity-1;
mLength=(-1==aLength) ? stringlen(aString) : aLength;
if(mLength>PRInt32(mCapacity))
mLength=mCapacity;
}
}
CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1) {
mBuffer=(char*)aString;
mCharSize=2;
mStackBased=aStackBased;
mLength=mCapacity=0;
mIsConst=PR_TRUE;
if(aString && aCapacity>1) {
mCapacity=aCapacity-1;
mLength=(-1==aLength) ? stringlen(aString) : aLength;
if(mLength>PRInt32(mCapacity))
mLength=mCapacity;
}
}
char* mBuffer;
PRUint32 mCapacity;
PRInt32 mLength;
PRBool mStackBased;
PRBool mIsConst;
char mCharSize;
};
//----------------------------------------------------------------------------------------
@ -218,9 +149,9 @@ template <class CharType>
struct nsStringValueImpl {
nsStringValueImpl() {
mBuffer=0;
mBuffer=(CharType*)gCommonEmptyBuffer;
mLength=mCapacity=0;
mRefCount=1;
mRefCount=2; //so we don't ever try to free the shared buffer
}
nsStringValueImpl(const nsStringValueImpl<CharType>& aCopy) {
@ -244,7 +175,7 @@ struct nsStringValueImpl {
mBuffer=theString;
}
else {
mBuffer=0;
mBuffer=(CharType*)gCommonEmptyBuffer;
mLength=mCapacity=0;
}
mRefCount=1;
@ -266,6 +197,7 @@ struct nsStringValueImpl {
void* GetBuffer() {return mBuffer;}
PRUint32 GetLength() {return mLength;}
PRUint32 GetCapacity() {return mCapacity;}
size_t GetCharSize() {return sizeof(CharType);}
CharType* mBuffer;

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

@ -1,5 +1,4 @@
#include "nsString2.h"
#include "nsString.h"
#include "nsBufferManager.h"
#include <stdio.h>
@ -9,6 +8,27 @@
static const char* kWhitespace="\b\t\r\n ";
void Subsume(nsStringValueImpl<char> &aDest,nsStringValueImpl<char> &aSource){
if(aSource.mBuffer && aSource.mLength) {
#if 0
if(aSource.mOwnsBuffer){
nsStr::Destroy(aDest);
aDest.mStr=aSource.mStr;
aDest.mLength=aSource.mLength;
aDest.mCapacity=aSource.mCapacity;
aDest.mOwnsBuffer=aSource.mOwnsBuffer;
aSource.mOwnsBuffer=PR_FALSE;
aSource.mStr=0;
}
else{
nsStr::StrAssign(aDest,aSource,0,aSource.mLength);
}
#endif
}
else SVTruncate(aDest,0);
}
//******************************************
// Ctor's
//******************************************
@ -18,33 +38,30 @@ nsCString::nsCString() : mStringValue() {
//call this to copy construct nsCString
nsCString::nsCString(const nsCString& aString,PRInt32 aLength) : mStringValue() {
Assign(aString,aLength,0);
SVAppend(mStringValue,aString.mStringValue,aLength,0);
}
//call this to construct from nsString
nsCString::nsCString(const nsString& aString,PRInt32 aLength) : mStringValue() {
Assign(aString,aLength,0);
SVAppend(mStringValue,aString.mStringValue,aLength,0);
}
//call this version for char*'s....
nsCString::nsCString(const char* aString,PRInt32 aLength) : mStringValue() {
Assign(aString,aLength);
Append(aString,aLength);
}
//call this version for PRUnichar*'s....
nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength) : mStringValue() {
Assign(aString,aLength,0);
Append(aString,aLength,0);
}
//call this version for a single char of type char...
nsCString::nsCString(const nsSubsumeCStr &aSubsumeString) : mStringValue(aSubsumeString.mLHS) {
SVAppend(mStringValue,aSubsumeString.mRHS,aSubsumeString.mRHS.mLength,0);
nsCString::nsCString(nsSubsumeCStr &aSubsumeString) : mStringValue() {
mStringValue=aSubsumeString.mStringValue;
memset(&aSubsumeString.mStringValue,0,sizeof(mStringValue));
}
//call this version for a single char of type char...
nsCString::nsCString(nsSubsumeCStr &aSubsumeString) : mStringValue(aSubsumeString.mLHS) {
SVAppend(mStringValue,aSubsumeString.mRHS,aSubsumeString.mRHS.mLength,0);
}
//call this version for a single char of type char...
nsCString::nsCString(const char aChar) : mStringValue() {
@ -56,12 +73,10 @@ nsCString::nsCString(const nsAReadableString &aString) : mStringValue() {
Assign(aString);
}
//call this version for stack-based string buffers...
nsCString::nsCString(const nsCStackBuffer &aBuffer) : mStringValue(aBuffer) {
nsCString::~nsCString() {
SVFree(mStringValue);
}
nsCString::~nsCString() { }
void nsCString::Reinitialize(char* aBuffer,PRUint32 aCapacity,PRInt32 aLength) {
mStringValue.mBuffer=aBuffer;
@ -76,18 +91,22 @@ void nsCString::Reinitialize(char* aBuffer,PRUint32 aCapacity,PRInt32 aLength) {
//******************************************
nsSubsumeCStr nsCString::operator+(const nsCString &aCString) {
nsSubsumeCStr result(mStringValue,aCString.mStringValue);
return result;
nsCString theString(*this);
SVAppend(theString.mStringValue,aCString.mStringValue);
return nsSubsumeCStr(theString);
}
nsSubsumeCStr nsCString::operator+(const char* aCString) {
nsSubsumeCStr result; //NOT IMPLEMENTED
return result;
nsSubsumeCStr nsCString::operator+(const char* aString) {
nsCString theTempString(*this);
nsStringValueImpl<char> theTempStrValue(NS_CONST_CAST(char*,aString));
SVAppend(theTempString.mStringValue,theTempStrValue);
return nsSubsumeCStr(theTempString);
}
nsSubsumeCStr nsCString::operator+(char aChar) {
nsSubsumeCStr result; //NOT IMPLEMENTED
return result;
nsCString theTempString(*this);
theTempString.Append(aChar);
return nsSubsumeCStr(theTempString);
}
//******************************************
@ -108,14 +127,14 @@ nsCString& nsCString::operator=(const nsString &aString) {
nsCString& nsCString::operator=(const char *aString) {
if(mStringValue.mBuffer!=aString) {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
SVAssign(mStringValue,theStringValue,theStringValue.mLength,0);
}
return *this;
}
nsCString& nsCString::operator=(const PRUnichar *aString) {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString));
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString));
Assign(aString);
return *this;
}
@ -126,7 +145,8 @@ nsCString& nsCString::operator=(const char aChar) {
}
nsCString& nsCString::operator=(const nsSubsumeCStr &aSubsumeString) {
//NOT IMPLEMENTED
mStringValue=aSubsumeString.mStringValue;
memset((void*)&aSubsumeString.mStringValue,0,sizeof(mStringValue));
return *this;
}
@ -176,11 +196,7 @@ nsresult nsCString::Truncate(PRUint32 anOffset) {
* @return this
*/
nsresult nsCString::Assign(const nsCString& aString,PRInt32 aLength,PRUint32 aSrcOffset) {
nsresult result=NS_OK;
if(mStringValue.mBuffer!=aString.mStringValue.mBuffer){
Truncate();
result=Append(aString,aLength,aSrcOffset);
}
nsresult result=SVAssign(mStringValue,aString.mStringValue,aLength,aSrcOffset);
return result;
}
@ -194,9 +210,8 @@ nsresult nsCString::Assign(const nsCString& aString,PRInt32 aLength,PRUint32 aSr
* @return this
*/
nsresult nsCString::Assign(const nsString& aString,PRInt32 aLength,PRUint32 aSrcOffset) {
nsresult result=NS_OK;
Truncate();
return Append(aString,aLength,aSrcOffset);
nsresult result=SVAssign(mStringValue,aString.mStringValue,aLength,aSrcOffset);
return result;
}
/*
@ -293,7 +308,7 @@ nsresult nsCString::Append(const nsString &aString,PRInt32 aLength,PRUint32 aSrc
nsresult nsCString::Append(const char* aString,PRInt32 aLength,PRUint32 aSrcOffset) {
nsresult result=NS_OK;
if(aString) {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aLength);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aLength);
result=SVAppend(mStringValue,theStringValue,aLength,aSrcOffset);
}
return result;
@ -339,7 +354,7 @@ nsresult nsCString::Append(const PRUnichar aChar) {
nsresult nsCString::Append(const PRUnichar* aString,PRInt32 aLength,PRUint32 aSrcOffset) {
nsresult result=NS_OK;
if(aString) {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString),aLength);
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString),aLength);
result=SVAppend(mStringValue,theStringValue,aLength,aSrcOffset);
}
return result;
@ -461,7 +476,7 @@ nsresult nsCString::Insert(const char* aString,PRUint32 anOffset,PRInt32 aLength
nsresult result=NS_OK;
if(aString){
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aLength);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aLength);
if(0<theStringValue.mLength){
result=SVInsert(mStringValue,anOffset,theStringValue,theStringValue.mLength,0);
@ -522,8 +537,8 @@ nsresult nsCString::ReplaceSubstring( const nsCString& aTarget,const nsCString&
nsresult nsCString::ReplaceSubstring(const char* aTarget,const char* aNewValue) {
nsresult result=NS_OK;
const nsStringValueImpl<char> theTarget(CONST_CAST(char*,aTarget));
const nsStringValueImpl<char> theNewValue(CONST_CAST(char*,aNewValue));
const nsStringValueImpl<char> theTarget(NS_CONST_CAST(char*,aTarget));
const nsStringValueImpl<char> theNewValue(NS_CONST_CAST(char*,aNewValue));
return SVReplace(mStringValue,theTarget,theNewValue,mStringValue.mLength,0);
}
@ -567,7 +582,7 @@ nsresult nsCString::StripChar(PRInt32 anInt,PRUint32 anOffset){
* @return *this
*/
nsresult nsCString::StripChars(const char* aSet,PRInt32 aLength){
nsStringValueImpl<char> theSet(CONST_CAST(char*,aSet),aLength);
nsStringValueImpl<char> theSet(NS_CONST_CAST(char*,aSet),aLength);
return SVStripCharsInSet(mStringValue,aSet,mStringValue.mLength,0);
}
@ -664,12 +679,12 @@ PRInt32 nsCString::Compare(const nsCString &aString,PRBool aIgnoreCase,PRInt32
}
PRInt32 nsCString::Compare(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aCount);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aCount);
return SVCompareChars(mStringValue,theStringValue,aIgnoreCase,aCount);
}
PRInt32 nsCString::Compare(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString),aCount);
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString),aCount);
return SVCompare(mStringValue,theStringValue,aIgnoreCase,aCount);
}
@ -683,13 +698,13 @@ PRBool nsCString::Equals(const nsCString &aString,PRBool aIgnoreCase,PRInt32 aC
}
PRBool nsCString::Equals(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString),aCount);
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString),aCount);
return PRBool(0==SVCompareChars(mStringValue,theStringValue,aIgnoreCase,aCount));
}
PRBool nsCString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString),aCount);
nsStringValueImpl<PRUnichar> theStringValue(NS_CONST_CAST(PRUnichar*,aString),aCount);
return PRBool(0==SVCompare(mStringValue,theStringValue,aIgnoreCase,aCount));
}
@ -742,7 +757,7 @@ PRInt32 nsCString::Find(const nsCString &aString,PRBool aIgnoreCase,PRUint32 anO
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 nsCString::Find(const char* aString,PRBool aIgnoreCase,PRUint32 anOffset,PRInt32 aRepCount) const {
nsStringValueImpl<char> theString(CONST_CAST(char*,aString));
nsStringValueImpl<char> theString(NS_CONST_CAST(char*,aString));
return SVFind(mStringValue,theString,aIgnoreCase,aRepCount,anOffset);
}
@ -758,7 +773,7 @@ PRInt32 nsCString::Find(const char* aString,PRBool aIgnoreCase,PRUint32 anOffset
*/
PRInt32 nsCString::Find(const PRUnichar* aString,PRBool aIgnoreCase,PRUint32 anOffset,PRInt32 aRepCount) const {
nsStringValueImpl<PRUnichar> theString(CONST_CAST(PRUnichar*,aString));
nsStringValueImpl<PRUnichar> theString(NS_CONST_CAST(PRUnichar*,aString));
return SVFind(mStringValue,theString,aIgnoreCase,aRepCount,anOffset);
}
@ -787,7 +802,7 @@ PRInt32 nsCString::FindCharInSet(const nsCString& aString,PRUint32 anOffset) con
*/
PRInt32 nsCString::FindCharInSet(const char *aString,PRUint32 anOffset) const{
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
return SVFindCharInSet(mStringValue,theStringValue,PR_FALSE,mStringValue.mLength,anOffset);
}
@ -821,7 +836,7 @@ PRInt32 nsCString::RFindChar(char aChar,PRBool aIgnoreCase,PRInt32 anOffset,PRIn
PRInt32 nsCString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aRepCount) const{
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
return SVRFind(mStringValue,theStringValue,aIgnoreCase,aRepCount,anOffset);
}
@ -850,7 +865,7 @@ PRInt32 nsCString::RFindCharInSet(const nsCString& aString,PRInt32 anOffset) con
* @return
*/
PRInt32 nsCString::RFindCharInSet(const char* aString,PRInt32 anOffset) const{
nsStringValueImpl<char> theStringValue(CONST_CAST(char*,aString));
nsStringValueImpl<char> theStringValue(NS_CONST_CAST(char*,aString));
return SVRFindCharInSet(mStringValue,theStringValue,PR_FALSE,mStringValue.mLength,anOffset);
}
@ -888,9 +903,7 @@ PRUnichar* nsCString::ToNewUnicode() const {
char* nsCString::ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset) const {
int len=(mStringValue.mLength<aBufLength) ? mStringValue.mLength : aBufLength;
for(int i=0;i<len;i++){
aBuf[i]=(char)mStringValue.mBuffer[i];
}
SVCopyBuffer_(aBuf,mStringValue.mBuffer,len);
aBuf[len]=0;
return aBuf;
}
@ -943,46 +956,23 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const{
/*****************************************************************
Now we declare the nsSubsumeCStr class
Now we define the nsSubsumeCStr class
*****************************************************************/
nsSubsumeCStr::nsSubsumeCStr() {
nsSubsumeCStr::nsSubsumeCStr(nsCString& aString) : nsCString() {
mStringValue=aString.mStringValue;
mOwnsBuffer=PR_TRUE;
aString.mStringValue.mBuffer=0;
aString.mStringValue.mRefCount=0;
}
nsSubsumeCStr::nsSubsumeCStr(const nsCString& aCString) : mLHS(aCString.mStringValue), mRHS() {
nsSubsumeCStr::nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() {
mStringValue.mBuffer=aString;
mStringValue.mCapacity=mStringValue.mLength=(-1==aLength) ? stringlen(aString) : aLength;
mOwnsBuffer=assumeOwnership;
}
nsSubsumeCStr::nsSubsumeCStr(const nsSubsumeCStr& aSubsumeString) :
mLHS(aSubsumeString.mLHS),
mRHS(aSubsumeString.mRHS) {
}
nsSubsumeCStr::nsSubsumeCStr(const nsStringValueImpl<char> &aLHS,const nsSubsumeCStr& aSubsumeString) :
mLHS(aLHS),
mRHS(aSubsumeString.mLHS) {
}
nsSubsumeCStr::nsSubsumeCStr(const nsStringValueImpl<char> &aLHS,const nsStringValueImpl<char> &aRHS) :
mLHS(aLHS),
mRHS(aRHS) {
}
nsSubsumeCStr::nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) {
}
nsSubsumeCStr nsSubsumeCStr::operator+(const nsSubsumeCStr &aSubsumeString) {
nsSubsumeCStr result(*this);
return result;
}
nsSubsumeCStr nsSubsumeCStr::operator+(const nsCString &aCString) {
SVAppend(mLHS,mRHS,mRHS.mLength,0);
memcpy(&mRHS,&aCString.mStringValue,sizeof(mRHS));
return *this;
}
/**
*
@ -1024,122 +1014,108 @@ void Recycle( char* aBuffer) {
nsCAutoString::nsCAutoString() : nsCString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mInternalBuffer[0]=0;
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
mStringValue.mRefCount=2;
}
//call this version nsAutoString derivatives...
nsCAutoString::nsCAutoString(const nsCAutoString& aString,PRInt32 aLength) : nsCString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mInternalBuffer[0]=0;
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
Assign(aString,aLength);
mStringValue.mRefCount=2;
nsCString::Assign(aString,aLength,0);
}
//call this version for nsCString,nsCString and the autostrings
nsCAutoString::nsCAutoString(const nsCString& aString,PRInt32 aLength) : nsCString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mInternalBuffer[0]=0;
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
Assign(aString,aLength);
mStringValue.mRefCount=2;
nsCString::Assign(aString,aLength,0);
}
//call this version for char*'s....
nsCAutoString::nsCAutoString(const char* aString,PRInt32 aLength) : nsCString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
nsCString theString(aString);
mInternalBuffer[0]=0;
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
Assign(theString,aLength);
mStringValue.mRefCount=2;
nsCString::Assign(aString,aLength,0);
}
//call this version for a single char of type char...
nsCAutoString::nsCAutoString(const char aChar) : nsCString() {
char theBuffer[]={aChar,0};
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mInternalBuffer[0]=0;
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
nsCString theString(theBuffer,1);
Assign(theString,1,0);
mStringValue.mRefCount=2;
nsCString::Assign(aChar);
}
//call this version for PRUnichar*'s....
nsCAutoString::nsCAutoString(const PRUnichar* aString,PRInt32 aLength) : nsCString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mInternalBuffer[0]=0;
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
nsStringValueImpl<PRUnichar> theString(CONST_CAST(PRUnichar*,aString),aLength);
SVAssign(mStringValue,theString,theString.mLength,0);
mStringValue.mRefCount=2;
nsCString::Assign(aString,aLength);
}
//call this version for all other ABT versions of readable strings
nsCAutoString::nsCAutoString(const nsAReadableString &aString) : nsCString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mInternalBuffer[0]=0;
mStringValue.mCapacity=kDefaultStringSize;
mStringValue.mBuffer=mInternalBuffer;
Assign(aString);
mStringValue.mRefCount=2;
nsCString::Assign(aString);
}
nsCAutoString::nsCAutoString(const nsCStackBuffer &aBuffer) : nsCString() {
memset(mInternalBuffer,0,sizeof(mInternalBuffer));
mStringValue.mLength=aBuffer.mLength;
mStringValue.mCapacity=aBuffer.mCapacity;
mStringValue.mBuffer=aBuffer.mBuffer;
}
nsCAutoString::nsCAutoString(const CBufDescriptor& aBuffer) : nsCString() {
nsCAutoString::nsCAutoString(const CBufDescriptor &aBuffer) : nsCString() {
mInternalBuffer[0]=0;
mStringValue.mBuffer=aBuffer.mBuffer;
mStringValue.mCapacity=aBuffer.mCapacity;
mStringValue.mLength=aBuffer.mLength;
mStringValue.mRefCount=(aBuffer.mStackBased) ? 2 : 1;
mStringValue.mRefCount=(aBuffer.mOwnsBuffer) ? 1 : 2;
}
nsCAutoString::nsCAutoString(const nsSubsumeCStr& aCSubsumeStringX) : nsCString() {
nsCAutoString::nsCAutoString(const nsSubsumeCStr& aSubsumeString) : nsCString() {
mInternalBuffer[0]=0;
mStringValue=aSubsumeString.mStringValue;
memset((void*)&aSubsumeString.mStringValue,0,sizeof(mStringValue));
}
nsCAutoString::~nsCAutoString() {
}
nsCAutoString& nsCAutoString::operator=(const nsCAutoString& aCopy) {
if(aCopy.mStringValue.mBuffer!=mStringValue.mBuffer) {
Assign(aCopy);
}
nsCAutoString& nsCAutoString::operator=(const nsCAutoString& aString) {
nsCString::operator=(aString);
return *this;
}
nsCAutoString& nsCAutoString::operator=(const nsCString& aString) {
Assign(aString);
nsCString::operator=(aString);
return *this;
}
nsCAutoString& nsCAutoString::operator=(const char* aString) {
if(mStringValue.mBuffer!=aString) {
nsCString theStringValue(CONST_CAST(char*,aString));
Assign(aString);
}
nsCString::operator=(aString);
return *this;
}
nsCAutoString& nsCAutoString::operator=(const PRUnichar* aString) {
nsStringValueImpl<PRUnichar> theStringValue(CONST_CAST(PRUnichar*,aString));
SVAssign(mStringValue,theStringValue,theStringValue.mLength,0);
nsCString::operator=(aString);
return *this;
}
nsCAutoString& nsCAutoString::operator=(const char aChar) {
Assign(aChar);
nsCString::operator=(aChar);
return *this;
}

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

@ -1,548 +0,0 @@
/*****************************************************************************************************************
*
* This template provides the basic implementation for nsCString.
*
* Contributor(s):
* Rick Gessner <rickg@netscape.com>
*
* History:
*
* 02.29.2000: Original files (rickg)
* 03.02.2000: Expand the API's to be a bit more realistic (rickg):
* 1. Flesh out the interface to be compatible with old library
* 2. Enable both nsCString-based interfaces and nsAReadableString interfaces
* 3. Build enough infrastructure to test nsCString(s) and nsImmutableString constructions.
* 03.03.2000: Finished mapping original nsCString functionality to template form (rickg)
*
*****************************************************************************************************************/
#ifndef _NS_CSTRING_
#define _NS_CSTRING_
#include "nsStringValue.h"
#include "nsString2.h"
class NS_COM nsSubsumeCStr;
typedef nsStackBuffer<char> nsCStackBuffer;
class NS_COM nsCString {
public:
//******************************************
// Ctor's
//******************************************
nsCString();
//copy constructor
nsCString(const nsCString& aString,PRInt32 aLength=-1);
//constructor from nsString
nsCString(const nsString& aString,PRInt32 aLength=-1);
//call this version for char*'s....
nsCString(const char* aString,PRInt32 aLength=-1) ;
//call this version for a single char
nsCString(const char aChar);
//call this version for PRUnichar*'s....
nsCString(const PRUnichar* aString,PRInt32 aLength=-1) ;
//call this version for stack-based string buffers...
nsCString(const nsSubsumeCStr &aSubsumeString);
//call this version for stack-based string buffers...
nsCString(nsSubsumeCStr &aSubsumeString);
//call this version for all other ABT versions of readable strings
nsCString(const nsAReadableString &aString) ;
//call this version for stack-based string buffers...
nsCString(const nsCStackBuffer &aBuffer);
virtual ~nsCString();
void Reinitialize(char* aBuffer,PRUint32 aCapacity,PRInt32 aLength=-1);
//******************************************
// Concat operators
//******************************************
nsSubsumeCStr operator+(const nsCString &aCString);
nsSubsumeCStr operator+(const char* aCString);
nsSubsumeCStr operator+(char aChar);
//******************************************
// Assigment operators
//******************************************
nsCString& operator=(const nsCString& aString);
nsCString& operator=(const nsString& aString);
nsCString& operator=(const char* aString);
nsCString& operator=(const PRUnichar* aString);
nsCString& operator=(const char aChar);
nsCString& operator=(const nsSubsumeCStr &aSubsumeString);
//******************************************
// Here are the accessor methods...
//******************************************
virtual nsresult SetLength(PRUint32 aLength) {
if(aLength>mStringValue.mLength)
SetCapacity(aLength);
Truncate(aLength);
return NS_OK;
}
virtual nsresult SetCapacity(PRUint32 aCapacity);
char* GetBuffer() const { return mStringValue.mBuffer;}
char operator[](PRUint32 aOffset) const {return CharAt(aOffset);}
operator const char*() const {return (const char*)mStringValue.mBuffer;}
operator char*() const {return mStringValue.mBuffer;}
PRUint32 Length() const {return mStringValue.mLength;}
PRUint32 Capacity() const {return mStringValue.mCapacity;}
size_t GetCharSize() const {return sizeof(mStringValue.mBuffer[0]);}
PRBool IsUnicode() const {return PR_FALSE;}
PRBool IsEmpty(void) const {return PRBool(mStringValue.mLength==0);}
char CharAt(PRUint32 anIndex) const {
return (anIndex<mStringValue.mLength) ? mStringValue.mBuffer[anIndex] : 0;
}
PRBool SetCharAt(char aChar,PRUint32 anIndex);
char First(void) const {
return CharAt(0);
}
char Last(void) const {
return CharAt(mStringValue.mLength-1);
}
virtual void SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const {
if (aResult) {
*aResult = sizeof(*this) + Capacity();
}
}
//******************************************
// Here are the Assignment methods,
// and other mutators...
//******************************************
virtual nsresult Truncate(PRUint32 anOffset=0);
/*
* This method assign from a stringimpl
* string at aString[anOffset].
*
* @update rickg 03.01.2000
* @param aString -- source String to be inserted into this
* @param aLength -- number of PRUnichars to be copied from aCopy
* @param aSrcOffset -- insertion position within this str
* @return this
*/
virtual nsresult Assign(const nsCString& aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(const nsString& aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(const char* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(const PRUnichar* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(const nsAReadableString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Assign(char aChar);
nsCString& SetString(const char* aString,PRInt32 aLength=-1) {Assign(aString, aLength); return *this;}
nsCString& SetString(const nsCString& aString,PRInt32 aLength=-1) {Assign(aString, aLength); return *this;}
//***************************************
// Here come the append methods...
//***************************************
/*
* This method appends a stringimpl starting at aString[aSrcOffset]
*
* @update rickg 03.01.2000
* @param aString -- source String to be inserted into this
* @param aLength -- number of PRUnichars to be copied from aCopy
* @param aSrcOffset -- insertion position within this str
* @return this
*/
virtual nsresult Append(const nsCString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const nsString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const char* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const PRUnichar* aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(const char aChar) ;
virtual nsresult Append(const PRUnichar aChar);
virtual nsresult Append(const nsAReadableString &aString,PRInt32 aLength=-1,PRUint32 aSrcOffset=0);
virtual nsresult Append(PRInt32 anInteger,PRInt32 aRadix=10);
virtual nsresult Append(float aFloat);
//***************************************
// Here come a few deletion methods...
//***************************************
nsresult Cut(PRUint32 anOffset,PRInt32 aCount);
nsresult Trim(const char* aTrimSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE,PRBool aIgnoreQuotes=PR_FALSE);
/**
* This method strips PRUnichars in given set from string.
* You can control whether PRUnichars are yanked from
* start and end of string as well.
*
* @update rickg 03.01.2000
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsresult CompressSet(const char* aSet, char aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE);
/**
* This method compresses whitespace within a string. Multiple ws in a row get compressed 1.
* You can also control whether whitespace is yanked from each end.
*
* @update rickg 03.01.2000
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
nsresult CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE);
//***************************************
// Here come a wad of insert methods...
//***************************************
/*
* This method inserts n PRUnichars from given string into this
* string at str[anOffset].
*
* @update rickg 03.01.2000
* @param aString -- source String to be inserted into this
* @param anOffset -- insertion position within this str
* @param aCount -- number of chars to be copied from aCopy
* @return this
*/
nsresult Insert(const nsCString& aString,PRUint32 anOffset=0,PRInt32 aCount=-1);
nsresult Insert(const char* aString,PRUint32 anOffset=0,PRInt32 aLength=-1);
nsresult Insert(char aChar,PRUint32 anOffset=0);
//*******************************************
// Here come inplace replacement methods...
//*******************************************
/**
* swaps occurence of target for another
*
* @return this
*/
nsresult ReplaceSubstring( const nsCString& aTarget,const nsCString& aNewValue);
nsresult ReplaceChar(char anOldChar, char aNewChar);
nsresult ReplaceChar(const char* aSet,char aNewPRUnichar);
nsresult ReplaceSubstring(const char* aTarget,const char* aNewValue);
//*******************************************
// Here come the stripPRUnichar methods...
//*******************************************
/**
* This method is used to remove all occurances of the
* given char(s) from this string.
*
* @update rickg 03.01.2000
* @param aChar -- PRUnichar to be stripped
* @param anOffset -- where in this string to start stripping PRUnichars
* @return *this
*/
nsresult StripChar(char aChar,PRUint32 anOffset=0);
nsresult StripChar(PRInt32 anInt,PRUint32 anOffset=0);
nsresult StripChars(const char* aSet,PRInt32 aLength=-1);
nsresult StripWhitespace();
//**************************************************
// Here are some methods that extract substrings...
//**************************************************
nsresult Left(nsCString& aCopy,PRInt32 aCount) const;
nsresult Mid(nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount) const;
nsresult Right(nsCString& aCopy,PRInt32 aCount) const;
//*******************************************
// Here come the operator+=() methods...
//*******************************************
nsCString& operator+=(const nsCString& aString);
nsCString& operator+=(const PRUnichar* aString);
nsCString& operator+=(const char* aString);
nsCString& operator+=(const PRUnichar aChar);
nsCString& operator+=(const char aChar);
nsCString& operator+=(const int anInt);
nsCString& operator+=(const nsSubsumeCStr &aSubsumeString) {
//NOT IMPLEMENTED
return *this;
}
/***********************************
Comparison methods...
***********************************/
PRBool operator==(const nsCString& aString) const {return Equals(aString);}
PRBool operator==(const PRUnichar* aString) const {return Equals(aString);}
PRBool operator==(const char* aString) const {return Equals(aString);}
PRBool operator==(char* aString) const {return Equals(aString);}
PRBool operator!=(const nsCString& aString) const {return PRBool(Compare(aString)!=0);}
PRBool operator!=(const PRUnichar* aString) const {return PRBool(Compare(aString)!=0);}
PRBool operator!=(const char* aString) const {return PRBool(Compare(aString)!=0);}
PRBool operator<(const nsCString& aString) const {return PRBool(Compare(aString)<0);}
PRBool operator<(const PRUnichar* aString) const {return PRBool(Compare(aString)<0);}
PRBool operator<(const char* aString) const {return PRBool(Compare(aString)<0);}
PRBool operator>(const nsCString& aString) const {return PRBool(Compare(aString)>0);}
PRBool operator>(const PRUnichar* aString) const {return PRBool(Compare(aString)>0);}
PRBool operator>(const char* aString) const {return PRBool(Compare(aString)>0);}
PRBool operator<=(const nsCString& aString) const {return PRBool(Compare(aString)<=0);}
PRBool operator<=(const PRUnichar* aString) const {return PRBool(Compare(aString)<=0);}
PRBool operator<=(const char* aString) const {return PRBool(Compare(aString)<=0);}
PRBool operator>=(const nsCString& aString) const {return PRBool(Compare(aString)>=0);}
PRBool operator>=(const PRUnichar* aString) const {return PRBool(Compare(aString)>=0);}
PRBool operator>=(const char* aString) const {return PRBool(Compare(aString)>=0);}
PRInt32 Compare(const nsCString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const ;
PRBool Equals(const nsCString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const;
PRBool Equals(/*FIX: const */nsIAtom* anAtom,PRBool aIgnoreCase) const;
PRBool EqualsIgnoreCase(const nsCString &aString) const {return Equals(aString,PR_TRUE);}
PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const {return Equals(aString,PR_TRUE);}
PRBool EqualsIgnoreCase(/*FIX: const */nsIAtom *aAtom) const;
/***************************************
These are string searching methods...
***************************************/
/**
* search for given string within this string
*
* @update rickg 03.01.2000
* @param aString - substr to be found
* @param aIgnoreCase tells us whether or not to do caseless compare
* @param anOffset tells us where in this string to start searching
* @param aRepCount tells us how many iterations to make starting at the given offset
* @return offset in string, or -1 (kNotFound)
*/
PRInt32 Find(const nsCString& aTarget,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 Find(const nsString& aTarget,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRUint32 anOffset=0,PRInt32 aRepCount=-1) const;
PRInt32 FindChar(char aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=0,PRInt32 aRepCount=-1) const ;
PRInt32 FindCharInSet(const nsCString& aString,PRUint32 anOffset=0) const;
PRInt32 FindCharInSet(const nsString& aString,PRUint32 anOffset=0) const;
PRInt32 FindCharInSet(const char *aString,PRUint32 anOffset=0) const;
PRInt32 RFind(const nsCString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFind(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFindChar(char aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1,PRInt32 aRepCount=-1) const;
PRInt32 RFindCharInSet(const nsCString& aString,PRInt32 anOffset=-1) const ;
PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const;
/***************************************
These convert to a different type
***************************************/
static void Recycle(nsCString* aString);
static nsCString* CreateString(void);
nsCString* ToNewString() const;
char* ToNewCString() const;
char* ToNewUTF8String() const;
PRUnichar* ToNewUnicode() const;
char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const;
//******************************************
// Utility methods
//******************************************
PRInt32 CountChar(char aChar);
//This will not work correctly for any unicode set other than ascii
void DebugDump(void) 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
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
* @return int rep of string value, and possible (out) error code
*/
PRInt32 ToInteger(PRInt32* anErrorCode,PRUint32 aRadix=kAutoDetect) const;
void ToLowerCase();
void ToLowerCase(nsCString &aString) const;
void ToUpperCase();
void ToUpperCase(nsCString &aString) const;
protected:
nsStringValueImpl<char> mStringValue;
friend class nsString;
friend class nsSubsumeCStr;
};
/*****************************************************************
Now we declare the nsSubsumeCStr class
*****************************************************************/
class NS_COM nsSubsumeCStr {
public:
nsSubsumeCStr();
nsSubsumeCStr(const nsCString& aCString);
nsSubsumeCStr(const nsSubsumeCStr& aSubsumeString);
nsSubsumeCStr(const nsStringValueImpl<char> &aLHS,const nsSubsumeCStr& aSubsumeString);
nsSubsumeCStr(const nsStringValueImpl<char> &aLHS,const nsStringValueImpl<char> &aSubsumeString);
nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1);
nsSubsumeCStr operator+(const nsSubsumeCStr &aSubsumeString);
nsSubsumeCStr operator+(const nsCString &aCString);
operator const char*() {return 0;}
nsStringValueImpl<char> mLHS;
nsStringValueImpl<char> mRHS;
};
/*****************************************************************
Now we declare the nsCAutoString class
*****************************************************************/
class NS_COM nsCAutoString : public nsCString {
public:
nsCAutoString() ;
//call this version nsAutoString derivatives...
nsCAutoString(const nsCAutoString& aString,PRInt32 aLength=-1);
//call this version for nsCString,nsCString and the autostrings
nsCAutoString(const nsCString& aString,PRInt32 aLength=-1) ;
//call this version for char*'s....
nsCAutoString(const char* aString,PRInt32 aLength=-1);
//call this version for a single char of type char...
nsCAutoString(const char aChar) ;
//call this version for PRUnichar*'s....
nsCAutoString(const PRUnichar* aString,PRInt32 aLength=-1) ;
//call this version for all other ABT versions of readable strings
nsCAutoString(const nsAReadableString &aString);
nsCAutoString(const nsCStackBuffer &aBuffer) ;
nsCAutoString(const CBufDescriptor& aBuffer) ;
nsCAutoString(const nsSubsumeCStr& aCSubsumeStringX) ;
virtual ~nsCAutoString() ;
nsCAutoString& operator=(const nsCAutoString& aCopy) ;
nsCAutoString& operator=(const nsCString& aString) ;
nsCAutoString& operator=(const char* aString) ;
nsCAutoString& operator=(const PRUnichar* aString) ;
nsCAutoString& operator=(const char aChar) ;
nsCAutoString& operator=(const nsSubsumeCStr &aSubsumeString);
protected:
char mInternalBuffer[kDefaultStringSize+1];
};
extern PRUint32 HashCode(const nsCString& aDest);
extern NS_COM int fputs(const nsCString& aString, FILE* out);
extern NS_COM void Recycle( char* aBuffer);
#endif