string.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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       /* In case of these two special cases assume that they really
00253        * mean SETX/SETXY and also "eat" the paramater. If this was
00254        * not the case the string was broken to begin with and this
00255        * would not break much more. */
00256       if (c == SCC_SETX) {
00257         str++;
00258       } else if (c == SCC_SETXY) {
00259         str += 2;
00260       }
00261     }
00262   }
00263 
00264   *dst = '\0';
00265 }
00266 
00274 bool StrValid(const char *str, const char *last)
00275 {
00276   /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
00277 
00278   while (str <= last && *str != '\0') {
00279     size_t len = Utf8EncodedCharLen(*str);
00280     /* Encoded length is 0 if the character isn't known.
00281      * The length check is needed to prevent Utf8Decode to read
00282      * over the terminating '\0' if that happens to be placed
00283      * within the encoding of an UTF8 character. */
00284     if (len == 0 || str + len > last) return false;
00285 
00286     WChar c;
00287     len = Utf8Decode(&c, str);
00288     if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
00289       return false;
00290     }
00291 
00292     str += len;
00293   }
00294 
00295   return *str == '\0';
00296 }
00297 
00299 void str_strip_colours(char *str)
00300 {
00301   char *dst = str;
00302   WChar c;
00303   size_t len;
00304 
00305   for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
00306     if (c < SCC_BLUE || c > SCC_BLACK) {
00307       /* Copy the character back. Even if dst is current the same as str
00308        * (i.e. no characters have been changed) this is quicker than
00309        * moving the pointers ahead by len */
00310       do {
00311         *dst++ = *str++;
00312       } while (--len != 0);
00313     } else {
00314       /* Just skip (strip) the colour codes */
00315       str += len;
00316     }
00317   }
00318   *dst = '\0';
00319 }
00320 
00327 size_t Utf8StringLength(const char *s)
00328 {
00329   size_t len = 0;
00330   const char *t = s;
00331   while (Utf8Consume(&t) != 0) len++;
00332   return len;
00333 }
00334 
00335 
00347 bool strtolower(char *str)
00348 {
00349   bool changed = false;
00350   for (; *str != '\0'; str++) {
00351     char new_str = tolower(*str);
00352     changed |= new_str != *str;
00353     *str = new_str;
00354   }
00355   return changed;
00356 }
00357 
00365 bool IsValidChar(WChar key, CharSetFilter afilter)
00366 {
00367   switch (afilter) {
00368     case CS_ALPHANUMERAL:  return IsPrintable(key);
00369     case CS_NUMERAL:       return (key >= '0' && key <= '9');
00370     case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
00371     case CS_ALPHA:         return IsPrintable(key) && !(key >= '0' && key <= '9');
00372     case CS_HEXADECIMAL:   return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
00373   }
00374 
00375   return false;
00376 }
00377 
00378 #ifdef WIN32
00379 /* Since version 3.14, MinGW Runtime has snprintf() and vsnprintf() conform to C99 but it's not the case for older versions */
00380 #if (__MINGW32_MAJOR_VERSION < 3) || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION < 14))
00381 int CDECL snprintf(char *str, size_t size, const char *format, ...)
00382 {
00383   va_list ap;
00384   int ret;
00385 
00386   va_start(ap, format);
00387   ret = vsnprintf(str, size, format, ap);
00388   va_end(ap);
00389   return ret;
00390 }
00391 #endif /* MinGW Runtime < 3.14 */
00392 
00393 #ifdef _MSC_VER
00394 
00401 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
00402 {
00403   if (size == 0) return 0;
00404 
00405   errno = 0;
00406   int ret = _vsnprintf(str, size, format, ap);
00407 
00408   if (ret < 0) {
00409     if (errno != ERANGE) {
00410       /* There's a formatting error, better get that looked
00411        * at properly instead of ignoring it. */
00412       NOT_REACHED();
00413     }
00414   } else if ((size_t)ret < size) {
00415     /* The buffer is big enough for the number of
00416      * characers stored (excluding null), i.e.
00417      * the string has been null-terminated. */
00418     return ret;
00419   }
00420 
00421   /* The buffer is too small for _vsnprintf to write the
00422    * null-terminator at its end and return size. */
00423   str[size - 1] = '\0';
00424   return (int)size;
00425 }
00426 #endif /* _MSC_VER */
00427 
00428 #endif /* WIN32 */
00429 
00439 int CDECL seprintf(char *str, const char *last, const char *format, ...)
00440 {
00441   va_list ap;
00442 
00443   va_start(ap, format);
00444   int ret = vseprintf(str, last, format, ap);
00445   va_end(ap);
00446   return ret;
00447 }
00448 
00449 
00457 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
00458 {
00459   char *p = buf;
00460 
00461   for (uint i = 0; i < 16; i++) {
00462     p += seprintf(p, last, "%02X", md5sum[i]);
00463   }
00464 
00465   return p;
00466 }
00467 
00468 
00469 /* UTF-8 handling routines */
00470 
00471 
00478 size_t Utf8Decode(WChar *c, const char *s)
00479 {
00480   assert(c != NULL);
00481 
00482   if (!HasBit(s[0], 7)) {
00483     /* Single byte character: 0xxxxxxx */
00484     *c = s[0];
00485     return 1;
00486   } else if (GB(s[0], 5, 3) == 6) {
00487     if (IsUtf8Part(s[1])) {
00488       /* Double byte character: 110xxxxx 10xxxxxx */
00489       *c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
00490       if (*c >= 0x80) return 2;
00491     }
00492   } else if (GB(s[0], 4, 4) == 14) {
00493     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
00494       /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
00495       *c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
00496       if (*c >= 0x800) return 3;
00497     }
00498   } else if (GB(s[0], 3, 5) == 30) {
00499     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
00500       /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
00501       *c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
00502       if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
00503     }
00504   }
00505 
00506   /* DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence"); */
00507   *c = '?';
00508   return 1;
00509 }
00510 
00511 
00518 size_t Utf8Encode(char *buf, WChar c)
00519 {
00520   if (c < 0x80) {
00521     *buf = c;
00522     return 1;
00523   } else if (c < 0x800) {
00524     *buf++ = 0xC0 + GB(c,  6, 5);
00525     *buf   = 0x80 + GB(c,  0, 6);
00526     return 2;
00527   } else if (c < 0x10000) {
00528     *buf++ = 0xE0 + GB(c, 12, 4);
00529     *buf++ = 0x80 + GB(c,  6, 6);
00530     *buf   = 0x80 + GB(c,  0, 6);
00531     return 3;
00532   } else if (c < 0x110000) {
00533     *buf++ = 0xF0 + GB(c, 18, 3);
00534     *buf++ = 0x80 + GB(c, 12, 6);
00535     *buf++ = 0x80 + GB(c,  6, 6);
00536     *buf   = 0x80 + GB(c,  0, 6);
00537     return 4;
00538   }
00539 
00540   /* DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c); */
00541   *buf = '?';
00542   return 1;
00543 }
00544 
00552 size_t Utf8TrimString(char *s, size_t maxlen)
00553 {
00554   size_t length = 0;
00555 
00556   for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
00557     size_t len = Utf8EncodedCharLen(*s);
00558     /* Silently ignore invalid UTF8 sequences, our only concern trimming */
00559     if (len == 0) len = 1;
00560 
00561     /* Take care when a hard cutoff was made for the string and
00562      * the last UTF8 sequence is invalid */
00563     if (length + len >= maxlen || (s + len > ptr)) break;
00564     s += len;
00565     length += len;
00566   }
00567 
00568   *s = '\0';
00569   return length;
00570 }
00571 
00572 #ifdef DEFINE_STRNDUP
00573 #include "core/math_func.hpp"
00574 char *strndup(const char *s, size_t len)
00575 {
00576   len = min(strlen(s), len);
00577   char *tmp = CallocT<char>(len + 1);
00578   memcpy(tmp, s, len);
00579   return tmp;
00580 }
00581 #endif /* DEFINE_STRNDUP */
00582 
00583 #ifdef DEFINE_STRCASESTR
00584 char *strcasestr(const char *haystack, const char *needle)
00585 {
00586   size_t hay_len = strlen(haystack);
00587   size_t needle_len = strlen(needle);
00588   while (hay_len >= needle_len) {
00589     if (strncasecmp(haystack, needle, needle_len) == 0) return const_cast<char *>(haystack);
00590 
00591     haystack++;
00592     hay_len--;
00593   }
00594 
00595   return NULL;
00596 }
00597 #endif /* DEFINE_STRCASESTR */
00598 
00606 int strnatcmp(const char *s1, const char *s2)
00607 {
00608 #ifdef WITH_ICU
00609   if (_current_collator != NULL) {
00610     UErrorCode status = U_ZERO_ERROR;
00611     int result;
00612 
00613     /* We want to use the new faster method for ICU 4.2 and higher. */
00614 #if U_ICU_VERSION_MAJOR_NUM > 4 || (U_ICU_VERSION_MAJOR_NUM == 4 && U_ICU_VERSION_MINOR_NUM >= 2)
00615     /* The StringPiece parameter gets implicitly constructed from the char *. */
00616     result = _current_collator->compareUTF8(s1, s2, status);
00617 #else /* The following for 4.0 and lower. */
00618     UChar buffer1[DRAW_STRING_BUFFER];
00619     u_strFromUTF8Lenient(buffer1, lengthof(buffer1), NULL, s1, -1, &status);
00620     UChar buffer2[DRAW_STRING_BUFFER];
00621     u_strFromUTF8Lenient(buffer2, lengthof(buffer2), NULL, s2, -1, &status);
00622 
00623     result = _current_collator->compare(buffer1, buffer2, status);
00624 #endif /* ICU version check. */
00625     if (U_SUCCESS(status)) return result;
00626   }
00627 
00628 #endif /* WITH_ICU */
00629 
00630   /* Do a normal comparison if ICU is missing or if we cannot create a collator. */
00631   return strcasecmp(s1, s2);
00632 }