OpenTTD Source: src/string.cpp Source File

OpenTTD Source 1.11.0-RC1

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "core/alloc_func.hpp"
13 #include "core/math_func.hpp"
14 #include "string_func.h"
15 #include "string_base.h"
16
17 #include "table/control_codes.h"
18
19 #include <stdarg.h>
20 #include <ctype.h> /* required for tolower() */
21 #include <sstream>
22
23 #ifdef _MSC_VER
24 #include <errno.h> // required by vsnprintf implementation for MSVC
25 #endif
26
27 #ifdef _WIN32
28 #include "os/windows/win32.h"
29 #endif
30
31 #ifdef WITH_UNISCRIBE
32 #include "os/windows/string_uniscribe.h"
33 #endif
34
35 #ifdef WITH_ICU_I18N
36 /* Required by strnatcmp. */
37 #include <unicode/ustring.h>
38 #include "language.h"
39 #include "gfx_func.h"
40 #endif /* WITH_ICU_I18N */
41
42 #if defined(WITH_COCOA)
43 #include "os/macosx/string_osx.h"
44 #endif
45
46 /* The function vsnprintf is used internally to perform the required formatting
47 * tasks. As such this one must be allowed, and makes sure it's terminated. */
48 #include "safeguards.h"
49 #undef vsnprintf
50
61 int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
62 {
63 ptrdiff_t diff = last - str;
64 if (diff < 0) return 0;
65 return std::min(static_cast<int>(diff), vsnprintf(str, diff + 1, format, ap));
66 }
67
84 char *strecat(char *dst, const char *src, const char *last)
85 {
86 assert(dst <= last);
87 while (*dst != '\0') {
88 if (dst == last) return dst;
89 dst++;
90 }
91
92 return strecpy(dst, src, last);
93 }
94
95
112 char *strecpy(char *dst, const char *src, const char *last)
113 {
114 assert(dst <= last);
115 while (dst != last && *src != '\0') {
116 *dst++ = *src++;
117 }
118 *dst = '\0';
119
120 if (dst == last && *src != '\0') {
121 #if defined(STRGEN) || defined(SETTINGSGEN)
122 error("String too long for destination buffer");
123 #else /* STRGEN || SETTINGSGEN */
124 DEBUG(misc, 0, "String too long for destination buffer");
125 #endif /* STRGEN || SETTINGSGEN */
126 }
127 return dst;
128 }
129
137 char *stredup(const char *s, const char *last)
138 {
139 size_t len = last == nullptr ? strlen(s) : ttd_strnlen(s, last - s + 1);
140 char *tmp = CallocT<char>(len + 1);
141 memcpy(tmp, s, len);
142 return tmp;
143 }
144
150 char *CDECL str_fmt(const char *str, ...)
151 {
152 char buf[4096];
153 va_list va;
154
155 va_start(va, str);
156 int len = vseprintf(buf, lastof(buf), str, va);
157 va_end(va);
158 char *p = MallocT<char>(len + 1);
159 memcpy(p, buf, len + 1);
160 return p;
161 }
162
169 void str_fix_scc_encoded(char *str, const char *last)
170 {
171 while (str <= last && *str != '\0') {
172 size_t len = Utf8EncodedCharLen(*str);
173 if ((len == 0 && str + 4 > last) || str + len > last) break;
174
175 WChar c;
176 Utf8Decode(&c, str);
177 if (c == '\0') break;
178
179 if (c == 0xE028 || c == 0xE02A) {
180 c = SCC_ENCODED;
181 }
182 str += Utf8Encode(str, c);
183 }
184 *str = '\0';
185 }
186
187
188 template <class T>
189 static void str_validate(T &dst, const char *str, const char *last, StringValidationSettings settings)
190 {
191 /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
192
193 while (str <= last && *str != '\0') {
194 size_t len = Utf8EncodedCharLen(*str);
195 /* If the character is unknown, i.e. encoded length is 0
196 * we assume worst case for the length check.
197 * The length check is needed to prevent Utf8Decode to read
198 * over the terminating '\0' if that happens to be placed
199 * within the encoding of an UTF8 character. */
200 if ((len == 0 && str + 4 > last) || str + len > last) break;
201
202 WChar c;
203 len = Utf8Decode(&c, str);
204 /* It's possible to encode the string termination character
205 * into a multiple bytes. This prevents those termination
206 * characters to be skipped */
207 if (c == '\0') break;
208
209 if ((IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END)) || ((settings & SVS_ALLOW_CONTROL_CODE) != 0 && c == SCC_ENCODED)) {
210 /* Copy the character back. Even if dst is current the same as str
211 * (i.e. no characters have been changed) this is quicker than
212 * moving the pointers ahead by len */
213 do {
214 *dst++ = *str++;
215 } while (--len != 0);
216 } else if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\n') {
217 *dst++ = *str++;
218 } else {
219 if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') {
220 str += len;
221 continue;
222 }
223 /* Replace the undesirable character with a question mark */
224 str += len;
225 if ((settings & SVS_REPLACE_WITH_QUESTION_MARK) != 0) *dst++ = '?';
226 }
227 }
228 }
229
237 void str_validate(char *str, const char *last, StringValidationSettings settings)
238 {
239 char *dst = str;
240 str_validate(dst, str, last, settings);
241 *dst = '\0';
242 }
243
250 std::string str_validate(const std::string &str, StringValidationSettings settings)
251 {
252 auto buf = str.data();
253 auto last = buf + str.size();
254
255 std::ostringstream dst;
256 std::ostreambuf_iterator<char> dst_iter(dst);
257 str_validate(dst_iter, buf, last, settings);
258
259 return dst.str();
260 }
261
267 void ValidateString(const char *str)
268 {
269 /* We know it is '\0' terminated. */
270 str_validate(const_cast<char *>(str), str + strlen(str) + 1);
271 }
272
273
281 bool StrValid(const char *str, const char *last)
282 {
283 /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
284
285 while (str <= last && *str != '\0') {
286 size_t len = Utf8EncodedCharLen(*str);
287 /* Encoded length is 0 if the character isn't known.
288 * The length check is needed to prevent Utf8Decode to read
289 * over the terminating '\0' if that happens to be placed
290 * within the encoding of an UTF8 character. */
291 if (len == 0 || str + len > last) return false;
292
293 WChar c;
294 len = Utf8Decode(&c, str);
295 if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
296 return false;
297 }
298
299 str += len;
300 }
301
302 return *str == '\0';
303 }
304
306 void str_strip_colours(char *str)
307 {
308 char *dst = str;
309 WChar c;
310 size_t len;
311
312 for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
313 if (c < SCC_BLUE || c > SCC_BLACK) {
314 /* Copy the character back. Even if dst is current the same as str
315 * (i.e. no characters have been changed) this is quicker than
316 * moving the pointers ahead by len */
317 do {
318 *dst++ = *str++;
319 } while (--len != 0);
320 } else {
321 /* Just skip (strip) the colour codes */
322 str += len;
323 }
324 }
325 *dst = '\0';
326 }
327
334 size_t Utf8StringLength(const char *s)
335 {
336 size_t len = 0;
337 const char *t = s;
338 while (Utf8Consume(&t) != 0) len++;
339 return len;
340 }
341
342
354 bool strtolower(char *str)
355 {
356 bool changed = false;
357 for (; *str != '\0'; str++) {
358 char new_str = tolower(*str);
359 changed |= new_str != *str;
360 *str = new_str;
361 }
362 return changed;
363 }
364
365 bool strtolower(std::string &str, std::string::size_type offs)
366 {
367 bool changed = false;
368 for (auto ch = str.begin() + offs; ch != str.end(); ++ch) {
369 auto new_ch = static_cast<char>(tolower(static_cast<unsigned char>(*ch)));
370 changed |= new_ch != *ch;
371 *ch = new_ch;
372 }
373 return changed;
374 }
375
383 bool IsValidChar(WChar key, CharSetFilter afilter)
384 {
385 switch (afilter) {
386 case CS_ALPHANUMERAL: return IsPrintable(key);
387 case CS_NUMERAL: return (key >= '0' && key <= '9');
388 case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
389 case CS_ALPHA: return IsPrintable(key) && !(key >= '0' && key <= '9');
390 case CS_HEXADECIMAL: return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
391 default: NOT_REACHED();
392 }
393 }
394
395 #ifdef _WIN32
396 #if defined(_MSC_VER) && _MSC_VER < 1900
397
404 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
405 {
406 if (size == 0) return 0;
407
408 errno = 0;
409 int ret = _vsnprintf(str, size, format, ap);
410
411 if (ret < 0) {
412 if (errno != ERANGE) {
413 /* There's a formatting error, better get that looked
414 * at properly instead of ignoring it. */
415 NOT_REACHED();
416 }
417 } else if ((size_t)ret < size) {
418 /* The buffer is big enough for the number of
419 * characters stored (excluding null), i.e.
420 * the string has been null-terminated. */
421 return ret;
422 }
423
424 /* The buffer is too small for _vsnprintf to write the
425 * null-terminator at its end and return size. */
426 str[size - 1] = '\0';
427 return (int)size;
428 }
429 #endif /* _MSC_VER */
430
431 #endif /* _WIN32 */
432
442 int CDECL seprintf(char *str, const char *last, const char *format, ...)
443 {
444 va_list ap;
445
446 va_start(ap, format);
447 int ret = vseprintf(str, last, format, ap);
448 va_end(ap);
449 return ret;
450 }
451
452
460 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
461 {
462 char *p = buf;
463
464 for (uint i = 0; i < 16; i++) {
465 p += seprintf(p, last, "%02X", md5sum[i]);
466 }
467
468 return p;
469 }
470
471
472 /* UTF-8 handling routines */
473
474
481 size_t Utf8Decode(WChar *c, const char *s)
482 {
483 assert(c != nullptr);
484
485 if (!HasBit(s[0], 7)) {
486 /* Single byte character: 0xxxxxxx */
487 *c = s[0];
488 return 1;
489 } else if (GB(s[0], 5, 3) == 6) {
490 if (IsUtf8Part(s[1])) {
491 /* Double byte character: 110xxxxx 10xxxxxx */
492 *c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
493 if (*c >= 0x80) return 2;
494 }
495 } else if (GB(s[0], 4, 4) == 14) {
496 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
497 /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
498 *c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
499 if (*c >= 0x800) return 3;
500 }
501 } else if (GB(s[0], 3, 5) == 30) {
502 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
503 /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
504 *c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
505 if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
506 }
507 }
508
509 /* DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence"); */
510 *c = '?';
511 return 1;
512 }
513
514
522 template <class T>
523 inline size_t Utf8Encode(T buf, WChar c)
524 {
525 if (c < 0x80) {
526 *buf = c;
527 return 1;
528 } else if (c < 0x800) {
529 *buf++ = 0xC0 + GB(c, 6, 5);
530 *buf = 0x80 + GB(c, 0, 6);
531 return 2;
532 } else if (c < 0x10000) {
533 *buf++ = 0xE0 + GB(c, 12, 4);
534 *buf++ = 0x80 + GB(c, 6, 6);
535 *buf = 0x80 + GB(c, 0, 6);
536 return 3;
537 } else if (c < 0x110000) {
538 *buf++ = 0xF0 + GB(c, 18, 3);
539 *buf++ = 0x80 + GB(c, 12, 6);
540 *buf++ = 0x80 + GB(c, 6, 6);
541 *buf = 0x80 + GB(c, 0, 6);
542 return 4;
543 }
544
545 /* DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c); */
546 *buf = '?';
547 return 1;
548 }
549
550 size_t Utf8Encode(char *buf, WChar c)
551 {
552 return Utf8Encode<char *>(buf, c);
553 }
554
555 size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, WChar c)
556 {
557 return Utf8Encode<std::ostreambuf_iterator<char> &>(buf, c);
558 }
559
567 size_t Utf8TrimString(char *s, size_t maxlen)
568 {
569 size_t length = 0;
570
571 for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
572 size_t len = Utf8EncodedCharLen(*s);
573 /* Silently ignore invalid UTF8 sequences, our only concern trimming */
574 if (len == 0) len = 1;
575
576 /* Take care when a hard cutoff was made for the string and
577 * the last UTF8 sequence is invalid */
578 if (length + len >= maxlen || (s + len > ptr)) break;
579 s += len;
580 length += len;
581 }
582
583 *s = '\0';
584 return length;
585 }
586
587 #ifdef DEFINE_STRCASESTR
588 char *strcasestr(const char *haystack, const char *needle)
589 {
590 size_t hay_len = strlen(haystack);
591 size_t needle_len = strlen(needle);
592 while (hay_len >= needle_len) {
593 if (strncasecmp(haystack, needle, needle_len) == 0) return const_cast<char *>(haystack);
594
595 haystack++;
596 hay_len--;
597 }
598
599 return nullptr;
600 }
601 #endif /* DEFINE_STRCASESTR */
602
611 static const char *SkipGarbage(const char *str)
612 {
613 while (*str != '\0' && (*str < '0' || IsInsideMM(*str, ';', '@' + 1) || IsInsideMM(*str, '[', '`' + 1) || IsInsideMM(*str, '{', '~' + 1))) str++;
614 return str;
615 }
616
625 int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
626 {
627 if (ignore_garbage_at_front) {
628 s1 = SkipGarbage(s1);
629 s2 = SkipGarbage(s2);
630 }
631
632 #ifdef WITH_ICU_I18N
633 if (_current_collator) {
634 UErrorCode status = U_ZERO_ERROR;
635 int result = _current_collator->compareUTF8(s1, s2, status);
636 if (U_SUCCESS(status)) return result;
637 }
638 #endif /* WITH_ICU_I18N */
639
640 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
641 int res = OTTDStringCompare(s1, s2);
642 if (res != 0) return res - 2; // Convert to normal C return values.
643 #endif
644
645 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
646 int res = MacOSStringCompare(s1, s2);
647 if (res != 0) return res - 2; // Convert to normal C return values.
648 #endif
649
650 /* Do a normal comparison if ICU is missing or if we cannot create a collator. */
651 return strcasecmp(s1, s2);
652 }
653
654 #ifdef WITH_UNISCRIBE
655
656 /* static */ StringIterator *StringIterator::Create()
657 {
658 return new UniscribeStringIterator();
659 }
660
661 #elif defined(WITH_ICU_I18N)
662
663 #include <unicode/utext.h>
664 #include <unicode/brkiter.h>
665
667 class IcuStringIterator : public StringIterator
668 {
669 icu::BreakIterator *char_itr;
670 icu::BreakIterator *word_itr;
671
672 std::vector<UChar> utf16_str;
673 std::vector<size_t> utf16_to_utf8;
674
675 public:
676 IcuStringIterator() : char_itr(nullptr), word_itr(nullptr)
677 {
678 UErrorCode status = U_ZERO_ERROR;
679 this->char_itr = icu::BreakIterator::createCharacterInstance(icu::Locale(_current_language != nullptr ? _current_language->isocode : "en"), status);
680 this->word_itr = icu::BreakIterator::createWordInstance(icu::Locale(_current_language != nullptr ? _current_language->isocode : "en"), status);
681
682 this->utf16_str.push_back('\0');
683 this->utf16_to_utf8.push_back(0);
684 }
685
686 ~IcuStringIterator() override
687 {
688 delete this->char_itr;
689 delete this->word_itr;
690 }
691
692 void SetString(const char *s) override
693 {
694 const char *string_base = s;
695
696 /* Unfortunately current ICU versions only provide rudimentary support
697 * for word break iterators (especially for CJK languages) in combination
698 * with UTF-8 input. As a work around we have to convert the input to
699 * UTF-16 and create a mapping back to UTF-8 character indices. */
700 this->utf16_str.clear();
701 this->utf16_to_utf8.clear();
702
703 while (*s != '\0') {
704 size_t idx = s - string_base;
705
706 WChar c = Utf8Consume(&s);
707 if (c < 0x10000) {
708 this->utf16_str.push_back((UChar)c);
709 } else {
710 /* Make a surrogate pair. */
711 this->utf16_str.push_back((UChar)(0xD800 + ((c - 0x10000) >> 10)));
712 this->utf16_str.push_back((UChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
713 this->utf16_to_utf8.push_back(idx);
714 }
715 this->utf16_to_utf8.push_back(idx);
716 }
717 this->utf16_str.push_back('\0');
718 this->utf16_to_utf8.push_back(s - string_base);
719
720 UText text = UTEXT_INITIALIZER;
721 UErrorCode status = U_ZERO_ERROR;
722 utext_openUChars(&text, this->utf16_str.data(), this->utf16_str.size() - 1, &status);
723 this->char_itr->setText(&text, status);
724 this->word_itr->setText(&text, status);
725 this->char_itr->first();
726 this->word_itr->first();
727 }
728
729 size_t SetCurPosition(size_t pos) override
730 {
731 /* Convert incoming position to an UTF-16 string index. */
732 uint utf16_pos = 0;
733 for (uint i = 0; i < this->utf16_to_utf8.size(); i++) {
734 if (this->utf16_to_utf8[i] == pos) {
735 utf16_pos = i;
736 break;
737 }
738 }
739
740 /* isBoundary has the documented side-effect of setting the current
741 * position to the first valid boundary equal to or greater than
742 * the passed value. */
743 this->char_itr->isBoundary(utf16_pos);
744 return this->utf16_to_utf8[this->char_itr->current()];
745 }
746
747 size_t Next(IterType what) override
748 {
749 int32_t pos;
750 switch (what) {
751 case ITER_CHARACTER:
752 pos = this->char_itr->next();
753 break;
754
755 case ITER_WORD:
756 pos = this->word_itr->following(this->char_itr->current());
757 /* The ICU word iterator considers both the start and the end of a word a valid
758 * break point, but we only want word starts. Move to the next location in
759 * case the new position points to whitespace. */
760 while (pos != icu::BreakIterator::DONE &&
761 IsWhitespace(Utf16DecodeChar((const uint16 *)&this->utf16_str[pos]))) {
762 int32_t new_pos = this->word_itr->next();
763 /* Don't set it to DONE if it was valid before. Otherwise we'll return END
764 * even though the iterator wasn't at the end of the string before. */
765 if (new_pos == icu::BreakIterator::DONE) break;
766 pos = new_pos;
767 }
768
769 this->char_itr->isBoundary(pos);
770 break;
771
772 default:
773 NOT_REACHED();
774 }
775
776 return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
777 }
778
779 size_t Prev(IterType what) override
780 {
781 int32_t pos;
782 switch (what) {
783 case ITER_CHARACTER:
784 pos = this->char_itr->previous();
785 break;
786
787 case ITER_WORD:
788 pos = this->word_itr->preceding(this->char_itr->current());
789 /* The ICU word iterator considers both the start and the end of a word a valid
790 * break point, but we only want word starts. Move to the previous location in
791 * case the new position points to whitespace. */
792 while (pos != icu::BreakIterator::DONE &&
793 IsWhitespace(Utf16DecodeChar((const uint16 *)&this->utf16_str[pos]))) {
794 int32_t new_pos = this->word_itr->previous();
795 /* Don't set it to DONE if it was valid before. Otherwise we'll return END
796 * even though the iterator wasn't at the start of the string before. */
797 if (new_pos == icu::BreakIterator::DONE) break;
798 pos = new_pos;
799 }
800
801 this->char_itr->isBoundary(pos);
802 break;
803
804 default:
805 NOT_REACHED();
806 }
807
808 return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
809 }
810 };
811
812 /* static */ StringIterator *StringIterator::Create()
813 {
814 return new IcuStringIterator();
815 }
816
817 #else
818
820 class DefaultStringIterator : public StringIterator
821 {
822 const char *string;
823 size_t len;
824 size_t cur_pos;
825
826 public:
827 DefaultStringIterator() : string(nullptr), len(0), cur_pos(0)
828 {
829 }
830
831 virtual void SetString(const char *s)
832 {
833 this->string = s;
834 this->len = strlen(s);
835 this->cur_pos = 0;
836 }
837
838 virtual size_t SetCurPosition(size_t pos)
839 {
840 assert(this->string != nullptr && pos <= this->len);
841 /* Sanitize in case we get a position inside an UTF-8 sequence. */
842 while (pos > 0 && IsUtf8Part(this->string[pos])) pos--;
843 return this->cur_pos = pos;
844 }
845
846 virtual size_t Next(IterType what)
847 {
848 assert(this->string != nullptr);
849
850 /* Already at the end? */
851 if (this->cur_pos >= this->len) return END;
852
853 switch (what) {
854 case ITER_CHARACTER: {
855 WChar c;
856 this->cur_pos += Utf8Decode(&c, this->string + this->cur_pos);
857 return this->cur_pos;
858 }
859
860 case ITER_WORD: {
861 WChar c;
862 /* Consume current word. */
863 size_t offs = Utf8Decode(&c, this->string + this->cur_pos);
864 while (this->cur_pos < this->len && !IsWhitespace(c)) {
865 this->cur_pos += offs;
866 offs = Utf8Decode(&c, this->string + this->cur_pos);
867 }
868 /* Consume whitespace to the next word. */
869 while (this->cur_pos < this->len && IsWhitespace(c)) {
870 this->cur_pos += offs;
871 offs = Utf8Decode(&c, this->string + this->cur_pos);
872 }
873
874 return this->cur_pos;
875 }
876
877 default:
878 NOT_REACHED();
879 }
880
881 return END;
882 }
883
884 virtual size_t Prev(IterType what)
885 {
886 assert(this->string != nullptr);
887
888 /* Already at the beginning? */
889 if (this->cur_pos == 0) return END;
890
891 switch (what) {
892 case ITER_CHARACTER:
893 return this->cur_pos = Utf8PrevChar(this->string + this->cur_pos) - this->string;
894
895 case ITER_WORD: {
896 const char *s = this->string + this->cur_pos;
897 WChar c;
898 /* Consume preceding whitespace. */
899 do {
900 s = Utf8PrevChar(s);
901 Utf8Decode(&c, s);
902 } while (s > this->string && IsWhitespace(c));
903 /* Consume preceding word. */
904 while (s > this->string && !IsWhitespace(c)) {
905 s = Utf8PrevChar(s);
906 Utf8Decode(&c, s);
907 }
908 /* Move caret back to the beginning of the word. */
909 if (IsWhitespace(c)) Utf8Consume(&s);
910
911 return this->cur_pos = s - this->string;
912 }
913
914 default:
915 NOT_REACHED();
916 }
917
918 return END;
919 }
920 };
921
922 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
923 /* static */ StringIterator *StringIterator::Create()
924 {
925 StringIterator *i = OSXStringIterator::Create();
926 if (i != nullptr) return i;
927
928 return new DefaultStringIterator();
929 }
930 #else
931 /* static */ StringIterator *StringIterator::Create()
932 {
933 return new DefaultStringIterator();
934 }
935 #endif /* defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) */
936
937 #endif
StringIterator::Prev
virtual size_t Prev(IterType what=ITER_CHARACTER)=0
Move the cursor back by one iteration unit.
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:673
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
SVS_ALLOW_NEWLINE
@ SVS_ALLOW_NEWLINE
Allow newlines.
Definition: string_type.h:51
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
StringIterator::IterType
IterType
Type of the iterator.
Definition: string_base.h:17
str_validate
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:237
strtolower
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:354
win32.h
StringIterator::END
static const size_t END
Sentinel to indicate end-of-iteration.
Definition: string_base.h:23
math_func.hpp
str_fix_scc_encoded
void str_fix_scc_encoded(char *str, const char *last)
Scan the string for old values of SCC_ENCODED and fix it to it's new, static value.
Definition: string.cpp:169
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
IcuStringIterator::word_itr
icu::BreakIterator * word_itr
ICU iterator for words.
Definition: string.cpp:670
_current_collator
std::unique_ptr< icu::Collator > _current_collator
Collator for the language currently in use.
Definition: strings.cpp:51
Utf8Encode
size_t Utf8Encode(T buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:523
StringIterator::Next
virtual size_t Next(IterType what=ITER_CHARACTER)=0
Advance the cursor by one iteration unit.
CS_ALPHA
@ CS_ALPHA
Only alphabetic values.
Definition: string_type.h:30
UniscribeStringIterator
String iterator using Uniscribe as a backend.
Definition: string_uniscribe.h:67
Utf16DecodeChar
static WChar Utf16DecodeChar(const uint16 *c)
Decode an UTF-16 character.
Definition: string_func.h:205
StringIterator::SetString
virtual void SetString(const char *s)=0
Set a new iteration string.
IcuStringIterator::char_itr
icu::BreakIterator * char_itr
ICU iterator for characters.
Definition: string.cpp:669
StringIterator::ITER_CHARACTER
@ ITER_CHARACTER
Iterate over characters (or more exactly grapheme clusters).
Definition: string_base.h:18
control_codes.h
IsInsideMM
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:204
IcuStringIterator::Next
size_t Next(IterType what) override
Advance the cursor by one iteration unit.
Definition: string.cpp:747
string_osx.h
gfx_func.h
Utf8StringLength
size_t Utf8StringLength(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition: string.cpp:334
StrValid
bool StrValid(const char *str, const char *last)
Checks whether the given string is valid, i.e.
Definition: string.cpp:281
StringIterator::Create
static StringIterator * Create()
Create a new iterator instance.
Definition: string.cpp:812
SVS_ALLOW_CONTROL_CODE
@ SVS_ALLOW_CONTROL_CODE
Allow the special control codes.
Definition: string_type.h:52
StringIterator
Class for iterating over different kind of parts of a string.
Definition: string_base.h:14
IcuStringIterator::SetCurPosition
size_t SetCurPosition(size_t pos) override
Change the current string cursor.
Definition: string.cpp:729
StringIterator::SetCurPosition
virtual size_t SetCurPosition(size_t pos)=0
Change the current string cursor.
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
str_strip_colours
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
Definition: string.cpp:306
_current_language
const LanguageMetadata * _current_language
The currently loaded language.
Definition: strings.cpp:46
safeguards.h
IcuStringIterator::utf16_str
std::vector< UChar > utf16_str
UTF-16 copy of the string.
Definition: string.cpp:672
IsValidChar
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:383
ttd_strnlen
static size_t ttd_strnlen(const char *str, size_t maxlen)
Get the length of a string, within a limited buffer.
Definition: string_func.h:72
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
ValidateString
void ValidateString(const char *str)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:267
vseprintf
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:61
StringIterator::ITER_WORD
@ ITER_WORD
Iterate over words.
Definition: string_base.h:19
language.h
stdafx.h
CS_ALPHANUMERAL
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:27
LanguagePackHeader::isocode
char isocode[16]
the ISO code for the language (not country code)
Definition: language.h:31
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
Utf8Decode
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:481
string_func.h
str_fmt
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
Definition: string.cpp:150
alloc_func.hpp
IcuStringIterator::SetString
void SetString(const char *s) override
Set a new iteration string.
Definition: string.cpp:692
IcuStringIterator
String iterator using ICU as a backend.
Definition: string.cpp:667
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:131
Utf8TrimString
size_t Utf8TrimString(char *s, size_t maxlen)
Properly terminate an UTF8 string to some maximum length.
Definition: string.cpp:567
CS_NUMERAL_SPACE
@ CS_NUMERAL_SPACE
Only numbers and spaces.
Definition: string_type.h:29
SkipGarbage
static const char * SkipGarbage(const char *str)
Skip some of the 'garbage' in the string that we don't want to use to sort on.
Definition: string.cpp:611
IsWhitespace
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:252
strnatcmp
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:625
md5sumToString
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:460
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
CS_HEXADECIMAL
@ CS_HEXADECIMAL
Only hexadecimal characters.
Definition: string_type.h:31
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
MacOSStringCompare
int MacOSStringCompare(const char *s1, const char *s2)
Compares two strings using case insensitive natural sort.
Definition: string_osx.cpp:323
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
CS_NUMERAL
@ CS_NUMERAL
Only numeric ones.
Definition: string_type.h:28
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:26
Utf8PrevChar
static char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
Definition: string_func.h:153
debug.h
string_uniscribe.h
Utf8EncodedCharLen
static int8 Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Definition: string_func.h:128
IcuStringIterator::Prev
size_t Prev(IterType what) override
Move the cursor back by one iteration unit.
Definition: string.cpp:779