16 #include "string_base.h"
24 # define strncasecmp strnicmp
37 # include <unicode/ustring.h>
42 #if defined(WITH_COCOA)
65 char *
strecpy(
char *dst,
const char *src,
const char *last)
68 while (dst != last && *src !=
'\0') {
73 if (dst == last && *src !=
'\0') {
74 #if defined(STRGEN) || defined(SETTINGSGEN)
75 FatalError(
"String too long for destination buffer");
77 Debug(misc, 0,
"String too long for destination buffer");
91 str.reserve(data.size() * 2 + 1);
94 fmt::format_to(std::back_inserter(str),
"{:02X}", b);
118 while (str <= last && *str !=
'\0') {
144 if (len == 0 || str + len > last + 1 || len !=
Utf8Decode(&c, str)) {
156 }
while (--len != 0);
214 if (str.empty())
return {};
216 auto buf = str.data();
217 auto last = buf + str.size() - 1;
219 std::ostringstream dst;
220 std::ostreambuf_iterator<char> dst_iter(dst);
237 while (str <= last && *str !=
'\0') {
243 if (len == 0 || str + len > last)
return false;
247 if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
265 size_t pos = str.find_first_not_of(
' ');
277 size_t pos = str.find_last_not_of(
' ');
278 if (pos != std::string::npos) str.erase(pos + 1);
302 if (str.size() < prefix.size())
return false;
308 static bool eq(
char c1,
char c2) {
return toupper(c1) == toupper(c2); }
309 static bool ne(
char c1,
char c2) {
return toupper(c1) != toupper(c2); }
310 static bool lt(
char c1,
char c2) {
return toupper(c1) < toupper(c2); }
312 static int compare(
const char *s1,
const char *s2,
size_t n)
315 if (toupper(*s1) < toupper(*s2))
return -1;
316 if (toupper(*s1) > toupper(*s2))
return 1;
322 static const char *find(
const char *s,
size_t n,
char a)
324 for (; n > 0; --n, ++s) {
325 if (toupper(*s) == toupper(a))
return s;
342 if (str.size() < suffix.size())
return false;
357 return ci_str1.compare(ci_str2);
368 if (str1.size() != str2.size())
return false;
382 while (Utf8Consume(&t) != 0) len++;
397 bool strtolower(std::string &str, std::string::size_type offs)
399 bool changed =
false;
400 for (
auto ch = str.begin() + offs; ch != str.end(); ++ch) {
401 auto new_ch =
static_cast<char>(tolower(
static_cast<unsigned char>(*ch)));
402 changed |= new_ch != *ch;
419 case CS_NUMERAL:
return (key >=
'0' && key <=
'9');
422 case CS_ALPHA:
return IsPrintable(key) && !(key >=
'0' && key <=
'9');
423 case CS_HEXADECIMAL:
return (key >=
'0' && key <=
'9') || (key >=
'a' && key <=
'f') || (key >=
'A' && key <=
'F');
424 default: NOT_REACHED();
440 assert(c !=
nullptr);
446 }
else if (
GB(s[0], 5, 3) == 6) {
447 if (IsUtf8Part(s[1])) {
449 *c =
GB(s[0], 0, 5) << 6 |
GB(s[1], 0, 6);
450 if (*c >= 0x80)
return 2;
452 }
else if (
GB(s[0], 4, 4) == 14) {
453 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
455 *c =
GB(s[0], 0, 4) << 12 |
GB(s[1], 0, 6) << 6 |
GB(s[2], 0, 6);
456 if (*c >= 0x800)
return 3;
458 }
else if (
GB(s[0], 3, 5) == 30) {
459 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
461 *c =
GB(s[0], 0, 3) << 18 |
GB(s[1], 0, 6) << 12 |
GB(s[2], 0, 6) << 6 |
GB(s[3], 0, 6);
462 if (*c >= 0x10000 && *c <= 0x10FFFF)
return 4;
484 }
else if (c < 0x800) {
485 *buf++ = 0xC0 +
GB(c, 6, 5);
486 *buf = 0x80 +
GB(c, 0, 6);
488 }
else if (c < 0x10000) {
489 *buf++ = 0xE0 +
GB(c, 12, 4);
490 *buf++ = 0x80 +
GB(c, 6, 6);
491 *buf = 0x80 +
GB(c, 0, 6);
493 }
else if (c < 0x110000) {
494 *buf++ = 0xF0 +
GB(c, 18, 3);
495 *buf++ = 0x80 +
GB(c, 12, 6);
496 *buf++ = 0x80 +
GB(c, 6, 6);
497 *buf = 0x80 +
GB(c, 0, 6);
507 return Utf8Encode<char *>(buf, c);
510 size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, char32_t c)
512 return Utf8Encode<std::ostreambuf_iterator<char> &>(buf, c);
515 size_t Utf8Encode(std::back_insert_iterator<std::string> &buf, char32_t c)
517 return Utf8Encode<std::back_insert_iterator<std::string> &>(buf, c);
531 for (
const char *ptr = strchr(s,
'\0'); *s !=
'\0';) {
534 if (len == 0) len = 1;
538 if (length + len >= maxlen || (s + len > ptr))
break;
547 #ifdef DEFINE_STRCASESTR
548 char *strcasestr(
const char *haystack,
const char *needle)
550 size_t hay_len = strlen(haystack);
551 size_t needle_len = strlen(needle);
552 while (hay_len >= needle_len) {
553 if (strncasecmp(haystack, needle, needle_len) == 0)
return const_cast<char *
>(haystack);
573 while (!str.empty() && (str[0] <
'0' ||
IsInsideMM(str[0],
';',
'@' + 1) ||
IsInsideMM(str[0],
'[',
'`' + 1) ||
IsInsideMM(str[0],
'{',
'~' + 1))) str.remove_prefix(1);
587 if (ignore_garbage_at_front) {
594 UErrorCode status = U_ZERO_ERROR;
595 int result =
_current_collator->compareUTF8(icu::StringPiece(s1.data(), s1.size()), icu::StringPiece(s2.data(), s2.size()), status);
596 if (U_SUCCESS(status))
return result;
600 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
601 int res = OTTDStringCompare(s1, s2);
602 if (res != 0)
return res - 2;
605 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
607 if (res != 0)
return res - 2;
616 #include <unicode/stsearch.h>
626 static int ICUStringContains(
const std::string_view str,
const std::string_view value,
bool case_insensitive)
629 std::unique_ptr<icu::RuleBasedCollator> coll(
dynamic_cast<icu::RuleBasedCollator *
>(
_current_collator->clone()));
631 UErrorCode status = U_ZERO_ERROR;
632 coll->setStrength(case_insensitive ? icu::Collator::SECONDARY : icu::Collator::TERTIARY);
633 coll->setAttribute(UCOL_NUMERIC_COLLATION, UCOL_OFF, status);
635 auto u_str = icu::UnicodeString::fromUTF8(icu::StringPiece(str.data(), str.size()));
636 auto u_value = icu::UnicodeString::fromUTF8(icu::StringPiece(value.data(), value.size()));
637 icu::StringSearch u_searcher(u_value, u_str, coll.get(),
nullptr, status);
638 if (U_SUCCESS(status)) {
639 auto pos = u_searcher.first(status);
640 if (U_SUCCESS(status))
return pos != USEARCH_DONE ? 1 : 0;
660 if (res_u >= 0)
return res_u > 0;
663 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
665 if (res >= 0)
return res > 0;
668 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
670 if (res >= 0)
return res > 0;
673 return str.find(value) != std::string_view::npos;
687 if (res_u >= 0)
return res_u > 0;
690 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
692 if (res >= 0)
return res > 0;
695 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
697 if (res >= 0)
return res > 0;
702 return ci_str.find(ci_value) != CaseInsensitiveStringView::npos;
713 if (c >=
'0' && c <=
'9')
return c -
'0';
714 if (c >=
'A' && c <=
'F')
return c + 10 -
'A';
715 if (c >=
'a' && c <=
'f')
return c + 10 -
'a';
732 if (bytes.size() != hex.size() / 2) {
737 if (hex.size() % 2 != 0) {
741 for (
size_t i = 0; i < hex.size() / 2; i++) {
745 if (hi < 0 || lo < 0) {
749 bytes[i] = (hi << 4) | lo;
755 #ifdef WITH_UNISCRIBE
759 return std::make_unique<UniscribeStringIterator>();
762 #elif defined(WITH_ICU_I18N)
764 #include <unicode/utext.h>
765 #include <unicode/brkiter.h>
779 UErrorCode status = U_ZERO_ERROR;
783 this->utf16_str.push_back(
'\0');
784 this->utf16_to_utf8.push_back(0);
795 const char *string_base = s;
801 this->utf16_str.clear();
802 this->utf16_to_utf8.clear();
805 size_t idx = s - string_base;
807 char32_t c = Utf8Consume(&s);
809 this->utf16_str.push_back((UChar)c);
812 this->utf16_str.push_back((UChar)(0xD800 + ((c - 0x10000) >> 10)));
813 this->utf16_str.push_back((UChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
814 this->utf16_to_utf8.push_back(idx);
816 this->utf16_to_utf8.push_back(idx);
818 this->utf16_str.push_back(
'\0');
819 this->utf16_to_utf8.push_back(s - string_base);
821 UText text = UTEXT_INITIALIZER;
822 UErrorCode status = U_ZERO_ERROR;
823 utext_openUChars(&text, this->utf16_str.data(), this->utf16_str.size() - 1, &status);
824 this->char_itr->setText(&text, status);
825 this->word_itr->setText(&text, status);
826 this->char_itr->first();
827 this->word_itr->first();
834 for (uint i = 0; i < this->utf16_to_utf8.size(); i++) {
835 if (this->utf16_to_utf8[i] == pos) {
844 this->char_itr->isBoundary(utf16_pos);
845 return this->utf16_to_utf8[this->char_itr->current()];
853 pos = this->char_itr->next();
857 pos = this->word_itr->following(this->char_itr->current());
861 while (pos != icu::BreakIterator::DONE &&
863 int32_t new_pos = this->word_itr->next();
866 if (new_pos == icu::BreakIterator::DONE)
break;
870 this->char_itr->isBoundary(pos);
877 return pos == icu::BreakIterator::DONE ?
END : this->utf16_to_utf8[pos];
885 pos = this->char_itr->previous();
889 pos = this->word_itr->preceding(this->char_itr->current());
893 while (pos != icu::BreakIterator::DONE &&
895 int32_t new_pos = this->word_itr->previous();
898 if (new_pos == icu::BreakIterator::DONE)
break;
902 this->char_itr->isBoundary(pos);
909 return pos == icu::BreakIterator::DONE ?
END : this->utf16_to_utf8[pos];
915 return std::make_unique<IcuStringIterator>();
928 DefaultStringIterator() : string(nullptr), len(0), cur_pos(0)
935 this->len = strlen(s);
941 assert(this->
string !=
nullptr && pos <= this->len);
943 while (pos > 0 && IsUtf8Part(this->
string[pos])) pos--;
944 return this->cur_pos = pos;
947 size_t Next(IterType what)
override
949 assert(this->
string !=
nullptr);
952 if (this->cur_pos >= this->len)
return END;
957 this->cur_pos +=
Utf8Decode(&c, this->
string + this->cur_pos);
958 return this->cur_pos;
964 size_t offs =
Utf8Decode(&c, this->
string + this->cur_pos);
966 this->cur_pos += offs;
967 offs =
Utf8Decode(&c, this->
string + this->cur_pos);
971 this->cur_pos += offs;
972 offs =
Utf8Decode(&c, this->
string + this->cur_pos);
975 return this->cur_pos;
985 size_t Prev(IterType what)
override
987 assert(this->
string !=
nullptr);
990 if (this->cur_pos == 0)
return END;
994 return this->cur_pos =
Utf8PrevChar(this->
string + this->cur_pos) - this->string;
997 const char *s = this->
string + this->cur_pos;
1012 return this->cur_pos = s - this->string;
1023 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
1026 std::unique_ptr<StringIterator> i = OSXStringIterator::Create();
1027 if (i !=
nullptr)
return i;
1029 return std::make_unique<DefaultStringIterator>();
1034 return std::make_unique<DefaultStringIterator>();