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