userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
text_light.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/text_light.hpp
4
/// @brief Text utilities that do not use locales
5
/// @ingroup userver_universal
6
7
#
include
<
string
>
8
#
include
<
string_view
>
9
#
include
<
vector
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
/// @brief Text utilities
14
namespace
utils
::
text
{
15
16
/// Return trimmed copy of string.
17
[[
nodiscard
]]
18
std::string
Trim
(std::string_view str);
19
20
/// Trim string in-place.
21
[[
nodiscard
]]
22
std::string
Trim
(std::string&& str);
23
24
/// Return trimmed view of a string.
25
[[
nodiscard
]]
26
std::string_view
TrimView
(std::string_view str);
27
28
enum
class
SplitFlags {
29
kNone = 0,
30
kCompressAdjacentSeparators = 1 << 0,
31
};
32
33
/// Split string by separators
34
///
35
/// @snippet utils/text_light_test.cpp SplitMultiple
36
std::vector<std::string>
Split
(
37
std::string_view str,
38
std::string_view separators,
39
SplitFlags split_flags = SplitFlags::kCompressAdjacentSeparators
40
);
41
42
/// Split string by separators and return a non-owning container of chunks.
43
///
44
/// @warning Initial `str` should outlive the result of the function
45
///
46
/// @snippet utils/text_light_test.cpp SplitStringViewMultiple
47
std::vector<std::string_view>
SplitIntoStringViewVector
(std::string_view str, std::string_view separators);
48
49
/// Join string
50
std::string
Join
(
const
std::vector<std::string>& strs, std::string_view sep);
51
52
/// Return number formatted
53
std::string
Format
(
double
value,
int
ndigits);
54
55
/// Return true if `hay` starts with `needle`, false otherwise.
56
/// @deprecated Use std::string_view::starts_with() instead.
57
constexpr
bool
StartsWith
(std::string_view hay, std::string_view needle)
noexcept
{
return
hay.starts_with(needle); }
58
59
/// Return true if `hay` ends with `needle`, false otherwise.
60
/// @deprecated Use std::string_view::ends_with() instead.
61
constexpr
bool
EndsWith
(std::string_view hay, std::string_view needle)
noexcept
{
return
hay.ends_with(needle); }
62
63
/// Case insensitive (ASCII only) variant of StartsWith()
64
bool
ICaseStartsWith
(std::string_view hay, std::string_view needle)
noexcept
;
65
66
/// Case insensitive (ASCII only) variant of EndsWith()
67
bool
ICaseEndsWith
(std::string_view hay, std::string_view needle)
noexcept
;
68
69
/// Removes double quotes from front and back of string.
70
///
71
/// Examples:
72
/// @code
73
/// RemoveQuotes("\"test\"") // returns "test"
74
/// RemoveQuotes("\"test") // returns "\"test"
75
/// RemoveQuotes("'test'") // returns "'test'"
76
/// RemoveQuotes("\"\"test\"\"") // returns "\"test\""
77
/// @endcode
78
std::string
RemoveQuotes
(std::string_view str);
79
80
/// Checks whether the character is an ASCII character
81
bool
IsAscii
(
char
ch)
noexcept
;
82
83
/// Checks whether the character is a whitespace character in C locale
84
bool
IsAsciiSpace
(
char
ch)
noexcept
;
85
86
/// Checks if text contains only ASCII characters
87
bool
IsAscii
(std::string_view text)
noexcept
;
88
89
/// @brief UTF8 text utilities
90
namespace
utf8
{
91
92
/// Returns the length in bytes of the UTF-8 code point by the first byte.
93
unsigned
CodePointLengthByFirstByte
(
unsigned
char
c)
noexcept
;
94
95
/// `bytes` must not be a nullptr, `length` must not be 0.
96
bool
IsWellFormedCodePoint
(
const
unsigned
char
* bytes, std::size_t length)
noexcept
;
97
98
/// `bytes` must not be a nullptr, `length` must not be 0.
99
bool
IsValid
(
const
unsigned
char
* bytes, std::size_t length)
noexcept
;
100
101
/// returns number of utf-8 code points, text must be in utf-8 encoding
102
/// @throws std::runtime_error if not a valid UTF8 text
103
std::size_t
GetCodePointsCount
(std::string_view text);
104
105
/// Removes the longest (possible empty) suffix of `str` which is a proper
106
/// prefix of some utf-8 multibyte character. If `str` is not in utf-8 it may
107
/// remove some suffix of length up to 3.
108
void
TrimTruncatedEnding
(std::string& str);
109
110
/// @see void TrimTruncatedEnding(std::string& str)
111
/// @warning this **does not** change the original string
112
void
TrimViewTruncatedEnding
(std::string_view& view);
113
114
/// Returns position in `text` where utf-8 code point with position `pos` starts
115
/// OR `text.length()` if `text` contains less than or equal to `pos` points
116
/// @warning this **does not** check if `text` is valid utf-8 text
117
std::size_t
GetTextPosByCodePointPos
(std::string_view text, std::size_t pos)
noexcept
;
118
119
/// Removes the first `count` utf-8 code points from `text`
120
/// @warning this **does not** check if `text` is valid utf-8 text
121
void
RemovePrefix
(std::string& text, std::size_t count)
noexcept
;
122
123
/// @see void RemovePrefix(std::string& text, std::size_t count)
124
/// @warning this **does not** change the original string
125
void
RemoveViewPrefix
(std::string_view& text, std::size_t count)
noexcept
;
126
127
/// Takes the first `count` utf-8 code points from `text`
128
/// @warning this **does not** check if `text` is valid utf-8 text
129
void
TakePrefix
(std::string& text, std::size_t count)
noexcept
;
130
131
/// @see void TakePrefix(std::string& text, std::size_t count)
132
/// @warning this **does not** change the original string
133
void
TakeViewPrefix
(std::string_view& text, std::size_t count)
noexcept
;
134
135
}
// namespace utf8
136
137
/// Checks if text is in utf-8 encoding
138
bool
IsUtf8
(std::string_view text)
noexcept
;
139
140
/// Checks text on matching to the following conditions:
141
/// 1. text is in utf-8 encoding
142
/// 2. text does not contain any of control ascii characters
143
/// 3. if flag ascii is true than text contains only ascii characters
144
bool
IsPrintable
(std::string_view text,
bool
ascii_only =
true
)
noexcept
;
145
146
/// Checks if there are no embedded null ('\0') characters in text
147
bool
IsCString
(std::string_view text)
noexcept
;
148
149
/// convert CamelCase to snake_case(underscore)
150
std::string
CamelCaseToSnake
(std::string_view camel);
151
152
/// convert snake_case or SCREAMING_SNAKE_CASE to CamelCase
153
std::string
SnakeCaseToCamel
(std::string_view snake);
154
155
}
// namespace utils::text
156
157
USERVER_NAMESPACE_END
userver
utils
text_light.hpp
Generated on
for userver by
Doxygen
1.17.0