2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
2000-05-16 11:18:04 +04:00
|
|
|
* Rick Gessner <rickg@netscape.com>
|
|
|
|
* Scott Collins <scc@mozilla.org>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-09-04 21:26:37 +04:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
#include <ctype.h>
|
1999-07-17 11:26:16 +04:00
|
|
|
#include <string.h>
|
1998-04-14 00:24:54 +04:00
|
|
|
#include <stdio.h>
|
1999-09-20 09:32:36 +04:00
|
|
|
#include <stdlib.h>
|
2002-02-15 02:23:55 +03:00
|
|
|
#include "nsStrPrivate.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsString.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
1999-07-09 10:01:55 +04:00
|
|
|
#include "nsDebug.h"
|
1999-07-17 11:26:16 +04:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsDeque.h"
|
|
|
|
|
|
|
|
#ifndef RICKG_TESTBED
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "prdtoa.h"
|
1998-06-06 01:38:36 +04:00
|
|
|
#include "nsISizeOfHandler.h"
|
1999-07-17 11:26:16 +04:00
|
|
|
#endif
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2000-02-19 12:17:49 +03:00
|
|
|
static const char* kPossibleNull = "Error: possible unintended null in string";
|
1999-07-17 11:26:16 +04:00
|
|
|
static const char* kNullPointerError = "Error: unexpected null ptr";
|
|
|
|
static const char* kWhitespace="\b\t\r\n ";
|
1998-08-04 00:46:38 +04:00
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
const nsBufferHandle<char>*
|
|
|
|
nsCString::GetFlatBufferHandle() const
|
|
|
|
{
|
|
|
|
return NS_REINTERPRET_CAST(const nsBufferHandle<char>*, 1);
|
|
|
|
}
|
1998-08-04 00:46:38 +04:00
|
|
|
|
1998-05-20 20:21:48 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Default constructor.
|
1998-05-20 20:21:48 +04:00
|
|
|
*/
|
1999-10-05 08:47:19 +04:00
|
|
|
nsCString::nsCString() {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,eOneByte);
|
1998-05-20 20:21:48 +04:00
|
|
|
}
|
|
|
|
|
2000-03-31 02:51:51 +04:00
|
|
|
nsCString::nsCString(const char* aCString) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,eOneByte);
|
2000-03-31 02:51:51 +04:00
|
|
|
Assign(aCString);
|
|
|
|
}
|
|
|
|
|
1999-07-09 09:11:25 +04:00
|
|
|
/**
|
1999-07-17 11:26:16 +04:00
|
|
|
* This constructor accepts an ascii string
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 1/4/99
|
1999-07-09 09:11:25 +04:00
|
|
|
* @param aCString is a ptr to a 1-byte cstr
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aLength tells us how many chars to copy from given CString
|
1999-07-09 09:11:25 +04:00
|
|
|
*/
|
1999-10-05 08:47:19 +04:00
|
|
|
nsCString::nsCString(const char* aCString,PRInt32 aLength) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,eOneByte);
|
1999-07-17 11:26:16 +04:00
|
|
|
Assign(aCString,aLength);
|
1999-07-09 09:11:25 +04:00
|
|
|
}
|
1998-07-30 22:35:58 +04:00
|
|
|
|
1999-07-09 09:11:25 +04:00
|
|
|
/**
|
1999-07-17 11:26:16 +04:00
|
|
|
* This is our copy constructor
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 1/4/99
|
1999-07-17 11:26:16 +04:00
|
|
|
* @param reference to another nsCString
|
1999-07-09 09:11:25 +04:00
|
|
|
*/
|
1999-10-05 08:47:19 +04:00
|
|
|
nsCString::nsCString(const nsCString& aString) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,aString.GetCharSize());
|
|
|
|
nsStrPrivate::StrAssign(*this,aString,0,aString.mLength);
|
1999-07-09 09:11:25 +04:00
|
|
|
}
|
1998-08-04 00:46:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
nsCString::~nsCString() {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Destroy(*this);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2000-03-25 01:06:57 +03:00
|
|
|
const char* nsCString::GetReadableFragment( nsReadableFragment<char>& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const {
|
2000-03-23 05:12:23 +03:00
|
|
|
switch ( aRequest ) {
|
|
|
|
case kFirstFragment:
|
|
|
|
case kLastFragment:
|
|
|
|
case kFragmentAt:
|
|
|
|
aFragment.mEnd = (aFragment.mStart = mStr) + mLength;
|
|
|
|
return aFragment.mStart + aOffset;
|
|
|
|
|
|
|
|
case kPrevFragment:
|
|
|
|
case kNextFragment:
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-25 01:06:57 +03:00
|
|
|
char* nsCString::GetWritableFragment( nsWritableFragment<char>& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) {
|
2000-03-23 05:12:23 +03:00
|
|
|
switch ( aRequest ) {
|
|
|
|
case kFirstFragment:
|
|
|
|
case kLastFragment:
|
|
|
|
case kFragmentAt:
|
|
|
|
aFragment.mEnd = (aFragment.mStart = mStr) + mLength;
|
|
|
|
return aFragment.mStart + aOffset;
|
|
|
|
|
|
|
|
case kPrevFragment:
|
|
|
|
case kNextFragment:
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
nsCString::nsCString( const nsACString& aReadable ) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,eOneByte);
|
2000-03-23 05:12:23 +03:00
|
|
|
Assign(aReadable);
|
|
|
|
}
|
|
|
|
|
2001-10-16 09:31:36 +04:00
|
|
|
#ifdef DEBUG
|
1999-08-31 07:04:37 +04:00
|
|
|
void nsCString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const {
|
|
|
|
if (aResult) {
|
2002-01-25 02:46:56 +03:00
|
|
|
*aResult = sizeof(*this) + GetCapacity() * GetCharSize();
|
1999-08-31 07:04:37 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
}
|
2001-10-16 09:31:36 +04:00
|
|
|
#endif
|
1998-09-04 21:26:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method truncates this string to given length.
|
|
|
|
*
|
2000-03-24 10:21:56 +03:00
|
|
|
* @update rickg 03.23.2000
|
1998-09-04 21:26:37 +04:00
|
|
|
* @param anIndex -- new length of string
|
|
|
|
* @return nada
|
|
|
|
*/
|
2000-03-23 05:12:23 +03:00
|
|
|
void nsCString::SetLength(PRUint32 anIndex) {
|
2002-01-25 02:46:56 +03:00
|
|
|
if ( anIndex > GetCapacity() )
|
2000-03-23 05:12:23 +03:00
|
|
|
SetCapacity(anIndex);
|
2000-08-09 12:26:23 +04:00
|
|
|
// |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAWritableString.h),
|
|
|
|
// we can only use it since our local implementation, |nsCString::SetCapacity|, is known to do what we want
|
|
|
|
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrTruncate(*this,anIndex);
|
1998-06-06 01:38:36 +04:00
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
|
|
|
|
/**
|
2000-07-29 07:11:59 +04:00
|
|
|
* Call this method if you want to force the string to a certain capacity;
|
|
|
|
* |SetCapacity(0)| discards associated storage.
|
|
|
|
*
|
|
|
|
* @param aNewCapacity -- desired minimum capacity
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-07-29 07:11:59 +04:00
|
|
|
void
|
|
|
|
nsCString::SetCapacity( PRUint32 aNewCapacity )
|
|
|
|
{
|
|
|
|
if ( aNewCapacity )
|
|
|
|
{
|
2002-01-25 02:46:56 +03:00
|
|
|
if( aNewCapacity > GetCapacity() )
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::GrowCapacity(*this,aNewCapacity);
|
2000-07-29 07:11:59 +04:00
|
|
|
AddNullTerminator(*this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Destroy(*this);
|
|
|
|
nsStrPrivate::Initialize(*this, eOneByte);
|
2000-07-29 07:11:59 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**********************************************************************
|
|
|
|
Accessor methods...
|
|
|
|
*********************************************************************/
|
1999-07-09 09:11:25 +04:00
|
|
|
|
1999-08-30 10:55:13 +04:00
|
|
|
/**
|
|
|
|
* set a char inside this string at given index
|
|
|
|
* @param aChar is the char you want to write into this string
|
|
|
|
* @param anIndex is the ofs where you want to write the given char
|
|
|
|
* @return TRUE if successful
|
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
PRBool nsCString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){
|
1999-03-22 11:29:26 +03:00
|
|
|
PRBool result=PR_FALSE;
|
|
|
|
if(anIndex<mLength){
|
1999-07-25 21:20:37 +04:00
|
|
|
mStr[anIndex]=(char)aChar;
|
1999-03-22 11:29:26 +03:00
|
|
|
result=PR_TRUE;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-11-14 09:22:52 +03:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/*********************************************************
|
|
|
|
append (operator+) METHODS....
|
|
|
|
*********************************************************/
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**********************************************************************
|
|
|
|
Lexomorphic transforms...
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to remove all occurances of the
|
|
|
|
* characters found in aSet from this string.
|
|
|
|
*
|
2000-03-28 04:02:41 +04:00
|
|
|
* @update rickg 03.27.2000
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aChar -- char to be stripped
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return *this
|
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::StripChar(PRUnichar aChar,PRInt32 anOffset){
|
2000-03-28 04:02:41 +04:00
|
|
|
if(mLength && (anOffset<PRInt32(mLength))) {
|
2002-01-24 22:14:18 +03:00
|
|
|
char* to = mStr + anOffset;
|
|
|
|
char* from = mStr + anOffset;
|
|
|
|
char* end = mStr + mLength;
|
|
|
|
|
|
|
|
while (from < end) {
|
|
|
|
char theChar = *from++;
|
|
|
|
if(aChar!=theChar) {
|
|
|
|
*to++ = theChar;
|
2000-03-28 04:02:41 +04:00
|
|
|
}
|
|
|
|
}
|
2002-01-24 22:14:18 +03:00
|
|
|
*to = 0; //add the null
|
|
|
|
mLength=to - mStr;
|
1999-12-03 03:30:29 +03:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
}
|
1999-07-09 10:01:55 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::StripChars(const char* aSet){
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StripChars1(*this,aSet);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-07-17 11:26:16 +04:00
|
|
|
* This method strips whitespace throughout the string
|
|
|
|
*
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @return this
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::StripWhitespace() {
|
|
|
|
StripChars(kWhitespace);
|
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
|
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::ReplaceChar(PRUnichar aOldChar, PRUnichar aNewChar) {
|
1999-07-17 11:26:16 +04:00
|
|
|
PRUint32 theIndex=0;
|
2000-02-18 10:47:47 +03:00
|
|
|
|
|
|
|
if((aOldChar<256) && (aNewChar<256)){
|
|
|
|
//only execute this if oldchar and newchar are within legal ascii range
|
|
|
|
for(theIndex=0;theIndex<mLength;theIndex++){
|
|
|
|
if(mStr[theIndex]==(char)aOldChar) {
|
|
|
|
mStr[theIndex]=(char)aNewChar;
|
|
|
|
}//if
|
|
|
|
}
|
1999-03-07 22:23:28 +03:00
|
|
|
}
|
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
|
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::ReplaceChar(const char* aSet, PRUnichar aNewChar){
|
2000-02-18 10:47:47 +03:00
|
|
|
if(aSet && (aNewChar<256)){
|
|
|
|
//only execute this if newchar is valid ascii, and aset isn't null.
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 theIndex=FindCharInSet(aSet,0);
|
|
|
|
while(kNotFound<theIndex) {
|
|
|
|
mStr[theIndex]=(char)aNewChar;
|
|
|
|
theIndex=FindCharInSet(aSet,theIndex+1);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-02 13:14:38 +03:00
|
|
|
/**
|
|
|
|
* This method is used to replace all occurances of the
|
|
|
|
* given target with the given replacement
|
|
|
|
*
|
|
|
|
* @param
|
|
|
|
* @return *this
|
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::ReplaceSubstring(const char* aTarget,const char* aNewValue){
|
1999-12-02 13:14:38 +03:00
|
|
|
if(aTarget && aNewValue) {
|
|
|
|
|
|
|
|
PRInt32 len=strlen(aTarget);
|
|
|
|
if(0<len) {
|
|
|
|
CBufDescriptor theDesc1(aTarget,PR_TRUE, len+1,len);
|
|
|
|
nsCAutoString theTarget(theDesc1);
|
|
|
|
|
|
|
|
len=strlen(aNewValue);
|
|
|
|
if(0<len) {
|
|
|
|
CBufDescriptor theDesc2(aNewValue,PR_TRUE, len+1,len);
|
|
|
|
nsCAutoString theNewValue(theDesc2);
|
|
|
|
|
|
|
|
ReplaceSubstring(theTarget,theNewValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to replace all occurances of the
|
|
|
|
* given target substring with the given replacement substring
|
|
|
|
*
|
|
|
|
* @param aTarget
|
|
|
|
* @param aNewValue
|
|
|
|
* @return *this
|
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::ReplaceSubstring(const nsCString& aTarget,const nsCString& aNewValue){
|
1999-12-02 13:14:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
//WARNING: This is not working yet!!!!!
|
|
|
|
|
|
|
|
if(aTarget.mLength && aNewValue.mLength) {
|
|
|
|
PRBool isSameLen=(aTarget.mLength==aNewValue.mLength);
|
|
|
|
|
|
|
|
if((isSameLen) && (1==aNewValue.mLength)) {
|
|
|
|
ReplaceChar(aTarget.CharAt(0),aNewValue.CharAt(0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRInt32 theIndex=0;
|
2002-02-15 02:23:55 +03:00
|
|
|
while(kNotFound!=(theIndex=nsStrPrivate::FindSubstr1in1(*this,aTarget,PR_FALSE,theIndex,mLength))) {
|
1999-12-02 13:14:38 +03:00
|
|
|
if(aNewValue.mLength<aTarget.mLength) {
|
|
|
|
//Since target is longer than newValue, we should delete a few chars first, then overwrite.
|
|
|
|
PRInt32 theDelLen=aTarget.mLength-aNewValue.mLength;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Delete1(*this,theIndex,theDelLen);
|
|
|
|
nsStrPrivate::Overwrite(*this,aNewValue,theIndex);
|
1999-12-02 13:14:38 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
//this is the worst case: the newvalue is larger than the substr it's replacing
|
|
|
|
//so we have to insert some characters...
|
|
|
|
PRInt32 theInsLen=aNewValue.mLength-aTarget.mLength;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrInsert1into1(*this,theIndex,aNewValue,0,theInsLen);
|
|
|
|
nsStrPrivate::Overwrite(*this,aNewValue,theIndex);
|
2002-01-03 02:54:25 +03:00
|
|
|
theIndex += aNewValue.mLength;
|
1999-12-02 13:14:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-07-17 11:26:16 +04: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
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::Trim(const char* aTrimSet, PRBool aEliminateLeading,PRBool aEliminateTrailing,PRBool aIgnoreQuotes){
|
2000-02-11 15:11:03 +03:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
if(aTrimSet){
|
2000-02-11 15:11:03 +03:00
|
|
|
|
|
|
|
PRUnichar theFirstChar=0;
|
|
|
|
PRUnichar theLastChar=0;
|
|
|
|
PRBool theQuotesAreNeeded=PR_FALSE;
|
|
|
|
|
|
|
|
if(aIgnoreQuotes && (mLength>2)) {
|
|
|
|
theFirstChar=First();
|
|
|
|
theLastChar=Last();
|
|
|
|
if(theFirstChar==theLastChar) {
|
|
|
|
if(('\''==theFirstChar) || ('"'==theFirstChar)) {
|
|
|
|
Cut(0,1);
|
|
|
|
Truncate(mLength-1);
|
|
|
|
theQuotesAreNeeded=PR_TRUE;
|
|
|
|
}
|
|
|
|
else theFirstChar=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Trim(*this,aTrimSet,aEliminateLeading,aEliminateTrailing);
|
2000-02-11 15:11:03 +03:00
|
|
|
|
|
|
|
if(aIgnoreQuotes && theQuotesAreNeeded) {
|
2000-04-15 04:05:17 +04:00
|
|
|
InsertWithConversion(theFirstChar,0);
|
|
|
|
AppendWithConversion(theLastChar);
|
2000-02-11 15:11:03 +03:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* This method strips chars in given set from string.
|
|
|
|
* You can control whether chars are yanked from
|
1999-07-17 11:26:16 +04:00
|
|
|
* 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
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){
|
1999-07-17 11:26:16 +04:00
|
|
|
if(aSet){
|
|
|
|
ReplaceChar(aSet,aChar);
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::CompressSet1(*this,aSet,aEliminateLeading,aEliminateTrailing);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-07-17 11:26:16 +04:00
|
|
|
* 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
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-04-21 10:59:14 +04:00
|
|
|
void
|
|
|
|
nsCString::CompressWhitespace( PRBool aEliminateLeading,PRBool aEliminateTrailing){
|
1999-07-17 11:26:16 +04:00
|
|
|
CompressSet(kWhitespace,' ',aEliminateLeading,aEliminateTrailing);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**********************************************************************
|
|
|
|
string conversion methods...
|
|
|
|
*********************************************************************/
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
|
|
|
* Perform string to float conversion.
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
1998-08-04 00:46:38 +04:00
|
|
|
* @param aErrorCode will contain error if one occurs
|
|
|
|
* @return float rep of string value
|
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
float nsCString::ToFloat(PRInt32* aErrorCode) const {
|
|
|
|
char buf[100];
|
1998-07-25 06:11:02 +04:00
|
|
|
if (mLength > PRInt32(sizeof(buf)-1)) {
|
1998-04-26 07:13:20 +04:00
|
|
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
1998-04-14 00:24:54 +04:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2001-10-16 09:35:38 +04:00
|
|
|
char *cp = strncpy(buf, get(), sizeof(buf) - 1);
|
|
|
|
buf[sizeof(buf)-1] = '\0';
|
1998-04-14 00:24:54 +04:00
|
|
|
float f = (float) PR_strtod(cp, &cp);
|
|
|
|
if (*cp != 0) {
|
1998-04-26 07:13:20 +04:00
|
|
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1998-04-26 07:13:20 +04:00
|
|
|
*aErrorCode = (PRInt32) NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
/**
|
1999-07-17 11:26:16 +04:00
|
|
|
* Perform decimal numeric string to int conversion.
|
|
|
|
* NOTE: In this version, we use the radix you give, even if it's wrong.
|
2000-02-13 20:46:15 +03:00
|
|
|
* @update gess 02/14/00
|
1999-07-17 11:26:16 +04:00
|
|
|
* @param aErrorCode will contain error if one occurs
|
2000-02-13 20:46:15 +03:00
|
|
|
* @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix.
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return int rep of string value
|
1999-07-09 10:01:55 +04:00
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
2000-02-13 20:46:15 +03:00
|
|
|
char* cp=mStr;
|
2000-08-09 12:26:23 +04:00
|
|
|
PRInt32 theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect)
|
2000-02-11 15:11:03 +03:00
|
|
|
PRInt32 result=0;
|
|
|
|
PRBool negate=PR_FALSE;
|
|
|
|
char theChar=0;
|
|
|
|
|
2000-08-09 12:26:23 +04:00
|
|
|
//initial value, override if we find an integer
|
2000-02-11 15:11:03 +03:00
|
|
|
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
if(cp) {
|
|
|
|
|
|
|
|
//begin by skipping over leading chars that shouldn't be part of the number...
|
|
|
|
|
2000-02-13 20:46:15 +03:00
|
|
|
char* endcp=cp+mLength;
|
2000-02-11 15:11:03 +03:00
|
|
|
PRBool done=PR_FALSE;
|
|
|
|
|
|
|
|
while((cp<endcp) && (!done)){
|
|
|
|
switch(*cp++) {
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
theRadix=16;
|
|
|
|
done=PR_TRUE;
|
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
done=PR_TRUE;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
negate=PR_TRUE; //fall through...
|
|
|
|
break;
|
|
|
|
case 'X': case 'x':
|
|
|
|
theRadix=16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} //switch
|
|
|
|
}
|
|
|
|
|
2000-08-09 12:26:23 +04:00
|
|
|
if (done) {
|
|
|
|
|
2000-08-23 21:27:06 +04:00
|
|
|
//integer found
|
2000-08-09 12:26:23 +04:00
|
|
|
*anErrorCode = NS_OK;
|
2000-03-08 09:16:29 +03:00
|
|
|
|
2000-08-09 12:26:23 +04:00
|
|
|
if (aRadix!=kAutoDetect) theRadix = aRadix; // override
|
2000-02-11 15:11:03 +03:00
|
|
|
|
|
|
|
//now iterate the numeric chars and build our result
|
|
|
|
char* first=--cp; //in case we have to back up.
|
|
|
|
|
2000-07-29 07:11:59 +04:00
|
|
|
while(cp<endcp){
|
2000-02-11 15:11:03 +03:00
|
|
|
theChar=*cp++;
|
|
|
|
if(('0'<=theChar) && (theChar<='9')){
|
|
|
|
result = (theRadix * result) + (theChar-'0');
|
|
|
|
}
|
|
|
|
else if((theChar>='A') && (theChar<='F')) {
|
|
|
|
if(10==theRadix) {
|
|
|
|
if(kAutoDetect==aRadix){
|
|
|
|
theRadix=16;
|
|
|
|
cp=first; //backup
|
|
|
|
result=0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (theRadix * result) + ((theChar-'A')+10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((theChar>='a') && (theChar<='f')) {
|
|
|
|
if(10==theRadix) {
|
|
|
|
if(kAutoDetect==aRadix){
|
|
|
|
theRadix=16;
|
|
|
|
cp=first; //backup
|
|
|
|
result=0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (theRadix * result) + ((theChar-'a')+10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(('X'==theChar) || ('x'==theChar) || ('#'==theChar) || ('+'==theChar)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//we've encountered a char that's not a legal number or sign
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} //while
|
|
|
|
if(negate)
|
|
|
|
result=-result;
|
|
|
|
} //if
|
|
|
|
}
|
|
|
|
return result;
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**********************************************************************
|
|
|
|
String manipulation methods...
|
|
|
|
*********************************************************************/
|
|
|
|
|
2000-03-31 02:51:51 +04:00
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* assign given unichar* to this string
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aCString: buffer to be assigned to this
|
1999-11-19 11:05:30 +03:00
|
|
|
* @param aCount -- length of given buffer or -1 if you want me to compute length.
|
|
|
|
* NOTE: IFF you pass -1 as aCount, then your buffer must be null terminated.
|
|
|
|
*
|
1999-08-30 10:55:13 +04:00
|
|
|
* @return this
|
|
|
|
*/
|
2000-03-29 04:06:31 +04:00
|
|
|
void nsCString::AssignWithConversion(const PRUnichar* aString,PRInt32 aCount) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrTruncate(*this,0);
|
1999-08-30 10:55:13 +04:00
|
|
|
|
1999-11-14 09:22:52 +03:00
|
|
|
if(aString && aCount){
|
1999-08-30 10:55:13 +04:00
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
1999-08-30 10:55:13 +04:00
|
|
|
temp.mUStr=(PRUnichar*)aString;
|
|
|
|
|
|
|
|
if(0<aCount) {
|
|
|
|
temp.mLength=aCount;
|
1999-11-14 09:22:52 +03:00
|
|
|
|
|
|
|
// If this assertion fires, the caller is probably lying about the length of
|
|
|
|
// the passed-in string. File a bug on the caller.
|
|
|
|
#ifdef NS_DEBUG
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 len=nsStrPrivate::FindChar2(temp,0,PR_FALSE,temp.mLength);
|
2000-02-19 12:17:49 +03:00
|
|
|
if(kNotFound<len) {
|
|
|
|
NS_WARNING(kPossibleNull);
|
|
|
|
}
|
1999-11-14 09:22:52 +03:00
|
|
|
#endif
|
|
|
|
|
1999-08-30 10:55:13 +04:00
|
|
|
}
|
|
|
|
else aCount=temp.mLength=nsCRT::strlen(aString);
|
|
|
|
|
|
|
|
if(0<aCount)
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrAppend(*this,temp,0,aCount);
|
1999-08-30 10:55:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-22 13:17:20 +04:00
|
|
|
void nsCString::AssignWithConversion( const nsString& aString ) {
|
2001-06-30 15:02:25 +04:00
|
|
|
AssignWithConversion(aString.get(), aString.Length());
|
2000-04-22 13:17:20 +04:00
|
|
|
}
|
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
void nsCString::AssignWithConversion( const nsAString& aString ) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrTruncate(*this,0);
|
2000-08-23 21:27:06 +04:00
|
|
|
PRInt32 count = aString.Length();
|
|
|
|
|
|
|
|
if(count){
|
2001-09-25 13:35:50 +04:00
|
|
|
nsAString::const_iterator start; aString.BeginReading(start);
|
|
|
|
nsAString::const_iterator end; aString.EndReading(end);
|
2000-08-23 21:27:06 +04:00
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
PRUint32 fraglen = start.size_forward();
|
|
|
|
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
2000-08-23 21:27:06 +04:00
|
|
|
temp.mUStr=(PRUnichar*)start.get();
|
|
|
|
|
|
|
|
temp.mLength=fraglen;
|
|
|
|
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrAppend(*this,temp,0,fraglen);
|
2001-04-02 23:40:52 +04:00
|
|
|
|
|
|
|
start.advance(fraglen);
|
2000-08-23 21:27:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
void nsCString::AppendWithConversion( const nsAString& aString ) {
|
2000-08-23 21:27:06 +04:00
|
|
|
PRInt32 count = aString.Length();
|
|
|
|
|
|
|
|
if(count){
|
2001-09-25 13:35:50 +04:00
|
|
|
nsAString::const_iterator start; aString.BeginReading(start);
|
|
|
|
nsAString::const_iterator end; aString.EndReading(end);
|
2000-08-23 21:27:06 +04:00
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
PRUint32 fraglen = start.size_forward();
|
|
|
|
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
2000-08-23 21:27:06 +04:00
|
|
|
temp.mUStr=(PRUnichar*)start.get();
|
2000-04-22 13:17:20 +04:00
|
|
|
|
2000-08-23 21:27:06 +04:00
|
|
|
temp.mLength=fraglen;
|
|
|
|
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrAppend(*this,temp,0,fraglen);
|
2000-08-23 21:27:06 +04:00
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
start.advance(fraglen);
|
2000-08-23 21:27:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-11-14 09:22:52 +03:00
|
|
|
|
1999-08-30 10:55:13 +04:00
|
|
|
/**
|
|
|
|
* assign given unichar to this string
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
1998-04-14 00:24:54 +04:00
|
|
|
* @param aChar: char to be assignd to this
|
|
|
|
* @return this
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-03-29 04:06:31 +04:00
|
|
|
void nsCString::AssignWithConversion(PRUnichar aChar) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrTruncate(*this,0);
|
2000-04-15 04:05:17 +04:00
|
|
|
AppendWithConversion(aChar);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
2000-03-31 02:51:51 +04:00
|
|
|
* append given char to this string
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aChar: char to be appended to this
|
1998-04-14 00:24:54 +04:00
|
|
|
* @return this
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-03-31 02:51:51 +04:00
|
|
|
void nsCString::AppendWithConversion(PRUnichar aChar) {
|
|
|
|
PRUnichar buf[2]={0,0};
|
1999-07-17 11:26:16 +04:00
|
|
|
buf[0]=aChar;
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
2000-03-31 02:51:51 +04:00
|
|
|
temp.mUStr=buf;
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mLength=1;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrAppend(*this,temp,0,1);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
|
|
|
|
2000-08-19 23:34:00 +04:00
|
|
|
void nsCString::AppendWithConversion( const PRUnichar* aBuffer, PRInt32 aLength )
|
|
|
|
{
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp, eTwoByte);
|
2000-08-19 23:34:00 +04:00
|
|
|
temp.mUStr = NS_CONST_CAST(PRUnichar*, aBuffer);
|
|
|
|
|
|
|
|
if ( aLength < 0 )
|
|
|
|
aLength = nsCharTraits<PRUnichar>::length(aBuffer);
|
|
|
|
|
|
|
|
if ( aLength > 0 )
|
2000-08-21 00:41:31 +04:00
|
|
|
{
|
|
|
|
temp.mLength = aLength;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrAppend(*this, temp, 0, aLength);
|
2000-08-21 00:41:31 +04:00
|
|
|
}
|
2000-08-19 23:34:00 +04:00
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Append the given integer to this string
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aInteger:
|
|
|
|
* @param aRadix:
|
1998-04-14 00:24:54 +04:00
|
|
|
* @return
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-04-15 04:05:17 +04:00
|
|
|
void nsCString::AppendInt(PRInt32 anInteger,PRInt32 aRadix) {
|
1999-09-20 09:50:43 +04:00
|
|
|
|
1999-09-30 08:03:49 +04:00
|
|
|
PRUint32 theInt=(PRUint32)anInteger;
|
1999-09-20 09:10:32 +04:00
|
|
|
|
1999-09-30 08:03:49 +04:00
|
|
|
char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
1999-09-20 09:10:32 +04:00
|
|
|
|
1999-09-30 08:03:49 +04:00
|
|
|
PRInt32 radices[] = {1000000000,268435456};
|
|
|
|
PRInt32 mask1=radices[16==aRadix];
|
1999-09-20 09:10:32 +04:00
|
|
|
|
1999-09-30 08:03:49 +04:00
|
|
|
PRInt32 charpos=0;
|
|
|
|
if(anInteger<0) {
|
|
|
|
theInt*=-1;
|
|
|
|
if(10==aRadix) {
|
|
|
|
buf[charpos++]='-';
|
|
|
|
}
|
|
|
|
else theInt=(int)~(theInt-1);
|
1999-09-20 09:50:43 +04:00
|
|
|
}
|
|
|
|
|
1999-09-30 09:15:37 +04:00
|
|
|
PRBool isfirst=PR_TRUE;
|
1999-09-30 08:03:49 +04:00
|
|
|
while(mask1>=1) {
|
1999-10-05 08:47:19 +04:00
|
|
|
PRInt32 theDiv=theInt/mask1;
|
|
|
|
if((theDiv) || (!isfirst)) {
|
|
|
|
buf[charpos++]="0123456789abcdef"[theDiv];
|
1999-09-30 09:15:37 +04:00
|
|
|
isfirst=PR_FALSE;
|
1999-09-30 08:03:49 +04:00
|
|
|
}
|
1999-10-05 08:47:19 +04:00
|
|
|
theInt-=theDiv*mask1;
|
1999-09-30 08:03:49 +04:00
|
|
|
mask1/=aRadix;
|
|
|
|
}
|
2000-03-31 02:51:51 +04:00
|
|
|
Append(buf);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Append the given float to this string
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aFloat:
|
1998-04-14 00:24:54 +04:00
|
|
|
* @return
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-04-15 04:05:17 +04:00
|
|
|
void nsCString::AppendFloat( double aFloat ){
|
1998-04-14 00:24:54 +04:00
|
|
|
char buf[40];
|
1999-07-17 11:26:16 +04:00
|
|
|
// *** XX UNCOMMENT THIS LINE
|
|
|
|
//PR_snprintf(buf, sizeof(buf), "%g", aFloat);
|
|
|
|
sprintf(buf,"%g",aFloat);
|
2000-03-31 02:51:51 +04:00
|
|
|
Append(buf);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2000-04-01 04:24:32 +04:00
|
|
|
/**
|
|
|
|
* append given string to this string;
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @param aString : string to be appended to this
|
|
|
|
* @return this
|
|
|
|
*/
|
|
|
|
void nsCString::AppendWithConversion(const nsString& aString,PRInt32 aCount) {
|
|
|
|
|
|
|
|
if(aCount<0)
|
|
|
|
aCount=aString.mLength;
|
|
|
|
else aCount=MinInt(aCount,aString.mLength);
|
|
|
|
|
|
|
|
if(0<aCount)
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrAppend(*this,aString,0,aCount);
|
2000-04-01 04:24:32 +04:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2000-03-31 02:51:51 +04:00
|
|
|
/**
|
|
|
|
* Insert a single unichar into this string at a specified offset.
|
|
|
|
*
|
|
|
|
* @update gess4/22/98
|
|
|
|
* @param aChar unichar to be inserted into this string
|
|
|
|
* @param anOffset is insert pos in str
|
|
|
|
* @return this
|
|
|
|
*/
|
|
|
|
void nsCString::InsertWithConversion(PRUnichar aChar,PRUint32 anOffset){
|
|
|
|
PRUnichar theBuffer[2]={0,0};
|
|
|
|
theBuffer[0]=aChar;
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
2000-03-31 02:51:51 +04:00
|
|
|
temp.mUStr=theBuffer;
|
|
|
|
temp.mLength=1;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::StrInsert2into1(*this,anOffset,temp,0,1);
|
2000-03-31 02:51:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-27 02:14:12 +03:00
|
|
|
void nsCString::Adopt(char* aPtr, PRInt32 aLength) {
|
2002-01-02 12:00:02 +03:00
|
|
|
NS_ASSERTION(aPtr, "Can't adopt |0|");
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Destroy(*this);
|
2001-12-27 02:14:12 +03:00
|
|
|
if (aLength == -1)
|
|
|
|
aLength = nsCharTraits<char>::length(aPtr);
|
|
|
|
// We don't know the capacity, so we'll just have to assume
|
|
|
|
// capacity = length.
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this, aPtr, aLength, aLength, eOneByte, PR_TRUE);
|
2001-12-27 02:14:12 +03:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**********************************************************************
|
|
|
|
Searching methods...
|
|
|
|
*********************************************************************/
|
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
|
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Search for given cstr within this string
|
1999-07-09 10:01:55 +04:00
|
|
|
*
|
1999-07-17 11:26:16 +04:00
|
|
|
* @update gess 3/25/98
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aCString - substr to be found
|
2000-02-11 15:11:03 +03:00
|
|
|
* @param aIgnoreCase tells us whether or not to do caseless compare
|
|
|
|
* @param anOffset tells us where in this string to start searching
|
|
|
|
* @param aCount tells us how many iterations to make starting at the given offset
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return offset in string, or -1 (kNotFound)
|
1999-07-09 10:01:55 +04:00
|
|
|
*/
|
2000-02-11 15:11:03 +03:00
|
|
|
PRInt32 nsCString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 result=kNotFound;
|
|
|
|
if(aCString) {
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eOneByte);
|
2002-02-19 19:01:45 +03:00
|
|
|
temp.mLength = strlen(aCString);
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mStr=(char*)aCString;
|
2002-02-15 02:23:55 +03:00
|
|
|
result=nsStrPrivate::FindSubstr1in1(*this,temp,aIgnoreCase,anOffset,aCount);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1998-05-12 03:27:35 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**
|
|
|
|
* Search for given buffer within this string
|
1999-07-09 10:01:55 +04:00
|
|
|
*
|
1999-07-17 11:26:16 +04:00
|
|
|
* @update gess 3/25/98
|
2000-02-11 15:11:03 +03:00
|
|
|
* @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 aCount tells us how many iterations to make starting at the given offset
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return offset in string, or -1 (kNotFound)
|
1999-07-09 10:01:55 +04:00
|
|
|
*/
|
2000-02-11 15:11:03 +03:00
|
|
|
PRInt32 nsCString::Find(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{
|
1999-07-17 11:26:16 +04:00
|
|
|
NS_ASSERTION(0!=aString,kNullPointerError);
|
|
|
|
|
|
|
|
PRInt32 result=kNotFound;
|
|
|
|
if(aString) {
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
2000-02-11 15:11:03 +03:00
|
|
|
temp.mLength = nsCRT::strlen(aString);
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mUStr=(PRUnichar*)aString;
|
2002-02-15 02:23:55 +03:00
|
|
|
result=nsStrPrivate::FindSubstr2in1(*this,temp,aIgnoreCase,anOffset,aCount);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1998-08-04 00:46:38 +04:00
|
|
|
|
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Search for given string within this string
|
1999-07-09 10:01:55 +04:00
|
|
|
*
|
1999-07-17 11:26:16 +04:00
|
|
|
* @update gess 3/25/98
|
2000-02-11 15:11:03 +03:00
|
|
|
* @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 aCount tells us how many iterations to make starting at the given offset
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return offset in string, or -1 (kNotFound)
|
|
|
|
*/
|
2002-01-24 22:14:18 +03:00
|
|
|
PRInt32 nsCString::Find(const nsCString& aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::FindSubstr1in1(*this,aString,aIgnoreCase,anOffset,aCount);
|
2002-01-24 22:14:18 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 nsCString::Find(const nsString& aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::FindSubstr1in2(*this,aString,aIgnoreCase,anOffset,aCount);
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**
|
2000-02-11 15:11:03 +03:00
|
|
|
* This searches this string for a given character
|
1999-07-17 11:26:16 +04:00
|
|
|
*
|
2000-02-11 15:11:03 +03:00
|
|
|
* @update gess 2/04/00
|
|
|
|
* @param char is the character you're trying to find.
|
|
|
|
* @param aIgnorecase indicates case sensitivity of search
|
|
|
|
* @param anOffset tells us where to start the search; -1 means start at 0.
|
|
|
|
* @param aCount tell us how many chars to search from offset; -1 means use full length.
|
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
|
|
|
*/
|
2002-02-01 04:53:09 +03:00
|
|
|
PRInt32 nsCString::FindChar(PRUnichar aChar,PRInt32 anOffset,PRInt32 aCount) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::FindChar1(*this,aChar,anOffset,aCount);
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
|
|
|
}
|
1999-07-09 10:01:55 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* This method finds the offset of the first char in this string that is
|
|
|
|
* a member of the given charset, starting the search at anOffset
|
1999-07-17 11:26:16 +04:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aCStringSet
|
|
|
|
* @param anOffset -- where in this string to start searching
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
PRInt32 nsCString::FindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{
|
|
|
|
NS_ASSERTION(0!=aCStringSet,kNullPointerError);
|
1999-07-09 10:01:55 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 result=kNotFound;
|
|
|
|
if(aCStringSet) {
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eOneByte);
|
2002-02-19 19:01:45 +03:00
|
|
|
temp.mLength=strlen(aCStringSet);
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mStr=(char*)aCStringSet;
|
2002-02-15 02:23:55 +03:00
|
|
|
result=nsStrPrivate::FindCharInSet1(*this,temp,PR_FALSE,anOffset);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* This method finds the offset of the first char in this string that is
|
|
|
|
* a member of the given charset, starting the search at anOffset
|
1998-08-29 09:06:50 +04:00
|
|
|
*
|
1999-07-09 10:01:55 +04:00
|
|
|
* @update gess 3/25/98
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aStringSet
|
|
|
|
* @param anOffset -- where in this string to start searching
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return
|
1998-08-29 09:06:50 +04:00
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 nsCString::FindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{
|
|
|
|
NS_ASSERTION(0!=aStringSet,kNullPointerError);
|
1998-08-29 09:06:50 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 result=kNotFound;
|
|
|
|
if(aStringSet) {
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mLength=nsCRT::strlen(aStringSet);
|
|
|
|
temp.mStr=(char*)aStringSet;
|
2002-02-15 02:23:55 +03:00
|
|
|
result=nsStrPrivate::FindCharInSet2(*this,temp,anOffset);
|
1998-08-29 09:06:50 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1998-08-29 09:06:50 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* This method finds the offset of the first char in this string that is
|
|
|
|
* a member of the given charset, starting the search at anOffset
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aCStringSet
|
|
|
|
* @param anOffset -- where in this string to start searching
|
1999-07-17 11:26:16 +04:00
|
|
|
* @return
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2002-01-24 22:14:18 +03:00
|
|
|
PRInt32 nsCString::FindCharInSet(const nsCString& aSet,PRInt32 anOffset) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::FindCharInSet1(*this,aSet,PR_FALSE,anOffset);
|
2002-01-24 22:14:18 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 nsCString::FindCharInSet(const nsString& aSet,PRInt32 anOffset) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::FindCharInSet2(*this,aSet,anOffset);
|
1998-04-14 00:24:54 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
/**
|
2000-02-11 15:11:03 +03:00
|
|
|
* Reverse search for given string within this string
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
2000-02-11 15:11:03 +03:00
|
|
|
* @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 aCount tells us how many iterations to make starting at the given offset
|
|
|
|
* @return offset in string, or -1 (kNotFound)
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2002-01-24 22:14:18 +03:00
|
|
|
PRInt32 nsCString::RFind(const nsCString& aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::RFindSubstr1in1(*this,aString,aIgnoreCase,anOffset,aCount);
|
2002-01-24 22:14:18 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 nsCString::RFind(const nsString& aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::RFindSubstr2in1(*this,aString,aIgnoreCase,anOffset,aCount);
|
1998-04-14 00:24:54 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
2000-02-11 15:11:03 +03:00
|
|
|
* Reverse search for given string within this string
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
2000-02-11 15:11:03 +03:00
|
|
|
* @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 aCount tells us how many iterations to make starting at the given offset
|
|
|
|
* @return offset in string, or -1 (kNotFound)
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-02-11 15:11:03 +03:00
|
|
|
PRInt32 nsCString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) const{
|
1999-07-17 11:26:16 +04:00
|
|
|
NS_ASSERTION(0!=aString,kNullPointerError);
|
1999-07-09 10:01:55 +04:00
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 result=kNotFound;
|
|
|
|
if(aString) {
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eOneByte);
|
2002-02-19 19:01:45 +03:00
|
|
|
temp.mLength=strlen(aString);
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mStr=(char*)aString;
|
2002-02-15 02:23:55 +03:00
|
|
|
result=nsStrPrivate::RFindSubstr1in1(*this,temp,aIgnoreCase,anOffset,aCount);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
2000-02-11 15:11:03 +03:00
|
|
|
* This reverse searches this string for a given character
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2000-02-11 15:11:03 +03:00
|
|
|
* @update gess 2/04/00
|
|
|
|
* @param char is the character you're trying to find.
|
|
|
|
* @param aIgnorecase indicates case sensitivity of search
|
|
|
|
* @param anOffset tells us where to start the search; -1 means start at 0.
|
|
|
|
* @param aCount tell us how many chars to search from offset; -1 means use full length.
|
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
|
|
|
*/
|
2002-02-01 04:53:09 +03:00
|
|
|
PRInt32 nsCString::RFindChar(PRUnichar aChar,PRInt32 anOffset,PRInt32 aCount) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::RFindChar1(*this,aChar,anOffset,aCount);
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Reverse search for char in this string that is also a member of given charset
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aCStringSet
|
|
|
|
* @param anOffset
|
|
|
|
* @return offset of found char, or -1 (kNotFound)
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 nsCString::RFindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{
|
|
|
|
NS_ASSERTION(0!=aCStringSet,kNullPointerError);
|
|
|
|
|
|
|
|
PRInt32 result=kNotFound;
|
|
|
|
if(aCStringSet) {
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eOneByte);
|
2002-02-19 19:01:45 +03:00
|
|
|
temp.mLength=strlen(aCStringSet);
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mStr=(char*)aCStringSet;
|
2002-02-15 02:23:55 +03:00
|
|
|
result=nsStrPrivate::RFindCharInSet1(*this,temp,PR_FALSE,anOffset);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Reverse search for char in this string that is also a member of given charset
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aStringSet
|
|
|
|
* @param anOffset
|
|
|
|
* @return offset of found char, or -1 (kNotFound)
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
PRInt32 nsCString::RFindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{
|
|
|
|
NS_ASSERTION(0!=aStringSet,kNullPointerError);
|
|
|
|
|
|
|
|
PRInt32 result=kNotFound;
|
|
|
|
if(aStringSet) {
|
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
1999-07-17 11:26:16 +04:00
|
|
|
temp.mLength=nsCRT::strlen(aStringSet);
|
|
|
|
temp.mUStr=(PRUnichar*)aStringSet;
|
2002-02-15 02:23:55 +03:00
|
|
|
result=nsStrPrivate::RFindCharInSet2(*this,temp,anOffset);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Reverse search for char in this string that is also a member of given charset
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1999-08-30 10:55:13 +04:00
|
|
|
* @param aCStringSet
|
|
|
|
* @param anOffset
|
|
|
|
* @return offset of found char, or -1 (kNotFound)
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2002-01-24 22:14:18 +03:00
|
|
|
PRInt32 nsCString::RFindCharInSet(const nsCString& aSet,PRInt32 anOffset) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::RFindCharInSet1(*this,aSet,PR_FALSE,anOffset);
|
2002-01-24 22:14:18 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 nsCString::RFindCharInSet(const nsString& aSet,PRInt32 anOffset) const{
|
2002-02-15 02:23:55 +03:00
|
|
|
PRInt32 result=nsStrPrivate::RFindCharInSet2(*this,aSet,anOffset);
|
1999-07-17 11:26:16 +04:00
|
|
|
return result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
|
|
|
|
/**************************************************************
|
|
|
|
COMPARISON METHODS...
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
2000-03-31 11:26:13 +04:00
|
|
|
* Compares given unistring to this string.
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
2000-03-31 11:26:13 +04:00
|
|
|
* @param aString pts to a uni-string
|
1999-07-17 11:26:16 +04:00
|
|
|
* @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
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-03-31 11:26:13 +04:00
|
|
|
PRInt32 nsCString::CompareWithConversion(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
|
|
|
|
NS_ASSERTION(0!=aString,kNullPointerError);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2000-03-31 11:26:13 +04:00
|
|
|
if(aString) {
|
1999-07-17 11:26:16 +04:00
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eTwoByte);
|
2000-03-31 11:26:13 +04:00
|
|
|
temp.mLength=nsCRT::strlen(aString);
|
|
|
|
temp.mUStr=(PRUnichar*)aString;
|
2002-02-15 02:23:55 +03:00
|
|
|
return nsStrPrivate::StrCompare1To2(*this,temp,aCount,aIgnoreCase);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return 0;
|
1999-07-09 09:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-03-31 11:26:13 +04:00
|
|
|
* Compares given cstring to this string.
|
1999-09-07 02:59:11 +04:00
|
|
|
* @update gess 01/04/99
|
2000-03-31 11:26:13 +04:00
|
|
|
* @param aCString points to a cstring
|
1999-07-17 11:26:16 +04:00
|
|
|
* @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-07-09 09:11:25 +04:00
|
|
|
*/
|
2000-03-31 11:26:13 +04:00
|
|
|
PRInt32 nsCString::CompareWithConversion(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount) const {
|
|
|
|
NS_ASSERTION(0!=aCString,kNullPointerError);
|
1999-07-17 11:26:16 +04:00
|
|
|
|
2000-03-31 11:26:13 +04:00
|
|
|
if(aCString) {
|
1999-07-17 11:26:16 +04:00
|
|
|
nsStr temp;
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(temp,eOneByte);
|
2002-02-19 19:01:45 +03:00
|
|
|
temp.mLength=strlen(aCString);
|
2000-03-31 11:26:13 +04:00
|
|
|
temp.mStr=(char*)aCString;
|
2002-02-15 02:23:55 +03:00
|
|
|
return nsStrPrivate::StrCompare1To1(*this,temp,aCount,aIgnoreCase);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
return 0;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
PRBool nsCString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const {
|
2000-03-31 11:26:13 +04:00
|
|
|
return EqualsWithConversion(aString,PR_TRUE,aLength);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
PRBool nsCString::EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aLength) const {
|
2000-03-31 11:26:13 +04:00
|
|
|
return EqualsWithConversion(aString,PR_TRUE,aLength);
|
1999-07-17 11:26:16 +04:00
|
|
|
}
|
1999-07-09 09:11:25 +04:00
|
|
|
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
|
|
|
* Compare this to given string; note that we compare full strings here.
|
1999-07-17 11:26:16 +04:00
|
|
|
*
|
|
|
|
* @param aString is the CString to be compared
|
|
|
|
* @param aCount tells us how many chars you want to compare starting with start of string
|
|
|
|
* @param aIgnorecase tells us whether to be case sensitive
|
|
|
|
* @param aCount tells us how many chars to test; -1 implies full length
|
1998-04-14 00:24:54 +04:00
|
|
|
* @return TRUE if equal
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2000-03-31 11:26:13 +04:00
|
|
|
PRBool nsCString::EqualsWithConversion(const char* aCString,PRBool aIgnoreCase,PRInt32 aCount) const{
|
|
|
|
PRInt32 theAnswer=CompareWithConversion(aCString,aIgnoreCase,aCount);
|
1999-07-17 11:26:16 +04:00
|
|
|
PRBool result=PRBool(0==theAnswer);
|
|
|
|
return result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2000-03-31 11:26:13 +04:00
|
|
|
PRBool nsCString::EqualsWithConversion(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const {
|
|
|
|
PRInt32 theAnswer=CompareWithConversion(aString,aIgnoreCase,aCount);
|
1999-07-17 11:26:16 +04:00
|
|
|
PRBool result=PRBool(0==theAnswer);
|
|
|
|
return result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2000-03-31 11:55:08 +04:00
|
|
|
PRBool nsCString::EqualsWithConversion(const nsString& aString, PRBool aIgnoreCase,PRInt32 aCount) const {
|
2001-06-30 15:02:25 +04:00
|
|
|
PRInt32 theAnswer=CompareWithConversion(aString.get(),aIgnoreCase,aCount);
|
2000-03-31 11:55:08 +04:00
|
|
|
PRBool result=PRBool(0==theAnswer);
|
|
|
|
return result;
|
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
|
2000-05-27 02:24:34 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
NS_ConvertUCS2toUTF8::NS_ConvertUCS2toUTF8( const nsAString& aString )
|
2000-08-23 21:27:06 +04:00
|
|
|
{
|
2001-09-25 13:35:50 +04:00
|
|
|
nsAString::const_iterator start; aString.BeginReading(start);
|
|
|
|
nsAString::const_iterator end; aString.EndReading(end);
|
2000-08-23 21:27:06 +04:00
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
nsReadableFragment<PRUnichar> frag(start.fragment());
|
|
|
|
Append(frag.mStart, frag.mEnd - frag.mStart);
|
2001-04-02 23:40:52 +04:00
|
|
|
start.advance(start.size_forward());
|
2000-08-23 21:27:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-27 04:17:10 +04:00
|
|
|
void
|
2000-08-23 21:27:06 +04:00
|
|
|
NS_ConvertUCS2toUTF8::Append( const PRUnichar* aString, PRUint32 aLength )
|
2000-05-27 02:24:34 +04:00
|
|
|
{
|
2000-06-17 05:24:39 +04:00
|
|
|
// Handle null string by just leaving us as a brand-new
|
|
|
|
// uninitialized nsCAutoString.
|
|
|
|
if (! aString)
|
|
|
|
return;
|
|
|
|
|
2001-09-29 12:28:41 +04:00
|
|
|
// Calculate how many bytes we need
|
2000-05-27 02:24:34 +04:00
|
|
|
const PRUnichar* p;
|
|
|
|
PRInt32 count, utf8len;
|
|
|
|
for (p = aString, utf8len = 0, count = aLength; 0 != count && 0 != (*p); count--, p++)
|
|
|
|
{
|
|
|
|
if (! ((*p) & 0xFF80))
|
|
|
|
utf8len += 1; // 0000 0000 - 0000 007F
|
|
|
|
else if (! ((*p) & 0xF800))
|
|
|
|
utf8len += 2; // 0000 0080 - 0000 07FF
|
|
|
|
else
|
|
|
|
utf8len += 3; // 0000 0800 - 0000 FFFF
|
|
|
|
// Note: Surrogate pair needs 4 bytes, but in this calcuation
|
|
|
|
// we count it as 6 bytes. It will waste 2 bytes per surrogate pair
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure our buffer's big enough, so we don't need to do
|
|
|
|
// multiple allocations.
|
2000-08-23 21:27:06 +04:00
|
|
|
if(mLength+PRUint32(utf8len+1) > sizeof(mBuffer))
|
|
|
|
SetCapacity(mLength+utf8len+1);
|
2000-08-09 12:26:23 +04:00
|
|
|
// |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAWritableString.h),
|
|
|
|
// we can only use it since our local implementation, |nsCString::SetCapacity|, is known to do what we want
|
2000-05-27 02:24:34 +04:00
|
|
|
|
2000-08-23 21:27:06 +04:00
|
|
|
char* out = mStr+mLength;
|
2000-05-27 02:24:34 +04:00
|
|
|
PRUint32 ucs4=0;
|
|
|
|
|
2000-06-09 03:07:15 +04:00
|
|
|
for (p = aString, count = aLength; 0 != count && 0 != (*p); count--, p++)
|
2000-05-27 02:24:34 +04:00
|
|
|
{
|
|
|
|
if (0 == ucs4)
|
|
|
|
{
|
|
|
|
if (! ((*p) & 0xFF80))
|
|
|
|
{
|
|
|
|
*out++ = (char)*p;
|
|
|
|
}
|
|
|
|
else if (! ((*p) & 0xF800))
|
|
|
|
{
|
|
|
|
*out++ = 0xC0 | (char)((*p) >> 6);
|
|
|
|
*out++ = 0x80 | (char)(0x003F & (*p));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (0xD800 == (0xFC00 & (*p)))
|
|
|
|
{
|
|
|
|
// D800- DBFF - High Surrogate
|
|
|
|
// N = (H- D800) *400 + 10000 + ...
|
|
|
|
ucs4 = 0x10000 | ((0x03FF & (*p)) << 10);
|
|
|
|
}
|
|
|
|
else if (0xDC00 == (0xFC00 & (*p)))
|
|
|
|
{
|
|
|
|
// DC00- DFFF - Low Surrogate
|
|
|
|
// error here. We should hit High Surrogate first
|
|
|
|
// Do not output any thing in this case
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*out++ = 0xE0 | (char)((*p) >> 12);
|
|
|
|
*out++ = 0x80 | (char)(0x003F & (*p >> 6));
|
|
|
|
*out++ = 0x80 | (char)(0x003F & (*p) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (0xDC00 == (0xFC00 & (*p)))
|
|
|
|
{
|
|
|
|
// DC00- DFFF - Low Surrogate
|
|
|
|
// N += ( L - DC00 )
|
|
|
|
ucs4 |= (0x03FF & (*p));
|
|
|
|
|
|
|
|
// 0001 0000-001F FFFF
|
|
|
|
*out++ = 0xF0 | (char)(ucs4 >> 18);
|
|
|
|
*out++ = 0x80 | (char)(0x003F & (ucs4 >> 12));
|
|
|
|
*out++ = 0x80 | (char)(0x003F & (ucs4 >> 6));
|
|
|
|
*out++ = 0x80 | (char)(0x003F & ucs4) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Got a High Surrogate but no low surrogate
|
|
|
|
// output nothing.
|
|
|
|
}
|
|
|
|
ucs4 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = '\0'; // null terminate
|
2000-08-23 21:27:06 +04:00
|
|
|
mLength += utf8len;
|
2000-05-27 02:24:34 +04:00
|
|
|
}
|
|
|
|
|
2001-09-29 12:28:41 +04:00
|
|
|
NS_LossyConvertUCS2toASCII::NS_LossyConvertUCS2toASCII( const nsAString& aString )
|
|
|
|
{
|
|
|
|
SetCapacity(aString.Length());
|
|
|
|
|
|
|
|
nsAString::const_iterator start; aString.BeginReading(start);
|
|
|
|
nsAString::const_iterator end; aString.EndReading(end);
|
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
nsReadableFragment<PRUnichar> frag(start.fragment());
|
|
|
|
AppendWithConversion(frag.mStart, frag.mEnd - frag.mStart);
|
|
|
|
start.advance(start.size_forward());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
IMPLEMENTATION NOTES: AUTOSTRING...
|
|
|
|
***********************************************************************/
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Default constructor
|
|
|
|
*
|
1999-07-09 10:01:55 +04:00
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
nsCAutoString::nsCAutoString() : nsCString(){
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE);
|
1999-07-17 11:26:16 +04:00
|
|
|
AddNullTerminator(*this);
|
1999-08-30 10:55:13 +04:00
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1999-07-09 09:11:25 +04:00
|
|
|
|
2000-04-28 04:21:44 +04:00
|
|
|
nsCAutoString::nsCAutoString( const nsCString& aString ) : nsCString(){
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE);
|
1999-07-17 11:26:16 +04:00
|
|
|
AddNullTerminator(*this);
|
|
|
|
Append(aString);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
nsCAutoString::nsCAutoString( const nsACString& aString ) : nsCString(){
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE);
|
2000-08-23 21:27:06 +04:00
|
|
|
AddNullTerminator(*this);
|
|
|
|
Append(aString);
|
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
|
2000-05-15 07:10:19 +04:00
|
|
|
nsCAutoString::nsCAutoString(const char* aCString) : nsCString() {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE);
|
2000-05-15 07:10:19 +04:00
|
|
|
AddNullTerminator(*this);
|
|
|
|
Append(aCString);
|
|
|
|
}
|
|
|
|
|
1999-07-09 10:01:55 +04:00
|
|
|
/**
|
1999-07-17 11:26:16 +04:00
|
|
|
* Copy construct from ascii c-string
|
|
|
|
* @param aCString is a ptr to a 1-byte cstr
|
1999-07-09 10:01:55 +04:00
|
|
|
*/
|
1999-07-17 11:26:16 +04:00
|
|
|
nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE);
|
1999-07-17 11:26:16 +04:00
|
|
|
AddNullTerminator(*this);
|
|
|
|
Append(aCString,aLength);
|
1998-09-06 03:21:42 +04:00
|
|
|
}
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* Copy construct using an external buffer descriptor
|
|
|
|
* @param aBuffer -- descibes external buffer
|
1999-07-17 11:26:16 +04:00
|
|
|
*/
|
1999-08-24 04:52:32 +04:00
|
|
|
nsCAutoString::nsCAutoString(const CBufDescriptor& aBuffer) : nsCString() {
|
1999-07-17 11:26:16 +04:00
|
|
|
if(!aBuffer.mBuffer) {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE);
|
1999-07-17 11:26:16 +04:00
|
|
|
}
|
|
|
|
else {
|
2002-02-15 02:23:55 +03:00
|
|
|
nsStrPrivate::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased);
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1999-09-19 20:43:09 +04:00
|
|
|
if(!aBuffer.mIsConst)
|
|
|
|
AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants.
|
1999-07-09 10:01:55 +04:00
|
|
|
}
|
1998-08-04 00:46:38 +04:00
|
|
|
|
|
|
|
|
1999-07-17 11:26:16 +04:00
|
|
|
/**
|
1999-08-30 10:55:13 +04:00
|
|
|
* deconstructor
|
1999-07-17 11:26:16 +04:00
|
|
|
* @param
|
|
|
|
*/
|
|
|
|
nsCAutoString::~nsCAutoString(){
|
|
|
|
}
|
1999-07-09 10:01:55 +04:00
|
|
|
|
2001-10-16 09:31:36 +04:00
|
|
|
#ifdef DEBUG
|
1999-08-31 07:04:37 +04:00
|
|
|
void nsCAutoString::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const {
|
|
|
|
if (aResult) {
|
2002-01-25 02:46:56 +03:00
|
|
|
*aResult = sizeof(*this) + GetCapacity() * GetCharSize();
|
1999-08-31 07:04:37 +04:00
|
|
|
}
|
1999-07-17 11:26:16 +04:00
|
|
|
}
|
2001-10-16 09:31:36 +04:00
|
|
|
#endif
|