string_func.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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
00046 void ttd_strlcat(char *dst, const char *src, size_t size);
00047
00062 void ttd_strlcpy(char *dst, const char *src, size_t size);
00063
00080 char *strecat(char *dst, const char *src, const char *last);
00081
00098 char *strecpy(char *dst, const char *src, const char *last);
00099
00100 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
00101
00102 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
00103
00112 void str_validate(char *str, const char *last, bool allow_newlines = false, bool ignore = false);
00113
00115 void str_strip_colours(char *str);
00116
00118 void strtolower(char *str);
00119
00127 bool StrValid(const char *str, const char *last);
00128
00136 static inline bool StrEmpty(const char *s)
00137 {
00138 return s == NULL || s[0] == '\0';
00139 }
00140
00148 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
00149 {
00150 const char *t;
00151 for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
00152 return t - str;
00153 }
00154
00156 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
00157
00165 bool IsValidChar(WChar key, CharSetFilter afilter);
00166
00167 size_t Utf8Decode(WChar *c, const char *s);
00168 size_t Utf8Encode(char *buf, WChar c);
00169 size_t Utf8TrimString(char *s, size_t maxlen);
00170
00171
00172 static inline WChar Utf8Consume(const char **s)
00173 {
00174 WChar c;
00175 *s += Utf8Decode(&c, *s);
00176 return c;
00177 }
00178
00179
00185 static inline int8 Utf8CharLen(WChar c)
00186 {
00187 if (c < 0x80) return 1;
00188 if (c < 0x800) return 2;
00189 if (c < 0x10000) return 3;
00190 if (c < 0x110000) return 4;
00191
00192
00193 return 1;
00194 }
00195
00196
00204 static inline int8 Utf8EncodedCharLen(char c)
00205 {
00206 if (GB(c, 3, 5) == 0x1E) return 4;
00207 if (GB(c, 4, 4) == 0x0E) return 3;
00208 if (GB(c, 5, 3) == 0x06) return 2;
00209 if (GB(c, 7, 1) == 0x00) return 1;
00210
00211
00212 return 0;
00213 }
00214
00215
00216
00217 static inline bool IsUtf8Part(char c)
00218 {
00219 return GB(c, 6, 2) == 2;
00220 }
00221
00229 static inline char *Utf8PrevChar(char *s)
00230 {
00231 char *ret = s;
00232 while (IsUtf8Part(*--ret)) {}
00233 return ret;
00234 }
00235
00236 size_t Utf8StringLength(const char *s);
00237
00244 static inline bool IsTextDirectionChar(WChar c)
00245 {
00246 switch (c) {
00247 case CHAR_TD_LRM:
00248 case CHAR_TD_RLM:
00249 case CHAR_TD_LRE:
00250 case CHAR_TD_RLE:
00251 case CHAR_TD_LRO:
00252 case CHAR_TD_RLO:
00253 case CHAR_TD_PDF:
00254 return true;
00255
00256 default:
00257 return false;
00258 }
00259 }
00260
00261 static inline bool IsPrintable(WChar c)
00262 {
00263 if (c < 0x20) return false;
00264 if (c < 0xE000) return true;
00265 if (c < 0xE200) return false;
00266 return true;
00267 }
00268
00276 static inline bool IsWhitespace(WChar c)
00277 {
00278 return c == 0x0020 || c == 0x3000;
00279 }
00280
00281
00282 #if defined(__NetBSD__) || defined(__FreeBSD__)
00283 #include <sys/param.h>
00284 #endif
00285
00286
00287 #if defined(_GNU_SOURCE) || (defined(__NetBSD_Version__) && 400000000 <= __NetBSD_Version__) || (defined(__FreeBSD_version) && 701101 <= __FreeBSD_version)
00288 # undef DEFINE_STRNDUP
00289 #else
00290 # define DEFINE_STRNDUP
00291 char *strndup(const char *s, size_t len);
00292 #endif
00293
00294
00295 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
00296 # undef DEFINE_STRCASESTR
00297 #else
00298 # define DEFINE_STRCASESTR
00299 char *strcasestr(const char *haystack, const char *needle);
00300 #endif
00301
00302 #endif