# string.cpp

Go to the documentation of this file.

    00001 /* $Id: string.cpp 17920 2009-10-31 14:06:16Z 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
    00033 static int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
    00034 {
    00035   if (str >= last) return 0;
    00036   size_t size = last - str + 1;
    00037   return min((int)size, vsnprintf(str, size, format, ap));
    00038 }
    00039
    00040 void ttd_strlcat(char *dst, const char *src, size_t size)
    00041 {
    00042   assert(size > 0);
    00043   while (size > 0 && *dst != '\0') {
    00044     size--;
    00045     dst++;
    00046   }
    00047
    00048   ttd_strlcpy(dst, src, size);
    00049 }
    00050
    00051
    00052 void ttd_strlcpy(char *dst, const char *src, size_t size)
    00053 {
    00054   assert(size > 0);
    00055   while (--size > 0 && *src != '\0') {
    00056     *dst++ = *src++;
    00057   }
    00058   *dst = '\0';
    00059 }
    00060
    00061
    00062 char *strecat(char *dst, const char *src, const char *last)
    00063 {
    00064   assert(dst <= last);
    00065   while (*dst != '\0') {
    00066     if (dst == last) return dst;
    00067     dst++;
    00068   }
    00069
    00070   return strecpy(dst, src, last);
    00071 }
    00072
    00073
    00074 char *strecpy(char *dst, const char *src, const char *last)
    00075 {
    00076   assert(dst <= last);
    00077   while (dst != last && *src != '\0') {
    00078     *dst++ = *src++;
    00079   }
    00080   *dst = '\0';
    00081
    00082   if (dst == last && *src != '\0') {
    00083 #ifdef STRGEN
    00084     error("String too long for destination buffer");
    00085 #else /* STRGEN */
    00086     DEBUG(misc, 0, "String too long for destination buffer");
    00087 #endif /* STRGEN */
    00088   }
    00089   return dst;
    00090 }
    00091
    00092
    00093 char *CDECL str_fmt(const char *str, ...)
    00094 {
    00095   char buf[4096];
    00096   va_list va;
    00097
    00098   va_start(va, str);
    00099   int len = vseprintf(buf, lastof(buf), str, va);
    00100   va_end(va);
    00101   char *p = MallocT<char>(len + 1);
    00102   memcpy(p, buf, len + 1);
    00103   return p;
    00104 }
    00105
    00106
    00107 void str_validate(char *str, const char *last, bool allow_newlines, bool ignore)
    00108 {
    00109   /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
    00110
    00111   char *dst = str;
    00112   while (*str != '\0') {
    00113     size_t len = Utf8EncodedCharLen(*str);
    00114     /* If the character is unknown, i.e. encoded length is 0
    00115      * we assume worst case for the length check.
    00116      * The length check is needed to prevent Utf8Decode to read
    00117      * over the terminating '\0' if that happens to be placed
    00118      * within the encoding of an UTF8 character. */
    00119     if ((len == 0 && str + 4 > last) || str + len > last) break;
    00120
    00121     WChar c;
    00122     len = Utf8Decode(&c, str);
    00123     /* It's possible to encode the string termination character
    00124      * into a multiple bytes. This prevents those termination
    00125      * characters to be skipped */
    00126     if (c == '\0') break;
    00127
    00128     if (IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END)) {
    00129       /* Copy the character back. Even if dst is current the same as str
    00130        * (i.e. no characters have been changed) this is quicker than
    00131        * moving the pointers ahead by len */
    00132       do {
    00133         *dst++ = *str++;
    00134       } while (--len != 0);
    00135     } else if (allow_newlines && c == '\n') {
    00136       *dst++ = *str++;
    00137     } else {
    00138       if (allow_newlines && c == '\r' && str[1] == '\n') {
    00139         str += len;
    00140         continue;
    00141       }
    00142       /* Replace the undesirable character with a question mark */
    00143       str += len;
    00144       if (!ignore) *dst++ = '?';
    00145     }
    00146   }
    00147
    00148   *dst = '\0';
    00149 }
    00150
    00151
    00152 void str_strip_colours(char *str)
    00153 {
    00154   char *dst = str;
    00155   WChar c;
    00156   size_t len;
    00157
    00158   for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
    00159     if (c < SCC_BLUE || c > SCC_BLACK) {
    00160       /* Copy the character back. Even if dst is current the same as str
    00161        * (i.e. no characters have been changed) this is quicker than
    00162        * moving the pointers ahead by len */
    00163       do {
    00164         *dst++ = *str++;
    00165       } while (--len != 0);
    00166     } else {
    00167       /* Just skip (strip) the colour codes */
    00168       str += len;
    00169     }
    00170   }
    00171   *dst = '\0';
    00172 }
    00173
    00182 void strtolower(char *str)
    00183 {
    00184   for (; *str != '\0'; str++) *str = tolower(*str);
    00185 }
    00186
    00194 bool IsValidChar(WChar key, CharSetFilter afilter)
    00195 {
    00196   switch (afilter) {
    00197     case CS_ALPHANUMERAL:  return IsPrintable(key);
    00198     case CS_NUMERAL:       return (key >= '0' && key <= '9');
    00199     case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
    00200     case CS_ALPHA:         return IsPrintable(key) && !(key >= '0' && key <= '9');
    00201   }
    00202
    00203   return false;
    00204 }
    00205
    00206 #ifdef WIN32
    00207 /* Since version 3.14, MinGW Runtime has snprintf() and vsnprintf() conform to C99 but it's not the case for older versions */
    00208 #if (__MINGW32_MAJOR_VERSION < 3) || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION < 14))
    00209 int CDECL snprintf(char *str, size_t size, const char *format, ...)
    00210 {
    00211   va_list ap;
    00212   int ret;
    00213
    00214   va_start(ap, format);
    00215   ret = vsnprintf(str, size, format, ap);
    00216   va_end(ap);
    00217   return ret;
    00218 }
    00219 #endif /* MinGW Runtime < 3.14 */
    00220
    00221 #ifdef _MSC_VER
    00222 /* *nprintf broken, not POSIX compliant, MSDN description
    00223  * - If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.
    00224  * - If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.
    00225  * - If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned
    00226  */
    00227 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
    00228 {
    00229   int ret;
    00230   ret = _vsnprintf(str, size, format, ap);
    00231   if (ret < 0 || ret == size) str[size - 1] = '\0';
    00232   return ret;
    00233 }
    00234 #endif /* _MSC_VER */
    00235
    00236 #endif /* WIN32 */
    00237
    00247 int CDECL seprintf(char *str, const char *last, const char *format, ...)
    00248 {
    00249   va_list ap;
    00250
    00251   va_start(ap, format);
    00252   int ret = vseprintf(str, last, format, ap);
    00253   va_end(ap);
    00254   return ret;
    00255 }
    00256
    00257
    00263 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
    00264 {
    00265   char *p = buf;
    00266
    00267   for (uint i = 0; i < 16; i++) {
    00268     p += seprintf(p, last, "%02X", md5sum[i]);
    00269   }
    00270
    00271   return p;
    00272 }
    00273
    00274
    00275 /* UTF-8 handling routines */
    00276
    00277
    00278 /* Decode and consume the next UTF-8 encoded character
    00279  * @param c Buffer to place decoded character.
    00280  * @param s Character stream to retrieve character from.
    00281  * @return Number of characters in the sequence.
    00282  */
    00283 size_t Utf8Decode(WChar *c, const char *s)
    00284 {
    00285   assert(c != NULL);
    00286
    00287   if (!HasBit(s[0], 7)) {
    00288     /* Single byte character: 0xxxxxxx */
    00289     *c = s[0];
    00290     return 1;
    00291   } else if (GB(s[0], 5, 3) == 6) {
    00292     if (IsUtf8Part(s[1])) {
    00293       /* Double byte character: 110xxxxx 10xxxxxx */
    00294       *c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
    00295       if (*c >= 0x80) return 2;
    00296     }
    00297   } else if (GB(s[0], 4, 4) == 14) {
    00298     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
    00299       /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
    00300       *c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
    00301       if (*c >= 0x800) return 3;
    00302     }
    00303   } else if (GB(s[0], 3, 5) == 30) {
    00304     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
    00305       /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
    00306       *c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
    00307       if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
    00308     }
    00309   }
    00310
    00311   /* DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence"); */
    00312   *c = '?';
    00313   return 1;
    00314 }
    00315
    00316
    00317 /* Encode a unicode character and place it in the buffer
    00318  * @param buf Buffer to place character.
    00319  * @param c   Unicode character to encode.
    00320  * @return Number of characters in the encoded sequence.
    00321  */
    00322 size_t Utf8Encode(char *buf, WChar c)
    00323 {
    00324   if (c < 0x80) {
    00325     *buf = c;
    00326     return 1;
    00327   } else if (c < 0x800) {
    00328     *buf++ = 0xC0 + GB(c,  6, 5);
    00329     *buf   = 0x80 + GB(c,  0, 6);
    00330     return 2;
    00331   } else if (c < 0x10000) {
    00332     *buf++ = 0xE0 + GB(c, 12, 4);
    00333     *buf++ = 0x80 + GB(c,  6, 6);
    00334     *buf   = 0x80 + GB(c,  0, 6);
    00335     return 3;
    00336   } else if (c < 0x110000) {
    00337     *buf++ = 0xF0 + GB(c, 18, 3);
    00338     *buf++ = 0x80 + GB(c, 12, 6);
    00339     *buf++ = 0x80 + GB(c,  6, 6);
    00340     *buf   = 0x80 + GB(c,  0, 6);
    00341     return 4;
    00342   }
    00343
    00344   /* DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c); */
    00345   *buf = '?';
    00346   return 1;
    00347 }
    00348
    00356 size_t Utf8TrimString(char *s, size_t maxlen)
    00357 {
    00358   size_t length = 0;
    00359
    00360   for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
    00361     size_t len = Utf8EncodedCharLen(*s);
    00362     /* Silently ignore invalid UTF8 sequences, our only concern trimming */
    00363     if (len == 0) len = 1;
    00364
    00365     /* Take care when a hard cutoff was made for the string and
    00366      * the last UTF8 sequence is invalid */
    00367     if (length + len >= maxlen || (s + len > ptr)) break;
    00368     s += len;
    00369     length += len;
    00370   }
    00371
    00372   *s = '\0';
    00373   return length;
    00374 }
    00375
    00376 #ifndef _GNU_SOURCE
    00377 #include "core/math_func.hpp"
    00378 char *strndup(const char *s, size_t len)
    00379 {
    00380   len = min(strlen(s), len);
    00381   char *tmp = CallocT<char>(len + 1);
    00382   memcpy(tmp, s, len);
    00383   return tmp;
    00384 }
    00385 #endif /* !_GNU_SOURCE */
    00386
    00387 #ifdef DEFINE_STRCASESTR
    00388 const char *strcasestr(const char *haystack, const char *needle)
    00389 {
    00390   size_t hay_len = strlen(haystack);
    00391   size_t needle_len = strlen(needle);
    00392   while (hay_len >= needle_len) {
    00393     if (strncasecmp(haystack, needle, needle_len) == 0) return haystack;
    00394
    00395     haystack++;
    00396     hay_len--;
    00397   }
    00398
    00399   return NULL;
    00400 }
    00401 #endif /* DEFINE_STRCASESTR */