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/>.
9 * @file string_func.h Functions related to low-level strings.
17 #include "core/bitmath_func.hpp"
18 #include "string_type.h"
20 void strecpy(std::span
<char> dst
, std::string_view src
);
22 std::string
FormatArrayAsHex(std::span
<const uint8_t> data
);
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
);
28 bool strtolower(std::string
&str
, std::string::size_type offs
= 0);
30 [[nodiscard
]] bool StrValid(std::span
<const char> str
);
31 void StrTrimInPlace(std::string
&str
);
32 std::string_view
StrTrimView(std::string_view str
);
34 [[nodiscard
]] bool StrStartsWithIgnoreCase(std::string_view str
, const std::string_view prefix
);
35 [[nodiscard
]] bool StrEndsWithIgnoreCase(std::string_view str
, const std::string_view suffix
);
37 [[nodiscard
]] int StrCompareIgnoreCase(const std::string_view str1
, const std::string_view str2
);
38 [[nodiscard
]] bool StrEqualsIgnoreCase(const std::string_view str1
, const std::string_view str2
);
39 [[nodiscard
]] int StrNaturalCompare(std::string_view s1
, std::string_view s2
, bool ignore_garbage_at_front
= false);
40 [[nodiscard
]] bool StrNaturalContains(const std::string_view str
, const std::string_view value
);
41 [[nodiscard
]] bool StrNaturalContainsIgnoreCase(const std::string_view str
, const std::string_view value
);
43 bool ConvertHexToBytes(std::string_view hex
, std::span
<uint8_t> bytes
);
45 /** Case insensitive comparator for strings, for example for use in std::map. */
46 struct CaseInsensitiveComparator
{
47 bool operator()(const std::string_view s1
, const std::string_view s2
) const { return StrCompareIgnoreCase(s1
, s2
) < 0; }
51 * Check if a string buffer is empty.
53 * @param s The pointer to the first element of the buffer
54 * @return true if the buffer starts with the terminating null-character or
55 * if the given pointer points to nullptr else return false
57 inline bool StrEmpty(const char *s
)
59 return s
== nullptr || s
[0] == '\0';
63 * Get the length of a string, within a limited buffer.
65 * @param str The pointer to the first element of the buffer
66 * @param maxlen The maximum size of the buffer
67 * @return The length of the string
69 inline size_t ttd_strnlen(const char *str
, size_t maxlen
)
72 for (t
= str
; static_cast<size_t>(t
- str
) < maxlen
&& *t
!= '\0'; t
++) {}
76 bool IsValidChar(char32_t key
, CharSetFilter afilter
);
78 size_t Utf8Decode(char32_t
*c
, const char *s
);
79 size_t Utf8Encode(char *buf
, char32_t c
);
80 size_t Utf8Encode(std::ostreambuf_iterator
<char> &buf
, char32_t c
);
81 size_t Utf8Encode(std::back_insert_iterator
<std::string
> &buf
, char32_t c
);
82 size_t Utf8TrimString(char *s
, size_t maxlen
);
85 inline char32_t
Utf8Consume(const char **s
)
88 *s
+= Utf8Decode(&c
, *s
);
93 inline char32_t
Utf8Consume(Titr
&s
)
96 s
+= Utf8Decode(&c
, &*s
);
101 * Return the length of a UTF-8 encoded character.
102 * @param c Unicode character.
103 * @return Length of UTF-8 encoding for character.
105 inline int8_t Utf8CharLen(char32_t c
)
107 if (c
< 0x80) return 1;
108 if (c
< 0x800) return 2;
109 if (c
< 0x10000) return 3;
110 if (c
< 0x110000) return 4;
112 /* Invalid valid, we encode as a '?' */
118 * Return the length of an UTF-8 encoded value based on a single char. This
119 * char should be the first byte of the UTF-8 encoding. If not, or encoding
120 * is invalid, return value is 0
121 * @param c char to query length of
122 * @return requested size
124 inline int8_t Utf8EncodedCharLen(char c
)
126 if (GB(c
, 3, 5) == 0x1E) return 4;
127 if (GB(c
, 4, 4) == 0x0E) return 3;
128 if (GB(c
, 5, 3) == 0x06) return 2;
129 if (GB(c
, 7, 1) == 0x00) return 1;
131 /* Invalid UTF8 start encoding */
136 /* Check if the given character is part of a UTF8 sequence */
137 inline bool IsUtf8Part(char c
)
139 return GB(c
, 6, 2) == 2;
143 * Retrieve the previous UNICODE character in an UTF-8 encoded string.
144 * @param s char pointer pointing to (the first char of) the next character
145 * @return a pointer in 's' to the previous UNICODE character's first byte
146 * @note The function should not be used to determine the length of the previous
147 * encoded char because it might be an invalid/corrupt start-sequence
149 inline char *Utf8PrevChar(char *s
)
152 while (IsUtf8Part(*--ret
)) {}
156 inline const char *Utf8PrevChar(const char *s
)
159 while (IsUtf8Part(*--ret
)) {}
163 size_t Utf8StringLength(const char *s
);
164 size_t Utf8StringLength(const std::string
&str
);
167 * Is the given character a lead surrogate code point?
168 * @param c The character to test.
169 * @return True if the character is a lead surrogate code point.
171 inline bool Utf16IsLeadSurrogate(uint c
)
173 return c
>= 0xD800 && c
<= 0xDBFF;
177 * Is the given character a lead surrogate code point?
178 * @param c The character to test.
179 * @return True if the character is a lead surrogate code point.
181 inline bool Utf16IsTrailSurrogate(uint c
)
183 return c
>= 0xDC00 && c
<= 0xDFFF;
187 * Convert an UTF-16 surrogate pair to the corresponding Unicode character.
188 * @param lead Lead surrogate code point.
189 * @param trail Trail surrogate code point.
190 * @return Decoded Unicode character.
192 inline char32_t
Utf16DecodeSurrogate(uint lead
, uint trail
)
194 return 0x10000 + (((lead
- 0xD800) << 10) | (trail
- 0xDC00));
198 * Decode an UTF-16 character.
199 * @param c Pointer to one or two UTF-16 code points.
200 * @return Decoded Unicode character.
202 inline char32_t
Utf16DecodeChar(const uint16_t *c
)
204 if (Utf16IsLeadSurrogate(c
[0])) {
205 return Utf16DecodeSurrogate(c
[0], c
[1]);
212 * Is the given character a text direction character.
213 * @param c The character to test.
214 * @return true iff the character is used to influence
215 * the text direction.
217 inline bool IsTextDirectionChar(char32_t c
)
234 inline bool IsPrintable(char32_t c
)
236 if (c
< 0x20) return false;
237 if (c
< 0xE000) return true;
238 if (c
< 0xE200) return false;
243 * Check whether UNICODE character is whitespace or not, i.e. whether
244 * this is a potential line-break character.
245 * @param c UNICODE character to check
246 * @return a boolean value whether 'c' is a whitespace character or not
247 * @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
249 inline bool IsWhitespace(char32_t c
)
251 return c
== 0x0020 /* SPACE */ || c
== 0x3000; /* IDEOGRAPHIC SPACE */
254 /* Needed for NetBSD version (so feature) testing */
255 #if defined(__NetBSD__) || defined(__FreeBSD__)
256 #include <sys/param.h>
259 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
260 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
261 # undef DEFINE_STRCASESTR
263 # define DEFINE_STRCASESTR
264 char *strcasestr(const char *haystack
, const char *needle
);
265 #endif /* strcasestr is available */
267 #endif /* STRING_FUNC_H */