1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/Common/String.h Wed Dec 31 06:09:35 2014 +0100 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 "Vector.h" 1.13 + 1.14 +#ifdef _WIN32 1.15 +#include "MyWindows.h" 1.16 +#endif 1.17 + 1.18 +static const char *kTrimDefaultCharSet = " \n\t"; 1.19 + 1.20 +template <class T> 1.21 +inline int MyStringLen(const T *s) 1.22 +{ 1.23 + int i; 1.24 + for (i = 0; s[i] != '\0'; i++); 1.25 + return i; 1.26 +} 1.27 + 1.28 +template <class T> 1.29 +inline T * MyStringCopy(T *dest, const T *src) 1.30 +{ 1.31 + T *destStart = dest; 1.32 + while((*dest++ = *src++) != 0); 1.33 + return destStart; 1.34 +} 1.35 + 1.36 +inline wchar_t* MyStringGetNextCharPointer(wchar_t *p) 1.37 + { return (p + 1); } 1.38 +inline const wchar_t* MyStringGetNextCharPointer(const wchar_t *p) 1.39 + { return (p + 1); } 1.40 +inline wchar_t* MyStringGetPrevCharPointer(const wchar_t *, wchar_t *p) 1.41 + { return (p - 1); } 1.42 +inline const wchar_t* MyStringGetPrevCharPointer(const wchar_t *, const wchar_t *p) 1.43 + { return (p - 1); } 1.44 + 1.45 +#ifdef _WIN32 1.46 + 1.47 +inline char* MyStringGetNextCharPointer(char *p) 1.48 + { return CharNextA(p); } 1.49 +inline const char* MyStringGetNextCharPointer(const char *p) 1.50 + { return CharNextA(p); } 1.51 + 1.52 +inline char* MyStringGetPrevCharPointer(char *base, char *p) 1.53 + { return CharPrevA(base, p); } 1.54 +inline const char* MyStringGetPrevCharPointer(const char *base, const char *p) 1.55 + { return CharPrevA(base, p); } 1.56 + 1.57 +inline char MyCharUpper(char c) 1.58 + { return (char)(unsigned int)CharUpperA((LPSTR)(unsigned int)(unsigned char)c); } 1.59 +#ifdef _UNICODE 1.60 +inline wchar_t MyCharUpper(wchar_t c) 1.61 + { return (wchar_t)CharUpperW((LPWSTR)c); } 1.62 +#else 1.63 +wchar_t MyCharUpper(wchar_t c); 1.64 +#endif 1.65 + 1.66 +inline char MyCharLower(char c) 1.67 + { return (char)(unsigned int)CharLowerA((LPSTR)(unsigned int)(unsigned char)c); } 1.68 +#ifdef _UNICODE 1.69 +inline wchar_t MyCharLower(wchar_t c) 1.70 + { return (wchar_t)CharLowerW((LPWSTR)c); } 1.71 +#else 1.72 +wchar_t MyCharLower(wchar_t c); 1.73 +#endif 1.74 + 1.75 +inline char * MyStringUpper(char *s) { return CharUpperA(s); } 1.76 +#ifdef _UNICODE 1.77 +inline wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); } 1.78 +#else 1.79 +wchar_t * MyStringUpper(wchar_t *s); 1.80 +#endif 1.81 + 1.82 +inline char * MyStringLower(char *s) { return CharLowerA(s); } 1.83 +#ifdef _UNICODE 1.84 +inline wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); } 1.85 +#else 1.86 +wchar_t * MyStringLower(wchar_t *s); 1.87 +#endif 1.88 + 1.89 +#else // Standard-C 1.90 +wchar_t MyCharUpper(wchar_t c); 1.91 +#endif 1.92 + 1.93 +////////////////////////////////////// 1.94 +// Compare 1.95 + 1.96 +/* 1.97 +#ifndef _WIN32_WCE 1.98 +int MyStringCollate(const char *s1, const char *s2); 1.99 +int MyStringCollateNoCase(const char *s1, const char *s2); 1.100 +#endif 1.101 +int MyStringCollate(const wchar_t *s1, const wchar_t *s2); 1.102 +int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2); 1.103 +*/ 1.104 + 1.105 +int MyStringCompare(const char *s1, const char *s2); 1.106 +int MyStringCompare(const wchar_t *s1, const wchar_t *s2); 1.107 + 1.108 +#ifdef _WIN32 1.109 +int MyStringCompareNoCase(const char *s1, const char *s2); 1.110 +#endif 1.111 + 1.112 +int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2); 1.113 + 1.114 +template <class T> 1.115 +class CStringBase 1.116 +{ 1.117 + void TrimLeftWithCharSet(const CStringBase &charSet) 1.118 + { 1.119 + const T *p = _chars; 1.120 + while (charSet.Find(*p) >= 0 && (*p != 0)) 1.121 + p = GetNextCharPointer(p); 1.122 + Delete(0, (int)(p - _chars)); 1.123 + } 1.124 + void TrimRightWithCharSet(const CStringBase &charSet) 1.125 + { 1.126 + const T *p = _chars; 1.127 + const T *pLast = NULL; 1.128 + while (*p != 0) 1.129 + { 1.130 + if (charSet.Find(*p) >= 0) 1.131 + { 1.132 + if (pLast == NULL) 1.133 + pLast = p; 1.134 + } 1.135 + else 1.136 + pLast = NULL; 1.137 + p = GetNextCharPointer(p); 1.138 + } 1.139 + if(pLast != NULL) 1.140 + { 1.141 + int i = (int)(pLast - _chars); 1.142 + Delete(i, _length - i); 1.143 + } 1.144 + 1.145 + } 1.146 + void MoveItems(int destIndex, int srcIndex) 1.147 + { 1.148 + memmove(_chars + destIndex, _chars + srcIndex, 1.149 + sizeof(T) * (_length - srcIndex + 1)); 1.150 + } 1.151 + 1.152 + void InsertSpace(int &index, int size) 1.153 + { 1.154 + CorrectIndex(index); 1.155 + GrowLength(size); 1.156 + MoveItems(index + size, index); 1.157 + } 1.158 + 1.159 + static T *GetNextCharPointer(T *p) 1.160 + { return MyStringGetNextCharPointer(p); } 1.161 + static const T *GetNextCharPointer(const T *p) 1.162 + { return MyStringGetNextCharPointer(p); } 1.163 + static T *GetPrevCharPointer(T *base, T *p) 1.164 + { return MyStringGetPrevCharPointer(base, p); } 1.165 + static const T *GetPrevCharPointer(const T *base, const T *p) 1.166 + { return MyStringGetPrevCharPointer(base, p); } 1.167 +protected: 1.168 + T *_chars; 1.169 + int _length; 1.170 + int _capacity; 1.171 + 1.172 + void SetCapacity(int newCapacity) 1.173 + { 1.174 + int realCapacity = newCapacity + 1; 1.175 + if(realCapacity == _capacity) 1.176 + return; 1.177 + /* 1.178 + const int kMaxStringSize = 0x20000000; 1.179 + #ifndef _WIN32_WCE 1.180 + if(newCapacity > kMaxStringSize || newCapacity < _length) 1.181 + throw 1052337; 1.182 + #endif 1.183 + */ 1.184 + T *newBuffer = new T[realCapacity]; 1.185 + if(_capacity > 0) 1.186 + { 1.187 + for (int i = 0; i < (_length + 1); i++) 1.188 + newBuffer[i] = _chars[i]; 1.189 + delete []_chars; 1.190 + _chars = newBuffer; 1.191 + } 1.192 + else 1.193 + { 1.194 + _chars = newBuffer; 1.195 + _chars[0] = 0; 1.196 + } 1.197 + _capacity = realCapacity; 1.198 + } 1.199 + 1.200 + void GrowLength(int n) 1.201 + { 1.202 + int freeSize = _capacity - _length - 1; 1.203 + if (n <= freeSize) 1.204 + return; 1.205 + int delta; 1.206 + if (_capacity > 64) 1.207 + delta = _capacity / 2; 1.208 + else if (_capacity > 8) 1.209 + delta = 16; 1.210 + else 1.211 + delta = 4; 1.212 + if (freeSize + delta < n) 1.213 + delta = n - freeSize; 1.214 + SetCapacity(_capacity + delta); 1.215 + } 1.216 + 1.217 + void CorrectIndex(int &index) const 1.218 + { 1.219 + if (index > _length) 1.220 + index = _length; 1.221 + } 1.222 + 1.223 +public: 1.224 + CStringBase(): _chars(0), _length(0), _capacity(0) 1.225 + { SetCapacity(16 - 1); } 1.226 + CStringBase(T c): _chars(0), _length(0), _capacity(0) 1.227 + { 1.228 + SetCapacity(1); 1.229 + _chars[0] = c; 1.230 + _chars[1] = 0; 1.231 + _length = 1; 1.232 + } 1.233 + CStringBase(const T *chars): _chars(0), _length(0), _capacity(0) 1.234 + { 1.235 + int length = MyStringLen(chars); 1.236 + SetCapacity(length); 1.237 + MyStringCopy(_chars, chars); // can be optimized by memove() 1.238 + _length = length; 1.239 + } 1.240 + CStringBase(const CStringBase &s): _chars(0), _length(0), _capacity(0) 1.241 + { 1.242 + SetCapacity(s._length); 1.243 + MyStringCopy(_chars, s._chars); 1.244 + _length = s._length; 1.245 + } 1.246 + ~CStringBase() { delete []_chars; } 1.247 + 1.248 + operator const T*() const { return _chars;} 1.249 + 1.250 + // The minimum size of the character buffer in characters. 1.251 + // This value does not include space for a null terminator. 1.252 + T* GetBuffer(int minBufLength) 1.253 + { 1.254 + if(minBufLength >= _capacity) 1.255 + SetCapacity(minBufLength + 1); 1.256 + return _chars; 1.257 + } 1.258 + void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); } 1.259 + void ReleaseBuffer(int newLength) 1.260 + { 1.261 + /* 1.262 + #ifndef _WIN32_WCE 1.263 + if(newLength >= _capacity) 1.264 + throw 282217; 1.265 + #endif 1.266 + */ 1.267 + _chars[newLength] = 0; 1.268 + _length = newLength; 1.269 + } 1.270 + 1.271 + CStringBase& operator=(T c) 1.272 + { 1.273 + Empty(); 1.274 + SetCapacity(1); 1.275 + _chars[0] = c; 1.276 + _chars[1] = 0; 1.277 + _length = 1; 1.278 + return *this; 1.279 + } 1.280 + CStringBase& operator=(const T *chars) 1.281 + { 1.282 + Empty(); 1.283 + int length = MyStringLen(chars); 1.284 + SetCapacity(length); 1.285 + MyStringCopy(_chars, chars); 1.286 + _length = length; 1.287 + return *this; 1.288 + } 1.289 + CStringBase& operator=(const CStringBase& s) 1.290 + { 1.291 + if(&s == this) 1.292 + return *this; 1.293 + Empty(); 1.294 + SetCapacity(s._length); 1.295 + MyStringCopy(_chars, s._chars); 1.296 + _length = s._length; 1.297 + return *this; 1.298 + } 1.299 + 1.300 + CStringBase& operator+=(T c) 1.301 + { 1.302 + GrowLength(1); 1.303 + _chars[_length] = c; 1.304 + _chars[++_length] = 0; 1.305 + return *this; 1.306 + } 1.307 + CStringBase& operator+=(const T *s) 1.308 + { 1.309 + int len = MyStringLen(s); 1.310 + GrowLength(len); 1.311 + MyStringCopy(_chars + _length, s); 1.312 + _length += len; 1.313 + return *this; 1.314 + } 1.315 + CStringBase& operator+=(const CStringBase &s) 1.316 + { 1.317 + GrowLength(s._length); 1.318 + MyStringCopy(_chars + _length, s._chars); 1.319 + _length += s._length; 1.320 + return *this; 1.321 + } 1.322 + void Empty() 1.323 + { 1.324 + _length = 0; 1.325 + _chars[0] = 0; 1.326 + } 1.327 + int Length() const { return _length; } 1.328 + bool IsEmpty() const { return (_length == 0); } 1.329 + 1.330 + CStringBase Mid(int startIndex) const 1.331 + { return Mid(startIndex, _length - startIndex); } 1.332 + CStringBase Mid(int startIndex, int count ) const 1.333 + { 1.334 + if (startIndex + count > _length) 1.335 + count = _length - startIndex; 1.336 + 1.337 + if (startIndex == 0 && startIndex + count == _length) 1.338 + return *this; 1.339 + 1.340 + CStringBase<T> result; 1.341 + result.SetCapacity(count); 1.342 + // MyStringNCopy(result._chars, _chars + startIndex, count); 1.343 + for (int i = 0; i < count; i++) 1.344 + result._chars[i] = _chars[startIndex + i]; 1.345 + result._chars[count] = 0; 1.346 + result._length = count; 1.347 + return result; 1.348 + } 1.349 + CStringBase Left(int count) const 1.350 + { return Mid(0, count); } 1.351 + CStringBase Right(int count) const 1.352 + { 1.353 + if (count > _length) 1.354 + count = _length; 1.355 + return Mid(_length - count, count); 1.356 + } 1.357 + 1.358 + void MakeUpper() 1.359 + { MyStringUpper(_chars); } 1.360 + void MakeLower() 1.361 + { MyStringLower(_chars); } 1.362 + 1.363 + int Compare(const CStringBase& s) const 1.364 + { return MyStringCompare(_chars, s._chars); } 1.365 + 1.366 + int CompareNoCase(const CStringBase& s) const 1.367 + { return MyStringCompareNoCase(_chars, s._chars); } 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 + while (true) 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 + while (true) 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 + for(int i = 0; i < (int)(sizeof(kTrimDefaultCharSet) / 1.438 + sizeof(kTrimDefaultCharSet[0])); i++) 1.439 + charSet += (T)kTrimDefaultCharSet[i]; 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