15 #include "string_base.h" 34 #if defined(WITH_COCOA) 40 #include <unicode/ustring.h> 60 int CDECL
vseprintf(
char *str,
const char *last,
const char *format, va_list ap)
62 ptrdiff_t diff = last - str;
63 if (diff < 0)
return 0;
64 return min((
int)diff, vsnprintf(str, diff + 1, format, ap));
83 char *
strecat(
char *dst,
const char *src,
const char *last)
86 while (*dst !=
'\0') {
87 if (dst == last)
return dst;
111 char *
strecpy(
char *dst,
const char *src,
const char *last)
114 while (dst != last && *src !=
'\0') {
119 if (dst == last && *src !=
'\0') {
120 #if defined(STRGEN) || defined(SETTINGSGEN) 121 error(
"String too long for destination buffer");
123 DEBUG(misc, 0,
"String too long for destination buffer");
136 char *
stredup(
const char *s,
const char *last)
138 size_t len = last ==
nullptr ? strlen(s) :
ttd_strnlen(s, last - s + 1);
139 char *tmp = CallocT<char>(len + 1);
157 char *p = MallocT<char>(len + 1);
158 memcpy(p, buf, len + 1);
170 while (str <= last && *str !=
'\0') {
172 if ((len == 0 && str + 4 > last) || str + len > last)
break;
176 if (c ==
'\0')
break;
178 if (c == 0xE028 || c == 0xE02A) {
199 while (str <= last && *str !=
'\0') {
206 if ((len == 0 && str + 4 > last) || str + len > last)
break;
213 if (c ==
'\0')
break;
215 if ((IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END)) || ((settings &
SVS_ALLOW_CONTROL_CODE) != 0 && c == SCC_ENCODED)) {
221 }
while (--len != 0);
225 if ((settings & SVS_ALLOW_NEWLINE) != 0 && c ==
'\r' && str[1] ==
'\n') {
246 str_validate(const_cast<char *>(str), str + strlen(str) + 1);
261 while (str <= last && *str !=
'\0') {
267 if (len == 0 || str + len > last)
return false;
271 if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
289 if (c < SCC_BLUE || c > SCC_BLACK) {
295 }
while (--len != 0);
314 while (Utf8Consume(&t) != 0) len++;
332 bool changed =
false;
333 for (; *str !=
'\0'; str++) {
334 char new_str = tolower(*str);
335 changed |= new_str != *str;
352 case CS_NUMERAL:
return (key >=
'0' && key <=
'9');
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();
361 #if defined(_MSC_VER) && _MSC_VER < 1900 369 int CDECL vsnprintf(
char *str,
size_t size,
const char *format, va_list ap)
371 if (size == 0)
return 0;
374 int ret = _vsnprintf(str, size, format, ap);
377 if (errno != ERANGE) {
382 }
else if ((
size_t)ret < size) {
391 str[size - 1] =
'\0';
407 int CDECL
seprintf(
char *str,
const char *last,
const char *format, ...)
411 va_start(ap, format);
412 int ret =
vseprintf(str, last, format, ap);
429 for (uint i = 0; i < 16; i++) {
430 p +=
seprintf(p, last,
"%02X", md5sum[i]);
448 assert(c !=
nullptr);
454 }
else if (
GB(s[0], 5, 3) == 6) {
455 if (IsUtf8Part(s[1])) {
457 *c =
GB(s[0], 0, 5) << 6 |
GB(s[1], 0, 6);
458 if (*c >= 0x80)
return 2;
460 }
else if (
GB(s[0], 4, 4) == 14) {
461 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
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;
466 }
else if (
GB(s[0], 3, 5) == 30) {
467 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
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;
491 }
else if (c < 0x800) {
492 *buf++ = 0xC0 +
GB(c, 6, 5);
493 *buf = 0x80 +
GB(c, 0, 6);
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);
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);
524 for (
const char *ptr = strchr(s,
'\0'); *s !=
'\0';) {
527 if (len == 0) len = 1;
531 if (length + len >= maxlen || (s + len > ptr))
break;
540 #ifdef DEFINE_STRCASESTR 541 char *strcasestr(
const char *haystack,
const char *needle)
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);
578 int strnatcmp(
const char *s1,
const char *s2,
bool ignore_garbage_at_front)
580 if (ignore_garbage_at_front) {
587 UErrorCode status = U_ZERO_ERROR;
589 if (U_SUCCESS(status))
return result;
593 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN) 594 int res = OTTDStringCompare(s1, s2);
595 if (res != 0)
return res - 2;
598 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) 600 if (res != 0)
return res - 2;
604 return strcasecmp(s1, s2);
607 #ifdef WITH_UNISCRIBE 611 return new UniscribeStringIterator();
614 #elif defined(WITH_ICU_I18N) 616 #include <unicode/utext.h> 617 #include <unicode/brkiter.h> 631 UErrorCode status = U_ZERO_ERROR;
635 this->utf16_str.push_back(
'\0');
636 this->utf16_to_utf8.push_back(0);
641 delete this->char_itr;
642 delete this->word_itr;
647 const char *string_base = s;
653 this->utf16_str.clear();
654 this->utf16_to_utf8.clear();
657 size_t idx = s - string_base;
659 WChar c = Utf8Consume(&s);
661 this->utf16_str.push_back((UChar)c);
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);
668 this->utf16_to_utf8.push_back(idx);
670 this->utf16_str.push_back(
'\0');
671 this->utf16_to_utf8.push_back(s - string_base);
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();
686 for (uint i = 0; i < this->utf16_to_utf8.size(); i++) {
687 if (this->utf16_to_utf8[i] == pos) {
696 this->char_itr->isBoundary(utf16_pos);
697 return this->utf16_to_utf8[this->char_itr->current()];
705 pos = this->char_itr->next();
709 pos = this->word_itr->following(this->char_itr->current());
713 while (pos != icu::BreakIterator::DONE &&
715 int32_t new_pos = this->word_itr->next();
718 if (new_pos == icu::BreakIterator::DONE)
break;
722 this->char_itr->isBoundary(pos);
729 return pos == icu::BreakIterator::DONE ?
END : this->utf16_to_utf8[pos];
737 pos = this->char_itr->previous();
741 pos = this->word_itr->preceding(this->char_itr->current());
745 while (pos != icu::BreakIterator::DONE &&
747 int32_t new_pos = this->word_itr->previous();
750 if (new_pos == icu::BreakIterator::DONE)
break;
754 this->char_itr->isBoundary(pos);
761 return pos == icu::BreakIterator::DONE ?
END : this->utf16_to_utf8[pos];
780 DefaultStringIterator() : string(
nullptr), len(0), cur_pos(0)
787 this->len = strlen(s);
793 assert(this->
string !=
nullptr && pos <= this->len);
795 while (pos > 0 && IsUtf8Part(this->
string[pos])) pos--;
796 return this->cur_pos = pos;
801 assert(this->
string !=
nullptr);
804 if (this->cur_pos >= this->len)
return END;
809 this->cur_pos +=
Utf8Decode(&c, this->
string + this->cur_pos);
810 return this->cur_pos;
816 size_t offs =
Utf8Decode(&c, this->
string + this->cur_pos);
818 this->cur_pos += offs;
819 offs =
Utf8Decode(&c, this->
string + this->cur_pos);
823 this->cur_pos += offs;
824 offs =
Utf8Decode(&c, this->
string + this->cur_pos);
827 return this->cur_pos;
839 assert(this->
string !=
nullptr);
842 if (this->cur_pos == 0)
return END;
846 return this->cur_pos =
Utf8PrevChar(this->
string + this->cur_pos) - this->string;
849 const char *s = this->
string + this->cur_pos;
864 return this->cur_pos = s - this->string;
875 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) 879 if (i !=
nullptr)
return i;
881 return new DefaultStringIterator();
886 return new DefaultStringIterator();
Functions related to laying out text on Win32.
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
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:
Only hexadecimal characters.
static const size_t END
Sentinel to indicate end-of-iteration.
Functions related to debugging.
static StringIterator * Create()
Create a new iterator instance.
icu::BreakIterator * word_itr
ICU iterator for words.
int MacOSStringCompare(const char *s1, const char *s2)
Compares two strings using case insensitive natural sort.
fluid_settings_t * settings
FluidSynth settings handle.
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
const LanguageMetadata * _current_language
The currently loaded language.
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
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.
std::vector< size_t > utf16_to_utf8
Mapping from UTF-16 code point position to index in the UTF-8 source string.
icu::BreakIterator * char_itr
ICU iterator for characters.
size_t SetCurPosition(size_t pos) override
Change the current string cursor.
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
#define lastof(x)
Get the last element of an fixed size array.
virtual void SetString(const char *s)=0
Set a new iteration string.
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
size_t Next(IterType what) override
Advance the cursor by one iteration unit.
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.
Iterate over characters (or more exactly grapheme clusters).
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
static int8 Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Functions related to low-level strings.
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
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...
static WChar Utf16DecodeChar(const uint16 *c)
Decode an UTF-16 character.
Functions related to the allocation of memory.
size_t Utf8TrimString(char *s, size_t maxlen)
Properly terminate an UTF8 string to some maximum length.
Functions related to the gfx engine.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
IterType
Type of the iterator.
Functions related to localized text support on OSX.
Information about languages and their files.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
static T min(const T a, const T b)
Returns the minimum of two values.
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
CharSetFilter
Valid filter types for IsValidChar.
#define DEBUG(name, level,...)
Output a line of debugging information.
std::vector< UChar > utf16_str
UTF-16 copy of the string.
Both numeric and alphabetic and spaces and stuff.
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
static size_t ttd_strnlen(const char *str, size_t maxlen)
Get the length of a string, within a limited buffer.
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...
Replace the unknown/bad bits with question marks.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
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.
static char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
Class for iterating over different kind of parts of a string.
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.
Allow the special control codes.
size_t Prev(IterType what) override
Move the cursor back by one iteration unit.
size_t Utf8Encode(char *buf, WChar c)
Encode a unicode character and place it in the buffer.
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...
String iterator using ICU as a backend.
uint32 WChar
Type for wide characters, i.e.
declarations of functions for MS windows systems
icu::Collator * _current_collator
Collator for the language currently in use.
bool StrValid(const char *str, const char *last)
Checks whether the given string is valid, i.e.