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