OpenTTD Source 20250524-master-gc366e6a48e
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{
31 return std::move(str);
32}
33
34bool strtolower(std::string &str, std::string::size_type offs = 0);
35
36[[nodiscard]] bool StrValid(std::span<const char> str);
37void StrTrimInPlace(std::string &str);
38[[nodiscard]] std::string_view StrTrimView(std::string_view str, std::string_view characters_to_trim);
39
40[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix);
41[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix);
42
43[[nodiscard]] int StrCompareIgnoreCase(std::string_view str1, std::string_view str2);
44[[nodiscard]] bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2);
45[[nodiscard]] bool StrContainsIgnoreCase(std::string_view str, std::string_view value);
46[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
47[[nodiscard]] bool StrNaturalContains(std::string_view str, std::string_view value);
48[[nodiscard]] bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value);
49
50bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
51
54 bool operator()(std::string_view s1, std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
55};
56
57bool IsValidChar(char32_t key, CharSetFilter afilter);
58
59size_t Utf8StringLength(std::string_view str);
60
66inline bool Utf16IsLeadSurrogate(uint c)
67{
68 return c >= 0xD800 && c <= 0xDBFF;
69}
70
76inline bool Utf16IsTrailSurrogate(uint c)
77{
78 return c >= 0xDC00 && c <= 0xDFFF;
79}
80
87inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
88{
89 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
90}
91
97inline char32_t Utf16DecodeChar(const uint16_t *c)
98{
99 if (Utf16IsLeadSurrogate(c[0])) {
100 return Utf16DecodeSurrogate(c[0], c[1]);
101 } else {
102 return *c;
103 }
104}
105
112inline bool IsTextDirectionChar(char32_t c)
113{
114 switch (c) {
115 case CHAR_TD_LRM:
116 case CHAR_TD_RLM:
117 case CHAR_TD_LRE:
118 case CHAR_TD_RLE:
119 case CHAR_TD_LRO:
120 case CHAR_TD_RLO:
121 case CHAR_TD_PDF:
122 return true;
123
124 default:
125 return false;
126 }
127}
128
129inline bool IsPrintable(char32_t c)
130{
131 if (c < 0x20) return false;
132 if (c < 0xE000) return true;
133 if (c < 0xE200) return false;
134 return true;
135}
136
144inline bool IsWhitespace(char32_t c)
145{
146 return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
147}
148
149/* Needed for NetBSD version (so feature) testing */
150#if defined(__NetBSD__) || defined(__FreeBSD__)
151#include <sys/param.h>
152#endif
153
154std::optional<std::string_view> GetEnv(const char *variable);
155
156#endif /* STRING_FUNC_H */
Functions related to bit mathematics.
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:87
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:347
char32_t Utf16DecodeChar(const uint16_t *c)
Decode an UTF-16 character.
Definition string_func.h:97
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:66
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:371
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:860
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:54
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:75
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:321
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:295
bool StrValid(std::span< const char > str)
Checks whether the given string is valid, i.e.
Definition string.cpp:203
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:155
bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:76
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:226
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:255
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:425
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:308
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:334
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:186
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:53