OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef STRING_FUNC_H
11#define STRING_FUNC_H
12
13#include "string_type.h"
14
15void strecpy(std::span<char> dst, std::string_view src);
16
17std::string FormatArrayAsHex(std::span<const uint8_t> data);
18
21
24{
26 return std::move(str);
27}
28
29bool strtolower(std::string &str, std::string::size_type offs = 0);
30
31[[nodiscard]] bool StrValid(std::span<const char> str);
32void StrTrimInPlace(std::string &str);
33[[nodiscard]] std::string_view StrTrimView(std::string_view str, std::string_view characters_to_trim);
34
35[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix);
36[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix);
37
38[[nodiscard]] int StrCompareIgnoreCase(std::string_view str1, std::string_view str2);
39[[nodiscard]] bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2);
40[[nodiscard]] bool StrContainsIgnoreCase(std::string_view str, std::string_view value);
41[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
42[[nodiscard]] bool StrNaturalContains(std::string_view str, std::string_view value);
43[[nodiscard]] bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value);
44
45bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
46
49 bool operator()(std::string_view s1, std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
50};
51
52bool IsValidChar(char32_t key, CharSetFilter afilter);
53
54size_t Utf8StringLength(std::string_view str);
55
61inline bool Utf16IsLeadSurrogate(uint c)
62{
63 return c >= 0xD800 && c <= 0xDBFF;
64}
65
71inline bool Utf16IsTrailSurrogate(uint c)
72{
73 return c >= 0xDC00 && c <= 0xDFFF;
74}
75
82inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
83{
84 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
85}
86
92inline char32_t Utf16DecodeChar(const uint16_t *c)
93{
94 if (Utf16IsLeadSurrogate(c[0])) {
95 return Utf16DecodeSurrogate(c[0], c[1]);
96 } else {
97 return *c;
98 }
99}
100
107inline bool IsTextDirectionChar(char32_t c)
108{
109 switch (c) {
110 case CHAR_TD_LRM:
111 case CHAR_TD_RLM:
112 case CHAR_TD_LRE:
113 case CHAR_TD_RLE:
114 case CHAR_TD_LRO:
115 case CHAR_TD_RLO:
116 case CHAR_TD_PDF:
117 return true;
118
119 default:
120 return false;
121 }
122}
123
124inline bool IsPrintable(char32_t c)
125{
126 if (c < 0x20) return false;
127 if (c < 0xE000) return true;
128 if (c < 0xE200) return false;
129 return true;
130}
131
139inline bool IsWhitespace(char32_t c)
140{
141 return c == 0x0020 /* SPACE */ || (c >= 0x2000 && c <= 0x200C) || c == 0x3000; /* IDEOGRAPHIC SPACE */
142}
143
149inline bool IsNonbreakingWhitespace(char32_t c)
150{
151 return c == 0x00A0 /* NBSP */
152 || c == 0x202F /* NARROW NO-BREAK SPACE */
153 || c == 0x200D /* ZERO WIDTH JOINER */
154 || c == 0x2060; /* WORD JOINER */
155}
156
157std::optional<std::string_view> GetEnv(const char *variable);
158
159#endif /* STRING_FUNC_H */
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:571
bool StrNaturalContainsIgnoreCase(std::string_view str, 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:524
char32_t Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition string_func.h:82
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:350
char32_t Utf16DecodeChar(const uint16_t *c)
Decode an UTF-16 character.
Definition string_func.h:92
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:61
bool StrNaturalContains(std::string_view str, 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:497
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition string.cpp:374
std::optional< std::string_view > GetEnv(const char *variable)
Get the environment variable using std::getenv and when it is an empty string (or nullptr),...
Definition string.cpp:855
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
bool IsNonbreakingWhitespace(char32_t c)
Check wheather a character is non-breaking whitespace.
void strecpy(std::span< char > dst, std::string_view src)
Copies characters from one buffer to another.
Definition string.cpp:55
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:76
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:324
bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix)
Check whether the given string ends with the given suffix, ignoring case.
Definition string.cpp:298
bool StrValid(std::span< const char > str)
Checks whether the given string is valid, i.e.
Definition string.cpp:206
void StrMakeValidInPlace(char *str, StringValidationSettings settings=StringValidationSetting::ReplaceWithQuestionMark)
Scans the string for invalid characters and replaces them with a question mark '?
Definition string.cpp:156
bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:71
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
Definition string.cpp:229
bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix)
Check whether the given string starts with the given prefix, ignoring case.
Definition string.cpp:258
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:428
int StrCompareIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
Definition string.cpp:311
bool StrContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string, while ignoring the case of the characters.
Definition string.cpp:337
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:188
Types for strings.
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
Definition string_type.h:45
static const char32_t CHAR_TD_RLE
The following text is embedded right-to-left.
Definition string_type.h:38
EnumBitSet< StringValidationSetting, uint8_t > StringValidationSettings
Bitset of StringValidationSetting elements.
Definition string_type.h:57
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:48