string_func.h

Go to the documentation of this file.
00001 /* $Id: string_func.h 25979 2013-11-13 21:28:21Z 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 
00026 #ifndef STRING_FUNC_H
00027 #define STRING_FUNC_H
00028 
00029 #include "core/bitmath_func.hpp"
00030 #include "string_type.h"
00031 
00032 void ttd_strlcat(char *dst, const char *src, size_t size);
00033 void ttd_strlcpy(char *dst, const char *src, size_t size);
00034 
00035 char *strecat(char *dst, const char *src, const char *last);
00036 char *strecpy(char *dst, const char *src, const char *last);
00037 
00038 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
00039 
00040 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
00041 
00042 void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
00043 void ValidateString(const char *str);
00044 
00045 void str_fix_scc_encoded(char *str, const char *last);
00046 void str_strip_colours(char *str);
00047 bool strtolower(char *str);
00048 
00049 bool StrValid(const char *str, const char *last);
00050 
00058 static inline bool StrEmpty(const char *s)
00059 {
00060   return s == NULL || s[0] == '\0';
00061 }
00062 
00070 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
00071 {
00072   const char *t;
00073   for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
00074   return t - str;
00075 }
00076 
00077 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
00078 
00079 bool IsValidChar(WChar key, CharSetFilter afilter);
00080 
00081 size_t Utf8Decode(WChar *c, const char *s);
00082 size_t Utf8Encode(char *buf, WChar c);
00083 size_t Utf8TrimString(char *s, size_t maxlen);
00084 
00085 
00086 static inline WChar Utf8Consume(const char **s)
00087 {
00088   WChar c;
00089   *s += Utf8Decode(&c, *s);
00090   return c;
00091 }
00092 
00098 static inline int8 Utf8CharLen(WChar c)
00099 {
00100   if (c < 0x80)       return 1;
00101   if (c < 0x800)      return 2;
00102   if (c < 0x10000)    return 3;
00103   if (c < 0x110000)   return 4;
00104 
00105   /* Invalid valid, we encode as a '?' */
00106   return 1;
00107 }
00108 
00109 
00117 static inline int8 Utf8EncodedCharLen(char c)
00118 {
00119   if (GB(c, 3, 5) == 0x1E) return 4;
00120   if (GB(c, 4, 4) == 0x0E) return 3;
00121   if (GB(c, 5, 3) == 0x06) return 2;
00122   if (GB(c, 7, 1) == 0x00) return 1;
00123 
00124   /* Invalid UTF8 start encoding */
00125   return 0;
00126 }
00127 
00128 
00129 /* Check if the given character is part of a UTF8 sequence */
00130 static inline bool IsUtf8Part(char c)
00131 {
00132   return GB(c, 6, 2) == 2;
00133 }
00134 
00142 static inline char *Utf8PrevChar(char *s)
00143 {
00144   char *ret = s;
00145   while (IsUtf8Part(*--ret)) {}
00146   return ret;
00147 }
00148 
00149 static inline const char *Utf8PrevChar(const char *s)
00150 {
00151   const char *ret = s;
00152   while (IsUtf8Part(*--ret)) {}
00153   return ret;
00154 }
00155 
00156 size_t Utf8StringLength(const char *s);
00157 
00163 static inline bool Utf16IsLeadSurrogate(uint c)
00164 {
00165   return c >= 0xD800 && c <= 0xDBFF;
00166 }
00167 
00173 static inline bool Utf16IsTrailSurrogate(uint c)
00174 {
00175   return c >= 0xDC00 && c <= 0xDFFF;
00176 }
00177 
00184 static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
00185 {
00186   return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
00187 }
00188 
00194 static inline WChar Utf16DecodeChar(const uint16 *c)
00195 {
00196   if (Utf16IsLeadSurrogate(c[0])) {
00197     return Utf16DecodeSurrogate(c[0], c[1]);
00198   } else {
00199     return *c;
00200   }
00201 }
00202 
00209 static inline bool IsTextDirectionChar(WChar c)
00210 {
00211   switch (c) {
00212     case CHAR_TD_LRM:
00213     case CHAR_TD_RLM:
00214     case CHAR_TD_LRE:
00215     case CHAR_TD_RLE:
00216     case CHAR_TD_LRO:
00217     case CHAR_TD_RLO:
00218     case CHAR_TD_PDF:
00219       return true;
00220 
00221     default:
00222       return false;
00223   }
00224 }
00225 
00226 static inline bool IsPrintable(WChar c)
00227 {
00228   if (c < 0x20)   return false;
00229   if (c < 0xE000) return true;
00230   if (c < 0xE200) return false;
00231   return true;
00232 }
00233 
00241 static inline bool IsWhitespace(WChar c)
00242 {
00243   return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
00244 }
00245 
00246 /* Needed for NetBSD version (so feature) testing */
00247 #if defined(__NetBSD__) || defined(__FreeBSD__)
00248 #include <sys/param.h>
00249 #endif
00250 
00251 /* strndup is a GNU extension */
00252 #if defined(_GNU_SOURCE) || (defined(__NetBSD_Version__) && 400000000 <= __NetBSD_Version__) || (defined(__FreeBSD_version) && 701101 <= __FreeBSD_version) || (defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L)
00253 # undef DEFINE_STRNDUP
00254 #else
00255 # define DEFINE_STRNDUP
00256 char *strndup(const char *s, size_t len);
00257 #endif /* strndup is available */
00258 
00259 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
00260 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
00261 # undef DEFINE_STRCASESTR
00262 #else
00263 # define DEFINE_STRCASESTR
00264 char *strcasestr(const char *haystack, const char *needle);
00265 #endif /* strcasestr is available */
00266 
00267 int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front = false);
00268 
00269 #endif /* STRING_FUNC_H */