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 
8 /**
9  * @file string_func.h Functions related to low-level strings.
10  *
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.
22  */
23 
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 "string_type.h"
32 
33 char *strecat(char *dst, const char *src, const char *last) NOACCESS(3);
34 char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
35 char *stredup(const char *src, const char *last = nullptr) NOACCESS(2);
36 
37 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4) NOACCESS(2);
38 int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap) WARN_FORMAT(3, 0) NOACCESS(2);
39 
40 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
41 
42 void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
43 [[nodiscard]] std::string StrMakeValid(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
44 void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
45 
46 void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
47 void str_strip_colours(char *str);
48 bool strtolower(char *str);
49 bool strtolower(std::string &str, std::string::size_type offs = 0);
50 
51 bool StrValid(const char *str, const char *last) NOACCESS(2);
52 void StrTrimInPlace(std::string &str);
53 
54 bool StrStartsWith(const std::string_view str, const std::string_view prefix);
55 bool StrEndsWith(const std::string_view str, const std::string_view suffix);
56 
57 /**
58  * Check if a string buffer is empty.
59  *
60  * @param s The pointer to the first element of the buffer
61  * @return true if the buffer starts with the terminating null-character or
62  *         if the given pointer points to nullptr else return false
63  */
StrEmpty(const char * s)64 static inline bool StrEmpty(const char *s)
65 {
66 	return s == nullptr || s[0] == '\0';
67 }
68 
69 /**
70  * Get the length of a string, within a limited buffer.
71  *
72  * @param str The pointer to the first element of the buffer
73  * @param maxlen The maximum size of the buffer
74  * @return The length of the string
75  */
ttd_strnlen(const char * str,size_t maxlen)76 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
77 {
78 	const char *t;
79 	for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
80 	return t - str;
81 }
82 
83 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
84 
85 bool IsValidChar(WChar key, CharSetFilter afilter);
86 
87 size_t Utf8Decode(WChar *c, const char *s);
88 size_t Utf8Encode(char *buf, WChar c);
89 size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, WChar c);
90 size_t Utf8TrimString(char *s, size_t maxlen);
91 
92 
Utf8Consume(const char ** s)93 static inline WChar Utf8Consume(const char **s)
94 {
95 	WChar c;
96 	*s += Utf8Decode(&c, *s);
97 	return c;
98 }
99 
100 template <class Titr>
Utf8Consume(Titr & s)101 static inline WChar Utf8Consume(Titr &s)
102 {
103 	WChar c;
104 	s += Utf8Decode(&c, &*s);
105 	return c;
106 }
107 
108 /**
109  * Return the length of a UTF-8 encoded character.
110  * @param c Unicode character.
111  * @return Length of UTF-8 encoding for character.
112  */
Utf8CharLen(WChar c)113 static inline int8 Utf8CharLen(WChar c)
114 {
115 	if (c < 0x80)       return 1;
116 	if (c < 0x800)      return 2;
117 	if (c < 0x10000)    return 3;
118 	if (c < 0x110000)   return 4;
119 
120 	/* Invalid valid, we encode as a '?' */
121 	return 1;
122 }
123 
124 
125 /**
126  * Return the length of an UTF-8 encoded value based on a single char. This
127  * char should be the first byte of the UTF-8 encoding. If not, or encoding
128  * is invalid, return value is 0
129  * @param c char to query length of
130  * @return requested size
131  */
Utf8EncodedCharLen(char c)132 static inline int8 Utf8EncodedCharLen(char c)
133 {
134 	if (GB(c, 3, 5) == 0x1E) return 4;
135 	if (GB(c, 4, 4) == 0x0E) return 3;
136 	if (GB(c, 5, 3) == 0x06) return 2;
137 	if (GB(c, 7, 1) == 0x00) return 1;
138 
139 	/* Invalid UTF8 start encoding */
140 	return 0;
141 }
142 
143 
144 /* Check if the given character is part of a UTF8 sequence */
IsUtf8Part(char c)145 static inline bool IsUtf8Part(char c)
146 {
147 	return GB(c, 6, 2) == 2;
148 }
149 
150 /**
151  * Retrieve the previous UNICODE character in an UTF-8 encoded string.
152  * @param s char pointer pointing to (the first char of) the next character
153  * @return a pointer in 's' to the previous UNICODE character's first byte
154  * @note The function should not be used to determine the length of the previous
155  * encoded char because it might be an invalid/corrupt start-sequence
156  */
Utf8PrevChar(char * s)157 static inline char *Utf8PrevChar(char *s)
158 {
159 	char *ret = s;
160 	while (IsUtf8Part(*--ret)) {}
161 	return ret;
162 }
163 
Utf8PrevChar(const char * s)164 static inline const char *Utf8PrevChar(const char *s)
165 {
166 	const char *ret = s;
167 	while (IsUtf8Part(*--ret)) {}
168 	return ret;
169 }
170 
171 size_t Utf8StringLength(const char *s);
172 size_t Utf8StringLength(const std::string &str);
173 
174 /**
175  * Is the given character a lead surrogate code point?
176  * @param c The character to test.
177  * @return True if the character is a lead surrogate code point.
178  */
Utf16IsLeadSurrogate(uint c)179 static inline bool Utf16IsLeadSurrogate(uint c)
180 {
181 	return c >= 0xD800 && c <= 0xDBFF;
182 }
183 
184 /**
185  * Is the given character a lead surrogate code point?
186  * @param c The character to test.
187  * @return True if the character is a lead surrogate code point.
188  */
Utf16IsTrailSurrogate(uint c)189 static inline bool Utf16IsTrailSurrogate(uint c)
190 {
191 	return c >= 0xDC00 && c <= 0xDFFF;
192 }
193 
194 /**
195  * Convert an UTF-16 surrogate pair to the corresponding Unicode character.
196  * @param lead Lead surrogate code point.
197  * @param trail Trail surrogate code point.
198  * @return Decoded Unicode character.
199  */
Utf16DecodeSurrogate(uint lead,uint trail)200 static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
201 {
202 	return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
203 }
204 
205 /**
206  * Decode an UTF-16 character.
207  * @param c Pointer to one or two UTF-16 code points.
208  * @return Decoded Unicode character.
209  */
Utf16DecodeChar(const uint16 * c)210 static inline WChar Utf16DecodeChar(const uint16 *c)
211 {
212 	if (Utf16IsLeadSurrogate(c[0])) {
213 		return Utf16DecodeSurrogate(c[0], c[1]);
214 	} else {
215 		return *c;
216 	}
217 }
218 
219 /**
220  * Is the given character a text direction character.
221  * @param c The character to test.
222  * @return true iff the character is used to influence
223  *         the text direction.
224  */
IsTextDirectionChar(WChar c)225 static inline bool IsTextDirectionChar(WChar c)
226 {
227 	switch (c) {
228 		case CHAR_TD_LRM:
229 		case CHAR_TD_RLM:
230 		case CHAR_TD_LRE:
231 		case CHAR_TD_RLE:
232 		case CHAR_TD_LRO:
233 		case CHAR_TD_RLO:
234 		case CHAR_TD_PDF:
235 			return true;
236 
237 		default:
238 			return false;
239 	}
240 }
241 
IsPrintable(WChar c)242 static inline bool IsPrintable(WChar c)
243 {
244 	if (c < 0x20)   return false;
245 	if (c < 0xE000) return true;
246 	if (c < 0xE200) return false;
247 	return true;
248 }
249 
250 /**
251  * Check whether UNICODE character is whitespace or not, i.e. whether
252  * this is a potential line-break character.
253  * @param c UNICODE character to check
254  * @return a boolean value whether 'c' is a whitespace character or not
255  * @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
256  */
IsWhitespace(WChar c)257 static inline bool IsWhitespace(WChar c)
258 {
259 	return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
260 }
261 
262 /* Needed for NetBSD version (so feature) testing */
263 #if defined(__NetBSD__) || defined(__FreeBSD__)
264 #include <sys/param.h>
265 #endif
266 
267 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
268 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
269 #	undef DEFINE_STRCASESTR
270 #else
271 #	define DEFINE_STRCASESTR
272 char *strcasestr(const char *haystack, const char *needle);
273 #endif /* strcasestr is available */
274 
275 int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front = false);
276 
277 #endif /* STRING_FUNC_H */
278