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