Feature: Purchase land multiple tiles at a time
[openttd-github.git] / src / string_func.h
bloba5d3499c76e2ac32526a7212418c2f3cf029065a
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 */
8 /**
9 * @file string_func.h Functions related to low-level strings.
11 * @note Be aware of "dangerous" string functions; string functions that
12 * have behaviour that could easily cause buffer overruns and such:
13 * - strncpy: does not '\0' terminate when input string is longer than
14 * the size of the output string. Use strecpy instead.
15 * - [v]snprintf: returns the length of the string as it would be written
16 * when the output is large enough, so it can be more than the size of
17 * the buffer and than can underflow size_t (uint-ish) which makes all
18 * subsequent snprintf alikes write outside of the buffer. Use
19 * [v]seprintf instead; it will return the number of bytes actually
20 * added so no [v]seprintf will cause outside of bounds writes.
21 * - [v]sprintf: does not bounds checking: use [v]seprintf instead.
24 #ifndef STRING_FUNC_H
25 #define STRING_FUNC_H
27 #include <stdarg.h>
28 #include <iosfwd>
30 #include "core/bitmath_func.hpp"
31 #include "core/span_type.hpp"
32 #include "string_type.h"
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);
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);
41 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
43 std::string FormatArrayAsHex(span<const byte> data);
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);
47 void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
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);
54 bool StrValid(const char *str, const char *last) NOACCESS(2);
55 void StrTrimInPlace(std::string &str);
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);
60 /**
61 * Check if a string buffer is empty.
63 * @param s The pointer to the first element of the buffer
64 * @return true if the buffer starts with the terminating null-character or
65 * if the given pointer points to nullptr else return false
67 static inline bool StrEmpty(const char *s)
69 return s == nullptr || s[0] == '\0';
72 /**
73 * Get the length of a string, within a limited buffer.
75 * @param str The pointer to the first element of the buffer
76 * @param maxlen The maximum size of the buffer
77 * @return The length of the string
79 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
81 const char *t;
82 for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
83 return t - str;
86 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
88 bool IsValidChar(WChar key, CharSetFilter afilter);
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);
96 static inline WChar Utf8Consume(const char **s)
98 WChar c;
99 *s += Utf8Decode(&c, *s);
100 return c;
103 template <class Titr>
104 static inline WChar Utf8Consume(Titr &s)
106 WChar c;
107 s += Utf8Decode(&c, &*s);
108 return c;
112 * Return the length of a UTF-8 encoded character.
113 * @param c Unicode character.
114 * @return Length of UTF-8 encoding for character.
116 static inline int8 Utf8CharLen(WChar c)
118 if (c < 0x80) return 1;
119 if (c < 0x800) return 2;
120 if (c < 0x10000) return 3;
121 if (c < 0x110000) return 4;
123 /* Invalid valid, we encode as a '?' */
124 return 1;
129 * Return the length of an UTF-8 encoded value based on a single char. This
130 * char should be the first byte of the UTF-8 encoding. If not, or encoding
131 * is invalid, return value is 0
132 * @param c char to query length of
133 * @return requested size
135 static inline int8 Utf8EncodedCharLen(char c)
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;
142 /* Invalid UTF8 start encoding */
143 return 0;
147 /* Check if the given character is part of a UTF8 sequence */
148 static inline bool IsUtf8Part(char c)
150 return GB(c, 6, 2) == 2;
154 * Retrieve the previous UNICODE character in an UTF-8 encoded string.
155 * @param s char pointer pointing to (the first char of) the next character
156 * @return a pointer in 's' to the previous UNICODE character's first byte
157 * @note The function should not be used to determine the length of the previous
158 * encoded char because it might be an invalid/corrupt start-sequence
160 static inline char *Utf8PrevChar(char *s)
162 char *ret = s;
163 while (IsUtf8Part(*--ret)) {}
164 return ret;
167 static inline const char *Utf8PrevChar(const char *s)
169 const char *ret = s;
170 while (IsUtf8Part(*--ret)) {}
171 return ret;
174 size_t Utf8StringLength(const char *s);
175 size_t Utf8StringLength(const std::string &str);
178 * Is the given character a lead surrogate code point?
179 * @param c The character to test.
180 * @return True if the character is a lead surrogate code point.
182 static inline bool Utf16IsLeadSurrogate(uint c)
184 return c >= 0xD800 && c <= 0xDBFF;
188 * Is the given character a lead surrogate code point?
189 * @param c The character to test.
190 * @return True if the character is a lead surrogate code point.
192 static inline bool Utf16IsTrailSurrogate(uint c)
194 return c >= 0xDC00 && c <= 0xDFFF;
198 * Convert an UTF-16 surrogate pair to the corresponding Unicode character.
199 * @param lead Lead surrogate code point.
200 * @param trail Trail surrogate code point.
201 * @return Decoded Unicode character.
203 static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
205 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
209 * Decode an UTF-16 character.
210 * @param c Pointer to one or two UTF-16 code points.
211 * @return Decoded Unicode character.
213 static inline WChar Utf16DecodeChar(const uint16 *c)
215 if (Utf16IsLeadSurrogate(c[0])) {
216 return Utf16DecodeSurrogate(c[0], c[1]);
217 } else {
218 return *c;
223 * Is the given character a text direction character.
224 * @param c The character to test.
225 * @return true iff the character is used to influence
226 * the text direction.
228 static inline bool IsTextDirectionChar(WChar c)
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;
240 default:
241 return false;
245 static inline bool IsPrintable(WChar c)
247 if (c < 0x20) return false;
248 if (c < 0xE000) return true;
249 if (c < 0xE200) return false;
250 return true;
254 * Check whether UNICODE character is whitespace or not, i.e. whether
255 * this is a potential line-break character.
256 * @param c UNICODE character to check
257 * @return a boolean value whether 'c' is a whitespace character or not
258 * @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
260 static inline bool IsWhitespace(WChar c)
262 return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
265 /* Needed for NetBSD version (so feature) testing */
266 #if defined(__NetBSD__) || defined(__FreeBSD__)
267 #include <sys/param.h>
268 #endif
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 */
278 int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front = false);
280 #endif /* STRING_FUNC_H */