OpenTTD Source: src/string.cpp Source File

OpenTTD Source 15.0-RC2

string.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "debug.h"
12#include "error_func.h"
13#include "string_func.h"
14#include "string_base.h"
15#include "core/utf8.hpp"
16#include "core/string_inplace.hpp"
17
18#include "table/control_codes.h"
19
20#ifdef _WIN32
21# include "os/windows/win32.h"
22#endif
23
24#ifdef WITH_UNISCRIBE
25# include "os/windows/string_uniscribe.h"
26#endif
27
28#ifdef WITH_ICU_I18N
29/* Required by StrNaturalCompare. */
30# include <unicode/brkiter.h>
31# include <unicode/stsearch.h>
32# include <unicode/ustring.h>
33# include <unicode/utext.h>
34# include "language.h"
35# include "gfx_func.h"
36#endif /* WITH_ICU_I18N */
37
38#if defined(WITH_COCOA)
39# include "os/macosx/string_osx.h"
40#endif
41
42#include "safeguards.h"
43
44
56 void strecpy(std::span<char> dst, std::string_view src)
57{
58 /* Ensure source string fits with NUL terminator; dst must be at least 1 character longer than src. */
59 if (std::empty(dst) || std::size(src) >= std::size(dst) - 1U) {
60#if defined(STRGEN) || defined(SETTINGSGEN)
61 FatalError("String too long for destination buffer");
62#else /* STRGEN || SETTINGSGEN */
63 Debug(misc, 0, "String too long for destination buffer");
64 src = src.substr(0, std::size(dst) - 1U);
65#endif /* STRGEN || SETTINGSGEN */
66 }
67
68 auto it = std::copy(std::begin(src), std::end(src), std::begin(dst));
69 *it = '\0';
70}
71
77std::string FormatArrayAsHex(std::span<const uint8_t> data)
78{
79 std::string str;
80 str.reserve(data.size() * 2 + 1);
81
82 for (auto b : data) {
83 format_append(str, "{:02X}", b);
84 }
85
86 return str;
87}
88
94 static bool IsSccEncodedCode(char32_t c)
95{
96 switch (c) {
97 case SCC_RECORD_SEPARATOR:
98 case SCC_ENCODED:
99 case SCC_ENCODED_INTERNAL:
100 case SCC_ENCODED_NUMERIC:
101 case SCC_ENCODED_STRING:
102 return true;
103
104 default:
105 return false;
106 }
107}
108
118template <class Builder>
119 static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
120{
121 /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
122 while (consumer.AnyBytesLeft()) {
123 auto c = consumer.TryReadUtf8();
124 if (!c.has_value()) {
125 /* Maybe the next byte is still a valid character? */
126 consumer.Skip(1);
127 continue;
128 }
129 if (*c == 0) break;
130
131 if ((IsPrintable(*c) && (*c < SCC_SPRITE_START || *c > SCC_SPRITE_END)) ||
132 (settings.Test(StringValidationSetting::AllowControlCode) && IsSccEncodedCode(*c)) ||
133 (settings.Test(StringValidationSetting::AllowNewline) && *c == '\n')) {
134 builder.PutUtf8(*c);
135 } else if (settings.Test(StringValidationSetting::AllowNewline) && *c == '\r' && consumer.PeekCharIf('\n')) {
136 /* Skip \r, if followed by \n */
137 /* continue */
138 } else if (settings.Test(StringValidationSetting::ReplaceTabCrNlWithSpace) && (*c == '\r' || *c == '\n' || *c == '\t')) {
139 /* Replace the tab, carriage return or newline with a space. */
140 builder.PutChar(' ');
141 } else if (settings.Test(StringValidationSetting::ReplaceWithQuestionMark)) {
142 /* Replace the undesirable character with a question mark */
143 builder.PutChar('?');
144 }
145 }
146
147 /* String termination, if needed, is left to the caller of this function. */
148}
149
157 void StrMakeValidInPlace(char *str, StringValidationSettings settings)
158{
159 InPlaceReplacement inplace(std::span(str, strlen(str)));
160 StrMakeValid(inplace.builder, inplace.consumer, settings);
161 /* Add NUL terminator, if we ended up with less bytes than before */
162 if (inplace.builder.AnyBytesUnused()) inplace.builder.PutChar('\0');
163}
164
172 void StrMakeValidInPlace(std::string &str, StringValidationSettings settings)
173{
174 if (str.empty()) return;
175
176 InPlaceReplacement inplace(std::span(str.data(), str.size()));
177 StrMakeValid(inplace.builder, inplace.consumer, settings);
178 str.erase(inplace.builder.GetBytesWritten(), std::string::npos);
179}
180
188std::string StrMakeValid(std::string_view str, StringValidationSettings settings)
189{
190 std::string result;
191 StringBuilder builder(result);
192 StringConsumer consumer(str);
193 StrMakeValid(builder, consumer, settings);
194 return result;
195}
196
205 bool StrValid(std::span<const char> str)
206{
207 /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
208 StringConsumer consumer(str);
209 while (consumer.AnyBytesLeft()) {
210 auto c = consumer.TryReadUtf8();
211 if (!c.has_value()) return false; // invalid codepoint
212 if (*c == 0) return true; // NUL termination
213 if (!IsPrintable(*c) || (*c >= SCC_SPRITE_START && *c <= SCC_SPRITE_END)) {
214 return false;
215 }
216 }
217
218 return false; // missing NUL termination
219}
220
228 void StrTrimInPlace(std::string &str)
229{
230 size_t first_pos = str.find_first_not_of(StringConsumer::WHITESPACE_NO_NEWLINE);
231 if (first_pos == std::string::npos) {
232 str.clear();
233 return;
234 }
235 str.erase(0, first_pos);
236
237 size_t last_pos = str.find_last_not_of(StringConsumer::WHITESPACE_NO_NEWLINE);
238 str.erase(last_pos + 1);
239}
240
241std::string_view StrTrimView(std::string_view str, std::string_view characters_to_trim)
242{
243 size_t first_pos = str.find_first_not_of(characters_to_trim);
244 if (first_pos == std::string::npos) {
245 return std::string_view{};
246 }
247 size_t last_pos = str.find_last_not_of(characters_to_trim);
248 return str.substr(first_pos, last_pos - first_pos + 1);
249}
250
257 bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix)
258{
259 if (str.size() < prefix.size()) return false;
260 return StrEqualsIgnoreCase(str.substr(0, prefix.size()), prefix);
261}
262
264 struct CaseInsensitiveCharTraits : public std::char_traits<char> {
265 static bool eq(char c1, char c2) { return toupper(c1) == toupper(c2); }
266 static bool ne(char c1, char c2) { return toupper(c1) != toupper(c2); }
267 static bool lt(char c1, char c2) { return toupper(c1) < toupper(c2); }
268
269 static int compare(const char *s1, const char *s2, size_t n)
270 {
271 while (n-- != 0) {
272 if (toupper(*s1) < toupper(*s2)) return -1;
273 if (toupper(*s1) > toupper(*s2)) return 1;
274 ++s1; ++s2;
275 }
276 return 0;
277 }
278
279 static const char *find(const char *s, size_t n, char a)
280 {
281 for (; n > 0; --n, ++s) {
282 if (toupper(*s) == toupper(a)) return s;
283 }
284 return nullptr;
285 }
286};
287
289 typedef std::basic_string_view<char, CaseInsensitiveCharTraits> CaseInsensitiveStringView;
290
297 bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix)
298{
299 if (str.size() < suffix.size()) return false;
300 return StrEqualsIgnoreCase(str.substr(str.size() - suffix.size()), suffix);
301}
302
310 int StrCompareIgnoreCase(std::string_view str1, std::string_view str2)
311{
312 CaseInsensitiveStringView ci_str1{ str1.data(), str1.size() };
313 CaseInsensitiveStringView ci_str2{ str2.data(), str2.size() };
314 return ci_str1.compare(ci_str2);
315}
316
323 bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
324{
325 if (str1.size() != str2.size()) return false;
326 return StrCompareIgnoreCase(str1, str2) == 0;
327}
328
336 bool StrContainsIgnoreCase(std::string_view str, std::string_view value)
337{
338 CaseInsensitiveStringView ci_str{ str.data(), str.size() };
339 CaseInsensitiveStringView ci_value{ value.data(), value.size() };
340 return ci_str.find(ci_value) != ci_str.npos;
341}
342
349 size_t Utf8StringLength(std::string_view str)
350{
351 Utf8View view(str);
352 return std::distance(view.begin(), view.end());
353}
354
355bool strtolower(std::string &str, std::string::size_type offs)
356{
357 bool changed = false;
358 for (auto ch = str.begin() + offs; ch != str.end(); ++ch) {
359 auto new_ch = static_cast<char>(tolower(static_cast<unsigned char>(*ch)));
360 changed |= new_ch != *ch;
361 *ch = new_ch;
362 }
363 return changed;
364}
365
373 bool IsValidChar(char32_t key, CharSetFilter afilter)
374{
375 switch (afilter) {
376 case CS_ALPHANUMERAL: return IsPrintable(key);
377 case CS_NUMERAL: return (key >= '0' && key <= '9');
378 case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
379 case CS_NUMERAL_SIGNED: return (key >= '0' && key <= '9') || key == '-';
380 case CS_ALPHA: return IsPrintable(key) && !(key >= '0' && key <= '9');
381 case CS_HEXADECIMAL: return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
382 default: NOT_REACHED();
383 }
384}
385
391 static bool IsGarbageCharacter(char32_t c)
392{
393 if (c >= '0' && c <= '9') return false;
394 if (c >= 'A' && c <= 'Z') return false;
395 if (c >= 'a' && c <= 'z') return false;
396 if (c >= SCC_CONTROL_START && c <= SCC_CONTROL_END) return true;
397 if (c >= 0xC0 && c <= 0x10FFFF) return false;
398
399 return true;
400}
401
410 static std::string_view SkipGarbage(std::string_view str)
411{
412 Utf8View view(str);
413 auto it = view.begin();
414 const auto end = view.end();
415 while (it != end && IsGarbageCharacter(*it)) ++it;
416 return str.substr(it.GetByteOffset());
417}
418
427 int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
428{
429 if (ignore_garbage_at_front) {
430 s1 = SkipGarbage(s1);
431 s2 = SkipGarbage(s2);
432 }
433
434#ifdef WITH_ICU_I18N
435 if (_current_collator) {
436 UErrorCode status = U_ZERO_ERROR;
437 int result = _current_collator->compareUTF8(icu::StringPiece(s1.data(), s1.size()), icu::StringPiece(s2.data(), s2.size()), status);
438 if (U_SUCCESS(status)) return result;
439 }
440#endif /* WITH_ICU_I18N */
441
442#if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
443 int res = OTTDStringCompare(s1, s2);
444 if (res != 0) return res - 2; // Convert to normal C return values.
445#endif
446
447#if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
448 int res = MacOSStringCompare(s1, s2);
449 if (res != 0) return res - 2; // Convert to normal C return values.
450#endif
451
452 /* Do a normal comparison if ICU is missing or if we cannot create a collator. */
453 return StrCompareIgnoreCase(s1, s2);
454}
455
456#ifdef WITH_ICU_I18N
457
466 static int ICUStringContains(std::string_view str, std::string_view value, bool case_insensitive)
467{
468 if (_current_collator) {
469 std::unique_ptr<icu::RuleBasedCollator> coll(dynamic_cast<icu::RuleBasedCollator *>(_current_collator->clone()));
470 if (coll) {
471 UErrorCode status = U_ZERO_ERROR;
472 coll->setStrength(case_insensitive ? icu::Collator::SECONDARY : icu::Collator::TERTIARY);
473 coll->setAttribute(UCOL_NUMERIC_COLLATION, UCOL_OFF, status);
474
475 auto u_str = icu::UnicodeString::fromUTF8(icu::StringPiece(str.data(), str.size()));
476 auto u_value = icu::UnicodeString::fromUTF8(icu::StringPiece(value.data(), value.size()));
477 icu::StringSearch u_searcher(u_value, u_str, coll.get(), nullptr, status);
478 if (U_SUCCESS(status)) {
479 auto pos = u_searcher.first(status);
480 if (U_SUCCESS(status)) return pos != USEARCH_DONE ? 1 : 0;
481 }
482 }
483 }
484
485 return -1;
486}
487#endif /* WITH_ICU_I18N */
488
496[[nodiscard]] bool StrNaturalContains(std::string_view str, std::string_view value)
497{
498#ifdef WITH_ICU_I18N
499 int res_u = ICUStringContains(str, value, false);
500 if (res_u >= 0) return res_u > 0;
501#endif /* WITH_ICU_I18N */
502
503#if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
504 int res = Win32StringContains(str, value, false);
505 if (res >= 0) return res > 0;
506#endif
507
508#if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
509 int res = MacOSStringContains(str, value, false);
510 if (res >= 0) return res > 0;
511#endif
512
513 return str.find(value) != std::string_view::npos;
514}
515
523[[nodiscard]] bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value)
524{
525#ifdef WITH_ICU_I18N
526 int res_u = ICUStringContains(str, value, true);
527 if (res_u >= 0) return res_u > 0;
528#endif /* WITH_ICU_I18N */
529
530#if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
531 int res = Win32StringContains(str, value, true);
532 if (res >= 0) return res > 0;
533#endif
534
535#if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
536 int res = MacOSStringContains(str, value, true);
537 if (res >= 0) return res > 0;
538#endif
539
540 CaseInsensitiveStringView ci_str{ str.data(), str.size() };
541 CaseInsensitiveStringView ci_value{ value.data(), value.size() };
542 return ci_str.find(ci_value) != CaseInsensitiveStringView::npos;
543}
544
551 static int ConvertHexNibbleToByte(char c)
552{
553 if (c >= '0' && c <= '9') return c - '0';
554 if (c >= 'A' && c <= 'F') return c + 10 - 'A';
555 if (c >= 'a' && c <= 'f') return c + 10 - 'a';
556 return -1;
557}
558
570 bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes)
571{
572 if (bytes.size() != hex.size() / 2) {
573 return false;
574 }
575
576 /* Hex-string lengths are always divisible by 2. */
577 if (hex.size() % 2 != 0) {
578 return false;
579 }
580
581 for (size_t i = 0; i < hex.size() / 2; i++) {
582 auto hi = ConvertHexNibbleToByte(hex[i * 2]);
583 auto lo = ConvertHexNibbleToByte(hex[i * 2 + 1]);
584
585 if (hi < 0 || lo < 0) {
586 return false;
587 }
588
589 bytes[i] = (hi << 4) | lo;
590 }
591
592 return true;
593}
594
595#ifdef WITH_UNISCRIBE
596
597/* static */ std::unique_ptr<StringIterator> StringIterator::Create()
598{
599 return std::make_unique<UniscribeStringIterator>();
600}
601
602#elif defined(WITH_ICU_I18N)
603
605 class IcuStringIterator : public StringIterator
606{
607 std::unique_ptr<icu::BreakIterator> char_itr;
608 std::unique_ptr<icu::BreakIterator> word_itr;
609
610 std::vector<UChar> utf16_str;
611 std::vector<size_t> utf16_to_utf8;
612
613public:
614 IcuStringIterator()
615 {
616 UErrorCode status = U_ZERO_ERROR;
617 auto locale = icu::Locale(_current_language != nullptr ? _current_language->isocode : "en");
618 this->char_itr.reset(icu::BreakIterator::createCharacterInstance(locale, status));
619 this->word_itr.reset(icu::BreakIterator::createWordInstance(locale, status));
620
621 this->utf16_str.push_back('\0');
622 this->utf16_to_utf8.push_back(0);
623 }
624
625 ~IcuStringIterator() override = default;
626
627 void SetString(std::string_view s) override
628 {
629 /* Unfortunately current ICU versions only provide rudimentary support
630 * for word break iterators (especially for CJK languages) in combination
631 * with UTF-8 input. As a work around we have to convert the input to
632 * UTF-16 and create a mapping back to UTF-8 character indices. */
633 this->utf16_str.clear();
634 this->utf16_to_utf8.clear();
635
636 Utf8View view(s);
637 for (auto it = view.begin(), end = view.end(); it != end; ++it) {
638 size_t idx = it.GetByteOffset();
639 char32_t c = *it;
640 if (c < 0x10000) {
641 this->utf16_str.push_back((UChar)c);
642 } else {
643 /* Make a surrogate pair. */
644 this->utf16_str.push_back((UChar)(0xD800 + ((c - 0x10000) >> 10)));
645 this->utf16_str.push_back((UChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
646 this->utf16_to_utf8.push_back(idx);
647 }
648 this->utf16_to_utf8.push_back(idx);
649 }
650 this->utf16_str.push_back('\0');
651 this->utf16_to_utf8.push_back(s.size());
652
653 UText text = UTEXT_INITIALIZER;
654 UErrorCode status = U_ZERO_ERROR;
655 utext_openUChars(&text, this->utf16_str.data(), this->utf16_str.size() - 1, &status);
656 this->char_itr->setText(&text, status);
657 this->word_itr->setText(&text, status);
658 this->char_itr->first();
659 this->word_itr->first();
660 }
661
662 size_t SetCurPosition(size_t pos) override
663 {
664 /* Convert incoming position to an UTF-16 string index. */
665 uint utf16_pos = 0;
666 for (uint i = 0; i < this->utf16_to_utf8.size(); i++) {
667 if (this->utf16_to_utf8[i] == pos) {
668 utf16_pos = i;
669 break;
670 }
671 }
672
673 /* isBoundary has the documented side-effect of setting the current
674 * position to the first valid boundary equal to or greater than
675 * the passed value. */
676 this->char_itr->isBoundary(utf16_pos);
677 return this->utf16_to_utf8[this->char_itr->current()];
678 }
679
680 size_t Next(IterType what) override
681 {
682 int32_t pos;
683 switch (what) {
684 case ITER_CHARACTER:
685 pos = this->char_itr->next();
686 break;
687
688 case ITER_WORD:
689 pos = this->word_itr->following(this->char_itr->current());
690 /* The ICU word iterator considers both the start and the end of a word a valid
691 * break point, but we only want word starts. Move to the next location in
692 * case the new position points to whitespace. */
693 while (pos != icu::BreakIterator::DONE &&
694 IsWhitespace(Utf16DecodeChar((const uint16_t *)&this->utf16_str[pos]))) {
695 int32_t new_pos = this->word_itr->next();
696 /* Don't set it to DONE if it was valid before. Otherwise we'll return END
697 * even though the iterator wasn't at the end of the string before. */
698 if (new_pos == icu::BreakIterator::DONE) break;
699 pos = new_pos;
700 }
701
702 this->char_itr->isBoundary(pos);
703 break;
704
705 default:
706 NOT_REACHED();
707 }
708
709 return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
710 }
711
712 size_t Prev(IterType what) override
713 {
714 int32_t pos;
715 switch (what) {
716 case ITER_CHARACTER:
717 pos = this->char_itr->previous();
718 break;
719
720 case ITER_WORD:
721 pos = this->word_itr->preceding(this->char_itr->current());
722 /* The ICU word iterator considers both the start and the end of a word a valid
723 * break point, but we only want word starts. Move to the previous location in
724 * case the new position points to whitespace. */
725 while (pos != icu::BreakIterator::DONE &&
726 IsWhitespace(Utf16DecodeChar((const uint16_t *)&this->utf16_str[pos]))) {
727 int32_t new_pos = this->word_itr->previous();
728 /* Don't set it to DONE if it was valid before. Otherwise we'll return END
729 * even though the iterator wasn't at the start of the string before. */
730 if (new_pos == icu::BreakIterator::DONE) break;
731 pos = new_pos;
732 }
733
734 this->char_itr->isBoundary(pos);
735 break;
736
737 default:
738 NOT_REACHED();
739 }
740
741 return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
742 }
743};
744
745/* static */ std::unique_ptr<StringIterator> StringIterator::Create()
746{
747 return std::make_unique<IcuStringIterator>();
748}
749
750#else
751
753class DefaultStringIterator : public StringIterator
754{
755 Utf8View string;
756 Utf8View::iterator cur_pos; //< Current iteration position.
757
758public:
759 void SetString(std::string_view s) override
760 {
761 this->string = s;
762 this->cur_pos = this->string.begin();
763 }
764
765 size_t SetCurPosition(size_t pos) override
766 {
767 this->cur_pos = this->string.GetIterAtByte(pos);
768 return this->cur_pos.GetByteOffset();
769 }
770
771 size_t Next(IterType what) override
772 {
773 const auto end = this->string.end();
774 /* Already at the end? */
775 if (this->cur_pos >= end) return END;
776
777 switch (what) {
778 case ITER_CHARACTER:
779 ++this->cur_pos;
780 return this->cur_pos.GetByteOffset();
781
782 case ITER_WORD:
783 /* Consume current word. */
784 while (this->cur_pos != end && !IsWhitespace(*this->cur_pos)) {
785 ++this->cur_pos;
786 }
787 /* Consume whitespace to the next word. */
788 while (this->cur_pos != end && IsWhitespace(*this->cur_pos)) {
789 ++this->cur_pos;
790 }
791 return this->cur_pos.GetByteOffset();
792
793 default:
794 NOT_REACHED();
795 }
796
797 return END;
798 }
799
800 size_t Prev(IterType what) override
801 {
802 const auto begin = this->string.begin();
803 /* Already at the beginning? */
804 if (this->cur_pos == begin) return END;
805
806 switch (what) {
807 case ITER_CHARACTER:
808 --this->cur_pos;
809 return this->cur_pos.GetByteOffset();
810
811 case ITER_WORD:
812 /* Consume preceding whitespace. */
813 do {
814 --this->cur_pos;
815 } while (this->cur_pos != begin && IsWhitespace(*this->cur_pos));
816 /* Consume preceding word. */
817 while (this->cur_pos != begin && !IsWhitespace(*this->cur_pos)) {
818 --this->cur_pos;
819 }
820 /* Move caret back to the beginning of the word. */
821 if (IsWhitespace(*this->cur_pos)) ++this->cur_pos;
822 return this->cur_pos.GetByteOffset();
823
824 default:
825 NOT_REACHED();
826 }
827
828 return END;
829 }
830};
831
832#if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
833/* static */ std::unique_ptr<StringIterator> StringIterator::Create()
834{
835 std::unique_ptr<StringIterator> i = OSXStringIterator::Create();
836 if (i != nullptr) return i;
837
838 return std::make_unique<DefaultStringIterator>();
839}
840#else
841/* static */ std::unique_ptr<StringIterator> StringIterator::Create()
842{
843 return std::make_unique<DefaultStringIterator>();
844}
845#endif /* defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) */
846
847#endif
848
854std::optional<std::string_view> GetEnv(const char *variable)
855{
856 auto val = std::getenv(variable);
857 if (val == nullptr || *val == '\0') return std::nullopt;
858 return val;
859}
BaseStringBuilder::PutChar
void PutChar(char c)
Append 8-bit char.
Definition string_builder.cpp:105
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
IcuStringIterator
String iterator using ICU as a backend.
Definition string.cpp:606
IcuStringIterator::Prev
size_t Prev(IterType what) override
Move the cursor back by one iteration unit.
Definition string.cpp:712
IcuStringIterator::word_itr
std::unique_ptr< icu::BreakIterator > word_itr
ICU iterator for words.
Definition string.cpp:608
IcuStringIterator::Next
size_t Next(IterType what) override
Advance the cursor by one iteration unit.
Definition string.cpp:680
IcuStringIterator::utf16_to_utf8
std::vector< size_t > utf16_to_utf8
Mapping from UTF-16 code point position to index in the UTF-8 source string.
Definition string.cpp:611
IcuStringIterator::char_itr
std::unique_ptr< icu::BreakIterator > char_itr
ICU iterator for characters.
Definition string.cpp:607
IcuStringIterator::SetString
void SetString(std::string_view s) override
Set a new iteration string.
Definition string.cpp:627
IcuStringIterator::SetCurPosition
size_t SetCurPosition(size_t pos) override
Change the current string cursor.
Definition string.cpp:662
IcuStringIterator::utf16_str
std::vector< UChar > utf16_str
UTF-16 copy of the string.
Definition string.cpp:610
InPlaceBuilder::AnyBytesUnused
bool AnyBytesUnused() const noexcept
Check whether any unused bytes are left between the Builder and Consumer position.
Definition string_inplace.cpp:19
InPlaceBuilder::GetBytesWritten
size_type GetBytesWritten() const noexcept
Get number of already written bytes.
Definition string_inplace.hpp:41
InPlaceReplacement
Compose data into a fixed size buffer, which is consumed at the same time.
Definition string_inplace.hpp:91
InPlaceReplacement::builder
InPlaceBuilder builder
Builder into shared buffer.
Definition string_inplace.hpp:94
InPlaceReplacement::consumer
StringConsumer consumer
Consumer from shared buffer.
Definition string_inplace.hpp:93
StringBuilder
Compose data into a growing std::string.
Definition string_builder.hpp:65
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
StringConsumer::AnyBytesLeft
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
Definition string_consumer.hpp:70
StringConsumer::PeekCharIf
bool PeekCharIf(char c) const
Check whether the next 8-bit char matches 'c'.
Definition string_consumer.hpp:471
StringConsumer::WHITESPACE_NO_NEWLINE
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
Definition string_consumer.hpp:40
StringConsumer::TryReadUtf8
std::optional< char32_t > TryReadUtf8()
Try to read a UTF-8 character, and then advance reader.
Definition string_consumer.hpp:412
StringConsumer::Skip
void Skip(size_type len)
Discard some bytes.
Definition string_consumer.cpp:97
StringIterator
Class for iterating over different kind of parts of a string.
Definition string_base.h:14
StringIterator::END
static const size_t END
Sentinel to indicate end-of-iteration.
Definition string_base.h:23
StringIterator::Prev
virtual size_t Prev(IterType what=ITER_CHARACTER)=0
Move the cursor back by one iteration unit.
StringIterator::SetCurPosition
virtual size_t SetCurPosition(size_t pos)=0
Change the current string cursor.
StringIterator::Next
virtual size_t Next(IterType what=ITER_CHARACTER)=0
Advance the cursor by one iteration unit.
StringIterator::Create
static std::unique_ptr< StringIterator > Create()
Create a new iterator instance.
Definition string.cpp:745
StringIterator::IterType
IterType
Type of the iterator.
Definition string_base.h:17
StringIterator::ITER_WORD
@ ITER_WORD
Iterate over words.
Definition string_base.h:19
StringIterator::ITER_CHARACTER
@ ITER_CHARACTER
Iterate over characters (or more exactly grapheme clusters).
Definition string_base.h:18
StringIterator::SetString
virtual void SetString(std::string_view s)=0
Set a new iteration string.
Utf8View::iterator
Bidirectional input iterator over codepoints.
Definition utf8.hpp:43
Utf8View
Constant span of UTF-8 encoded data.
Definition utf8.hpp:30
control_codes.h
Control codes that are embedded in the translation strings.
SCC_ENCODED
@ SCC_ENCODED
Encoded string marker and sub-string parameter.
Definition control_codes.h:27
SCC_ENCODED_NUMERIC
@ SCC_ENCODED_NUMERIC
Encoded numeric parameter.
Definition control_codes.h:29
SCC_ENCODED_STRING
@ SCC_ENCODED_STRING
Encoded string parameter.
Definition control_codes.h:30
SCC_ENCODED_INTERNAL
@ SCC_ENCODED_INTERNAL
Encoded text from OpenTTD.
Definition control_codes.h:28
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
error_func.h
Error reporting related functions.
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:25
gfx_func.h
Functions related to the gfx engine.
language.h
Information about languages and their files.
_current_language
const LanguageMetadata * _current_language
The currently loaded language.
Definition strings.cpp:54
_current_collator
std::unique_ptr< icu::Collator > _current_collator
Collator for the language currently in use.
Definition strings.cpp:59
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
ConvertHexToBytes
bool ConvertHexToBytes(std::string_view hex, std::span< uint8_t > bytes)
Convert a hex-string to a byte-array, while validating it was actually hex.
Definition string.cpp:570
StrNaturalContainsIgnoreCase
bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case insensi...
Definition string.cpp:523
Utf8StringLength
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:349
ICUStringContains
static int ICUStringContains(std::string_view str, std::string_view value, bool case_insensitive)
Search if a string is contained in another string using the current locale.
Definition string.cpp:466
StrNaturalContains
bool StrNaturalContains(std::string_view str, std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case sensiti...
Definition string.cpp:496
IsValidChar
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition string.cpp:373
GetEnv
std::optional< std::string_view > GetEnv(const char *variable)
Get the environment variable using std::getenv and when it is an empty string (or nullptr),...
Definition string.cpp:854
StrMakeValidInPlace
void StrMakeValidInPlace(char *str, StringValidationSettings settings)
Scans the string for invalid characters and replaces them with a question mark '?' (if not ignored).
Definition string.cpp:157
strecpy
void strecpy(std::span< char > dst, std::string_view src)
Copies characters from one buffer to another.
Definition string.cpp:56
FormatArrayAsHex
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:77
StrEqualsIgnoreCase
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:323
StrEndsWithIgnoreCase
bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix)
Check whether the given string ends with the given suffix, ignoring case.
Definition string.cpp:297
StrValid
bool StrValid(std::span< const char > str)
Checks whether the given string is valid, i.e.
Definition string.cpp:205
ConvertHexNibbleToByte
static int ConvertHexNibbleToByte(char c)
Convert a single hex-nibble to a byte.
Definition string.cpp:551
SkipGarbage
static std::string_view SkipGarbage(std::string_view str)
Skip some of the 'garbage' in the string that we don't want to use to sort on.
Definition string.cpp:410
IsSccEncodedCode
static bool IsSccEncodedCode(char32_t c)
Test if a character is (only) part of an encoded string.
Definition string.cpp:94
StrNaturalCompare
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition string.cpp:427
CaseInsensitiveStringView
std::basic_string_view< char, CaseInsensitiveCharTraits > CaseInsensitiveStringView
Case insensitive string view.
Definition string.cpp:289
StrTrimInPlace
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
Definition string.cpp:228
StrStartsWithIgnoreCase
bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix)
Check whether the given string starts with the given prefix, ignoring case.
Definition string.cpp:257
StrMakeValid
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:119
StrCompareIgnoreCase
int StrCompareIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
Definition string.cpp:310
IsGarbageCharacter
static bool IsGarbageCharacter(char32_t c)
Test if a unicode character is considered garbage to be skipped.
Definition string.cpp:391
StrContainsIgnoreCase
bool StrContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string, while ignoring the case of the characters.
Definition string.cpp:336
string_func.h
Functions related to low-level strings.
Utf16DecodeChar
char32_t Utf16DecodeChar(const uint16_t *c)
Decode an UTF-16 character.
Definition string_func.h:96
IsWhitespace
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
Definition string_func.h:143
string_inplace.hpp
Inplace-replacement of textual and binary data.
MacOSStringCompare
int MacOSStringCompare(std::string_view s1, std::string_view s2)
Compares two strings using case insensitive natural sort.
Definition string_osx.cpp:331
MacOSStringContains
int MacOSStringContains(std::string_view str, std::string_view value, bool case_insensitive)
Search if a string is contained in another string using the current locale.
Definition string_osx.cpp:355
string_osx.h
Functions related to localized text support on OSX.
StringValidationSetting::ReplaceWithQuestionMark
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
StringValidationSetting::AllowControlCode
@ AllowControlCode
Allow the special control codes.
StringValidationSetting::AllowNewline
@ AllowNewline
Allow newlines; replaces '\r ' with ' ' during processing.
StringValidationSetting::ReplaceTabCrNlWithSpace
@ ReplaceTabCrNlWithSpace
Replace tabs ('\t'), carriage returns ('\r') and newlines (' ') with spaces.
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition string_type.h:24
CS_NUMERAL_SPACE
@ CS_NUMERAL_SPACE
Only numbers and spaces.
Definition string_type.h:27
CS_HEXADECIMAL
@ CS_HEXADECIMAL
Only hexadecimal characters.
Definition string_type.h:30
CS_NUMERAL
@ CS_NUMERAL
Only numeric ones.
Definition string_type.h:26
CS_NUMERAL_SIGNED
@ CS_NUMERAL_SIGNED
Only numbers and '-' for negative values.
Definition string_type.h:28
CS_ALPHA
@ CS_ALPHA
Only alphabetic values.
Definition string_type.h:29
CS_ALPHANUMERAL
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition string_type.h:25
string_uniscribe.h
Functions related to laying out text on Win32.
CaseInsensitiveCharTraits
Case insensitive implementation of the standard character type traits.
Definition string.cpp:264
LanguagePackHeader::isocode
char isocode[16]
the ISO code for the language (not country code)
Definition language.h:31
utf8.hpp
Handling of UTF-8 encoded data.
Win32StringContains
int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive)
Search if a string is contained in another string using the current locale.
Definition win32.cpp:480
win32.h
declarations of functions for MS windows systems