tron@2186: /* $Id$ */ tron@2186: glx@8844: /** @file string_func.h Functions related to low-level strings. */ belugas@6916: rubidium@8710: #ifndef STRING_FUNC_H rubidium@8710: #define STRING_FUNC_H tron@1317: rubidium@8609: #include "core/bitmath_func.hpp" rubidium@8710: #include "string_type.h" peter1138@5108: belugas@6916: /** belugas@6916: * usage ttd_strlcpy(dst, src, lengthof(dst)); belugas@6916: * @param dst destination buffer belugas@6916: * @param src string to copy/concatenate belugas@6916: * @param size size of the destination buffer tron@1317: */ tron@1317: void ttd_strlcat(char *dst, const char *src, size_t size); tron@1317: void ttd_strlcpy(char *dst, const char *src, size_t size); tron@1317: belugas@6916: /** tron@1317: * usage: strecpy(dst, src, lastof(dst)); belugas@6916: * @param dst destination buffer belugas@6916: * @param src string to copy belugas@6916: * @param last pointer to the last element in the dst array belugas@6916: * if NULL no boundary check is performed belugas@6916: * @return a pointer to the terminating \0 in the destination buffer tron@1317: */ rubidium@7814: char *strecat(char *dst, const char *src, const char *last); rubidium@7814: char *strecpy(char *dst, const char *src, const char *last); tron@1317: rubidium@7814: char *CDECL str_fmt(const char *str, ...); tron@2234: Darkvater@2775: /** Scans the string for valid characters and if it finds invalid ones, Darkvater@2775: * replaces them with a question mark '?' */ Darkvater@2775: void str_validate(char *str); Darkvater@2775: Darkvater@5101: /** Scans the string for colour codes and strips them */ Darkvater@5101: void str_strip_colours(char *str); Darkvater@5101: Darkvater@5164: /** Convert the given string to lowercase, only works with ASCII! */ peter1138@5108: void strtolower(char *str); peter1138@5108: peter1138@5317: rubidium@8006: static inline bool StrEmpty(const char *s) { return s == NULL || s[0] == '\0'; } tron@5889: tron@5889: peter1138@5317: /** Get the length of a string, within a limited buffer */ peter1138@5317: static inline int ttd_strnlen(const char *str, int maxlen) peter1138@5317: { peter1138@5317: const char *t; smatz@9191: for (t = str; *t != '\0' && t - str < maxlen; t++) {} peter1138@5317: return t - str; peter1138@5317: } peter1138@5317: Darkvater@5885: /** Convert the md5sum number to a 'hexadecimal' string, return next pos in buffer */ Darkvater@5885: char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]); peter1138@5317: truelight@4300: /** truelight@4300: * Only allow certain keys. You can define the filter to be used. This makes truelight@4300: * sure no invalid keys can get into an editbox, like BELL. truelight@4300: * @param key character to be checked truelight@4300: * @param afilter the filter to use truelight@4300: * @return true or false depending if the character is printable/valid or not truelight@4300: */ peter1138@5108: bool IsValidChar(WChar key, CharSetFilter afilter); truelight@4300: peter1138@5108: size_t Utf8Decode(WChar *c, const char *s); peter1138@5108: size_t Utf8Encode(char *buf, WChar c); Darkvater@6541: size_t Utf8TrimString(char *s, size_t maxlen); peter1138@5108: peter1138@5108: peter1138@5108: static inline WChar Utf8Consume(const char **s) peter1138@5108: { peter1138@5108: WChar c; peter1138@5108: *s += Utf8Decode(&c, *s); peter1138@5108: return c; peter1138@5108: } peter1138@5108: peter1138@5108: peter1138@5108: /** Return the length of a UTF-8 encoded character. peter1138@5108: * @param c Unicode character. peter1138@5108: * @return Length of UTF-8 encoding for character. peter1138@5108: */ peter1138@5108: static inline size_t Utf8CharLen(WChar c) peter1138@5108: { peter1138@5108: if (c < 0x80) return 1; peter1138@5108: if (c < 0x800) return 2; peter1138@5108: if (c < 0x10000) return 3; peter1138@5108: if (c < 0x110000) return 4; peter1138@5108: peter1138@5108: /* Invalid valid, we encode as a '?' */ peter1138@5108: return 1; peter1138@5108: } peter1138@5108: peter1138@5108: Darkvater@6541: /** Darkvater@6541: * Return the length of an UTF-8 encoded value based on a single char. This Darkvater@6541: * char should be the first byte of the UTF-8 encoding. If not, or encoding Darkvater@6541: * is invalid, return value is 0 belugas@6916: * @param c char to query length of belugas@6916: * @return requested size Darkvater@6541: */ Darkvater@6541: static inline size_t Utf8EncodedCharLen(char c) Darkvater@6541: { Darkvater@6541: if (GB(c, 3, 5) == 0x1E) return 4; Darkvater@6541: if (GB(c, 4, 4) == 0x0E) return 3; Darkvater@6541: if (GB(c, 5, 3) == 0x06) return 2; Darkvater@6541: if (GB(c, 7, 1) == 0x00) return 1; Darkvater@6541: Darkvater@6541: /* Invalid UTF8 start encoding */ Darkvater@6541: return 0; Darkvater@6541: } Darkvater@6541: Darkvater@6541: peter1138@5108: /* Check if the given character is part of a UTF8 sequence */ peter1138@5108: static inline bool IsUtf8Part(char c) peter1138@5108: { peter1138@5108: return GB(c, 6, 2) == 2; peter1138@5108: } peter1138@5108: Darkvater@6537: /** Darkvater@6540: * Retrieve the previous UNICODE character in an UTF-8 encoded string. Darkvater@6540: * @param s char pointer pointing to (the first char of) the next character belugas@6916: * @return a pointer in 's' to the previous UNICODE character's first byte Darkvater@6540: * @note The function should not be used to determine the length of the previous Darkvater@6540: * encoded char because it might be an invalid/corrupt start-sequence Darkvater@6537: */ Darkvater@6540: static inline char *Utf8PrevChar(const char *s) Darkvater@6537: { Darkvater@6540: const char *ret = s; smatz@9191: while (IsUtf8Part(*--ret)) {} Darkvater@6540: return (char*)ret; Darkvater@6537: } Darkvater@6537: peter1138@5108: peter1138@5108: static inline bool IsPrintable(WChar c) peter1138@5108: { peter1138@5108: if (c < 0x20) return false; peter1138@5108: if (c < 0xE000) return true; peter1138@5108: if (c < 0xE200) return false; peter1138@5108: return true; peter1138@5108: } peter1138@5108: Darkvater@6541: /** peter1138@9207: * Check whether UNICODE character is whitespace or not, i.e. whether peter1138@9207: * this is a potential line-break character. Darkvater@6541: * @param c UNICODE character to check Darkvater@6541: * @return a boolean value whether 'c' is a whitespace character or not Darkvater@6541: * @see http://www.fileformat.info/info/unicode/category/Zs/list.htm Darkvater@6541: */ Darkvater@6541: static inline bool IsWhitespace(WChar c) Darkvater@6541: { Darkvater@6541: return Darkvater@6541: c == 0x0020 /* SPACE */ || Darkvater@6541: c == 0x3000 /* IDEOGRAPHIC SPACE */ Darkvater@6541: ; Darkvater@6541: } Darkvater@6541: rubidium@8710: #endif /* STRING_FUNC_H */