2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
/*************************************************************************************
|
|
|
|
*
|
|
|
|
* MODULES NOTES:
|
|
|
|
*
|
|
|
|
* 1. See nsStringImpl.h
|
|
|
|
* 2. We still have the ownership flag (bool) as a seperate data member. This will
|
|
|
|
* get merged into the length or capacity field after things stabilize a bit.
|
|
|
|
* (Likely only a few more days).
|
|
|
|
*
|
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)
|
|
|
|
*
|
|
|
|
*************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef NSSTRINGVALUE_
|
|
|
|
#define NSSTRINGVALUE_
|
2000-03-31 13:24:08 +04:00
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsIAllocator.h"
|
2000-03-03 12:04:50 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#if 0
|
2000-03-03 12:04:50 +03:00
|
|
|
typedef int PRInt32;
|
|
|
|
typedef unsigned int PRUint32;
|
|
|
|
typedef short int PRUnichar;
|
|
|
|
typedef int nsresult;
|
|
|
|
typedef char PRBool;
|
2000-03-31 13:24:08 +04:00
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
#define NS_OK 0
|
2000-03-09 13:50:02 +03:00
|
|
|
#define NS_MEMORY_ERROR 1000
|
2000-03-03 12:04:50 +03:00
|
|
|
#define PR_TRUE 1
|
|
|
|
#define PR_FALSE 0
|
2000-03-31 13:24:08 +04:00
|
|
|
#define NS_SUCCEEDED(x) (NS_OK==x)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
typedef long int _RefCountType;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
static const int kRadix10=10;
|
|
|
|
static const int kRadix16=16;
|
|
|
|
static const int kAutoDetect=100;
|
|
|
|
static const int kRadixUnknown=kAutoDetect+1;
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
static const int kDefaultStringSize=64;
|
2000-03-09 13:50:02 +03:00
|
|
|
static const int kNotFound=-1;
|
2000-03-31 13:24:08 +04:00
|
|
|
#define IGNORE_CASE (PR_TRUE)
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
class nsAReadableString {
|
2000-03-09 13:50:02 +03:00
|
|
|
public:
|
2000-03-31 13:24:08 +04:00
|
|
|
virtual PRUint32 Length() const =0;
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
};
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
#define MODERN_CPP //make this false for aix/solaris...
|
|
|
|
|
|
|
|
#ifdef MODERN_CPP
|
|
|
|
#define CONST_CAST(type,arg) const_cast<type>(arg)
|
|
|
|
#else
|
|
|
|
#define CONST_CAST(type,arg) (type)arg
|
|
|
|
#endif
|
2000-03-09 13:50:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
*
|
2000-03-31 13:24:08 +04:00
|
|
|
* The following (hack) template functions will go away, but for now they
|
|
|
|
* provide basic string copying and appending for my prototype. (rickg)
|
2000-03-09 13:50:02 +03:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
template <class T1,class T2>
|
|
|
|
inline PRInt32 MinInt(T1 anInt1,T2 anInt2){
|
|
|
|
return (anInt1<(T1)anInt2) ? anInt1 : anInt2;
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
template <class T1,class T2>
|
|
|
|
inline PRInt32 MaxInt(T1 anInt1,T2 anInt2){
|
|
|
|
return (anInt1<(T1)anInt2) ? anInt2 : anInt1;
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
inline size_t stringlen(const PRUnichar* theString) {
|
|
|
|
const PRUnichar* theEnd=theString;
|
|
|
|
while(*theEnd++); //skip to end of aDest...
|
|
|
|
size_t result=theEnd-theString-1;
|
|
|
|
return result;
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
inline size_t stringlen(const char* theString) {
|
|
|
|
return ::strlen(theString);
|
|
|
|
}
|
2000-03-09 13:50:02 +03:00
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2000-03-09 13:50:02 +03:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* This is the templatized base class from which stringvalues are derived. (rickg)
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
template <class CharType>
|
|
|
|
struct nsStackBuffer {
|
|
|
|
nsStackBuffer(CharType *aBuffer,PRUint32 aLength,PRUint32 aCapacity) {
|
|
|
|
mBuffer=aBuffer;
|
|
|
|
mLength=aLength;
|
|
|
|
mCapacity=aCapacity;
|
|
|
|
}
|
|
|
|
CharType* mBuffer;
|
|
|
|
PRUint32 mLength;
|
|
|
|
PRUint32 mCapacity;
|
|
|
|
};
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
template <class CharType>
|
|
|
|
struct RCBuffer {
|
|
|
|
|
|
|
|
void* operator new(size_t aSize) {
|
|
|
|
CharType* theBuf = ::new CharType[aSize+1+sizeof(_RefCountType)];
|
|
|
|
memset(theBuf,0xff,aSize+additionalBytes+1);
|
|
|
|
return theBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void *p, size_t aSize) {
|
|
|
|
::delete [] p;
|
|
|
|
}
|
|
|
|
|
|
|
|
_RefCountType mRefCount;
|
|
|
|
CharType mBuffer[1];
|
|
|
|
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-03-03 12:04:50 +03:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* This is the templatized base class from which stringvalues are derived. (rickg)
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
template <class CharType>
|
2000-03-31 13:24:08 +04:00
|
|
|
struct nsStringValueImpl {
|
2000-03-03 12:04:50 +03:00
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl() {
|
2000-04-05 10:11:03 +04:00
|
|
|
mBuffer=(CharType*)gCommonEmptyBuffer;
|
2000-03-09 13:50:02 +03:00
|
|
|
mLength=mCapacity=0;
|
2000-04-05 10:11:03 +04:00
|
|
|
mRefCount=2; //so we don't ever try to free the shared buffer
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl(const nsStringValueImpl<CharType>& aCopy) {
|
2000-03-09 13:50:02 +03:00
|
|
|
mBuffer=aCopy.mBuffer;
|
|
|
|
mLength=aCopy.mLength;
|
|
|
|
mCapacity=aCopy.mCapacity;
|
2000-03-31 13:24:08 +04:00
|
|
|
mRefCount=1;
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl(const nsStringValueImpl<CharType>& aCopy,PRInt32 aLength){
|
2000-03-09 13:50:02 +03:00
|
|
|
mBuffer=aCopy.mBuffer;
|
|
|
|
mLength=aLength;
|
|
|
|
mCapacity=aCopy.mCapacity;
|
2000-03-31 13:24:08 +04:00
|
|
|
mRefCount=1;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl(CharType* theString,PRInt32 aLength=-1,PRUint32 aCapacity=0) {
|
2000-03-03 12:04:50 +03:00
|
|
|
if(theString) {
|
|
|
|
mLength = (-1==aLength) ? stringlen(theString) : aLength;
|
2000-03-31 13:24:08 +04:00
|
|
|
mCapacity=(0==aCapacity) ? mLength+1 : aCapacity;
|
2000-03-03 12:04:50 +03:00
|
|
|
mBuffer=theString;
|
|
|
|
}
|
|
|
|
else {
|
2000-04-05 10:11:03 +04:00
|
|
|
mBuffer=(CharType*)gCommonEmptyBuffer;
|
2000-03-09 13:50:02 +03:00
|
|
|
mLength=mCapacity=0;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
2000-03-31 13:24:08 +04:00
|
|
|
mRefCount=1;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
nsStringValueImpl(const nsStackBuffer<CharType> &aBuffer) {
|
2000-03-09 13:50:02 +03:00
|
|
|
mCapacity=aBuffer.mCapacity;
|
|
|
|
mLength=aBuffer.mLength;
|
|
|
|
mBuffer=aBuffer.mBuffer;
|
2000-03-31 13:24:08 +04:00
|
|
|
mRefCount=2; //set it to 2 so we don't try to free it.
|
2000-03-09 13:50:02 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
void operator=(const nsStringValueImpl<CharType>& aCopy) {
|
2000-03-03 12:04:50 +03:00
|
|
|
mBuffer=aCopy.mBuffer;
|
|
|
|
mLength=aCopy.mLength;
|
|
|
|
mCapacity=aCopy.mCapacity;
|
2000-03-31 13:24:08 +04:00
|
|
|
mRefCount=aCopy.mRefCount;
|
2000-03-03 12:04:50 +03:00
|
|
|
}
|
|
|
|
|
2000-03-31 13:24:08 +04:00
|
|
|
void* GetBuffer() {return mBuffer;}
|
|
|
|
PRUint32 GetLength() {return mLength;}
|
2000-04-05 10:11:03 +04:00
|
|
|
PRUint32 GetCapacity() {return mCapacity;}
|
2000-03-31 13:24:08 +04:00
|
|
|
size_t GetCharSize() {return sizeof(CharType);}
|
2000-03-03 12:04:50 +03:00
|
|
|
|
|
|
|
CharType* mBuffer;
|
2000-03-09 13:50:02 +03:00
|
|
|
PRUint32 mLength;
|
|
|
|
PRUint32 mCapacity;
|
2000-03-31 13:24:08 +04:00
|
|
|
PRUint32 mRefCount;
|
2000-03-03 12:04:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|