OpenTTD Source 20260108-master-g8ba1860eaa
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
10#ifndef STRING_FUNC_H
11#define STRING_FUNC_H
12
13#include <iosfwd>
14
15#include "string_type.h"
16
17void strecpy(std::span<char> dst, std::string_view src);
18
19std::string FormatArrayAsHex(std::span<const uint8_t> data);
20
23
26{
28 return std::move(str);
29}
30
31bool strtolower(std::string &str, std::string::size_type offs = 0);
32
33[[nodiscard]] bool StrValid(std::span<const char> str);
34void StrTrimInPlace(std::string &str);
35[[nodiscard]] std::string_view StrTrimView(std::string_view str, std::string_view characters_to_trim);
36
37[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix);
38[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix);
39
40[[nodiscard]] int StrCompareIgnoreCase(std::string_view str1, std::string_view str2);
41[[nodiscard]] bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2);
42[[nodiscard]] bool StrContainsIgnoreCase(std::string_view str, std::string_view value);
43[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
44[[nodiscard]] bool StrNaturalContains(std::string_view str, std::string_view value);
45[[nodiscard]] bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value);
46
47bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
48
51 bool operator()(std::string_view s1, std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
52};
53
54bool IsValidChar(char32_t key, CharSetFilter afilter);
55
56size_t Utf8StringLength(std::string_view str);
57
63inline bool Utf16IsLeadSurrogate(uint c)
64{
65 return c >= 0xD800 && c <= 0xDBFF;
66}
67
73inline bool Utf16IsTrailSurrogate(uint c)
74{
75 return c >= 0xDC00 && c <= 0xDFFF;
76}
77
84inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
85{
86 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
87}
88
94inline char32_t Utf16DecodeChar(const uint16_t *c)
95{
96 if (Utf16IsLeadSurrogate(c[0])) {
97 return Utf16DecodeSurrogate(c[0], c[1]);
98 } else {
99 return *c;
100 }
101}
102
109inline bool IsTextDirectionChar(char32_t c)
110{
111 switch (c) {
112 case CHAR_TD_LRM:
113 case CHAR_TD_RLM:
114 case CHAR_TD_LRE:
115 case CHAR_TD_RLE:
116 case CHAR_TD_LRO:
117 case CHAR_TD_RLO:
118 case CHAR_TD_PDF:
119 return true;
120
121 default:
122 return false;
123 }
124}
125
126inline bool IsPrintable(char32_t c)
127{
128 if (c < 0x20) return false;
129 if (c < 0xE000) return true;
130 if (c < 0xE200) return false;
131 return true;
132}
133
141inline bool IsWhitespace(char32_t c)
142{
143 return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
144}
145
146/* Needed for NetBSD version (so feature) testing */
147#if defined(__NetBSD__) || defined(__FreeBSD__)
148#include <sys/param.h>
149#endif
150
151std::optional<std::string_view> GetEnv(const char *variable);
152
153#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:84
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:94
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:63
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:73
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:50