OpenTTD Source 15.0-beta2
string_func.h
Go to the documentation of this file.
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
12#ifndef STRING_FUNC_H
13#define STRING_FUNC_H
14
15#include <iosfwd>
16
17#include "core/bitmath_func.hpp"
18#include "string_type.h"
19
20void strecpy(std::span<char> dst, std::string_view src);
21
22std::string FormatArrayAsHex(std::span<const uint8_t> data);
23
26
29{
30 return StrMakeValid(std::string_view(str), settings);
31}
33{
35 return std::move(str);
36}
37
38bool strtolower(std::string &str, std::string::size_type offs = 0);
39
40[[nodiscard]] bool StrValid(std::span<const char> str);
41void StrTrimInPlace(std::string &str);
42[[nodiscard]] std::string_view StrTrimView(std::string_view str);
43
44[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix);
45[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix);
46
47[[nodiscard]] int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2);
48[[nodiscard]] bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2);
49[[nodiscard]] bool StrContainsIgnoreCase(const std::string_view str, const std::string_view value);
50[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
51[[nodiscard]] bool StrNaturalContains(const std::string_view str, const std::string_view value);
52[[nodiscard]] bool StrNaturalContainsIgnoreCase(const std::string_view str, const std::string_view value);
53
54bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
55
58 bool operator()(const std::string_view s1, const std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
59};
60
68inline bool StrEmpty(const char *s)
69{
70 return s == nullptr || s[0] == '\0';
71}
72
80inline size_t ttd_strnlen(const char *str, size_t maxlen)
81{
82 const char *t;
83 for (t = str; static_cast<size_t>(t - str) < maxlen && *t != '\0'; t++) {}
84 return t - str;
85}
86
87bool IsValidChar(char32_t key, CharSetFilter afilter);
88
89size_t Utf8Decode(char32_t *c, const char *s);
90/* std::string_view::iterator might be char *, in which case we do not want this templated variant to be taken. */
91template <typename T> requires (!std::is_same_v<T, char *> && (std::is_same_v<std::string_view::iterator, T> || std::is_same_v<std::string::iterator, T>))
92inline size_t Utf8Decode(char32_t *c, T &s) { return Utf8Decode(c, &*s); }
93
94inline char32_t Utf8Consume(const char **s)
95{
96 char32_t c;
97 *s += Utf8Decode(&c, *s);
98 return c;
99}
100
101template <class Titr>
102inline char32_t Utf8Consume(Titr &s)
103{
104 char32_t c;
105 s += Utf8Decode(&c, &*s);
106 return c;
107}
108
116inline int8_t Utf8EncodedCharLen(char c)
117{
118 if (GB(c, 3, 5) == 0x1E) return 4;
119 if (GB(c, 4, 4) == 0x0E) return 3;
120 if (GB(c, 5, 3) == 0x06) return 2;
121 if (GB(c, 7, 1) == 0x00) return 1;
122
123 /* Invalid UTF8 start encoding */
124 return 0;
125}
126
127
128/* Check if the given character is part of a UTF8 sequence */
129inline bool IsUtf8Part(char c)
130{
131 return GB(c, 6, 2) == 2;
132}
133
134size_t Utf8StringLength(std::string_view str);
135
141inline bool Utf16IsLeadSurrogate(uint c)
142{
143 return c >= 0xD800 && c <= 0xDBFF;
144}
145
151inline bool Utf16IsTrailSurrogate(uint c)
152{
153 return c >= 0xDC00 && c <= 0xDFFF;
154}
155
162inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
163{
164 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
165}
166
172inline char32_t Utf16DecodeChar(const uint16_t *c)
173{
174 if (Utf16IsLeadSurrogate(c[0])) {
175 return Utf16DecodeSurrogate(c[0], c[1]);
176 } else {
177 return *c;
178 }
179}
180
187inline bool IsTextDirectionChar(char32_t c)
188{
189 switch (c) {
190 case CHAR_TD_LRM:
191 case CHAR_TD_RLM:
192 case CHAR_TD_LRE:
193 case CHAR_TD_RLE:
194 case CHAR_TD_LRO:
195 case CHAR_TD_RLO:
196 case CHAR_TD_PDF:
197 return true;
198
199 default:
200 return false;
201 }
202}
203
204inline bool IsPrintable(char32_t c)
205{
206 if (c < 0x20) return false;
207 if (c < 0xE000) return true;
208 if (c < 0xE200) return false;
209 return true;
210}
211
219inline bool IsWhitespace(char32_t c)
220{
221 return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
222}
223
224/* Needed for NetBSD version (so feature) testing */
225#if defined(__NetBSD__) || defined(__FreeBSD__)
226#include <sys/param.h>
227#endif
228
229#endif /* STRING_FUNC_H */
Functions related to bit mathematics.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Enum-as-bit-set wrapper.
fluid_settings_t * settings
FluidSynth settings handle.
bool ConvertHexToBytes(std::string_view hex, std::span< uint8_t > bytes)
Convert a hex-string to a byte-array, while validating it was actually hex.
Definition string.cpp:659
char32_t Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:394
char32_t Utf16DecodeChar(const uint16_t *c)
Decode an UTF-16 character.
bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition string_func.h:68
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition string.cpp:418
bool StrContainsIgnoreCase(const std::string_view str, const std::string_view value)
Checks if a string is contained in another string, while ignoring the case of the characters.
Definition string.cpp:381
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:368
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
bool StrNaturalContains(const std::string_view str, const std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case sensiti...
Definition string.cpp:585
int8_t Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
void strecpy(std::span< char > dst, std::string_view src)
Copies characters from one buffer to another.
Definition string.cpp:57
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:78
bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix, ignoring case.
Definition string.cpp:302
bool StrValid(std::span< const char > str)
Checks whether the given string is valid, i.e.
Definition string.cpp:248
size_t Utf8Decode(char32_t *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition string.cpp:441
void StrMakeValidInPlace(char *str, StringValidationSettings settings=StringValidationSetting::ReplaceWithQuestionMark)
Scans the string for invalid characters and replaces them with a question mark '?' (if not ignored).
Definition string.cpp:194
bool StrNaturalContainsIgnoreCase(const std::string_view str, const std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case insensi...
Definition string.cpp:612
bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
Definition string.cpp:355
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix)
Check whether the given string ends with the given suffix, ignoring case.
Definition string.cpp:342
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
Definition string.cpp:281
size_t ttd_strnlen(const char *str, size_t maxlen)
Get the length of a string, within a limited buffer.
Definition string_func.h:80
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front=false)
Compares two strings using case insensitive natural sort.
Definition string.cpp:514
std::string StrMakeValid(std::string_view str, StringValidationSettings settings=StringValidationSetting::ReplaceWithQuestionMark)
Copies the valid (UTF-8) characters from str to the returned string.
Definition string.cpp:226
Types for strings.
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
static const char32_t CHAR_TD_RLE
The following text is embedded right-to-left.
Definition string_type.h:38
static const char32_t CHAR_TD_LRO
Force the following characters to be treated as left-to-right characters.
Definition string_type.h:39
static const char32_t CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition string_type.h:35
CharSetFilter
Valid filter types for IsValidChar.
Definition string_type.h:24
static const char32_t CHAR_TD_RLO
Force the following characters to be treated as right-to-left characters.
Definition string_type.h:40
static const char32_t CHAR_TD_LRE
The following text is embedded left-to-right.
Definition string_type.h:37
static const char32_t CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition string_type.h:36
static const char32_t CHAR_TD_PDF
Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
Definition string_type.h:41
Case insensitive comparator for strings, for example for use in std::map.
Definition string_func.h:57