# string_func.h

Go to the documentation of this file.

    00001 /* $Id: string_func.h 14269 2008-09-07 22:14:48Z rubidium $ */
    00002
    00005 #ifndef STRING_FUNC_H
    00006 #define STRING_FUNC_H
    00007
    00008 #include "core/bitmath_func.hpp"
    00009 #include "string_type.h"
    00010
    00017 void ttd_strlcat(char *dst, const char *src, size_t size);
    00018 void ttd_strlcpy(char *dst, const char *src, size_t size);
    00019
    00028 char *strecat(char *dst, const char *src, const char *last);
    00029 char *strecpy(char *dst, const char *src, const char *last);
    00030
    00031 char *CDECL str_fmt(const char *str, ...);
    00032
    00035 void str_validate(char *str);
    00036
    00038 void str_strip_colours(char *str);
    00039
    00041 void strtolower(char *str);
    00042
    00043
    00044 static inline bool StrEmpty(const char *s) { return s == NULL || s[0] == '\0'; }
    00045
    00046
    00048 static inline int ttd_strnlen(const char *str, int maxlen)
    00049 {
    00050   const char *t;
    00051   for (t = str; t - str < maxlen && *t != '\0'; t++) {}
    00052   return t - str;
    00053 }
    00054
    00056 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
    00057
    00065 bool IsValidChar(WChar key, CharSetFilter afilter);
    00066
    00067 size_t Utf8Decode(WChar *c, const char *s);
    00068 size_t Utf8Encode(char *buf, WChar c);
    00069 size_t Utf8TrimString(char *s, size_t maxlen);
    00070
    00071
    00072 static inline WChar Utf8Consume(const char **s)
    00073 {
    00074   WChar c;
    00075   *s += Utf8Decode(&c, *s);
    00076   return c;
    00077 }
    00078
    00079
    00084 static inline size_t Utf8CharLen(WChar c)
    00085 {
    00086   if (c < 0x80)       return 1;
    00087   if (c < 0x800)      return 2;
    00088   if (c < 0x10000)    return 3;
    00089   if (c < 0x110000)   return 4;
    00090
    00091   /* Invalid valid, we encode as a '?' */
    00092   return 1;
    00093 }
    00094
    00095
    00103 static inline size_t Utf8EncodedCharLen(char c)
    00104 {
    00105   if (GB(c, 3, 5) == 0x1E) return 4;
    00106   if (GB(c, 4, 4) == 0x0E) return 3;
    00107   if (GB(c, 5, 3) == 0x06) return 2;
    00108   if (GB(c, 7, 1) == 0x00) return 1;
    00109
    00110   /* Invalid UTF8 start encoding */
    00111   return 0;
    00112 }
    00113
    00114
    00115 /* Check if the given character is part of a UTF8 sequence */
    00116 static inline bool IsUtf8Part(char c)
    00117 {
    00118   return GB(c, 6, 2) == 2;
    00119 }
    00120
    00128 static inline char *Utf8PrevChar(const char *s)
    00129 {
    00130   const char *ret = s;
    00131   while (IsUtf8Part(*--ret)) {}
    00132   return (char*)ret;
    00133 }
    00134
    00135
    00136 static inline bool IsPrintable(WChar c)
    00137 {
    00138   if (c < 0x20)   return false;
    00139   if (c < 0xE000) return true;
    00140   if (c < 0xE200) return false;
    00141   return true;
    00142 }
    00143
    00151 static inline bool IsWhitespace(WChar c)
    00152 {
    00153   return
    00154     c == 0x0020 /* SPACE */ ||
    00155     c == 0x3000 /* IDEOGRAPHIC SPACE */
    00156   ;
    00157 }
    00158
    00159 #endif /* STRING_FUNC_H */