Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / string_func.h
blobdf487f12d22e35205b2421f4b9a6a970468c5526
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.
12 #ifndef STRING_FUNC_H
13 #define STRING_FUNC_H
15 #include <iosfwd>
17 #include "core/bitmath_func.hpp"
18 #include "string_type.h"
20 char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
22 std::string FormatArrayAsHex(std::span<const byte> 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(const char *str, const char *last) NOACCESS(2);
31 void StrTrimInPlace(std::string &str);
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);
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);
42 bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
44 /** Case insensitive comparator for strings, for example for use in std::map. */
45 struct CaseInsensitiveComparator {
46 bool operator()(const std::string_view s1, const std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
49 /**
50 * Check if a string buffer is empty.
52 * @param s The pointer to the first element of the buffer
53 * @return true if the buffer starts with the terminating null-character or
54 * if the given pointer points to nullptr else return false
56 inline bool StrEmpty(const char *s)
58 return s == nullptr || s[0] == '\0';
61 /**
62 * Get the length of a string, within a limited buffer.
64 * @param str The pointer to the first element of the buffer
65 * @param maxlen The maximum size of the buffer
66 * @return The length of the string
68 inline size_t ttd_strnlen(const char *str, size_t maxlen)
70 const char *t;
71 for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
72 return t - str;
75 bool IsValidChar(char32_t key, CharSetFilter afilter);
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);
84 inline char32_t Utf8Consume(const char **s)
86 char32_t c;
87 *s += Utf8Decode(&c, *s);
88 return c;
91 template <class Titr>
92 inline char32_t Utf8Consume(Titr &s)
94 char32_t c;
95 s += Utf8Decode(&c, &*s);
96 return c;
99 /**
100 * Return the length of a UTF-8 encoded character.
101 * @param c Unicode character.
102 * @return Length of UTF-8 encoding for character.
104 inline int8_t Utf8CharLen(char32_t c)
106 if (c < 0x80) return 1;
107 if (c < 0x800) return 2;
108 if (c < 0x10000) return 3;
109 if (c < 0x110000) return 4;
111 /* Invalid valid, we encode as a '?' */
112 return 1;
117 * Return the length of an UTF-8 encoded value based on a single char. This
118 * char should be the first byte of the UTF-8 encoding. If not, or encoding
119 * is invalid, return value is 0
120 * @param c char to query length of
121 * @return requested size
123 inline int8_t Utf8EncodedCharLen(char c)
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;
130 /* Invalid UTF8 start encoding */
131 return 0;
135 /* Check if the given character is part of a UTF8 sequence */
136 inline bool IsUtf8Part(char c)
138 return GB(c, 6, 2) == 2;
142 * Retrieve the previous UNICODE character in an UTF-8 encoded string.
143 * @param s char pointer pointing to (the first char of) the next character
144 * @return a pointer in 's' to the previous UNICODE character's first byte
145 * @note The function should not be used to determine the length of the previous
146 * encoded char because it might be an invalid/corrupt start-sequence
148 inline char *Utf8PrevChar(char *s)
150 char *ret = s;
151 while (IsUtf8Part(*--ret)) {}
152 return ret;
155 inline const char *Utf8PrevChar(const char *s)
157 const char *ret = s;
158 while (IsUtf8Part(*--ret)) {}
159 return ret;
162 size_t Utf8StringLength(const char *s);
163 size_t Utf8StringLength(const std::string &str);
166 * Is the given character a lead surrogate code point?
167 * @param c The character to test.
168 * @return True if the character is a lead surrogate code point.
170 inline bool Utf16IsLeadSurrogate(uint c)
172 return c >= 0xD800 && c <= 0xDBFF;
176 * Is the given character a lead surrogate code point?
177 * @param c The character to test.
178 * @return True if the character is a lead surrogate code point.
180 inline bool Utf16IsTrailSurrogate(uint c)
182 return c >= 0xDC00 && c <= 0xDFFF;
186 * Convert an UTF-16 surrogate pair to the corresponding Unicode character.
187 * @param lead Lead surrogate code point.
188 * @param trail Trail surrogate code point.
189 * @return Decoded Unicode character.
191 inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
193 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
197 * Decode an UTF-16 character.
198 * @param c Pointer to one or two UTF-16 code points.
199 * @return Decoded Unicode character.
201 inline char32_t Utf16DecodeChar(const uint16_t *c)
203 if (Utf16IsLeadSurrogate(c[0])) {
204 return Utf16DecodeSurrogate(c[0], c[1]);
205 } else {
206 return *c;
211 * Is the given character a text direction character.
212 * @param c The character to test.
213 * @return true iff the character is used to influence
214 * the text direction.
216 inline bool IsTextDirectionChar(char32_t c)
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;
228 default:
229 return false;
233 inline bool IsPrintable(char32_t c)
235 if (c < 0x20) return false;
236 if (c < 0xE000) return true;
237 if (c < 0xE200) return false;
238 return true;
242 * Check whether UNICODE character is whitespace or not, i.e. whether
243 * this is a potential line-break character.
244 * @param c UNICODE character to check
245 * @return a boolean value whether 'c' is a whitespace character or not
246 * @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
248 inline bool IsWhitespace(char32_t c)
250 return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
253 /* Needed for NetBSD version (so feature) testing */
254 #if defined(__NetBSD__) || defined(__FreeBSD__)
255 #include <sys/param.h>
256 #endif
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 */
266 #endif /* STRING_FUNC_H */