tron@2186: /* $Id$ */ tron@2186: celestar@9908: /** @file string.h */ celestar@9908: tron@1317: #ifndef STRING_H tron@1317: #define STRING_H tron@1317: peter1138@5108: #include "macros.h" peter1138@5108: celestar@9908: /** celestar@9908: * usage ttd_strlcpy(dst, src, lengthof(dst)); celestar@9908: * @param dst destination buffer celestar@9908: * @param src string to copy/concatenate celestar@9908: * @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: celestar@9908: /** tron@1317: * usage: strecpy(dst, src, lastof(dst)); celestar@9908: * @param dst destination buffer celestar@9908: * @param src string to copy celestar@9908: * @param last pointer to the last element in the dst array celestar@9908: * if NULL no boundary check is performed celestar@9908: * @return a pointer to the terminating \0 in the destination buffer tron@1317: */ tron@1317: char* strecat(char* dst, const char* src, const char* last); tron@1317: char* strecpy(char* dst, const char* src, const char* last); tron@1317: tron@2234: 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: truelight@4300: /** peter1138@5108: * Valid filter types for IsValidChar. truelight@4300: */ celestar@9895: enum CharSetFilter { celestar@9908: CS_ALPHANUMERAL, ///< Both numeric and alphabetic and spaces and stuff celestar@9908: CS_NUMERAL, ///< Only numeric ones celestar@9908: CS_ALPHA, ///< Only alphabetic values celestar@9895: }; truelight@4300: Darkvater@5164: /** Convert the given string to lowercase, only works with ASCII! */ peter1138@5108: void strtolower(char *str); peter1138@5108: peter1138@5317: tron@5889: static inline bool StrEmpty(const char* s) { return 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; peter1138@5317: 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: peter1138@5108: typedef uint32 WChar; peter1138@5108: 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 celestar@9908: * @param c char to query length of celestar@9908: * @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 celestar@9908: * @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; Darkvater@6540: 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: /** Darkvater@6541: * Check whether UNICODE character is whitespace or not 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 == 0x00A0 /* NO-BREAK SPACE */ || Darkvater@6541: c == 0x3000 /* IDEOGRAPHIC SPACE */ Darkvater@6541: ; Darkvater@6541: } Darkvater@6541: Darkvater@4200: Darkvater@2436: #endif /* STRING_H */