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 
00181 
00190 void str_validate(char *str, const char *last, bool allow_newlines, bool ignore)
00191 {
00192   /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
00193 
00194   char *dst = str;
00195   while (str <= last && *str != '\0') {
00196     size_t len = Utf8EncodedCharLen(*str);
00197     /* If the character is unknown, i.e. encoded length is 0
00198      * we assume worst case for the length check.
00199      * The length check is needed to prevent Utf8Decode to read
00200      * over the terminating '\0' if that happens to be placed
00201      * within the encoding of an UTF8 character. */
00202     if ((len == 0 && str + 4 > last) || str + len > last) break;
00203 
00204     WChar c;
00205     len = Utf8Decode(&c, str);
00206     /* It's possible to encode the string termination character
00207      * into a multiple bytes. This prevents those termination
00208      * characters to be skipped */
00209     if (c == '\0') break;
00210 
00211     if (IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END)) {
00212       /* Copy the character back. Even if dst is current the same as str
00213        * (i.e. no characters have been changed) this is quicker than
00214        * moving the pointers ahead by len */
00215       do {
00216         *dst++ = *str++;
00217       } while (--len != 0);
00218     } else if (allow_newlines && c == '\n') {
00219       *dst++ = *str++;
00220     } else {
00221       if (allow_newlines && c == '\r' && str[1] == '\n') {
00222         str += len;
00223         continue;
00224       }
00225       /* Replace the undesirable character with a question mark */
00226       str += len;
00227       if (!ignore) *dst++ = '?';
00228 
00229       /* In case of these two special cases assume that they really
00230        * mean SETX/SETXY and also "eat" the paramater. If this was
00231        * not the case the string was broken to begin with and this
00232        * would not break much more. */
00233       if (c == SCC_SETX) {
00234         str++;
00235       } else if (c == SCC_SETXY) {
00236         str += 2;
00237       }
00238     }
00239   }
00240 
00241   *dst = '\0';
00242 }
00243 
00251 bool StrValid(const char *str, const char *last)
00252 {
00253   /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
00254 
00255   while (str <= last && *str != '\0') {
00256     size_t len = Utf8EncodedCharLen(*str);
00257     /* Encoded length is 0 if the character isn't known.
00258      * The length check is needed to prevent Utf8Decode to read
00259      * over the terminating '\0' if that happens to be placed
00260      * within the encoding of an UTF8 character. */
00261     if (len == 0 || str + len > last) return false;
00262 
00263     WChar c;
00264     len = Utf8Decode(&c, str);
00265     if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
00266       return false;
00267     }
00268 
00269     str += len;
00270   }
00271 
00272   return *str == '\0';
00273 }
00274 
00276 void str_strip_colours(char *str)
00277 {
00278   char *dst = str;
00279   WChar c;
00280   size_t len;
00281 
00282   for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
00283     if (c < SCC_BLUE || c > SCC_BLACK) {
00284       /* Copy the character back. Even if dst is current the same as str
00285        * (i.e. no characters have been changed) this is quicker than
00286        * moving the pointers ahead by len */
00287       do {
00288         *dst++ = *str++;
00289       } while (--len != 0);
00290     } else {
00291       /* Just skip (strip) the colour codes */
00292       str += len;
00293     }
00294   }
00295   *dst = '\0';
00296 }
00297 
00304 size_t Utf8StringLength(const char *s)
00305 {
00306   size_t len = 0;
00307   const char *t = s;
00308   while (Utf8Consume(&t) != 0) len++;
00309   return len;
00310 }
00311 
00312 
00324 bool strtolower(char *str)
00325 {
00326   bool changed = false;
00327   for (; *str != '\0'; str++) {
00328     char new_str = tolower(*str);
00329     changed |= new_str != *str;
00330     *str = new_str;
00331   }
00332   return changed;
00333 }
00334 
00342 bool IsValidChar(WChar key, CharSetFilter afilter)
00343 {
00344   switch (afilter) {
00345     case CS_ALPHANUMERAL:  return IsPrintable(key);
00346     case CS_NUMERAL:       return (key >= '0' && key <= '9');
00347     case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
00348     case CS_ALPHA:         return IsPrintable(key) && !(key >= '0' && key <= '9');
00349     case CS_HEXADECIMAL:   return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
00350   }
00351 
00352   return false;
00353 }
00354 
00355 #ifdef WIN32
00356 /* Since version 3.14, MinGW Runtime has snprintf() and vsnprintf() conform to C99 but it's not the case for older versions */
00357 #if (__MINGW32_MAJOR_VERSION < 3) || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION < 14))
00358 int CDECL snprintf(char *str, size_t size, const char *format, ...)
00359 {
00360   va_list ap;
00361   int ret;
00362 
00363   va_start(ap, format);
00364   ret = vsnprintf(str, size, format, ap);
00365   va_end(ap);
00366   return ret;
00367 }
00368 #endif /* MinGW Runtime < 3.14 */
00369 
00370 #ifdef _MSC_VER
00371 
00378 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
00379 {
00380   if (size == 0) return 0;
00381 
00382   errno = 0;
00383   int ret = _vsnprintf(str, size, format, ap);
00384 
00385   if (ret < 0) {
00386     if (errno != ERANGE) {
00387       /* There's a formatting error, better get that looked
00388        * at properly instead of ignoring it. */
00389       NOT_REACHED();
00390     }
00391   } else if ((size_t)ret < size) {
00392     /* The buffer is big enough for the number of
00393      * characers stored (excluding null), i.e.
00394      * the string has been null-terminated. */
00395     return ret;
00396   }
00397 
00398   /* The buffer is too small for _vsnprintf to write the
00399    * null-terminator at its end and return size. */
00400   str[size - 1] = '\0';
00401   return (int)size;
00402 }
00403 #endif /* _MSC_VER */
00404 
00405 #endif /* WIN32 */
00406 
00416 int CDECL seprintf(char *str, const char *last, const char *format, ...)
00417 {
00418   va_list ap;
00419 
00420   va_start(ap, format);
00421   int ret = vseprintf(str, last, format, ap);
00422   va_end(ap);
00423   return ret;
00424 }
00425 
00426 
00434 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
00435 {
00436   char *p = buf;
00437 
00438   for (uint i = 0; i < 16; i++) {
00439     p += seprintf(p, last, "%02X", md5sum[i]);
00440   }
00441 
00442   return p;
00443 }
00444 
00445 
00446 /* UTF-8 handling routines */
00447 
00448 
00455 size_t Utf8Decode(WChar *c, const char *s)
00456 {
00457   assert(c != NULL);
00458 
00459   if (!HasBit(s[0], 7)) {
00460     /* Single byte character: 0xxxxxxx */
00461     *c = s[0];
00462     return 1;
00463   } else if (GB(s[0], 5, 3) == 6) {
00464     if (IsUtf8Part(s[1])) {
00465       /* Double byte character: 110xxxxx 10xxxxxx */
00466       *c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
00467       if (*c >= 0x80) return 2;
00468     }
00469   } else if (GB(s[0], 4, 4) == 14) {
00470     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
00471       /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
00472       *c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
00473       if (*c >= 0x800) return 3;
00474     }
00475   } else if (GB(s[0], 3, 5) == 30) {
00476     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
00477       /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
00478       *c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
00479       if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
00480     }
00481   }
00482 
00483   /* DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence"); */
00484   *c = '?';
00485   return 1;
00486 }
00487 
00488 
00495 size_t Utf8Encode(char *buf, WChar c)
00496 {
00497   if (c < 0x80) {
00498     *buf = c;
00499     return 1;
00500   } else if (c < 0x800) {
00501     *buf++ = 0xC0 + GB(c,  6, 5);
00502     *buf   = 0x80 + GB(c,  0, 6);
00503     return 2;
00504   } else if (c < 0x10000) {
00505     *buf++ = 0xE0 + GB(c, 12, 4);
00506     *buf++ = 0x80 + GB(c,  6, 6);
00507     *buf   = 0x80 + GB(c,  0, 6);
00508     return 3;
00509   } else if (c < 0x110000) {
00510     *buf++ = 0xF0 + GB(c, 18, 3);
00511     *buf++ = 0x80 + GB(c, 12, 6);
00512     *buf++ = 0x80 + GB(c,  6, 6);
00513     *buf   = 0x80 + GB(c,  0, 6);
00514     return 4;
00515   }
00516 
00517   /* DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c); */
00518   *buf = '?';
00519   return 1;
00520 }
00521 
00529 size_t Utf8TrimString(char *s, size_t maxlen)
00530 {
00531   size_t length = 0;
00532 
00533   for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
00534     size_t len = Utf8EncodedCharLen(*s);
00535     /* Silently ignore invalid UTF8 sequences, our only concern trimming */
00536     if (len == 0) len = 1;
00537 
00538     /* Take care when a hard cutoff was made for the string and
00539      * the last UTF8 sequence is invalid */
00540     if (length + len >= maxlen || (s + len > ptr)) break;
00541     s += len;
00542     length += len;
00543   }
00544 
00545   *s = '\0';
00546   return length;
00547 }
00548 
00549 #ifdef DEFINE_STRNDUP
00550 #include "core/math_func.hpp"
00551 char *strndup(const char *s, size_t len)
00552 {
00553   len = min(strlen(s), len);
00554   char *tmp = CallocT<char>(len + 1);
00555   memcpy(tmp, s, len);
00556   return tmp;
00557 }
00558 #endif /* DEFINE_STRNDUP */
00559 
00560 #ifdef DEFINE_STRCASESTR
00561 char *strcasestr(const char *haystack, const char *needle)
00562 {
00563   size_t hay_len = strlen(haystack);
00564   size_t needle_len = strlen(needle);
00565   while (hay_len >= needle_len) {
00566     if (strncasecmp(haystack, needle, needle_len) == 0) return const_cast<char *>(haystack);
00567 
00568     haystack++;
00569     hay_len--;
00570   }
00571 
00572   return NULL;
00573 }
00574 #endif /* DEFINE_STRCASESTR */
00575 
00583 int strnatcmp(const char *s1, const char *s2)
00584 {
00585 #ifdef WITH_ICU
00586   if (_current_collator != NULL) {
00587     UErrorCode status = U_ZERO_ERROR;
00588     int result;
00589 
00590     /* We want to use the new faster method for ICU 4.2 and higher. */
00591 #if U_ICU_VERSION_MAJOR_NUM > 4 || (U_ICU_VERSION_MAJOR_NUM == 4 && U_ICU_VERSION_MINOR_NUM >= 2)
00592     /* The StringPiece parameter gets implicitly constructed from the char *. */
00593     result = _current_collator->compareUTF8(s1, s2, status);
00594 #else /* The following for 4.0 and lower. */
00595     UChar buffer1[DRAW_STRING_BUFFER];
00596     u_strFromUTF8Lenient(buffer1, lengthof(buffer1), NULL, s1, -1, &status);
00597     UChar buffer2[DRAW_STRING_BUFFER];
00598     u_strFromUTF8Lenient(buffer2, lengthof(buffer2), NULL, s2, -1, &status);
00599 
00600     result = _current_collator->compare(buffer1, buffer2, status);
00601 #endif /* ICU version check. */
00602     if (U_SUCCESS(status)) return result;
00603   }
00604 
00605 #endif /* WITH_ICU */
00606 
00607   /* Do a normal comparison if ICU is missing or if we cannot create a collator. */
00608   return strcasecmp(s1, s2);
00609 }