Mercurial > vba-clojure
diff src/win32/7zip/7z/CPP/Common/MyString.h @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7z/CPP/Common/MyString.h Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,631 @@ 1.4 +// Common/String.h 1.5 + 1.6 +#ifndef __COMMON_STRING_H 1.7 +#define __COMMON_STRING_H 1.8 + 1.9 +#include <string.h> 1.10 +// #include <wchar.h> 1.11 + 1.12 +#include "MyVector.h" 1.13 + 1.14 +#ifdef _WIN32 1.15 +#include "MyWindows.h" 1.16 +#endif 1.17 + 1.18 +template <class T> 1.19 +inline int MyStringLen(const T *s) 1.20 +{ 1.21 + int i; 1.22 + for (i = 0; s[i] != '\0'; i++); 1.23 + return i; 1.24 +} 1.25 + 1.26 +template <class T> 1.27 +inline T * MyStringCopy(T *dest, const T *src) 1.28 +{ 1.29 + T *destStart = dest; 1.30 + while ((*dest++ = *src++) != 0); 1.31 + return destStart; 1.32 +} 1.33 + 1.34 +inline wchar_t* MyStringGetNextCharPointer(wchar_t *p) 1.35 + { return (p + 1); } 1.36 +inline const wchar_t* MyStringGetNextCharPointer(const wchar_t *p) 1.37 + { return (p + 1); } 1.38 +inline wchar_t* MyStringGetPrevCharPointer(const wchar_t *, wchar_t *p) 1.39 + { return (p - 1); } 1.40 +inline const wchar_t* MyStringGetPrevCharPointer(const wchar_t *, const wchar_t *p) 1.41 + { return (p - 1); } 1.42 + 1.43 +#ifdef _WIN32 1.44 + 1.45 +inline char* MyStringGetNextCharPointer(char *p) 1.46 + { return CharNextA(p); } 1.47 +inline const char* MyStringGetNextCharPointer(const char *p) 1.48 + { return CharNextA(p); } 1.49 + 1.50 +inline char* MyStringGetPrevCharPointer(char *base, char *p) 1.51 + { return CharPrevA(base, p); } 1.52 +inline const char* MyStringGetPrevCharPointer(const char *base, const char *p) 1.53 + { return CharPrevA(base, p); } 1.54 + 1.55 +inline char MyCharUpper(char c) 1.56 + { return (char)(unsigned int)(UINT_PTR)CharUpperA((LPSTR)(UINT_PTR)(unsigned int)(unsigned char)c); } 1.57 +#ifdef _UNICODE 1.58 +inline wchar_t MyCharUpper(wchar_t c) 1.59 + { return (wchar_t)(unsigned int)(UINT_PTR)CharUpperW((LPWSTR)(UINT_PTR)(unsigned int)c); } 1.60 +#else 1.61 +wchar_t MyCharUpper(wchar_t c); 1.62 +#endif 1.63 + 1.64 +inline char MyCharLower(char c) 1.65 + { return (char)(unsigned int)(UINT_PTR)CharLowerA((LPSTR)(UINT_PTR)(unsigned int)(unsigned char)c); } 1.66 +#ifdef _UNICODE 1.67 +inline wchar_t MyCharLower(wchar_t c) 1.68 + { return (wchar_t)(unsigned int)(UINT_PTR)CharLowerW((LPWSTR)(UINT_PTR)(unsigned int)c); } 1.69 +#else 1.70 +wchar_t MyCharLower(wchar_t c); 1.71 +#endif 1.72 + 1.73 +inline char * MyStringUpper(char *s) { return CharUpperA(s); } 1.74 +#ifdef _UNICODE 1.75 +inline wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); } 1.76 +#else 1.77 +wchar_t * MyStringUpper(wchar_t *s); 1.78 +#endif 1.79 + 1.80 +inline char * MyStringLower(char *s) { return CharLowerA(s); } 1.81 +#ifdef _UNICODE 1.82 +inline wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); } 1.83 +#else 1.84 +wchar_t * MyStringLower(wchar_t *s); 1.85 +#endif 1.86 + 1.87 +#else // Standard-C 1.88 +wchar_t MyCharUpper(wchar_t c); 1.89 +#endif 1.90 + 1.91 +////////////////////////////////////// 1.92 +// Compare 1.93 + 1.94 +/* 1.95 +#ifndef _WIN32_WCE 1.96 +int MyStringCollate(const char *s1, const char *s2); 1.97 +int MyStringCollateNoCase(const char *s1, const char *s2); 1.98 +#endif 1.99 +int MyStringCollate(const wchar_t *s1, const wchar_t *s2); 1.100 +int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2); 1.101 +*/ 1.102 + 1.103 +int MyStringCompare(const char *s1, const char *s2); 1.104 +int MyStringCompare(const wchar_t *s1, const wchar_t *s2); 1.105 + 1.106 +#ifdef _WIN32 1.107 +int MyStringCompareNoCase(const char *s1, const char *s2); 1.108 +#endif 1.109 + 1.110 +int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2); 1.111 + 1.112 +template <class T> 1.113 +class CStringBase 1.114 +{ 1.115 + void TrimLeftWithCharSet(const CStringBase &charSet) 1.116 + { 1.117 + const T *p = _chars; 1.118 + while (charSet.Find(*p) >= 0 && (*p != 0)) 1.119 + p = GetNextCharPointer(p); 1.120 + Delete(0, (int)(p - _chars)); 1.121 + } 1.122 + void TrimRightWithCharSet(const CStringBase &charSet) 1.123 + { 1.124 + const T *p = _chars; 1.125 + const T *pLast = NULL; 1.126 + while (*p != 0) 1.127 + { 1.128 + if (charSet.Find(*p) >= 0) 1.129 + { 1.130 + if (pLast == NULL) 1.131 + pLast = p; 1.132 + } 1.133 + else 1.134 + pLast = NULL; 1.135 + p = GetNextCharPointer(p); 1.136 + } 1.137 + if (pLast != NULL) 1.138 + { 1.139 + int i = (int)(pLast - _chars); 1.140 + Delete(i, _length - i); 1.141 + } 1.142 + 1.143 + } 1.144 + void MoveItems(int destIndex, int srcIndex) 1.145 + { 1.146 + memmove(_chars + destIndex, _chars + srcIndex, 1.147 + sizeof(T) * (_length - srcIndex + 1)); 1.148 + } 1.149 + 1.150 + void InsertSpace(int &index, int size) 1.151 + { 1.152 + CorrectIndex(index); 1.153 + GrowLength(size); 1.154 + MoveItems(index + size, index); 1.155 + } 1.156 + 1.157 + static T *GetNextCharPointer(T *p) 1.158 + { return MyStringGetNextCharPointer(p); } 1.159 + static const T *GetNextCharPointer(const T *p) 1.160 + { return MyStringGetNextCharPointer(p); } 1.161 + static T *GetPrevCharPointer(T *base, T *p) 1.162 + { return MyStringGetPrevCharPointer(base, p); } 1.163 + static const T *GetPrevCharPointer(const T *base, const T *p) 1.164 + { return MyStringGetPrevCharPointer(base, p); } 1.165 +protected: 1.166 + T *_chars; 1.167 + int _length; 1.168 + int _capacity; 1.169 + 1.170 + void SetCapacity(int newCapacity) 1.171 + { 1.172 + int realCapacity = newCapacity + 1; 1.173 + if (realCapacity == _capacity) 1.174 + return; 1.175 + /* 1.176 + const int kMaxStringSize = 0x20000000; 1.177 + #ifndef _WIN32_WCE 1.178 + if (newCapacity > kMaxStringSize || newCapacity < _length) 1.179 + throw 1052337; 1.180 + #endif 1.181 + */ 1.182 + T *newBuffer = new T[realCapacity]; 1.183 + if (_capacity > 0) 1.184 + { 1.185 + for (int i = 0; i < _length; i++) 1.186 + newBuffer[i] = _chars[i]; 1.187 + delete []_chars; 1.188 + } 1.189 + _chars = newBuffer; 1.190 + _chars[_length] = 0; 1.191 + _capacity = realCapacity; 1.192 + } 1.193 + 1.194 + void GrowLength(int n) 1.195 + { 1.196 + int freeSize = _capacity - _length - 1; 1.197 + if (n <= freeSize) 1.198 + return; 1.199 + int delta; 1.200 + if (_capacity > 64) 1.201 + delta = _capacity / 2; 1.202 + else if (_capacity > 8) 1.203 + delta = 16; 1.204 + else 1.205 + delta = 4; 1.206 + if (freeSize + delta < n) 1.207 + delta = n - freeSize; 1.208 + SetCapacity(_capacity + delta); 1.209 + } 1.210 + 1.211 + void CorrectIndex(int &index) const 1.212 + { 1.213 + if (index > _length) 1.214 + index = _length; 1.215 + } 1.216 + 1.217 +public: 1.218 + CStringBase(): _chars(0), _length(0), _capacity(0) { SetCapacity(3); } 1.219 + CStringBase(T c): _chars(0), _length(0), _capacity(0) 1.220 + { 1.221 + SetCapacity(1); 1.222 + _chars[0] = c; 1.223 + _chars[1] = 0; 1.224 + _length = 1; 1.225 + } 1.226 + CStringBase(const T *chars): _chars(0), _length(0), _capacity(0) 1.227 + { 1.228 + int length = MyStringLen(chars); 1.229 + SetCapacity(length); 1.230 + MyStringCopy(_chars, chars); // can be optimized by memove() 1.231 + _length = length; 1.232 + } 1.233 + CStringBase(const CStringBase &s): _chars(0), _length(0), _capacity(0) 1.234 + { 1.235 + SetCapacity(s._length); 1.236 + MyStringCopy(_chars, s._chars); 1.237 + _length = s._length; 1.238 + } 1.239 + ~CStringBase() { delete []_chars; } 1.240 + 1.241 + operator const T*() const { return _chars;} 1.242 + 1.243 + // The minimum size of the character buffer in characters. 1.244 + // This value does not include space for a null terminator. 1.245 + T* GetBuffer(int minBufLength) 1.246 + { 1.247 + if (minBufLength >= _capacity) 1.248 + SetCapacity(minBufLength); 1.249 + return _chars; 1.250 + } 1.251 + void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); } 1.252 + void ReleaseBuffer(int newLength) 1.253 + { 1.254 + /* 1.255 + #ifndef _WIN32_WCE 1.256 + if (newLength >= _capacity) 1.257 + throw 282217; 1.258 + #endif 1.259 + */ 1.260 + _chars[newLength] = 0; 1.261 + _length = newLength; 1.262 + } 1.263 + 1.264 + CStringBase& operator=(T c) 1.265 + { 1.266 + Empty(); 1.267 + SetCapacity(1); 1.268 + _chars[0] = c; 1.269 + _chars[1] = 0; 1.270 + _length = 1; 1.271 + return *this; 1.272 + } 1.273 + CStringBase& operator=(const T *chars) 1.274 + { 1.275 + Empty(); 1.276 + int length = MyStringLen(chars); 1.277 + SetCapacity(length); 1.278 + MyStringCopy(_chars, chars); 1.279 + _length = length; 1.280 + return *this; 1.281 + } 1.282 + CStringBase& operator=(const CStringBase& s) 1.283 + { 1.284 + if (&s == this) 1.285 + return *this; 1.286 + Empty(); 1.287 + SetCapacity(s._length); 1.288 + MyStringCopy(_chars, s._chars); 1.289 + _length = s._length; 1.290 + return *this; 1.291 + } 1.292 + 1.293 + CStringBase& operator+=(T c) 1.294 + { 1.295 + GrowLength(1); 1.296 + _chars[_length] = c; 1.297 + _chars[++_length] = 0; 1.298 + return *this; 1.299 + } 1.300 + CStringBase& operator+=(const T *s) 1.301 + { 1.302 + int len = MyStringLen(s); 1.303 + GrowLength(len); 1.304 + MyStringCopy(_chars + _length, s); 1.305 + _length += len; 1.306 + return *this; 1.307 + } 1.308 + CStringBase& operator+=(const CStringBase &s) 1.309 + { 1.310 + GrowLength(s._length); 1.311 + MyStringCopy(_chars + _length, s._chars); 1.312 + _length += s._length; 1.313 + return *this; 1.314 + } 1.315 + void Empty() 1.316 + { 1.317 + _length = 0; 1.318 + _chars[0] = 0; 1.319 + } 1.320 + int Length() const { return _length; } 1.321 + bool IsEmpty() const { return (_length == 0); } 1.322 + 1.323 + CStringBase Mid(int startIndex) const 1.324 + { return Mid(startIndex, _length - startIndex); } 1.325 + CStringBase Mid(int startIndex, int count ) const 1.326 + { 1.327 + if (startIndex + count > _length) 1.328 + count = _length - startIndex; 1.329 + 1.330 + if (startIndex == 0 && startIndex + count == _length) 1.331 + return *this; 1.332 + 1.333 + CStringBase<T> result; 1.334 + result.SetCapacity(count); 1.335 + // MyStringNCopy(result._chars, _chars + startIndex, count); 1.336 + for (int i = 0; i < count; i++) 1.337 + result._chars[i] = _chars[startIndex + i]; 1.338 + result._chars[count] = 0; 1.339 + result._length = count; 1.340 + return result; 1.341 + } 1.342 + CStringBase Left(int count) const 1.343 + { return Mid(0, count); } 1.344 + CStringBase Right(int count) const 1.345 + { 1.346 + if (count > _length) 1.347 + count = _length; 1.348 + return Mid(_length - count, count); 1.349 + } 1.350 + 1.351 + void MakeUpper() 1.352 + { MyStringUpper(_chars); } 1.353 + void MakeLower() 1.354 + { MyStringLower(_chars); } 1.355 + 1.356 + int Compare(const CStringBase& s) const 1.357 + { return MyStringCompare(_chars, s._chars); } 1.358 + 1.359 + int Compare(const T *s) const 1.360 + { return MyStringCompare(_chars, s); } 1.361 + 1.362 + int CompareNoCase(const CStringBase& s) const 1.363 + { return MyStringCompareNoCase(_chars, s._chars); } 1.364 + 1.365 + int CompareNoCase(const T *s) const 1.366 + { return MyStringCompareNoCase(_chars, s); } 1.367 + 1.368 + /* 1.369 + int Collate(const CStringBase& s) const 1.370 + { return MyStringCollate(_chars, s._chars); } 1.371 + int CollateNoCase(const CStringBase& s) const 1.372 + { return MyStringCollateNoCase(_chars, s._chars); } 1.373 + */ 1.374 + 1.375 + int Find(T c) const { return Find(c, 0); } 1.376 + int Find(T c, int startIndex) const 1.377 + { 1.378 + T *p = _chars + startIndex; 1.379 + for (;;) 1.380 + { 1.381 + if (*p == c) 1.382 + return (int)(p - _chars); 1.383 + if (*p == 0) 1.384 + return -1; 1.385 + p = GetNextCharPointer(p); 1.386 + } 1.387 + } 1.388 + int Find(const CStringBase &s) const { return Find(s, 0); } 1.389 + int Find(const CStringBase &s, int startIndex) const 1.390 + { 1.391 + if (s.IsEmpty()) 1.392 + return startIndex; 1.393 + for (; startIndex < _length; startIndex++) 1.394 + { 1.395 + int j; 1.396 + for (j = 0; j < s._length && startIndex + j < _length; j++) 1.397 + if (_chars[startIndex+j] != s._chars[j]) 1.398 + break; 1.399 + if (j == s._length) 1.400 + return startIndex; 1.401 + } 1.402 + return -1; 1.403 + } 1.404 + int ReverseFind(T c) const 1.405 + { 1.406 + if (_length == 0) 1.407 + return -1; 1.408 + T *p = _chars + _length - 1; 1.409 + for (;;) 1.410 + { 1.411 + if (*p == c) 1.412 + return (int)(p - _chars); 1.413 + if (p == _chars) 1.414 + return -1; 1.415 + p = GetPrevCharPointer(_chars, p); 1.416 + } 1.417 + } 1.418 + int FindOneOf(const CStringBase &s) const 1.419 + { 1.420 + for (int i = 0; i < _length; i++) 1.421 + if (s.Find(_chars[i]) >= 0) 1.422 + return i; 1.423 + return -1; 1.424 + } 1.425 + 1.426 + void TrimLeft(T c) 1.427 + { 1.428 + const T *p = _chars; 1.429 + while (c == *p) 1.430 + p = GetNextCharPointer(p); 1.431 + Delete(0, p - _chars); 1.432 + } 1.433 + private: 1.434 + CStringBase GetTrimDefaultCharSet() 1.435 + { 1.436 + CStringBase<T> charSet; 1.437 + charSet += (T)' '; 1.438 + charSet += (T)'\n'; 1.439 + charSet += (T)'\t'; 1.440 + return charSet; 1.441 + } 1.442 + public: 1.443 + 1.444 + void TrimLeft() 1.445 + { 1.446 + TrimLeftWithCharSet(GetTrimDefaultCharSet()); 1.447 + } 1.448 + void TrimRight() 1.449 + { 1.450 + TrimRightWithCharSet(GetTrimDefaultCharSet()); 1.451 + } 1.452 + void TrimRight(T c) 1.453 + { 1.454 + const T *p = _chars; 1.455 + const T *pLast = NULL; 1.456 + while (*p != 0) 1.457 + { 1.458 + if (*p == c) 1.459 + { 1.460 + if (pLast == NULL) 1.461 + pLast = p; 1.462 + } 1.463 + else 1.464 + pLast = NULL; 1.465 + p = GetNextCharPointer(p); 1.466 + } 1.467 + if (pLast != NULL) 1.468 + { 1.469 + int i = pLast - _chars; 1.470 + Delete(i, _length - i); 1.471 + } 1.472 + } 1.473 + void Trim() 1.474 + { 1.475 + TrimRight(); 1.476 + TrimLeft(); 1.477 + } 1.478 + 1.479 + int Insert(int index, T c) 1.480 + { 1.481 + InsertSpace(index, 1); 1.482 + _chars[index] = c; 1.483 + _length++; 1.484 + return _length; 1.485 + } 1.486 + int Insert(int index, const CStringBase &s) 1.487 + { 1.488 + CorrectIndex(index); 1.489 + if (s.IsEmpty()) 1.490 + return _length; 1.491 + int numInsertChars = s.Length(); 1.492 + InsertSpace(index, numInsertChars); 1.493 + for (int i = 0; i < numInsertChars; i++) 1.494 + _chars[index + i] = s[i]; 1.495 + _length += numInsertChars; 1.496 + return _length; 1.497 + } 1.498 + 1.499 + // !!!!!!!!!!!!!!! test it if newChar = '\0' 1.500 + int Replace(T oldChar, T newChar) 1.501 + { 1.502 + if (oldChar == newChar) 1.503 + return 0; 1.504 + int number = 0; 1.505 + int pos = 0; 1.506 + while (pos < Length()) 1.507 + { 1.508 + pos = Find(oldChar, pos); 1.509 + if (pos < 0) 1.510 + break; 1.511 + _chars[pos] = newChar; 1.512 + pos++; 1.513 + number++; 1.514 + } 1.515 + return number; 1.516 + } 1.517 + int Replace(const CStringBase &oldString, const CStringBase &newString) 1.518 + { 1.519 + if (oldString.IsEmpty()) 1.520 + return 0; 1.521 + if (oldString == newString) 1.522 + return 0; 1.523 + int oldStringLength = oldString.Length(); 1.524 + int newStringLength = newString.Length(); 1.525 + int number = 0; 1.526 + int pos = 0; 1.527 + while (pos < _length) 1.528 + { 1.529 + pos = Find(oldString, pos); 1.530 + if (pos < 0) 1.531 + break; 1.532 + Delete(pos, oldStringLength); 1.533 + Insert(pos, newString); 1.534 + pos += newStringLength; 1.535 + number++; 1.536 + } 1.537 + return number; 1.538 + } 1.539 + int Delete(int index, int count = 1 ) 1.540 + { 1.541 + if (index + count > _length) 1.542 + count = _length - index; 1.543 + if (count > 0) 1.544 + { 1.545 + MoveItems(index, index + count); 1.546 + _length -= count; 1.547 + } 1.548 + return _length; 1.549 + } 1.550 +}; 1.551 + 1.552 +template <class T> 1.553 +CStringBase<T> operator+(const CStringBase<T>& s1, const CStringBase<T>& s2) 1.554 +{ 1.555 + CStringBase<T> result(s1); 1.556 + result += s2; 1.557 + return result; 1.558 +} 1.559 + 1.560 +template <class T> 1.561 +CStringBase<T> operator+(const CStringBase<T>& s, T c) 1.562 +{ 1.563 + CStringBase<T> result(s); 1.564 + result += c; 1.565 + return result; 1.566 +} 1.567 + 1.568 +template <class T> 1.569 +CStringBase<T> operator+(T c, const CStringBase<T>& s) 1.570 +{ 1.571 + CStringBase<T> result(c); 1.572 + result += s; 1.573 + return result; 1.574 +} 1.575 + 1.576 +template <class T> 1.577 +CStringBase<T> operator+(const CStringBase<T>& s, const T * chars) 1.578 +{ 1.579 + CStringBase<T> result(s); 1.580 + result += chars; 1.581 + return result; 1.582 +} 1.583 + 1.584 +template <class T> 1.585 +CStringBase<T> operator+(const T * chars, const CStringBase<T>& s) 1.586 +{ 1.587 + CStringBase<T> result(chars); 1.588 + result += s; 1.589 + return result; 1.590 +} 1.591 + 1.592 +template <class T> 1.593 +bool operator==(const CStringBase<T>& s1, const CStringBase<T>& s2) 1.594 + { return (s1.Compare(s2) == 0); } 1.595 + 1.596 +template <class T> 1.597 +bool operator<(const CStringBase<T>& s1, const CStringBase<T>& s2) 1.598 + { return (s1.Compare(s2) < 0); } 1.599 + 1.600 +template <class T> 1.601 +bool operator==(const T *s1, const CStringBase<T>& s2) 1.602 + { return (s2.Compare(s1) == 0); } 1.603 + 1.604 +template <class T> 1.605 +bool operator==(const CStringBase<T>& s1, const T *s2) 1.606 + { return (s1.Compare(s2) == 0); } 1.607 + 1.608 +template <class T> 1.609 +bool operator!=(const CStringBase<T>& s1, const CStringBase<T>& s2) 1.610 + { return (s1.Compare(s2) != 0); } 1.611 + 1.612 +template <class T> 1.613 +bool operator!=(const T *s1, const CStringBase<T>& s2) 1.614 + { return (s2.Compare(s1) != 0); } 1.615 + 1.616 +template <class T> 1.617 +bool operator!=(const CStringBase<T>& s1, const T *s2) 1.618 + { return (s1.Compare(s2) != 0); } 1.619 + 1.620 +typedef CStringBase<char> AString; 1.621 +typedef CStringBase<wchar_t> UString; 1.622 + 1.623 +typedef CObjectVector<AString> AStringVector; 1.624 +typedef CObjectVector<UString> UStringVector; 1.625 + 1.626 +#ifdef _UNICODE 1.627 + typedef UString CSysString; 1.628 +#else 1.629 + typedef AString CSysString; 1.630 +#endif 1.631 + 1.632 +typedef CObjectVector<CSysString> CSysStringVector; 1.633 + 1.634 +#endif