# string_func.h

Go to the documentation of this file.

    00001 /* $Id: string_func.h 16273 2009-05-10 21:33:55Z rubidium $ */
    00002
    00018 #ifndef STRING_FUNC_H
    00019 #define STRING_FUNC_H
    00020
    00021 #include "core/bitmath_func.hpp"
    00022 #include "string_type.h"
    00023
    00038 void ttd_strlcat(char *dst, const char *src, size_t size);
    00039
    00054 void ttd_strlcpy(char *dst, const char *src, size_t size);
    00055
    00072 char *strecat(char *dst, const char *src, const char *last);
    00073
    00090 char *strecpy(char *dst, const char *src, const char *last);
    00091
    00092 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
    00093
    00094 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
    00095
    00104 void str_validate(char *str, const char *last, bool allow_newlines = false, bool ignore = false);
    00105
    00107 void str_strip_colours(char *str);
    00108
    00110 void strtolower(char *str);
    00111
    00119 static inline bool StrEmpty(const char *s)
    00120 {
    00121   return s == NULL || s[0] == '\0';
    00122 }
    00123
    00131 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
    00132 {
    00133   const char *t;
    00134   for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
    00135   return t - str;
    00136 }
    00137
    00139 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
    00140
    00148 bool IsValidChar(WChar key, CharSetFilter afilter);
    00149
    00150 size_t Utf8Decode(WChar *c, const char *s);
    00151 size_t Utf8Encode(char *buf, WChar c);
    00152 size_t Utf8TrimString(char *s, size_t maxlen);
    00153
    00154
    00155 static inline WChar Utf8Consume(const char **s)
    00156 {
    00157   WChar c;
    00158   *s += Utf8Decode(&c, *s);
    00159   return c;
    00160 }
    00161
    00162
    00167 static inline int8 Utf8CharLen(WChar c)
    00168 {
    00169   if (c < 0x80)       return 1;
    00170   if (c < 0x800)      return 2;
    00171   if (c < 0x10000)    return 3;
    00172   if (c < 0x110000)   return 4;
    00173
    00174   /* Invalid valid, we encode as a '?' */
    00175   return 1;
    00176 }
    00177
    00178
    00186 static inline int8 Utf8EncodedCharLen(char c)
    00187 {
    00188   if (GB(c, 3, 5) == 0x1E) return 4;
    00189   if (GB(c, 4, 4) == 0x0E) return 3;
    00190   if (GB(c, 5, 3) == 0x06) return 2;
    00191   if (GB(c, 7, 1) == 0x00) return 1;
    00192
    00193   /* Invalid UTF8 start encoding */
    00194   return 0;
    00195 }
    00196
    00197
    00198 /* Check if the given character is part of a UTF8 sequence */
    00199 static inline bool IsUtf8Part(char c)
    00200 {
    00201   return GB(c, 6, 2) == 2;
    00202 }
    00203
    00211 static inline char *Utf8PrevChar(const char *s)
    00212 {
    00213   const char *ret = s;
    00214   while (IsUtf8Part(*--ret)) {}
    00215   return (char*)ret;
    00216 }
    00217
    00218
    00219 static inline bool IsPrintable(WChar c)
    00220 {
    00221   if (c < 0x20)   return false;
    00222   if (c < 0xE000) return true;
    00223   if (c < 0xE200) return false;
    00224   return true;
    00225 }
    00226
    00234 static inline bool IsWhitespace(WChar c)
    00235 {
    00236   return
    00237     c == 0x0020 /* SPACE */ ||
    00238     c == 0x3000 /* IDEOGRAPHIC SPACE */
    00239   ;
    00240 }
    00241
    00242 #ifndef _GNU_SOURCE
    00243 /* strndup is a GNU extension */
    00244 char *strndup(const char *s, size_t len);
    00245 #endif /* !_GNU_SOURCE */
    00246
    00247 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
    00248 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)))
    00249 # undef DEFINE_STRCASESTR
    00250 #else
    00251 # define DEFINE_STRCASESTR
    00252 const char *strcasestr(const char *haystack, const char *needle);
    00253 #endif /* strcasestr is available */
    00254
    00255 #endif /* STRING_FUNC_H */