michael@0: // Common/String.h michael@0: michael@0: #ifndef __COMMON_STRING_H michael@0: #define __COMMON_STRING_H michael@0: michael@0: #include michael@0: // #include michael@0: michael@0: #include "Vector.h" michael@0: michael@0: #ifdef _WIN32 michael@0: #include "MyWindows.h" michael@0: #endif michael@0: michael@0: static const char *kTrimDefaultCharSet = " \n\t"; michael@0: michael@0: template michael@0: inline int MyStringLen(const T *s) michael@0: { michael@0: int i; michael@0: for (i = 0; s[i] != '\0'; i++); michael@0: return i; michael@0: } michael@0: michael@0: template michael@0: inline T * MyStringCopy(T *dest, const T *src) michael@0: { michael@0: T *destStart = dest; michael@0: while((*dest++ = *src++) != 0); michael@0: return destStart; michael@0: } michael@0: michael@0: inline wchar_t* MyStringGetNextCharPointer(wchar_t *p) michael@0: { return (p + 1); } michael@0: inline const wchar_t* MyStringGetNextCharPointer(const wchar_t *p) michael@0: { return (p + 1); } michael@0: inline wchar_t* MyStringGetPrevCharPointer(const wchar_t *, wchar_t *p) michael@0: { return (p - 1); } michael@0: inline const wchar_t* MyStringGetPrevCharPointer(const wchar_t *, const wchar_t *p) michael@0: { return (p - 1); } michael@0: michael@0: #ifdef _WIN32 michael@0: michael@0: inline char* MyStringGetNextCharPointer(char *p) michael@0: { return CharNextA(p); } michael@0: inline const char* MyStringGetNextCharPointer(const char *p) michael@0: { return CharNextA(p); } michael@0: michael@0: inline char* MyStringGetPrevCharPointer(char *base, char *p) michael@0: { return CharPrevA(base, p); } michael@0: inline const char* MyStringGetPrevCharPointer(const char *base, const char *p) michael@0: { return CharPrevA(base, p); } michael@0: michael@0: inline char MyCharUpper(char c) michael@0: { return (char)(unsigned int)CharUpperA((LPSTR)(unsigned int)(unsigned char)c); } michael@0: #ifdef _UNICODE michael@0: inline wchar_t MyCharUpper(wchar_t c) michael@0: { return (wchar_t)CharUpperW((LPWSTR)c); } michael@0: #else michael@0: wchar_t MyCharUpper(wchar_t c); michael@0: #endif michael@0: michael@0: inline char MyCharLower(char c) michael@0: { return (char)(unsigned int)CharLowerA((LPSTR)(unsigned int)(unsigned char)c); } michael@0: #ifdef _UNICODE michael@0: inline wchar_t MyCharLower(wchar_t c) michael@0: { return (wchar_t)CharLowerW((LPWSTR)c); } michael@0: #else michael@0: wchar_t MyCharLower(wchar_t c); michael@0: #endif michael@0: michael@0: inline char * MyStringUpper(char *s) { return CharUpperA(s); } michael@0: #ifdef _UNICODE michael@0: inline wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); } michael@0: #else michael@0: wchar_t * MyStringUpper(wchar_t *s); michael@0: #endif michael@0: michael@0: inline char * MyStringLower(char *s) { return CharLowerA(s); } michael@0: #ifdef _UNICODE michael@0: inline wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); } michael@0: #else michael@0: wchar_t * MyStringLower(wchar_t *s); michael@0: #endif michael@0: michael@0: #else // Standard-C michael@0: wchar_t MyCharUpper(wchar_t c); michael@0: #endif michael@0: michael@0: ////////////////////////////////////// michael@0: // Compare michael@0: michael@0: /* michael@0: #ifndef _WIN32_WCE michael@0: int MyStringCollate(const char *s1, const char *s2); michael@0: int MyStringCollateNoCase(const char *s1, const char *s2); michael@0: #endif michael@0: int MyStringCollate(const wchar_t *s1, const wchar_t *s2); michael@0: int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2); michael@0: */ michael@0: michael@0: int MyStringCompare(const char *s1, const char *s2); michael@0: int MyStringCompare(const wchar_t *s1, const wchar_t *s2); michael@0: michael@0: #ifdef _WIN32 michael@0: int MyStringCompareNoCase(const char *s1, const char *s2); michael@0: #endif michael@0: michael@0: int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2); michael@0: michael@0: template michael@0: class CStringBase michael@0: { michael@0: void TrimLeftWithCharSet(const CStringBase &charSet) michael@0: { michael@0: const T *p = _chars; michael@0: while (charSet.Find(*p) >= 0 && (*p != 0)) michael@0: p = GetNextCharPointer(p); michael@0: Delete(0, (int)(p - _chars)); michael@0: } michael@0: void TrimRightWithCharSet(const CStringBase &charSet) michael@0: { michael@0: const T *p = _chars; michael@0: const T *pLast = NULL; michael@0: while (*p != 0) michael@0: { michael@0: if (charSet.Find(*p) >= 0) michael@0: { michael@0: if (pLast == NULL) michael@0: pLast = p; michael@0: } michael@0: else michael@0: pLast = NULL; michael@0: p = GetNextCharPointer(p); michael@0: } michael@0: if(pLast != NULL) michael@0: { michael@0: int i = (int)(pLast - _chars); michael@0: Delete(i, _length - i); michael@0: } michael@0: michael@0: } michael@0: void MoveItems(int destIndex, int srcIndex) michael@0: { michael@0: memmove(_chars + destIndex, _chars + srcIndex, michael@0: sizeof(T) * (_length - srcIndex + 1)); michael@0: } michael@0: michael@0: void InsertSpace(int &index, int size) michael@0: { michael@0: CorrectIndex(index); michael@0: GrowLength(size); michael@0: MoveItems(index + size, index); michael@0: } michael@0: michael@0: static T *GetNextCharPointer(T *p) michael@0: { return MyStringGetNextCharPointer(p); } michael@0: static const T *GetNextCharPointer(const T *p) michael@0: { return MyStringGetNextCharPointer(p); } michael@0: static T *GetPrevCharPointer(T *base, T *p) michael@0: { return MyStringGetPrevCharPointer(base, p); } michael@0: static const T *GetPrevCharPointer(const T *base, const T *p) michael@0: { return MyStringGetPrevCharPointer(base, p); } michael@0: protected: michael@0: T *_chars; michael@0: int _length; michael@0: int _capacity; michael@0: michael@0: void SetCapacity(int newCapacity) michael@0: { michael@0: int realCapacity = newCapacity + 1; michael@0: if(realCapacity == _capacity) michael@0: return; michael@0: /* michael@0: const int kMaxStringSize = 0x20000000; michael@0: #ifndef _WIN32_WCE michael@0: if(newCapacity > kMaxStringSize || newCapacity < _length) michael@0: throw 1052337; michael@0: #endif michael@0: */ michael@0: T *newBuffer = new T[realCapacity]; michael@0: if(_capacity > 0) michael@0: { michael@0: for (int i = 0; i < (_length + 1); i++) michael@0: newBuffer[i] = _chars[i]; michael@0: delete []_chars; michael@0: _chars = newBuffer; michael@0: } michael@0: else michael@0: { michael@0: _chars = newBuffer; michael@0: _chars[0] = 0; michael@0: } michael@0: _capacity = realCapacity; michael@0: } michael@0: michael@0: void GrowLength(int n) michael@0: { michael@0: int freeSize = _capacity - _length - 1; michael@0: if (n <= freeSize) michael@0: return; michael@0: int delta; michael@0: if (_capacity > 64) michael@0: delta = _capacity / 2; michael@0: else if (_capacity > 8) michael@0: delta = 16; michael@0: else michael@0: delta = 4; michael@0: if (freeSize + delta < n) michael@0: delta = n - freeSize; michael@0: SetCapacity(_capacity + delta); michael@0: } michael@0: michael@0: void CorrectIndex(int &index) const michael@0: { michael@0: if (index > _length) michael@0: index = _length; michael@0: } michael@0: michael@0: public: michael@0: CStringBase(): _chars(0), _length(0), _capacity(0) michael@0: { SetCapacity(16 - 1); } michael@0: CStringBase(T c): _chars(0), _length(0), _capacity(0) michael@0: { michael@0: SetCapacity(1); michael@0: _chars[0] = c; michael@0: _chars[1] = 0; michael@0: _length = 1; michael@0: } michael@0: CStringBase(const T *chars): _chars(0), _length(0), _capacity(0) michael@0: { michael@0: int length = MyStringLen(chars); michael@0: SetCapacity(length); michael@0: MyStringCopy(_chars, chars); // can be optimized by memove() michael@0: _length = length; michael@0: } michael@0: CStringBase(const CStringBase &s): _chars(0), _length(0), _capacity(0) michael@0: { michael@0: SetCapacity(s._length); michael@0: MyStringCopy(_chars, s._chars); michael@0: _length = s._length; michael@0: } michael@0: ~CStringBase() { delete []_chars; } michael@0: michael@0: operator const T*() const { return _chars;} michael@0: michael@0: // The minimum size of the character buffer in characters. michael@0: // This value does not include space for a null terminator. michael@0: T* GetBuffer(int minBufLength) michael@0: { michael@0: if(minBufLength >= _capacity) michael@0: SetCapacity(minBufLength + 1); michael@0: return _chars; michael@0: } michael@0: void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); } michael@0: void ReleaseBuffer(int newLength) michael@0: { michael@0: /* michael@0: #ifndef _WIN32_WCE michael@0: if(newLength >= _capacity) michael@0: throw 282217; michael@0: #endif michael@0: */ michael@0: _chars[newLength] = 0; michael@0: _length = newLength; michael@0: } michael@0: michael@0: CStringBase& operator=(T c) michael@0: { michael@0: Empty(); michael@0: SetCapacity(1); michael@0: _chars[0] = c; michael@0: _chars[1] = 0; michael@0: _length = 1; michael@0: return *this; michael@0: } michael@0: CStringBase& operator=(const T *chars) michael@0: { michael@0: Empty(); michael@0: int length = MyStringLen(chars); michael@0: SetCapacity(length); michael@0: MyStringCopy(_chars, chars); michael@0: _length = length; michael@0: return *this; michael@0: } michael@0: CStringBase& operator=(const CStringBase& s) michael@0: { michael@0: if(&s == this) michael@0: return *this; michael@0: Empty(); michael@0: SetCapacity(s._length); michael@0: MyStringCopy(_chars, s._chars); michael@0: _length = s._length; michael@0: return *this; michael@0: } michael@0: michael@0: CStringBase& operator+=(T c) michael@0: { michael@0: GrowLength(1); michael@0: _chars[_length] = c; michael@0: _chars[++_length] = 0; michael@0: return *this; michael@0: } michael@0: CStringBase& operator+=(const T *s) michael@0: { michael@0: int len = MyStringLen(s); michael@0: GrowLength(len); michael@0: MyStringCopy(_chars + _length, s); michael@0: _length += len; michael@0: return *this; michael@0: } michael@0: CStringBase& operator+=(const CStringBase &s) michael@0: { michael@0: GrowLength(s._length); michael@0: MyStringCopy(_chars + _length, s._chars); michael@0: _length += s._length; michael@0: return *this; michael@0: } michael@0: void Empty() michael@0: { michael@0: _length = 0; michael@0: _chars[0] = 0; michael@0: } michael@0: int Length() const { return _length; } michael@0: bool IsEmpty() const { return (_length == 0); } michael@0: michael@0: CStringBase Mid(int startIndex) const michael@0: { return Mid(startIndex, _length - startIndex); } michael@0: CStringBase Mid(int startIndex, int count ) const michael@0: { michael@0: if (startIndex + count > _length) michael@0: count = _length - startIndex; michael@0: michael@0: if (startIndex == 0 && startIndex + count == _length) michael@0: return *this; michael@0: michael@0: CStringBase result; michael@0: result.SetCapacity(count); michael@0: // MyStringNCopy(result._chars, _chars + startIndex, count); michael@0: for (int i = 0; i < count; i++) michael@0: result._chars[i] = _chars[startIndex + i]; michael@0: result._chars[count] = 0; michael@0: result._length = count; michael@0: return result; michael@0: } michael@0: CStringBase Left(int count) const michael@0: { return Mid(0, count); } michael@0: CStringBase Right(int count) const michael@0: { michael@0: if (count > _length) michael@0: count = _length; michael@0: return Mid(_length - count, count); michael@0: } michael@0: michael@0: void MakeUpper() michael@0: { MyStringUpper(_chars); } michael@0: void MakeLower() michael@0: { MyStringLower(_chars); } michael@0: michael@0: int Compare(const CStringBase& s) const michael@0: { return MyStringCompare(_chars, s._chars); } michael@0: michael@0: int CompareNoCase(const CStringBase& s) const michael@0: { return MyStringCompareNoCase(_chars, s._chars); } michael@0: /* michael@0: int Collate(const CStringBase& s) const michael@0: { return MyStringCollate(_chars, s._chars); } michael@0: int CollateNoCase(const CStringBase& s) const michael@0: { return MyStringCollateNoCase(_chars, s._chars); } michael@0: */ michael@0: michael@0: int Find(T c) const { return Find(c, 0); } michael@0: int Find(T c, int startIndex) const michael@0: { michael@0: T *p = _chars + startIndex; michael@0: while (true) michael@0: { michael@0: if (*p == c) michael@0: return (int)(p - _chars); michael@0: if (*p == 0) michael@0: return -1; michael@0: p = GetNextCharPointer(p); michael@0: } michael@0: } michael@0: int Find(const CStringBase &s) const { return Find(s, 0); } michael@0: int Find(const CStringBase &s, int startIndex) const michael@0: { michael@0: if (s.IsEmpty()) michael@0: return startIndex; michael@0: for (; startIndex < _length; startIndex++) michael@0: { michael@0: int j; michael@0: for (j = 0; j < s._length && startIndex + j < _length; j++) michael@0: if (_chars[startIndex+j] != s._chars[j]) michael@0: break; michael@0: if (j == s._length) michael@0: return startIndex; michael@0: } michael@0: return -1; michael@0: } michael@0: int ReverseFind(T c) const michael@0: { michael@0: if (_length == 0) michael@0: return -1; michael@0: T *p = _chars + _length - 1; michael@0: while (true) michael@0: { michael@0: if (*p == c) michael@0: return (int)(p - _chars); michael@0: if (p == _chars) michael@0: return -1; michael@0: p = GetPrevCharPointer(_chars, p); michael@0: } michael@0: } michael@0: int FindOneOf(const CStringBase &s) const michael@0: { michael@0: for(int i = 0; i < _length; i++) michael@0: if (s.Find(_chars[i]) >= 0) michael@0: return i; michael@0: return -1; michael@0: } michael@0: michael@0: void TrimLeft(T c) michael@0: { michael@0: const T *p = _chars; michael@0: while (c == *p) michael@0: p = GetNextCharPointer(p); michael@0: Delete(0, p - _chars); michael@0: } michael@0: private: michael@0: CStringBase GetTrimDefaultCharSet() michael@0: { michael@0: CStringBase charSet; michael@0: for(int i = 0; i < (int)(sizeof(kTrimDefaultCharSet) / michael@0: sizeof(kTrimDefaultCharSet[0])); i++) michael@0: charSet += (T)kTrimDefaultCharSet[i]; michael@0: return charSet; michael@0: } michael@0: public: michael@0: michael@0: void TrimLeft() michael@0: { michael@0: TrimLeftWithCharSet(GetTrimDefaultCharSet()); michael@0: } michael@0: void TrimRight() michael@0: { michael@0: TrimRightWithCharSet(GetTrimDefaultCharSet()); michael@0: } michael@0: void TrimRight(T c) michael@0: { michael@0: const T *p = _chars; michael@0: const T *pLast = NULL; michael@0: while (*p != 0) michael@0: { michael@0: if (*p == c) michael@0: { michael@0: if (pLast == NULL) michael@0: pLast = p; michael@0: } michael@0: else michael@0: pLast = NULL; michael@0: p = GetNextCharPointer(p); michael@0: } michael@0: if(pLast != NULL) michael@0: { michael@0: int i = pLast - _chars; michael@0: Delete(i, _length - i); michael@0: } michael@0: } michael@0: void Trim() michael@0: { michael@0: TrimRight(); michael@0: TrimLeft(); michael@0: } michael@0: michael@0: int Insert(int index, T c) michael@0: { michael@0: InsertSpace(index, 1); michael@0: _chars[index] = c; michael@0: _length++; michael@0: return _length; michael@0: } michael@0: int Insert(int index, const CStringBase &s) michael@0: { michael@0: CorrectIndex(index); michael@0: if (s.IsEmpty()) michael@0: return _length; michael@0: int numInsertChars = s.Length(); michael@0: InsertSpace(index, numInsertChars); michael@0: for(int i = 0; i < numInsertChars; i++) michael@0: _chars[index + i] = s[i]; michael@0: _length += numInsertChars; michael@0: return _length; michael@0: } michael@0: michael@0: // !!!!!!!!!!!!!!! test it if newChar = '\0' michael@0: int Replace(T oldChar, T newChar) michael@0: { michael@0: if (oldChar == newChar) michael@0: return 0; michael@0: int number = 0; michael@0: int pos = 0; michael@0: while (pos < Length()) michael@0: { michael@0: pos = Find(oldChar, pos); michael@0: if (pos < 0) michael@0: break; michael@0: _chars[pos] = newChar; michael@0: pos++; michael@0: number++; michael@0: } michael@0: return number; michael@0: } michael@0: int Replace(const CStringBase &oldString, const CStringBase &newString) michael@0: { michael@0: if (oldString.IsEmpty()) michael@0: return 0; michael@0: if (oldString == newString) michael@0: return 0; michael@0: int oldStringLength = oldString.Length(); michael@0: int newStringLength = newString.Length(); michael@0: int number = 0; michael@0: int pos = 0; michael@0: while (pos < _length) michael@0: { michael@0: pos = Find(oldString, pos); michael@0: if (pos < 0) michael@0: break; michael@0: Delete(pos, oldStringLength); michael@0: Insert(pos, newString); michael@0: pos += newStringLength; michael@0: number++; michael@0: } michael@0: return number; michael@0: } michael@0: int Delete(int index, int count = 1 ) michael@0: { michael@0: if (index + count > _length) michael@0: count = _length - index; michael@0: if (count > 0) michael@0: { michael@0: MoveItems(index, index + count); michael@0: _length -= count; michael@0: } michael@0: return _length; michael@0: } michael@0: }; michael@0: michael@0: template michael@0: CStringBase operator+(const CStringBase& s1, const CStringBase& s2) michael@0: { michael@0: CStringBase result(s1); michael@0: result += s2; michael@0: return result; michael@0: } michael@0: michael@0: template michael@0: CStringBase operator+(const CStringBase& s, T c) michael@0: { michael@0: CStringBase result(s); michael@0: result += c; michael@0: return result; michael@0: } michael@0: michael@0: template michael@0: CStringBase operator+(T c, const CStringBase& s) michael@0: { michael@0: CStringBase result(c); michael@0: result += s; michael@0: return result; michael@0: } michael@0: michael@0: template michael@0: CStringBase operator+(const CStringBase& s, const T * chars) michael@0: { michael@0: CStringBase result(s); michael@0: result += chars; michael@0: return result; michael@0: } michael@0: michael@0: template michael@0: CStringBase operator+(const T * chars, const CStringBase& s) michael@0: { michael@0: CStringBase result(chars); michael@0: result += s; michael@0: return result; michael@0: } michael@0: michael@0: template michael@0: bool operator==(const CStringBase& s1, const CStringBase& s2) michael@0: { return (s1.Compare(s2) == 0); } michael@0: michael@0: template michael@0: bool operator<(const CStringBase& s1, const CStringBase& s2) michael@0: { return (s1.Compare(s2) < 0); } michael@0: michael@0: template michael@0: bool operator==(const T *s1, const CStringBase& s2) michael@0: { return (s2.Compare(s1) == 0); } michael@0: michael@0: template michael@0: bool operator==(const CStringBase& s1, const T *s2) michael@0: { return (s1.Compare(s2) == 0); } michael@0: michael@0: template michael@0: bool operator!=(const CStringBase& s1, const CStringBase& s2) michael@0: { return (s1.Compare(s2) != 0); } michael@0: michael@0: template michael@0: bool operator!=(const T *s1, const CStringBase& s2) michael@0: { return (s2.Compare(s1) != 0); } michael@0: michael@0: template michael@0: bool operator!=(const CStringBase& s1, const T *s2) michael@0: { return (s1.Compare(s2) != 0); } michael@0: michael@0: typedef CStringBase AString; michael@0: typedef CStringBase UString; michael@0: michael@0: typedef CObjectVector AStringVector; michael@0: typedef CObjectVector UStringVector; michael@0: michael@0: #ifdef _UNICODE michael@0: typedef UString CSysString; michael@0: #else michael@0: typedef AString CSysString; michael@0: #endif michael@0: michael@0: typedef CObjectVector CSysStringVector; michael@0: michael@0: #endif