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