OpenTTD Source: src/string_func.h Source File

OpenTTD Source 14.0-beta1

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
12 #ifndef STRING_FUNC_H
13 #define STRING_FUNC_H
14
15 #include <iosfwd>
16
17 #include "core/bitmath_func.hpp"
18 #include "string_type.h"
19
20 char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
21
22 std::string FormatArrayAsHex(std::span<const byte> data);
23
24 void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
25 [[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
26 void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
27
28 bool strtolower(std::string &str, std::string::size_type offs = 0);
29
30 [[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2);
31 void StrTrimInPlace(std::string &str);
32
33 [[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix);
34 [[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix);
35
36 [[nodiscard]] int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2);
37 [[nodiscard]] bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2);
38 [[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
39 [[nodiscard]] bool StrNaturalContains(const std::string_view str, const std::string_view value);
40 [[nodiscard]] bool StrNaturalContainsIgnoreCase(const std::string_view str, const std::string_view value);
41
42 bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
43
45 struct CaseInsensitiveComparator {
46 bool operator()(const std::string_view s1, const std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
47 };
48
56 inline bool StrEmpty(const char *s)
57 {
58 return s == nullptr || s[0] == '\0';
59 }
60
68 inline size_t ttd_strnlen(const char *str, size_t maxlen)
69 {
70 const char *t;
71 for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
72 return t - str;
73 }
74
75 bool IsValidChar(char32_t key, CharSetFilter afilter);
76
77 size_t Utf8Decode(char32_t *c, const char *s);
78 size_t Utf8Encode(char *buf, char32_t c);
79 size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, char32_t c);
80 size_t Utf8Encode(std::back_insert_iterator<std::string> &buf, char32_t c);
81 size_t Utf8TrimString(char *s, size_t maxlen);
82
83
84 inline char32_t Utf8Consume(const char **s)
85 {
86 char32_t c;
87 *s += Utf8Decode(&c, *s);
88 return c;
89 }
90
91 template <class Titr>
92 inline char32_t Utf8Consume(Titr &s)
93 {
94 char32_t c;
95 s += Utf8Decode(&c, &*s);
96 return c;
97 }
98
104 inline int8_t Utf8CharLen(char32_t c)
105 {
106 if (c < 0x80) return 1;
107 if (c < 0x800) return 2;
108 if (c < 0x10000) return 3;
109 if (c < 0x110000) return 4;
110
111 /* Invalid valid, we encode as a '?' */
112 return 1;
113 }
114
115
123 inline int8_t Utf8EncodedCharLen(char c)
124 {
125 if (GB(c, 3, 5) == 0x1E) return 4;
126 if (GB(c, 4, 4) == 0x0E) return 3;
127 if (GB(c, 5, 3) == 0x06) return 2;
128 if (GB(c, 7, 1) == 0x00) return 1;
129
130 /* Invalid UTF8 start encoding */
131 return 0;
132 }
133
134
135 /* Check if the given character is part of a UTF8 sequence */
136 inline bool IsUtf8Part(char c)
137 {
138 return GB(c, 6, 2) == 2;
139 }
140
148 inline char *Utf8PrevChar(char *s)
149 {
150 char *ret = s;
151 while (IsUtf8Part(*--ret)) {}
152 return ret;
153 }
154
155 inline const char *Utf8PrevChar(const char *s)
156 {
157 const char *ret = s;
158 while (IsUtf8Part(*--ret)) {}
159 return ret;
160 }
161
162 size_t Utf8StringLength(const char *s);
163 size_t Utf8StringLength(const std::string &str);
164
170 inline bool Utf16IsLeadSurrogate(uint c)
171 {
172 return c >= 0xD800 && c <= 0xDBFF;
173 }
174
180 inline bool Utf16IsTrailSurrogate(uint c)
181 {
182 return c >= 0xDC00 && c <= 0xDFFF;
183 }
184
191 inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
192 {
193 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
194 }
195
201 inline char32_t Utf16DecodeChar(const uint16_t *c)
202 {
203 if (Utf16IsLeadSurrogate(c[0])) {
204 return Utf16DecodeSurrogate(c[0], c[1]);
205 } else {
206 return *c;
207 }
208 }
209
216 inline bool IsTextDirectionChar(char32_t c)
217 {
218 switch (c) {
219 case CHAR_TD_LRM:
220 case CHAR_TD_RLM:
221 case CHAR_TD_LRE:
222 case CHAR_TD_RLE:
223 case CHAR_TD_LRO:
224 case CHAR_TD_RLO:
225 case CHAR_TD_PDF:
226 return true;
227
228 default:
229 return false;
230 }
231 }
232
233 inline bool IsPrintable(char32_t c)
234 {
235 if (c < 0x20) return false;
236 if (c < 0xE000) return true;
237 if (c < 0xE200) return false;
238 return true;
239 }
240
248 inline bool IsWhitespace(char32_t c)
249 {
250 return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
251 }
252
253 /* Needed for NetBSD version (so feature) testing */
254 #if defined(__NetBSD__) || defined(__FreeBSD__)
255 #include <sys/param.h>
256 #endif
257
258 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
259 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
260 # undef DEFINE_STRCASESTR
261 #else
262 # define DEFINE_STRCASESTR
263 char *strcasestr(const char *haystack, const char *needle);
264 #endif /* strcasestr is available */
265
266 #endif /* STRING_FUNC_H */
IsValidChar
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:415
StrMakeValid
std::string StrMakeValid(std::string_view str, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Copies the valid (UTF-8) characters from str to the returned string.
Definition: string.cpp:212
Utf16DecodeSurrogate
char32_t Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition: string_func.h:191
StrTrimInPlace
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
Definition: string.cpp:288
Utf8CharLen
int8_t Utf8CharLen(char32_t c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:104
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
Utf8PrevChar
char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
Definition: string_func.h:148
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:185
CHAR_TD_RLO
static const char32_t CHAR_TD_RLO
Force the following characters to be treated as right-to-left characters.
Definition: string_type.h:40
Utf16DecodeChar
char32_t Utf16DecodeChar(const uint16_t *c)
Decode an UTF-16 character.
Definition: string_func.h:201
Utf16IsTrailSurrogate
bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition: string_func.h:180
StrNaturalCompare
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front=false)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:585
ttd_strnlen
size_t ttd_strnlen(const char *str, size_t maxlen)
Get the length of a string, within a limited buffer.
Definition: string_func.h:68
StrValid
bool StrValid(const char *str, const char *last) NOACCESS(2)
Checks whether the given string is valid, i.e.
Definition: string.cpp:233
Utf8TrimString
size_t Utf8TrimString(char *s, size_t maxlen)
Properly terminate an UTF8 string to some maximum length.
Definition: string.cpp:527
StrEmpty
bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:56
CHAR_TD_PDF
static const char32_t CHAR_TD_PDF
Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
Definition: string_type.h:41
bitmath_func.hpp
StrEndsWithIgnoreCase
bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix)
Check whether the given string ends with the given suffix, ignoring case.
Definition: string.cpp:340
FormatArrayAsHex
std::string FormatArrayAsHex(std::span< const byte > data)
Format a byte array into a continuous hex string.
Definition: string.cpp:88
StrStartsWithIgnoreCase
bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix, ignoring case.
Definition: string.cpp:300
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:378
IsTextDirectionChar
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
Definition: string_func.h:216
CHAR_TD_RLE
static const char32_t CHAR_TD_RLE
The following text is embedded right-to-left.
Definition: string_type.h:38
CHAR_TD_LRO
static const char32_t CHAR_TD_LRO
Force the following characters to be treated as left-to-right characters.
Definition: string_type.h:39
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
CHAR_TD_LRE
static const char32_t CHAR_TD_LRE
The following text is embedded left-to-right.
Definition: string_type.h:37
string_type.h
CaseInsensitiveComparator
Case insensitive comparator for strings, for example for use in std::map.
Definition: string_func.h:45
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:44
strecpy
char * strecpy(char *dst, const char *src, const char *last) NOACCESS(3)
Copies characters from one buffer to another.
Definition: string.cpp:65
StrNaturalContainsIgnoreCase
bool StrNaturalContainsIgnoreCase(const std::string_view str, const std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case insensi...
Definition: string.cpp:683
Utf16IsLeadSurrogate
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition: string_func.h:170
Utf8EncodedCharLen
int8_t Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Definition: string_func.h:123
Utf8Decode
size_t Utf8Decode(char32_t *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:438
IsWhitespace
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:248
CHAR_TD_RLM
static const char32_t CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition: string_type.h:36
StrNaturalContains
bool StrNaturalContains(const std::string_view str, const std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case sensiti...
Definition: string.cpp:656
Utf8Encode
size_t Utf8Encode(T buf, char32_t c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:479
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:46
StrEqualsIgnoreCase
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition: string.cpp:366
StrCompareIgnoreCase
int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
Definition: string.cpp:353
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:24
ConvertHexToBytes
bool ConvertHexToBytes(std::string_view hex, std::span< uint8_t > bytes)
Convert a hex-string to a byte-array, while validating it was actually hex.
Definition: string.cpp:730
CHAR_TD_LRM
static const char32_t CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition: string_type.h:35