OpenTTD Source 20251213-master-g1091fa6071
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
12#ifndef STRING_FUNC_H
13#define STRING_FUNC_H
14
15#include <iosfwd>
16
17#include "string_type.h"
18
19void strecpy(std::span<char> dst, std::string_view src);
20
21std::string FormatArrayAsHex(std::span<const uint8_t> data);
22
25
28{
30 return std::move(str);
31}
32
33bool strtolower(std::string &str, std::string::size_type offs = 0);
34
35[[nodiscard]] bool StrValid(std::span<const char> str);
36void StrTrimInPlace(std::string &str);
37[[nodiscard]] std::string_view StrTrimView(std::string_view str, std::string_view characters_to_trim);
38
39[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix);
40[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix);
41
42[[nodiscard]] int StrCompareIgnoreCase(std::string_view str1, std::string_view str2);
43[[nodiscard]] bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2);
44[[nodiscard]] bool StrContainsIgnoreCase(std::string_view str, std::string_view value);
45[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
46[[nodiscard]] bool StrNaturalContains(std::string_view str, std::string_view value);
47[[nodiscard]] bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value);
48
49bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
50
53 bool operator()(std::string_view s1, std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
54};
55
56bool IsValidChar(char32_t key, CharSetFilter afilter);
57
58size_t Utf8StringLength(std::string_view str);
59
65inline bool Utf16IsLeadSurrogate(uint c)
66{
67 return c >= 0xD800 && c <= 0xDBFF;
68}
69
75inline bool Utf16IsTrailSurrogate(uint c)
76{
77 return c >= 0xDC00 && c <= 0xDFFF;
78}
79
86inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
87{
88 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
89}
90
96inline char32_t Utf16DecodeChar(const uint16_t *c)
97{
98 if (Utf16IsLeadSurrogate(c[0])) {
99 return Utf16DecodeSurrogate(c[0], c[1]);
100 } else {
101 return *c;
102 }
103}
104
111inline bool IsTextDirectionChar(char32_t c)
112{
113 switch (c) {
114 case CHAR_TD_LRM:
115 case CHAR_TD_RLM:
116 case CHAR_TD_LRE:
117 case CHAR_TD_RLE:
118 case CHAR_TD_LRO:
119 case CHAR_TD_RLO:
120 case CHAR_TD_PDF:
121 return true;
122
123 default:
124 return false;
125 }
126}
127
128inline bool IsPrintable(char32_t c)
129{
130 if (c < 0x20) return false;
131 if (c < 0xE000) return true;
132 if (c < 0xE200) return false;
133 return true;
134}
135
143inline bool IsWhitespace(char32_t c)
144{
145 return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
146}
147
148/* Needed for NetBSD version (so feature) testing */
149#if defined(__NetBSD__) || defined(__FreeBSD__)
150#include <sys/param.h>
151#endif
152
153std::optional<std::string_view> GetEnv(const char *variable);
154
155#endif /* STRING_FUNC_H */
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:570
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:523
char32_t Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition string_func.h:86
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:349
char32_t Utf16DecodeChar(const uint16_t *c)
Decode an UTF-16 character.
Definition string_func.h:96
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:65
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:496
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition string.cpp:373
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:854
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
void strecpy(std::span< char > dst, std::string_view src)
Copies characters from one buffer to another.
Definition string.cpp:56
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:77
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:323
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:297
bool StrValid(std::span< const char > str)
Checks whether the given string is valid, i.e.
Definition string.cpp:205
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:157
bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:75
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:228
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:257
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:427
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:310
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:336
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.
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:52