Add: Overlay cargo icon in vehicle/depot list when holding shift+ctrl. (#12938)
[openttd-github.git] / src / string.cpp
blob727fd806813fdfaa4e03af9528350d21a117ad77
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 /** @file string.cpp Handling of C-type strings (char*). */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "core/alloc_func.hpp"
13 #include "core/math_func.hpp"
14 #include "error_func.h"
15 #include "string_func.h"
16 #include "string_base.h"
18 #include "table/control_codes.h"
20 #include <sstream>
21 #include <iomanip>
23 #ifdef _MSC_VER
24 # define strncasecmp strnicmp
25 #endif
27 #ifdef _WIN32
28 # include "os/windows/win32.h"
29 #endif
31 #ifdef WITH_UNISCRIBE
32 # include "os/windows/string_uniscribe.h"
33 #endif
35 #ifdef WITH_ICU_I18N
36 /* Required by StrNaturalCompare. */
37 # include <unicode/ustring.h>
38 # include "language.h"
39 # include "gfx_func.h"
40 #endif /* WITH_ICU_I18N */
42 #if defined(WITH_COCOA)
43 # include "os/macosx/string_osx.h"
44 #endif
46 #include "safeguards.h"
49 /**
50 * Copies characters from one buffer to another.
52 * Copies the source string to the destination buffer with respect of the
53 * terminating null-character and the size of the destination buffer.
55 * @note usage: strecpy(dst, src);
57 * @param dst The destination buffer
58 * @param src The buffer containing the string to copy
60 void strecpy(std::span<char> dst, std::string_view src)
62 /* Ensure source string fits with NUL terminator; dst must be at least 1 character longer than src. */
63 if (std::empty(dst) || std::size(src) >= std::size(dst) - 1U) {
64 #if defined(STRGEN) || defined(SETTINGSGEN)
65 FatalError("String too long for destination buffer");
66 #else /* STRGEN || SETTINGSGEN */
67 Debug(misc, 0, "String too long for destination buffer");
68 src = src.substr(0, std::size(dst) - 1U);
69 #endif /* STRGEN || SETTINGSGEN */
72 auto it = std::copy(std::begin(src), std::end(src), std::begin(dst));
73 *it = '\0';
76 /**
77 * Format a byte array into a continuous hex string.
78 * @param data Array to format
79 * @return Converted string.
81 std::string FormatArrayAsHex(std::span<const uint8_t> data)
83 std::string str;
84 str.reserve(data.size() * 2 + 1);
86 for (auto b : data) {
87 fmt::format_to(std::back_inserter(str), "{:02X}", b);
90 return str;
94 /**
95 * Copies the valid (UTF-8) characters from \c str up to \c last to the \c dst.
96 * Depending on the \c settings invalid characters can be replaced with a
97 * question mark, as well as determining what characters are deemed invalid.
99 * It is allowed for \c dst to be the same as \c src, in which case the string
100 * is make valid in place.
101 * @param dst The destination to write to.
102 * @param str The string to validate.
103 * @param last The last valid character of str.
104 * @param settings The settings for the string validation.
106 template <class T>
107 static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
109 /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
111 while (str <= last && *str != '\0') {
112 size_t len = Utf8EncodedCharLen(*str);
113 char32_t c;
114 /* If the first byte does not look like the first byte of an encoded
115 * character, i.e. encoded length is 0, then this byte is definitely bad
116 * and it should be skipped.
117 * When the first byte looks like the first byte of an encoded character,
118 * then the remaining bytes in the string are checked whether the whole
119 * encoded character can be there. If that is not the case, this byte is
120 * skipped.
121 * Finally we attempt to decode the encoded character, which does certain
122 * extra validations to see whether the correct number of bytes were used
123 * to encode the character. If that is not the case, the byte is probably
124 * invalid and it is skipped. We could emit a question mark, but then the
125 * logic below cannot just copy bytes, it would need to re-encode the
126 * decoded characters as the length in bytes may have changed.
128 * The goals here is to get as much valid Utf8 encoded characters from the
129 * source string to the destination string.
131 * Note: a multi-byte encoded termination ('\0') will trigger the encoded
132 * char length and the decoded length to differ, so it will be ignored as
133 * invalid character data. If it were to reach the termination, then we
134 * would also reach the "last" byte of the string and a normal '\0'
135 * termination will be placed after it.
137 if (len == 0 || str + len > last + 1 || len != Utf8Decode(&c, str)) {
138 /* Maybe the next byte is still a valid character? */
139 str++;
140 continue;
143 if ((IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END)) || ((settings & SVS_ALLOW_CONTROL_CODE) != 0 && c == SCC_ENCODED)) {
144 /* Copy the character back. Even if dst is current the same as str
145 * (i.e. no characters have been changed) this is quicker than
146 * moving the pointers ahead by len */
147 do {
148 *dst++ = *str++;
149 } while (--len != 0);
150 } else if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\n') {
151 *dst++ = *str++;
152 } else {
153 if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') {
154 str += len;
155 continue;
157 str += len;
158 if ((settings & SVS_REPLACE_TAB_CR_NL_WITH_SPACE) != 0 && (c == '\r' || c == '\n' || c == '\t')) {
159 /* Replace the tab, carriage return or newline with a space. */
160 *dst++ = ' ';
161 } else if ((settings & SVS_REPLACE_WITH_QUESTION_MARK) != 0) {
162 /* Replace the undesirable character with a question mark */
163 *dst++ = '?';
168 /* String termination, if needed, is left to the caller of this function. */
172 * Scans the string for invalid characters and replaces then with a
173 * question mark '?' (if not ignored).
174 * @param str The string to validate.
175 * @param last The last valid character of str.
176 * @param settings The settings for the string validation.
178 void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
180 char *dst = str;
181 StrMakeValid(dst, str, last, settings);
182 *dst = '\0';
186 * Scans the string for invalid characters and replaces then with a
187 * question mark '?' (if not ignored).
188 * Only use this function when you are sure the string ends with a '\0';
189 * otherwise use StrMakeValidInPlace(str, last, settings) variant.
190 * @param str The string (of which you are sure ends with '\0') to validate.
192 void StrMakeValidInPlace(char *str, StringValidationSettings settings)
194 /* We know it is '\0' terminated. */
195 StrMakeValidInPlace(str, str + strlen(str), settings);
199 * Copies the valid (UTF-8) characters from \c str to the returned string.
200 * Depending on the \c settings invalid characters can be replaced with a
201 * question mark, as well as determining what characters are deemed invalid.
202 * @param str The string to validate.
203 * @param settings The settings for the string validation.
205 std::string StrMakeValid(std::string_view str, StringValidationSettings settings)
207 if (str.empty()) return {};
209 auto buf = str.data();
210 auto last = buf + str.size() - 1;
212 std::ostringstream dst;
213 std::ostreambuf_iterator<char> dst_iter(dst);
214 StrMakeValid(dst_iter, buf, last, settings);
216 return dst.str();
220 * Checks whether the given string is valid, i.e. contains only
221 * valid (printable) characters and is properly terminated.
222 * @note std::span is used instead of std::string_view as we are validating fixed-length string buffers, and
223 * std::string_view's constructor will assume a C-string that ends with a NUL terminator, which is one of the things
224 * we are checking.
225 * @param str Span of chars to validate.
227 bool StrValid(std::span<const char> str)
229 /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
230 auto it = std::begin(str);
231 auto last = std::prev(std::end(str));
233 while (it <= last && *it != '\0') {
234 size_t len = Utf8EncodedCharLen(*it);
235 /* Encoded length is 0 if the character isn't known.
236 * The length check is needed to prevent Utf8Decode to read
237 * over the terminating '\0' if that happens to be placed
238 * within the encoding of an UTF8 character. */
239 if (len == 0 || it + len > last) return false;
241 char32_t c;
242 len = Utf8Decode(&c, &*it);
243 if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
244 return false;
247 it += len;
250 return *it == '\0';
254 * Trim the spaces from given string in place, i.e. the string buffer that
255 * is passed will be modified whenever spaces exist in the given string.
256 * When there are spaces at the begin, the whole string is moved forward
257 * and when there are spaces at the back the '\0' termination is moved.
258 * @param str The string to perform the in place trimming on.
260 void StrTrimInPlace(std::string &str)
262 str = StrTrimView(str);
265 std::string_view StrTrimView(std::string_view str)
267 size_t first_pos = str.find_first_not_of(' ');
268 if (first_pos == std::string::npos) {
269 return std::string_view{};
271 size_t last_pos = str.find_last_not_of(' ');
272 return str.substr(first_pos, last_pos - first_pos + 1);
276 * Check whether the given string starts with the given prefix, ignoring case.
277 * @param str The string to look at.
278 * @param prefix The prefix to look for.
279 * @return True iff the begin of the string is the same as the prefix, ignoring case.
281 bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix)
283 if (str.size() < prefix.size()) return false;
284 return StrEqualsIgnoreCase(str.substr(0, prefix.size()), prefix);
287 /** Case insensitive implementation of the standard character type traits. */
288 struct CaseInsensitiveCharTraits : public std::char_traits<char> {
289 static bool eq(char c1, char c2) { return toupper(c1) == toupper(c2); }
290 static bool ne(char c1, char c2) { return toupper(c1) != toupper(c2); }
291 static bool lt(char c1, char c2) { return toupper(c1) < toupper(c2); }
293 static int compare(const char *s1, const char *s2, size_t n)
295 while (n-- != 0) {
296 if (toupper(*s1) < toupper(*s2)) return -1;
297 if (toupper(*s1) > toupper(*s2)) return 1;
298 ++s1; ++s2;
300 return 0;
303 static const char *find(const char *s, size_t n, char a)
305 for (; n > 0; --n, ++s) {
306 if (toupper(*s) == toupper(a)) return s;
308 return nullptr;
312 /** Case insensitive string view. */
313 typedef std::basic_string_view<char, CaseInsensitiveCharTraits> CaseInsensitiveStringView;
316 * Check whether the given string ends with the given suffix, ignoring case.
317 * @param str The string to look at.
318 * @param suffix The suffix to look for.
319 * @return True iff the end of the string is the same as the suffix, ignoring case.
321 bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix)
323 if (str.size() < suffix.size()) return false;
324 return StrEqualsIgnoreCase(str.substr(str.size() - suffix.size()), suffix);
328 * Compares two string( view)s, while ignoring the case of the characters.
329 * @param str1 The first string.
330 * @param str2 The second string.
331 * @return Less than zero if str1 < str2, zero if str1 == str2, greater than
332 * zero if str1 > str2. All ignoring the case of the characters.
334 int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2)
336 CaseInsensitiveStringView ci_str1{ str1.data(), str1.size() };
337 CaseInsensitiveStringView ci_str2{ str2.data(), str2.size() };
338 return ci_str1.compare(ci_str2);
342 * Compares two string( view)s for equality, while ignoring the case of the characters.
343 * @param str1 The first string.
344 * @param str2 The second string.
345 * @return True iff both strings are equal, barring the case of the characters.
347 bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
349 if (str1.size() != str2.size()) return false;
350 return StrCompareIgnoreCase(str1, str2) == 0;
354 * Get the length of an UTF-8 encoded string in number of characters
355 * and thus not the number of bytes that the encoded string contains.
356 * @param s The string to get the length for.
357 * @return The length of the string in characters.
359 size_t Utf8StringLength(const char *s)
361 size_t len = 0;
362 const char *t = s;
363 while (Utf8Consume(&t) != 0) len++;
364 return len;
368 * Get the length of an UTF-8 encoded string in number of characters
369 * and thus not the number of bytes that the encoded string contains.
370 * @param s The string to get the length for.
371 * @return The length of the string in characters.
373 size_t Utf8StringLength(const std::string &str)
375 return Utf8StringLength(str.c_str());
378 bool strtolower(std::string &str, std::string::size_type offs)
380 bool changed = false;
381 for (auto ch = str.begin() + offs; ch != str.end(); ++ch) {
382 auto new_ch = static_cast<char>(tolower(static_cast<unsigned char>(*ch)));
383 changed |= new_ch != *ch;
384 *ch = new_ch;
386 return changed;
390 * Only allow certain keys. You can define the filter to be used. This makes
391 * sure no invalid keys can get into an editbox, like BELL.
392 * @param key character to be checked
393 * @param afilter the filter to use
394 * @return true or false depending if the character is printable/valid or not
396 bool IsValidChar(char32_t key, CharSetFilter afilter)
398 switch (afilter) {
399 case CS_ALPHANUMERAL: return IsPrintable(key);
400 case CS_NUMERAL: return (key >= '0' && key <= '9');
401 case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
402 case CS_NUMERAL_SIGNED: return (key >= '0' && key <= '9') || key == '-';
403 case CS_ALPHA: return IsPrintable(key) && !(key >= '0' && key <= '9');
404 case CS_HEXADECIMAL: return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
405 default: NOT_REACHED();
410 /* UTF-8 handling routines */
414 * Decode and consume the next UTF-8 encoded character.
415 * @param c Buffer to place decoded character.
416 * @param s Character stream to retrieve character from.
417 * @return Number of characters in the sequence.
419 size_t Utf8Decode(char32_t *c, const char *s)
421 assert(c != nullptr);
423 if (!HasBit(s[0], 7)) {
424 /* Single byte character: 0xxxxxxx */
425 *c = s[0];
426 return 1;
427 } else if (GB(s[0], 5, 3) == 6) {
428 if (IsUtf8Part(s[1])) {
429 /* Double byte character: 110xxxxx 10xxxxxx */
430 *c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
431 if (*c >= 0x80) return 2;
433 } else if (GB(s[0], 4, 4) == 14) {
434 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
435 /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
436 *c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
437 if (*c >= 0x800) return 3;
439 } else if (GB(s[0], 3, 5) == 30) {
440 if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
441 /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
442 *c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
443 if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
447 *c = '?';
448 return 1;
453 * Encode a unicode character and place it in the buffer.
454 * @tparam T Type of the buffer.
455 * @param buf Buffer to place character.
456 * @param c Unicode character to encode.
457 * @return Number of characters in the encoded sequence.
459 template <class T>
460 inline size_t Utf8Encode(T buf, char32_t c)
462 if (c < 0x80) {
463 *buf = c;
464 return 1;
465 } else if (c < 0x800) {
466 *buf++ = 0xC0 + GB(c, 6, 5);
467 *buf = 0x80 + GB(c, 0, 6);
468 return 2;
469 } else if (c < 0x10000) {
470 *buf++ = 0xE0 + GB(c, 12, 4);
471 *buf++ = 0x80 + GB(c, 6, 6);
472 *buf = 0x80 + GB(c, 0, 6);
473 return 3;
474 } else if (c < 0x110000) {
475 *buf++ = 0xF0 + GB(c, 18, 3);
476 *buf++ = 0x80 + GB(c, 12, 6);
477 *buf++ = 0x80 + GB(c, 6, 6);
478 *buf = 0x80 + GB(c, 0, 6);
479 return 4;
482 *buf = '?';
483 return 1;
486 size_t Utf8Encode(char *buf, char32_t c)
488 return Utf8Encode<char *>(buf, c);
491 size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, char32_t c)
493 return Utf8Encode<std::ostreambuf_iterator<char> &>(buf, c);
496 size_t Utf8Encode(std::back_insert_iterator<std::string> &buf, char32_t c)
498 return Utf8Encode<std::back_insert_iterator<std::string> &>(buf, c);
502 * Properly terminate an UTF8 string to some maximum length
503 * @param s string to check if it needs additional trimming
504 * @param maxlen the maximum length the buffer can have.
505 * @return the new length in bytes of the string (eg. strlen(new_string))
506 * @note maxlen is the string length _INCLUDING_ the terminating '\0'
508 size_t Utf8TrimString(char *s, size_t maxlen)
510 size_t length = 0;
512 for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
513 size_t len = Utf8EncodedCharLen(*s);
514 /* Silently ignore invalid UTF8 sequences, our only concern trimming */
515 if (len == 0) len = 1;
517 /* Take care when a hard cutoff was made for the string and
518 * the last UTF8 sequence is invalid */
519 if (length + len >= maxlen || (s + len > ptr)) break;
520 s += len;
521 length += len;
524 *s = '\0';
525 return length;
528 #ifdef DEFINE_STRCASESTR
529 char *strcasestr(const char *haystack, const char *needle)
531 size_t hay_len = strlen(haystack);
532 size_t needle_len = strlen(needle);
533 while (hay_len >= needle_len) {
534 if (strncasecmp(haystack, needle, needle_len) == 0) return const_cast<char *>(haystack);
536 haystack++;
537 hay_len--;
540 return nullptr;
542 #endif /* DEFINE_STRCASESTR */
545 * Skip some of the 'garbage' in the string that we don't want to use
546 * to sort on. This way the alphabetical sorting will work better as
547 * we would be actually using those characters instead of some other
548 * characters such as spaces and tildes at the begin of the name.
549 * @param str The string to skip the initial garbage of.
550 * @return The string with the garbage skipped.
552 static std::string_view SkipGarbage(std::string_view str)
554 while (!str.empty() && (str[0] < '0' || IsInsideMM(str[0], ';', '@' + 1) || IsInsideMM(str[0], '[', '`' + 1) || IsInsideMM(str[0], '{', '~' + 1))) str.remove_prefix(1);
555 return str;
559 * Compares two strings using case insensitive natural sort.
561 * @param s1 First string to compare.
562 * @param s2 Second string to compare.
563 * @param ignore_garbage_at_front Skip punctuation characters in the front
564 * @return Less than zero if s1 < s2, zero if s1 == s2, greater than zero if s1 > s2.
566 int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
568 if (ignore_garbage_at_front) {
569 s1 = SkipGarbage(s1);
570 s2 = SkipGarbage(s2);
573 #ifdef WITH_ICU_I18N
574 if (_current_collator) {
575 UErrorCode status = U_ZERO_ERROR;
576 int result = _current_collator->compareUTF8(icu::StringPiece(s1.data(), s1.size()), icu::StringPiece(s2.data(), s2.size()), status);
577 if (U_SUCCESS(status)) return result;
579 #endif /* WITH_ICU_I18N */
581 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
582 int res = OTTDStringCompare(s1, s2);
583 if (res != 0) return res - 2; // Convert to normal C return values.
584 #endif
586 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
587 int res = MacOSStringCompare(s1, s2);
588 if (res != 0) return res - 2; // Convert to normal C return values.
589 #endif
591 /* Do a normal comparison if ICU is missing or if we cannot create a collator. */
592 return StrCompareIgnoreCase(s1, s2);
595 #ifdef WITH_ICU_I18N
597 #include <unicode/stsearch.h>
600 * Search if a string is contained in another string using the current locale.
602 * @param str String to search in.
603 * @param value String to search for.
604 * @param case_insensitive Search case-insensitive.
605 * @return 1 if value was found, 0 if it was not found, or -1 if not supported by the OS.
607 static int ICUStringContains(const std::string_view str, const std::string_view value, bool case_insensitive)
609 if (_current_collator) {
610 std::unique_ptr<icu::RuleBasedCollator> coll(dynamic_cast<icu::RuleBasedCollator *>(_current_collator->clone()));
611 if (coll) {
612 UErrorCode status = U_ZERO_ERROR;
613 coll->setStrength(case_insensitive ? icu::Collator::SECONDARY : icu::Collator::TERTIARY);
614 coll->setAttribute(UCOL_NUMERIC_COLLATION, UCOL_OFF, status);
616 auto u_str = icu::UnicodeString::fromUTF8(icu::StringPiece(str.data(), str.size()));
617 auto u_value = icu::UnicodeString::fromUTF8(icu::StringPiece(value.data(), value.size()));
618 icu::StringSearch u_searcher(u_value, u_str, coll.get(), nullptr, status);
619 if (U_SUCCESS(status)) {
620 auto pos = u_searcher.first(status);
621 if (U_SUCCESS(status)) return pos != USEARCH_DONE ? 1 : 0;
626 return -1;
628 #endif /* WITH_ICU_I18N */
631 * Checks if a string is contained in another string with a locale-aware comparison that is case sensitive.
633 * @param str The string to search in.
634 * @param value The string to search for.
635 * @return True if a match was found.
637 [[nodiscard]] bool StrNaturalContains(const std::string_view str, const std::string_view value)
639 #ifdef WITH_ICU_I18N
640 int res_u = ICUStringContains(str, value, false);
641 if (res_u >= 0) return res_u > 0;
642 #endif /* WITH_ICU_I18N */
644 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
645 int res = Win32StringContains(str, value, false);
646 if (res >= 0) return res > 0;
647 #endif
649 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
650 int res = MacOSStringContains(str, value, false);
651 if (res >= 0) return res > 0;
652 #endif
654 return str.find(value) != std::string_view::npos;
658 * Checks if a string is contained in another string with a locale-aware comparison that is case insensitive.
660 * @param str The string to search in.
661 * @param value The string to search for.
662 * @return True if a match was found.
664 [[nodiscard]] bool StrNaturalContainsIgnoreCase(const std::string_view str, const std::string_view value)
666 #ifdef WITH_ICU_I18N
667 int res_u = ICUStringContains(str, value, true);
668 if (res_u >= 0) return res_u > 0;
669 #endif /* WITH_ICU_I18N */
671 #if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
672 int res = Win32StringContains(str, value, true);
673 if (res >= 0) return res > 0;
674 #endif
676 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
677 int res = MacOSStringContains(str, value, true);
678 if (res >= 0) return res > 0;
679 #endif
681 CaseInsensitiveStringView ci_str{ str.data(), str.size() };
682 CaseInsensitiveStringView ci_value{ value.data(), value.size() };
683 return ci_str.find(ci_value) != CaseInsensitiveStringView::npos;
687 * Convert a single hex-nibble to a byte.
689 * @param c The hex-nibble to convert.
690 * @return The byte the hex-nibble represents, or -1 if it is not a valid hex-nibble.
692 static int ConvertHexNibbleToByte(char c)
694 if (c >= '0' && c <= '9') return c - '0';
695 if (c >= 'A' && c <= 'F') return c + 10 - 'A';
696 if (c >= 'a' && c <= 'f') return c + 10 - 'a';
697 return -1;
701 * Convert a hex-string to a byte-array, while validating it was actually hex.
703 * @param hex The hex-string to convert.
704 * @param bytes The byte-array to write the result to.
706 * @note The length of the hex-string has to be exactly twice that of the length
707 * of the byte-array, otherwise conversion will fail.
709 * @return True iff the hex-string was valid and the conversion succeeded.
711 bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes)
713 if (bytes.size() != hex.size() / 2) {
714 return false;
717 /* Hex-string lengths are always divisible by 2. */
718 if (hex.size() % 2 != 0) {
719 return false;
722 for (size_t i = 0; i < hex.size() / 2; i++) {
723 auto hi = ConvertHexNibbleToByte(hex[i * 2]);
724 auto lo = ConvertHexNibbleToByte(hex[i * 2 + 1]);
726 if (hi < 0 || lo < 0) {
727 return false;
730 bytes[i] = (hi << 4) | lo;
733 return true;
736 #ifdef WITH_UNISCRIBE
738 /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
740 return std::make_unique<UniscribeStringIterator>();
743 #elif defined(WITH_ICU_I18N)
745 #include <unicode/utext.h>
746 #include <unicode/brkiter.h>
748 /** String iterator using ICU as a backend. */
749 class IcuStringIterator : public StringIterator
751 icu::BreakIterator *char_itr; ///< ICU iterator for characters.
752 icu::BreakIterator *word_itr; ///< ICU iterator for words.
754 std::vector<UChar> utf16_str; ///< UTF-16 copy of the string.
755 std::vector<size_t> utf16_to_utf8; ///< Mapping from UTF-16 code point position to index in the UTF-8 source string.
757 public:
758 IcuStringIterator() : char_itr(nullptr), word_itr(nullptr)
760 UErrorCode status = U_ZERO_ERROR;
761 this->char_itr = icu::BreakIterator::createCharacterInstance(icu::Locale(_current_language != nullptr ? _current_language->isocode : "en"), status);
762 this->word_itr = icu::BreakIterator::createWordInstance(icu::Locale(_current_language != nullptr ? _current_language->isocode : "en"), status);
764 this->utf16_str.push_back('\0');
765 this->utf16_to_utf8.push_back(0);
768 ~IcuStringIterator() override
770 delete this->char_itr;
771 delete this->word_itr;
774 void SetString(const char *s) override
776 const char *string_base = s;
778 /* Unfortunately current ICU versions only provide rudimentary support
779 * for word break iterators (especially for CJK languages) in combination
780 * with UTF-8 input. As a work around we have to convert the input to
781 * UTF-16 and create a mapping back to UTF-8 character indices. */
782 this->utf16_str.clear();
783 this->utf16_to_utf8.clear();
785 while (*s != '\0') {
786 size_t idx = s - string_base;
788 char32_t c = Utf8Consume(&s);
789 if (c < 0x10000) {
790 this->utf16_str.push_back((UChar)c);
791 } else {
792 /* Make a surrogate pair. */
793 this->utf16_str.push_back((UChar)(0xD800 + ((c - 0x10000) >> 10)));
794 this->utf16_str.push_back((UChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
795 this->utf16_to_utf8.push_back(idx);
797 this->utf16_to_utf8.push_back(idx);
799 this->utf16_str.push_back('\0');
800 this->utf16_to_utf8.push_back(s - string_base);
802 UText text = UTEXT_INITIALIZER;
803 UErrorCode status = U_ZERO_ERROR;
804 utext_openUChars(&text, this->utf16_str.data(), this->utf16_str.size() - 1, &status);
805 this->char_itr->setText(&text, status);
806 this->word_itr->setText(&text, status);
807 this->char_itr->first();
808 this->word_itr->first();
811 size_t SetCurPosition(size_t pos) override
813 /* Convert incoming position to an UTF-16 string index. */
814 uint utf16_pos = 0;
815 for (uint i = 0; i < this->utf16_to_utf8.size(); i++) {
816 if (this->utf16_to_utf8[i] == pos) {
817 utf16_pos = i;
818 break;
822 /* isBoundary has the documented side-effect of setting the current
823 * position to the first valid boundary equal to or greater than
824 * the passed value. */
825 this->char_itr->isBoundary(utf16_pos);
826 return this->utf16_to_utf8[this->char_itr->current()];
829 size_t Next(IterType what) override
831 int32_t pos;
832 switch (what) {
833 case ITER_CHARACTER:
834 pos = this->char_itr->next();
835 break;
837 case ITER_WORD:
838 pos = this->word_itr->following(this->char_itr->current());
839 /* The ICU word iterator considers both the start and the end of a word a valid
840 * break point, but we only want word starts. Move to the next location in
841 * case the new position points to whitespace. */
842 while (pos != icu::BreakIterator::DONE &&
843 IsWhitespace(Utf16DecodeChar((const uint16_t *)&this->utf16_str[pos]))) {
844 int32_t new_pos = this->word_itr->next();
845 /* Don't set it to DONE if it was valid before. Otherwise we'll return END
846 * even though the iterator wasn't at the end of the string before. */
847 if (new_pos == icu::BreakIterator::DONE) break;
848 pos = new_pos;
851 this->char_itr->isBoundary(pos);
852 break;
854 default:
855 NOT_REACHED();
858 return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
861 size_t Prev(IterType what) override
863 int32_t pos;
864 switch (what) {
865 case ITER_CHARACTER:
866 pos = this->char_itr->previous();
867 break;
869 case ITER_WORD:
870 pos = this->word_itr->preceding(this->char_itr->current());
871 /* The ICU word iterator considers both the start and the end of a word a valid
872 * break point, but we only want word starts. Move to the previous location in
873 * case the new position points to whitespace. */
874 while (pos != icu::BreakIterator::DONE &&
875 IsWhitespace(Utf16DecodeChar((const uint16_t *)&this->utf16_str[pos]))) {
876 int32_t new_pos = this->word_itr->previous();
877 /* Don't set it to DONE if it was valid before. Otherwise we'll return END
878 * even though the iterator wasn't at the start of the string before. */
879 if (new_pos == icu::BreakIterator::DONE) break;
880 pos = new_pos;
883 this->char_itr->isBoundary(pos);
884 break;
886 default:
887 NOT_REACHED();
890 return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
894 /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
896 return std::make_unique<IcuStringIterator>();
899 #else
901 /** Fallback simple string iterator. */
902 class DefaultStringIterator : public StringIterator
904 const char *string; ///< Current string.
905 size_t len; ///< String length.
906 size_t cur_pos; ///< Current iteration position.
908 public:
909 DefaultStringIterator() : string(nullptr), len(0), cur_pos(0)
913 void SetString(const char *s) override
915 this->string = s;
916 this->len = strlen(s);
917 this->cur_pos = 0;
920 size_t SetCurPosition(size_t pos) override
922 assert(this->string != nullptr && pos <= this->len);
923 /* Sanitize in case we get a position inside an UTF-8 sequence. */
924 while (pos > 0 && IsUtf8Part(this->string[pos])) pos--;
925 return this->cur_pos = pos;
928 size_t Next(IterType what) override
930 assert(this->string != nullptr);
932 /* Already at the end? */
933 if (this->cur_pos >= this->len) return END;
935 switch (what) {
936 case ITER_CHARACTER: {
937 char32_t c;
938 this->cur_pos += Utf8Decode(&c, this->string + this->cur_pos);
939 return this->cur_pos;
942 case ITER_WORD: {
943 char32_t c;
944 /* Consume current word. */
945 size_t offs = Utf8Decode(&c, this->string + this->cur_pos);
946 while (this->cur_pos < this->len && !IsWhitespace(c)) {
947 this->cur_pos += offs;
948 offs = Utf8Decode(&c, this->string + this->cur_pos);
950 /* Consume whitespace to the next word. */
951 while (this->cur_pos < this->len && IsWhitespace(c)) {
952 this->cur_pos += offs;
953 offs = Utf8Decode(&c, this->string + this->cur_pos);
956 return this->cur_pos;
959 default:
960 NOT_REACHED();
963 return END;
966 size_t Prev(IterType what) override
968 assert(this->string != nullptr);
970 /* Already at the beginning? */
971 if (this->cur_pos == 0) return END;
973 switch (what) {
974 case ITER_CHARACTER:
975 return this->cur_pos = Utf8PrevChar(this->string + this->cur_pos) - this->string;
977 case ITER_WORD: {
978 const char *s = this->string + this->cur_pos;
979 char32_t c;
980 /* Consume preceding whitespace. */
981 do {
982 s = Utf8PrevChar(s);
983 Utf8Decode(&c, s);
984 } while (s > this->string && IsWhitespace(c));
985 /* Consume preceding word. */
986 while (s > this->string && !IsWhitespace(c)) {
987 s = Utf8PrevChar(s);
988 Utf8Decode(&c, s);
990 /* Move caret back to the beginning of the word. */
991 if (IsWhitespace(c)) Utf8Consume(&s);
993 return this->cur_pos = s - this->string;
996 default:
997 NOT_REACHED();
1000 return END;
1004 #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
1005 /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
1007 std::unique_ptr<StringIterator> i = OSXStringIterator::Create();
1008 if (i != nullptr) return i;
1010 return std::make_unique<DefaultStringIterator>();
1012 #else
1013 /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
1015 return std::make_unique<DefaultStringIterator>();
1017 #endif /* defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) */
1019 #endif