2000-03-03 12:04:50 +03:00
|
|
|
/*************************************************************************************
|
|
|
|
*
|
|
|
|
* MODULES NOTES:
|
|
|
|
*
|
|
|
|
* 1. See nsStringImpl.h
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* Contributor(s):
|
|
|
|
* Rick Gessner <rickg@netscape.com>
|
|
|
|
*
|
|
|
|
* History:
|
2000-03-03 12:04:50 +03:00
|
|
|
*
|
|
|
|
* 02.29.2000: Original files (rickg)
|
|
|
|
* 03.02.2000: Flesh out the interface to be compatible with old library (rickg)
|
2000-03-09 13:50:02 +03:00
|
|
|
* 03.03.2000: Finished mapping original nsStr functionality to template form (rickg)
|
2000-03-31 13:24:08 +04:00
|
|
|
* 03.10.2000: Fixed off-by-1 error in StripChar() (rickg)
|
|
|
|
* 03.20.2000: Ran regressions by comparing operations against original nsString (rickg)
|
2000-03-03 12:04:50 +03:00
|
|
|
*
|
|
|
|
*************************************************************************************/
|
|
|
|
|
|
|
|
#ifndef NS_BUFFERMANAGER_
|
|
|
|
#define NS_BUFFERMANAGER_
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsStringValue.h"
|
|
|
|
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#define NS_MEMORY_ERROR 1000
|
|
|
|
|
|
|
|
|
|
|
|
// uncomment the following line to caught nsString char* casting problem
|
|
|
|
//#define DEBUG_ILLEGAL_CAST_UP
|
|
|
|
//#define DEBUG_ILLEGAL_CAST_DOWN
|
|
|
|
|
|
|
|
#if defined(DEBUG_ILLEGAL_CAST_UP) || defined(DEBUG_ILLEGAL_CAST_DOWN)
|
|
|
|
|
|
|
|
static PRBool track_illegal = PR_TRUE;
|
|
|
|
static PRBool track_latin1 = PR_TRUE;
|
|
|
|
|
|
|
|
#ifdef XP_UNIX
|
|
|
|
#include "nsTraceRefcnt.h"
|
|
|
|
class CTraceFile {
|
|
|
|
public:
|
|
|
|
CTraceFile() {
|
|
|
|
mFile = fopen("nsStringTrace.txt" , "a+");
|
|
|
|
}
|
|
|
|
~CTraceFile() {
|
|
|
|
fflush(mFile);
|
|
|
|
fclose(mFile);
|
|
|
|
}
|
|
|
|
void ReportCastUp(const char* data, const char* msg)
|
|
|
|
{
|
|
|
|
if(mFile) {
|
|
|
|
fprintf(mFile, "ERRORTEXT= %s\n", msg);
|
|
|
|
fprintf(mFile, "BEGINDATA\n");
|
|
|
|
const char* s=data;
|
|
|
|
while(*s) {
|
|
|
|
if(*s & 0x80) {
|
|
|
|
fprintf(mFile, "[%2X]", (char)*s);
|
|
|
|
} else {
|
|
|
|
fprintf(mFile, "%c", *s);
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
fprintf(mFile, "\n");
|
|
|
|
fprintf(mFile, "ENDDATA\n");
|
|
|
|
fprintf(mFile, "BEGINSTACK\n");
|
|
|
|
nsTraceRefcnt::WalkTheStack(mFile);
|
|
|
|
fprintf(mFile, "\n");
|
|
|
|
fprintf(mFile, "ENDSTACK\n");
|
|
|
|
fflush(mFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void ReportCastDown(const PRUnichar* data, const char* msg)
|
|
|
|
{
|
|
|
|
if(mFile) {
|
|
|
|
fprintf(mFile, "ERRORTEXT=%s\n", msg);
|
|
|
|
fprintf(mFile, "BEGINDATA\n");
|
|
|
|
const PRUnichar* s=data;
|
|
|
|
while(*s) {
|
|
|
|
if(*s & 0xFF80) {
|
|
|
|
fprintf(mFile, "\\u%X", *s);
|
|
|
|
} else {
|
|
|
|
fprintf(mFile, "%c", *s);
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
fprintf(mFile, "\n");
|
|
|
|
fprintf(mFile, "ENDDATA\n");
|
|
|
|
fprintf(mFile, "BEGINSTACK\n");
|
|
|
|
nsTraceRefcnt::WalkTheStack(mFile);
|
|
|
|
fprintf(mFile, "\n");
|
|
|
|
fprintf(mFile, "ENDSTACK\n");
|
|
|
|
fflush(mFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
FILE* mFile;
|
|
|
|
};
|
|
|
|
static CTraceFile gTrace;
|
|
|
|
#define TRACE_ILLEGAL_CAST_UP(c, s, m) if(!(c)) gTrace.ReportCastUp(s,m);
|
|
|
|
#define TRACE_ILLEGAL_CAST_DOWN(c, s, m) if(!(c)) gTrace.ReportCastDown(s,m);
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#else // XP_UNIX
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#define TRACE_ILLEGAL_CAST_UP(c, s, m) NS_ASSERTION((c), (m))
|
|
|
|
#define TRACE_ILLEGAL_CAST_DOWN(c, s, m) NS_ASSERTION((c), (m))
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#endif //XP_UNIX
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#endif
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************************
|
|
|
|
*
|
|
|
|
* These methods offer the low level buffer manipulation routines used throughout
|
|
|
|
* the string hierarchy. T1 and T2 will be types of stringvalues. (rickg)
|
|
|
|
*
|
|
|
|
*************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
template <class CharType>
|
|
|
|
inline void SVAddNullTerminator( nsStringValueImpl<CharType>& aDest) {
|
|
|
|
if(aDest.mBuffer) {
|
|
|
|
aDest.mBuffer[aDest.mLength]=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
template <class CharType>
|
|
|
|
inline CharType ToLower(CharType aChar) {
|
|
|
|
if((aChar>='A') && (aChar<='Z'))
|
|
|
|
aChar+=32;
|
|
|
|
return aChar;
|
|
|
|
}
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
static const PRUnichar gCommonEmptyBuffer[1] = {0};
|
|
|
|
static nsresult gStringAcquiredMemory = NS_OK;
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
|
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
template <class CharType>
|
2000-03-09 13:50:02 +03:00
|
|
|
inline nsresult SVAlloc( nsStringValueImpl<CharType>& aDest, PRUint32 aCount) {
|
|
|
|
|
|
|
|
nsresult result=NS_OK;
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
//we're given the acount value in charunits; now scale up to next multiple.
|
2000-03-09 13:50:02 +03:00
|
|
|
aDest.mCapacity=kDefaultStringSize;
|
|
|
|
while(aDest.mCapacity<aCount){
|
|
|
|
aDest.mCapacity<<=1;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aDest.mBuffer = new CharType[1+aDest.mCapacity];
|
|
|
|
|
|
|
|
// PRUint32 theSize=(theNewCapacity<<aDest.mCharSize);
|
|
|
|
// aDest.mBuffer = (char*)nsAllocator::Alloc(theSize);
|
|
|
|
|
|
|
|
if(aDest.mBuffer) {
|
2000-03-09 13:50:02 +03:00
|
|
|
aDest.mBuffer[0]=0;
|
2000-03-03 12:04:50 +03:00
|
|
|
gStringAcquiredMemory=PR_TRUE;
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
else {
|
|
|
|
gStringAcquiredMemory=PR_FALSE;
|
|
|
|
result=NS_MEMORY_ERROR;
|
|
|
|
}
|
|
|
|
return result;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free memory from given stringvalueimpl
|
|
|
|
*
|
|
|
|
* @update rickg 03.02.2000
|
|
|
|
* @return memory error (usually returns NS_OK)
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-09 13:50:02 +03:00
|
|
|
inline nsresult SVFree( nsStringValueImpl<CharType>& aDest){
|
2000-03-31 13:24:08 +04:00
|
|
|
aDest.mRefCount--;
|
|
|
|
|
|
|
|
if((aDest.mBuffer) && (!aDest.mRefCount)){
|
2000-03-09 13:50:02 +03:00
|
|
|
delete [] aDest.mBuffer; // nsAllocator::Free(aDest.mBuffer);
|
2000-03-03 12:04:50 +03:00
|
|
|
aDest.mBuffer=0;
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
return NS_OK;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Realloc memory from given stringvalueimpl
|
|
|
|
*
|
|
|
|
* @update rickg 03.02.2000
|
|
|
|
* @return memory error (usually returns NS_OK)
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-09 13:50:02 +03:00
|
|
|
inline nsresult SVRealloc(nsStringValueImpl<CharType>&aDest,PRUint32 aCount){
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
nsStringValueImpl<CharType> temp(aDest);
|
2000-03-31 13:24:08 +04:00
|
|
|
nsresult result=SVAlloc(temp,aCount);
|
|
|
|
if(NS_SUCCEEDED(result)) {
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if(0<aDest.mLength) {
|
|
|
|
memcpy(temp.mBuffer,aDest.mBuffer,aDest.mLength*sizeof(CharType));
|
|
|
|
temp.mLength=aDest.mLength;
|
|
|
|
temp.mBuffer[temp.mLength]=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SVFree(aDest);
|
|
|
|
aDest=temp;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
/**
|
|
|
|
* Retrieve last memory error
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.02.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @return memory error (usually returns NS_OK)
|
|
|
|
*/
|
2000-03-09 13:50:02 +03:00
|
|
|
static PRBool DidAcquireMemory(void) {
|
2000-03-03 12:04:50 +03:00
|
|
|
return gStringAcquiredMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
/**
|
|
|
|
* This is the low-level buffer copying routine between 2 stringvalues.
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.01.2000
|
|
|
|
* @param aDest is the destination stringvalue
|
|
|
|
* @param aSource is the source stringvalue (you must account for offset yourself in calling routine)
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aCount tells us how many chars to copy from aSource
|
2000-03-09 13:50:02 +03:00
|
|
|
*/
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVCopyBuffer_( char* aDest, const char* aSource, PRUint32 aCount) {
|
|
|
|
memcpy(aDest,aSource,aCount);
|
2000-03-09 13:50:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVCopyBuffer_( PRUnichar* aDest, const PRUnichar* aSource, PRUint32 aCount) {
|
|
|
|
memcpy(aDest,aSource,aCount*sizeof(PRUnichar));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the low-level buffer copying routine between 2 stringvalues.
|
|
|
|
*
|
|
|
|
* @update rickg 03.01.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @param aDest is the destination stringvalue
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aSource is the source stringvalue (you must account for offset yourself in calling routine)
|
|
|
|
* @param aCount tells us how many chars to copy from aSource
|
2000-03-03 12:04:50 +03:00
|
|
|
*/
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVCopyBuffer_( char* aDest, const PRUnichar* aSource, PRUint32 aCount) {
|
|
|
|
const PRUnichar* aLast=aSource+aCount;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ILLEGAL_CAST_UP
|
|
|
|
int i=0;
|
|
|
|
for(i=0;i<aCount;i++){
|
|
|
|
if(aSource[i]>255) {
|
|
|
|
NS_ASSERTION( (aSource[i] < 256), "data in U+0100-U+FFFF will be lost");
|
|
|
|
if(track_illegal) {
|
|
|
|
if(track_latin1) {
|
|
|
|
if(aSource[i] & 0xFF80)
|
|
|
|
illegal = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
if(aSource[i] & 0xFF00)
|
|
|
|
illegal = PR_TRUE;
|
|
|
|
} // track_latin1
|
|
|
|
} // track_illegal
|
|
|
|
break;
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
}
|
|
|
|
#endif
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(aSource<aLast) {
|
|
|
|
*aDest++=(char)*aSource++;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
return NS_OK;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVCopyBuffer_( PRUnichar* aDest, const char* aSource, PRUint32 aCount) {
|
|
|
|
const char* aLast=aSource+aCount;
|
|
|
|
while(aSource<aLast) {
|
|
|
|
*aDest++=(PRUnichar)*aSource++;
|
|
|
|
}
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#ifdef DEBUG_ILLEGAL_CAST_UP
|
|
|
|
int i=0;
|
|
|
|
for(i=0;i<aCount;i++){
|
|
|
|
if(track_illegal && track_latin1 && ((aDest[i])& 0x80)) {
|
|
|
|
TRACE_ILLEGAL_CAST_UP((!illegal), aSource, "illegal cast up in copying 1-to-2 byte buffers.");
|
|
|
|
break;
|
|
|
|
}
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
#endif
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
return NS_OK;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method deletes chars from the given str.
|
|
|
|
* The given allocator may choose to resize the str as well.
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.01.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @param aDest is the stringvalue to be modified
|
|
|
|
* @param aDestOffset tells us where in dest to start deleting
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aCount tells us the (max) # of chars to delete (-1 means delete the rest of the string starting at offset)
|
2000-03-03 12:04:50 +03:00
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVDelete( nsStringValueImpl<CharType>& aDest,PRUint32 anOffset,PRInt32 aCount=-1){
|
2000-03-09 13:50:02 +03:00
|
|
|
nsresult result=NS_OK;
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 theLength =(aCount<0) ? aDest.mLength : aCount;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType* root_cp = aDest.mBuffer;
|
|
|
|
CharType* null_cp = root_cp+aDest.mLength;
|
|
|
|
CharType* offset_cp = root_cp+anOffset; //dst1
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if(offset_cp<null_cp) {
|
|
|
|
|
|
|
|
//Proceed only if the offset is in range
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType* max_cp = offset_cp+aCount; //dst2
|
|
|
|
|
|
|
|
if(max_cp<null_cp) {
|
|
|
|
aDest.mLength-=(max_cp-offset_cp);
|
|
|
|
memmove(offset_cp,max_cp,(null_cp-max_cp)*sizeof(CharType));
|
|
|
|
}
|
|
|
|
else aDest.mLength=anOffset;
|
|
|
|
aDest.mBuffer[aDest.mLength]=0;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
return result;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to truncate the given string.
|
|
|
|
* The given allocator may choose to resize the str as well (but it's not likely).
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.01.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @param aDest is the stringvalue to be modified
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param anOffset tells us where in dest to truncate
|
2000-03-03 12:04:50 +03:00
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-31 13:24:08 +04:00
|
|
|
inline void SVTruncate( nsStringValueImpl<CharType>& aDest,PRUint32 anOffset=0) {
|
|
|
|
if(anOffset<aDest.mLength){
|
|
|
|
aDest.mLength=anOffset;
|
|
|
|
aDest.mBuffer[aDest.mLength]=0;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
}
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* These methods are used to append content to the given stringvalue
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.01.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @param aDest is the stringvalue to be modified
|
|
|
|
* @param aSource is the buffer to be copied from
|
|
|
|
* @param anOffset tells us where in source to start copying
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aCount tells us the (max) # of chars to append
|
2000-03-03 12:04:50 +03:00
|
|
|
*/
|
|
|
|
template <class CharType1, class CharType2>
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVAppend( nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aSource,PRInt32 aCount=-1,PRUint32 aSrcOffset=0){
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
nsresult result=NS_OK;
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if(aSrcOffset<aSource.mLength){
|
|
|
|
PRInt32 theRealLen=(aCount<0) ? aSource.mLength : (aCount<(PRInt32)aSource.mLength) ? aCount : aSource.mLength;
|
2000-03-09 13:50:02 +03:00
|
|
|
PRInt32 theLength=(aSrcOffset+theRealLen<(PRInt32)aSource.mLength) ? theRealLen : (aSource.mLength-aSrcOffset);
|
2000-03-03 12:04:50 +03:00
|
|
|
if(0<theLength){
|
|
|
|
|
|
|
|
PRBool isBigEnough=PR_TRUE;
|
|
|
|
if(aDest.mLength+theLength > aDest.mCapacity) {
|
2000-03-31 13:24:08 +04:00
|
|
|
result=SVRealloc(aDest,aDest.mLength+theLength);
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if(NS_SUCCEEDED(result)) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
//now append new chars, starting at offset
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType1 *to_cp = aDest.mBuffer+aDest.mLength;
|
|
|
|
const CharType2 *from_cp = aSource.mBuffer+aSrcOffset;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
SVCopyBuffer_(to_cp,from_cp,theLength);
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
aDest.mLength+=theLength;
|
2000-03-31 13:24:08 +04:00
|
|
|
aDest.mBuffer[aDest.mLength]=0;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
return result;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-03-09 13:50:02 +03:00
|
|
|
* This method is used to append content to the given stringvalue from an nsAReadableString
|
2000-03-03 12:04:50 +03:00
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.01.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @param aDest is the stringvalue to be modified
|
|
|
|
* @param aSource is the readablestring to be copied from
|
|
|
|
* @param anOffset tells us where in source to start copying
|
|
|
|
* @param aCount tells us the (max) # of chars to copy
|
|
|
|
*/
|
|
|
|
template <class CharType1>
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVAppendReadable( nsStringValueImpl<CharType1>& aDest,const nsAReadableString& aSource,PRInt32 aCount,PRUint32 anSrcOffset){
|
2000-03-09 13:50:02 +03:00
|
|
|
nsresult result=NS_OK;
|
2000-03-03 12:04:50 +03:00
|
|
|
/*
|
|
|
|
//I expect a pattern similar to this that could work on any readable string
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
nsAReadableStringIterator theFirst=aSource.First();
|
|
|
|
nsAReadableStringIterator theLast=aSource.Last();
|
2000-03-03 12:04:50 +03:00
|
|
|
CharType1 *theDest=aDest.mBuffer;
|
|
|
|
while(theFirst<theLast) {
|
|
|
|
*theDest++=theFirst++;
|
|
|
|
}
|
|
|
|
*/
|
2000-03-09 13:50:02 +03:00
|
|
|
return result;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These methods are used to assign contents of a source string to dest string
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.01.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @param aDest is the stringvalue to be modified
|
|
|
|
* @param aSource is the buffer to be copied from
|
|
|
|
* @param anOffset tells us where in source to start copying
|
|
|
|
* @param aCount tells us the (max) # of chars to copy
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <class CharType1, class CharType2>
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVAssign(nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aSource,PRInt32 aCount=-1,PRUint32 aSrcOffset=0) {
|
2000-03-09 13:50:02 +03:00
|
|
|
nsresult result=NS_OK;
|
|
|
|
if((void*)aDest.mBuffer!=(void*)aSource.mBuffer){
|
2000-03-03 12:04:50 +03:00
|
|
|
SVTruncate(aDest,0);
|
2000-03-09 13:50:02 +03:00
|
|
|
SVAppend(aDest,aSource,aCount,aSrcOffset);
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
return result;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
/**
|
|
|
|
* These methods are used to insert content from source string to the dest stringvalue
|
|
|
|
*
|
2000-03-09 13:50:02 +03:00
|
|
|
* @update rickg 03.01.2000
|
2000-03-03 12:04:50 +03:00
|
|
|
* @param aDest is the stringvalue to be modified
|
|
|
|
* @param aDestOffset tells us where in dest to start insertion
|
|
|
|
* @param aSource is the buffer to be copied from
|
|
|
|
* @param aSrcOffset tells us where in source to start copying
|
|
|
|
* @param aCount tells us the (max) # of chars to insert
|
|
|
|
*/
|
|
|
|
template <class CharType1, class CharType2>
|
2000-03-31 13:24:08 +04:00
|
|
|
inline nsresult SVInsert( nsStringValueImpl<CharType1>& aDest,PRUint32 aDestOffset,const nsStringValueImpl<CharType2>& aSource,PRInt32 aCount=-1,PRUint32 aSrcOffset=0) {
|
2000-03-09 13:50:02 +03:00
|
|
|
nsresult result=NS_OK;
|
|
|
|
|
|
|
|
//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).
|
|
|
|
if(0<aSource.mLength){
|
|
|
|
if(aDest.mLength){
|
|
|
|
if(aDestOffset<aDest.mLength){
|
2000-03-31 13:24:08 +04:00
|
|
|
PRUint32 theRealLen=(aCount<0) ? aSource.mLength : (aCount<(PRInt32)aSource.mLength) ? aCount : aSource.mLength;
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
PRUint32 theLength=(aSrcOffset+theRealLen<aSource.mLength) ? theRealLen : (aSource.mLength-aSrcOffset);
|
|
|
|
|
|
|
|
if(aSrcOffset<(PRInt32)aSource.mLength) {
|
|
|
|
//here's the only new case we have to handle.
|
|
|
|
//chars are really being inserted into our buffer...
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType2 *from_src=aSource.mBuffer+aSrcOffset;
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
if(aDest.mLength+theLength > aDest.mCapacity) {
|
|
|
|
|
|
|
|
nsStringValueImpl<CharType1> theTempStr;
|
2000-03-31 13:24:08 +04:00
|
|
|
result=SVAlloc(theTempStr,aDest.mLength+theLength);
|
|
|
|
|
|
|
|
if(NS_SUCCEEDED(result)) {
|
|
|
|
|
|
|
|
CharType1 *to_cp=theTempStr.mBuffer;
|
|
|
|
|
|
|
|
memcpy(to_cp,aDest.mBuffer,aDestOffset*sizeof(CharType1)); //copy front of existing string...
|
|
|
|
|
|
|
|
to_cp+=aDestOffset;
|
|
|
|
SVCopyBuffer_(to_cp,from_src,theLength); //now insert the new data...
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
PRUint32 theRemains=aDest.mLength-aDestOffset;
|
|
|
|
if(theRemains) {
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType1 *from_dst=to_cp;
|
|
|
|
to_cp+=theLength;
|
|
|
|
memcpy(to_cp,from_dst,theRemains*sizeof(CharType1));
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SVFree(aDest);
|
|
|
|
aDest=theTempStr;
|
|
|
|
theTempStr.mBuffer=0; //make sure to null this out so that you don't lose the buffer you just stole...
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2000-03-31 13:24:08 +04:00
|
|
|
//if you're here, it's because we have enough room to insert the new chars into the buffer
|
|
|
|
//that we already possess.
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType1* root_cp = aDest.mBuffer;
|
|
|
|
CharType1* end_cp = root_cp+aDest.mLength;
|
|
|
|
CharType1* offset_cp= root_cp+aDestOffset;
|
|
|
|
CharType1* max_cp = offset_cp+aCount;
|
|
|
|
|
|
|
|
memmove(max_cp,offset_cp,sizeof(CharType1)*(end_cp-offset_cp));
|
|
|
|
|
|
|
|
SVCopyBuffer_(offset_cp,from_src,theLength);
|
|
|
|
|
|
|
|
//finally, make sure to update the string length...
|
2000-03-09 13:50:02 +03:00
|
|
|
aDest.mLength+=theLength;
|
2000-03-31 13:24:08 +04:00
|
|
|
aDest.mBuffer[aDest.mLength]=0;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}//if
|
|
|
|
//else nothing to do!
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
else {
|
|
|
|
SVAppend(aDest,aSource,aCount,0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SVAppend(aDest,aSource,aCount,0);
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method compares characters strings of two types, and ignores case
|
|
|
|
*
|
|
|
|
* @update rickg 03.01.2000
|
|
|
|
* @param aLeft
|
|
|
|
* @param anEnd
|
|
|
|
* @param aTarget
|
|
|
|
* @return aDest<aSource=-1;aDest==aSource==0;aDest>aSource=1
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
inline PRInt32 SVCompareCase_(const CharType1* aLeft,const CharType1* anEnd,const CharType2* aTarget){
|
|
|
|
|
|
|
|
while(aLeft<anEnd){
|
2000-03-31 13:24:08 +04:00
|
|
|
PRUnichar theChar=(PRUnichar)ToLower(*aLeft++);
|
|
|
|
PRUnichar theTargetChar=(PRUnichar)ToLower(*aTarget++);
|
2000-03-09 13:50:02 +03:00
|
|
|
if(theChar<theTargetChar)
|
|
|
|
return -1;
|
|
|
|
else if(theTargetChar<theChar)
|
|
|
|
return 1;
|
|
|
|
} //while
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method compares characters strings of two types (case sensitive)
|
|
|
|
*
|
|
|
|
* @update rickg 03.01.2000
|
|
|
|
* @param aLeft
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param anEnd
|
|
|
|
* @param aTarget
|
|
|
|
* @return aDest<aSource=-1;aDest==aSource==0;aDest>aSource=1
|
|
|
|
*/
|
|
|
|
inline PRInt32 SVCompare_(const char* aLeft,const char* anEnd,const char* aTarget){
|
|
|
|
size_t theCount=anEnd-aLeft;
|
|
|
|
return memcmp(aLeft,aTarget,theCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method compares characters strings of two types (case sensitive)
|
|
|
|
*
|
|
|
|
* @update rickg 03.01.2000
|
|
|
|
* @param aLeft
|
2000-03-09 13:50:02 +03:00
|
|
|
* @param anEnd
|
|
|
|
* @param aTarget
|
|
|
|
* @return aDest<aSource=-1;aDest==aSource==0;aDest>aSource=1
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
inline PRInt32 SVCompare_(const CharType1* aLeft,const CharType1* anEnd,const CharType2* aTarget){
|
|
|
|
|
|
|
|
while(aLeft<anEnd){
|
|
|
|
CharType1 theChar=*aLeft++;
|
|
|
|
CharType2 theTargetChar=*aTarget++;
|
|
|
|
if(theChar<theTargetChar)
|
|
|
|
return -1;
|
|
|
|
else if(theTargetChar<theChar)
|
|
|
|
return 1;
|
|
|
|
} //while
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aSource
|
2000-03-09 13:50:02 +03:00
|
|
|
* @param aIgnoreCase
|
|
|
|
* @param aCount --- tells us how many iterations to make.
|
|
|
|
* @return aDest<aSource=-1;aDest==aSource==0;aDest>aSource=1
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
inline PRInt32 SVCompare(const nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aSource,PRBool aIgnoreCase, PRUint32 aCount){
|
|
|
|
|
|
|
|
PRInt32 result=0;
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if((void*)aDest.mBuffer!=(void*)aSource.mBuffer) {
|
|
|
|
if(aCount) {
|
|
|
|
PRUint32 minlen=(aSource.mLength<aDest.mLength) ? aSource.mLength : aDest.mLength;
|
|
|
|
|
|
|
|
if(0==minlen) {
|
|
|
|
if ((aDest.mLength == 0) && (aSource.mLength == 0))
|
|
|
|
return 0;
|
|
|
|
if (aDest.mLength == 0)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 theCount = (aCount<0) ? minlen: MinInt(aCount,minlen);
|
|
|
|
|
|
|
|
const CharType1* root_cp = aDest.mBuffer;
|
|
|
|
const CharType1* last_cp = root_cp+theCount;
|
|
|
|
const CharType1* null_cp = root_cp+aDest.mLength;
|
|
|
|
const CharType1* end_cp = (last_cp<null_cp) ? last_cp : null_cp;
|
|
|
|
const CharType2* target = aSource.mBuffer;
|
|
|
|
|
|
|
|
if(aIgnoreCase)
|
|
|
|
result = SVCompareCase_(root_cp,end_cp,target) ;
|
|
|
|
else result =SVCompare_(root_cp,end_cp,target);
|
|
|
|
|
|
|
|
if (0==result) {
|
|
|
|
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
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
}
|
|
|
|
return result;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
template <class CharType>
|
|
|
|
inline PRInt32 SVCompareChars(const nsStringValueImpl<CharType>& aLHS,const nsStringValueImpl<CharType>& aRHS,PRBool aIgnoreCase,PRInt32 aCount) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 result=0;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if(aLHS.mBuffer!=aRHS.mBuffer) {
|
|
|
|
if(!aIgnoreCase) {
|
|
|
|
PRInt32 aMax = (aLHS.mLength<aRHS.mLength) ? aLHS.mLength : aRHS.mLength;
|
|
|
|
if((0<aCount) && (aCount<aMax))
|
|
|
|
aMax=aCount;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
result=memcmp(aLHS.mBuffer,aRHS.mBuffer,aMax*sizeof(CharType));
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if(0==result) {
|
|
|
|
if(-1==aCount) {
|
|
|
|
if (aLHS.mLength != aRHS.mLength) {
|
|
|
|
result = (aLHS.mLength<aRHS.mLength) ? -1 : 1;
|
|
|
|
}
|
|
|
|
} //if
|
2000-03-09 13:50:02 +03:00
|
|
|
} //if
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
else result=SVCompare(aLHS,aRHS,aIgnoreCase,aCount);
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aTarget
|
|
|
|
* @param aIgnoreCase
|
|
|
|
* @param aCount : tells us how many iterations to make, starting at offset
|
|
|
|
* @param aOffset: tells us where to start searching within aDest
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
2000-03-31 13:24:08 +04:00
|
|
|
inline PRInt32 SVFindOrig(const nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aTarget,PRBool aIgnoreCase,PRInt32 aCount=-1,PRUint32 aSrcOffset=0) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
PRUint32 minlen=(aTarget.mLength<aDest.mLength) ? aTarget.mLength : aDest.mLength;
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
aCount = (-1==aCount) ? aDest.mLength-minlen+1 : aCount;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
if((0<aCount) && minlen) {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
const CharType1 *root_cp = aDest.mBuffer;
|
|
|
|
const CharType1 *offset_cp = root_cp+aSrcOffset;
|
|
|
|
const CharType1 *null_cp = root_cp+aDest.mLength;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if(offset_cp<null_cp) {
|
|
|
|
PRInt32 cmp=0;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
const CharType1* last_cp = offset_cp+aCount;
|
|
|
|
const CharType1* end_cp = (last_cp<null_cp) ? last_cp : null_cp;
|
|
|
|
|
|
|
|
if(aIgnoreCase) {
|
|
|
|
while(offset_cp<=end_cp) {
|
|
|
|
cmp = SVCompareCase_(offset_cp,offset_cp+minlen,aTarget.mBuffer) ;
|
|
|
|
if(0==cmp) {
|
|
|
|
return offset_cp-root_cp;
|
|
|
|
}
|
|
|
|
offset_cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
while(offset_cp<=null_cp) {
|
|
|
|
cmp=SVCompare_(offset_cp,offset_cp+minlen,aTarget.mBuffer);
|
|
|
|
if(0==cmp) {
|
|
|
|
return offset_cp-root_cp;
|
|
|
|
}
|
|
|
|
offset_cp++;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
}
|
|
|
|
return kNotFound;
|
|
|
|
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
|
2000-03-31 13:24:08 +04: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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aTarget
|
|
|
|
* @param aIgnoreCase
|
|
|
|
* @param aCount : tells us how many iterations to make, starting at offset
|
|
|
|
* @param aOffset: tells us where to start searching within aDest
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
inline PRInt32 SVFind(const nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aTarget,PRBool aIgnoreCase,PRInt32 aCount=-1,PRUint32 aSrcOffset=0) {
|
|
|
|
|
|
|
|
PRUint32 minlen=(aTarget.mLength<aDest.mLength) ? aTarget.mLength : aDest.mLength;
|
|
|
|
|
|
|
|
aCount = (-1==aCount) ? aDest.mLength-minlen+1 : aCount;
|
|
|
|
|
|
|
|
if((0<aCount) && minlen) {
|
|
|
|
|
|
|
|
const CharType1* root_cp = aDest.mBuffer;
|
|
|
|
const CharType1* offset_cp = root_cp+aSrcOffset;
|
|
|
|
const CharType1* null_cp = root_cp+aDest.mLength;
|
|
|
|
|
|
|
|
if(offset_cp<null_cp) {
|
|
|
|
PRInt32 cmp=0;
|
|
|
|
|
|
|
|
const CharType1* last_cp = offset_cp+aCount;
|
|
|
|
const CharType1* end_cp = (last_cp<null_cp) ? last_cp : null_cp;
|
|
|
|
|
|
|
|
if(aIgnoreCase) {
|
|
|
|
while(offset_cp<=end_cp) {
|
|
|
|
cmp = SVCompareCase_(offset_cp,offset_cp+minlen,aTarget.mBuffer) ;
|
|
|
|
if(0==cmp) {
|
|
|
|
return offset_cp-root_cp;
|
|
|
|
}
|
|
|
|
offset_cp++;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
//XXX Hack alert. I'm only playing around here with a lookaside buffer until I can get around to implementing
|
|
|
|
// a NMP search suitable for unicode chars.
|
|
|
|
|
|
|
|
const CharType1* theLATable[256];
|
|
|
|
PRInt32 theLACount=0;
|
|
|
|
|
|
|
|
const CharType1 *leftLA=offset_cp;
|
|
|
|
const CharType1 *endLA=end_cp;
|
|
|
|
const CharType1 *lastLA=last_cp;
|
|
|
|
|
|
|
|
const CharType2 targetFirst=aTarget.mBuffer[0];
|
|
|
|
const CharType2 targetLast=aTarget.mBuffer[aTarget.mLength-1];
|
|
|
|
size_t countLA=aTarget.mLength-1;
|
|
|
|
|
|
|
|
while(leftLA<=endLA) {
|
|
|
|
if((*leftLA==targetFirst) && (*(leftLA+countLA)==targetLast)) {
|
|
|
|
theLATable[theLACount++]=leftLA;
|
|
|
|
}
|
|
|
|
leftLA++;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
theLATable[theLACount]=0;
|
|
|
|
|
|
|
|
|
|
|
|
PRInt32 theLAIndex=0;
|
|
|
|
while(offset_cp=theLATable[theLAIndex++]) {
|
|
|
|
cmp =SVCompare_(offset_cp,offset_cp+minlen,aTarget.mBuffer);
|
|
|
|
if(0==cmp) {
|
|
|
|
return offset_cp-root_cp;
|
|
|
|
}
|
|
|
|
} //while
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
/**
|
|
|
|
* This searches for the given char withing the given string
|
|
|
|
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
|
|
|
|
*
|
|
|
|
* @update rickg 03.02.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
|
|
|
* @param aEliminateLeading
|
|
|
|
* @param aEliminateTrailing
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 SVFindChar(const nsStringValueImpl<CharType>& aDest,PRUnichar aChar,PRBool aIgnoreCase,PRInt32 aCount=-1,PRUint32 aSrcOffset=0) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
if(aSrcOffset<0)
|
|
|
|
aSrcOffset=0;
|
|
|
|
|
|
|
|
if(aCount<0)
|
|
|
|
aCount = (PRInt32)aDest.mLength;
|
|
|
|
|
|
|
|
size_t theCharSize=sizeof(CharType);
|
|
|
|
if((2==theCharSize) || (aChar<256)) {
|
|
|
|
if((0<aDest.mLength) && ((PRUint32)aSrcOffset<aDest.mLength)) {
|
|
|
|
|
|
|
|
//We'll only search if the given aChar is within the normal ascii a range,
|
|
|
|
//(Since this string is definitely within the ascii range).
|
|
|
|
|
|
|
|
if(0<aCount) {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
const CharType* root_cp = aDest.mBuffer;
|
|
|
|
const CharType* offset_cp = root_cp+aSrcOffset;
|
|
|
|
const CharType* last_cp = offset_cp+aCount;
|
|
|
|
const CharType* max_cp = offset_cp+aDest.mLength;
|
|
|
|
const CharType* end_cp = (last_cp<max_cp) ? last_cp : max_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
if(aIgnoreCase) {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
PRUnichar theChar=ToLower(aChar);
|
|
|
|
while(offset_cp<end_cp){
|
|
|
|
if(ToLower(*offset_cp)==theChar)
|
|
|
|
return offset_cp-root_cp;
|
|
|
|
++offset_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (1==theCharSize){
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 theMax = end_cp-offset_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
if(0<theMax) {
|
|
|
|
CharType theChar=(CharType)aChar;
|
2000-03-31 13:24:08 +04:00
|
|
|
const char* result=(const char*)memchr(offset_cp, theChar, theMax);
|
2000-03-09 13:50:02 +03:00
|
|
|
if(result) {
|
2000-03-31 13:24:08 +04:00
|
|
|
return result-(char*)root_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2000-03-31 13:24:08 +04:00
|
|
|
while(offset_cp<end_cp){
|
|
|
|
if(*offset_cp==aChar)
|
|
|
|
return (offset_cp-root_cp);
|
|
|
|
++offset_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
} //while
|
|
|
|
} //else
|
|
|
|
}
|
|
|
|
} //if
|
|
|
|
}//if
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This searches for the given char withing the given string
|
|
|
|
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
|
|
|
|
*
|
|
|
|
* @update rickg 03.02.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
|
|
|
* @param aEliminateLeading
|
|
|
|
* @param aEliminateTrailing
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
inline PRInt32 SVFindCharInSet(const nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aSet,PRBool aIgnoreCase,PRInt32 aCount,PRInt32 anOffset) {
|
|
|
|
|
|
|
|
if(anOffset<0)
|
|
|
|
anOffset=0;
|
|
|
|
|
|
|
|
if(aCount<0)
|
|
|
|
aCount = (PRInt32)aDest.mLength;
|
|
|
|
|
|
|
|
if((0<aCount) && (0<aSet.mLength)){
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
const CharType1* root_cp = aDest.mBuffer;
|
|
|
|
const CharType1* offset_cp = root_cp+anOffset;
|
|
|
|
const CharType1* last_cp = offset_cp+aCount;
|
|
|
|
const CharType1* nullcp = root_cp+aDest.mLength;
|
|
|
|
const CharType1* end_cp = (last_cp<nullcp) ? last_cp : nullcp;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
PRInt32 cmp=0;
|
2000-03-31 13:24:08 +04:00
|
|
|
while(offset_cp<=end_cp) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
PRUnichar theChar=(PRUnichar)*offset_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 thePos=SVFindChar(aSet,theChar,aIgnoreCase,aSet.mLength,0);
|
2000-03-09 13:50:02 +03:00
|
|
|
if(kNotFound<thePos) {
|
2000-03-31 13:24:08 +04:00
|
|
|
return offset_cp-root_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
offset_cp++;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
|
|
|
* @param aEliminateLeading
|
|
|
|
* @param aEliminateTrailing
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
inline PRInt32 SVRFind(const nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aTarget,PRBool aIgnoreCase,PRInt32 aCount,PRInt32 aSrcOffset) {
|
|
|
|
|
|
|
|
if(aSrcOffset<0)
|
|
|
|
aSrcOffset=(PRInt32)aDest.mLength-1;
|
|
|
|
|
|
|
|
if(aCount<0)
|
|
|
|
aCount = aSrcOffset;
|
|
|
|
|
|
|
|
size_t theCharSize=sizeof(CharType1);
|
|
|
|
if((0<aTarget.mLength) && (0<aDest.mLength) && ((PRUint32)aSrcOffset<aDest.mLength)) {
|
|
|
|
|
|
|
|
//We'll only search if the given aChar is within the normal ascii a range,
|
|
|
|
//(Since this string is definitely within the ascii range).
|
|
|
|
|
|
|
|
if(0<aCount) {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
const CharType1* root_cp = aDest.mBuffer;
|
|
|
|
const CharType1* first_cp = root_cp+aSrcOffset;
|
|
|
|
const CharType1* last_cp = first_cp+aTarget.mLength;
|
|
|
|
const CharType1* min_cp = first_cp-aCount+1;
|
|
|
|
const CharType1* left_cp = (min_cp<root_cp) ? root_cp : min_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
PRInt32 cmp=0;
|
2000-03-31 13:24:08 +04:00
|
|
|
while(left_cp<=first_cp) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
if(aIgnoreCase)
|
2000-03-31 13:24:08 +04:00
|
|
|
cmp = SVCompareCase_(first_cp,last_cp,aTarget.mBuffer) ;
|
|
|
|
else cmp =SVCompare_(first_cp,last_cp,aTarget.mBuffer);
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
if(0==cmp) {
|
2000-03-31 13:24:08 +04:00
|
|
|
return first_cp-root_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
first_cp--;
|
|
|
|
last_cp--;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods cans the given buffer (in reverse) for the given char
|
|
|
|
*
|
|
|
|
* @update gess 02/17/00
|
|
|
|
* @param aDest is the buffer to be searched
|
|
|
|
* @param anOffset is the start pos to begin searching
|
|
|
|
* @param aChar is the target character we're looking for
|
|
|
|
* @param aIgnorecase tells us whether to use a case sensitive search
|
|
|
|
* @param aCount tells us how many characters to iterate through (which may be different than aLength); -1 means use full length.
|
|
|
|
* @return index of pos if found, else -1 (kNotFound)
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
|
|
|
PRInt32 SVRFindChar(const nsStringValueImpl<CharType>& aDest,PRUnichar aChar,PRBool aIgnoreCase,PRInt32 aCount,PRInt32 aSrcOffset) {
|
|
|
|
|
|
|
|
if(aSrcOffset<0)
|
|
|
|
aSrcOffset=(PRInt32)aDest.mLength-1;
|
|
|
|
|
|
|
|
if(aCount<0)
|
|
|
|
aCount = aSrcOffset;
|
|
|
|
|
|
|
|
size_t theCharSize=sizeof(CharType);
|
|
|
|
if((2==theCharSize) || (aChar<256)) {
|
|
|
|
if((0<aDest.mLength) && ((PRUint32)aSrcOffset<aDest.mLength)) {
|
|
|
|
|
|
|
|
//We'll only search if the given aChar is within the normal ascii a range,
|
|
|
|
//(Since this string is definitely within the ascii range).
|
|
|
|
|
|
|
|
if(0<aCount) {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
const CharType* root_cp = aDest.mBuffer;
|
|
|
|
const CharType* right_cp = root_cp+aSrcOffset;
|
|
|
|
const CharType* min_cp = right_cp-aCount+1;
|
|
|
|
const CharType* left_cp = (min_cp<root_cp) ? root_cp : min_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
if(aIgnoreCase) {
|
2000-03-31 13:24:08 +04:00
|
|
|
|
|
|
|
PRUnichar theChar=ToLower(aChar);
|
|
|
|
|
|
|
|
while(left_cp<=right_cp){
|
|
|
|
if(ToLower(*right_cp)==theChar)
|
|
|
|
return right_cp-root_cp;
|
|
|
|
--right_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType theChar=(CharType)aChar;
|
|
|
|
|
|
|
|
while(left_cp<=right_cp){
|
|
|
|
if((*right_cp)==theChar)
|
|
|
|
return right_cp-root_cp;
|
|
|
|
--right_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods cans the given buffer (in reverse) for the given char
|
|
|
|
*
|
|
|
|
* @update gess 02/17/00
|
|
|
|
* @param aDest is the buffer to be searched
|
|
|
|
* @param anOffset is the start pos to begin searching
|
|
|
|
* @param aChar is the target character we're looking for
|
|
|
|
* @param aIgnorecase tells us whether to use a case sensitive search
|
|
|
|
* @param aCount tells us how many characters to iterate through (which may be different than aLength); -1 means use full length.
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aSrcOffset
|
2000-03-09 13:50:02 +03:00
|
|
|
* @return index of pos if found, else -1 (kNotFound)
|
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
inline PRInt32 SVRFindCharInSet(const nsStringValueImpl<CharType1>& aDest,const nsStringValueImpl<CharType2>& aSet,PRBool aIgnoreCase,PRInt32 aCount,PRInt32 aSrcOffset) {
|
|
|
|
|
|
|
|
if(aSrcOffset<0)
|
|
|
|
aSrcOffset=(PRInt32)aDest.mLength-1;
|
|
|
|
|
|
|
|
if(aCount<0)
|
|
|
|
aCount = aSrcOffset;
|
|
|
|
|
|
|
|
if((0<aDest.mLength) && ((PRUint32)aSrcOffset<aDest.mLength)) {
|
|
|
|
|
|
|
|
//We'll only search if the given aChar is within the normal ascii a range,
|
|
|
|
//(Since this string is definitely within the ascii range).
|
|
|
|
|
|
|
|
if(0<aCount) {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
const CharType1* root_cp = aDest.mBuffer;
|
|
|
|
const CharType1* right_cp = root_cp+aSrcOffset;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(root_cp<=--right_cp){
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType1 theChar=(aIgnoreCase) ? ToLower(*right_cp) : *right_cp;
|
|
|
|
PRInt32 thePos=SVFindChar(aSet,theChar,aIgnoreCase,aSet.mLength,0);
|
2000-03-09 13:50:02 +03:00
|
|
|
if(kNotFound<thePos) {
|
2000-03-31 13:24:08 +04:00
|
|
|
return right_cp-root_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
|
|
|
* @param aEliminateLeading
|
|
|
|
* @param aEliminateTrailing
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
|
|
|
nsresult SVTrim(nsStringValueImpl<CharType>& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
|
|
|
|
|
|
|
|
nsresult result=NS_OK;
|
|
|
|
|
|
|
|
if(0<aDest.mLength) {
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *root_cp = aDest.mBuffer;
|
|
|
|
CharType *end_cp = root_cp+aDest.mLength;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl<char> theSet((char*)aSet);
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
if(aEliminateTrailing) {
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(root_cp<end_cp) {
|
|
|
|
CharType theChar=*(end_cp-1);
|
|
|
|
PRInt32 thePos=SVFindChar(theSet,theChar,PR_FALSE,theSet.mLength,0);
|
2000-03-09 13:50:02 +03:00
|
|
|
if(kNotFound==thePos)
|
|
|
|
break;
|
2000-03-31 13:24:08 +04:00
|
|
|
--end_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
aDest.mLength=(end_cp-root_cp);
|
|
|
|
aDest.mBuffer[aDest.mLength]=0;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *cp = root_cp-1;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
if(aEliminateLeading) {
|
2000-03-31 13:24:08 +04:00
|
|
|
while(++cp<end_cp) {
|
2000-03-09 13:50:02 +03:00
|
|
|
CharType theChar=*cp;
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 thePos=SVFindChar(theSet,theChar,PR_FALSE,theSet.mLength,0);
|
2000-03-09 13:50:02 +03:00
|
|
|
if(kNotFound==thePos)
|
|
|
|
break;
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
size_t theCount=cp-root_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
if(0<theCount) {
|
|
|
|
result=SVDelete(aDest,0,theCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
|
|
|
* @param aEliminateLeading
|
|
|
|
* @param aEliminateTrailing
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-31 13:24:08 +04:00
|
|
|
nsresult SVStripCharsInSet(nsStringValueImpl<CharType>& aDest,const char* aSet,PRInt32 aCount,PRInt32 anOffset) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if((0<aDest.mLength) && aSet && (anOffset<(PRInt32)aDest.mLength)){
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *left_cp = aDest.mBuffer+anOffset;
|
|
|
|
CharType *theWriteCP = left_cp;
|
|
|
|
const CharType *last_cp= left_cp+aCount;
|
|
|
|
const CharType *max_cp = left_cp+aDest.mLength;
|
|
|
|
const CharType *end_cp = (last_cp<max_cp) ? last_cp : max_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl<char> theSet((char*)aSet);
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(left_cp<end_cp){
|
|
|
|
CharType theChar=*left_cp;
|
|
|
|
PRInt32 thePos=SVFindChar(theSet,theChar,PR_FALSE,theSet.mLength,0);
|
2000-03-09 13:50:02 +03:00
|
|
|
if(kNotFound==thePos) {
|
2000-03-31 13:24:08 +04:00
|
|
|
*theWriteCP++=*left_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
++left_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
} //while
|
|
|
|
*theWriteCP=0;
|
2000-03-31 13:24:08 +04:00
|
|
|
aDest.mLength-=(left_cp-theWriteCP);
|
2000-03-09 13:50:02 +03:00
|
|
|
} //if
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method removes a given char the underlying stringvalue
|
|
|
|
*
|
|
|
|
* @update rickg 03.01.2000
|
|
|
|
* @param aDest is the stringvalue to be modified
|
|
|
|
* @param anOffset tells us where in source to start copying
|
|
|
|
* @param aCount tells us the (max) # of chars to copy
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
|
|
|
nsresult SVStripChar( nsStringValueImpl<CharType>& aDest, CharType aChar,PRInt32 aCount,PRInt32 anOffset) {
|
|
|
|
|
|
|
|
nsresult result=NS_OK;
|
|
|
|
|
|
|
|
if(anOffset<(PRInt32)aDest.mLength){
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *root_cp=aDest.mBuffer;
|
|
|
|
CharType *theCP=root_cp+anOffset;
|
|
|
|
CharType *end_cp=root_cp+aDest.mLength;
|
2000-03-09 13:50:02 +03:00
|
|
|
CharType *theWriteCP=theCP;
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(theCP<end_cp) {
|
2000-03-09 13:50:02 +03:00
|
|
|
if(*theCP!=aChar) {
|
|
|
|
*theWriteCP++=*theCP;
|
|
|
|
}
|
|
|
|
theCP++;
|
|
|
|
}
|
|
|
|
*theWriteCP=0;
|
|
|
|
aDest.mLength-=(theCP-theWriteCP);
|
|
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method removes a given char the underlying stringvalue
|
|
|
|
*
|
|
|
|
* @update rickg 03.01.2000
|
|
|
|
* @param aDest is the stringvalue to be modified
|
|
|
|
* @param anOffset tells us where in source to start copying
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aRepCount tells us the (max) # of iterations
|
2000-03-09 13:50:02 +03:00
|
|
|
*/
|
|
|
|
template <class CharType1,class CharType2>
|
|
|
|
nsresult SVReplace( nsStringValueImpl<CharType1> &aDest,
|
|
|
|
const nsStringValueImpl<CharType2> &aTarget,
|
|
|
|
const nsStringValueImpl<CharType2> &aNewValue,
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 aRepCount,
|
|
|
|
PRUint32 anOffset) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
nsresult result=NS_OK;
|
|
|
|
|
|
|
|
if((aTarget.mLength) && (aNewValue.mLength)) {
|
|
|
|
|
|
|
|
nsStringValueImpl<CharType1> theTempString;
|
|
|
|
PRUint32 thePrevOffset=0;
|
|
|
|
PRInt32 thePos=SVFind(aDest,aTarget,PR_FALSE,aDest.mLength,thePrevOffset);
|
|
|
|
|
|
|
|
while(kNotFound<thePos) {
|
|
|
|
if(0<thePos) {
|
|
|
|
SVAppend(theTempString,aDest,thePos-thePrevOffset,thePrevOffset);
|
|
|
|
}
|
|
|
|
SVAppend(theTempString,aNewValue,aNewValue.mLength,0);
|
|
|
|
thePrevOffset=thePos+aTarget.mLength;
|
|
|
|
thePos=SVFind(aDest,aTarget,PR_FALSE,aDest.mLength-thePrevOffset,thePrevOffset);
|
|
|
|
}
|
|
|
|
if(thePrevOffset<aDest.mLength) {
|
|
|
|
SVAppend(theTempString,aDest,aDest.mLength-thePrevOffset,thePrevOffset);
|
|
|
|
}
|
|
|
|
SVFree(aDest);
|
|
|
|
aDest=theTempString;
|
|
|
|
theTempString.mBuffer=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
2000-03-31 13:24:08 +04:00
|
|
|
* @param aNewChar
|
|
|
|
* @param aCount
|
|
|
|
* @param anoffset
|
2000-03-09 13:50:02 +03:00
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-31 13:24:08 +04:00
|
|
|
nsresult SVReplaceCharsInSet(nsStringValueImpl<CharType>& aDest,const char* aSet,PRUnichar aNewChar,PRInt32 aCount,PRUint32 anOffset) {
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
if((0<aDest.mLength) && (anOffset<aDest.mLength)){
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
PRInt32 theCount = (-1==aCount) ? aDest.mLength : aCount;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *root_cp=aDest.mBuffer;
|
|
|
|
CharType *offset_cp=root_cp+anOffset; //was theCP
|
|
|
|
CharType *last_cp=offset_cp+aCount;
|
|
|
|
CharType *null_cp=root_cp+theCount;
|
|
|
|
CharType *end_cp=(last_cp<null_cp) ? last_cp : null_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl<char> theSet((char*)aSet);
|
|
|
|
while(offset_cp<end_cp){
|
|
|
|
CharType theChar=*offset_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
PRInt32 thePos=SVFindChar(theSet,theChar,PR_FALSE,theSet.mLength,0);
|
|
|
|
if(kNotFound!=thePos) {
|
2000-03-31 13:24:08 +04:00
|
|
|
*offset_cp=(CharType)aNewChar;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
++offset_cp;
|
2000-03-09 13:50:02 +03:00
|
|
|
} //while
|
|
|
|
} //if
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 rickg 03.01.2000
|
|
|
|
* @param aDest
|
|
|
|
* @param aSet
|
|
|
|
* @param aEliminateLeading
|
|
|
|
* @param aEliminateTrailing
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
2000-03-31 13:24:08 +04:00
|
|
|
nsresult SVCompressSet(nsStringValueImpl<CharType>& aDest,const char* aSet, PRUnichar aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE){
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
nsresult result=NS_OK;
|
|
|
|
|
|
|
|
if(0<aDest.mLength){
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
result=SVReplaceCharsInSet(aDest, aSet, aChar, aDest.mLength,0);
|
|
|
|
SVTrim(aDest,aSet,aEliminateLeading,aEliminateTrailing);
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *root_cp = aDest.mBuffer;
|
|
|
|
CharType *end_cp = root_cp + aDest.mLength;
|
|
|
|
CharType *to_cp = root_cp;
|
|
|
|
|
|
|
|
nsStringValueImpl<char> theSet((char*)aSet);
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
//this code converts /n, /t, /r into normal space ' ';
|
|
|
|
//it also compresses runs of whitespace down to a single char...
|
|
|
|
PRUint32 aSetLen=strlen(aSet);
|
2000-03-31 13:24:08 +04:00
|
|
|
if(aSetLen) {
|
|
|
|
|
|
|
|
while (root_cp < end_cp) {
|
|
|
|
CharType theChar = *root_cp++;
|
|
|
|
|
|
|
|
*to_cp++=theChar; //always copy this char
|
|
|
|
if((theChar<256) && (kNotFound<SVFindChar(theSet,theChar,PR_FALSE,theSet.mLength,0))){
|
|
|
|
while (root_cp <= end_cp) {
|
|
|
|
theChar = *root_cp++;
|
|
|
|
PRInt32 thePos=SVFindChar(theSet,theChar,PR_FALSE,theSet.mLength,0);
|
|
|
|
if(kNotFound==thePos){
|
|
|
|
*to_cp++ = theChar;
|
|
|
|
break;
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
} //while
|
|
|
|
|
|
|
|
*to_cp = 0;
|
|
|
|
aDest.mLength = to_cp - (CharType*)aDest.mBuffer;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class CharType>
|
|
|
|
void SVToLowerCase(nsStringValueImpl<CharType> &aString) {
|
|
|
|
|
|
|
|
CharType *theCP=aString.mBuffer;
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *end_cp=theCP+aString.mLength;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(theCP<end_cp) {
|
2000-03-09 13:50:02 +03:00
|
|
|
CharType theChar=*theCP;
|
|
|
|
if ((theChar >= 'A') && (theChar <= 'Z')) {
|
|
|
|
*theCP = 'a' + (theChar - 'A');
|
|
|
|
}
|
|
|
|
theCP++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class CharType>
|
|
|
|
void SVToUpperCase(nsStringValueImpl<CharType> &aString) {
|
|
|
|
CharType *theCP=aString.mBuffer;
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType *end_cp=theCP+aString.mLength;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(theCP<end_cp) {
|
2000-03-09 13:50:02 +03:00
|
|
|
CharType theChar=*theCP;
|
|
|
|
if ((theChar>= 'a') && (theChar<= 'z')) {
|
|
|
|
*theCP= 'A' + (theChar - 'a');
|
|
|
|
}
|
|
|
|
theCP++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class CharType>
|
|
|
|
PRInt32 SVCountChar(const nsStringValueImpl<CharType> &aString,PRUnichar aChar) {
|
|
|
|
PRInt32 result=0;
|
2000-03-31 13:24:08 +04:00
|
|
|
|
|
|
|
const CharType* cp=aString.mBuffer;
|
|
|
|
const CharType* end_cp=cp+aString.mLength;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(cp<end_cp) {
|
|
|
|
if(*cp==aChar) {
|
|
|
|
result++;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
cp++;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//This will not work correctly for any unicode set other than ascii
|
|
|
|
template <class CharType>
|
|
|
|
void SVDebugDump(const nsStringValueImpl<CharType> &aString) {
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
CharType* cp=aString.mBuffer;
|
|
|
|
CharType* end_cp=cp+aString.mLength;
|
|
|
|
|
|
|
|
char theBuffer[256]={0};
|
|
|
|
char* outp=theBuffer;
|
|
|
|
char* outendp=&theBuffer[255];
|
|
|
|
|
|
|
|
while(cp<end_cp) {
|
|
|
|
if(outp==outendp) {
|
|
|
|
*outp=0; //write null
|
|
|
|
printf("%s",theBuffer);
|
|
|
|
outp=theBuffer;
|
|
|
|
cp--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*outp++=(char)*cp;
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
} //while
|
|
|
|
*outp=0; //force a null
|
|
|
|
printf("%s",theBuffer);
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform string to int conversion.
|
|
|
|
* @param aErrorCode will contain error if one occurs
|
|
|
|
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
|
|
|
* @return int rep of string value, and possible (out) error code
|
|
|
|
*/
|
|
|
|
template <class CharType>
|
|
|
|
PRInt32 SVToInteger(const nsStringValueImpl<CharType> &aString,PRInt32* anErrorCode,PRUint32 aRadix=kAutoDetect) {
|
|
|
|
|
|
|
|
CharType* cp=aString.mBuffer;
|
|
|
|
PRInt32 theRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
|
|
|
PRInt32 result=0;
|
|
|
|
PRBool negate=PR_FALSE;
|
|
|
|
PRUnichar theChar=0;
|
|
|
|
|
|
|
|
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
if(cp) {
|
|
|
|
|
|
|
|
//begin by skipping over leading chars that shouldn't be part of the number...
|
|
|
|
|
|
|
|
CharType* endcp=cp+aString.mLength;
|
|
|
|
PRBool done=PR_FALSE;
|
|
|
|
|
|
|
|
while((cp<endcp) && (!done)){
|
|
|
|
theChar=*cp;
|
|
|
|
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-03-31 13:24:08 +04:00
|
|
|
theRadix = (kAutoDetect==aRadix) ? theRadix : aRadix;
|
|
|
|
*anErrorCode = NS_OK;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
//now iterate the numeric chars and build our result
|
|
|
|
CharType* first=--cp; //in case we have to back up.
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
while(cp<endcp){
|
|
|
|
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;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-03-31 13:24:08 +04:00
|
|
|
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
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;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-03-31 13:24:08 +04:00
|
|
|
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2000-03-31 13:24:08 +04:00
|
|
|
result = (theRadix * result) + ((theChar-'a')+10);
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
}
|
|
|
|
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;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
return result;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
|