pjs/string/obsolete/nsString2.cpp

2121 строка
54 KiB
C++
Исходник Обычный вид История

1999-03-22 12:54:46 +03:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <ctype.h>
#include <string.h>
1999-07-17 11:26:16 +04:00
#include "nsString.h"
1999-03-22 12:54:46 +03:00
#include "nsDebug.h"
#include "nsDeque.h"
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
#ifndef RICKG_TESTBED
#include "prdtoa.h"
#include "nsISizeOfHandler.h"
1999-07-19 07:10:41 +04:00
#endif
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
static const char* kNullPointerError = "Error: unexpected null ptr";
static const char* kWhitespace="\b\t\r\n ";
1999-07-17 11:26:16 +04:00
static void Subsume(nsStr& aDest,nsStr& aSource){
if(aSource.mStr && aSource.mLength) {
if(aSource.mOwnsBuffer){
1999-07-17 11:26:16 +04:00
nsStr::Destroy(aDest,0);
aDest.mStr=aSource.mStr;
aDest.mLength=aSource.mLength;
aDest.mCharSize=aSource.mCharSize;
aDest.mCapacity=aSource.mCapacity;
aDest.mOwnsBuffer=aSource.mOwnsBuffer;
aSource.mOwnsBuffer=PR_FALSE;
aSource.mStr=0;
}
else{
nsStr::Assign(aDest,aSource,0,aSource.mLength,0);
}
}
else nsStr::Truncate(aDest,0,0);
}
1999-03-22 12:54:46 +03:00
1999-03-22 12:54:46 +03:00
/**
* 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.
*/
1999-07-17 11:26:16 +04:00
nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) {
nsStr::Initialize(*this,aCharSize);
1999-03-22 12:54:46 +03:00
}
/**
* This constructor accepts an ascii string
* @update gess 1/4/99
* @param aCString is a ptr to a 1-byte cstr
*/
1999-07-17 11:26:16 +04:00
nsString::nsString(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) {
1999-03-22 12:54:46 +03:00
nsStr::Initialize(*this,aCharSize);
Assign(aCString);
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
}
/**
* This constructor accepts an ascii string
* @update gess 1/4/99
* @param aCString is a ptr to a 1-byte cstr
*/
1999-07-17 11:26:16 +04:00
nsString::nsString(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) {
1999-03-22 12:54:46 +03:00
nsStr::Initialize(*this,aCharSize);
Assign(aString);
}
/**
* This is our copy constructor
* @update gess 1/4/99
1999-07-17 11:26:16 +04:00
* @param reference to another nsString
1999-03-22 12:54:46 +03:00
*/
1999-07-17 11:26:16 +04:00
nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) {
1999-03-22 12:54:46 +03:00
nsStr::Initialize(*this,aCharSize);
nsStr::Assign(*this,aString,0,aString.mLength,mAgent);
}
/**
* This is our copy constructor
* @update gess 1/4/99
1999-07-17 11:26:16 +04:00
* @param reference to another nsString
1999-03-22 12:54:46 +03:00
*/
1999-07-17 11:26:16 +04:00
nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) {
1999-06-03 14:49:14 +04:00
nsStr::Initialize(*this,aString.mCharSize);
1999-03-22 12:54:46 +03:00
nsStr::Assign(*this,aString,0,aString.mLength,mAgent);
}
/**
* construct off a subsumeable string
* @update gess 1/4/99
* @param reference to a subsumeString
*/
1999-07-17 11:26:16 +04:00
nsString::nsString(nsSubsumeStr& aSubsumeStr) :mAgent(0) {
Subsume(*this,aSubsumeStr);
}
1999-03-22 12:54:46 +03:00
/**
* Destructor
* Make sure we call nsStr::Destroy.
*/
1999-07-17 11:26:16 +04:00
nsString::~nsString() {
1999-03-22 12:54:46 +03:00
nsStr::Destroy(*this,mAgent);
}
1999-07-17 11:26:16 +04:00
void nsString::SizeOf(nsISizeOfHandler* aHandler) const {
#ifndef RICKG_TESTBED
1999-03-22 12:54:46 +03:00
aHandler->Add(sizeof(*this));
aHandler->Add(mCapacity << mCharSize);
1999-07-17 11:26:16 +04:00
#endif
1999-03-22 12:54:46 +03:00
}
/**
* This method truncates this string to given length.
*
* @update gess 01/04/99
* @param anIndex -- new length of string
* @return nada
*/
1999-07-17 11:26:16 +04:00
void nsString::Truncate(PRInt32 anIndex) {
1999-03-22 12:54:46 +03:00
nsStr::Truncate(*this,anIndex,mAgent);
}
/**
* Determine whether or not the characters in this
* string are in sorted order.
*
* @update gess 8/25/98
* @return TRUE if ordered.
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::IsOrdered(void) const {
1999-03-22 12:54:46 +03:00
PRBool result=PR_TRUE;
if(mLength>1) {
PRUint32 theIndex;
PRUnichar c1=0;
PRUnichar c2=GetCharAt(*this,0);
1999-03-22 12:54:46 +03:00
for(theIndex=1;theIndex<mLength;theIndex++) {
c1=c2;
c2=GetCharAt(*this,theIndex);
1999-03-22 12:54:46 +03:00
if(c1>c2) {
result=PR_FALSE;
break;
}
}
}
return result;
}
/**
* Call this method if you want to force the string to a certain capacity
* @update gess 1/4/99
* @param aLength -- contains new length for mStr
* @return
*/
1999-07-17 11:26:16 +04:00
void nsString::SetCapacity(PRUint32 aLength) {
if(aLength>mCapacity) {
1999-03-22 12:54:46 +03:00
GrowCapacity(*this,aLength,mAgent);
}
AddNullTerminator(*this);
}
/**********************************************************************
Accessor methods...
*********************************************************************/
//static char gChar=0;
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
/**
1999-03-22 12:54:46 +03:00
*
* @update gess1/4/99
* @return ptr to internal buffer (if 1-byte), otherwise NULL
1999-03-22 12:54:46 +03:00
*/
1999-07-17 11:26:16 +04:00
const char* nsString::GetBuffer(void) const {
1999-06-03 14:49:14 +04:00
const char* result=(eOneByte==mCharSize) ? mStr : 0;
return result;
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
/**
1999-07-17 11:26:16 +04:00
* This method returns the internal unicode buffer.
* Now that we've factored the string class, this should never
* be able to return a 1 byte string.
*
1999-03-22 12:54:46 +03:00
* @update gess1/4/99
1999-07-17 11:26:16 +04:00
* @return ptr to internal (2-byte) buffer;
1999-03-22 12:54:46 +03:00
*/
1999-07-17 11:26:16 +04:00
const PRUnichar* nsString::GetUnicode(void) const {
const PRUnichar* result=(eOneByte==mCharSize) ? 0 : mUStr;
1999-06-03 14:49:14 +04:00
return result;
1999-03-22 12:54:46 +03:00
}
/**
* Get nth character.
*/
1999-07-17 11:26:16 +04:00
PRUnichar nsString::operator[](PRUint32 anIndex) const {
return GetCharAt(*this,anIndex);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
PRUnichar nsString::CharAt(PRUint32 anIndex) const {
return GetCharAt(*this,anIndex);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
PRUnichar nsString::First(void) const{
return GetCharAt(*this,0);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
PRUnichar nsString::Last(void) const{
return GetCharAt(*this,mLength-1);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
1999-03-22 12:54:46 +03:00
PRBool result=PR_FALSE;
if(anIndex<mLength){
1999-06-03 14:49:14 +04:00
if(eOneByte==mCharSize)
1999-07-17 11:26:16 +04:00
mStr[anIndex]=char(aChar);
1999-03-23 10:33:10 +03:00
else mUStr[anIndex]=aChar;
// SOON! if(0==aChar) mLength=anIndex;
1999-03-22 12:54:46 +03:00
result=PR_TRUE;
}
return result;
}
/*********************************************************
append (operator+) METHODS....
*********************************************************/
/**
* Create a new string by appending given string to this
* @update gess 01/04/99
* @param aString -- 2nd string to be appended
* @return new string
*/
1999-07-17 11:26:16 +04:00
nsSubsumeStr nsString::operator+(const nsStr& aString){
nsString temp(*this); //make a temp string the same size as this...
1999-03-22 12:54:46 +03:00
nsStr::Append(temp,aString,0,aString.mLength,mAgent);
return nsSubsumeStr(temp);
1999-03-22 12:54:46 +03:00
}
/**
* Create a new string by appending given string to this
* @update gess 01/04/99
* @param aString -- 2nd string to be appended
* @return new string
*/
1999-07-17 11:26:16 +04:00
nsSubsumeStr nsString::operator+(const nsString& aString){
nsString temp(*this); //make a temp string the same size as this...
1999-03-22 12:54:46 +03:00
nsStr::Append(temp,aString,0,aString.mLength,mAgent);
return nsSubsumeStr(temp);
1999-03-22 12:54:46 +03:00
}
/**
* create a new string by adding this to the given buffer.
* @update gess 01/04/99
* @param aCString is a ptr to cstring to be added to this
* @return newly created string
*/
1999-07-17 11:26:16 +04:00
nsSubsumeStr nsString::operator+(const char* aCString) {
nsString temp(*this);
temp.Append(aCString);
return nsSubsumeStr(temp);
1999-03-22 12:54:46 +03:00
}
/**
* create a new string by adding this to the given char.
* @update gess 01/04/99
* @param aChar is a char to be added to this
* @return newly created string
*/
1999-07-17 11:26:16 +04:00
nsSubsumeStr nsString::operator+(char aChar) {
nsString temp(*this);
1999-03-22 12:54:46 +03:00
temp.Append(char(aChar));
return nsSubsumeStr(temp);
1999-03-22 12:54:46 +03:00
}
/**
* create a new string by adding this to the given buffer.
* @update gess 01/04/99
* @param aString is a ptr to unistring to be added to this
* @return newly created string
*/
1999-07-17 11:26:16 +04:00
nsSubsumeStr nsString::operator+(const PRUnichar* aString) {
nsString temp(*this);
1999-03-22 12:54:46 +03:00
temp.Append(aString);
return nsSubsumeStr(temp);
1999-03-22 12:54:46 +03:00
}
/**
* create a new string by adding this to the given char.
* @update gess 01/04/99
* @param aChar is a unichar to be added to this
* @return newly created string
*/
1999-07-17 11:26:16 +04:00
nsSubsumeStr nsString::operator+(PRUnichar aChar) {
nsString temp(*this);
1999-03-22 12:54:46 +03:00
temp.Append(char(aChar));
return nsSubsumeStr(temp);
1999-03-22 12:54:46 +03:00
}
/**********************************************************************
Lexomorphic transforms...
*********************************************************************/
/**
* Converts all chars in internal string to lower
* @update gess 01/04/99
*/
1999-07-17 11:26:16 +04:00
void nsString::ToLowerCase() {
1999-03-22 12:54:46 +03:00
nsStr::ChangeCase(*this,PR_FALSE);
}
/**
* Converts all chars in internal string to upper
* @update gess 01/04/99
*/
1999-07-17 11:26:16 +04:00
void nsString::ToUpperCase() {
1999-03-22 12:54:46 +03:00
nsStr::ChangeCase(*this,PR_TRUE);
}
/**
* Converts chars in this to lowercase, and
* stores them in aString
* @update gess 01/04/99
* @param aOut is a string to contain result
*/
1999-07-17 11:26:16 +04:00
void nsString::ToLowerCase(nsString& aString) const {
1999-03-22 12:54:46 +03:00
aString=*this;
nsStr::ChangeCase(aString,PR_FALSE);
}
/**
* Converts chars in this to lowercase, and
* stores them in a given output string
* @update gess 01/04/99
* @param aOut is a string to contain result
*/
1999-07-17 11:26:16 +04:00
void nsString::ToUpperCase(nsString& aString) const {
1999-03-22 12:54:46 +03:00
aString=*this;
nsStr::ChangeCase(aString,PR_TRUE);
}
/**
* This method is used to remove all occurances of the
* characters found in aSet from this string.
*
* @update gess 01/04/99
* @param aSet -- characters to be cut from this
* @return *this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::StripChar(PRUnichar aChar){
PRInt32 theIndex=FindChar(aChar,PR_FALSE,0);
while(kNotFound<theIndex) {
Cut(theIndex,1);
theIndex=FindChar(aChar,PR_FALSE,theIndex);
}
return *this;
}
/**
* This method is used to remove all occurances of the
* characters found in aSet from this string.
*
* @update gess 01/04/99
* @param aSet -- characters to be cut from this
* @return *this
*/
nsString& nsString::StripChars(const char* aSet){
1999-03-22 12:54:46 +03:00
if(aSet){
1999-07-17 11:26:16 +04:00
PRInt32 theIndex=FindCharInSet(aSet,0);
while(kNotFound<theIndex) {
Cut(theIndex,1);
theIndex=FindCharInSet(aSet,theIndex);
}
1999-03-22 12:54:46 +03:00
}
return *this;
}
/**
* This method strips whitespace throughout the string
*
* @update gess 01/04/99
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::StripWhitespace() {
StripChars(kWhitespace);
1999-03-22 12:54:46 +03:00
return *this;
}
/**
* This method is used to replace all occurances of the
* given source char with the given dest char
*
* @param
* @return *this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) {
1999-03-22 12:54:46 +03:00
PRUint32 theIndex=0;
1999-06-03 14:49:14 +04:00
if(eTwoByte==mCharSize){
for(theIndex=0;theIndex<mLength;theIndex++){
if(mUStr[theIndex]==aSourceChar) {
1999-03-23 10:33:10 +03:00
mUStr[theIndex]=aDestChar;
1999-06-03 14:49:14 +04:00
}//if
1999-03-22 12:54:46 +03:00
}
1999-06-03 14:49:14 +04:00
}
else{
for(theIndex=0;theIndex<mLength;theIndex++){
if(mStr[theIndex]==(char)aSourceChar) {
1999-03-23 10:33:10 +03:00
mStr[theIndex]=(char)aDestChar;
1999-06-03 14:49:14 +04:00
}//if
1999-03-22 12:54:46 +03:00
}
}
return *this;
}
1999-07-17 11:26:16 +04:00
/**
* This method is used to replace all occurances of the
* given source char with the given dest char
*
* @param
* @return *this
*/
nsString& nsString::ReplaceChar(const char* aSet, PRUnichar aNewChar){
if(aSet){
PRInt32 theIndex=FindCharInSet(aSet,0);
while(kNotFound<theIndex) {
if(eTwoByte==mCharSize)
mUStr[theIndex]=aNewChar;
else mStr[theIndex]=(char)aNewChar;
theIndex=FindCharInSet(aSet,theIndex+1);
}
}
return *this;
}
/**
* This method is used to replace all occurances of the
* given source char with the given dest char
*
* @param
* @return *this
*/
PRInt32 nsString::CountChar(PRUnichar aChar) {
PRInt32 theIndex=0;
PRInt32 theCount=0;
PRInt32 theLen=(PRInt32)mLength;
for(theIndex=0;theIndex<theLen;theIndex++){
PRUnichar theChar=GetCharAt(*this,theIndex);
if(theChar==aChar)
theCount++;
}
return theCount;
}
1999-03-22 12:54:46 +03:00
/**
* This method trims characters found in aTrimSet from
* either end of the underlying string.
*
* @update gess 3/31/98
* @param aTrimSet -- contains chars to be trimmed from
* both ends
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Trim(const char* aTrimSet, PRBool aEliminateLeading,PRBool aEliminateTrailing){
1999-03-22 12:54:46 +03:00
if(aTrimSet){
nsStr::Trim(*this,aTrimSet,aEliminateLeading,aEliminateTrailing);
}
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
return *this;
}
/**
* This method strips whitespace from string.
* You can control whether whitespace is yanked from
* start and end of string as well.
*
* @update gess 3/31/98
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){
1999-03-22 12:54:46 +03:00
if(aSet){
1999-07-17 11:26:16 +04:00
ReplaceChar(aSet,aChar);
nsStr::CompressSet(*this,aSet,aEliminateLeading,aEliminateTrailing);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
return *this;
}
/**
* This method strips whitespace from string.
* You can control whether whitespace is yanked from
* start and end of string as well.
*
* @update gess 3/31/98
* @param aEliminateLeading controls stripping of leading ws
* @param aEliminateTrailing controls stripping of trailing ws
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::CompressWhitespace( PRBool aEliminateLeading,PRBool aEliminateTrailing){
CompressSet(kWhitespace,' ',aEliminateLeading,aEliminateTrailing);
1999-03-22 12:54:46 +03:00
return *this;
}
/**********************************************************************
string conversion methods...
*********************************************************************/
/**
* Creates a duplicate clone (ptr) of this string.
* @update gess 01/04/99
* @return ptr to clone of this string
*/
1999-07-17 11:26:16 +04:00
nsString* nsString::ToNewString() const {
return new nsString(*this);
1999-03-22 12:54:46 +03:00
}
/**
* Creates an ascii clone of this string
* @update gess 01/04/99
* @return ptr to new ascii string
*/
1999-07-17 11:26:16 +04:00
char* nsString::ToNewCString() const {
nsCString temp(*this);
temp.SetCapacity(8); //ensure that we get an allocated buffer instead of the common empty one.
1999-07-17 11:26:16 +04:00
char* result=temp.mStr;
temp.mStr=0;
temp.mOwnsBuffer=PR_FALSE;
#ifdef RICKG_DEBUG
// fstream& theStream=GetLogStream();
// theStream << "tonewcString() " << result << endl;
#endif
1999-03-22 12:54:46 +03:00
return result;
}
/**
* Creates an ascii clone of this string
* @update gess 01/04/99
* @return ptr to new ascii string
*/
1999-07-17 11:26:16 +04:00
PRUnichar* nsString::ToNewUnicode() const {
PRUnichar* result=0;
if(eOneByte==mCharSize) {
nsString temp(mStr);
temp.SetCapacity(8);
result=temp.mUStr;
temp.mStr=0;
temp.mOwnsBuffer=PR_FALSE;
}
else{
nsString temp(mUStr);
temp.SetCapacity(8);
result=temp.mUStr;
temp.mStr=0;
temp.mOwnsBuffer=PR_FALSE;
}
1999-03-22 12:54:46 +03:00
return result;
}
/**
* Copies contents of this string into he given buffer
* @update gess 01/04/99
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
char* nsString::ToCString(char* aBuf, PRUint32 aBufLength,PRUint32 anOffset) const{
if(aBuf) {
CBufDescriptor theSB(aBuf,PR_TRUE,aBufLength,0);
1999-07-17 11:26:16 +04:00
nsCAutoString temp(theSB);
temp.Assign(*this);
temp.mStr=0;
1999-03-22 12:54:46 +03:00
}
return aBuf;
}
/**
* Perform string to float conversion.
* @update gess 01/04/99
* @param aErrorCode will contain error if one occurs
* @return float rep of string value
*/
1999-07-17 11:26:16 +04:00
float nsString::ToFloat(PRInt32* aErrorCode) const {
1999-03-22 12:54:46 +03:00
char buf[100];
if (mLength > PRInt32(sizeof(buf)-1)) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
return 0.0f;
}
char* cp = ToCString(buf, sizeof(buf));
float f = (float) PR_strtod(cp, &cp);
if (*cp != 0) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
*aErrorCode = (PRInt32) NS_OK;
return f;
}
/**
* Perform numeric string to int conversion with given radix.
* NOTE: 1. This method mandates that the string is well formed.
* 2. This method will return an error if the string you give
contains chars outside the range for the specified radix.
1999-03-22 12:54:46 +03:00
* @update gess 10/01/98
* @param aErrorCode will contain error if one occurs
* @param aRadix tells us what base to expect the string in.
* @return int rep of string value
*/
1999-07-17 11:26:16 +04:00
PRInt32 _ToInteger(nsString& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
1999-03-22 12:54:46 +03:00
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
PRInt32 result=0;
char* cp = aString.mStr + aString.mLength;
PRInt32 theMult=1;
1999-03-22 12:54:46 +03:00
*anErrorCode = NS_OK;
1999-03-22 12:54:46 +03:00
//now iterate the numeric chars and build our result
char theDigit=0;
while(--cp>=aString.mStr){
char theChar=*cp;
1999-03-22 12:54:46 +03:00
if((theChar>='0') && (theChar<='9')){
theDigit=theChar-'0';
1999-03-22 12:54:46 +03:00
}
else if((theChar>='A') && (theChar<='F')) {
if(10==aRadix){
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
result=0;
break;
}
theDigit=(theChar-'A')+10;
1999-03-22 12:54:46 +03:00
}
else if('-'==theChar) {
result=-result;
break;
}
else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this...
1999-03-22 12:54:46 +03:00
break;
}
else {
//we've encountered a char that's not a legal number or sign
1999-03-22 12:54:46 +03:00
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
result=0;
break;
}
result+=theDigit*theMult;
1999-03-22 12:54:46 +03:00
theMult*=aRadix;
}
return result;
}
/**
* Call this method to extract the rightmost numeric value from the given
* 1-byte input string, and simultaneously determine the radix.
* NOTE: This method mandates that the string is well formed.
* Leading and trailing gunk should be removed, and the case upper.
* @update gess 10/01/98
* @param anInputString contains orig string
* @param anOutString contains numeric portion copy of input string
* @param aRadix (an out parm) tells the caller what base we think the string is in.
* @return non-zero error code if this string is non-numeric
*/
1999-07-17 11:26:16 +04:00
PRInt32 GetNumericSubstring(nsString& aString,PRUint32& aRadix) {
aString.ToUpperCase();
1999-07-17 11:26:16 +04:00
PRInt32 decPt=nsStr::FindChar(aString,'.',PR_TRUE,0);
char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1;
1999-06-17 11:24:13 +04:00
aRadix=kRadixUnknown; //assume for starters...
// Skip trailing non-numeric...
while (cp >= aString.mStr) {
if((*cp>='0') && (*cp<='9')){
1999-06-17 11:24:13 +04:00
if(kRadixUnknown==aRadix)
aRadix=kRadix10;
break;
}
else if((*cp>='A') && (*cp<='F')) {
aRadix=16;
break;
}
cp--;
}
aString.Truncate(cp-aString.mStr+1);
//ok, now scan through chars until you find the start of this number...
//we delimit the number by the presence of: +,-,#,X
// Skip trailing non-numeric...
cp++;
while (--cp >= aString.mStr) {
if((*cp>='0') && (*cp<='9')){
continue;
}
else if((*cp>='A') && (*cp<='F')) {
continue;
}
1999-06-15 06:03:33 +04:00
else if((*cp=='-') || (*cp=='+')){
break;
}
else {
if(('#'==(*cp)) || ('X'==(*cp)))
1999-06-17 11:24:13 +04:00
aRadix=kRadix16;
cp++; //move back by one
break;
}
}
if(cp>aString.mStr)
aString.Cut(0,cp-aString.mStr);
PRInt32 result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
return result;
}
1999-06-17 11:24:13 +04:00
/**
1999-06-17 11:24:13 +04:00
* This method tries to autodetect that radix given a string
* @update gess 10/01/98
1999-06-17 11:24:13 +04:00
* @return 10,16,or 0 (meaning I don't know)
*/
1999-07-17 11:26:16 +04:00
PRUint32 nsString::DetermineRadix(void) {
1999-06-17 11:24:13 +04:00
PRUint32 result=kRadixUnknown;
1999-06-15 06:03:33 +04:00
if(0<mLength) {
1999-07-17 11:26:16 +04:00
nsAutoString theString(*this,eOneByte);
1999-06-17 11:24:13 +04:00
if(NS_OK!=GetNumericSubstring(theString,result))
result=kRadixUnknown;
}
return result;
}
1999-06-17 11:24:13 +04:00
/**
* Perform decimal numeric string to int conversion.
* NOTE: In this version, we use the radix you give, even if it's wrong.
* @update gess 10/01/98
* @param aErrorCode will contain error if one occurs
* @param aRadix tells us what base to expect the given string in.
* @return int rep of string value
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
1999-07-17 11:26:16 +04:00
nsAutoString theString(*this,eOneByte);
1999-06-17 11:24:13 +04:00
PRUint32 theRadix=aRadix;
PRInt32 result=0;
*anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
if(NS_OK==*anErrorCode){
1999-06-17 11:24:13 +04:00
if(kAutoDetect==aRadix)
aRadix=theRadix;
if((kRadix10==aRadix) || (kRadix16==aRadix))
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
}
return result;
}
1999-03-22 12:54:46 +03:00
/**********************************************************************
String manipulation methods...
*********************************************************************/
/**
* assign given string to this one
* @update gess 01/04/99
* @param aString: string to be added to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Assign(const nsStr& aString,PRInt32 aCount) {
1999-06-03 14:49:14 +04:00
if(this!=&aString){
1999-07-17 11:26:16 +04:00
nsStr::Truncate(*this,0,0);
1999-07-19 01:26:03 +04:00
if(aCount<0)
aCount=aString.mLength;
else aCount=MinInt(aCount,aString.mLength);
1999-06-03 14:49:14 +04:00
nsStr::Assign(*this,aString,0,aCount,mAgent);
}
1999-03-22 12:54:46 +03:00
return *this;
}
/**
* assign given char* to this string
* @update gess 01/04/99
* @param aCString: buffer to be assigned to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Assign(const char* aCString,PRInt32 aCount) {
nsStr::Truncate(*this,0,0);
1999-03-22 12:54:46 +03:00
if(aCString){
Append(aCString,aCount);
}
return *this;
}
/**
* assign given unichar* to this string
* @update gess 01/04/99
* @param aString: buffer to be assigned to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Assign(const PRUnichar* aString,PRInt32 aCount) {
nsStr::Truncate(*this,0,0);
1999-03-22 12:54:46 +03:00
if(aString){
Append(aString,aCount);
}
return *this;
}
/**
* assign given char to this string
* @update gess 01/04/99
* @param aChar: char to be assignd to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Assign(char aChar) {
1999-03-22 12:54:46 +03:00
nsStr::Truncate(*this,0,0);
return Append(aChar);
}
/**
* assign given char to this string
* @update gess 01/04/99
* @param aChar: char to be assignd to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Assign(PRUnichar aChar) {
1999-03-22 12:54:46 +03:00
nsStr::Truncate(*this,0,0);
return Append(aChar);
}
/**
* WARNING! THIS IS A VERY SPECIAL METHOD.
* This method "steals" the contents of aSource and hands it to aDest.
* Ordinarily a copy is made, but not in this version.
* @update gess10/30/98
* @param
* @return
1999-03-22 12:54:46 +03:00
*/
#ifdef AIX
1999-07-17 11:26:16 +04:00
nsString& nsString::operator=(const nsSubsumeStr& aSubsumeString) {
nsSubsumeStr temp(aSubsumeString); // a temp is needed for the AIX compiler
Subsume(*this,temp);
#else
1999-07-17 11:26:16 +04:00
nsString& nsString::operator=(nsSubsumeStr& aSubsumeString) {
Subsume(*this,aSubsumeString);
#endif // AIX
return *this;
1999-03-22 12:54:46 +03:00
}
1999-03-22 12:54:46 +03:00
/**
* append given string to this string
* @update gess 01/04/99
* @param aString : string to be appended to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(const nsStr& aString,PRInt32 aCount) {
1999-07-19 01:26:03 +04:00
if(aCount<0)
aCount=aString.mLength;
else aCount=MinInt(aCount,aString.mLength);
1999-07-17 11:26:16 +04:00
if(0<aCount)
nsStr::Append(*this,aString,0,aCount,mAgent);
1999-03-22 12:54:46 +03:00
return *this;
}
/**
* append given string to this string
* @update gess 01/04/99
* @param aString : string to be appended to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(const nsString& aString,PRInt32 aCount) {
1999-07-19 01:26:03 +04:00
if(aCount<0)
aCount=aString.mLength;
else aCount=MinInt(aCount,aString.mLength);
1999-07-17 11:26:16 +04:00
if(0<aCount)
nsStr::Append(*this,aString,0,aCount,mAgent);
1999-03-22 12:54:46 +03:00
return *this;
}
/**
* append given string to this string
* @update gess 01/04/99
* @param aString : string to be appended to this
* @param aCount -- number of chars to copy; -1 tells us to compute the strlen for you
1999-03-22 12:54:46 +03:00
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(const char* aCString,PRInt32 aCount) {
if(aCString && aCount){ //if astring is null or count==0 there's nothing to do
nsStr temp;
Initialize(temp,eOneByte);
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
1999-07-17 11:26:16 +04:00
if(0<aCount)
nsStr::Append(*this,temp,0,aCount,mAgent);
1999-03-22 12:54:46 +03:00
}
return *this;
}
/**
* append given string to this string
1999-03-22 12:54:46 +03:00
* @update gess 01/04/99
* @param aString : string to be appended to this
* @param aCount -- number of chars to copy; -1 tells us to compute the strlen for you
1999-03-22 12:54:46 +03:00
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(const PRUnichar* aString,PRInt32 aCount) {
if(aString && aCount){ //if astring is null or count==0 there's nothing to do
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
}
else aCount=temp.mLength=nsCRT::strlen(aString);
1999-07-17 11:26:16 +04:00
if(0<aCount)
nsStr::Append(*this,temp,0,aCount,mAgent);
1999-03-22 12:54:46 +03:00
}
return *this;
}
/**
* append given string to this string
* @update gess 01/04/99
* @param aString : string to be appended to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(char aChar) {
1999-03-22 12:54:46 +03:00
char buf[2]={0,0};
buf[0]=aChar;
nsStr temp;
Initialize(temp,eOneByte);
temp.mStr=buf;
temp.mLength=1;
nsStr::Append(*this,temp,0,1,mAgent);
1999-03-22 12:54:46 +03:00
return *this;
}
/**
* append given string to this string
* @update gess 01/04/99
* @param aString : string to be appended to this
* @return this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(PRUnichar aChar) {
1999-03-22 12:54:46 +03:00
PRUnichar buf[2]={0,0};
buf[0]=aChar;
nsStr temp;
Initialize(temp,eTwoByte);
temp.mUStr=buf;
temp.mLength=1;
nsStr::Append(*this,temp,0,1,mAgent);
1999-03-22 12:54:46 +03:00
return *this;
}
/**
*
* @update gess 01/04/99
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) {
1999-03-22 12:54:46 +03:00
char* fmt = "%d";
if (8 == aRadix) {
fmt = "%o";
} else if (16 == aRadix) {
fmt = "%x";
}
char buf[40];
// *** XX UNCOMMENT THIS LINE
//PR_snprintf(buf, sizeof(buf), fmt, aInteger);
sprintf(buf,fmt,aInteger);
return Append(buf);
}
/**
*
* @update gess 01/04/99
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Append(float aFloat){
1999-03-22 12:54:46 +03:00
char buf[40];
// *** XX UNCOMMENT THIS LINE
//PR_snprintf(buf, sizeof(buf), "%g", aFloat);
sprintf(buf,"%g",aFloat);
1999-07-17 11:26:16 +04:00
Append(buf);
return *this;
1999-03-22 12:54:46 +03:00
}
/*
* Copies n characters from this string to given string,
* starting at the leftmost offset.
*
*
* @update gess 4/1/98
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
1999-07-17 11:26:16 +04:00
PRUint32 nsString::Left(nsString& aDest,PRInt32 aCount) const{
1999-07-19 01:26:03 +04:00
if(aCount<0)
aCount=mLength;
else aCount=MinInt(aCount,mLength);
1999-03-22 12:54:46 +03:00
nsStr::Assign(aDest,*this,0,aCount,mAgent);
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
return aDest.mLength;
}
/*
* Copies n characters from this string to given string,
* starting at the given offset.
*
*
* @update gess 4/1/98
* @param aDest -- Receiving string
* @param aCount -- number of chars to copy
* @param anOffset -- position where copying begins
* @return number of chars copied
*/
1999-07-17 11:26:16 +04:00
PRUint32 nsString::Mid(nsString& aDest,PRUint32 anOffset,PRInt32 aCount) const{
1999-07-19 01:26:03 +04:00
if(aCount<0)
aCount=mLength;
else aCount=MinInt(aCount,mLength);
1999-03-22 12:54:46 +03:00
nsStr::Assign(aDest,*this,anOffset,aCount,mAgent);
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
return aDest.mLength;
}
/*
* Copies n characters from this string to given string,
* starting at rightmost char.
*
*
* @update gess 4/1/98
* @param aCopy -- Receiving string
* @param aCount -- number of chars to copy
* @return number of chars copied
*/
1999-07-17 11:26:16 +04:00
PRUint32 nsString::Right(nsString& aCopy,PRInt32 aCount) const{
1999-03-22 12:54:46 +03:00
PRInt32 offset=MaxInt(mLength-aCount,0);
return Mid(aCopy,offset,aCount);
}
/*
* This method inserts n chars from given string into this
* string at str[anOffset].
*
* @update gess 4/1/98
* @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
1999-03-22 12:54:46 +03:00
* @return number of chars inserted into this.
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) {
1999-03-22 12:54:46 +03:00
nsStr::Insert(*this,anOffset,aCopy,0,aCount,mAgent);
return *this;
}
/**
* Insert a single unicode char into this string at
* a specified offset.
*
* @update gess4/22/98
* @param aChar char to be inserted into this string
* @param anOffset is insert pos in str
* @param aCounttells us how many chars to insert
1999-03-22 12:54:46 +03:00
* @return the number of chars inserted into this string
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){
if(aCString && aCount){
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mStr=(char*)aCString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
}
else aCount=temp.mLength=nsCRT::strlen(aCString);
if(0<aCount){
nsStr::Insert(*this,anOffset,temp,0,aCount,0);
1999-03-22 12:54:46 +03:00
}
}
return *this;
}
/**
* Insert a unicode* into this string at
* a specified offset.
*
* @update gess4/22/98
* @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
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Insert(const PRUnichar* aString,PRUint32 anOffset,PRInt32 aCount){
if(aString && aCount){
1999-07-17 11:26:16 +04:00
nsStr temp;
nsStr::Initialize(temp,eTwoByte);
temp.mUStr=(PRUnichar*)aString;
if(0<aCount) {
//this has to be done to make sure someone doesn't tell us
//aCount=n but offer a string whose len<aCount
temp.mLength=aCount;
PRInt32 pos=nsStr::FindChar(temp,0,PR_FALSE,0);
if((0<=pos) && (pos<aCount)) {
aCount=temp.mLength=pos;
}
}
else aCount=temp.mLength=nsCRT::strlen(aString);
if(0<aCount){
1999-07-17 11:26:16 +04:00
nsStr::Insert(*this,anOffset,temp,0,aCount,0);
1999-03-22 12:54:46 +03:00
}
}
return *this;
}
/**
* Insert a single uni-char into this string at
* a specified offset.
*
* @update gess4/22/98
* @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
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Insert(PRUnichar aChar,PRUint32 anOffset){
1999-03-22 12:54:46 +03:00
PRUnichar theBuffer[2]={0,0};
theBuffer[0]=aChar;
nsStr temp;
nsStr::Initialize(temp,eTwoByte);
temp.mUStr=theBuffer;
temp.mLength=1;
nsStr::Insert(*this,anOffset,temp,0,1,0);
1999-03-22 12:54:46 +03:00
return *this;
}
/*
* This method is used to cut characters in this string
* starting at anOffset, continuing for aCount chars.
*
* @update gess 01/04/99
* @param anOffset -- start pos for cut operation
* @param aCount -- number of chars to be cut
* @return *this
*/
1999-07-17 11:26:16 +04:00
nsString& nsString::Cut(PRUint32 anOffset, PRInt32 aCount) {
1999-07-19 01:26:03 +04:00
if(0<aCount) {
nsStr::Delete(*this,anOffset,aCount,mAgent);
}
1999-03-22 12:54:46 +03:00
return *this;
}
/**********************************************************************
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)
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::BinarySearch(PRUnichar aChar) const{
1999-03-22 12:54:46 +03:00
PRInt32 low=0;
PRInt32 high=mLength-1;
while (low <= high) {
int middle = (low + high) >> 1;
PRUnichar theChar=GetCharAt(*this,middle);
1999-03-22 12:54:46 +03:00
if (theChar==aChar)
return middle;
if (theChar>aChar)
high = middle - 1;
else
low = middle + 1;
}
return kNotFound;
}
/**
* Search for given buffer within this string
*
* @update gess 3/25/98
* @param aCStringBuf - charstr to be found
* @return offset in string, or -1 (kNotFound)
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset) const{
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=aCString,kNullPointerError);
PRInt32 result=kNotFound;
if(aCString) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aCString);
temp.mStr=(char*)aCString;
1999-07-17 11:26:16 +04:00
result=nsStr::FindSubstr(*this,temp,aIgnoreCase,anOffset);
1999-03-22 12:54:46 +03:00
}
return result;
}
/**
1999-03-22 12:54:46 +03:00
* Search for given buffer within this string
*
* @update gess 3/25/98
* @param aCStringBuf - charstr to be found
* @return offset in string, or -1 (kNotFound)
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Find(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 anOffset) const{
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=aString,kNullPointerError);
PRInt32 result=kNotFound;
if(aString) {
nsStr temp;
nsStr::Initialize(temp,eTwoByte);
temp.mLength=nsCRT::strlen(aString);
temp.mUStr=(PRUnichar*)aString;
1999-07-17 11:26:16 +04:00
result=nsStr::FindSubstr(*this,temp,aIgnoreCase,anOffset);
1999-03-22 12:54:46 +03:00
}
return result;
}
/**
* Search for given buffer within this string
*
* @update gess 3/25/98
1999-07-17 11:26:16 +04:00
* @param nsString -- buffer to be found
1999-03-22 12:54:46 +03:00
* @return offset in string, or -1 (kNotFound)
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Find(const nsStr& aString,PRBool aIgnoreCase,PRInt32 anOffset) const{
PRInt32 result=nsStr::FindSubstr(*this,aString,aIgnoreCase,anOffset);
1999-03-22 12:54:46 +03:00
return result;
}
/**
* Search for given buffer within this string
*
* @update gess 3/25/98
1999-07-17 11:26:16 +04:00
* @param nsString -- buffer to be found
* @return offset in string, or -1 (kNotFound)
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Find(const nsString& aString,PRBool aIgnoreCase,PRInt32 anOffset) const{
PRInt32 result=nsStr::FindSubstr(*this,aString,aIgnoreCase,anOffset);
return result;
}
1999-03-22 12:54:46 +03:00
/**
* Search for a given char, starting at given offset
*
* @update gess 3/25/98
* @param
* @return offset of found char, or -1 (kNotFound)
*/
1999-07-19 07:10:41 +04:00
PRInt32 nsString::Find(PRUnichar aChar,PRInt32 anOffset,PRBool aIgnoreCase) const{
1999-07-17 11:26:16 +04:00
PRInt32 result=nsStr::FindChar(*this,aChar,aIgnoreCase,anOffset);
return result;
}
/**
* Search for a given char, starting at given offset
*
* @update gess 3/25/98
* @param
* @return offset of found char, or -1 (kNotFound)
*/
PRInt32 nsString::FindChar(PRUnichar aChar,PRBool aIgnoreCase,PRInt32 anOffset) const{
PRInt32 result=nsStr::FindChar(*this,aChar,aIgnoreCase,anOffset);
1999-03-22 12:54:46 +03:00
return result;
}
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::FindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=aCStringSet,kNullPointerError);
PRInt32 result=kNotFound;
if(aCStringSet) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aCStringSet);
temp.mStr=(char*)aCStringSet;
result=nsStr::FindCharInSet(*this,temp,PR_FALSE,anOffset);
1999-03-22 12:54:46 +03:00
}
return result;
}
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::FindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{
NS_ASSERTION(0!=aStringSet,kNullPointerError);
1999-03-22 12:54:46 +03:00
PRInt32 result=kNotFound;
1999-07-17 11:26:16 +04:00
if(aStringSet) {
nsStr temp;
1999-07-17 11:26:16 +04:00
nsStr::Initialize(temp,eTwoByte);
temp.mLength=nsCRT::strlen(aStringSet);
temp.mStr=(char*)aStringSet;
result=nsStr::FindCharInSet(*this,temp,PR_FALSE,anOffset);
1999-03-22 12:54:46 +03:00
}
return result;
}
/**
*
*
* @update gess 3/25/98
* @param
1999-07-17 11:26:16 +04:00
* @return
1999-03-22 12:54:46 +03:00
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::FindCharInSet(const nsStr& aSet,PRInt32 anOffset) const{
PRInt32 result=nsStr::FindCharInSet(*this,aSet,PR_FALSE,anOffset);
1999-03-22 12:54:46 +03:00
return result;
}
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::RFind(const nsStr& aString,PRBool aIgnoreCase,PRInt32 anOffset) const{
PRInt32 result=nsStr::RFindSubstr(*this,aString,aIgnoreCase,anOffset);
1999-03-22 12:54:46 +03:00
return result;
}
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase,PRInt32 anOffset) const{
PRInt32 result=nsStr::RFindSubstr(*this,aString,aIgnoreCase,anOffset);
return result;
}
1999-03-22 12:54:46 +03:00
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset) const{
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=aString,kNullPointerError);
PRInt32 result=kNotFound;
if(aString) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aString);
temp.mStr=(char*)aString;
1999-07-17 11:26:16 +04:00
result=nsStr::RFindSubstr(*this,temp,aIgnoreCase,anOffset);
1999-03-22 12:54:46 +03:00
}
return result;
}
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
/**
* Search for a given char, starting at given offset
*
* @update gess 3/25/98
* @param
* @return offset of found char, or -1 (kNotFound)
*/
1999-07-19 07:10:41 +04:00
PRInt32 nsString::RFind(PRUnichar aChar,PRInt32 anOffset,PRBool aIgnoreCase) const{
1999-03-22 12:54:46 +03:00
PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset);
return result;
}
1999-07-19 07:10:41 +04:00
1999-07-17 11:26:16 +04:00
/**
* Search for a given char, starting at given offset
*
* @update gess 3/25/98
* @param
* @return offset of found char, or -1 (kNotFound)
*/
PRInt32 nsString::RFindChar(PRUnichar aChar,PRBool aIgnoreCase,PRInt32 anOffset) const{
PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset);
return result;
}
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
PRInt32 nsString::RFindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{
NS_ASSERTION(0!=aCStringSet,kNullPointerError);
PRInt32 result=kNotFound;
if(aCStringSet) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aCStringSet);
temp.mStr=(char*)aCStringSet;
result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset);
}
return result;
}
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
PRInt32 nsString::RFindCharInSet(const nsStr& aSet,PRInt32 anOffset) const{
PRInt32 result=nsStr::RFindCharInSet(*this,aSet,PR_FALSE,anOffset);
return result;
}
/**
*
*
* @update gess 3/25/98
* @param
* @return
*/
PRInt32 nsString::RFindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{
NS_ASSERTION(0!=aStringSet,kNullPointerError);
PRInt32 result=kNotFound;
if(aStringSet) {
nsStr temp;
nsStr::Initialize(temp,eTwoByte);
temp.mLength=nsCRT::strlen(aStringSet);
temp.mUStr=(PRUnichar*)aStringSet;
result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset);
}
return result;
}
1999-03-22 12:54:46 +03:00
/**************************************************************
COMPARISON METHODS...
**************************************************************/
/**
* Compares given cstring to this string.
* @update gess 01/04/99
* @param aCString pts to a cstring
* @param aIgnoreCase tells us how to treat case
1999-07-17 11:26:16 +04:00
* @param aCount tells us how many chars to test; -1 implies full length
* @return -1,0,1
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Compare(const nsString& aString,PRBool aIgnoreCase,PRInt32 aCount) const {
PRInt32 result=nsStr::Compare(*this,aString,aCount,aIgnoreCase);
return result;
}
1999-03-22 12:54:46 +03:00
/**
* Compares given cstring to this string.
* @update gess 01/04/99
* @param aCString pts to a cstring
* @param aIgnoreCase tells us how to treat case
1999-07-17 11:26:16 +04:00
* @param aCount tells us how many chars to test; -1 implies full length
1999-03-22 12:54:46 +03:00
* @return -1,0,1
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount) const {
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=aCString,kNullPointerError);
if(aCString) {
nsStr temp;
nsStr::Initialize(temp,eOneByte);
temp.mLength=nsCRT::strlen(aCString);
temp.mStr=(char*)aCString;
1999-07-17 11:26:16 +04:00
return nsStr::Compare(*this,temp,aCount,aIgnoreCase);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
1999-03-22 12:54:46 +03:00
return 0;
}
/**
* Compares given unistring to this string.
* @update gess 01/04/99
* @param aString pts to a uni-string
* @param aIgnoreCase tells us how to treat case
1999-07-17 11:26:16 +04:00
* @param aCount tells us how many chars to test; -1 implies full length
1999-03-22 12:54:46 +03:00
* @return -1,0,1
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Compare(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=aString,kNullPointerError);
if(aString) {
nsStr temp;
nsStr::Initialize(temp,eTwoByte);
temp.mLength=nsCRT::strlen(aString);
temp.mUStr=(PRUnichar*)aString;
1999-07-17 11:26:16 +04:00
return nsStr::Compare(*this,temp,aCount,aIgnoreCase);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
return 0;
1999-03-22 12:54:46 +03:00
}
/**
1999-07-17 11:26:16 +04:00
* Compare given nsStr with this cstring.
1999-03-22 12:54:46 +03:00
*
1999-07-17 11:26:16 +04:00
* @param aString is an nsStr instance to be compared
* @param aIgnoreCase tells us how to treat case
* @param aCount tells us how many chars to test; -1 implies full length
* @return -1,0,1
1999-03-22 12:54:46 +03:00
*/
1999-07-17 11:26:16 +04:00
PRInt32 nsString::Compare(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const {
return nsStr::Compare(*this,aString,aCount,aIgnoreCase);
1999-03-22 12:54:46 +03:00
}
1999-07-17 11:26:16 +04:00
PRBool nsString::operator==(const nsString& S) const {return Equals(S);}
PRBool nsString::operator==(const nsStr& S) const {return Equals(S);}
PRBool nsString::operator==(const char* s) const {return Equals(s);}
PRBool nsString::operator==(const PRUnichar* s) const {return Equals(s);}
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
PRBool nsString::operator!=(const nsString& S) const {return PRBool(Compare(S)!=0);}
PRBool nsString::operator!=(const nsStr& S) const {return PRBool(Compare(S)!=0);}
PRBool nsString::operator!=(const char* s) const {return PRBool(Compare(s)!=0);}
PRBool nsString::operator!=(const PRUnichar* s) const {return PRBool(Compare(s)!=0);}
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
PRBool nsString::operator<(const nsString& S) const {return PRBool(Compare(S)<0);}
PRBool nsString::operator<(const nsStr& S) const {return PRBool(Compare(S)<0);}
PRBool nsString::operator<(const char* s) const {return PRBool(Compare(s)<0);}
PRBool nsString::operator<(const PRUnichar* s) const {return PRBool(Compare(s)<0);}
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
PRBool nsString::operator>(const nsString& S) const {return PRBool(Compare(S)>0);}
PRBool nsString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);}
PRBool nsString::operator>(const char* s) const {return PRBool(Compare(s)>0);}
PRBool nsString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);}
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
PRBool nsString::operator<=(const nsString& S) const {return PRBool(Compare(S)<=0);}
PRBool nsString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);}
PRBool nsString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);}
PRBool nsString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);}
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
PRBool nsString::operator>=(const nsString& S) const {return PRBool(Compare(S)>=0);}
PRBool nsString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);}
PRBool nsString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);}
PRBool nsString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);}
1999-03-22 12:54:46 +03:00
1999-07-17 11:26:16 +04:00
PRBool nsString::EqualsIgnoreCase(const nsString& aString) const {
1999-03-22 12:54:46 +03:00
return Equals(aString,PR_TRUE);
}
1999-07-17 11:26:16 +04:00
PRBool nsString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const {
return Equals(aString,PR_TRUE,aLength);
1999-03-22 12:54:46 +03:00
}
PRBool nsString::EqualsIgnoreCase(nsIAtom *aAtom) const {
1999-03-22 12:54:46 +03:00
return Equals(aAtom,PR_TRUE);
}
1999-07-17 11:26:16 +04:00
PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const {
1999-03-22 12:54:46 +03:00
return Equals(s1,s2,PR_TRUE);
}
/**
* Compare this to given string; note that we compare full strings here.
*
* @update gess 01/04/99
1999-07-17 11:26:16 +04:00
* @param aString is the other nsString to be compared to
* @param aCount tells us how many chars to test; -1 implies full length
* @return TRUE if equal
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::Equals(const nsString& aString,PRBool aIgnoreCase,PRInt32 aCount) const {
PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase);
PRBool result=PRBool(0==theAnswer);
return result;
}
1999-03-22 12:54:46 +03:00
/**
* Compare this to given string; note that we compare full strings here.
*
* @update gess 01/04/99
1999-07-17 11:26:16 +04:00
* @param aString is the other nsString to be compared to
* @param aCount tells us how many chars to test; -1 implies full length
1999-03-22 12:54:46 +03:00
* @return TRUE if equal
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const {
PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase);
PRBool result=PRBool(0==theAnswer);
return result;
1999-03-22 12:54:46 +03:00
}
/**
1999-07-17 11:26:16 +04:00
* Compare this to given c-string; note that we compare full strings here.
*
* @param aString is the CString to be compared
* @param aIgnorecase tells us whether to be case sensitive
* @param aCount tells us how many chars to test; -1 implies full length
1999-03-22 12:54:46 +03:00
* @return TRUE if equal
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::Equals(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount);
PRBool result=PRBool(0==theAnswer);
1999-03-22 12:54:46 +03:00
return result;
}
/**
1999-07-17 11:26:16 +04:00
* Compare this to given unicode string; note that we compare full strings here.
*
* @param aString is the U-String to be compared
* @param aIgnorecase tells us whether to be case sensitive
* @param aCount tells us how many chars to test; -1 implies full length
1999-03-22 12:54:46 +03:00
* @return TRUE if equal
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount);
PRBool result=PRBool(0==theAnswer);
1999-03-22 12:54:46 +03:00
return result;
}
/**
1999-07-17 11:26:16 +04:00
* Compare this to given atom; note that we compare full strings here.
1999-03-22 12:54:46 +03:00
* 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.
*
* @update gess 01/04/99
* @param aString -- unistring to compare to this
* @param aLength -- length of given string.
* @return TRUE if equal
*/
PRBool nsString::Equals(nsIAtom* aAtom,PRBool aIgnoreCase) const{
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=aAtom,kNullPointerError);
PRBool result=PR_FALSE;
if(aAtom){
1999-07-17 11:26:16 +04:00
PRInt32 cmp=0;
PRUnichar* unicode;
if (aAtom->GetUnicode(&unicode) != NS_OK || unicode == nsnull)
return PR_FALSE;
1999-07-17 11:26:16 +04:00
if (aIgnoreCase)
cmp=nsCRT::strcasecmp(mUStr,unicode);
1999-07-17 11:26:16 +04:00
else
cmp=nsCRT::strcmp(mUStr,unicode);
1999-03-22 12:54:46 +03:00
result=PRBool(0==cmp);
}
1999-07-17 11:26:16 +04:00
return result;
1999-03-22 12:54:46 +03:00
}
/**
* Compare given strings
* @update gess 7/27/98
1999-07-17 11:26:16 +04:00
* @param s1 -- first unichar string to be compared
* @param s2 -- second unichar string to be compared
1999-03-22 12:54:46 +03:00
* @return TRUE if equal
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase) const {
1999-03-22 12:54:46 +03:00
NS_ASSERTION(0!=s1,kNullPointerError);
NS_ASSERTION(0!=s2,kNullPointerError);
PRBool result=PR_FALSE;
if((s1) && (s2)){
PRInt32 cmp=(aIgnoreCase) ? nsCRT::strcasecmp(s1,s2) : nsCRT::strcmp(s1,s2);
result=PRBool(0==cmp);
}
1999-07-17 11:26:16 +04:00
return result;
1999-03-22 12:54:46 +03:00
}
/**
* Determine if given char in valid alpha range
*
* @update gess 3/31/98
* @param aChar is character to be tested
* @return TRUE if in alpha range
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::IsAlpha(PRUnichar aChar) {
1999-03-22 12:54:46 +03:00
// XXX i18n
if (((aChar >= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) {
return PR_TRUE;
}
return PR_FALSE;
}
/**
* Determine if given char is a valid space character
*
* @update gess 3/31/98
* @param aChar is character to be tested
* @return TRUE if is valid space char
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::IsSpace(PRUnichar aChar) {
1999-03-22 12:54:46 +03:00
// XXX i18n
if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) {
return PR_TRUE;
}
return PR_FALSE;
}
/**
* Determine if given buffer contains plain ascii
*
* @param aBuffer -- if null, then we test *this, otherwise we test given buffer
* @return TRUE if is all ascii chars, or if strlen==0
*/
PRBool nsString::IsASCII(const PRUnichar* aBuffer) {
if(!aBuffer) {
aBuffer=mUStr;
if(eOneByte==mCharSize)
return PR_TRUE;
}
if(aBuffer) {
while(*aBuffer) {
if(*aBuffer>255){
return PR_FALSE;
}
aBuffer++;
}
}
return PR_TRUE;
}
1999-03-22 12:54:46 +03:00
/**
* Determine if given char is valid digit
*
* @update gess 3/31/98
* @param aChar is character to be tested
* @return TRUE if char is a valid digit
*/
1999-07-17 11:26:16 +04:00
PRBool nsString::IsDigit(PRUnichar aChar) {
1999-03-22 12:54:46 +03:00
// XXX i18n
return PRBool((aChar >= '0') && (aChar <= '9'));
}
1999-07-17 11:26:16 +04:00
#ifndef RICKG_TESTBED
/**************************************************************
Define the string deallocator class...
**************************************************************/
class nsStringDeallocator: public nsDequeFunctor{
public:
virtual void* operator()(void* anObject) {
static nsMemoryAgent theAgent;
1999-07-17 11:26:16 +04:00
nsString* aString= (nsString*)anObject;
if(aString){
aString->mAgent=&theAgent;
delete aString;
}
return 0;
}
};
/****************************************************************************
1999-07-17 11:26:16 +04:00
* This class, appropriately enough, creates and recycles nsString objects..
****************************************************************************/
class nsStringRecycler {
public:
nsStringRecycler() : mDeque(0) {
}
~nsStringRecycler() {
nsStringDeallocator theDeallocator;
mDeque.ForEach(theDeallocator); //now delete the strings
}
1999-07-17 11:26:16 +04:00
void Recycle(nsString* aString) {
mDeque.Push(aString);
}
1999-07-17 11:26:16 +04:00
nsString* CreateString(eCharSize aCharSize){
nsString* result=(nsString*)mDeque.Pop();
if(!result)
1999-07-17 11:26:16 +04:00
result=new nsString(aCharSize);
return result;
}
nsDeque mDeque;
};
1999-07-17 11:26:16 +04:00
static nsStringRecycler& GetRecycler(void);
1999-04-25 06:05:03 +04:00
/**
*
* @update gess 01/04/99
* @param
* @return
*/
nsStringRecycler& GetRecycler(void){
static nsStringRecycler gRecycler;
return gRecycler;
}
1999-07-17 11:26:16 +04:00
#endif
/**
* Call this mehod when you're done
* @update gess 01/04/99
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
nsString* nsString::CreateString(eCharSize aCharSize){
nsString* result=0;
#ifndef RICKG_TESTBED
GetRecycler().CreateString(aCharSize);
#endif
return result;
}
/**
* Call this mehod when you're done
* @update gess 01/04/99
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
void nsString::Recycle(nsString* aString){
#ifndef RICKG_TESTBED
GetRecycler().Recycle(aString);
1999-07-17 11:26:16 +04:00
#else
delete aString;
#endif
}
1999-07-17 14:08:10 +04:00
#if 0
1999-03-22 12:54:46 +03:00
/**
*
* @update gess 01/04/99
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
void nsString::DebugDump(ostream& aStream) const {
1999-03-22 12:54:46 +03:00
for(PRUint32 i=0;i<mLength;i++) {
1999-03-23 10:33:10 +03:00
aStream <<mStr[i];
1999-03-22 12:54:46 +03:00
}
aStream << endl;
}
/**
*
* @update gess8/8/98
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
ostream& operator<<(ostream& aStream,const nsString& aString){
1999-06-03 14:49:14 +04:00
if(eOneByte==aString.mCharSize) {
aStream<<aString.mStr;
1999-03-22 12:54:46 +03:00
}
else{
1999-06-03 14:49:14 +04:00
PRUint32 theOffset=0;
const PRUint32 theBufSize=300;
char theBuf[theBufSize+1];
PRUint32 theCount=0;
PRUint32 theRemains=0;
while(theOffset<aString.mLength){
theRemains=aString.mLength-theOffset;
theCount=(theRemains<theBufSize) ? theRemains : theBufSize;
aString.ToCString(theBuf,theCount+1,theOffset);
theBuf[theCount]=0;
aStream<<theBuf;
theOffset+=theCount;
}
1999-03-22 12:54:46 +03:00
}
1999-06-03 14:49:14 +04:00
return aStream;
1999-03-22 12:54:46 +03:00
}
1999-07-17 14:08:10 +04:00
#endif
1999-03-22 12:54:46 +03:00
/**
*
* @update gess 01/04/99
* @param
* @return
*/
1999-07-17 11:26:16 +04:00
NS_COM int fputs(const nsString& aString, FILE* out)
1999-03-22 12:54:46 +03:00
{
char buf[100];
char* cp = buf;
PRInt32 len = aString.mLength;
if (len >= PRInt32(sizeof(buf))) {
cp = aString.ToNewCString();
} else {
aString.ToCString(cp, len + 1);
}
if(len>0)
::fwrite(cp, 1, len, out);
if (cp != buf) {
delete[] cp;
1999-03-22 12:54:46 +03:00
}
return (int) len;
}
/***********************************************************************
IMPLEMENTATION NOTES: AUTOSTRING...
***********************************************************************/
1999-04-25 06:05:03 +04:00
1999-03-22 12:54:46 +03:00
/**
* Special case constructor, that allows the consumer to provide
* an underlying buffer for performance reasons.
* @param aBuffer points to your buffer
* @param aBufSize defines the size of your buffer
* @param aCurrentLength tells us the current length of the buffer
*/
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString(eCharSize aCharSize) : nsString(aCharSize){
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
mAgent=0;
AddNullTerminator(*this);
}
/**
* Copy construct from ascii c-string
* @param aCString is a ptr to a 1-byte cstr
*/
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString(const char* aCString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) {
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
mAgent=0;
AddNullTerminator(*this);
1999-07-17 11:26:16 +04:00
Append(aCString,aLength);
}
/**
* Copy construct from uni-string
* @param aString is a ptr to a unistr
*/
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString(const PRUnichar* aString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) {
mAgent=0;
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
AddNullTerminator(*this);
1999-07-17 11:26:16 +04:00
Append(aString,aLength);
}
/**
* Copy construct from uni-string
* @param aString is a ptr to a unistr
*/
nsAutoString::nsAutoString(CBufDescriptor& aBuffer) : nsString(eTwoByte) {
mAgent=0;
1999-07-17 11:26:16 +04:00
if(!aBuffer.mBuffer) {
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE);
}
else {
1999-07-17 11:26:16 +04:00
nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased);
}
AddNullTerminator(*this);
}
1999-07-17 11:26:16 +04:00
/**
1999-07-17 11:26:16 +04:00
* Copy construct from an nsString
* @param
*/
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString(const nsStr& aString,eCharSize aCharSize) : nsString(aCharSize) {
mAgent=0;
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
AddNullTerminator(*this);
1999-07-17 11:26:16 +04:00
Append(aString);
1999-07-09 10:01:55 +04:00
}
/**
1999-07-17 11:26:16 +04:00
* Copy construct from an nsString
1999-07-09 10:01:55 +04:00
* @param
*/
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString(const nsAutoString& aString,eCharSize aCharSize) : nsString(aCharSize) {
1999-07-09 10:01:55 +04:00
mAgent=0;
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
AddNullTerminator(*this);
1999-07-17 11:26:16 +04:00
Append(aString);
}
/**
1999-07-17 11:26:16 +04:00
* Copy construct from an nsString
* @param
*/
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString(PRUnichar aChar,eCharSize aCharSize) : nsString(aCharSize){
mAgent=0;
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
AddNullTerminator(*this);
1999-07-17 11:26:16 +04:00
Append(aChar);
}
/**
* construct from a subsumeable string
* @update gess 1/4/99
* @param reference to a subsumeString
*/
#ifdef AIX
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString(const nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) {
mAgent=0;
nsSubsumeStr temp(aSubsumeStr); // a temp is needed for the AIX compiler
Subsume(*this,temp);
#else
1999-07-17 11:26:16 +04:00
nsAutoString::nsAutoString( nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) {
mAgent=0;
Subsume(*this,aSubsumeStr);
#endif // AIX
}
/**
* deconstruct the autstring
* @param
*/
1999-07-17 11:26:16 +04:00
nsAutoString::~nsAutoString(){
}
1999-07-17 11:26:16 +04:00
void nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const {
#ifndef RICKG_TESTBED
aHandler->Add(sizeof(*this));
aHandler->Add(mCapacity << mCharSize);
1999-07-17 11:26:16 +04:00
#endif
}
1999-07-17 11:26:16 +04:00
nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString(aString.mCharSize) {
1999-07-09 10:01:55 +04:00
Subsume(*this,aString);
}
1999-07-17 11:26:16 +04:00
nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eTwoByte) {
mUStr=aString;
mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1;
mOwnsBuffer=assumeOwnership;
}
1999-06-03 14:49:14 +04:00
1999-07-17 11:26:16 +04:00
nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eOneByte) {
mStr=aString;
mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1;
mOwnsBuffer=assumeOwnership;
}