OpenTTD Source: src/string_func.h Source File

OpenTTD Source 15.0-RC2

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
23void StrMakeValidInPlace(char *str, StringValidationSettings settings = StringValidationSetting::ReplaceWithQuestionMark);
24void StrMakeValidInPlace(std::string &str, StringValidationSettings settings = StringValidationSetting::ReplaceWithQuestionMark);
25
26[[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = StringValidationSetting::ReplaceWithQuestionMark);
27[[nodiscard]] inline std::string StrMakeValid(std::string &&str, StringValidationSettings settings = StringValidationSetting::ReplaceWithQuestionMark)
28{
29 StrMakeValidInPlace(str, settings);
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
52 struct CaseInsensitiveComparator {
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
65 inline bool Utf16IsLeadSurrogate(uint c)
66{
67 return c >= 0xD800 && c <= 0xDBFF;
68}
69
75 inline bool Utf16IsTrailSurrogate(uint c)
76{
77 return c >= 0xDC00 && c <= 0xDFFF;
78}
79
86 inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
87{
88 return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
89}
90
96 inline 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
111 inline 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
143 inline 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 */
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:25
ConvertHexToBytes
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
StrNaturalContainsIgnoreCase
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
Utf16DecodeSurrogate
char32_t Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition string_func.h:86
Utf8StringLength
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
Utf16DecodeChar
char32_t Utf16DecodeChar(const uint16_t *c)
Decode an UTF-16 character.
Definition string_func.h:96
Utf16IsLeadSurrogate
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:65
StrNaturalContains
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
IsValidChar
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition string.cpp:373
GetEnv
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
IsWhitespace
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
Definition string_func.h:143
strecpy
void strecpy(std::span< char > dst, std::string_view src)
Copies characters from one buffer to another.
Definition string.cpp:56
FormatArrayAsHex
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:77
StrEqualsIgnoreCase
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
StrEndsWithIgnoreCase
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
StrValid
bool StrValid(std::span< const char > str)
Checks whether the given string is valid, i.e.
Definition string.cpp:205
StrMakeValidInPlace
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
Utf16IsTrailSurrogate
bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:75
IsTextDirectionChar
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
Definition string_func.h:111
StrTrimInPlace
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
Definition string.cpp:228
StrStartsWithIgnoreCase
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
StrNaturalCompare
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
StrCompareIgnoreCase
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
StrContainsIgnoreCase
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
StrMakeValid
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
string_type.h
Types for strings.
StringValidationSetting::ReplaceWithQuestionMark
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
CHAR_TD_RLE
static const char32_t CHAR_TD_RLE
The following text is embedded right-to-left.
Definition string_type.h:38
CHAR_TD_LRO
static const char32_t CHAR_TD_LRO
Force the following characters to be treated as left-to-right characters.
Definition string_type.h:39
CHAR_TD_LRM
static const char32_t CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition string_type.h:35
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition string_type.h:24
CHAR_TD_RLO
static const char32_t CHAR_TD_RLO
Force the following characters to be treated as right-to-left characters.
Definition string_type.h:40
CHAR_TD_LRE
static const char32_t CHAR_TD_LRE
The following text is embedded left-to-right.
Definition string_type.h:37
CHAR_TD_RLM
static const char32_t CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition string_type.h:36
CHAR_TD_PDF
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
CaseInsensitiveComparator
Case insensitive comparator for strings, for example for use in std::map.
Definition string_func.h:52