OpenTTD
string_func.h
Go to the documentation of this file.
1 /* $Id: string_func.h 26513 2014-04-25 19:25:38Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
26 #ifndef STRING_FUNC_H
27 #define STRING_FUNC_H
28 
29 #include <stdarg.h>
30 
31 #include "core/bitmath_func.hpp"
32 #include "string_type.h"
33 
34 char *strecat(char *dst, const char *src, const char *last);
35 char *strecpy(char *dst, const char *src, const char *last);
36 char *stredup(const char *src, const char *last = NULL);
37 
38 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
39 int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap);
40 
41 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
42 
43 void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
44 void ValidateString(const char *str);
45 
46 void str_fix_scc_encoded(char *str, const char *last);
47 void str_strip_colours(char *str);
48 bool strtolower(char *str);
49 
50 bool StrValid(const char *str, const char *last);
51 
59 static inline bool StrEmpty(const char *s)
60 {
61  return s == NULL || s[0] == '\0';
62 }
63 
71 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
72 {
73  const char *t;
74  for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
75  return t - str;
76 }
77 
78 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
79 
80 bool IsValidChar(WChar key, CharSetFilter afilter);
81 
82 size_t Utf8Decode(WChar *c, const char *s);
83 size_t Utf8Encode(char *buf, WChar c);
84 size_t Utf8TrimString(char *s, size_t maxlen);
85 
86 
87 static inline WChar Utf8Consume(const char **s)
88 {
89  WChar c;
90  *s += Utf8Decode(&c, *s);
91  return c;
92 }
93 
99 static inline int8 Utf8CharLen(WChar c)
100 {
101  if (c < 0x80) return 1;
102  if (c < 0x800) return 2;
103  if (c < 0x10000) return 3;
104  if (c < 0x110000) return 4;
105 
106  /* Invalid valid, we encode as a '?' */
107  return 1;
108 }
109 
110 
118 static inline int8 Utf8EncodedCharLen(char c)
119 {
120  if (GB(c, 3, 5) == 0x1E) return 4;
121  if (GB(c, 4, 4) == 0x0E) return 3;
122  if (GB(c, 5, 3) == 0x06) return 2;
123  if (GB(c, 7, 1) == 0x00) return 1;
124 
125  /* Invalid UTF8 start encoding */
126  return 0;
127 }
128 
129 
130 /* Check if the given character is part of a UTF8 sequence */
131 static inline bool IsUtf8Part(char c)
132 {
133  return GB(c, 6, 2) == 2;
134 }
135 
143 static inline char *Utf8PrevChar(char *s)
144 {
145  char *ret = s;
146  while (IsUtf8Part(*--ret)) {}
147  return ret;
148 }
149 
150 static inline const char *Utf8PrevChar(const char *s)
151 {
152  const char *ret = s;
153  while (IsUtf8Part(*--ret)) {}
154  return ret;
155 }
156 
157 size_t Utf8StringLength(const char *s);
158 
164 static inline bool Utf16IsLeadSurrogate(uint c)
165 {
166  return c >= 0xD800 && c <= 0xDBFF;
167 }
168 
174 static inline bool Utf16IsTrailSurrogate(uint c)
175 {
176  return c >= 0xDC00 && c <= 0xDFFF;
177 }
178 
185 static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
186 {
187  return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
188 }
189 
195 static inline WChar Utf16DecodeChar(const uint16 *c)
196 {
197  if (Utf16IsLeadSurrogate(c[0])) {
198  return Utf16DecodeSurrogate(c[0], c[1]);
199  } else {
200  return *c;
201  }
202 }
203 
210 static inline bool IsTextDirectionChar(WChar c)
211 {
212  switch (c) {
213  case CHAR_TD_LRM:
214  case CHAR_TD_RLM:
215  case CHAR_TD_LRE:
216  case CHAR_TD_RLE:
217  case CHAR_TD_LRO:
218  case CHAR_TD_RLO:
219  case CHAR_TD_PDF:
220  return true;
221 
222  default:
223  return false;
224  }
225 }
226 
227 static inline bool IsPrintable(WChar c)
228 {
229  if (c < 0x20) return false;
230  if (c < 0xE000) return true;
231  if (c < 0xE200) return false;
232  return true;
233 }
234 
242 static inline bool IsWhitespace(WChar c)
243 {
244  return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
245 }
246 
247 /* Needed for NetBSD version (so feature) testing */
248 #if defined(__NetBSD__) || defined(__FreeBSD__)
249 #include <sys/param.h>
250 #endif
251 
252 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
253 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
254 # undef DEFINE_STRCASESTR
255 #else
256 # define DEFINE_STRCASESTR
257 char *strcasestr(const char *haystack, const char *needle);
258 #endif /* strcasestr is available */
259 
260 int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front = false);
261 
262 #endif /* STRING_FUNC_H */
size_t Utf8StringLength(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition: string.cpp:300
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
Definition: string.cpp:139
static const WChar CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition: string_type.h:39
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:338
static const WChar CHAR_TD_LRE
The following text is embedded left-to-right.
Definition: string_type.h:41
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:416
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:242
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
static bool IsTextDirectionChar(WChar c)
Is the given character a text direction character.
Definition: string_func.h:210
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:73
Functions related to bit mathematics.
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
static int8 Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Definition: string_func.h:118
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:437
static WChar Utf16DecodeChar(const uint16 *c)
Decode an UTF-16 character.
Definition: string_func.h:195
void ValidateString(const char *str)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:233
size_t Utf8TrimString(char *s, size_t maxlen)
Properly terminate an UTF8 string to some maximum length.
Definition: string.cpp:511
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:320
static int8 Utf8CharLen(WChar c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:99
int CDECL int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:50
static const WChar CHAR_TD_RLE
The following text is embedded right-to-left.
Definition: string_type.h:42
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:26
static WChar Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition: string_func.h:185
size_t Utf8Encode(char *buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:477
static const WChar CHAR_TD_PDF
Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
Definition: string_type.h:45
char * stredup(const char *src, const char *last=NULL)
Create a duplicate of the given string.
Definition: string.cpp:126
static bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition: string_func.h:174
char *CDECL void str_validate(char *str, const char *last, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:184
static size_t ttd_strnlen(const char *str, size_t maxlen)
Get the length of a string, within a limited buffer.
Definition: string_func.h:71
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
Definition: string.cpp:272
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static const WChar CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition: string_type.h:40
static char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
Definition: string_func.h:143
bool StrValid(const char *str, const char *last)
Checks whether the given string is valid, i.e.
Definition: string.cpp:247
static bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition: string_func.h:164
Types for strings.
void str_fix_scc_encoded(char *str, const char *last)
Scan the string for old values of SCC_ENCODED and fix it to it&#39;s new, static value.
Definition: string.cpp:158
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
static const WChar CHAR_TD_LRO
Force the following characters to be treated as left-to-right characters.
Definition: string_type.h:43
static const WChar CHAR_TD_RLO
Force the following characters to be treated as right-to-left characters.
Definition: string_type.h:44
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front=false)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:569