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