1999-03-22 09:54:46 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
1999-11-06 03:43:54 +00: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/
|
1999-03-22 09:54:46 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00: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.
|
1999-03-22 09:54:46 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-03-22 09:54:46 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:43:54 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-03-22 09:54:46 +00:00
|
|
|
*/
|
1999-06-03 18:45:39 +00:00
|
|
|
|
1999-03-22 09:54:46 +00:00
|
|
|
/******************************************************************************************
|
|
|
|
MODULE NOTES:
|
|
|
|
|
|
|
|
This file contains the nsStr data structure.
|
|
|
|
This general purpose buffer management class is used as the basis for our strings.
|
|
|
|
It's benefits include:
|
|
|
|
1. An efficient set of library style functions for manipulating nsStrs
|
|
|
|
2. Support for 1 and 2 byte character strings (which can easily be increased to n)
|
|
|
|
3. Unicode awareness and interoperability.
|
|
|
|
|
|
|
|
*******************************************************************************************/
|
|
|
|
|
|
|
|
#include "nsStr.h"
|
|
|
|
#include "bufferRoutines.h"
|
1999-12-01 06:12:01 +00:00
|
|
|
#include <stdio.h> //only used for printf
|
1999-07-09 06:01:55 +00:00
|
|
|
#include "nsCRT.h"
|
1999-07-17 07:26:16 +00:00
|
|
|
#include "nsDeque.h"
|
1999-07-19 03:10:41 +00:00
|
|
|
|
1999-07-20 06:58:41 +00:00
|
|
|
//static const char* kCallFindChar = "For better performance, call FindChar() for targets whose length==1.";
|
|
|
|
//static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1.";
|
1999-04-25 01:24:10 +00:00
|
|
|
|
1999-11-19 08:10:40 +00:00
|
|
|
static const PRUnichar gCommonEmptyBuffer[1] = {0};
|
|
|
|
static PRBool gStringAcquiredMemory = PR_TRUE;
|
1999-03-22 09:54:46 +00:00
|
|
|
|
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This method initializes all the members of the nsStr structure
|
|
|
|
*
|
1999-03-22 09:54:46 +00:00
|
|
|
* @update gess10/30/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) {
|
1999-09-08 20:21:27 +00:00
|
|
|
aDest.mStr=(char*)gCommonEmptyBuffer;
|
1999-03-22 09:54:46 +00:00
|
|
|
aDest.mLength=0;
|
|
|
|
aDest.mCapacity=0;
|
1999-05-06 05:13:08 +00:00
|
|
|
aDest.mCharSize=aCharSize;
|
1999-03-22 09:54:46 +00:00
|
|
|
aDest.mOwnsBuffer=0;
|
|
|
|
}
|
|
|
|
|
1999-04-25 01:24:10 +00:00
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This method initializes all the members of the nsStr structure
|
1999-04-25 01:24:10 +00:00
|
|
|
* @update gess10/30/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
1999-05-06 05:13:08 +00:00
|
|
|
void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){
|
1999-09-08 20:21:27 +00:00
|
|
|
aDest.mStr=(aCString) ? aCString : (char*)gCommonEmptyBuffer;
|
1999-05-06 05:13:08 +00:00
|
|
|
aDest.mLength=aLength;
|
|
|
|
aDest.mCapacity=aCapacity;
|
|
|
|
aDest.mCharSize=aCharSize;
|
|
|
|
aDest.mOwnsBuffer=aOwnsBuffer;
|
|
|
|
}
|
1999-04-25 03:10:44 +00:00
|
|
|
|
1999-03-22 09:54:46 +00:00
|
|
|
|
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This member destroys the memory buffer owned by an nsStr object (if it actually owns it)
|
1999-03-22 09:54:46 +00:00
|
|
|
* @update gess10/30/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
void nsStr::Destroy(nsStr& aDest) {
|
1999-09-08 20:21:27 +00:00
|
|
|
if((aDest.mStr) && (aDest.mStr!=(char*)gCommonEmptyBuffer)) {
|
1999-10-05 04:47:19 +00:00
|
|
|
Free(aDest);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method gets called when the internal buffer needs
|
|
|
|
* to grow to a given size. The original contents are not preserved.
|
|
|
|
* @update gess 3/30/98
|
|
|
|
* @param aNewLength -- new capacity of string in charSize units
|
|
|
|
* @return void
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength) {
|
1999-09-30 04:03:49 +00:00
|
|
|
PRBool result=PR_TRUE;
|
1999-03-22 09:54:46 +00:00
|
|
|
if(aNewLength>aString.mCapacity) {
|
1999-10-05 04:47:19 +00:00
|
|
|
result=Realloc(aString,aNewLength);
|
1999-09-30 04:03:49 +00:00
|
|
|
if(aString.mStr)
|
|
|
|
AddNullTerminator(aString);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
1999-09-30 04:03:49 +00:00
|
|
|
return result;
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method gets called when the internal buffer needs
|
|
|
|
* to grow to a given size. The original contents ARE preserved.
|
|
|
|
* @update gess 3/30/98
|
|
|
|
* @param aNewLength -- new capacity of string in charSize units
|
|
|
|
* @return void
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength) {
|
1999-09-30 04:03:49 +00:00
|
|
|
PRBool result=PR_TRUE;
|
1999-03-22 09:54:46 +00:00
|
|
|
if(aNewLength>aDest.mCapacity) {
|
|
|
|
nsStr theTempStr;
|
1999-06-03 10:49:14 +00:00
|
|
|
nsStr::Initialize(theTempStr,aDest.mCharSize);
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-10-05 04:47:19 +00:00
|
|
|
result=EnsureCapacity(theTempStr,aNewLength);
|
1999-09-30 04:03:49 +00:00
|
|
|
if(result) {
|
|
|
|
if(aDest.mLength) {
|
1999-10-05 04:47:19 +00:00
|
|
|
Append(theTempStr,aDest,0,aDest.mLength);
|
1999-09-30 04:03:49 +00:00
|
|
|
}
|
1999-10-05 04:47:19 +00:00
|
|
|
Free(aDest);
|
1999-09-30 04:03:49 +00:00
|
|
|
aDest.mStr = theTempStr.mStr;
|
|
|
|
theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole...
|
|
|
|
aDest.mLength=theTempStr.mLength;
|
|
|
|
aDest.mCapacity=theTempStr.mCapacity;
|
|
|
|
aDest.mOwnsBuffer=theTempStr.mOwnsBuffer;
|
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
1999-09-30 04:03:49 +00:00
|
|
|
return result;
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces the contents of aDest with aSource, up to aCount of chars.
|
|
|
|
* @update gess10/30/98
|
|
|
|
* @param aDest is the nsStr that gets changed.
|
|
|
|
* @param aSource is where chars are copied from
|
|
|
|
* @param aCount is the number of chars copied from aSource
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount){
|
1999-06-03 10:49:14 +00:00
|
|
|
if(&aDest!=&aSource){
|
1999-10-05 04:47:19 +00:00
|
|
|
Truncate(aDest,0);
|
|
|
|
Append(aDest,aSource,anOffset,aCount);
|
1999-06-03 10:49:14 +00:00
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method appends the given nsStr to this one. Note that we have to
|
|
|
|
* pay attention to the underlying char-size of both structs.
|
1999-05-06 05:13:08 +00:00
|
|
|
* @update gess10/30/98
|
1999-03-22 09:54:46 +00:00
|
|
|
* @param aDest is the nsStr to be manipulated
|
|
|
|
* @param aSource is where char are copied from
|
|
|
|
* @aCount is the number of bytes to be copied
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount){
|
1999-04-05 21:38:50 +00:00
|
|
|
if(anOffset<aSource.mLength){
|
1999-03-23 08:47:54 +00:00
|
|
|
PRUint32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength);
|
|
|
|
PRUint32 theLength=(anOffset+theRealLen<aSource.mLength) ? theRealLen : (aSource.mLength-anOffset);
|
|
|
|
if(0<theLength){
|
1999-09-30 04:03:49 +00:00
|
|
|
|
|
|
|
PRBool isBigEnough=PR_TRUE;
|
1999-03-23 08:47:54 +00:00
|
|
|
if(aDest.mLength+theLength > aDest.mCapacity) {
|
1999-10-05 04:47:19 +00:00
|
|
|
isBigEnough=GrowCapacity(aDest,aDest.mLength+theLength);
|
1999-03-23 08:47:54 +00:00
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-09-30 04:03:49 +00:00
|
|
|
if(isBigEnough) {
|
|
|
|
//now append new chars, starting at offset
|
|
|
|
(*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength);
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-09-30 04:03:49 +00:00
|
|
|
aDest.mLength+=theLength;
|
|
|
|
AddNullTerminator(aDest);
|
1999-11-20 07:28:45 +00:00
|
|
|
NSSTR_SEEN(aDest);
|
1999-09-30 04:03:49 +00:00
|
|
|
}
|
1999-03-23 08:47:54 +00:00
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method inserts up to "aCount" chars from a source nsStr into a dest nsStr.
|
|
|
|
* @update gess10/30/98
|
|
|
|
* @param aDest is the nsStr that gets changed
|
|
|
|
* @param aDestOffset is where in aDest the insertion is to occur
|
|
|
|
* @param aSource is where chars are copied from
|
|
|
|
* @param aSrcOffset is where in aSource chars are copied from
|
|
|
|
* @param aCount is the number of chars from aSource to be inserted into aDest
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
void nsStr::Insert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){
|
1999-03-22 09:54:46 +00:00
|
|
|
//there are a few cases for insert:
|
|
|
|
// 1. You're inserting chars into an empty string (assign)
|
|
|
|
// 2. You're inserting onto the end of a string (append)
|
|
|
|
// 3. You're inserting onto the 1..n-1 pos of a string (the hard case).
|
1999-05-06 05:13:08 +00:00
|
|
|
if(0<aSource.mLength){
|
1999-03-22 09:54:46 +00:00
|
|
|
if(aDest.mLength){
|
|
|
|
if(aDestOffset<aDest.mLength){
|
1999-05-06 05:13:08 +00:00
|
|
|
PRInt32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength);
|
|
|
|
PRInt32 theLength=(aSrcOffset+theRealLen<aSource.mLength) ? theRealLen : (aSource.mLength-aSrcOffset);
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-05-06 05:13:08 +00:00
|
|
|
if(aSrcOffset<aSource.mLength) {
|
|
|
|
//here's the only new case we have to handle.
|
|
|
|
//chars are really being inserted into our buffer...
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-09-17 23:46:55 +00:00
|
|
|
if(aDest.mLength+theLength > aDest.mCapacity) {
|
|
|
|
nsStr theTempStr;
|
|
|
|
nsStr::Initialize(theTempStr,aDest.mCharSize);
|
|
|
|
|
1999-10-05 04:47:19 +00:00
|
|
|
PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength); //grow the temp buffer to the right size
|
1999-09-17 23:46:55 +00:00
|
|
|
|
1999-09-30 04:03:49 +00:00
|
|
|
if(isBigEnough) {
|
|
|
|
if(aDestOffset) {
|
1999-10-05 04:47:19 +00:00
|
|
|
Append(theTempStr,aDest,0,aDestOffset); //first copy leftmost data...
|
1999-09-30 04:03:49 +00:00
|
|
|
}
|
1999-09-17 23:46:55 +00:00
|
|
|
|
1999-10-05 04:47:19 +00:00
|
|
|
Append(theTempStr,aSource,0,aSource.mLength); //next copy inserted (new) data
|
1999-09-17 23:46:55 +00:00
|
|
|
|
1999-09-30 04:03:49 +00:00
|
|
|
PRUint32 theRemains=aDest.mLength-aDestOffset;
|
|
|
|
if(theRemains) {
|
1999-10-05 04:47:19 +00:00
|
|
|
Append(theTempStr,aDest,aDestOffset,theRemains); //next copy rightmost data
|
1999-09-30 04:03:49 +00:00
|
|
|
}
|
|
|
|
|
1999-10-05 04:47:19 +00:00
|
|
|
Free(aDest);
|
1999-09-30 04:03:49 +00:00
|
|
|
aDest.mStr = theTempStr.mStr;
|
|
|
|
theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole...
|
|
|
|
aDest.mCapacity=theTempStr.mCapacity;
|
|
|
|
aDest.mOwnsBuffer=theTempStr.mOwnsBuffer;
|
1999-09-17 23:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
//shift the chars right by theDelta...
|
|
|
|
(*gShiftChars[aDest.mCharSize][KSHIFTRIGHT])(aDest.mStr,aDest.mLength,aDestOffset,theLength);
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-09-17 23:46:55 +00:00
|
|
|
//now insert new chars, starting at offset
|
|
|
|
(*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength);
|
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-09-17 23:46:55 +00:00
|
|
|
//finally, make sure to update the string length...
|
1999-05-06 05:13:08 +00:00
|
|
|
aDest.mLength+=theLength;
|
|
|
|
AddNullTerminator(aDest);
|
1999-11-20 07:28:45 +00:00
|
|
|
NSSTR_SEEN(aDest);
|
1999-05-06 05:13:08 +00:00
|
|
|
}//if
|
|
|
|
//else nothing to do!
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
1999-10-05 04:47:19 +00:00
|
|
|
else Append(aDest,aSource,0,aCount);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
1999-10-05 04:47:19 +00:00
|
|
|
else Append(aDest,aSource,0,aCount);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method deletes up to aCount chars from aDest
|
|
|
|
* @update gess10/30/98
|
|
|
|
* @param aDest is the nsStr to be manipulated
|
|
|
|
* @param aDestOffset is where in aDest deletion is to occur
|
|
|
|
* @param aCount is the number of chars to be deleted in aDest
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){
|
1999-05-06 05:13:08 +00:00
|
|
|
if(aDestOffset<aDest.mLength){
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-05-06 05:13:08 +00:00
|
|
|
PRUint32 theDelta=aDest.mLength-aDestOffset;
|
|
|
|
PRUint32 theLength=(theDelta<aCount) ? theDelta : aCount;
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-05-06 05:13:08 +00:00
|
|
|
if(aDestOffset+theLength<aDest.mLength) {
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-05-06 05:13:08 +00:00
|
|
|
//if you're here, it means we're cutting chars out of the middle of the string...
|
|
|
|
//so shift the chars left by theLength...
|
1999-06-03 10:49:14 +00:00
|
|
|
(*gShiftChars[aDest.mCharSize][KSHIFTLEFT])(aDest.mStr,aDest.mLength,aDestOffset,theLength);
|
1999-05-06 05:13:08 +00:00
|
|
|
aDest.mLength-=theLength;
|
1999-09-20 06:11:36 +00:00
|
|
|
AddNullTerminator(aDest);
|
1999-11-20 07:28:45 +00:00
|
|
|
NSSTR_SEEN(aDest);
|
1999-05-06 05:13:08 +00:00
|
|
|
}
|
1999-10-05 04:47:19 +00:00
|
|
|
else Truncate(aDest,aDestOffset);
|
1999-03-22 09:54:46 +00:00
|
|
|
}//if
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method truncates the given nsStr at given offset
|
|
|
|
* @update gess10/30/98
|
|
|
|
* @param aDest is the nsStr to be truncated
|
|
|
|
* @param aDestOffset is where in aDest truncation is to occur
|
|
|
|
*/
|
1999-10-05 04:47:19 +00:00
|
|
|
void nsStr::Truncate(nsStr& aDest,PRUint32 aDestOffset){
|
1999-03-22 09:54:46 +00:00
|
|
|
if(aDestOffset<aDest.mLength){
|
|
|
|
aDest.mLength=aDestOffset;
|
|
|
|
AddNullTerminator(aDest);
|
1999-11-20 07:28:45 +00:00
|
|
|
NSSTR_SEEN(aDest);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This method forces the given string to upper or lowercase
|
1999-03-22 09:54:46 +00:00
|
|
|
* @update gess1/7/99
|
1999-07-17 07:26:16 +00:00
|
|
|
* @param aDest is the string you're going to change
|
|
|
|
* @param aToUpper: if TRUE, then we go uppercase, otherwise we go lowercase
|
1999-03-22 09:54:46 +00:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) {
|
|
|
|
// somehow UnicharUtil return failed, fallback to the old ascii only code
|
1999-05-06 05:13:08 +00:00
|
|
|
gCaseConverters[aDest.mCharSize](aDest.mStr,aDest.mLength,aToUpper);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
1999-11-14 06:22:52 +00:00
|
|
|
* This method removes characters from the given set from this string.
|
|
|
|
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
|
|
|
|
*
|
|
|
|
* @update gess 11/7/99
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
|
|
|
* @param aEliminateLeading
|
|
|
|
* @param aEliminateTrailing
|
|
|
|
* @return nothing
|
1999-03-22 09:54:46 +00:00
|
|
|
*/
|
1999-07-17 07:26:16 +00:00
|
|
|
void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-07-17 07:26:16 +00:00
|
|
|
if((aDest.mLength>0) && aSet){
|
|
|
|
PRInt32 theIndex=-1;
|
|
|
|
PRInt32 theMax=aDest.mLength;
|
|
|
|
PRInt32 theSetLen=nsCRT::strlen(aSet);
|
|
|
|
|
|
|
|
if(aEliminateLeading) {
|
|
|
|
while(++theIndex<=theMax) {
|
|
|
|
PRUnichar theChar=GetCharAt(aDest,theIndex);
|
2000-02-11 12:11:03 +00:00
|
|
|
PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE,theSetLen);
|
1999-07-17 07:26:16 +00:00
|
|
|
if(kNotFound==thePos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(0<theIndex) {
|
|
|
|
if(theIndex<theMax) {
|
1999-10-05 04:47:19 +00:00
|
|
|
Delete(aDest,0,theIndex);
|
1999-07-17 07:26:16 +00:00
|
|
|
}
|
|
|
|
else Truncate(aDest,0);
|
|
|
|
}
|
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-07-17 07:26:16 +00:00
|
|
|
if(aEliminateTrailing) {
|
|
|
|
theIndex=aDest.mLength;
|
|
|
|
PRInt32 theNewLen=theIndex;
|
|
|
|
while(--theIndex>0) {
|
|
|
|
PRUnichar theChar=GetCharAt(aDest,theIndex); //read at end now...
|
2000-02-11 12:11:03 +00:00
|
|
|
PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE,theSetLen);
|
1999-07-17 07:26:16 +00:00
|
|
|
if(kNotFound<thePos)
|
|
|
|
theNewLen=theIndex;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
if(theNewLen<theMax) {
|
|
|
|
Truncate(aDest,theNewLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @update gess1/7/99
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
1999-07-17 07:26:16 +00:00
|
|
|
void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
|
|
|
|
Trim(aDest,aSet,aEliminateLeading,aEliminateTrailing);
|
|
|
|
PRUint32 aNewLen=gCompressChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet);
|
1999-03-22 09:54:46 +00:00
|
|
|
aDest.mLength=aNewLen;
|
1999-11-20 07:28:45 +00:00
|
|
|
NSSTR_SEEN(aDest);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 04:03:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @update gess1/7/99
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
void nsStr::StripChars(nsStr& aDest,const char* aSet){
|
|
|
|
if((0<aDest.mLength) && (aSet)) {
|
|
|
|
PRUint32 aNewLen=gStripChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet);
|
|
|
|
aDest.mLength=aNewLen;
|
1999-11-20 07:28:45 +00:00
|
|
|
NSSTR_SEEN(aDest);
|
1999-09-30 04:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-22 09:54:46 +00:00
|
|
|
/**************************************************************
|
|
|
|
Searching methods...
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
|
1999-07-17 07:26:16 +00:00
|
|
|
/**
|
|
|
|
* This searches aDest for a given substring
|
|
|
|
*
|
2000-02-11 12:11:03 +00:00
|
|
|
* @update gess 2/04/00: added aCount argument to restrict search
|
1999-07-17 07:26:16 +00:00
|
|
|
* @param aDest string to search
|
|
|
|
* @param aTarget is the substring you're trying to find.
|
|
|
|
* @param aIgnorecase indicates case sensitivity of search
|
|
|
|
* @param anOffset tells us where to start the search
|
2000-02-11 12:11:03 +00:00
|
|
|
* @param aCount tells us how many iterations to make from offset; -1 means the full length of the string
|
1999-07-17 07:26:16 +00:00
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
|
|
|
*/
|
2000-02-11 12:11:03 +00:00
|
|
|
PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
|
|
|
PRInt32 theMaxPos = aDest.mLength-aTarget.mLength; //this is the last pos that is feasible for starting the search, with given lengths...
|
1999-07-09 06:01:55 +00:00
|
|
|
|
2000-02-11 12:11:03 +00:00
|
|
|
if(0<=theMaxPos) {
|
|
|
|
|
|
|
|
if(anOffset<0)
|
|
|
|
anOffset=0;
|
|
|
|
|
|
|
|
if((0<aDest.mLength) && (anOffset<=theMaxPos) && (aTarget.mLength)) {
|
|
|
|
|
|
|
|
if(aCount<0)
|
|
|
|
aCount = MaxInt(theMaxPos,1);
|
|
|
|
|
|
|
|
if(0<aCount) {
|
|
|
|
|
|
|
|
PRInt32 aDelta= (aDest.mCharSize == eOneByte) ? 1 : 2;
|
|
|
|
const char* root = aDest.mStr;
|
|
|
|
const char* left = root+(anOffset*aDelta);
|
|
|
|
const char* last = left+((aCount-1)*aDelta);
|
|
|
|
const char* max = root+(theMaxPos*aDelta);
|
|
|
|
const char* right = (last<max) ? last : max;
|
|
|
|
|
|
|
|
while(left<=right){
|
|
|
|
PRInt32 cmp=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(left,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
|
|
|
if(0==cmp) {
|
|
|
|
return (left-root)/aDelta;
|
|
|
|
}
|
|
|
|
left+=aDelta;
|
|
|
|
} //while
|
|
|
|
|
|
|
|
} //if
|
|
|
|
}
|
|
|
|
} //if
|
|
|
|
|
|
|
|
return kNotFound;
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This searches aDest for a given character
|
|
|
|
*
|
2000-02-11 12:11:03 +00:00
|
|
|
* @update gess 2/04/00: added aCount argument to restrict search
|
1999-07-17 07:26:16 +00:00
|
|
|
* @param aDest string to search
|
|
|
|
* @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
|
2000-02-11 12:11:03 +00:00
|
|
|
* @param aCount tell us how many chars to search from offset
|
1999-07-17 07:26:16 +00:00
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
1999-03-22 09:54:46 +00:00
|
|
|
*/
|
2000-02-11 12:11:03 +00:00
|
|
|
PRInt32 nsStr::FindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
|
|
|
return gFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase,aCount);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This searches aDest for a character found in aSet.
|
1999-03-22 09:54:46 +00:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1999-07-17 07:26:16 +00:00
|
|
|
* @param aDest string to search
|
|
|
|
* @param aSet contains a list of chars to be searched for
|
|
|
|
* @param aIgnorecase indicates case sensitivity of search
|
|
|
|
* @param anOffset tells us where to start the search
|
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
1999-03-22 09:54:46 +00:00
|
|
|
*/
|
1999-07-17 07:26:16 +00:00
|
|
|
PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset) {
|
|
|
|
//NS_PRECONDITION(aSet.mLength!=1,kCallFindChar);
|
|
|
|
|
|
|
|
PRInt32 index=(0<=anOffset) ? anOffset-1 : -1;
|
|
|
|
PRInt32 thePos;
|
|
|
|
|
|
|
|
//Note that the search is inverted here. We're scanning aDest, one char at a time
|
|
|
|
//but doing the search against the given set. That's why we use 0 as the offset below.
|
|
|
|
if((0<aDest.mLength) && (0<aSet.mLength)){
|
|
|
|
while(++index<(PRInt32)aDest.mLength) {
|
|
|
|
PRUnichar theChar=GetCharAt(aDest,index);
|
2000-02-11 12:11:03 +00:00
|
|
|
thePos=gFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase,aSet.mLength);
|
1999-07-17 07:26:16 +00:00
|
|
|
if(kNotFound!=thePos)
|
|
|
|
return index;
|
|
|
|
} //while
|
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************
|
|
|
|
Reverse Searching methods...
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
|
1999-07-17 07:26:16 +00:00
|
|
|
/**
|
|
|
|
* This searches aDest (in reverse) for a given substring
|
|
|
|
*
|
2000-02-11 12:11:03 +00:00
|
|
|
* @update gess 2/04/00
|
1999-07-17 07:26:16 +00:00
|
|
|
* @param aDest string to search
|
|
|
|
* @param aTarget is the substring you're trying to find.
|
|
|
|
* @param aIgnorecase indicates case sensitivity of search
|
1999-07-19 03:10:41 +00:00
|
|
|
* @param anOffset tells us where to start the search (counting from left)
|
2000-02-11 12:11:03 +00:00
|
|
|
* @param aCount tell us how many iterations to perform from offset
|
1999-07-17 07:26:16 +00:00
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
|
|
|
*/
|
2000-02-11 12:11:03 +00:00
|
|
|
PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
|
|
|
if(anOffset<0)
|
|
|
|
anOffset=(PRInt32)aDest.mLength-1;
|
1999-07-17 07:26:16 +00:00
|
|
|
|
2000-02-11 12:11:03 +00:00
|
|
|
if(aCount<0)
|
|
|
|
aCount = aDest.mLength;
|
1999-06-03 10:49:14 +00:00
|
|
|
|
2000-02-11 12:11:03 +00:00
|
|
|
if((0<aDest.mLength) && ((PRUint32)anOffset<aDest.mLength) && (aTarget.mLength)) {
|
|
|
|
|
|
|
|
if(0<aCount) {
|
|
|
|
|
|
|
|
PRInt32 aDelta = (aDest.mCharSize == eOneByte) ? 1 : 2;
|
|
|
|
const char* root = aDest.mStr;
|
|
|
|
const char* destLast = root+((aDest.mLength-1)*aDelta); //pts to last char in aDest (likely null)
|
1999-03-22 09:54:46 +00:00
|
|
|
|
2000-02-11 12:11:03 +00:00
|
|
|
const char* rightmost = root+(anOffset*aDelta);
|
|
|
|
const char* min = rightmost-((aCount-1)*aDelta);
|
1999-07-17 07:26:16 +00:00
|
|
|
|
2000-02-11 12:11:03 +00:00
|
|
|
const char* leftmost = (min<root) ? root: min;
|
|
|
|
|
|
|
|
while(leftmost<=rightmost) {
|
|
|
|
if(aTarget.mLength<=PRUint32(destLast-rightmost)) {
|
|
|
|
PRInt32 result=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(rightmost,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
|
|
|
if(0==result) {
|
|
|
|
return (rightmost-root)/aDelta;
|
|
|
|
}
|
1999-07-17 07:26:16 +00:00
|
|
|
} //if
|
2000-02-11 12:11:03 +00:00
|
|
|
rightmost-=aDelta;
|
1999-07-17 07:26:16 +00:00
|
|
|
} //while
|
2000-02-11 12:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
1999-06-03 10:49:14 +00:00
|
|
|
|
1999-03-22 09:54:46 +00:00
|
|
|
|
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This searches aDest (in reverse) for a given character
|
|
|
|
*
|
2000-02-11 12:11:03 +00:00
|
|
|
* @update gess 2/04/00
|
1999-07-17 07:26:16 +00:00
|
|
|
* @param aDest string to search
|
|
|
|
* @param char is the character you're trying to find.
|
|
|
|
* @param aIgnorecase indicates case sensitivity of search
|
2000-02-11 12:11:03 +00:00
|
|
|
* @param anOffset tells us where to start the search; -1 means start at very end (mLength)
|
|
|
|
* @param aCount tell us how many iterations to perform from offset; -1 means use full length.
|
1999-07-17 07:26:16 +00:00
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
1999-03-22 09:54:46 +00:00
|
|
|
*/
|
2000-02-11 12:11:03 +00:00
|
|
|
PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
|
|
|
return gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase,aCount);
|
1999-03-22 09:54:46 +00:00
|
|
|
}
|
|
|
|
|
2000-02-11 12:11:03 +00:00
|
|
|
|
1999-03-22 09:54:46 +00:00
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* This searches aDest (in reverese) for a character found in aSet.
|
1999-03-22 09:54:46 +00:00
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1999-07-17 07:26:16 +00:00
|
|
|
* @param aDest string to search
|
|
|
|
* @param aSet contains a list of chars to be searched for
|
|
|
|
* @param aIgnorecase indicates case sensitivity of search
|
|
|
|
* @param anOffset tells us where to start the search
|
|
|
|
* @return index in aDest where member of aSet occurs, or -1 if not found
|
1999-03-22 09:54:46 +00:00
|
|
|
*/
|
1999-07-17 07:26:16 +00:00
|
|
|
PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset) {
|
|
|
|
//NS_PRECONDITION(aSet.mLength!=1,kCallRFindChar);
|
|
|
|
|
|
|
|
PRInt32 index=(0<=anOffset) ? anOffset : aDest.mLength;
|
|
|
|
PRInt32 thePos;
|
|
|
|
|
|
|
|
//note that the search is inverted here. We're scanning aDest, one char at a time
|
|
|
|
//but doing the search against the given set. That's why we use 0 as the offset below.
|
|
|
|
if(0<aDest.mLength) {
|
|
|
|
while(--index>=0) {
|
|
|
|
PRUnichar theChar=GetCharAt(aDest,index);
|
2000-02-11 12:11:03 +00:00
|
|
|
thePos=gFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase,aSet.mLength);
|
1999-07-17 07:26:16 +00:00
|
|
|
if(kNotFound!=thePos)
|
|
|
|
return index;
|
|
|
|
} //while
|
|
|
|
}
|
1999-03-22 09:54:46 +00:00
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
1999-07-17 07:26:16 +00:00
|
|
|
* Compare source and dest strings, up to an (optional max) number of chars
|
|
|
|
* @param aDest is the first str to compare
|
|
|
|
* @param aSource is the second str to compare
|
|
|
|
* @param aCount -- if (-1), then we use length of longer string; if (0<aCount) then it gives the max # of chars to compare
|
|
|
|
* @param aIgnorecase tells us whether to search with case sensitivity
|
1999-03-22 09:54:46 +00:00
|
|
|
* @return aDest<aSource=-1;aDest==aSource==0;aDest>aSource=1
|
|
|
|
*/
|
1999-05-06 05:13:08 +00:00
|
|
|
PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) {
|
1999-07-17 07:26:16 +00:00
|
|
|
PRInt32 result=0;
|
|
|
|
|
|
|
|
if(aCount) {
|
|
|
|
PRInt32 minlen=(aSource.mLength<aDest.mLength) ? aSource.mLength : aDest.mLength;
|
1999-07-09 05:11:25 +00:00
|
|
|
|
1999-07-17 07:26:16 +00:00
|
|
|
if(0==minlen) {
|
|
|
|
if ((aDest.mLength == 0) && (aSource.mLength == 0))
|
|
|
|
return 0;
|
|
|
|
if (aDest.mLength == 0)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-12-07 09:52:43 +00:00
|
|
|
PRInt32 theCount = (aCount<0) ? minlen: MinInt(aCount,minlen);
|
|
|
|
result=(*gCompare[aDest.mCharSize][aSource.mCharSize])(aDest.mStr,aSource.mStr,theCount,aIgnoreCase);
|
1999-12-02 10:14:38 +00:00
|
|
|
|
|
|
|
if (0==result) {
|
1999-12-07 09:52:43 +00:00
|
|
|
if(-1==aCount) {
|
|
|
|
|
|
|
|
//Since the caller didn't give us a length to test, and minlen characters matched,
|
|
|
|
//we have to assume that the longer string is greater.
|
|
|
|
|
|
|
|
if (aDest.mLength != aSource.mLength) {
|
|
|
|
//we think they match, but we've only compared minlen characters.
|
|
|
|
//if the string lengths are different, then they don't really match.
|
|
|
|
result = (aDest.mLength<aSource.mLength) ? -1 : 1;
|
|
|
|
}
|
|
|
|
} //if
|
1999-12-02 10:14:38 +00:00
|
|
|
}
|
|
|
|
|
1999-07-17 07:26:16 +00:00
|
|
|
}
|
1999-07-09 06:01:55 +00:00
|
|
|
return result;
|
1999-07-09 05:11:25 +00:00
|
|
|
}
|
|
|
|
|
1999-12-02 10:14:38 +00:00
|
|
|
/**
|
|
|
|
* Overwrites the contents of dest at offset with contents of aSource
|
|
|
|
*
|
|
|
|
* @param aDest is the first str to compare
|
|
|
|
* @param aSource is the second str to compare
|
|
|
|
* @param aDestOffset is the offset within aDest where source should be copied
|
|
|
|
* @return error code
|
|
|
|
*/
|
|
|
|
void nsStr::Overwrite(nsStr& aDest,const nsStr& aSource,PRInt32 aDestOffset) {
|
|
|
|
if(aDest.mLength && aSource.mLength) {
|
|
|
|
if((aDest.mLength-aDestOffset)>=aSource.mLength) {
|
|
|
|
//if you're here, then both dest and source have valid lengths
|
|
|
|
//and there's enough room in dest (at offset) to contain source.
|
|
|
|
(*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDestOffset,aSource.mStr,0,aSource.mLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-17 07:26:16 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
|
1999-10-05 04:47:19 +00:00
|
|
|
PRBool nsStr::Alloc(nsStr& aDest,PRUint32 aCount) {
|
|
|
|
|
|
|
|
static int mAllocCount=0;
|
|
|
|
mAllocCount++;
|
|
|
|
|
|
|
|
//we're given the acount value in charunits; now scale up to next multiple.
|
|
|
|
PRUint32 theNewCapacity=kDefaultStringSize;
|
|
|
|
while(theNewCapacity<aCount){
|
|
|
|
theNewCapacity<<=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDest.mCapacity=theNewCapacity++;
|
|
|
|
PRUint32 theSize=(theNewCapacity<<aDest.mCharSize);
|
|
|
|
aDest.mStr = (char*)nsAllocator::Alloc(theSize);
|
|
|
|
|
|
|
|
if(aDest.mStr) {
|
|
|
|
aDest.mOwnsBuffer=1;
|
1999-11-14 06:22:52 +00:00
|
|
|
gStringAcquiredMemory=PR_TRUE;
|
1999-10-05 04:47:19 +00:00
|
|
|
}
|
1999-11-14 06:22:52 +00:00
|
|
|
else gStringAcquiredMemory=PR_FALSE;
|
|
|
|
return gStringAcquiredMemory;
|
1999-10-05 04:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsStr::Free(nsStr& aDest){
|
|
|
|
if(aDest.mStr){
|
|
|
|
if(aDest.mOwnsBuffer){
|
|
|
|
nsAllocator::Free(aDest.mStr);
|
|
|
|
}
|
|
|
|
aDest.mStr=0;
|
|
|
|
aDest.mOwnsBuffer=0;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsStr::Realloc(nsStr& aDest,PRUint32 aCount){
|
|
|
|
|
|
|
|
nsStr temp;
|
|
|
|
memcpy(&temp,&aDest,sizeof(aDest));
|
|
|
|
|
|
|
|
PRBool result=Alloc(temp,aCount);
|
|
|
|
if(result) {
|
|
|
|
Free(aDest);
|
|
|
|
aDest.mStr=temp.mStr;
|
|
|
|
aDest.mCapacity=temp.mCapacity;
|
1999-10-07 07:22:57 +00:00
|
|
|
aDest.mOwnsBuffer=temp.mOwnsBuffer;
|
1999-10-05 04:47:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-11-14 06:22:52 +00:00
|
|
|
/**
|
|
|
|
* Retrieve last memory error
|
|
|
|
*
|
|
|
|
* @update gess 10/11/99
|
1999-11-19 08:10:40 +00:00
|
|
|
* @return memory error (usually returns PR_TRUE)
|
1999-11-14 06:22:52 +00:00
|
|
|
*/
|
|
|
|
PRBool nsStr::DidAcquireMemory(void) {
|
|
|
|
return gStringAcquiredMemory;
|
|
|
|
}
|
|
|
|
|
1999-10-05 04:47:19 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
|
1999-07-25 17:21:34 +00:00
|
|
|
CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) {
|
1999-07-17 07:26:16 +00:00
|
|
|
mBuffer=aString;
|
|
|
|
mCharSize=eOneByte;
|
|
|
|
mStackBased=aStackBased;
|
1999-09-19 16:43:09 +00:00
|
|
|
mIsConst=PR_FALSE;
|
1999-07-17 07:26:16 +00:00
|
|
|
mLength=mCapacity=0;
|
|
|
|
if(aString && aCapacity>1) {
|
|
|
|
mCapacity=aCapacity-1;
|
|
|
|
mLength=(-1==aLength) ? strlen(aString) : aLength;
|
|
|
|
if(mLength>PRInt32(mCapacity))
|
|
|
|
mLength=mCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-19 16:43:09 +00:00
|
|
|
CBufDescriptor::CBufDescriptor(const char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) {
|
|
|
|
mBuffer=(char*)aString;
|
|
|
|
mCharSize=eOneByte;
|
|
|
|
mStackBased=aStackBased;
|
|
|
|
mIsConst=PR_TRUE;
|
|
|
|
mLength=mCapacity=0;
|
|
|
|
if(aString && aCapacity>1) {
|
|
|
|
mCapacity=aCapacity-1;
|
|
|
|
mLength=(-1==aLength) ? strlen(aString) : aLength;
|
|
|
|
if(mLength>PRInt32(mCapacity))
|
|
|
|
mLength=mCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-25 17:21:34 +00:00
|
|
|
CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) {
|
1999-07-17 07:26:16 +00:00
|
|
|
mBuffer=(char*)aString;
|
|
|
|
mCharSize=eTwoByte;
|
|
|
|
mStackBased=aStackBased;
|
|
|
|
mLength=mCapacity=0;
|
1999-09-19 16:43:09 +00:00
|
|
|
mIsConst=PR_FALSE;
|
1999-07-17 07:26:16 +00:00
|
|
|
if(aString && aCapacity>1) {
|
|
|
|
mCapacity=aCapacity-1;
|
|
|
|
mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength;
|
|
|
|
if(mLength>PRInt32(mCapacity))
|
|
|
|
mLength=mCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-19 16:43:09 +00:00
|
|
|
CBufDescriptor::CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) {
|
|
|
|
mBuffer=(char*)aString;
|
|
|
|
mCharSize=eTwoByte;
|
|
|
|
mStackBased=aStackBased;
|
|
|
|
mLength=mCapacity=0;
|
|
|
|
mIsConst=PR_TRUE;
|
|
|
|
if(aString && aCapacity>1) {
|
|
|
|
mCapacity=aCapacity-1;
|
|
|
|
mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength;
|
|
|
|
if(mLength>PRInt32(mCapacity))
|
|
|
|
mLength=mCapacity;
|
|
|
|
}
|
|
|
|
}
|
1999-07-17 07:26:16 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-10-05 04:47:19 +00:00
|
|
|
|
1999-11-20 04:22:26 +00:00
|
|
|
PRUint32
|
|
|
|
nsStr::HashCode(const nsStr& aDest)
|
|
|
|
{
|
|
|
|
if (aDest.mCharSize == eTwoByte) {
|
|
|
|
PRUint32 h;
|
|
|
|
PRUint32 n = aDest.mLength;
|
|
|
|
PRUint32 m;
|
|
|
|
const PRUnichar* c;
|
|
|
|
h = 0;
|
|
|
|
|
|
|
|
c = aDest.mUStr;
|
|
|
|
if (n < 16) { /* Hash every char in a short string. */
|
|
|
|
for(; n; c++, n--)
|
|
|
|
h = (h >> 28) ^ (h << 4) ^ *c;
|
|
|
|
}
|
|
|
|
else { /* Sample a la java.lang.String.hash(). */
|
|
|
|
for(m = n / 8; n >= m; c += m, n -= m)
|
|
|
|
h = (h >> 28) ^ (h << 4) ^ *c;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
return (PRUint32)PL_HashString((const void*) aDest.mStr);
|
|
|
|
}
|
|
|
|
|
1999-11-20 07:28:45 +00:00
|
|
|
#ifdef NS_STR_STATS
|
1999-11-20 04:22:26 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
1999-11-20 05:46:04 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#define isascii(c) ((unsigned)(c) < 0x80)
|
|
|
|
#endif
|
|
|
|
|
1999-11-20 04:22:26 +00:00
|
|
|
void
|
|
|
|
nsStr::Print(const nsStr& aDest, FILE* out, PRBool truncate)
|
|
|
|
{
|
|
|
|
PRInt32 printLen = (PRInt32)aDest.mLength;
|
|
|
|
|
|
|
|
if (aDest.mCharSize == eOneByte) {
|
|
|
|
const char* chars = aDest.mStr;
|
|
|
|
while (printLen-- && (!truncate || *chars != '\n')) {
|
|
|
|
fputc(*chars++, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const PRUnichar* chars = aDest.mUStr;
|
|
|
|
while (printLen-- && (!truncate || *chars != '\n')) {
|
|
|
|
if (isascii(*chars))
|
|
|
|
fputc((char)(*chars++), out);
|
|
|
|
else
|
|
|
|
fputc('-', out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// String Usage Statistics Routines
|
|
|
|
|
|
|
|
static PLHashTable* gStringInfo = nsnull;
|
|
|
|
PRLock* gStringInfoLock = nsnull;
|
|
|
|
PRBool gNoStringInfo = PR_FALSE;
|
|
|
|
|
|
|
|
nsStringInfo::nsStringInfo(nsStr& str)
|
|
|
|
: mCount(0)
|
|
|
|
{
|
|
|
|
nsStr::Initialize(mStr, str.mCharSize);
|
|
|
|
nsStr::Assign(mStr, str, 0, -1);
|
|
|
|
// nsStr::Print(mStr, stdout);
|
|
|
|
// fputc('\n', stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_EXTERN(PRHashNumber)
|
|
|
|
nsStr_Hash(const void* key)
|
|
|
|
{
|
|
|
|
nsStr* str = (nsStr*)key;
|
|
|
|
return nsStr::HashCode(*str);
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_EXTERN(PRIntn)
|
|
|
|
nsStr_Compare(const void *v1, const void *v2)
|
|
|
|
{
|
|
|
|
nsStr* str1 = (nsStr*)v1;
|
|
|
|
nsStr* str2 = (nsStr*)v2;
|
|
|
|
return nsStr::Compare(*str1, *str2, -1, PR_FALSE) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStringInfo*
|
|
|
|
nsStringInfo::GetInfo(nsStr& str)
|
|
|
|
{
|
|
|
|
if (gStringInfo == nsnull) {
|
|
|
|
gStringInfo = PL_NewHashTable(1024,
|
|
|
|
nsStr_Hash,
|
|
|
|
nsStr_Compare,
|
|
|
|
PL_CompareValues,
|
|
|
|
NULL, NULL);
|
|
|
|
gStringInfoLock = PR_NewLock();
|
|
|
|
}
|
|
|
|
PR_Lock(gStringInfoLock);
|
|
|
|
nsStringInfo* info =
|
|
|
|
(nsStringInfo*)PL_HashTableLookup(gStringInfo, &str);
|
|
|
|
if (info == NULL) {
|
|
|
|
gNoStringInfo = PR_TRUE;
|
|
|
|
info = new nsStringInfo(str);
|
|
|
|
if (info) {
|
|
|
|
PLHashEntry* e = PL_HashTableAdd(gStringInfo, &info->mStr, info);
|
|
|
|
if (e == NULL) {
|
|
|
|
delete info;
|
|
|
|
info = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gNoStringInfo = PR_FALSE;
|
|
|
|
}
|
|
|
|
PR_Unlock(gStringInfoLock);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsStringInfo::Seen(nsStr& str)
|
|
|
|
{
|
|
|
|
if (!gNoStringInfo) {
|
|
|
|
nsStringInfo* info = GetInfo(str);
|
|
|
|
info->mCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsStringInfo::Report(FILE* out)
|
|
|
|
{
|
|
|
|
if (gStringInfo) {
|
|
|
|
fprintf(out, "\n== String Stats\n");
|
|
|
|
PL_HashTableEnumerateEntries(gStringInfo, nsStringInfo::ReportEntry, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntn
|
|
|
|
nsStringInfo::ReportEntry(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
nsStringInfo* entry = (nsStringInfo*)he->value;
|
|
|
|
FILE* out = (FILE*)arg;
|
|
|
|
|
|
|
|
fprintf(out, "%d ==> (%d) ", entry->mCount, entry->mStr.mLength);
|
|
|
|
nsStr::Print(entry->mStr, out, PR_TRUE);
|
|
|
|
fputc('\n', out);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
1999-11-20 07:28:45 +00:00
|
|
|
#endif // NS_STR_STATS
|
1999-10-05 04:47:19 +00:00
|
|
|
|
1999-11-20 04:22:26 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|