16 #include "string_base.h"
24 # define strncasecmp strnicmp
37 # include <unicode/ustring.h>
42 #if defined(WITH_COCOA)
60 void strecpy(std::span<char> dst, std::string_view src)
63 if (std::empty(dst) || std::size(src) >= std::size(dst) - 1U) {
64 #if defined(STRGEN) || defined(SETTINGSGEN)
65 FatalError(
"String too long for destination buffer");
67 Debug(misc, 0,
"String too long for destination buffer");
68 src = src.substr(0, std::size(dst) - 1U);
72 auto it = std::copy(std::begin(src), std::end(src), std::begin(dst));
84 str.reserve(data.size() * 2 + 1);
87 fmt::format_to(std::back_inserter(str),
"{:02X}", b);
111 while (str <= last && *str !=
'\0') {
137 if (len == 0 || str + len > last + 1 || len !=
Utf8Decode(&c, str)) {
149 }
while (--len != 0);
207 if (str.empty())
return {};
209 auto buf = str.data();
210 auto last = buf + str.size() - 1;
212 std::ostringstream dst;
213 std::ostreambuf_iterator<char> dst_iter(dst);
230 auto it = std::begin(str);
231 auto last = std::prev(std::end(str));
233 while (it <= last && *it !=
'\0') {
239 if (len == 0 || it + len > last)
return false;
243 if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
262 str = StrTrimView(str);
265 std::string_view StrTrimView(std::string_view str)
267 size_t first_pos = str.find_first_not_of(
' ');
268 if (first_pos == std::string::npos) {
269 return std::string_view{};
271 size_t last_pos = str.find_last_not_of(
' ');
272 return str.substr(first_pos, last_pos - first_pos + 1);
283 if (str.size() < prefix.size())
return false;
289 static bool eq(
char c1,
char c2) {
return toupper(c1) == toupper(c2); }
290 static bool ne(
char c1,
char c2) {
return toupper(c1) != toupper(c2); }
291 static bool lt(
char c1,
char c2) {
return toupper(c1) < toupper(c2); }
293 static int compare(
const char *s1,
const char *s2,
size_t n)
296 if (toupper(*s1) < toupper(*s2))
return -1;
297 if (toupper(*s1) > toupper(*s2))
return 1;
303 static const char *find(
const char *s,
size_t n,
char a)
305 for (; n > 0; --n, ++s) {
306 if (toupper(*s) == toupper(a))
return s;
323 if (str.size() < suffix.size())
return false;
338 return ci_str1.compare(ci_str2);
349 if (str1.size() != str2.size())
return false;
363 while (Utf8Consume(&t) != 0) len++;
378 bool strtolower(std::string &str, std::string::size_type offs)
380 bool changed =
false;
381 for (
auto ch = str.begin() + offs; ch != str.end(); ++ch) {
382 auto new_ch =
static_cast<char>(tolower(
static_cast<unsigned char>(*ch)));
383 changed |= new_ch != *ch;
400 case CS_NUMERAL:
return (key >=
'0' && key <=
'9');
403 case CS_ALPHA:
return IsPrintable(key) && !(key >=
'0' && key <=
'9');
404 case CS_HEXADECIMAL:
return (key >=
'0' && key <=
'9') || (key >=
'a' && key <=
'f') || (key >=
'A' && key <=
'F');
405 default: NOT_REACHED();
421 assert(c !=
nullptr);
427 }
else if (
GB(s[0], 5, 3) == 6) {
428 if (IsUtf8Part(s[1])) {
430 *c =
GB(s[0], 0, 5) << 6 |
GB(s[1], 0, 6);
431 if (*c >= 0x80)
return 2;
433 }
else if (
GB(s[0], 4, 4) == 14) {
434 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
436 *c =
GB(s[0], 0, 4) << 12 |
GB(s[1], 0, 6) << 6 |
GB(s[2], 0, 6);
437 if (*c >= 0x800)
return 3;
439 }
else if (
GB(s[0], 3, 5) == 30) {
440 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
442 *c =
GB(s[0], 0, 3) << 18 |
GB(s[1], 0, 6) << 12 |
GB(s[2], 0, 6) << 6 |
GB(s[3], 0, 6);
443 if (*c >= 0x10000 && *c <= 0x10FFFF)
return 4;
465 }
else if (c < 0x800) {
466 *buf++ = 0xC0 +
GB(c, 6, 5);
467 *buf = 0x80 +
GB(c, 0, 6);
469 }
else if (c < 0x10000) {
470 *buf++ = 0xE0 +
GB(c, 12, 4);
471 *buf++ = 0x80 +
GB(c, 6, 6);
472 *buf = 0x80 +
GB(c, 0, 6);
474 }
else if (c < 0x110000) {
475 *buf++ = 0xF0 +
GB(c, 18, 3);
476 *buf++ = 0x80 +
GB(c, 12, 6);
477 *buf++ = 0x80 +
GB(c, 6, 6);
478 *buf = 0x80 +
GB(c, 0, 6);
488 return Utf8Encode<char *>(buf, c);
491 size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, char32_t c)
493 return Utf8Encode<std::ostreambuf_iterator<char> &>(buf, c);
496 size_t Utf8Encode(std::back_insert_iterator<std::string> &buf, char32_t c)
498 return Utf8Encode<std::back_insert_iterator<std::string> &>(buf, c);
512 for (
const char *ptr = strchr(s,
'\0'); *s !=
'\0';) {
515 if (len == 0) len = 1;
519 if (length + len >= maxlen || (s + len > ptr))
break;
528 #ifdef DEFINE_STRCASESTR
529 char *strcasestr(
const char *haystack,
const char *needle)
531 size_t hay_len = strlen(haystack);
532 size_t needle_len = strlen(needle);
533 while (hay_len >= needle_len) {
534 if (strncasecmp(haystack, needle, needle_len) == 0)
return const_cast<char *
>(haystack);
554 while (!str.empty() && (str[0] <
'0' ||
IsInsideMM(str[0],
';',
'@' + 1) ||
IsInsideMM(str[0],
'[',
'`' + 1) ||
IsInsideMM(str[0],
'{',
'~' + 1))) str.remove_prefix(1);
568 if (ignore_garbage_at_front) {
575 UErrorCode status = U_ZERO_ERROR;
576 int result =
_current_collator->compareUTF8(icu::StringPiece(s1.data(), s1.size()), icu::StringPiece(s2.data(), s2.size()), status);
577 if (U_SUCCESS(status))
return result;
581 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
582 int res = OTTDStringCompare(s1, s2);
583 if (res != 0)
return res - 2;
586 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
588 if (res != 0)
return res - 2;
597 #include <unicode/stsearch.h>
607 static int ICUStringContains(
const std::string_view str,
const std::string_view value,
bool case_insensitive)
610 std::unique_ptr<icu::RuleBasedCollator> coll(
dynamic_cast<icu::RuleBasedCollator *
>(
_current_collator->clone()));
612 UErrorCode status = U_ZERO_ERROR;
613 coll->setStrength(case_insensitive ? icu::Collator::SECONDARY : icu::Collator::TERTIARY);
614 coll->setAttribute(UCOL_NUMERIC_COLLATION, UCOL_OFF, status);
616 auto u_str = icu::UnicodeString::fromUTF8(icu::StringPiece(str.data(), str.size()));
617 auto u_value = icu::UnicodeString::fromUTF8(icu::StringPiece(value.data(), value.size()));
618 icu::StringSearch u_searcher(u_value, u_str, coll.get(),
nullptr, status);
619 if (U_SUCCESS(status)) {
620 auto pos = u_searcher.first(status);
621 if (U_SUCCESS(status))
return pos != USEARCH_DONE ? 1 : 0;
641 if (res_u >= 0)
return res_u > 0;
644 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
646 if (res >= 0)
return res > 0;
649 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
651 if (res >= 0)
return res > 0;
654 return str.find(value) != std::string_view::npos;
668 if (res_u >= 0)
return res_u > 0;
671 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
673 if (res >= 0)
return res > 0;
676 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
678 if (res >= 0)
return res > 0;
683 return ci_str.find(ci_value) != CaseInsensitiveStringView::npos;
694 if (c >=
'0' && c <=
'9')
return c -
'0';
695 if (c >=
'A' && c <=
'F')
return c + 10 -
'A';
696 if (c >=
'a' && c <=
'f')
return c + 10 -
'a';
713 if (bytes.size() != hex.size() / 2) {
718 if (hex.size() % 2 != 0) {
722 for (
size_t i = 0; i < hex.size() / 2; i++) {
726 if (hi < 0 || lo < 0) {
730 bytes[i] = (hi << 4) | lo;
736 #ifdef WITH_UNISCRIBE
740 return std::make_unique<UniscribeStringIterator>();
743 #elif defined(WITH_ICU_I18N)
745 #include <unicode/utext.h>
746 #include <unicode/brkiter.h>
760 UErrorCode status = U_ZERO_ERROR;
764 this->utf16_str.push_back(
'\0');
765 this->utf16_to_utf8.push_back(0);
776 const char *string_base = s;
782 this->utf16_str.clear();
783 this->utf16_to_utf8.clear();
786 size_t idx = s - string_base;
788 char32_t c = Utf8Consume(&s);
790 this->utf16_str.push_back((UChar)c);
793 this->utf16_str.push_back((UChar)(0xD800 + ((c - 0x10000) >> 10)));
794 this->utf16_str.push_back((UChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
795 this->utf16_to_utf8.push_back(idx);
797 this->utf16_to_utf8.push_back(idx);
799 this->utf16_str.push_back(
'\0');
800 this->utf16_to_utf8.push_back(s - string_base);
802 UText text = UTEXT_INITIALIZER;
803 UErrorCode status = U_ZERO_ERROR;
804 utext_openUChars(&text, this->utf16_str.data(), this->utf16_str.size() - 1, &status);
805 this->char_itr->setText(&text, status);
806 this->word_itr->setText(&text, status);
807 this->char_itr->first();
808 this->word_itr->first();
815 for (uint i = 0; i < this->utf16_to_utf8.size(); i++) {
816 if (this->utf16_to_utf8[i] == pos) {
825 this->char_itr->isBoundary(utf16_pos);
826 return this->utf16_to_utf8[this->char_itr->current()];
834 pos = this->char_itr->next();
838 pos = this->word_itr->following(this->char_itr->current());
842 while (pos != icu::BreakIterator::DONE &&
844 int32_t new_pos = this->word_itr->next();
847 if (new_pos == icu::BreakIterator::DONE)
break;
851 this->char_itr->isBoundary(pos);
858 return pos == icu::BreakIterator::DONE ?
END : this->utf16_to_utf8[pos];
866 pos = this->char_itr->previous();
870 pos = this->word_itr->preceding(this->char_itr->current());
874 while (pos != icu::BreakIterator::DONE &&
876 int32_t new_pos = this->word_itr->previous();
879 if (new_pos == icu::BreakIterator::DONE)
break;
883 this->char_itr->isBoundary(pos);
890 return pos == icu::BreakIterator::DONE ?
END : this->utf16_to_utf8[pos];
896 return std::make_unique<IcuStringIterator>();
909 DefaultStringIterator() : string(nullptr), len(0), cur_pos(0)
916 this->len = strlen(s);
922 assert(this->
string !=
nullptr && pos <= this->len);
924 while (pos > 0 && IsUtf8Part(this->
string[pos])) pos--;
925 return this->cur_pos = pos;
928 size_t Next(IterType what)
override
930 assert(this->
string !=
nullptr);
933 if (this->cur_pos >= this->len)
return END;
938 this->cur_pos +=
Utf8Decode(&c, this->
string + this->cur_pos);
939 return this->cur_pos;
945 size_t offs =
Utf8Decode(&c, this->
string + this->cur_pos);
947 this->cur_pos += offs;
948 offs =
Utf8Decode(&c, this->
string + this->cur_pos);
952 this->cur_pos += offs;
953 offs =
Utf8Decode(&c, this->
string + this->cur_pos);
956 return this->cur_pos;
966 size_t Prev(IterType what)
override
968 assert(this->
string !=
nullptr);
971 if (this->cur_pos == 0)
return END;
975 return this->cur_pos =
Utf8PrevChar(this->
string + this->cur_pos) - this->string;
978 const char *s = this->
string + this->cur_pos;
993 return this->cur_pos = s - this->string;
1004 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
1007 std::unique_ptr<StringIterator> i = OSXStringIterator::Create();
1008 if (i !=
nullptr)
return i;
1010 return std::make_unique<DefaultStringIterator>();
1015 return std::make_unique<DefaultStringIterator>();