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