|
1 // Common/String.h |
|
2 |
|
3 #ifndef __COMMON_STRING_H |
|
4 #define __COMMON_STRING_H |
|
5 |
|
6 #include <string.h> |
|
7 // #include <wchar.h> |
|
8 |
|
9 #include "Vector.h" |
|
10 |
|
11 #ifdef _WIN32 |
|
12 #include "MyWindows.h" |
|
13 #endif |
|
14 |
|
15 static const char *kTrimDefaultCharSet = " \n\t"; |
|
16 |
|
17 template <class T> |
|
18 inline int MyStringLen(const T *s) |
|
19 { |
|
20 int i; |
|
21 for (i = 0; s[i] != '\0'; i++); |
|
22 return i; |
|
23 } |
|
24 |
|
25 template <class T> |
|
26 inline T * MyStringCopy(T *dest, const T *src) |
|
27 { |
|
28 T *destStart = dest; |
|
29 while((*dest++ = *src++) != 0); |
|
30 return destStart; |
|
31 } |
|
32 |
|
33 inline wchar_t* MyStringGetNextCharPointer(wchar_t *p) |
|
34 { return (p + 1); } |
|
35 inline const wchar_t* MyStringGetNextCharPointer(const wchar_t *p) |
|
36 { return (p + 1); } |
|
37 inline wchar_t* MyStringGetPrevCharPointer(const wchar_t *, wchar_t *p) |
|
38 { return (p - 1); } |
|
39 inline const wchar_t* MyStringGetPrevCharPointer(const wchar_t *, const wchar_t *p) |
|
40 { return (p - 1); } |
|
41 |
|
42 #ifdef _WIN32 |
|
43 |
|
44 inline char* MyStringGetNextCharPointer(char *p) |
|
45 { return CharNextA(p); } |
|
46 inline const char* MyStringGetNextCharPointer(const char *p) |
|
47 { return CharNextA(p); } |
|
48 |
|
49 inline char* MyStringGetPrevCharPointer(char *base, char *p) |
|
50 { return CharPrevA(base, p); } |
|
51 inline const char* MyStringGetPrevCharPointer(const char *base, const char *p) |
|
52 { return CharPrevA(base, p); } |
|
53 |
|
54 inline char MyCharUpper(char c) |
|
55 { return (char)(unsigned int)CharUpperA((LPSTR)(unsigned int)(unsigned char)c); } |
|
56 #ifdef _UNICODE |
|
57 inline wchar_t MyCharUpper(wchar_t c) |
|
58 { return (wchar_t)CharUpperW((LPWSTR)c); } |
|
59 #else |
|
60 wchar_t MyCharUpper(wchar_t c); |
|
61 #endif |
|
62 |
|
63 inline char MyCharLower(char c) |
|
64 { return (char)(unsigned int)CharLowerA((LPSTR)(unsigned int)(unsigned char)c); } |
|
65 #ifdef _UNICODE |
|
66 inline wchar_t MyCharLower(wchar_t c) |
|
67 { return (wchar_t)CharLowerW((LPWSTR)c); } |
|
68 #else |
|
69 wchar_t MyCharLower(wchar_t c); |
|
70 #endif |
|
71 |
|
72 inline char * MyStringUpper(char *s) { return CharUpperA(s); } |
|
73 #ifdef _UNICODE |
|
74 inline wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); } |
|
75 #else |
|
76 wchar_t * MyStringUpper(wchar_t *s); |
|
77 #endif |
|
78 |
|
79 inline char * MyStringLower(char *s) { return CharLowerA(s); } |
|
80 #ifdef _UNICODE |
|
81 inline wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); } |
|
82 #else |
|
83 wchar_t * MyStringLower(wchar_t *s); |
|
84 #endif |
|
85 |
|
86 #else // Standard-C |
|
87 wchar_t MyCharUpper(wchar_t c); |
|
88 #endif |
|
89 |
|
90 ////////////////////////////////////// |
|
91 // Compare |
|
92 |
|
93 /* |
|
94 #ifndef _WIN32_WCE |
|
95 int MyStringCollate(const char *s1, const char *s2); |
|
96 int MyStringCollateNoCase(const char *s1, const char *s2); |
|
97 #endif |
|
98 int MyStringCollate(const wchar_t *s1, const wchar_t *s2); |
|
99 int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2); |
|
100 */ |
|
101 |
|
102 int MyStringCompare(const char *s1, const char *s2); |
|
103 int MyStringCompare(const wchar_t *s1, const wchar_t *s2); |
|
104 |
|
105 #ifdef _WIN32 |
|
106 int MyStringCompareNoCase(const char *s1, const char *s2); |
|
107 #endif |
|
108 |
|
109 int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2); |
|
110 |
|
111 template <class T> |
|
112 class CStringBase |
|
113 { |
|
114 void TrimLeftWithCharSet(const CStringBase &charSet) |
|
115 { |
|
116 const T *p = _chars; |
|
117 while (charSet.Find(*p) >= 0 && (*p != 0)) |
|
118 p = GetNextCharPointer(p); |
|
119 Delete(0, (int)(p - _chars)); |
|
120 } |
|
121 void TrimRightWithCharSet(const CStringBase &charSet) |
|
122 { |
|
123 const T *p = _chars; |
|
124 const T *pLast = NULL; |
|
125 while (*p != 0) |
|
126 { |
|
127 if (charSet.Find(*p) >= 0) |
|
128 { |
|
129 if (pLast == NULL) |
|
130 pLast = p; |
|
131 } |
|
132 else |
|
133 pLast = NULL; |
|
134 p = GetNextCharPointer(p); |
|
135 } |
|
136 if(pLast != NULL) |
|
137 { |
|
138 int i = (int)(pLast - _chars); |
|
139 Delete(i, _length - i); |
|
140 } |
|
141 |
|
142 } |
|
143 void MoveItems(int destIndex, int srcIndex) |
|
144 { |
|
145 memmove(_chars + destIndex, _chars + srcIndex, |
|
146 sizeof(T) * (_length - srcIndex + 1)); |
|
147 } |
|
148 |
|
149 void InsertSpace(int &index, int size) |
|
150 { |
|
151 CorrectIndex(index); |
|
152 GrowLength(size); |
|
153 MoveItems(index + size, index); |
|
154 } |
|
155 |
|
156 static T *GetNextCharPointer(T *p) |
|
157 { return MyStringGetNextCharPointer(p); } |
|
158 static const T *GetNextCharPointer(const T *p) |
|
159 { return MyStringGetNextCharPointer(p); } |
|
160 static T *GetPrevCharPointer(T *base, T *p) |
|
161 { return MyStringGetPrevCharPointer(base, p); } |
|
162 static const T *GetPrevCharPointer(const T *base, const T *p) |
|
163 { return MyStringGetPrevCharPointer(base, p); } |
|
164 protected: |
|
165 T *_chars; |
|
166 int _length; |
|
167 int _capacity; |
|
168 |
|
169 void SetCapacity(int newCapacity) |
|
170 { |
|
171 int realCapacity = newCapacity + 1; |
|
172 if(realCapacity == _capacity) |
|
173 return; |
|
174 /* |
|
175 const int kMaxStringSize = 0x20000000; |
|
176 #ifndef _WIN32_WCE |
|
177 if(newCapacity > kMaxStringSize || newCapacity < _length) |
|
178 throw 1052337; |
|
179 #endif |
|
180 */ |
|
181 T *newBuffer = new T[realCapacity]; |
|
182 if(_capacity > 0) |
|
183 { |
|
184 for (int i = 0; i < (_length + 1); i++) |
|
185 newBuffer[i] = _chars[i]; |
|
186 delete []_chars; |
|
187 _chars = newBuffer; |
|
188 } |
|
189 else |
|
190 { |
|
191 _chars = newBuffer; |
|
192 _chars[0] = 0; |
|
193 } |
|
194 _capacity = realCapacity; |
|
195 } |
|
196 |
|
197 void GrowLength(int n) |
|
198 { |
|
199 int freeSize = _capacity - _length - 1; |
|
200 if (n <= freeSize) |
|
201 return; |
|
202 int delta; |
|
203 if (_capacity > 64) |
|
204 delta = _capacity / 2; |
|
205 else if (_capacity > 8) |
|
206 delta = 16; |
|
207 else |
|
208 delta = 4; |
|
209 if (freeSize + delta < n) |
|
210 delta = n - freeSize; |
|
211 SetCapacity(_capacity + delta); |
|
212 } |
|
213 |
|
214 void CorrectIndex(int &index) const |
|
215 { |
|
216 if (index > _length) |
|
217 index = _length; |
|
218 } |
|
219 |
|
220 public: |
|
221 CStringBase(): _chars(0), _length(0), _capacity(0) |
|
222 { SetCapacity(16 - 1); } |
|
223 CStringBase(T c): _chars(0), _length(0), _capacity(0) |
|
224 { |
|
225 SetCapacity(1); |
|
226 _chars[0] = c; |
|
227 _chars[1] = 0; |
|
228 _length = 1; |
|
229 } |
|
230 CStringBase(const T *chars): _chars(0), _length(0), _capacity(0) |
|
231 { |
|
232 int length = MyStringLen(chars); |
|
233 SetCapacity(length); |
|
234 MyStringCopy(_chars, chars); // can be optimized by memove() |
|
235 _length = length; |
|
236 } |
|
237 CStringBase(const CStringBase &s): _chars(0), _length(0), _capacity(0) |
|
238 { |
|
239 SetCapacity(s._length); |
|
240 MyStringCopy(_chars, s._chars); |
|
241 _length = s._length; |
|
242 } |
|
243 ~CStringBase() { delete []_chars; } |
|
244 |
|
245 operator const T*() const { return _chars;} |
|
246 |
|
247 // The minimum size of the character buffer in characters. |
|
248 // This value does not include space for a null terminator. |
|
249 T* GetBuffer(int minBufLength) |
|
250 { |
|
251 if(minBufLength >= _capacity) |
|
252 SetCapacity(minBufLength + 1); |
|
253 return _chars; |
|
254 } |
|
255 void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); } |
|
256 void ReleaseBuffer(int newLength) |
|
257 { |
|
258 /* |
|
259 #ifndef _WIN32_WCE |
|
260 if(newLength >= _capacity) |
|
261 throw 282217; |
|
262 #endif |
|
263 */ |
|
264 _chars[newLength] = 0; |
|
265 _length = newLength; |
|
266 } |
|
267 |
|
268 CStringBase& operator=(T c) |
|
269 { |
|
270 Empty(); |
|
271 SetCapacity(1); |
|
272 _chars[0] = c; |
|
273 _chars[1] = 0; |
|
274 _length = 1; |
|
275 return *this; |
|
276 } |
|
277 CStringBase& operator=(const T *chars) |
|
278 { |
|
279 Empty(); |
|
280 int length = MyStringLen(chars); |
|
281 SetCapacity(length); |
|
282 MyStringCopy(_chars, chars); |
|
283 _length = length; |
|
284 return *this; |
|
285 } |
|
286 CStringBase& operator=(const CStringBase& s) |
|
287 { |
|
288 if(&s == this) |
|
289 return *this; |
|
290 Empty(); |
|
291 SetCapacity(s._length); |
|
292 MyStringCopy(_chars, s._chars); |
|
293 _length = s._length; |
|
294 return *this; |
|
295 } |
|
296 |
|
297 CStringBase& operator+=(T c) |
|
298 { |
|
299 GrowLength(1); |
|
300 _chars[_length] = c; |
|
301 _chars[++_length] = 0; |
|
302 return *this; |
|
303 } |
|
304 CStringBase& operator+=(const T *s) |
|
305 { |
|
306 int len = MyStringLen(s); |
|
307 GrowLength(len); |
|
308 MyStringCopy(_chars + _length, s); |
|
309 _length += len; |
|
310 return *this; |
|
311 } |
|
312 CStringBase& operator+=(const CStringBase &s) |
|
313 { |
|
314 GrowLength(s._length); |
|
315 MyStringCopy(_chars + _length, s._chars); |
|
316 _length += s._length; |
|
317 return *this; |
|
318 } |
|
319 void Empty() |
|
320 { |
|
321 _length = 0; |
|
322 _chars[0] = 0; |
|
323 } |
|
324 int Length() const { return _length; } |
|
325 bool IsEmpty() const { return (_length == 0); } |
|
326 |
|
327 CStringBase Mid(int startIndex) const |
|
328 { return Mid(startIndex, _length - startIndex); } |
|
329 CStringBase Mid(int startIndex, int count ) const |
|
330 { |
|
331 if (startIndex + count > _length) |
|
332 count = _length - startIndex; |
|
333 |
|
334 if (startIndex == 0 && startIndex + count == _length) |
|
335 return *this; |
|
336 |
|
337 CStringBase<T> result; |
|
338 result.SetCapacity(count); |
|
339 // MyStringNCopy(result._chars, _chars + startIndex, count); |
|
340 for (int i = 0; i < count; i++) |
|
341 result._chars[i] = _chars[startIndex + i]; |
|
342 result._chars[count] = 0; |
|
343 result._length = count; |
|
344 return result; |
|
345 } |
|
346 CStringBase Left(int count) const |
|
347 { return Mid(0, count); } |
|
348 CStringBase Right(int count) const |
|
349 { |
|
350 if (count > _length) |
|
351 count = _length; |
|
352 return Mid(_length - count, count); |
|
353 } |
|
354 |
|
355 void MakeUpper() |
|
356 { MyStringUpper(_chars); } |
|
357 void MakeLower() |
|
358 { MyStringLower(_chars); } |
|
359 |
|
360 int Compare(const CStringBase& s) const |
|
361 { return MyStringCompare(_chars, s._chars); } |
|
362 |
|
363 int CompareNoCase(const CStringBase& s) const |
|
364 { return MyStringCompareNoCase(_chars, s._chars); } |
|
365 /* |
|
366 int Collate(const CStringBase& s) const |
|
367 { return MyStringCollate(_chars, s._chars); } |
|
368 int CollateNoCase(const CStringBase& s) const |
|
369 { return MyStringCollateNoCase(_chars, s._chars); } |
|
370 */ |
|
371 |
|
372 int Find(T c) const { return Find(c, 0); } |
|
373 int Find(T c, int startIndex) const |
|
374 { |
|
375 T *p = _chars + startIndex; |
|
376 while (true) |
|
377 { |
|
378 if (*p == c) |
|
379 return (int)(p - _chars); |
|
380 if (*p == 0) |
|
381 return -1; |
|
382 p = GetNextCharPointer(p); |
|
383 } |
|
384 } |
|
385 int Find(const CStringBase &s) const { return Find(s, 0); } |
|
386 int Find(const CStringBase &s, int startIndex) const |
|
387 { |
|
388 if (s.IsEmpty()) |
|
389 return startIndex; |
|
390 for (; startIndex < _length; startIndex++) |
|
391 { |
|
392 int j; |
|
393 for (j = 0; j < s._length && startIndex + j < _length; j++) |
|
394 if (_chars[startIndex+j] != s._chars[j]) |
|
395 break; |
|
396 if (j == s._length) |
|
397 return startIndex; |
|
398 } |
|
399 return -1; |
|
400 } |
|
401 int ReverseFind(T c) const |
|
402 { |
|
403 if (_length == 0) |
|
404 return -1; |
|
405 T *p = _chars + _length - 1; |
|
406 while (true) |
|
407 { |
|
408 if (*p == c) |
|
409 return (int)(p - _chars); |
|
410 if (p == _chars) |
|
411 return -1; |
|
412 p = GetPrevCharPointer(_chars, p); |
|
413 } |
|
414 } |
|
415 int FindOneOf(const CStringBase &s) const |
|
416 { |
|
417 for(int i = 0; i < _length; i++) |
|
418 if (s.Find(_chars[i]) >= 0) |
|
419 return i; |
|
420 return -1; |
|
421 } |
|
422 |
|
423 void TrimLeft(T c) |
|
424 { |
|
425 const T *p = _chars; |
|
426 while (c == *p) |
|
427 p = GetNextCharPointer(p); |
|
428 Delete(0, p - _chars); |
|
429 } |
|
430 private: |
|
431 CStringBase GetTrimDefaultCharSet() |
|
432 { |
|
433 CStringBase<T> charSet; |
|
434 for(int i = 0; i < (int)(sizeof(kTrimDefaultCharSet) / |
|
435 sizeof(kTrimDefaultCharSet[0])); i++) |
|
436 charSet += (T)kTrimDefaultCharSet[i]; |
|
437 return charSet; |
|
438 } |
|
439 public: |
|
440 |
|
441 void TrimLeft() |
|
442 { |
|
443 TrimLeftWithCharSet(GetTrimDefaultCharSet()); |
|
444 } |
|
445 void TrimRight() |
|
446 { |
|
447 TrimRightWithCharSet(GetTrimDefaultCharSet()); |
|
448 } |
|
449 void TrimRight(T c) |
|
450 { |
|
451 const T *p = _chars; |
|
452 const T *pLast = NULL; |
|
453 while (*p != 0) |
|
454 { |
|
455 if (*p == c) |
|
456 { |
|
457 if (pLast == NULL) |
|
458 pLast = p; |
|
459 } |
|
460 else |
|
461 pLast = NULL; |
|
462 p = GetNextCharPointer(p); |
|
463 } |
|
464 if(pLast != NULL) |
|
465 { |
|
466 int i = pLast - _chars; |
|
467 Delete(i, _length - i); |
|
468 } |
|
469 } |
|
470 void Trim() |
|
471 { |
|
472 TrimRight(); |
|
473 TrimLeft(); |
|
474 } |
|
475 |
|
476 int Insert(int index, T c) |
|
477 { |
|
478 InsertSpace(index, 1); |
|
479 _chars[index] = c; |
|
480 _length++; |
|
481 return _length; |
|
482 } |
|
483 int Insert(int index, const CStringBase &s) |
|
484 { |
|
485 CorrectIndex(index); |
|
486 if (s.IsEmpty()) |
|
487 return _length; |
|
488 int numInsertChars = s.Length(); |
|
489 InsertSpace(index, numInsertChars); |
|
490 for(int i = 0; i < numInsertChars; i++) |
|
491 _chars[index + i] = s[i]; |
|
492 _length += numInsertChars; |
|
493 return _length; |
|
494 } |
|
495 |
|
496 // !!!!!!!!!!!!!!! test it if newChar = '\0' |
|
497 int Replace(T oldChar, T newChar) |
|
498 { |
|
499 if (oldChar == newChar) |
|
500 return 0; |
|
501 int number = 0; |
|
502 int pos = 0; |
|
503 while (pos < Length()) |
|
504 { |
|
505 pos = Find(oldChar, pos); |
|
506 if (pos < 0) |
|
507 break; |
|
508 _chars[pos] = newChar; |
|
509 pos++; |
|
510 number++; |
|
511 } |
|
512 return number; |
|
513 } |
|
514 int Replace(const CStringBase &oldString, const CStringBase &newString) |
|
515 { |
|
516 if (oldString.IsEmpty()) |
|
517 return 0; |
|
518 if (oldString == newString) |
|
519 return 0; |
|
520 int oldStringLength = oldString.Length(); |
|
521 int newStringLength = newString.Length(); |
|
522 int number = 0; |
|
523 int pos = 0; |
|
524 while (pos < _length) |
|
525 { |
|
526 pos = Find(oldString, pos); |
|
527 if (pos < 0) |
|
528 break; |
|
529 Delete(pos, oldStringLength); |
|
530 Insert(pos, newString); |
|
531 pos += newStringLength; |
|
532 number++; |
|
533 } |
|
534 return number; |
|
535 } |
|
536 int Delete(int index, int count = 1 ) |
|
537 { |
|
538 if (index + count > _length) |
|
539 count = _length - index; |
|
540 if (count > 0) |
|
541 { |
|
542 MoveItems(index, index + count); |
|
543 _length -= count; |
|
544 } |
|
545 return _length; |
|
546 } |
|
547 }; |
|
548 |
|
549 template <class T> |
|
550 CStringBase<T> operator+(const CStringBase<T>& s1, const CStringBase<T>& s2) |
|
551 { |
|
552 CStringBase<T> result(s1); |
|
553 result += s2; |
|
554 return result; |
|
555 } |
|
556 |
|
557 template <class T> |
|
558 CStringBase<T> operator+(const CStringBase<T>& s, T c) |
|
559 { |
|
560 CStringBase<T> result(s); |
|
561 result += c; |
|
562 return result; |
|
563 } |
|
564 |
|
565 template <class T> |
|
566 CStringBase<T> operator+(T c, const CStringBase<T>& s) |
|
567 { |
|
568 CStringBase<T> result(c); |
|
569 result += s; |
|
570 return result; |
|
571 } |
|
572 |
|
573 template <class T> |
|
574 CStringBase<T> operator+(const CStringBase<T>& s, const T * chars) |
|
575 { |
|
576 CStringBase<T> result(s); |
|
577 result += chars; |
|
578 return result; |
|
579 } |
|
580 |
|
581 template <class T> |
|
582 CStringBase<T> operator+(const T * chars, const CStringBase<T>& s) |
|
583 { |
|
584 CStringBase<T> result(chars); |
|
585 result += s; |
|
586 return result; |
|
587 } |
|
588 |
|
589 template <class T> |
|
590 bool operator==(const CStringBase<T>& s1, const CStringBase<T>& s2) |
|
591 { return (s1.Compare(s2) == 0); } |
|
592 |
|
593 template <class T> |
|
594 bool operator<(const CStringBase<T>& s1, const CStringBase<T>& s2) |
|
595 { return (s1.Compare(s2) < 0); } |
|
596 |
|
597 template <class T> |
|
598 bool operator==(const T *s1, const CStringBase<T>& s2) |
|
599 { return (s2.Compare(s1) == 0); } |
|
600 |
|
601 template <class T> |
|
602 bool operator==(const CStringBase<T>& s1, const T *s2) |
|
603 { return (s1.Compare(s2) == 0); } |
|
604 |
|
605 template <class T> |
|
606 bool operator!=(const CStringBase<T>& s1, const CStringBase<T>& s2) |
|
607 { return (s1.Compare(s2) != 0); } |
|
608 |
|
609 template <class T> |
|
610 bool operator!=(const T *s1, const CStringBase<T>& s2) |
|
611 { return (s2.Compare(s1) != 0); } |
|
612 |
|
613 template <class T> |
|
614 bool operator!=(const CStringBase<T>& s1, const T *s2) |
|
615 { return (s1.Compare(s2) != 0); } |
|
616 |
|
617 typedef CStringBase<char> AString; |
|
618 typedef CStringBase<wchar_t> UString; |
|
619 |
|
620 typedef CObjectVector<AString> AStringVector; |
|
621 typedef CObjectVector<UString> UStringVector; |
|
622 |
|
623 #ifdef _UNICODE |
|
624 typedef UString CSysString; |
|
625 #else |
|
626 typedef AString CSysString; |
|
627 #endif |
|
628 |
|
629 typedef CObjectVector<CSysString> CSysStringVector; |
|
630 |
|
631 #endif |