string.cpp

Go to the documentation of this file.
00001 /* $Id: string.cpp 25528 2013-06-30 08:58:35Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "core/alloc_func.hpp"
00015 #include "core/math_func.hpp"
00016 #include "string_func.h"
00017 
00018 #include "table/control_codes.h"
00019 
00020 #include <stdarg.h>
00021 #include <ctype.h> /* required for tolower() */
00022 
00023 #ifdef _MSC_VER
00024 #include <errno.h> // required by vsnprintf implementation for MSVC
00025 #endif
00026 
00027 #ifdef WITH_ICU
00028 /* Required by strnatcmp. */
00029 #include <unicode/ustring.h>
00030 #include "language.h"
00031 #include "gfx_func.h"
00032 #endif /* WITH_ICU */
00033 
00044 static int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
00045 {
00046   ptrdiff_t diff = last - str;
00047   if (diff < 0) return 0;
00048   return min((int)diff, vsnprintf(str, diff + 1, format, ap));
00049 }
00050 
00065 void ttd_strlcat(char *dst, const char *src, size_t size)
00066 {
00067   assert(size > 0);
00068   while (size > 0 && *dst != '\0') {
00069     size--;
00070     dst++;
00071   }
00072 
00073   ttd_strlcpy(dst, src, size);
00074 }
00075 
00076 
00091 void ttd_strlcpy(char *dst, const char *src, size_t size)
00092 {
00093   assert(size > 0);
00094   while (--size > 0 && *src != '\0') {
00095     *dst++ = *src++;
00096   }
00097   *dst = '\0';
00098 }
00099 
00100 
00117 char *strecat(char *dst, const char *src, const char *last)
00118 {
00119   assert(dst <= last);
00120   while (*dst != '\0') {
00121     if (dst == last) return dst;
00122     dst++;
00123   }
00124 
00125   return strecpy(dst, src, last);
00126 }
00127 
00128 
00145 char *strecpy(char *dst, const char *src, const char *last)
00146 {
00147   assert(dst <= last);
00148   while (dst != last && *src != '\0') {
00149     *dst++ = *src++;
00150   }
00151   *dst = '\0';
00152 
00153   if (dst == last && *src != '\0') {
00154 #if defined(STRGEN) || defined(SETTINGSGEN)
00155     error("String too long for destination buffer");
00156 #else /* STRGEN || SETTINGSGEN */
00157     DEBUG(misc, 0, "String too long for destination buffer");
00158 #endif /* STRGEN || SETTINGSGEN */
00159   }
00160   return dst;
00161 }
00162 
00168 char *CDECL str_fmt(const char *str, ...)
00169 {
00170   char buf[4096];
00171   va_list va;
00172 
00173   va_start(va, str);
00174   int len = vseprintf(buf, lastof(buf), str, va);
00175   va_end(va);
00176   char *p = MallocT<char>(len + 1);
00177   memcpy(p, buf, len + 1);
00178   return p;
00179 }
00180 
00187 void str_fix_scc_encoded(char *str, const char *last)
00188 {
00189   while (str <= last && *str != '\0') {
00190     size_t len = Utf8EncodedCharLen(*str);
00191     if ((len == 0 && str + 4 > last) || str + len > last) break;
00192 
00193     WChar c;
00194     len = Utf8Decode(&c, str);
00195     if (c == '\0') break;
00196 
00197     if (c == 0xE028 || c == 0xE02A) {
00198       c = SCC_ENCODED;
00199     }
00200     str += Utf8Encode(str, c);
00201   }
00202   *str = '\0';
00203 }
00204 
00205 
00213 void str_validate(char *str, const char *last, StringValidationSettings settings)
00214 {
00215   /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
00216 
00217   char *dst = str;
00218   while (str <= last && *str != '\0') {
00219     size_t len = Utf8EncodedCharLen(*str);
00220     /* If the character is unknown, i.e. encoded length is 0
00221      * we assume worst case for the length check.
00222      * The length check is needed to prevent Utf8Decode to read
00223      * over the terminating '\0' if that happens to be placed
00224      * within the encoding of an UTF8 character. */
00225     if ((len == 0 && str + 4 > last) || str + len > last) break;
00226 
00227     WChar c;
00228     len = Utf8Decode(&c, str);
00229     /* It's possible to encode the string termination character
00230      * into a multiple bytes. This prevents those termination
00231      * characters to be skipped */
00232     if (c == '\0') break;
00233 
00234     if ((IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END)) || ((settings & SVS_ALLOW_CONTROL_CODE) != 0 && c == SCC_ENCODED)) {
00235       /* Copy the character back. Even if dst is current the same as str
00236        * (i.e. no characters have been changed) this is quicker than
00237        * moving the pointers ahead by len */
00238       do {
00239         *dst++ = *str++;
00240       } while (--len != 0);
00241     } else if ((settings & SVS_ALLOW_NEWLINE) != 0  && c == '\n') {
00242       *dst++ = *str++;
00243     } else {
00244       if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') {
00245         str += len;
00246         continue;
00247       }
00248       /* Replace the undesirable character with a question mark */
00249       str += len;
00250       if ((settings & SVS_REPLACE_WITH_QUESTION_MARK) != 0) *dst++ = '?';
00251     }
00252   }
00253 
00254   *dst = '\0';
00255 }
00256 
00264 bool StrValid(const char *str, const char *last)
00265 {
00266   /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
00267 
00268   while (str <= last && *str != '\0') {
00269     size_t len = Utf8EncodedCharLen(*str);
00270     /* Encoded length is 0 if the character isn't known.
00271      * The length check is needed to prevent Utf8Decode to read
00272      * over the terminating '\0' if that happens to be placed
00273      * within the encoding of an UTF8 character. */
00274     if (len == 0 || str + len > last) return false;
00275 
00276     WChar c;
00277     len = Utf8Decode(&c, str);
00278     if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
00279       return false;
00280     }
00281 
00282     str += len;
00283   }
00284 
00285   return *str == '\0';
00286 }
00287 
00289 void str_strip_colours(char *str)
00290 {
00291   char *dst = str;
00292   WChar c;
00293   size_t len;
00294 
00295   for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
00296     if (c < SCC_BLUE || c > SCC_BLACK) {
00297       /* Copy the character back. Even if dst is current the same as str
00298        * (i.e. no characters have been changed) this is quicker than
00299        * moving the pointers ahead by len */
00300       do {
00301         *dst++ = *str++;
00302       } while (--len != 0);
00303     } else {
00304       /* Just skip (strip) the colour codes */
00305       str += len;
00306     }
00307   }
00308   *dst = '\0';
00309 }
00310 
00317 size_t Utf8StringLength(const char *s)
00318 {
00319   size_t len = 0;
00320   const char *t = s;
00321   while (Utf8Consume(&t) != 0) len++;
00322   return len;
00323 }
00324 
00325 
00337 bool strtolower(char *str)
00338 {
00339   bool changed = false;
00340   for (; *str != '\0'; str++) {
00341     char new_str = tolower(*str);
00342     changed |= new_str != *str;
00343     *str = new_str;
00344   }
00345   return changed;
00346 }
00347 
00355 bool IsValidChar(WChar key, CharSetFilter afilter)
00356 {
00357   switch (afilter) {
00358     case CS_ALPHANUMERAL:  return IsPrintable(key);
00359     case CS_NUMERAL:       return (key >= '0' && key <= '9');
00360     case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
00361     case CS_ALPHA:         return IsPrintable(key) && !(key >= '0' && key <= '9');
00362     case CS_HEXADECIMAL:   return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
00363   }
00364 
00365   return false;
00366 }
00367 
00368 #ifdef WIN32
00369 /* Since version 3.14, MinGW Runtime has snprintf() and vsnprintf() conform to C99 but it's not the case for older versions */
00370 #if (__MINGW32_MAJOR_VERSION < 3) || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION < 14))
00371 int CDECL snprintf(char *str, size_t size, const char *format, ...)
00372 {
00373   va_list ap;
00374   int ret;
00375 
00376   va_start(ap, format);
00377   ret = vsnprintf(str, size, format, ap);
00378   va_end(ap);
00379   return ret;
00380 }
00381 #endif /* MinGW Runtime < 3.14 */
00382 
00383 #ifdef _MSC_VER
00384 
00391 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
00392 {
00393   if (size == 0) return 0;
00394 
00395   errno = 0;
00396   int ret = _vsnprintf(str, size, format, ap);
00397 
00398   if (ret < 0) {
00399     if (errno != ERANGE) {
00400       /* There's a formatting error, better get that looked
00401        * at properly instead of ignoring it. */
00402       NOT_REACHED();
00403     }
00404   } else if ((size_t)ret < size) {
00405     /* The buffer is big enough for the number of
00406      * characters stored (excluding null), i.e.
00407      * the string has been null-terminated. */
00408     return ret;
00409   }
00410 
00411   /* The buffer is too small for _vsnprintf to write the
00412    * null-terminator at its end and return size. */
00413   str[size - 1] = '\0';
00414   return (int)size;
00415 }
00416 #endif /* _MSC_VER */
00417 
00418 #endif /* WIN32 */
00419 
00429 int CDECL seprintf(char *str, const char *last, const char *format, ...)
00430 {
00431   va_list ap;
00432 
00433   va_start(ap, format);
00434   int ret = vseprintf(str, last, format, ap);
00435   va_end(ap);
00436   return ret;
00437 }
00438 
00439 
00447 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
00448 {
00449   char *p = buf;
00450 
00451   for (uint i = 0; i < 16; i++) {
00452     p += seprintf(p, last, "%02X", md5sum[i]);
00453   }
00454 
00455   return p;
00456 }
00457 
00458 
00459 /* UTF-8 handling routines */
00460 
00461 
00468 size_t Utf8Decode(WChar *c, const char *s)
00469 {
00470   assert(c != NULL);
00471 
00472   if (!HasBit(s[0], 7)) {
00473     /* Single byte character: 0xxxxxxx */
00474     *c = s[0];
00475     return 1;
00476   } else if (GB(s[0], 5, 3) == 6) {
00477     if (IsUtf8Part(s[1])) {
00478       /* Double byte character: 110xxxxx 10xxxxxx */
00479       *c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
00480       if (*c >= 0x80) return 2;
00481     }
00482   } else if (GB(s[0], 4, 4) == 14) {
00483     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
00484       /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
00485       *c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
00486       if (*c >= 0x800) return 3;
00487     }
00488   } else if (GB(s[0], 3, 5) == 30) {
00489     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
00490       /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
00491       *c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
00492       if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
00493     }
00494   }
00495 
00496   /* DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence"); */
00497   *c = '?';
00498   return 1;
00499 }
00500 
00501 
00508 size_t Utf8Encode(char *buf, WChar c)
00509 {
00510   if (c < 0x80) {
00511     *buf = c;
00512     return 1;
00513   } else if (c < 0x800) {
00514     *buf++ = 0xC0 + GB(c,  6, 5);
00515     *buf   = 0x80 + GB(c,  0, 6);
00516     return 2;
00517   } else if (c < 0x10000) {
00518     *buf++ = 0xE0 + GB(c, 12, 4);
00519     *buf++ = 0x80 + GB(c,  6, 6);
00520     *buf   = 0x80 + GB(c,  0, 6);
00521     return 3;
00522   } else if (c < 0x110000) {
00523     *buf++ = 0xF0 + GB(c, 18, 3);
00524     *buf++ = 0x80 + GB(c, 12, 6);
00525     *buf++ = 0x80 + GB(c,  6, 6);
00526     *buf   = 0x80 + GB(c,  0, 6);
00527     return 4;
00528   }
00529 
00530   /* DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c); */
00531   *buf = '?';
00532   return 1;
00533 }
00534 
00542 size_t Utf8TrimString(char *s, size_t maxlen)
00543 {
00544   size_t length = 0;
00545 
00546   for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
00547     size_t len = Utf8EncodedCharLen(*s);
00548     /* Silently ignore invalid UTF8 sequences, our only concern trimming */
00549     if (len == 0) len = 1;
00550 
00551     /* Take care when a hard cutoff was made for the string and
00552      * the last UTF8 sequence is invalid */
00553     if (length + len >= maxlen || (s + len > ptr)) break;
00554     s += len;
00555     length += len;
00556   }
00557 
00558   *s = '\0';
00559   return length;
00560 }
00561 
00562 #ifdef DEFINE_STRNDUP
00563 char *strndup(const char *s, size_t len)
00564 {
00565   len = ttd_strnlen(s, len);
00566   char *tmp = CallocT<char>(len + 1);
00567   memcpy(tmp, s, len);
00568   return tmp;
00569 }
00570 #endif /* DEFINE_STRNDUP */
00571 
00572 #ifdef DEFINE_STRCASESTR
00573 char *strcasestr(const char *haystack, const char *needle)
00574 {
00575   size_t hay_len = strlen(haystack);
00576   size_t needle_len = strlen(needle);
00577   while (hay_len >= needle_len) {
00578     if (strncasecmp(haystack, needle, needle_len) == 0) return const_cast<char *>(haystack);
00579 
00580     haystack++;
00581     hay_len--;
00582   }
00583 
00584   return NULL;
00585 }
00586 #endif /* DEFINE_STRCASESTR */
00587 
00596 static const char *SkipGarbage(const char *str)
00597 {
00598   while (*str != '\0' && (*str < 'A' || IsInsideMM(*str, '[', '`' + 1) || IsInsideMM(*str, '{', '~' + 1))) str++;
00599   return str;
00600 }
00601 
00610 int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
00611 {
00612   if (ignore_garbage_at_front) {
00613     s1 = SkipGarbage(s1);
00614     s2 = SkipGarbage(s2);
00615   }
00616 #ifdef WITH_ICU
00617   if (_current_collator != NULL) {
00618     UErrorCode status = U_ZERO_ERROR;
00619     int result;
00620 
00621     /* We want to use the new faster method for ICU 4.2 and higher. */
00622 #if U_ICU_VERSION_MAJOR_NUM > 4 || (U_ICU_VERSION_MAJOR_NUM == 4 && U_ICU_VERSION_MINOR_NUM >= 2)
00623     /* The StringPiece parameter gets implicitly constructed from the char *. */
00624     result = _current_collator->compareUTF8(s1, s2, status);
00625 #else /* The following for 4.0 and lower. */
00626     UChar buffer1[DRAW_STRING_BUFFER];
00627     u_strFromUTF8Lenient(buffer1, lengthof(buffer1), NULL, s1, -1, &status);
00628     UChar buffer2[DRAW_STRING_BUFFER];
00629     u_strFromUTF8Lenient(buffer2, lengthof(buffer2), NULL, s2, -1, &status);
00630 
00631     result = _current_collator->compare(buffer1, buffer2, status);
00632 #endif /* ICU version check. */
00633     if (U_SUCCESS(status)) return result;
00634   }
00635 
00636 #endif /* WITH_ICU */
00637 
00638   /* Do a normal comparison if ICU is missing or if we cannot create a collator. */
00639   return strcasecmp(s1, s2);
00640 }