diff options
author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-06-21 00:00:00 +0000 |
---|---|---|
committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-12-17 14:59:19 +0500 |
commit | 5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch) | |
tree | fe5e17420300b715021a76328444088d32047963 /CPP/Common/MyString.cpp | |
parent | 93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff) | |
download | 7zip-23.01.tar.gz 7zip-23.01.tar.bz2 7zip-23.01.zip |
23.0123.01
Diffstat (limited to 'CPP/Common/MyString.cpp')
-rw-r--r-- | CPP/Common/MyString.cpp | 140 |
1 files changed, 102 insertions, 38 deletions
diff --git a/CPP/Common/MyString.cpp b/CPP/Common/MyString.cpp index bf1638e..51c1c3b 100644 --- a/CPP/Common/MyString.cpp +++ b/CPP/Common/MyString.cpp | |||
@@ -408,7 +408,7 @@ void AString::ReAlloc(unsigned newLimit) | |||
408 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, (size_t)_len + 1); | 408 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, (size_t)_len + 1); |
409 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); | 409 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); |
410 | memcpy(newBuf, _chars, (size_t)_len + 1); | 410 | memcpy(newBuf, _chars, (size_t)_len + 1); |
411 | MY_STRING_DELETE(_chars); | 411 | MY_STRING_DELETE(_chars) |
412 | _chars = newBuf; | 412 | _chars = newBuf; |
413 | _limit = newLimit; | 413 | _limit = newLimit; |
414 | } | 414 | } |
@@ -419,9 +419,10 @@ void AString::ReAlloc2(unsigned newLimit) | |||
419 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, 0); | 419 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, 0); |
420 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); | 420 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); |
421 | newBuf[0] = 0; | 421 | newBuf[0] = 0; |
422 | MY_STRING_DELETE(_chars); | 422 | MY_STRING_DELETE(_chars) |
423 | _chars = newBuf; | 423 | _chars = newBuf; |
424 | _limit = newLimit; | 424 | _limit = newLimit; |
425 | _len = 0; | ||
425 | } | 426 | } |
426 | 427 | ||
427 | void AString::SetStartLen(unsigned len) | 428 | void AString::SetStartLen(unsigned len) |
@@ -541,7 +542,7 @@ AString &AString::operator=(char c) | |||
541 | if (1 > _limit) | 542 | if (1 > _limit) |
542 | { | 543 | { |
543 | char *newBuf = MY_STRING_NEW_char(1 + 1); | 544 | char *newBuf = MY_STRING_NEW_char(1 + 1); |
544 | MY_STRING_DELETE(_chars); | 545 | MY_STRING_DELETE(_chars) |
545 | _chars = newBuf; | 546 | _chars = newBuf; |
546 | _limit = 1; | 547 | _limit = 1; |
547 | } | 548 | } |
@@ -558,7 +559,7 @@ AString &AString::operator=(const char *s) | |||
558 | if (len > _limit) | 559 | if (len > _limit) |
559 | { | 560 | { |
560 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); | 561 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
561 | MY_STRING_DELETE(_chars); | 562 | MY_STRING_DELETE(_chars) |
562 | _chars = newBuf; | 563 | _chars = newBuf; |
563 | _limit = len; | 564 | _limit = len; |
564 | } | 565 | } |
@@ -575,7 +576,7 @@ AString &AString::operator=(const AString &s) | |||
575 | if (len > _limit) | 576 | if (len > _limit) |
576 | { | 577 | { |
577 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); | 578 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
578 | MY_STRING_DELETE(_chars); | 579 | MY_STRING_DELETE(_chars) |
579 | _chars = newBuf; | 580 | _chars = newBuf; |
580 | _limit = len; | 581 | _limit = len; |
581 | } | 582 | } |
@@ -600,7 +601,7 @@ void AString::SetFromWStr_if_Ascii(const wchar_t *s) | |||
600 | if (len > _limit) | 601 | if (len > _limit) |
601 | { | 602 | { |
602 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); | 603 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
603 | MY_STRING_DELETE(_chars); | 604 | MY_STRING_DELETE(_chars) |
604 | _chars = newBuf; | 605 | _chars = newBuf; |
605 | _limit = len; | 606 | _limit = len; |
606 | } | 607 | } |
@@ -624,7 +625,7 @@ void AString::SetFromBstr_if_Ascii(BSTR s) | |||
624 | if (len > _limit) | 625 | if (len > _limit) |
625 | { | 626 | { |
626 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); | 627 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
627 | MY_STRING_DELETE(_chars); | 628 | MY_STRING_DELETE(_chars) |
628 | _chars = newBuf; | 629 | _chars = newBuf; |
629 | _limit = len; | 630 | _limit = len; |
630 | } | 631 | } |
@@ -641,6 +642,8 @@ void AString::Add_Space() { operator+=(' '); } | |||
641 | void AString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } | 642 | void AString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } |
642 | void AString::Add_LF() { operator+=('\n'); } | 643 | void AString::Add_LF() { operator+=('\n'); } |
643 | void AString::Add_Slash() { operator+=('/'); } | 644 | void AString::Add_Slash() { operator+=('/'); } |
645 | void AString::Add_Dot() { operator+=('.'); } | ||
646 | void AString::Add_Minus() { operator+=('-'); } | ||
644 | 647 | ||
645 | AString &AString::operator+=(const char *s) | 648 | AString &AString::operator+=(const char *s) |
646 | { | 649 | { |
@@ -694,7 +697,7 @@ void AString::SetFrom(const char *s, unsigned len) // no check | |||
694 | if (len > _limit) | 697 | if (len > _limit) |
695 | { | 698 | { |
696 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); | 699 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
697 | MY_STRING_DELETE(_chars); | 700 | MY_STRING_DELETE(_chars) |
698 | _chars = newBuf; | 701 | _chars = newBuf; |
699 | _limit = len; | 702 | _limit = len; |
700 | } | 703 | } |
@@ -766,7 +769,7 @@ int AString::ReverseFind_PathSepar() const throw() | |||
766 | const char *p = _chars + _len - 1; | 769 | const char *p = _chars + _len - 1; |
767 | for (;;) | 770 | for (;;) |
768 | { | 771 | { |
769 | char c = *p; | 772 | const char c = *p; |
770 | if (IS_PATH_SEPAR(c)) | 773 | if (IS_PATH_SEPAR(c)) |
771 | return (int)(p - _chars); | 774 | return (int)(p - _chars); |
772 | if (p == _chars) | 775 | if (p == _chars) |
@@ -1001,7 +1004,7 @@ void UString::ReAlloc(unsigned newLimit) | |||
1001 | // MY_STRING_REALLOC(_chars, wchar_t, (size_t)newLimit + 1, (size_t)_len + 1); | 1004 | // MY_STRING_REALLOC(_chars, wchar_t, (size_t)newLimit + 1, (size_t)_len + 1); |
1002 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); | 1005 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); |
1003 | wmemcpy(newBuf, _chars, _len + 1); | 1006 | wmemcpy(newBuf, _chars, _len + 1); |
1004 | MY_STRING_DELETE(_chars); | 1007 | MY_STRING_DELETE(_chars) |
1005 | _chars = newBuf; | 1008 | _chars = newBuf; |
1006 | _limit = newLimit; | 1009 | _limit = newLimit; |
1007 | } | 1010 | } |
@@ -1012,14 +1015,15 @@ void UString::ReAlloc2(unsigned newLimit) | |||
1012 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); | 1015 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); |
1013 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); | 1016 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); |
1014 | newBuf[0] = 0; | 1017 | newBuf[0] = 0; |
1015 | MY_STRING_DELETE(_chars); | 1018 | MY_STRING_DELETE(_chars) |
1016 | _chars = newBuf; | 1019 | _chars = newBuf; |
1017 | _limit = newLimit; | 1020 | _limit = newLimit; |
1021 | _len = 0; | ||
1018 | } | 1022 | } |
1019 | 1023 | ||
1020 | void UString::SetStartLen(unsigned len) | 1024 | void UString::SetStartLen(unsigned len) |
1021 | { | 1025 | { |
1022 | _chars = 0; | 1026 | _chars = NULL; |
1023 | _chars = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1027 | _chars = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1024 | _len = len; | 1028 | _len = len; |
1025 | _limit = len; | 1029 | _limit = len; |
@@ -1101,7 +1105,7 @@ UString operator+(const wchar_t *s1, const UString &s2) { return UString(s1, MyS | |||
1101 | 1105 | ||
1102 | UString::UString() | 1106 | UString::UString() |
1103 | { | 1107 | { |
1104 | _chars = 0; | 1108 | _chars = NULL; |
1105 | _chars = MY_STRING_NEW_wchar_t(kStartStringCapacity); | 1109 | _chars = MY_STRING_NEW_wchar_t(kStartStringCapacity); |
1106 | _len = 0; | 1110 | _len = 0; |
1107 | _limit = kStartStringCapacity - 1; | 1111 | _limit = kStartStringCapacity - 1; |
@@ -1163,7 +1167,7 @@ UString &UString::operator=(wchar_t c) | |||
1163 | if (1 > _limit) | 1167 | if (1 > _limit) |
1164 | { | 1168 | { |
1165 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(1 + 1); | 1169 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(1 + 1); |
1166 | MY_STRING_DELETE(_chars); | 1170 | MY_STRING_DELETE(_chars) |
1167 | _chars = newBuf; | 1171 | _chars = newBuf; |
1168 | _limit = 1; | 1172 | _limit = 1; |
1169 | } | 1173 | } |
@@ -1180,7 +1184,7 @@ UString &UString::operator=(const wchar_t *s) | |||
1180 | if (len > _limit) | 1184 | if (len > _limit) |
1181 | { | 1185 | { |
1182 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1186 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1183 | MY_STRING_DELETE(_chars); | 1187 | MY_STRING_DELETE(_chars) |
1184 | _chars = newBuf; | 1188 | _chars = newBuf; |
1185 | _limit = len; | 1189 | _limit = len; |
1186 | } | 1190 | } |
@@ -1197,7 +1201,7 @@ UString &UString::operator=(const UString &s) | |||
1197 | if (len > _limit) | 1201 | if (len > _limit) |
1198 | { | 1202 | { |
1199 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1203 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1200 | MY_STRING_DELETE(_chars); | 1204 | MY_STRING_DELETE(_chars) |
1201 | _chars = newBuf; | 1205 | _chars = newBuf; |
1202 | _limit = len; | 1206 | _limit = len; |
1203 | } | 1207 | } |
@@ -1211,7 +1215,7 @@ void UString::SetFrom(const wchar_t *s, unsigned len) // no check | |||
1211 | if (len > _limit) | 1215 | if (len > _limit) |
1212 | { | 1216 | { |
1213 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1217 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1214 | MY_STRING_DELETE(_chars); | 1218 | MY_STRING_DELETE(_chars) |
1215 | _chars = newBuf; | 1219 | _chars = newBuf; |
1216 | _limit = len; | 1220 | _limit = len; |
1217 | } | 1221 | } |
@@ -1249,7 +1253,7 @@ void UString::SetFromBstr(LPCOLESTR s) | |||
1249 | if (len > _limit) | 1253 | if (len > _limit) |
1250 | { | 1254 | { |
1251 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1255 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1252 | MY_STRING_DELETE(_chars); | 1256 | MY_STRING_DELETE(_chars) |
1253 | _chars = newBuf; | 1257 | _chars = newBuf; |
1254 | _limit = len; | 1258 | _limit = len; |
1255 | } | 1259 | } |
@@ -1289,7 +1293,7 @@ UString &UString::operator=(const char *s) | |||
1289 | if (len > _limit) | 1293 | if (len > _limit) |
1290 | { | 1294 | { |
1291 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1295 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1292 | MY_STRING_DELETE(_chars); | 1296 | MY_STRING_DELETE(_chars) |
1293 | _chars = newBuf; | 1297 | _chars = newBuf; |
1294 | _limit = len; | 1298 | _limit = len; |
1295 | } | 1299 | } |
@@ -1301,6 +1305,7 @@ UString &UString::operator=(const char *s) | |||
1301 | return *this; | 1305 | return *this; |
1302 | } | 1306 | } |
1303 | 1307 | ||
1308 | void UString::Add_Dot() { operator+=(L'.'); } | ||
1304 | void UString::Add_Space() { operator+=(L' '); } | 1309 | void UString::Add_Space() { operator+=(L' '); } |
1305 | void UString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } | 1310 | void UString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } |
1306 | 1311 | ||
@@ -1393,31 +1398,26 @@ int UString::ReverseFind(wchar_t c) const throw() | |||
1393 | { | 1398 | { |
1394 | if (_len == 0) | 1399 | if (_len == 0) |
1395 | return -1; | 1400 | return -1; |
1396 | const wchar_t *p = _chars + _len - 1; | 1401 | const wchar_t *p = _chars + _len; |
1397 | for (;;) | 1402 | do |
1398 | { | 1403 | { |
1399 | if (*p == c) | 1404 | if (*(--p) == c) |
1400 | return (int)(p - _chars); | 1405 | return (int)(p - _chars); |
1401 | if (p == _chars) | ||
1402 | return -1; | ||
1403 | p--; | ||
1404 | } | 1406 | } |
1407 | while (p != _chars); | ||
1408 | return -1; | ||
1405 | } | 1409 | } |
1406 | 1410 | ||
1407 | int UString::ReverseFind_PathSepar() const throw() | 1411 | int UString::ReverseFind_PathSepar() const throw() |
1408 | { | 1412 | { |
1409 | if (_len == 0) | 1413 | const wchar_t *p = _chars + _len; |
1410 | return -1; | 1414 | while (p != _chars) |
1411 | const wchar_t *p = _chars + _len - 1; | ||
1412 | for (;;) | ||
1413 | { | 1415 | { |
1414 | wchar_t c = *p; | 1416 | const wchar_t c = *(--p); |
1415 | if (IS_PATH_SEPAR(c)) | 1417 | if (IS_PATH_SEPAR(c)) |
1416 | return (int)(p - _chars); | 1418 | return (int)(p - _chars); |
1417 | if (p == _chars) | ||
1418 | return -1; | ||
1419 | p--; | ||
1420 | } | 1419 | } |
1420 | return -1; | ||
1421 | } | 1421 | } |
1422 | 1422 | ||
1423 | void UString::TrimLeft() throw() | 1423 | void UString::TrimLeft() throw() |
@@ -1601,7 +1601,7 @@ void UString2::ReAlloc2(unsigned newLimit) | |||
1601 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); | 1601 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); |
1602 | if (_chars) | 1602 | if (_chars) |
1603 | { | 1603 | { |
1604 | MY_STRING_DELETE(_chars); | 1604 | MY_STRING_DELETE(_chars) |
1605 | _chars = NULL; | 1605 | _chars = NULL; |
1606 | // _len = 0; | 1606 | // _len = 0; |
1607 | } | 1607 | } |
@@ -1651,7 +1651,7 @@ UString2 &UString2::operator=(wchar_t c) | |||
1651 | { | 1651 | { |
1652 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(1 + 1); | 1652 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(1 + 1); |
1653 | if (_chars) | 1653 | if (_chars) |
1654 | MY_STRING_DELETE(_chars); | 1654 | MY_STRING_DELETE(_chars) |
1655 | _chars = newBuf; | 1655 | _chars = newBuf; |
1656 | } | 1656 | } |
1657 | _len = 1; | 1657 | _len = 1; |
@@ -1669,7 +1669,7 @@ UString2 &UString2::operator=(const wchar_t *s) | |||
1669 | { | 1669 | { |
1670 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1670 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1671 | if (_chars) | 1671 | if (_chars) |
1672 | MY_STRING_DELETE(_chars); | 1672 | MY_STRING_DELETE(_chars) |
1673 | _chars = newBuf; | 1673 | _chars = newBuf; |
1674 | } | 1674 | } |
1675 | _len = len; | 1675 | _len = len; |
@@ -1684,7 +1684,7 @@ void UString2::SetFromAscii(const char *s) | |||
1684 | { | 1684 | { |
1685 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1685 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1686 | if (_chars) | 1686 | if (_chars) |
1687 | MY_STRING_DELETE(_chars); | 1687 | MY_STRING_DELETE(_chars) |
1688 | _chars = newBuf; | 1688 | _chars = newBuf; |
1689 | } | 1689 | } |
1690 | wchar_t *chars = _chars; | 1690 | wchar_t *chars = _chars; |
@@ -1703,7 +1703,7 @@ UString2 &UString2::operator=(const UString2 &s) | |||
1703 | { | 1703 | { |
1704 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); | 1704 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
1705 | if (_chars) | 1705 | if (_chars) |
1706 | MY_STRING_DELETE(_chars); | 1706 | MY_STRING_DELETE(_chars) |
1707 | _chars = newBuf; | 1707 | _chars = newBuf; |
1708 | } | 1708 | } |
1709 | _len = len; | 1709 | _len = len; |
@@ -1793,3 +1793,67 @@ FString us2fs(const wchar_t *s) | |||
1793 | } | 1793 | } |
1794 | 1794 | ||
1795 | #endif // USE_UNICODE_FSTRING | 1795 | #endif // USE_UNICODE_FSTRING |
1796 | |||
1797 | |||
1798 | bool CStringFinder::FindWord_In_LowCaseAsciiList_NoCase(const char *p, const wchar_t *str) | ||
1799 | { | ||
1800 | _temp.Empty(); | ||
1801 | for (;;) | ||
1802 | { | ||
1803 | const wchar_t c = *str++; | ||
1804 | if (c == 0) | ||
1805 | break; | ||
1806 | if (c <= 0x20 || c > 0x7f) | ||
1807 | return false; | ||
1808 | _temp += (char)MyCharLower_Ascii((char)c); | ||
1809 | } | ||
1810 | |||
1811 | while (*p != 0) | ||
1812 | { | ||
1813 | const char *s2 = _temp.Ptr(); | ||
1814 | char c, c2; | ||
1815 | do | ||
1816 | { | ||
1817 | c = *p++; | ||
1818 | c2 = *s2++; | ||
1819 | } | ||
1820 | while (c == c2); | ||
1821 | |||
1822 | if (c == ' ') | ||
1823 | { | ||
1824 | if (c2 == 0) | ||
1825 | return true; | ||
1826 | continue; | ||
1827 | } | ||
1828 | |||
1829 | while (*p++ != ' '); | ||
1830 | } | ||
1831 | |||
1832 | return false; | ||
1833 | } | ||
1834 | |||
1835 | |||
1836 | void SplitString(const UString &srcString, UStringVector &destStrings) | ||
1837 | { | ||
1838 | destStrings.Clear(); | ||
1839 | unsigned len = srcString.Len(); | ||
1840 | if (len == 0) | ||
1841 | return; | ||
1842 | UString s; | ||
1843 | for (unsigned i = 0; i < len; i++) | ||
1844 | { | ||
1845 | const wchar_t c = srcString[i]; | ||
1846 | if (c == ' ') | ||
1847 | { | ||
1848 | if (!s.IsEmpty()) | ||
1849 | { | ||
1850 | destStrings.Add(s); | ||
1851 | s.Empty(); | ||
1852 | } | ||
1853 | } | ||
1854 | else | ||
1855 | s += c; | ||
1856 | } | ||
1857 | if (!s.IsEmpty()) | ||
1858 | destStrings.Add(s); | ||
1859 | } | ||