OpenTTD Source  13.2.1
string_func.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
24 #ifndef STRING_FUNC_H
25 #define STRING_FUNC_H
26 
27 #include <stdarg.h>
28 #include <iosfwd>
29 
30 #include "core/bitmath_func.hpp"
31 #include "core/span_type.hpp"
32 #include "string_type.h"
33 
34 char *strecat(char *dst, const char *src, const char *last) NOACCESS(3);
35 char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
36 char *stredup(const char *src, const char *last = nullptr) NOACCESS(2);
37 
38 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4) NOACCESS(2);
39 int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap) WARN_FORMAT(3, 0) NOACCESS(2);
40 
41 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
42 
43 std::string FormatArrayAsHex(span<const byte> data);
44 
45 void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
46 [[nodiscard]] std::string StrMakeValid(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
48 
49 void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
50 void str_strip_colours(char *str);
51 bool strtolower(char *str);
52 bool strtolower(std::string &str, std::string::size_type offs = 0);
53 
54 bool StrValid(const char *str, const char *last) NOACCESS(2);
55 void StrTrimInPlace(std::string &str);
56 
57 bool StrStartsWith(const std::string_view str, const std::string_view prefix);
58 bool StrEndsWith(const std::string_view str, const std::string_view suffix);
59 
67 static inline bool StrEmpty(const char *s)
68 {
69  return s == nullptr || s[0] == '\0';
70 }
71 
79 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
80 {
81  const char *t;
82  for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
83  return t - str;
84 }
85 
86 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
87 
88 bool IsValidChar(WChar key, CharSetFilter afilter);
89 
90 size_t Utf8Decode(WChar *c, const char *s);
91 size_t Utf8Encode(char *buf, WChar c);
92 size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, WChar c);
93 size_t Utf8TrimString(char *s, size_t maxlen);
94 
95 
96 static inline WChar Utf8Consume(const char **s)
97 {
98  WChar c;
99  *s += Utf8Decode(&c, *s);
100  return c;
101 }
102 
103 template <class Titr>
104 static inline WChar Utf8Consume(Titr &s)
105 {
106  WChar c;
107  s += Utf8Decode(&c, &*s);
108  return c;
109 }
110 
116 static inline int8 Utf8CharLen(WChar c)
117 {
118  if (c < 0x80) return 1;
119  if (c < 0x800) return 2;
120  if (c < 0x10000) return 3;
121  if (c < 0x110000) return 4;
122 
123  /* Invalid valid, we encode as a '?' */
124  return 1;
125 }
126 
127 
135 static inline int8 Utf8EncodedCharLen(char c)
136 {
137  if (GB(c, 3, 5) == 0x1E) return 4;
138  if (GB(c, 4, 4) == 0x0E) return 3;
139  if (GB(c, 5, 3) == 0x06) return 2;
140  if (GB(c, 7, 1) == 0x00) return 1;
141 
142  /* Invalid UTF8 start encoding */
143  return 0;
144 }
145 
146 
147 /* Check if the given character is part of a UTF8 sequence */
148 static inline bool IsUtf8Part(char c)
149 {
150  return GB(c, 6, 2) == 2;
151 }
152 
160 static inline char *Utf8PrevChar(char *s)
161 {
162  char *ret = s;
163  while (IsUtf8Part(*--ret)) {}
164  return ret;
165 }
166 
167 static inline const char *Utf8PrevChar(const char *s)
168 {
169  const char *ret = s;
170  while (IsUtf8Part(*--ret)) {}
171  return ret;
172 }
173 
174 size_t Utf8StringLength(const char *s);
175 size_t Utf8StringLength(const std::string &str);
176 
182 static inline bool Utf16IsLeadSurrogate(uint c)
183 {
184  return c >= 0xD800 && c <= 0xDBFF;
185 }
186 
192 static inline bool Utf16IsTrailSurrogate(uint c)
193 {
194  return c >= 0xDC00 && c <= 0xDFFF;
195 }
196 
203 static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
204 {
205  return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
206 }
207 
213 static inline WChar Utf16DecodeChar(const uint16 *c)
214 {
215  if (Utf16IsLeadSurrogate(c[0])) {
216  return Utf16DecodeSurrogate(c[0], c[1]);
217  } else {
218  return *c;
219  }
220 }
221 
228 static inline bool IsTextDirectionChar(WChar c)
229 {
230  switch (c) {
231  case CHAR_TD_LRM:
232  case CHAR_TD_RLM:
233  case CHAR_TD_LRE:
234  case CHAR_TD_RLE:
235  case CHAR_TD_LRO:
236  case CHAR_TD_RLO:
237  case CHAR_TD_PDF:
238  return true;
239 
240  default:
241  return false;
242  }
243 }
244 
245 static inline bool IsPrintable(WChar c)
246 {
247  if (c < 0x20) return false;
248  if (c < 0xE000) return true;
249  if (c < 0xE200) return false;
250  return true;
251 }
252 
260 static inline bool IsWhitespace(WChar c)
261 {
262  return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
263 }
264 
265 /* Needed for NetBSD version (so feature) testing */
266 #if defined(__NetBSD__) || defined(__FreeBSD__)
267 #include <sys/param.h>
268 #endif
269 
270 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
271 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
272 # undef DEFINE_STRCASESTR
273 #else
274 # define DEFINE_STRCASESTR
275 char *strcasestr(const char *haystack, const char *needle);
276 #endif /* strcasestr is available */
277 
278 int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front = false);
279 
280 #endif /* STRING_FUNC_H */
strecat
char * strecat(char *dst, const char *src, const char *last) NOACCESS(3)
Appends characters from one string to another.
Definition: string.cpp:85
Utf8Decode
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:593
stredup
char * stredup(const char *src, const char *last=nullptr) NOACCESS(2)
Create a duplicate of the given string.
Definition: string.cpp:138
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
StrTrimInPlace
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
Definition: string.cpp:373
Utf8CharLen
static int8 Utf8CharLen(WChar c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:116
StrMakeValidInPlace
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:273
CHAR_TD_LRO
static const WChar CHAR_TD_LRO
Force the following characters to be treated as left-to-right characters.
Definition: string_type.h:44
Utf8Encode
size_t Utf8Encode(T buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:635
strnatcmp
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:737
CHAR_TD_RLO
static const WChar CHAR_TD_RLO
Force the following characters to be treated as right-to-left characters.
Definition: string_type.h:45
Utf16DecodeChar
static WChar Utf16DecodeChar(const uint16 *c)
Decode an UTF-16 character.
Definition: string_func.h:213
StrValid
bool StrValid(const char *str, const char *last) NOACCESS(2)
Checks whether the given string is valid, i.e.
Definition: string.cpp:318
StrStartsWith
bool StrStartsWith(const std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix.
Definition: string.cpp:385
Utf8TrimString
size_t Utf8TrimString(char *s, size_t maxlen)
Properly terminate an UTF8 string to some maximum length.
Definition: string.cpp:679
StrEndsWith
bool StrEndsWith(const std::string_view str, const std::string_view suffix)
Check whether the given string ends with the given suffix.
Definition: string.cpp:398
IsValidChar
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:494
strtolower
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:465
CHAR_TD_RLE
static const WChar CHAR_TD_RLE
The following text is embedded right-to-left.
Definition: string_type.h:43
Utf16DecodeSurrogate
static WChar Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition: string_func.h:203
bitmath_func.hpp
CHAR_TD_PDF
static const WChar CHAR_TD_PDF
Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
Definition: string_type.h:46
CHAR_TD_LRE
static const WChar CHAR_TD_LRE
The following text is embedded left-to-right.
Definition: string_type.h:42
Utf8StringLength
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:435
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
md5sumToString
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:572
Utf16IsLeadSurrogate
static bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition: string_func.h:182
span_type.hpp
CHAR_TD_RLM
static const WChar CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition: string_type.h:41
ttd_strnlen
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:79
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:67
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
vseprintf
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:62
str_strip_colours
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
Definition: string.cpp:407
string_type.h
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:49
strecpy
char * strecpy(char *dst, const char *src, const char *last) NOACCESS(3)
Copies characters from one buffer to another.
Definition: string.cpp:113
StrMakeValid
std::string StrMakeValid(const std::string &str, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:299
str_fmt
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
Definition: string.cpp:151
FormatArrayAsHex
char *CDECL std::string FormatArrayAsHex(span< const byte > data)
Format a byte array into a continuous hex string.
Definition: string.cpp:169
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:554
CHAR_TD_LRM
static const WChar CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition: string_type.h:40
IsWhitespace
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:260
str_fix_scc_encoded
void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2)
Scan the string for old values of SCC_ENCODED and fix it to it's new, static value.
Definition: string.cpp:187
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:51
Utf16IsTrailSurrogate
static bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition: string_func.h:192
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:26
IsTextDirectionChar
static bool IsTextDirectionChar(WChar c)
Is the given character a text direction character.
Definition: string_func.h:228
Utf8PrevChar
static char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
Definition: string_func.h:160
Utf8EncodedCharLen
static int8 Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Definition: string_func.h:135