1370b324cSopenharmony_ci// Common/MyString.h 2370b324cSopenharmony_ci 3370b324cSopenharmony_ci#ifndef ZIP7_INC_COMMON_MY_STRING_H 4370b324cSopenharmony_ci#define ZIP7_INC_COMMON_MY_STRING_H 5370b324cSopenharmony_ci 6370b324cSopenharmony_ci#include <string.h> 7370b324cSopenharmony_ci 8370b324cSopenharmony_ci#ifndef _WIN32 9370b324cSopenharmony_ci#include <wctype.h> 10370b324cSopenharmony_ci#include <wchar.h> 11370b324cSopenharmony_ci#endif 12370b324cSopenharmony_ci 13370b324cSopenharmony_ci#include "Common.h" 14370b324cSopenharmony_ci#include "MyWindows.h" 15370b324cSopenharmony_ci#include "MyTypes.h" 16370b324cSopenharmony_ci#include "MyVector.h" 17370b324cSopenharmony_ci 18370b324cSopenharmony_ci 19370b324cSopenharmony_ci/* if (DEBUG_FSTRING_INHERITS_ASTRING is defined), then 20370b324cSopenharmony_ci FString inherits from AString, so we can find bugs related to FString at compile time. 21370b324cSopenharmony_ci DON'T define DEBUG_FSTRING_INHERITS_ASTRING in release code */ 22370b324cSopenharmony_ci 23370b324cSopenharmony_ci// #define DEBUG_FSTRING_INHERITS_ASTRING 24370b324cSopenharmony_ci 25370b324cSopenharmony_ci#ifdef DEBUG_FSTRING_INHERITS_ASTRING 26370b324cSopenharmony_ciclass FString; 27370b324cSopenharmony_ci#endif 28370b324cSopenharmony_ci 29370b324cSopenharmony_ci 30370b324cSopenharmony_ci#ifdef _MSC_VER 31370b324cSopenharmony_ci #ifdef _NATIVE_WCHAR_T_DEFINED 32370b324cSopenharmony_ci #define MY_NATIVE_WCHAR_T_DEFINED 33370b324cSopenharmony_ci #endif 34370b324cSopenharmony_ci#else 35370b324cSopenharmony_ci #define MY_NATIVE_WCHAR_T_DEFINED 36370b324cSopenharmony_ci#endif 37370b324cSopenharmony_ci 38370b324cSopenharmony_ci/* 39370b324cSopenharmony_ci native support for wchar_t: 40370b324cSopenharmony_ci _MSC_VER == 1600 : /Zc:wchar_t is not supported 41370b324cSopenharmony_ci _MSC_VER == 1310 (VS2003) 42370b324cSopenharmony_ci ? _MSC_VER == 1400 (VS2005) : wchar_t <- unsigned short 43370b324cSopenharmony_ci /Zc:wchar_t : wchar_t <- __wchar_t, _WCHAR_T_DEFINED and _NATIVE_WCHAR_T_DEFINED 44370b324cSopenharmony_ci _MSC_VER > 1400 (VS2008+) 45370b324cSopenharmony_ci /Zc:wchar_t[-] 46370b324cSopenharmony_ci /Zc:wchar_t is on by default 47370b324cSopenharmony_ci*/ 48370b324cSopenharmony_ci 49370b324cSopenharmony_ci#ifdef _WIN32 50370b324cSopenharmony_ci#define IS_PATH_SEPAR(c) ((c) == '\\' || (c) == '/') 51370b324cSopenharmony_ci#else 52370b324cSopenharmony_ci#define IS_PATH_SEPAR(c) ((c) == CHAR_PATH_SEPARATOR) 53370b324cSopenharmony_ci#endif 54370b324cSopenharmony_ci 55370b324cSopenharmony_ciinline bool IsPathSepar(char c) { return IS_PATH_SEPAR(c); } 56370b324cSopenharmony_ciinline bool IsPathSepar(wchar_t c) { return IS_PATH_SEPAR(c); } 57370b324cSopenharmony_ci 58370b324cSopenharmony_ciinline unsigned MyStringLen(const char *s) 59370b324cSopenharmony_ci{ 60370b324cSopenharmony_ci unsigned i; 61370b324cSopenharmony_ci for (i = 0; s[i] != 0; i++); 62370b324cSopenharmony_ci return i; 63370b324cSopenharmony_ci} 64370b324cSopenharmony_ci 65370b324cSopenharmony_ciinline void MyStringCopy(char *dest, const char *src) 66370b324cSopenharmony_ci{ 67370b324cSopenharmony_ci while ((*dest++ = *src++) != 0); 68370b324cSopenharmony_ci} 69370b324cSopenharmony_ci 70370b324cSopenharmony_ciinline char *MyStpCpy(char *dest, const char *src) 71370b324cSopenharmony_ci{ 72370b324cSopenharmony_ci for (;;) 73370b324cSopenharmony_ci { 74370b324cSopenharmony_ci const char c = *src; 75370b324cSopenharmony_ci *dest = c; 76370b324cSopenharmony_ci if (c == 0) 77370b324cSopenharmony_ci return dest; 78370b324cSopenharmony_ci src++; 79370b324cSopenharmony_ci dest++; 80370b324cSopenharmony_ci } 81370b324cSopenharmony_ci} 82370b324cSopenharmony_ci 83370b324cSopenharmony_ciinline void MyStringCat(char *dest, const char *src) 84370b324cSopenharmony_ci{ 85370b324cSopenharmony_ci for (; *dest != 0; dest++); 86370b324cSopenharmony_ci while ((*dest++ = *src++) != 0); 87370b324cSopenharmony_ci // MyStringCopy(dest + MyStringLen(dest), src); 88370b324cSopenharmony_ci} 89370b324cSopenharmony_ci 90370b324cSopenharmony_ciinline unsigned MyStringLen(const wchar_t *s) 91370b324cSopenharmony_ci{ 92370b324cSopenharmony_ci unsigned i; 93370b324cSopenharmony_ci for (i = 0; s[i] != 0; i++); 94370b324cSopenharmony_ci return i; 95370b324cSopenharmony_ci} 96370b324cSopenharmony_ci 97370b324cSopenharmony_ciinline void MyStringCopy(wchar_t *dest, const wchar_t *src) 98370b324cSopenharmony_ci{ 99370b324cSopenharmony_ci while ((*dest++ = *src++) != 0); 100370b324cSopenharmony_ci} 101370b324cSopenharmony_ci 102370b324cSopenharmony_ciinline void MyStringCat(wchar_t *dest, const wchar_t *src) 103370b324cSopenharmony_ci{ 104370b324cSopenharmony_ci for (; *dest != 0; dest++); 105370b324cSopenharmony_ci while ((*dest++ = *src++) != 0); 106370b324cSopenharmony_ci // MyStringCopy(dest + MyStringLen(dest), src); 107370b324cSopenharmony_ci} 108370b324cSopenharmony_ci 109370b324cSopenharmony_ci 110370b324cSopenharmony_ci/* 111370b324cSopenharmony_ciinline wchar_t *MyWcpCpy(wchar_t *dest, const wchar_t *src) 112370b324cSopenharmony_ci{ 113370b324cSopenharmony_ci for (;;) 114370b324cSopenharmony_ci { 115370b324cSopenharmony_ci const wchar_t c = *src; 116370b324cSopenharmony_ci *dest = c; 117370b324cSopenharmony_ci if (c == 0) 118370b324cSopenharmony_ci return dest; 119370b324cSopenharmony_ci src++; 120370b324cSopenharmony_ci dest++; 121370b324cSopenharmony_ci } 122370b324cSopenharmony_ci} 123370b324cSopenharmony_ci*/ 124370b324cSopenharmony_ci 125370b324cSopenharmony_ciint FindCharPosInString(const char *s, char c) throw(); 126370b324cSopenharmony_ciint FindCharPosInString(const wchar_t *s, wchar_t c) throw(); 127370b324cSopenharmony_ci 128370b324cSopenharmony_ci#ifdef _WIN32 129370b324cSopenharmony_ci #ifndef _UNICODE 130370b324cSopenharmony_ci #define STRING_UNICODE_THROW 131370b324cSopenharmony_ci #endif 132370b324cSopenharmony_ci#endif 133370b324cSopenharmony_ci 134370b324cSopenharmony_ci#ifndef STRING_UNICODE_THROW 135370b324cSopenharmony_ci #define STRING_UNICODE_THROW throw() 136370b324cSopenharmony_ci#endif 137370b324cSopenharmony_ci 138370b324cSopenharmony_ci 139370b324cSopenharmony_ciinline char MyCharUpper_Ascii(char c) 140370b324cSopenharmony_ci{ 141370b324cSopenharmony_ci if (c >= 'a' && c <= 'z') 142370b324cSopenharmony_ci return (char)((unsigned char)c - 0x20); 143370b324cSopenharmony_ci return c; 144370b324cSopenharmony_ci} 145370b324cSopenharmony_ci 146370b324cSopenharmony_ci/* 147370b324cSopenharmony_ciinline wchar_t MyCharUpper_Ascii(wchar_t c) 148370b324cSopenharmony_ci{ 149370b324cSopenharmony_ci if (c >= 'a' && c <= 'z') 150370b324cSopenharmony_ci return (wchar_t)(c - 0x20); 151370b324cSopenharmony_ci return c; 152370b324cSopenharmony_ci} 153370b324cSopenharmony_ci*/ 154370b324cSopenharmony_ci 155370b324cSopenharmony_ciinline char MyCharLower_Ascii(char c) 156370b324cSopenharmony_ci{ 157370b324cSopenharmony_ci if (c >= 'A' && c <= 'Z') 158370b324cSopenharmony_ci return (char)((unsigned char)c + 0x20); 159370b324cSopenharmony_ci return c; 160370b324cSopenharmony_ci} 161370b324cSopenharmony_ci 162370b324cSopenharmony_ciinline wchar_t MyCharLower_Ascii(wchar_t c) 163370b324cSopenharmony_ci{ 164370b324cSopenharmony_ci if (c >= 'A' && c <= 'Z') 165370b324cSopenharmony_ci return (wchar_t)(c + 0x20); 166370b324cSopenharmony_ci return c; 167370b324cSopenharmony_ci} 168370b324cSopenharmony_ci 169370b324cSopenharmony_ciwchar_t MyCharUpper_WIN(wchar_t c) throw(); 170370b324cSopenharmony_ci 171370b324cSopenharmony_ciinline wchar_t MyCharUpper(wchar_t c) throw() 172370b324cSopenharmony_ci{ 173370b324cSopenharmony_ci if (c < 'a') return c; 174370b324cSopenharmony_ci if (c <= 'z') return (wchar_t)(c - 0x20); 175370b324cSopenharmony_ci if (c <= 0x7F) return c; 176370b324cSopenharmony_ci #ifdef _WIN32 177370b324cSopenharmony_ci #ifdef _UNICODE 178370b324cSopenharmony_ci return (wchar_t)(unsigned)(UINT_PTR)CharUpperW((LPWSTR)(UINT_PTR)(unsigned)c); 179370b324cSopenharmony_ci #else 180370b324cSopenharmony_ci return (wchar_t)MyCharUpper_WIN(c); 181370b324cSopenharmony_ci #endif 182370b324cSopenharmony_ci #else 183370b324cSopenharmony_ci return (wchar_t)towupper((wint_t)c); 184370b324cSopenharmony_ci #endif 185370b324cSopenharmony_ci} 186370b324cSopenharmony_ci 187370b324cSopenharmony_ci/* 188370b324cSopenharmony_ciwchar_t MyCharLower_WIN(wchar_t c) throw(); 189370b324cSopenharmony_ci 190370b324cSopenharmony_ciinline wchar_t MyCharLower(wchar_t c) throw() 191370b324cSopenharmony_ci{ 192370b324cSopenharmony_ci if (c < 'A') return c; 193370b324cSopenharmony_ci if (c <= 'Z') return (wchar_t)(c + 0x20); 194370b324cSopenharmony_ci if (c <= 0x7F) return c; 195370b324cSopenharmony_ci #ifdef _WIN32 196370b324cSopenharmony_ci #ifdef _UNICODE 197370b324cSopenharmony_ci return (wchar_t)(unsigned)(UINT_PTR)CharLowerW((LPWSTR)(UINT_PTR)(unsigned)c); 198370b324cSopenharmony_ci #else 199370b324cSopenharmony_ci return (wchar_t)MyCharLower_WIN(c); 200370b324cSopenharmony_ci #endif 201370b324cSopenharmony_ci #else 202370b324cSopenharmony_ci return (wchar_t)tolower(c); 203370b324cSopenharmony_ci #endif 204370b324cSopenharmony_ci} 205370b324cSopenharmony_ci*/ 206370b324cSopenharmony_ci 207370b324cSopenharmony_ci// char *MyStringUpper(char *s) throw(); 208370b324cSopenharmony_ci// char *MyStringLower(char *s) throw(); 209370b324cSopenharmony_ci 210370b324cSopenharmony_ci// void MyStringUpper_Ascii(char *s) throw(); 211370b324cSopenharmony_ci// void MyStringUpper_Ascii(wchar_t *s) throw(); 212370b324cSopenharmony_civoid MyStringLower_Ascii(char *s) throw(); 213370b324cSopenharmony_civoid MyStringLower_Ascii(wchar_t *s) throw(); 214370b324cSopenharmony_ci// wchar_t *MyStringUpper(wchar_t *s) STRING_UNICODE_THROW; 215370b324cSopenharmony_ci// wchar_t *MyStringLower(wchar_t *s) STRING_UNICODE_THROW; 216370b324cSopenharmony_ci 217370b324cSopenharmony_cibool StringsAreEqualNoCase(const wchar_t *s1, const wchar_t *s2) throw(); 218370b324cSopenharmony_ci 219370b324cSopenharmony_cibool IsString1PrefixedByString2(const char *s1, const char *s2) throw(); 220370b324cSopenharmony_cibool IsString1PrefixedByString2(const wchar_t *s1, const wchar_t *s2) throw(); 221370b324cSopenharmony_cibool IsString1PrefixedByString2(const wchar_t *s1, const char *s2) throw(); 222370b324cSopenharmony_cibool IsString1PrefixedByString2_NoCase_Ascii(const char *s1, const char *s2) throw(); 223370b324cSopenharmony_cibool IsString1PrefixedByString2_NoCase_Ascii(const wchar_t *u, const char *a) throw(); 224370b324cSopenharmony_cibool IsString1PrefixedByString2_NoCase(const wchar_t *s1, const wchar_t *s2) throw(); 225370b324cSopenharmony_ci 226370b324cSopenharmony_ci#define MyStringCompare(s1, s2) wcscmp(s1, s2) 227370b324cSopenharmony_ciint MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2) throw(); 228370b324cSopenharmony_ci// int MyStringCompareNoCase_N(const wchar_t *s1, const wchar_t *s2, unsigned num) throw(); 229370b324cSopenharmony_ci 230370b324cSopenharmony_ci// ---------- ASCII ---------- 231370b324cSopenharmony_ci// char values in ASCII strings must be less then 128 232370b324cSopenharmony_cibool StringsAreEqual_Ascii(const char *u, const char *a) throw(); 233370b324cSopenharmony_cibool StringsAreEqual_Ascii(const wchar_t *u, const char *a) throw(); 234370b324cSopenharmony_cibool StringsAreEqualNoCase_Ascii(const char *s1, const char *s2) throw(); 235370b324cSopenharmony_cibool StringsAreEqualNoCase_Ascii(const wchar_t *s1, const char *s2) throw(); 236370b324cSopenharmony_cibool StringsAreEqualNoCase_Ascii(const wchar_t *s1, const wchar_t *s2) throw(); 237370b324cSopenharmony_ci 238370b324cSopenharmony_ci#define MY_STRING_DELETE(_p_) { delete [](_p_); } 239370b324cSopenharmony_ci// #define MY_STRING_DELETE(_p_) my_delete(_p_); 240370b324cSopenharmony_ci 241370b324cSopenharmony_ci 242370b324cSopenharmony_ci#define FORBID_STRING_OPS_2(cls, t) \ 243370b324cSopenharmony_ci void Find(t) const; \ 244370b324cSopenharmony_ci void Find(t, unsigned startIndex) const; \ 245370b324cSopenharmony_ci void ReverseFind(t) const; \ 246370b324cSopenharmony_ci void InsertAtFront(t); \ 247370b324cSopenharmony_ci void RemoveChar(t); \ 248370b324cSopenharmony_ci void Replace(t, t); \ 249370b324cSopenharmony_ci 250370b324cSopenharmony_ci#define FORBID_STRING_OPS(cls, t) \ 251370b324cSopenharmony_ci explicit cls(t); \ 252370b324cSopenharmony_ci explicit cls(const t *); \ 253370b324cSopenharmony_ci cls &operator=(t); \ 254370b324cSopenharmony_ci cls &operator=(const t *); \ 255370b324cSopenharmony_ci cls &operator+=(t); \ 256370b324cSopenharmony_ci cls &operator+=(const t *); \ 257370b324cSopenharmony_ci FORBID_STRING_OPS_2(cls, t) \ 258370b324cSopenharmony_ci 259370b324cSopenharmony_ci/* 260370b324cSopenharmony_ci cls &operator+(t); \ 261370b324cSopenharmony_ci cls &operator+(const t *); \ 262370b324cSopenharmony_ci*/ 263370b324cSopenharmony_ci 264370b324cSopenharmony_ci#define FORBID_STRING_OPS_AString(t) FORBID_STRING_OPS(AString, t) 265370b324cSopenharmony_ci#define FORBID_STRING_OPS_UString(t) FORBID_STRING_OPS(UString, t) 266370b324cSopenharmony_ci#define FORBID_STRING_OPS_UString2(t) FORBID_STRING_OPS(UString2, t) 267370b324cSopenharmony_ci 268370b324cSopenharmony_ciclass AString 269370b324cSopenharmony_ci{ 270370b324cSopenharmony_ci char *_chars; 271370b324cSopenharmony_ci unsigned _len; 272370b324cSopenharmony_ci unsigned _limit; 273370b324cSopenharmony_ci 274370b324cSopenharmony_ci void MoveItems(unsigned dest, unsigned src) 275370b324cSopenharmony_ci { 276370b324cSopenharmony_ci memmove(_chars + dest, _chars + src, (size_t)(_len - src + 1) * sizeof(char)); 277370b324cSopenharmony_ci } 278370b324cSopenharmony_ci 279370b324cSopenharmony_ci void InsertSpace(unsigned &index, unsigned size); 280370b324cSopenharmony_ci 281370b324cSopenharmony_ci void ReAlloc(unsigned newLimit); 282370b324cSopenharmony_ci void ReAlloc2(unsigned newLimit); 283370b324cSopenharmony_ci void SetStartLen(unsigned len); 284370b324cSopenharmony_ci void Grow_1(); 285370b324cSopenharmony_ci void Grow(unsigned n); 286370b324cSopenharmony_ci 287370b324cSopenharmony_ci AString(unsigned num, const char *s); 288370b324cSopenharmony_ci AString(unsigned num, const AString &s); 289370b324cSopenharmony_ci AString(const AString &s, char c); // it's for String + char 290370b324cSopenharmony_ci AString(const char *s1, unsigned num1, const char *s2, unsigned num2); 291370b324cSopenharmony_ci 292370b324cSopenharmony_ci friend AString operator+(const AString &s, char c) { return AString(s, c); } 293370b324cSopenharmony_ci // friend AString operator+(char c, const AString &s); // is not supported 294370b324cSopenharmony_ci 295370b324cSopenharmony_ci friend AString operator+(const AString &s1, const AString &s2); 296370b324cSopenharmony_ci friend AString operator+(const AString &s1, const char *s2); 297370b324cSopenharmony_ci friend AString operator+(const char *s1, const AString &s2); 298370b324cSopenharmony_ci 299370b324cSopenharmony_ci // ---------- forbidden functions ---------- 300370b324cSopenharmony_ci 301370b324cSopenharmony_ci #ifdef MY_NATIVE_WCHAR_T_DEFINED 302370b324cSopenharmony_ci FORBID_STRING_OPS_AString(wchar_t) 303370b324cSopenharmony_ci #endif 304370b324cSopenharmony_ci 305370b324cSopenharmony_ci FORBID_STRING_OPS_AString(signed char) 306370b324cSopenharmony_ci FORBID_STRING_OPS_AString(unsigned char) 307370b324cSopenharmony_ci FORBID_STRING_OPS_AString(short) 308370b324cSopenharmony_ci FORBID_STRING_OPS_AString(unsigned short) 309370b324cSopenharmony_ci FORBID_STRING_OPS_AString(int) 310370b324cSopenharmony_ci FORBID_STRING_OPS_AString(unsigned) 311370b324cSopenharmony_ci FORBID_STRING_OPS_AString(long) 312370b324cSopenharmony_ci FORBID_STRING_OPS_AString(unsigned long) 313370b324cSopenharmony_ci 314370b324cSopenharmony_ci #ifdef DEBUG_FSTRING_INHERITS_ASTRING 315370b324cSopenharmony_ci AString(const FString &s); 316370b324cSopenharmony_ci AString &operator=(const FString &s); 317370b324cSopenharmony_ci AString &operator+=(const FString &s); 318370b324cSopenharmony_ci #endif 319370b324cSopenharmony_ci 320370b324cSopenharmony_cipublic: 321370b324cSopenharmony_ci explicit AString(); 322370b324cSopenharmony_ci explicit AString(char c); 323370b324cSopenharmony_ci explicit AString(const char *s); 324370b324cSopenharmony_ci AString(const AString &s); 325370b324cSopenharmony_ci ~AString() { MY_STRING_DELETE(_chars) } 326370b324cSopenharmony_ci 327370b324cSopenharmony_ci unsigned Len() const { return _len; } 328370b324cSopenharmony_ci bool IsEmpty() const { return _len == 0; } 329370b324cSopenharmony_ci void Empty() { _len = 0; _chars[0] = 0; } 330370b324cSopenharmony_ci 331370b324cSopenharmony_ci operator const char *() const { return _chars; } 332370b324cSopenharmony_ci char *Ptr_non_const() const { return _chars; } 333370b324cSopenharmony_ci const char *Ptr() const { return _chars; } 334370b324cSopenharmony_ci const char *Ptr(unsigned pos) const { return _chars + pos; } 335370b324cSopenharmony_ci const char *Ptr(int pos) const { return _chars + (unsigned)pos; } 336370b324cSopenharmony_ci const char *RightPtr(unsigned num) const { return _chars + _len - num; } 337370b324cSopenharmony_ci char Back() const { return _chars[(size_t)_len - 1]; } 338370b324cSopenharmony_ci 339370b324cSopenharmony_ci void ReplaceOneCharAtPos(unsigned pos, char c) { _chars[pos] = c; } 340370b324cSopenharmony_ci 341370b324cSopenharmony_ci char *GetBuf() { return _chars; } 342370b324cSopenharmony_ci /* GetBuf(minLen): provides the buffer that can store 343370b324cSopenharmony_ci at least (minLen) characters and additional null terminator. 344370b324cSopenharmony_ci 9.35: GetBuf doesn't preserve old characters and terminator */ 345370b324cSopenharmony_ci char *GetBuf(unsigned minLen) 346370b324cSopenharmony_ci { 347370b324cSopenharmony_ci if (minLen > _limit) 348370b324cSopenharmony_ci ReAlloc2(minLen); 349370b324cSopenharmony_ci return _chars; 350370b324cSopenharmony_ci } 351370b324cSopenharmony_ci char *GetBuf_SetEnd(unsigned minLen) 352370b324cSopenharmony_ci { 353370b324cSopenharmony_ci if (minLen > _limit) 354370b324cSopenharmony_ci ReAlloc2(minLen); 355370b324cSopenharmony_ci char *chars = _chars; 356370b324cSopenharmony_ci chars[minLen] = 0; 357370b324cSopenharmony_ci _len = minLen; 358370b324cSopenharmony_ci return chars; 359370b324cSopenharmony_ci } 360370b324cSopenharmony_ci 361370b324cSopenharmony_ci void ReleaseBuf_SetLen(unsigned newLen) { _len = newLen; } 362370b324cSopenharmony_ci void ReleaseBuf_SetEnd(unsigned newLen) { _len = newLen; _chars[newLen] = 0; } 363370b324cSopenharmony_ci void ReleaseBuf_CalcLen(unsigned maxLen) 364370b324cSopenharmony_ci { 365370b324cSopenharmony_ci char *chars = _chars; 366370b324cSopenharmony_ci chars[maxLen] = 0; 367370b324cSopenharmony_ci _len = MyStringLen(chars); 368370b324cSopenharmony_ci } 369370b324cSopenharmony_ci 370370b324cSopenharmony_ci AString &operator=(char c); 371370b324cSopenharmony_ci AString &operator=(const char *s); 372370b324cSopenharmony_ci AString &operator=(const AString &s); 373370b324cSopenharmony_ci void SetFromWStr_if_Ascii(const wchar_t *s); 374370b324cSopenharmony_ci // void SetFromBstr_if_Ascii(BSTR s); 375370b324cSopenharmony_ci 376370b324cSopenharmony_ci AString &operator+=(char c) 377370b324cSopenharmony_ci { 378370b324cSopenharmony_ci if (_limit == _len) 379370b324cSopenharmony_ci Grow_1(); 380370b324cSopenharmony_ci unsigned len = _len; 381370b324cSopenharmony_ci char *chars = _chars; 382370b324cSopenharmony_ci chars[len++] = c; 383370b324cSopenharmony_ci chars[len] = 0; 384370b324cSopenharmony_ci _len = len; 385370b324cSopenharmony_ci return *this; 386370b324cSopenharmony_ci } 387370b324cSopenharmony_ci 388370b324cSopenharmony_ci void Add_Space(); 389370b324cSopenharmony_ci void Add_Space_if_NotEmpty(); 390370b324cSopenharmony_ci void Add_OptSpaced(const char *s); 391370b324cSopenharmony_ci void Add_LF(); 392370b324cSopenharmony_ci void Add_Slash(); 393370b324cSopenharmony_ci void Add_Dot(); 394370b324cSopenharmony_ci void Add_Minus(); 395370b324cSopenharmony_ci void Add_PathSepar() { operator+=(CHAR_PATH_SEPARATOR); } 396370b324cSopenharmony_ci 397370b324cSopenharmony_ci AString &operator+=(const char *s); 398370b324cSopenharmony_ci AString &operator+=(const AString &s); 399370b324cSopenharmony_ci 400370b324cSopenharmony_ci void Add_UInt32(UInt32 v); 401370b324cSopenharmony_ci void Add_UInt64(UInt64 v); 402370b324cSopenharmony_ci 403370b324cSopenharmony_ci void AddFrom(const char *s, unsigned len); // no check 404370b324cSopenharmony_ci void SetFrom(const char *s, unsigned len); // no check 405370b324cSopenharmony_ci void SetFrom(const char* s, int len) // no check 406370b324cSopenharmony_ci { 407370b324cSopenharmony_ci SetFrom(s, (unsigned)len); // no check 408370b324cSopenharmony_ci } 409370b324cSopenharmony_ci void SetFrom_CalcLen(const char *s, unsigned len); 410370b324cSopenharmony_ci 411370b324cSopenharmony_ci AString Mid(unsigned startIndex, unsigned count) const { return AString(count, _chars + startIndex); } 412370b324cSopenharmony_ci AString Left(unsigned count) const { return AString(count, *this); } 413370b324cSopenharmony_ci // void MakeUpper() { MyStringUpper(_chars); } 414370b324cSopenharmony_ci // void MakeLower() { MyStringLower(_chars); } 415370b324cSopenharmony_ci void MakeLower_Ascii() { MyStringLower_Ascii(_chars); } 416370b324cSopenharmony_ci 417370b324cSopenharmony_ci 418370b324cSopenharmony_ci bool IsEqualTo(const char *s) const { return strcmp(_chars, s) == 0; } 419370b324cSopenharmony_ci bool IsEqualTo_Ascii_NoCase(const char *s) const { return StringsAreEqualNoCase_Ascii(_chars, s); } 420370b324cSopenharmony_ci // int Compare(const char *s) const { return MyStringCompare(_chars, s); } 421370b324cSopenharmony_ci // int Compare(const AString &s) const { return MyStringCompare(_chars, s._chars); } 422370b324cSopenharmony_ci // int CompareNoCase(const char *s) const { return MyStringCompareNoCase(_chars, s); } 423370b324cSopenharmony_ci // int CompareNoCase(const AString &s) const { return MyStringCompareNoCase(_chars, s._chars); } 424370b324cSopenharmony_ci bool IsPrefixedBy(const char *s) const { return IsString1PrefixedByString2(_chars, s); } 425370b324cSopenharmony_ci bool IsPrefixedBy_Ascii_NoCase(const char *s) const throw(); 426370b324cSopenharmony_ci 427370b324cSopenharmony_ci bool IsAscii() const 428370b324cSopenharmony_ci { 429370b324cSopenharmony_ci unsigned len = Len(); 430370b324cSopenharmony_ci const char *s = _chars; 431370b324cSopenharmony_ci for (unsigned i = 0; i < len; i++) 432370b324cSopenharmony_ci if ((unsigned char)s[i] >= 0x80) 433370b324cSopenharmony_ci return false; 434370b324cSopenharmony_ci return true; 435370b324cSopenharmony_ci } 436370b324cSopenharmony_ci int Find(char c) const { return FindCharPosInString(_chars, c); } 437370b324cSopenharmony_ci int Find(char c, unsigned startIndex) const 438370b324cSopenharmony_ci { 439370b324cSopenharmony_ci const int pos = FindCharPosInString(_chars + startIndex, c); 440370b324cSopenharmony_ci return pos < 0 ? -1 : (int)startIndex + pos; 441370b324cSopenharmony_ci } 442370b324cSopenharmony_ci int Find(char c, int startIndex) const 443370b324cSopenharmony_ci { 444370b324cSopenharmony_ci return Find(c, (unsigned)startIndex); 445370b324cSopenharmony_ci } 446370b324cSopenharmony_ci 447370b324cSopenharmony_ci int ReverseFind(char c) const throw(); 448370b324cSopenharmony_ci int ReverseFind_Dot() const throw() { return ReverseFind('.'); } 449370b324cSopenharmony_ci int ReverseFind_PathSepar() const throw(); 450370b324cSopenharmony_ci 451370b324cSopenharmony_ci int Find(const char *s) const { return Find(s, 0); } 452370b324cSopenharmony_ci int Find(const char *s, unsigned startIndex) const throw(); 453370b324cSopenharmony_ci 454370b324cSopenharmony_ci void TrimLeft() throw(); 455370b324cSopenharmony_ci void TrimRight() throw(); 456370b324cSopenharmony_ci void Trim() 457370b324cSopenharmony_ci { 458370b324cSopenharmony_ci TrimRight(); 459370b324cSopenharmony_ci TrimLeft(); 460370b324cSopenharmony_ci } 461370b324cSopenharmony_ci 462370b324cSopenharmony_ci void InsertAtFront(char c); 463370b324cSopenharmony_ci // void Insert(unsigned index, char c); 464370b324cSopenharmony_ci void Insert(unsigned index, const char *s); 465370b324cSopenharmony_ci void Insert(unsigned index, const AString &s); 466370b324cSopenharmony_ci 467370b324cSopenharmony_ci void RemoveChar(char ch) throw(); 468370b324cSopenharmony_ci 469370b324cSopenharmony_ci void Replace(char oldChar, char newChar) throw(); 470370b324cSopenharmony_ci void Replace(const AString &oldString, const AString &newString); 471370b324cSopenharmony_ci 472370b324cSopenharmony_ci void Delete(unsigned index) throw(); 473370b324cSopenharmony_ci void Delete(unsigned index, unsigned count) throw(); 474370b324cSopenharmony_ci void DeleteFrontal(unsigned num) throw(); 475370b324cSopenharmony_ci void DeleteBack() { _chars[--_len] = 0; } 476370b324cSopenharmony_ci void DeleteFrom(unsigned index) 477370b324cSopenharmony_ci { 478370b324cSopenharmony_ci if (index < _len) 479370b324cSopenharmony_ci { 480370b324cSopenharmony_ci _len = index; 481370b324cSopenharmony_ci _chars[index] = 0; 482370b324cSopenharmony_ci } 483370b324cSopenharmony_ci } 484370b324cSopenharmony_ci void DeleteFrom(int index) 485370b324cSopenharmony_ci { 486370b324cSopenharmony_ci DeleteFrom((unsigned)index); 487370b324cSopenharmony_ci } 488370b324cSopenharmony_ci 489370b324cSopenharmony_ci 490370b324cSopenharmony_ci void Wipe_and_Empty() 491370b324cSopenharmony_ci { 492370b324cSopenharmony_ci if (_chars) 493370b324cSopenharmony_ci { 494370b324cSopenharmony_ci memset(_chars, 0, (_limit + 1) * sizeof(*_chars)); 495370b324cSopenharmony_ci _len = 0; 496370b324cSopenharmony_ci } 497370b324cSopenharmony_ci } 498370b324cSopenharmony_ci}; 499370b324cSopenharmony_ci 500370b324cSopenharmony_ci 501370b324cSopenharmony_ciclass AString_Wipe: public AString 502370b324cSopenharmony_ci{ 503370b324cSopenharmony_ci Z7_CLASS_NO_COPY(AString_Wipe) 504370b324cSopenharmony_cipublic: 505370b324cSopenharmony_ci AString_Wipe(): AString() {} 506370b324cSopenharmony_ci // AString_Wipe(const AString &s): AString(s) {} 507370b324cSopenharmony_ci // AString_Wipe &operator=(const AString &s) { AString::operator=(s); return *this; } 508370b324cSopenharmony_ci // AString_Wipe &operator=(const char *s) { AString::operator=(s); return *this; } 509370b324cSopenharmony_ci ~AString_Wipe() { Wipe_and_Empty(); } 510370b324cSopenharmony_ci}; 511370b324cSopenharmony_ci 512370b324cSopenharmony_ci 513370b324cSopenharmony_cibool operator<(const AString &s1, const AString &s2); 514370b324cSopenharmony_cibool operator>(const AString &s1, const AString &s2); 515370b324cSopenharmony_ci 516370b324cSopenharmony_ci/* 517370b324cSopenharmony_cibool operator==(const AString &s1, const AString &s2); 518370b324cSopenharmony_cibool operator==(const AString &s1, const char *s2); 519370b324cSopenharmony_cibool operator==(const char *s1, const AString &s2); 520370b324cSopenharmony_ci 521370b324cSopenharmony_cibool operator!=(const AString &s1, const AString &s2); 522370b324cSopenharmony_cibool operator!=(const AString &s1, const char *s2); 523370b324cSopenharmony_cibool operator!=(const char *s1, const AString &s2); 524370b324cSopenharmony_ci*/ 525370b324cSopenharmony_ci 526370b324cSopenharmony_ciinline bool operator==(const AString &s1, const AString &s2) { return s1.Len() == s2.Len() && strcmp(s1, s2) == 0; } 527370b324cSopenharmony_ciinline bool operator==(const AString &s1, const char *s2) { return strcmp(s1, s2) == 0; } 528370b324cSopenharmony_ciinline bool operator==(const char *s1, const AString &s2) { return strcmp(s1, s2) == 0; } 529370b324cSopenharmony_ci 530370b324cSopenharmony_ciinline bool operator!=(const AString &s1, const AString &s2) { return s1.Len() != s2.Len() || strcmp(s1, s2) != 0; } 531370b324cSopenharmony_ciinline bool operator!=(const AString &s1, const char *s2) { return strcmp(s1, s2) != 0; } 532370b324cSopenharmony_ciinline bool operator!=(const char *s1, const AString &s2) { return strcmp(s1, s2) != 0; } 533370b324cSopenharmony_ci 534370b324cSopenharmony_ci// ---------- forbidden functions ---------- 535370b324cSopenharmony_ci 536370b324cSopenharmony_civoid operator==(char c1, const AString &s2); 537370b324cSopenharmony_civoid operator==(const AString &s1, char c2); 538370b324cSopenharmony_ci 539370b324cSopenharmony_civoid operator+(char c, const AString &s); // this function can be OK, but we don't use it 540370b324cSopenharmony_ci 541370b324cSopenharmony_civoid operator+(const AString &s, int c); 542370b324cSopenharmony_civoid operator+(const AString &s, unsigned c); 543370b324cSopenharmony_civoid operator+(int c, const AString &s); 544370b324cSopenharmony_civoid operator+(unsigned c, const AString &s); 545370b324cSopenharmony_civoid operator-(const AString &s, int c); 546370b324cSopenharmony_civoid operator-(const AString &s, unsigned c); 547370b324cSopenharmony_ci 548370b324cSopenharmony_ci 549370b324cSopenharmony_ciclass UString 550370b324cSopenharmony_ci{ 551370b324cSopenharmony_ci wchar_t *_chars; 552370b324cSopenharmony_ci unsigned _len; 553370b324cSopenharmony_ci unsigned _limit; 554370b324cSopenharmony_ci 555370b324cSopenharmony_ci void MoveItems(unsigned dest, unsigned src) 556370b324cSopenharmony_ci { 557370b324cSopenharmony_ci memmove(_chars + dest, _chars + src, (size_t)(_len - src + 1) * sizeof(wchar_t)); 558370b324cSopenharmony_ci } 559370b324cSopenharmony_ci 560370b324cSopenharmony_ci void InsertSpace(unsigned index, unsigned size); 561370b324cSopenharmony_ci 562370b324cSopenharmony_ci void ReAlloc(unsigned newLimit); 563370b324cSopenharmony_ci void ReAlloc2(unsigned newLimit); 564370b324cSopenharmony_ci void SetStartLen(unsigned len); 565370b324cSopenharmony_ci void Grow_1(); 566370b324cSopenharmony_ci void Grow(unsigned n); 567370b324cSopenharmony_ci 568370b324cSopenharmony_ci UString(unsigned num, const wchar_t *s); // for Mid 569370b324cSopenharmony_ci UString(unsigned num, const UString &s); // for Left 570370b324cSopenharmony_ci UString(const UString &s, wchar_t c); // it's for String + char 571370b324cSopenharmony_ci UString(const wchar_t *s1, unsigned num1, const wchar_t *s2, unsigned num2); 572370b324cSopenharmony_ci 573370b324cSopenharmony_ci friend UString operator+(const UString &s, wchar_t c) { return UString(s, c); } 574370b324cSopenharmony_ci // friend UString operator+(wchar_t c, const UString &s); // is not supported 575370b324cSopenharmony_ci 576370b324cSopenharmony_ci friend UString operator+(const UString &s1, const UString &s2); 577370b324cSopenharmony_ci friend UString operator+(const UString &s1, const wchar_t *s2); 578370b324cSopenharmony_ci friend UString operator+(const wchar_t *s1, const UString &s2); 579370b324cSopenharmony_ci 580370b324cSopenharmony_ci // ---------- forbidden functions ---------- 581370b324cSopenharmony_ci 582370b324cSopenharmony_ci FORBID_STRING_OPS_UString(signed char) 583370b324cSopenharmony_ci FORBID_STRING_OPS_UString(unsigned char) 584370b324cSopenharmony_ci FORBID_STRING_OPS_UString(short) 585370b324cSopenharmony_ci 586370b324cSopenharmony_ci #ifdef MY_NATIVE_WCHAR_T_DEFINED 587370b324cSopenharmony_ci FORBID_STRING_OPS_UString(unsigned short) 588370b324cSopenharmony_ci #endif 589370b324cSopenharmony_ci 590370b324cSopenharmony_ci FORBID_STRING_OPS_UString(int) 591370b324cSopenharmony_ci FORBID_STRING_OPS_UString(unsigned) 592370b324cSopenharmony_ci FORBID_STRING_OPS_UString(long) 593370b324cSopenharmony_ci FORBID_STRING_OPS_UString(unsigned long) 594370b324cSopenharmony_ci 595370b324cSopenharmony_ci FORBID_STRING_OPS_2(UString, char) 596370b324cSopenharmony_ci 597370b324cSopenharmony_ci #ifdef DEBUG_FSTRING_INHERITS_ASTRING 598370b324cSopenharmony_ci UString(const FString &s); 599370b324cSopenharmony_ci UString &operator=(const FString &s); 600370b324cSopenharmony_ci UString &operator+=(const FString &s); 601370b324cSopenharmony_ci #endif 602370b324cSopenharmony_ci 603370b324cSopenharmony_cipublic: 604370b324cSopenharmony_ci UString(); 605370b324cSopenharmony_ci explicit UString(wchar_t c); 606370b324cSopenharmony_ci explicit UString(char c); 607370b324cSopenharmony_ci explicit UString(const char *s); 608370b324cSopenharmony_ci explicit UString(const AString &s); 609370b324cSopenharmony_ci UString(const wchar_t *s); 610370b324cSopenharmony_ci UString(const UString &s); 611370b324cSopenharmony_ci ~UString() { MY_STRING_DELETE(_chars) } 612370b324cSopenharmony_ci 613370b324cSopenharmony_ci unsigned Len() const { return _len; } 614370b324cSopenharmony_ci bool IsEmpty() const { return _len == 0; } 615370b324cSopenharmony_ci void Empty() { _len = 0; _chars[0] = 0; } 616370b324cSopenharmony_ci 617370b324cSopenharmony_ci operator const wchar_t *() const { return _chars; } 618370b324cSopenharmony_ci wchar_t *Ptr_non_const() const { return _chars; } 619370b324cSopenharmony_ci const wchar_t *Ptr() const { return _chars; } 620370b324cSopenharmony_ci const wchar_t *Ptr(int pos) const { return _chars + (unsigned)pos; } 621370b324cSopenharmony_ci const wchar_t *Ptr(unsigned pos) const { return _chars + pos; } 622370b324cSopenharmony_ci const wchar_t *RightPtr(unsigned num) const { return _chars + _len - num; } 623370b324cSopenharmony_ci wchar_t Back() const { return _chars[(size_t)_len - 1]; } 624370b324cSopenharmony_ci 625370b324cSopenharmony_ci void ReplaceOneCharAtPos(unsigned pos, wchar_t c) { _chars[pos] = c; } 626370b324cSopenharmony_ci 627370b324cSopenharmony_ci wchar_t *GetBuf() { return _chars; } 628370b324cSopenharmony_ci 629370b324cSopenharmony_ci /* 630370b324cSopenharmony_ci wchar_t *GetBuf_GetMaxAvail(unsigned &availBufLen) 631370b324cSopenharmony_ci { 632370b324cSopenharmony_ci availBufLen = _limit; 633370b324cSopenharmony_ci return _chars; 634370b324cSopenharmony_ci } 635370b324cSopenharmony_ci */ 636370b324cSopenharmony_ci 637370b324cSopenharmony_ci wchar_t *GetBuf(unsigned minLen) 638370b324cSopenharmony_ci { 639370b324cSopenharmony_ci if (minLen > _limit) 640370b324cSopenharmony_ci ReAlloc2(minLen); 641370b324cSopenharmony_ci return _chars; 642370b324cSopenharmony_ci } 643370b324cSopenharmony_ci wchar_t *GetBuf_SetEnd(unsigned minLen) 644370b324cSopenharmony_ci { 645370b324cSopenharmony_ci if (minLen > _limit) 646370b324cSopenharmony_ci ReAlloc2(minLen); 647370b324cSopenharmony_ci wchar_t *chars = _chars; 648370b324cSopenharmony_ci chars[minLen] = 0; 649370b324cSopenharmony_ci _len = minLen; 650370b324cSopenharmony_ci return chars; 651370b324cSopenharmony_ci } 652370b324cSopenharmony_ci 653370b324cSopenharmony_ci void ReleaseBuf_SetLen(unsigned newLen) { _len = newLen; } 654370b324cSopenharmony_ci void ReleaseBuf_SetEnd(unsigned newLen) { _len = newLen; _chars[newLen] = 0; } 655370b324cSopenharmony_ci void ReleaseBuf_CalcLen(unsigned maxLen) 656370b324cSopenharmony_ci { 657370b324cSopenharmony_ci wchar_t *chars = _chars; 658370b324cSopenharmony_ci chars[maxLen] = 0; 659370b324cSopenharmony_ci _len = MyStringLen(chars); 660370b324cSopenharmony_ci } 661370b324cSopenharmony_ci 662370b324cSopenharmony_ci UString &operator=(wchar_t c); 663370b324cSopenharmony_ci UString &operator=(char c) { return (*this)=((wchar_t)(unsigned char)c); } 664370b324cSopenharmony_ci UString &operator=(const wchar_t *s); 665370b324cSopenharmony_ci UString &operator=(const UString &s); 666370b324cSopenharmony_ci void SetFrom(const wchar_t *s, unsigned len); // no check 667370b324cSopenharmony_ci void SetFromBstr(LPCOLESTR s); 668370b324cSopenharmony_ci UString &operator=(const char *s); 669370b324cSopenharmony_ci UString &operator=(const AString &s) { return operator=(s.Ptr()); } 670370b324cSopenharmony_ci 671370b324cSopenharmony_ci UString &operator+=(wchar_t c) 672370b324cSopenharmony_ci { 673370b324cSopenharmony_ci if (_limit == _len) 674370b324cSopenharmony_ci Grow_1(); 675370b324cSopenharmony_ci unsigned len = _len; 676370b324cSopenharmony_ci wchar_t *chars = _chars; 677370b324cSopenharmony_ci chars[len++] = c; 678370b324cSopenharmony_ci chars[len] = 0; 679370b324cSopenharmony_ci _len = len; 680370b324cSopenharmony_ci return *this; 681370b324cSopenharmony_ci } 682370b324cSopenharmony_ci 683370b324cSopenharmony_ci UString &operator+=(char c) { return (*this)+=((wchar_t)(unsigned char)c); } 684370b324cSopenharmony_ci 685370b324cSopenharmony_ci void Add_Space(); 686370b324cSopenharmony_ci void Add_Space_if_NotEmpty(); 687370b324cSopenharmony_ci void Add_LF(); 688370b324cSopenharmony_ci void Add_Dot(); 689370b324cSopenharmony_ci void Add_PathSepar() { operator+=(WCHAR_PATH_SEPARATOR); } 690370b324cSopenharmony_ci 691370b324cSopenharmony_ci UString &operator+=(const wchar_t *s); 692370b324cSopenharmony_ci UString &operator+=(const UString &s); 693370b324cSopenharmony_ci UString &operator+=(const char *s); 694370b324cSopenharmony_ci UString &operator+=(const AString &s) { return operator+=(s.Ptr()); } 695370b324cSopenharmony_ci 696370b324cSopenharmony_ci void Add_UInt32(UInt32 v); 697370b324cSopenharmony_ci void Add_UInt64(UInt64 v); 698370b324cSopenharmony_ci 699370b324cSopenharmony_ci UString Mid(unsigned startIndex, unsigned count) const { return UString(count, _chars + startIndex); } 700370b324cSopenharmony_ci UString Left(unsigned count) const { return UString(count, *this); } 701370b324cSopenharmony_ci UString Left(int count) const { return Left((unsigned)count); } 702370b324cSopenharmony_ci 703370b324cSopenharmony_ci // void MakeUpper() { MyStringUpper(_chars); } 704370b324cSopenharmony_ci // void MakeUpper() { MyStringUpper_Ascii(_chars); } 705370b324cSopenharmony_ci // void MakeUpper_Ascii() { MyStringUpper_Ascii(_chars); } 706370b324cSopenharmony_ci void MakeLower_Ascii() { MyStringLower_Ascii(_chars); } 707370b324cSopenharmony_ci 708370b324cSopenharmony_ci bool IsEqualTo(const char *s) const { return StringsAreEqual_Ascii(_chars, s); } 709370b324cSopenharmony_ci bool IsEqualTo_NoCase(const wchar_t *s) const { return StringsAreEqualNoCase(_chars, s); } 710370b324cSopenharmony_ci bool IsEqualTo_Ascii_NoCase(const char *s) const { return StringsAreEqualNoCase_Ascii(_chars, s); } 711370b324cSopenharmony_ci int Compare(const wchar_t *s) const { return wcscmp(_chars, s); } 712370b324cSopenharmony_ci // int Compare(const UString &s) const { return MyStringCompare(_chars, s._chars); } 713370b324cSopenharmony_ci // int CompareNoCase(const wchar_t *s) const { return MyStringCompareNoCase(_chars, s); } 714370b324cSopenharmony_ci // int CompareNoCase(const UString &s) const { return MyStringCompareNoCase(_chars, s._chars); } 715370b324cSopenharmony_ci bool IsPrefixedBy(const wchar_t *s) const { return IsString1PrefixedByString2(_chars, s); } 716370b324cSopenharmony_ci bool IsPrefixedBy_NoCase(const wchar_t *s) const { return IsString1PrefixedByString2_NoCase(_chars, s); } 717370b324cSopenharmony_ci bool IsPrefixedBy_Ascii_NoCase(const char *s) const throw(); 718370b324cSopenharmony_ci 719370b324cSopenharmony_ci bool IsAscii() const 720370b324cSopenharmony_ci { 721370b324cSopenharmony_ci unsigned len = Len(); 722370b324cSopenharmony_ci const wchar_t *s = _chars; 723370b324cSopenharmony_ci for (unsigned i = 0; i < len; i++) 724370b324cSopenharmony_ci if (s[i] >= 0x80) 725370b324cSopenharmony_ci return false; 726370b324cSopenharmony_ci return true; 727370b324cSopenharmony_ci } 728370b324cSopenharmony_ci int Find(wchar_t c) const { return FindCharPosInString(_chars, c); } 729370b324cSopenharmony_ci int Find(wchar_t c, unsigned startIndex) const 730370b324cSopenharmony_ci { 731370b324cSopenharmony_ci int pos = FindCharPosInString(_chars + startIndex, c); 732370b324cSopenharmony_ci return pos < 0 ? -1 : (int)startIndex + pos; 733370b324cSopenharmony_ci } 734370b324cSopenharmony_ci 735370b324cSopenharmony_ci int ReverseFind(wchar_t c) const throw(); 736370b324cSopenharmony_ci int ReverseFind_Dot() const throw() { return ReverseFind(L'.'); } 737370b324cSopenharmony_ci int ReverseFind_PathSepar() const throw(); 738370b324cSopenharmony_ci 739370b324cSopenharmony_ci int Find(const wchar_t *s) const { return Find(s, 0); } 740370b324cSopenharmony_ci int Find(const wchar_t *s, unsigned startIndex) const throw(); 741370b324cSopenharmony_ci 742370b324cSopenharmony_ci void TrimLeft() throw(); 743370b324cSopenharmony_ci void TrimRight() throw(); 744370b324cSopenharmony_ci void Trim() 745370b324cSopenharmony_ci { 746370b324cSopenharmony_ci TrimRight(); 747370b324cSopenharmony_ci TrimLeft(); 748370b324cSopenharmony_ci } 749370b324cSopenharmony_ci 750370b324cSopenharmony_ci void InsertAtFront(wchar_t c); 751370b324cSopenharmony_ci // void Insert_wchar_t(unsigned index, wchar_t c); 752370b324cSopenharmony_ci void Insert(unsigned index, const wchar_t *s); 753370b324cSopenharmony_ci void Insert(unsigned index, const UString &s); 754370b324cSopenharmony_ci 755370b324cSopenharmony_ci void RemoveChar(wchar_t ch) throw(); 756370b324cSopenharmony_ci 757370b324cSopenharmony_ci void Replace(wchar_t oldChar, wchar_t newChar) throw(); 758370b324cSopenharmony_ci void Replace(const UString &oldString, const UString &newString); 759370b324cSopenharmony_ci 760370b324cSopenharmony_ci void Delete(int index) throw() { Delete((unsigned)index); } 761370b324cSopenharmony_ci void Delete(unsigned index) throw(); 762370b324cSopenharmony_ci void Delete(unsigned index, unsigned count) throw(); 763370b324cSopenharmony_ci void DeleteFrontal(unsigned num) throw(); 764370b324cSopenharmony_ci void DeleteBack() { _chars[--_len] = 0; } 765370b324cSopenharmony_ci void DeleteFrom(int index) { DeleteFrom((unsigned)index); } 766370b324cSopenharmony_ci void DeleteFrom(unsigned index) 767370b324cSopenharmony_ci { 768370b324cSopenharmony_ci if (index < _len) 769370b324cSopenharmony_ci { 770370b324cSopenharmony_ci _len = index; 771370b324cSopenharmony_ci _chars[index] = 0; 772370b324cSopenharmony_ci } 773370b324cSopenharmony_ci } 774370b324cSopenharmony_ci 775370b324cSopenharmony_ci void Wipe_and_Empty() 776370b324cSopenharmony_ci { 777370b324cSopenharmony_ci if (_chars) 778370b324cSopenharmony_ci { 779370b324cSopenharmony_ci memset(_chars, 0, (_limit + 1) * sizeof(*_chars)); 780370b324cSopenharmony_ci _len = 0; 781370b324cSopenharmony_ci } 782370b324cSopenharmony_ci } 783370b324cSopenharmony_ci}; 784370b324cSopenharmony_ci 785370b324cSopenharmony_ci 786370b324cSopenharmony_ciclass UString_Wipe: public UString 787370b324cSopenharmony_ci{ 788370b324cSopenharmony_ci Z7_CLASS_NO_COPY(UString_Wipe) 789370b324cSopenharmony_cipublic: 790370b324cSopenharmony_ci UString_Wipe(): UString() {} 791370b324cSopenharmony_ci // UString_Wipe(const UString &s): UString(s) {} 792370b324cSopenharmony_ci // UString_Wipe &operator=(const UString &s) { UString::operator=(s); return *this; } 793370b324cSopenharmony_ci // UString_Wipe &operator=(const wchar_t *s) { UString::operator=(s); return *this; } 794370b324cSopenharmony_ci ~UString_Wipe() { Wipe_and_Empty(); } 795370b324cSopenharmony_ci}; 796370b324cSopenharmony_ci 797370b324cSopenharmony_ci 798370b324cSopenharmony_cibool operator<(const UString &s1, const UString &s2); 799370b324cSopenharmony_cibool operator>(const UString &s1, const UString &s2); 800370b324cSopenharmony_ci 801370b324cSopenharmony_ciinline bool operator==(const UString &s1, const UString &s2) { return s1.Len() == s2.Len() && wcscmp(s1, s2) == 0; } 802370b324cSopenharmony_ciinline bool operator==(const UString &s1, const wchar_t *s2) { return wcscmp(s1, s2) == 0; } 803370b324cSopenharmony_ciinline bool operator==(const wchar_t *s1, const UString &s2) { return wcscmp(s1, s2) == 0; } 804370b324cSopenharmony_ci 805370b324cSopenharmony_ciinline bool operator!=(const UString &s1, const UString &s2) { return s1.Len() != s2.Len() || wcscmp(s1, s2) != 0; } 806370b324cSopenharmony_ciinline bool operator!=(const UString &s1, const wchar_t *s2) { return wcscmp(s1, s2) != 0; } 807370b324cSopenharmony_ciinline bool operator!=(const wchar_t *s1, const UString &s2) { return wcscmp(s1, s2) != 0; } 808370b324cSopenharmony_ci 809370b324cSopenharmony_ci 810370b324cSopenharmony_ci// ---------- forbidden functions ---------- 811370b324cSopenharmony_ci 812370b324cSopenharmony_civoid operator==(wchar_t c1, const UString &s2); 813370b324cSopenharmony_civoid operator==(const UString &s1, wchar_t c2); 814370b324cSopenharmony_ci 815370b324cSopenharmony_civoid operator+(wchar_t c, const UString &s); // this function can be OK, but we don't use it 816370b324cSopenharmony_ci 817370b324cSopenharmony_civoid operator+(const AString &s1, const UString &s2); 818370b324cSopenharmony_civoid operator+(const UString &s1, const AString &s2); 819370b324cSopenharmony_ci 820370b324cSopenharmony_civoid operator+(const UString &s1, const char *s2); 821370b324cSopenharmony_civoid operator+(const char *s1, const UString &s2); 822370b324cSopenharmony_ci 823370b324cSopenharmony_civoid operator+(const UString &s, char c); 824370b324cSopenharmony_civoid operator+(const UString &s, unsigned char c); 825370b324cSopenharmony_civoid operator+(char c, const UString &s); 826370b324cSopenharmony_civoid operator+(unsigned char c, const UString &s); 827370b324cSopenharmony_civoid operator-(const UString &s1, wchar_t c); 828370b324cSopenharmony_ci 829370b324cSopenharmony_ci#ifdef _WIN32 830370b324cSopenharmony_ci// can we forbid these functions, if wchar_t is 32-bit ? 831370b324cSopenharmony_civoid operator+(const UString &s, int c); 832370b324cSopenharmony_civoid operator+(const UString &s, unsigned c); 833370b324cSopenharmony_civoid operator+(int c, const UString &s); 834370b324cSopenharmony_civoid operator+(unsigned c, const UString &s); 835370b324cSopenharmony_civoid operator-(const UString &s1, int c); 836370b324cSopenharmony_civoid operator-(const UString &s1, unsigned c); 837370b324cSopenharmony_ci#endif 838370b324cSopenharmony_ci 839370b324cSopenharmony_ci 840370b324cSopenharmony_ci 841370b324cSopenharmony_ci 842370b324cSopenharmony_ci 843370b324cSopenharmony_ci 844370b324cSopenharmony_ci 845370b324cSopenharmony_ciclass UString2 846370b324cSopenharmony_ci{ 847370b324cSopenharmony_ci wchar_t *_chars; 848370b324cSopenharmony_ci unsigned _len; 849370b324cSopenharmony_ci 850370b324cSopenharmony_ci void ReAlloc2(unsigned newLimit); 851370b324cSopenharmony_ci void SetStartLen(unsigned len); 852370b324cSopenharmony_ci 853370b324cSopenharmony_ci // ---------- forbidden functions ---------- 854370b324cSopenharmony_ci 855370b324cSopenharmony_ci FORBID_STRING_OPS_UString2(char) 856370b324cSopenharmony_ci FORBID_STRING_OPS_UString2(signed char) 857370b324cSopenharmony_ci FORBID_STRING_OPS_UString2(unsigned char) 858370b324cSopenharmony_ci FORBID_STRING_OPS_UString2(short) 859370b324cSopenharmony_ci 860370b324cSopenharmony_ci UString2 &operator=(wchar_t c); 861370b324cSopenharmony_ci 862370b324cSopenharmony_ci UString2(const AString &s); 863370b324cSopenharmony_ci UString2 &operator=(const AString &s); 864370b324cSopenharmony_ci UString2 &operator+=(const AString &s); 865370b324cSopenharmony_ci 866370b324cSopenharmony_ci #ifdef DEBUG_FSTRING_INHERITS_ASTRING 867370b324cSopenharmony_ci UString2(const FString &s); 868370b324cSopenharmony_ci UString2 &operator=(const FString &s); 869370b324cSopenharmony_ci UString2 &operator+=(const FString &s); 870370b324cSopenharmony_ci #endif 871370b324cSopenharmony_ci 872370b324cSopenharmony_cipublic: 873370b324cSopenharmony_ci UString2(): _chars(NULL), _len(0) {} 874370b324cSopenharmony_ci UString2(const wchar_t *s); 875370b324cSopenharmony_ci UString2(const UString2 &s); 876370b324cSopenharmony_ci ~UString2() { if (_chars) { MY_STRING_DELETE(_chars) } } 877370b324cSopenharmony_ci 878370b324cSopenharmony_ci unsigned Len() const { return _len; } 879370b324cSopenharmony_ci bool IsEmpty() const { return _len == 0; } 880370b324cSopenharmony_ci // void Empty() { _len = 0; _chars[0] = 0; } 881370b324cSopenharmony_ci 882370b324cSopenharmony_ci // operator const wchar_t *() const { return _chars; } 883370b324cSopenharmony_ci const wchar_t *GetRawPtr() const { return _chars; } 884370b324cSopenharmony_ci 885370b324cSopenharmony_ci int Compare(const wchar_t *s) const { return wcscmp(_chars, s); } 886370b324cSopenharmony_ci 887370b324cSopenharmony_ci wchar_t *GetBuf(unsigned minLen) 888370b324cSopenharmony_ci { 889370b324cSopenharmony_ci if (!_chars || minLen > _len) 890370b324cSopenharmony_ci ReAlloc2(minLen); 891370b324cSopenharmony_ci return _chars; 892370b324cSopenharmony_ci } 893370b324cSopenharmony_ci void ReleaseBuf_SetLen(unsigned newLen) { _len = newLen; } 894370b324cSopenharmony_ci 895370b324cSopenharmony_ci UString2 &operator=(const wchar_t *s); 896370b324cSopenharmony_ci UString2 &operator=(const UString2 &s); 897370b324cSopenharmony_ci void SetFromAscii(const char *s); 898370b324cSopenharmony_ci}; 899370b324cSopenharmony_ci 900370b324cSopenharmony_cibool operator==(const UString2 &s1, const UString2 &s2); 901370b324cSopenharmony_cibool operator==(const UString2 &s1, const wchar_t *s2); 902370b324cSopenharmony_cibool operator==(const wchar_t *s1, const UString2 &s2); 903370b324cSopenharmony_ci 904370b324cSopenharmony_ciinline bool operator!=(const UString2 &s1, const UString2 &s2) { return !(s1 == s2); } 905370b324cSopenharmony_ciinline bool operator!=(const UString2 &s1, const wchar_t *s2) { return !(s1 == s2); } 906370b324cSopenharmony_ciinline bool operator!=(const wchar_t *s1, const UString2 &s2) { return !(s1 == s2); } 907370b324cSopenharmony_ci 908370b324cSopenharmony_ci 909370b324cSopenharmony_ci// ---------- forbidden functions ---------- 910370b324cSopenharmony_ci 911370b324cSopenharmony_civoid operator==(wchar_t c1, const UString2 &s2); 912370b324cSopenharmony_civoid operator==(const UString2 &s1, wchar_t c2); 913370b324cSopenharmony_cibool operator<(const UString2 &s1, const UString2 &s2); 914370b324cSopenharmony_cibool operator>(const UString2 &s1, const UString2 &s2); 915370b324cSopenharmony_ci 916370b324cSopenharmony_civoid operator+(const UString2 &s1, const UString2 &s2); 917370b324cSopenharmony_civoid operator+(const UString2 &s1, const wchar_t *s2); 918370b324cSopenharmony_civoid operator+(const wchar_t *s1, const UString2 &s2); 919370b324cSopenharmony_civoid operator+(wchar_t c, const UString2 &s); 920370b324cSopenharmony_civoid operator+(const UString2 &s, wchar_t c); 921370b324cSopenharmony_civoid operator+(const UString2 &s, char c); 922370b324cSopenharmony_civoid operator+(const UString2 &s, unsigned char c); 923370b324cSopenharmony_civoid operator+(char c, const UString2 &s); 924370b324cSopenharmony_civoid operator+(unsigned char c, const UString2 &s); 925370b324cSopenharmony_civoid operator-(const UString2 &s1, wchar_t c); 926370b324cSopenharmony_ci 927370b324cSopenharmony_ci 928370b324cSopenharmony_ci 929370b324cSopenharmony_ci 930370b324cSopenharmony_ci 931370b324cSopenharmony_ci 932370b324cSopenharmony_citypedef CObjectVector<AString> AStringVector; 933370b324cSopenharmony_citypedef CObjectVector<UString> UStringVector; 934370b324cSopenharmony_ci 935370b324cSopenharmony_ci#ifdef _UNICODE 936370b324cSopenharmony_ci typedef UString CSysString; 937370b324cSopenharmony_ci#else 938370b324cSopenharmony_ci typedef AString CSysString; 939370b324cSopenharmony_ci#endif 940370b324cSopenharmony_ci 941370b324cSopenharmony_citypedef CObjectVector<CSysString> CSysStringVector; 942370b324cSopenharmony_ci 943370b324cSopenharmony_ci 944370b324cSopenharmony_ci// ---------- FString ---------- 945370b324cSopenharmony_ci 946370b324cSopenharmony_ci#ifndef DEBUG_FSTRING_INHERITS_ASTRING 947370b324cSopenharmony_ci#ifdef _WIN32 948370b324cSopenharmony_ci #define USE_UNICODE_FSTRING 949370b324cSopenharmony_ci#endif 950370b324cSopenharmony_ci#endif 951370b324cSopenharmony_ci 952370b324cSopenharmony_ci#ifdef USE_UNICODE_FSTRING 953370b324cSopenharmony_ci 954370b324cSopenharmony_ci #define MY_FTEXT(quote) L##quote 955370b324cSopenharmony_ci 956370b324cSopenharmony_ci typedef wchar_t FChar; 957370b324cSopenharmony_ci typedef UString FString; 958370b324cSopenharmony_ci 959370b324cSopenharmony_ci #define fs2us(_x_) (_x_) 960370b324cSopenharmony_ci #define us2fs(_x_) (_x_) 961370b324cSopenharmony_ci FString fas2fs(const char *s); 962370b324cSopenharmony_ci FString fas2fs(const AString &s); 963370b324cSopenharmony_ci AString fs2fas(const FChar *s); 964370b324cSopenharmony_ci 965370b324cSopenharmony_ci#else // USE_UNICODE_FSTRING 966370b324cSopenharmony_ci 967370b324cSopenharmony_ci #define MY_FTEXT(quote) quote 968370b324cSopenharmony_ci 969370b324cSopenharmony_ci typedef char FChar; 970370b324cSopenharmony_ci 971370b324cSopenharmony_ci #ifdef DEBUG_FSTRING_INHERITS_ASTRING 972370b324cSopenharmony_ci 973370b324cSopenharmony_ci class FString: public AString 974370b324cSopenharmony_ci { 975370b324cSopenharmony_ci // FString &operator=(const char *s); 976370b324cSopenharmony_ci FString &operator=(const AString &s); 977370b324cSopenharmony_ci // FString &operator+=(const AString &s); 978370b324cSopenharmony_ci public: 979370b324cSopenharmony_ci FString(const AString &s): AString(s.Ptr()) {} 980370b324cSopenharmony_ci FString(const FString &s): AString(s.Ptr()) {} 981370b324cSopenharmony_ci FString(const char *s): AString(s) {} 982370b324cSopenharmony_ci FString() {} 983370b324cSopenharmony_ci FString &operator=(const FString &s) { AString::operator=((const AString &)s); return *this; } 984370b324cSopenharmony_ci FString &operator=(char c) { AString::operator=(c); return *this; } 985370b324cSopenharmony_ci FString &operator+=(char c) { AString::operator+=(c); return *this; } 986370b324cSopenharmony_ci FString &operator+=(const FString &s) { AString::operator+=((const AString &)s); return *this; } 987370b324cSopenharmony_ci FString Left(unsigned count) const { return FString(AString::Left(count)); } 988370b324cSopenharmony_ci }; 989370b324cSopenharmony_ci void operator+(const AString &s1, const FString &s2); 990370b324cSopenharmony_ci void operator+(const FString &s1, const AString &s2); 991370b324cSopenharmony_ci 992370b324cSopenharmony_ci inline FString operator+(const FString &s1, const FString &s2) 993370b324cSopenharmony_ci { 994370b324cSopenharmony_ci AString s =(const AString &)s1 + (const AString &)s2; 995370b324cSopenharmony_ci return FString(s.Ptr()); 996370b324cSopenharmony_ci // return FString((const AString &)s1 + (const AString &)s2); 997370b324cSopenharmony_ci } 998370b324cSopenharmony_ci inline FString operator+(const FString &s1, const FChar *s2) 999370b324cSopenharmony_ci { 1000370b324cSopenharmony_ci return s1 + (FString)s2; 1001370b324cSopenharmony_ci } 1002370b324cSopenharmony_ci /* 1003370b324cSopenharmony_ci inline FString operator+(const FChar *s1, const FString &s2) 1004370b324cSopenharmony_ci { 1005370b324cSopenharmony_ci return (FString)s1 + s2; 1006370b324cSopenharmony_ci } 1007370b324cSopenharmony_ci */ 1008370b324cSopenharmony_ci 1009370b324cSopenharmony_ci inline FString fas2fs(const char *s) { return FString(s); } 1010370b324cSopenharmony_ci 1011370b324cSopenharmony_ci #else // DEBUG_FSTRING_INHERITS_ASTRING 1012370b324cSopenharmony_ci typedef AString FString; 1013370b324cSopenharmony_ci #define fas2fs(_x_) (_x_) 1014370b324cSopenharmony_ci #endif // DEBUG_FSTRING_INHERITS_ASTRING 1015370b324cSopenharmony_ci 1016370b324cSopenharmony_ci UString fs2us(const FChar *s); 1017370b324cSopenharmony_ci UString fs2us(const FString &s); 1018370b324cSopenharmony_ci FString us2fs(const wchar_t *s); 1019370b324cSopenharmony_ci #define fs2fas(_x_) (_x_) 1020370b324cSopenharmony_ci 1021370b324cSopenharmony_ci#endif // USE_UNICODE_FSTRING 1022370b324cSopenharmony_ci 1023370b324cSopenharmony_ci#define FTEXT(quote) MY_FTEXT(quote) 1024370b324cSopenharmony_ci 1025370b324cSopenharmony_ci#define FCHAR_PATH_SEPARATOR FTEXT(CHAR_PATH_SEPARATOR) 1026370b324cSopenharmony_ci#define FSTRING_PATH_SEPARATOR FTEXT(STRING_PATH_SEPARATOR) 1027370b324cSopenharmony_ci 1028370b324cSopenharmony_ci// #define FCHAR_ANY_MASK FTEXT('*') 1029370b324cSopenharmony_ci// #define FSTRING_ANY_MASK FTEXT("*") 1030370b324cSopenharmony_ci 1031370b324cSopenharmony_citypedef const FChar *CFSTR; 1032370b324cSopenharmony_ci 1033370b324cSopenharmony_citypedef CObjectVector<FString> FStringVector; 1034370b324cSopenharmony_ci 1035370b324cSopenharmony_ci 1036370b324cSopenharmony_ciclass CStringFinder 1037370b324cSopenharmony_ci{ 1038370b324cSopenharmony_ci AString _temp; 1039370b324cSopenharmony_cipublic: 1040370b324cSopenharmony_ci // list - is list of low case Ascii strings separated by space " ". 1041370b324cSopenharmony_ci // the function returns true, if it can find exact word (str) in (list). 1042370b324cSopenharmony_ci bool FindWord_In_LowCaseAsciiList_NoCase(const char *list, const wchar_t *str); 1043370b324cSopenharmony_ci}; 1044370b324cSopenharmony_ci 1045370b324cSopenharmony_civoid SplitString(const UString &srcString, UStringVector &destStrings); 1046370b324cSopenharmony_ci 1047370b324cSopenharmony_ci#endif 1048370b324cSopenharmony_ci 1049370b324cSopenharmony_ci 1050370b324cSopenharmony_ci 1051370b324cSopenharmony_ci#if defined(_WIN32) 1052370b324cSopenharmony_ci // #include <wchar.h> 1053370b324cSopenharmony_ci // WCHAR_MAX is defined as ((wchar_t)-1) 1054370b324cSopenharmony_ci #define Z7_WCHART_IS_16BIT 1 1055370b324cSopenharmony_ci#elif (defined(WCHAR_MAX) && (WCHAR_MAX <= 0xffff)) \ 1056370b324cSopenharmony_ci || (defined(__SIZEOF_WCHAR_T__) && (__SIZEOF_WCHAR_T__ == 2)) 1057370b324cSopenharmony_ci #define Z7_WCHART_IS_16BIT 1 1058370b324cSopenharmony_ci#endif 1059370b324cSopenharmony_ci 1060370b324cSopenharmony_ci#if WCHAR_PATH_SEPARATOR == L'\\' 1061370b324cSopenharmony_ci// WSL scheme 1062370b324cSopenharmony_ci#define WCHAR_IN_FILE_NAME_BACKSLASH_REPLACEMENT ((wchar_t)((unsigned)(0xF000) + (unsigned)'\\')) 1063370b324cSopenharmony_ci// #define WCHAR_IN_FILE_NAME_BACKSLASH_REPLACEMENT '_' 1064370b324cSopenharmony_ci#endif 1065