3 #ifndef __COMMON_STRING_H
4 #define __COMMON_STRING_H
12 #include "MyWindows.h"
16 inline int MyStringLen(const T *s)
19 for (i = 0; s[i] != '\0'; i++);
24 inline T * MyStringCopy(T *dest, const T *src)
27 while((*dest++ = *src++) != 0);
31 inline wchar_t* MyStringGetNextCharPointer(wchar_t *p)
33 inline const wchar_t* MyStringGetNextCharPointer(const wchar_t *p)
35 inline wchar_t* MyStringGetPrevCharPointer(const wchar_t *, wchar_t *p)
37 inline const wchar_t* MyStringGetPrevCharPointer(const wchar_t *, const wchar_t *p)
42 inline char* MyStringGetNextCharPointer(char *p)
43 { return CharNextA(p); }
44 inline const char* MyStringGetNextCharPointer(const char *p)
45 { return CharNextA(p); }
47 inline char* MyStringGetPrevCharPointer(char *base, char *p)
48 { return CharPrevA(base, p); }
49 inline const char* MyStringGetPrevCharPointer(const char *base, const char *p)
50 { return CharPrevA(base, p); }
52 inline char MyCharUpper(char c)
53 { return (char)(unsigned int)(UINT_PTR)CharUpperA((LPSTR)(UINT_PTR)(unsigned int)(unsigned char)c); }
55 inline wchar_t MyCharUpper(wchar_t c)
56 { return (wchar_t)(unsigned int)(UINT_PTR)CharUpperW((LPWSTR)(UINT_PTR)(unsigned int)c); }
58 wchar_t MyCharUpper(wchar_t c);
61 inline char MyCharLower(char c)
62 { return (char)(unsigned int)(UINT_PTR)CharLowerA((LPSTR)(UINT_PTR)(unsigned int)(unsigned char)c); }
64 inline wchar_t MyCharLower(wchar_t c)
65 { return (wchar_t)(unsigned int)(UINT_PTR)CharLowerW((LPWSTR)(UINT_PTR)(unsigned int)c); }
67 wchar_t MyCharLower(wchar_t c);
70 inline char * MyStringUpper(char *s) { return CharUpperA(s); }
72 inline wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); }
74 wchar_t * MyStringUpper(wchar_t *s);
77 inline char * MyStringLower(char *s) { return CharLowerA(s); }
79 inline wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); }
81 wchar_t * MyStringLower(wchar_t *s);
85 wchar_t MyCharUpper(wchar_t c);
88 //////////////////////////////////////
93 int MyStringCollate(const char *s1, const char *s2);
94 int MyStringCollateNoCase(const char *s1, const char *s2);
96 int MyStringCollate(const wchar_t *s1, const wchar_t *s2);
97 int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2);
100 int MyStringCompare(const char *s1, const char *s2);
101 int MyStringCompare(const wchar_t *s1, const wchar_t *s2);
104 int MyStringCompareNoCase(const char *s1, const char *s2);
107 int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2);
112 void TrimLeftWithCharSet(const CStringBase &charSet)
115 while (charSet.Find(*p) >= 0 && (*p != 0))
116 p = GetNextCharPointer(p);
117 Delete(0, (int)(p - _chars));
119 void TrimRightWithCharSet(const CStringBase &charSet)
122 const T *pLast = NULL;
125 if (charSet.Find(*p) >= 0)
132 p = GetNextCharPointer(p);
136 int i = (int)(pLast - _chars);
137 Delete(i, _length - i);
141 void MoveItems(int destIndex, int srcIndex)
143 memmove(_chars + destIndex, _chars + srcIndex,
144 sizeof(T) * (_length - srcIndex + 1));
147 void InsertSpace(int &index, int size)
151 MoveItems(index + size, index);
154 static T *GetNextCharPointer(T *p)
155 { return MyStringGetNextCharPointer(p); }
156 static const T *GetNextCharPointer(const T *p)
157 { return MyStringGetNextCharPointer(p); }
158 static T *GetPrevCharPointer(T *base, T *p)
159 { return MyStringGetPrevCharPointer(base, p); }
160 static const T *GetPrevCharPointer(const T *base, const T *p)
161 { return MyStringGetPrevCharPointer(base, p); }
167 void SetCapacity(int newCapacity)
169 int realCapacity = newCapacity + 1;
170 if(realCapacity == _capacity)
173 const int kMaxStringSize = 0x20000000;
175 if(newCapacity > kMaxStringSize || newCapacity < _length)
179 T *newBuffer = new T[realCapacity];
182 for (int i = 0; i < (_length + 1); i++)
183 newBuffer[i] = _chars[i];
192 _capacity = realCapacity;
195 void GrowLength(int n)
197 int freeSize = _capacity - _length - 1;
202 delta = _capacity / 2;
203 else if (_capacity > 8)
207 if (freeSize + delta < n)
208 delta = n - freeSize;
209 SetCapacity(_capacity + delta);
212 void CorrectIndex(int &index) const
219 CStringBase(): _chars(0), _length(0), _capacity(0)
220 { SetCapacity(16 - 1); }
221 CStringBase(T c): _chars(0), _length(0), _capacity(0)
228 CStringBase(const T *chars): _chars(0), _length(0), _capacity(0)
230 int length = MyStringLen(chars);
232 MyStringCopy(_chars, chars); // can be optimized by memove()
235 CStringBase(const CStringBase &s): _chars(0), _length(0), _capacity(0)
237 SetCapacity(s._length);
238 MyStringCopy(_chars, s._chars);
241 ~CStringBase() { delete []_chars; }
243 operator const T*() const { return _chars;}
245 // The minimum size of the character buffer in characters.
246 // This value does not include space for a null terminator.
247 T* GetBuffer(int minBufLength)
249 if(minBufLength >= _capacity)
250 SetCapacity(minBufLength + 1);
253 void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); }
254 void ReleaseBuffer(int newLength)
258 if(newLength >= _capacity)
262 _chars[newLength] = 0;
266 CStringBase& operator=(T c)
275 CStringBase& operator=(const T *chars)
278 int length = MyStringLen(chars);
280 MyStringCopy(_chars, chars);
284 CStringBase& operator=(const CStringBase& s)
289 SetCapacity(s._length);
290 MyStringCopy(_chars, s._chars);
295 CStringBase& operator+=(T c)
299 _chars[++_length] = 0;
302 CStringBase& operator+=(const T *s)
304 int len = MyStringLen(s);
306 MyStringCopy(_chars + _length, s);
310 CStringBase& operator+=(const CStringBase &s)
312 GrowLength(s._length);
313 MyStringCopy(_chars + _length, s._chars);
314 _length += s._length;
322 int Length() const { return _length; }
323 bool IsEmpty() const { return (_length == 0); }
325 CStringBase Mid(int startIndex) const
326 { return Mid(startIndex, _length - startIndex); }
327 CStringBase Mid(int startIndex, int count ) const
329 if (startIndex + count > _length)
330 count = _length - startIndex;
332 if (startIndex == 0 && startIndex + count == _length)
335 CStringBase<T> result;
336 result.SetCapacity(count);
337 // MyStringNCopy(result._chars, _chars + startIndex, count);
338 for (int i = 0; i < count; i++)
339 result._chars[i] = _chars[startIndex + i];
340 result._chars[count] = 0;
341 result._length = count;
344 CStringBase Left(int count) const
345 { return Mid(0, count); }
346 CStringBase Right(int count) const
350 return Mid(_length - count, count);
354 { MyStringUpper(_chars); }
356 { MyStringLower(_chars); }
358 int Compare(const CStringBase& s) const
359 { return MyStringCompare(_chars, s._chars); }
361 int Compare(const T *s) const
362 { return MyStringCompare(_chars, s); }
364 int CompareNoCase(const CStringBase& s) const
365 { return MyStringCompareNoCase(_chars, s._chars); }
367 int CompareNoCase(const T *s) const
368 { return MyStringCompareNoCase(_chars, s); }
371 int Collate(const CStringBase& s) const
372 { return MyStringCollate(_chars, s._chars); }
373 int CollateNoCase(const CStringBase& s) const
374 { return MyStringCollateNoCase(_chars, s._chars); }
377 int Find(T c) const { return Find(c, 0); }
378 int Find(T c, int startIndex) const
380 T *p = _chars + startIndex;
384 return (int)(p - _chars);
387 p = GetNextCharPointer(p);
390 int Find(const CStringBase &s) const { return Find(s, 0); }
391 int Find(const CStringBase &s, int startIndex) const
395 for (; startIndex < _length; startIndex++)
398 for (j = 0; j < s._length && startIndex + j < _length; j++)
399 if (_chars[startIndex+j] != s._chars[j])
406 int ReverseFind(T c) const
410 T *p = _chars + _length - 1;
414 return (int)(p - _chars);
417 p = GetPrevCharPointer(_chars, p);
420 int FindOneOf(const CStringBase &s) const
422 for(int i = 0; i < _length; i++)
423 if (s.Find(_chars[i]) >= 0)
432 p = GetNextCharPointer(p);
433 Delete(0, p - _chars);
436 CStringBase GetTrimDefaultCharSet()
438 CStringBase<T> charSet;
448 TrimLeftWithCharSet(GetTrimDefaultCharSet());
452 TrimRightWithCharSet(GetTrimDefaultCharSet());
457 const T *pLast = NULL;
467 p = GetNextCharPointer(p);
471 int i = pLast - _chars;
472 Delete(i, _length - i);
481 int Insert(int index, T c)
483 InsertSpace(index, 1);
488 int Insert(int index, const CStringBase &s)
493 int numInsertChars = s.Length();
494 InsertSpace(index, numInsertChars);
495 for(int i = 0; i < numInsertChars; i++)
496 _chars[index + i] = s[i];
497 _length += numInsertChars;
501 // !!!!!!!!!!!!!!! test it if newChar = '\0'
502 int Replace(T oldChar, T newChar)
504 if (oldChar == newChar)
508 while (pos < Length())
510 pos = Find(oldChar, pos);
513 _chars[pos] = newChar;
519 int Replace(const CStringBase &oldString, const CStringBase &newString)
521 if (oldString.IsEmpty())
523 if (oldString == newString)
525 int oldStringLength = oldString.Length();
526 int newStringLength = newString.Length();
529 while (pos < _length)
531 pos = Find(oldString, pos);
534 Delete(pos, oldStringLength);
535 Insert(pos, newString);
536 pos += newStringLength;
541 int Delete(int index, int count = 1 )
543 if (index + count > _length)
544 count = _length - index;
547 MoveItems(index, index + count);
555 CStringBase<T> operator+(const CStringBase<T>& s1, const CStringBase<T>& s2)
557 CStringBase<T> result(s1);
563 CStringBase<T> operator+(const CStringBase<T>& s, T c)
565 CStringBase<T> result(s);
571 CStringBase<T> operator+(T c, const CStringBase<T>& s)
573 CStringBase<T> result(c);
579 CStringBase<T> operator+(const CStringBase<T>& s, const T * chars)
581 CStringBase<T> result(s);
587 CStringBase<T> operator+(const T * chars, const CStringBase<T>& s)
589 CStringBase<T> result(chars);
595 bool operator==(const CStringBase<T>& s1, const CStringBase<T>& s2)
596 { return (s1.Compare(s2) == 0); }
599 bool operator<(const CStringBase<T>& s1, const CStringBase<T>& s2)
600 { return (s1.Compare(s2) < 0); }
603 bool operator==(const T *s1, const CStringBase<T>& s2)
604 { return (s2.Compare(s1) == 0); }
607 bool operator==(const CStringBase<T>& s1, const T *s2)
608 { return (s1.Compare(s2) == 0); }
611 bool operator!=(const CStringBase<T>& s1, const CStringBase<T>& s2)
612 { return (s1.Compare(s2) != 0); }
615 bool operator!=(const T *s1, const CStringBase<T>& s2)
616 { return (s2.Compare(s1) != 0); }
619 bool operator!=(const CStringBase<T>& s1, const T *s2)
620 { return (s1.Compare(s2) != 0); }
622 typedef CStringBase<char> AString;
623 typedef CStringBase<wchar_t> UString;
625 typedef CObjectVector<AString> AStringVector;
626 typedef CObjectVector<UString> UStringVector;
629 typedef UString CSysString;
631 typedef AString CSysString;
634 typedef CObjectVector<CSysString> CSysStringVector;