diff options
| author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2024-05-14 00:00:00 +0000 |
|---|---|---|
| committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2024-05-15 23:55:04 +0500 |
| commit | fc662341e6f85da78ada0e443f6116b978f79f22 (patch) | |
| tree | 1be1cc402a7a9cbc18d4eeea6b141354c2d559e3 /CPP/Common/MyString.cpp | |
| parent | 5b39dc76f1bc82f941d5c800ab9f34407a06b53a (diff) | |
| download | 7zip-fc662341e6f85da78ada0e443f6116b978f79f22.tar.gz 7zip-fc662341e6f85da78ada0e443f6116b978f79f22.tar.bz2 7zip-fc662341e6f85da78ada0e443f6116b978f79f22.zip | |
24.0524.05
Diffstat (limited to 'CPP/Common/MyString.cpp')
| -rw-r--r-- | CPP/Common/MyString.cpp | 70 |
1 files changed, 41 insertions, 29 deletions
diff --git a/CPP/Common/MyString.cpp b/CPP/Common/MyString.cpp index 51c1c3b..b5f7e52 100644 --- a/CPP/Common/MyString.cpp +++ b/CPP/Common/MyString.cpp | |||
| @@ -62,7 +62,7 @@ void MyStringUpper_Ascii(char *s) throw() | |||
| 62 | { | 62 | { |
| 63 | for (;;) | 63 | for (;;) |
| 64 | { | 64 | { |
| 65 | char c = *s; | 65 | const char c = *s; |
| 66 | if (c == 0) | 66 | if (c == 0) |
| 67 | return; | 67 | return; |
| 68 | *s++ = MyCharUpper_Ascii(c); | 68 | *s++ = MyCharUpper_Ascii(c); |
| @@ -73,7 +73,7 @@ void MyStringUpper_Ascii(wchar_t *s) throw() | |||
| 73 | { | 73 | { |
| 74 | for (;;) | 74 | for (;;) |
| 75 | { | 75 | { |
| 76 | wchar_t c = *s; | 76 | const wchar_t c = *s; |
| 77 | if (c == 0) | 77 | if (c == 0) |
| 78 | return; | 78 | return; |
| 79 | *s++ = MyCharUpper_Ascii(c); | 79 | *s++ = MyCharUpper_Ascii(c); |
| @@ -85,7 +85,7 @@ void MyStringLower_Ascii(char *s) throw() | |||
| 85 | { | 85 | { |
| 86 | for (;;) | 86 | for (;;) |
| 87 | { | 87 | { |
| 88 | char c = *s; | 88 | const char c = *s; |
| 89 | if (c == 0) | 89 | if (c == 0) |
| 90 | return; | 90 | return; |
| 91 | *s++ = MyCharLower_Ascii(c); | 91 | *s++ = MyCharLower_Ascii(c); |
| @@ -96,7 +96,7 @@ void MyStringLower_Ascii(wchar_t *s) throw() | |||
| 96 | { | 96 | { |
| 97 | for (;;) | 97 | for (;;) |
| 98 | { | 98 | { |
| 99 | wchar_t c = *s; | 99 | const wchar_t c = *s; |
| 100 | if (c == 0) | 100 | if (c == 0) |
| 101 | return; | 101 | return; |
| 102 | *s++ = MyCharLower_Ascii(c); | 102 | *s++ = MyCharLower_Ascii(c); |
| @@ -190,8 +190,8 @@ bool IsString1PrefixedByString2(const char *s1, const char *s2) throw() | |||
| 190 | { | 190 | { |
| 191 | for (;;) | 191 | for (;;) |
| 192 | { | 192 | { |
| 193 | const unsigned char c2 = (unsigned char)*s2++; if (c2 == 0) return true; | 193 | const char c2 = *s2++; if (c2 == 0) return true; |
| 194 | const unsigned char c1 = (unsigned char)*s1++; if (c1 != c2) return false; | 194 | const char c1 = *s1++; if (c1 != c2) return false; |
| 195 | } | 195 | } |
| 196 | } | 196 | } |
| 197 | 197 | ||
| @@ -402,6 +402,7 @@ void AString::InsertSpace(unsigned &index, unsigned size) | |||
| 402 | } | 402 | } |
| 403 | 403 | ||
| 404 | #define k_Alloc_Len_Limit (0x40000000 - 2) | 404 | #define k_Alloc_Len_Limit (0x40000000 - 2) |
| 405 | // #define k_Alloc_Len_Limit (((unsigned)1 << (sizeof(unsigned) * 8 - 2)) - 2) | ||
| 405 | 406 | ||
| 406 | void AString::ReAlloc(unsigned newLimit) | 407 | void AString::ReAlloc(unsigned newLimit) |
| 407 | { | 408 | { |
| @@ -413,9 +414,14 @@ void AString::ReAlloc(unsigned newLimit) | |||
| 413 | _limit = newLimit; | 414 | _limit = newLimit; |
| 414 | } | 415 | } |
| 415 | 416 | ||
| 417 | #define THROW_STRING_ALLOC_EXCEPTION { throw 20130220; } | ||
| 418 | |||
| 419 | #define CHECK_STRING_ALLOC_LEN(len) \ | ||
| 420 | { if ((len) > k_Alloc_Len_Limit) THROW_STRING_ALLOC_EXCEPTION } | ||
| 421 | |||
| 416 | void AString::ReAlloc2(unsigned newLimit) | 422 | void AString::ReAlloc2(unsigned newLimit) |
| 417 | { | 423 | { |
| 418 | if (newLimit > k_Alloc_Len_Limit) throw 20130220; | 424 | CHECK_STRING_ALLOC_LEN(newLimit) |
| 419 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, 0); | 425 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, 0); |
| 420 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); | 426 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); |
| 421 | newBuf[0] = 0; | 427 | newBuf[0] = 0; |
| @@ -433,6 +439,7 @@ void AString::SetStartLen(unsigned len) | |||
| 433 | _limit = len; | 439 | _limit = len; |
| 434 | } | 440 | } |
| 435 | 441 | ||
| 442 | Z7_NO_INLINE | ||
| 436 | void AString::Grow_1() | 443 | void AString::Grow_1() |
| 437 | { | 444 | { |
| 438 | unsigned next = _len; | 445 | unsigned next = _len; |
| @@ -443,7 +450,7 @@ void AString::Grow_1() | |||
| 443 | if (next < _len || next > k_Alloc_Len_Limit) | 450 | if (next < _len || next > k_Alloc_Len_Limit) |
| 444 | next = k_Alloc_Len_Limit; | 451 | next = k_Alloc_Len_Limit; |
| 445 | if (next <= _len) | 452 | if (next <= _len) |
| 446 | throw 20130220; | 453 | THROW_STRING_ALLOC_EXCEPTION |
| 447 | ReAlloc(next); | 454 | ReAlloc(next); |
| 448 | // Grow(1); | 455 | // Grow(1); |
| 449 | } | 456 | } |
| @@ -461,7 +468,7 @@ void AString::Grow(unsigned n) | |||
| 461 | if (next < _len || next > k_Alloc_Len_Limit) | 468 | if (next < _len || next > k_Alloc_Len_Limit) |
| 462 | next = k_Alloc_Len_Limit; | 469 | next = k_Alloc_Len_Limit; |
| 463 | if (next <= _len || next - _len < n) | 470 | if (next <= _len || next - _len < n) |
| 464 | throw 20130220; | 471 | THROW_STRING_ALLOC_EXCEPTION |
| 465 | ReAlloc(next); | 472 | ReAlloc(next); |
| 466 | } | 473 | } |
| 467 | 474 | ||
| @@ -638,12 +645,14 @@ void AString::SetFromBstr_if_Ascii(BSTR s) | |||
| 638 | } | 645 | } |
| 639 | */ | 646 | */ |
| 640 | 647 | ||
| 648 | void AString::Add_Char(char c) { operator+=(c); } | ||
| 641 | void AString::Add_Space() { operator+=(' '); } | 649 | void AString::Add_Space() { operator+=(' '); } |
| 642 | void AString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } | 650 | void AString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } |
| 643 | void AString::Add_LF() { operator+=('\n'); } | 651 | void AString::Add_LF() { operator+=('\n'); } |
| 644 | void AString::Add_Slash() { operator+=('/'); } | 652 | void AString::Add_Slash() { operator+=('/'); } |
| 645 | void AString::Add_Dot() { operator+=('.'); } | 653 | void AString::Add_Dot() { operator+=('.'); } |
| 646 | void AString::Add_Minus() { operator+=('-'); } | 654 | void AString::Add_Minus() { operator+=('-'); } |
| 655 | void AString::Add_Colon() { operator+=(':'); } | ||
| 647 | 656 | ||
| 648 | AString &AString::operator+=(const char *s) | 657 | AString &AString::operator+=(const char *s) |
| 649 | { | 658 | { |
| @@ -696,6 +705,7 @@ void AString::SetFrom(const char *s, unsigned len) // no check | |||
| 696 | { | 705 | { |
| 697 | if (len > _limit) | 706 | if (len > _limit) |
| 698 | { | 707 | { |
| 708 | CHECK_STRING_ALLOC_LEN(len) | ||
| 699 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); | 709 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
| 700 | MY_STRING_DELETE(_chars) | 710 | MY_STRING_DELETE(_chars) |
| 701 | _chars = newBuf; | 711 | _chars = newBuf; |
| @@ -707,6 +717,12 @@ void AString::SetFrom(const char *s, unsigned len) // no check | |||
| 707 | _len = len; | 717 | _len = len; |
| 708 | } | 718 | } |
| 709 | 719 | ||
| 720 | void AString::SetFrom_Chars_SizeT(const char *s, size_t len) | ||
| 721 | { | ||
| 722 | CHECK_STRING_ALLOC_LEN(len) | ||
| 723 | SetFrom(s, (unsigned)len); | ||
| 724 | } | ||
| 725 | |||
| 710 | void AString::SetFrom_CalcLen(const char *s, unsigned len) // no check | 726 | void AString::SetFrom_CalcLen(const char *s, unsigned len) // no check |
| 711 | { | 727 | { |
| 712 | unsigned i; | 728 | unsigned i; |
| @@ -906,8 +922,8 @@ void AString::Replace(const AString &oldString, const AString &newString) | |||
| 906 | return; // 0; | 922 | return; // 0; |
| 907 | if (oldString == newString) | 923 | if (oldString == newString) |
| 908 | return; // 0; | 924 | return; // 0; |
| 909 | unsigned oldLen = oldString.Len(); | 925 | const unsigned oldLen = oldString.Len(); |
| 910 | unsigned newLen = newString.Len(); | 926 | const unsigned newLen = newString.Len(); |
| 911 | // unsigned number = 0; | 927 | // unsigned number = 0; |
| 912 | int pos = 0; | 928 | int pos = 0; |
| 913 | while ((unsigned)pos < _len) | 929 | while ((unsigned)pos < _len) |
| @@ -1011,7 +1027,7 @@ void UString::ReAlloc(unsigned newLimit) | |||
| 1011 | 1027 | ||
| 1012 | void UString::ReAlloc2(unsigned newLimit) | 1028 | void UString::ReAlloc2(unsigned newLimit) |
| 1013 | { | 1029 | { |
| 1014 | if (newLimit > k_Alloc_Len_Limit) throw 20130221; | 1030 | CHECK_STRING_ALLOC_LEN(newLimit) |
| 1015 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); | 1031 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); |
| 1016 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); | 1032 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); |
| 1017 | newBuf[0] = 0; | 1033 | newBuf[0] = 0; |
| @@ -1029,6 +1045,7 @@ void UString::SetStartLen(unsigned len) | |||
| 1029 | _limit = len; | 1045 | _limit = len; |
| 1030 | } | 1046 | } |
| 1031 | 1047 | ||
| 1048 | Z7_NO_INLINE | ||
| 1032 | void UString::Grow_1() | 1049 | void UString::Grow_1() |
| 1033 | { | 1050 | { |
| 1034 | unsigned next = _len; | 1051 | unsigned next = _len; |
| @@ -1039,7 +1056,7 @@ void UString::Grow_1() | |||
| 1039 | if (next < _len || next > k_Alloc_Len_Limit) | 1056 | if (next < _len || next > k_Alloc_Len_Limit) |
| 1040 | next = k_Alloc_Len_Limit; | 1057 | next = k_Alloc_Len_Limit; |
| 1041 | if (next <= _len) | 1058 | if (next <= _len) |
| 1042 | throw 20130220; | 1059 | THROW_STRING_ALLOC_EXCEPTION |
| 1043 | ReAlloc(next); | 1060 | ReAlloc(next); |
| 1044 | } | 1061 | } |
| 1045 | 1062 | ||
| @@ -1056,7 +1073,7 @@ void UString::Grow(unsigned n) | |||
| 1056 | if (next < _len || next > k_Alloc_Len_Limit) | 1073 | if (next < _len || next > k_Alloc_Len_Limit) |
| 1057 | next = k_Alloc_Len_Limit; | 1074 | next = k_Alloc_Len_Limit; |
| 1058 | if (next <= _len || next - _len < n) | 1075 | if (next <= _len || next - _len < n) |
| 1059 | throw 20130220; | 1076 | THROW_STRING_ALLOC_EXCEPTION |
| 1060 | ReAlloc(next - 1); | 1077 | ReAlloc(next - 1); |
| 1061 | } | 1078 | } |
| 1062 | 1079 | ||
| @@ -1214,6 +1231,7 @@ void UString::SetFrom(const wchar_t *s, unsigned len) // no check | |||
| 1214 | { | 1231 | { |
| 1215 | if (len > _limit) | 1232 | if (len > _limit) |
| 1216 | { | 1233 | { |
| 1234 | CHECK_STRING_ALLOC_LEN(len) | ||
| 1217 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1235 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1218 | MY_STRING_DELETE(_chars) | 1236 | MY_STRING_DELETE(_chars) |
| 1219 | _chars = newBuf; | 1237 | _chars = newBuf; |
| @@ -1238,7 +1256,7 @@ void UString::SetFromBstr(LPCOLESTR s) | |||
| 1238 | if (c >= 0xd800 && c < 0xdc00 && i + 1 != len) | 1256 | if (c >= 0xd800 && c < 0xdc00 && i + 1 != len) |
| 1239 | { | 1257 | { |
| 1240 | wchar_t c2 = s[i]; | 1258 | wchar_t c2 = s[i]; |
| 1241 | if (c2 >= 0xdc00 && c2 < 0x10000) | 1259 | if (c2 >= 0xdc00 && c2 < 0xe000) |
| 1242 | { | 1260 | { |
| 1243 | c = 0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff); | 1261 | c = 0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff); |
| 1244 | i++; | 1262 | i++; |
| @@ -1269,7 +1287,7 @@ void UString::SetFromBstr(LPCOLESTR s) | |||
| 1269 | if (c >= 0xd800 && c < 0xdc00 && i + 1 != len) | 1287 | if (c >= 0xd800 && c < 0xdc00 && i + 1 != len) |
| 1270 | { | 1288 | { |
| 1271 | wchar_t c2 = *s; | 1289 | wchar_t c2 = *s; |
| 1272 | if (c2 >= 0xdc00 && c2 < 0x10000) | 1290 | if (c2 >= 0xdc00 && c2 < 0xe000) |
| 1273 | { | 1291 | { |
| 1274 | s++; | 1292 | s++; |
| 1275 | c = 0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff); | 1293 | c = 0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff); |
| @@ -1305,21 +1323,15 @@ UString &UString::operator=(const char *s) | |||
| 1305 | return *this; | 1323 | return *this; |
| 1306 | } | 1324 | } |
| 1307 | 1325 | ||
| 1326 | void UString::Add_Char(char c) { operator+=((wchar_t)(unsigned char)c); } | ||
| 1327 | // void UString::Add_WChar(wchar_t c) { operator+=(c); } | ||
| 1308 | void UString::Add_Dot() { operator+=(L'.'); } | 1328 | void UString::Add_Dot() { operator+=(L'.'); } |
| 1309 | void UString::Add_Space() { operator+=(L' '); } | 1329 | void UString::Add_Space() { operator+=(L' '); } |
| 1330 | void UString::Add_Minus() { operator+=(L'-'); } | ||
| 1331 | void UString::Add_Colon() { operator+=(L':'); } | ||
| 1332 | void UString::Add_LF() { operator+=(L'\n'); } | ||
| 1310 | void UString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } | 1333 | void UString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } |
| 1311 | 1334 | ||
| 1312 | void UString::Add_LF() | ||
| 1313 | { | ||
| 1314 | if (_limit == _len) | ||
| 1315 | Grow_1(); | ||
| 1316 | unsigned len = _len; | ||
| 1317 | wchar_t *chars = _chars; | ||
| 1318 | chars[len++] = L'\n'; | ||
| 1319 | chars[len] = 0; | ||
| 1320 | _len = len; | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | UString &UString::operator+=(const wchar_t *s) | 1335 | UString &UString::operator+=(const wchar_t *s) |
| 1324 | { | 1336 | { |
| 1325 | unsigned len = MyStringLen(s); | 1337 | unsigned len = MyStringLen(s); |
| @@ -1597,7 +1609,7 @@ void UString::DeleteFrontal(unsigned num) throw() | |||
| 1597 | void UString2::ReAlloc2(unsigned newLimit) | 1609 | void UString2::ReAlloc2(unsigned newLimit) |
| 1598 | { | 1610 | { |
| 1599 | // wrong (_len) is allowed after this function | 1611 | // wrong (_len) is allowed after this function |
| 1600 | if (newLimit > k_Alloc_Len_Limit) throw 20130221; | 1612 | CHECK_STRING_ALLOC_LEN(newLimit) |
| 1601 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); | 1613 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); |
| 1602 | if (_chars) | 1614 | if (_chars) |
| 1603 | { | 1615 | { |
| @@ -1805,7 +1817,7 @@ bool CStringFinder::FindWord_In_LowCaseAsciiList_NoCase(const char *p, const wch | |||
| 1805 | break; | 1817 | break; |
| 1806 | if (c <= 0x20 || c > 0x7f) | 1818 | if (c <= 0x20 || c > 0x7f) |
| 1807 | return false; | 1819 | return false; |
| 1808 | _temp += (char)MyCharLower_Ascii((char)c); | 1820 | _temp.Add_Char((char)MyCharLower_Ascii((char)c)); |
| 1809 | } | 1821 | } |
| 1810 | 1822 | ||
| 1811 | while (*p != 0) | 1823 | while (*p != 0) |
