Bitcoin Core 31.99.0
P2P Digital Currency
string.h
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_UTIL_STRING_H
6#define BITCOIN_UTIL_STRING_H
7
8#include <algorithm>
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <initializer_list>
13#include <locale>
14#include <optional>
15#include <span>
16#include <sstream>
17#include <string>
18#include <string_view>
19#include <vector>
20
21#include <attributes.h>
22
23namespace util {
24namespace detail {
25template <unsigned num_params>
26constexpr static void CheckNumFormatSpecifiers(const char* str)
27{
28 unsigned count_normal{0}; // Number of "normal" specifiers, like %s
29 unsigned count_pos{0}; // Max number in positional specifier, like %8$s
30 for (auto it{str}; *it != '\0'; ++it) {
31 if (*it != '%' || *++it == '%') continue; // Skip escaped %%
32
33 auto add_arg = [&] {
34 unsigned maybe_num{0};
35 while ('0' <= *it && *it <= '9') {
36 maybe_num *= 10;
37 maybe_num += *it - '0';
38 ++it;
39 }
40
41 if (*it == '$') {
42 ++it;
43 // Positional specifier, like %8$s
44 if (maybe_num == 0) throw "Positional format specifier must have position of at least 1";
45 count_pos = std::max(count_pos, maybe_num);
46 } else {
47 // Non-positional specifier, like %s
48 ++count_normal;
49 }
50 };
51
52 // Increase argument count and consume positional specifier, if present.
53 add_arg();
54
55 // Consume flags.
56 while (*it == '#' || *it == '0' || *it == '-' || *it == ' ' || *it == '+') ++it;
57
58 auto parse_size = [&] {
59 if (*it == '*') {
60 ++it;
61 add_arg();
62 } else {
63 while ('0' <= *it && *it <= '9') ++it;
64 }
65 };
66
67 // Consume dynamic or static width value.
68 parse_size();
69
70 // Consume dynamic or static precision value.
71 if (*it == '.') {
72 ++it;
73 parse_size();
74 }
75
76 if (*it == '\0') throw "Format specifier incorrectly terminated by end of string";
77
78 // Length and type in "[flags][width][.precision][length]type"
79 // is not checked. Parsing continues with the next '%'.
80 }
81 if (count_normal && count_pos) throw "Format specifiers must be all positional or all non-positional!";
82 unsigned count{count_normal | count_pos};
83 if (num_params != count) throw "Format specifier count must match the argument count!";
84}
85} // namespace detail
86
95template <unsigned num_params>
97 const char* const fmt;
98 consteval ConstevalFormatString(const char* str) : fmt{str} { detail::CheckNumFormatSpecifiers<num_params>(fmt); }
99};
100
101void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
102
118template <typename T = std::span<const char>>
119std::vector<T> Split(const std::span<const char>& sp, std::string_view separators, bool include_sep = false)
120{
121 std::vector<T> ret;
122 auto it = sp.begin();
123 auto start = it;
124 while (it != sp.end()) {
125 if (separators.find(*it) != std::string::npos) {
126 if (include_sep) {
127 ret.emplace_back(start, it + 1);
128 } else {
129 ret.emplace_back(start, it);
130 }
131 start = it + 1;
132 }
133 ++it;
134 }
135 ret.emplace_back(start, it);
136 return ret;
137}
138
146template <typename T = std::span<const char>>
147std::vector<T> Split(const std::span<const char>& sp, char sep, bool include_sep = false)
148{
149 return Split<T>(sp, std::string_view{&sep, 1}, include_sep);
150}
151
152[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, char sep)
153{
154 return Split<std::string>(str, sep);
155}
156
157[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, std::string_view separators)
158{
159 return Split<std::string>(str, separators);
160}
161
162[[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
163{
164 std::string::size_type front = str.find_first_not_of(pattern);
165 if (front == std::string::npos) {
166 return {};
167 }
168 std::string::size_type end = str.find_last_not_of(pattern);
169 return str.substr(front, end - front + 1);
170}
171
172[[nodiscard]] inline std::string TrimString(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
173{
174 return std::string(TrimStringView(str, pattern));
175}
176
177[[nodiscard]] inline std::string_view RemoveSuffixView(std::string_view str, std::string_view suffix)
178{
179 if (str.ends_with(suffix)) {
180 return str.substr(0, str.size() - suffix.size());
181 }
182 return str;
183}
184
185[[nodiscard]] inline std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
186{
187 if (str.starts_with(prefix)) {
188 return str.substr(prefix.size());
189 }
190 return str;
191}
192
193[[nodiscard]] inline std::string RemovePrefix(std::string_view str, std::string_view prefix)
194{
195 return std::string(RemovePrefixView(str, prefix));
196}
197
206template <typename C, typename S, typename UnaryOp>
207// NOLINTNEXTLINE(misc-no-recursion)
208auto Join(const C& container, const S& separator, UnaryOp unary_op)
209{
210 decltype(unary_op(*container.begin())) ret;
211 bool first{true};
212 for (const auto& item : container) {
213 if (!first) ret += separator;
214 ret += unary_op(item);
215 first = false;
216 }
217 return ret;
218}
219
220template <typename C, typename S>
221auto Join(const C& container, const S& separator)
222{
223 return Join(container, separator, [](const auto& i) { return i; });
224}
225
229inline std::string MakeUnorderedList(const std::vector<std::string>& items)
230{
231 return Join(items, "\n", [](const std::string& item) { return "- " + item; });
232}
233
237[[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept
238{
239 for (auto c : str) {
240 if (c == 0) return false;
241 }
242 return true;
243}
244
248template <typename T>
249std::string ToString(const T& t)
250{
251 std::ostringstream oss;
252 oss.imbue(std::locale::classic());
253 oss << t;
254 return oss.str();
255}
256
260template <typename T1, size_t PREFIX_LEN>
261[[nodiscard]] inline bool HasPrefix(const T1& obj,
262 const std::array<uint8_t, PREFIX_LEN>& prefix)
263{
264 return obj.size() >= PREFIX_LEN &&
265 std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
266}
267
269 const std::span<const std::byte>::iterator start;
270 const std::span<const std::byte>::iterator end;
271 const size_t max_line_length;
272 std::span<const std::byte>::iterator it;
273
274 explicit LineReader(std::span<const std::byte> buffer, size_t max_line_length);
275 explicit LineReader(std::string_view str, size_t max_line_length) : LineReader{std::as_bytes(std::span{str}), max_line_length} {}
276
285 std::optional<std::string_view> ReadLine() LIFETIMEBOUND;
286
295 std::string_view ReadLength(size_t len) LIFETIMEBOUND;
296
300 size_t Remaining() const;
301
305 size_t Consumed() const;
306};
307} // namespace util
308
309#endif // BITCOIN_UTIL_STRING_H
#define LIFETIMEBOUND
Definition: attributes.h:16
int ret
static constexpr void CheckNumFormatSpecifiers(const char *str)
Definition: string.h:26
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:152
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
Definition: string.h:261
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:162
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition: string.h:229
std::string_view RemoveSuffixView(std::string_view str, std::string_view suffix)
Definition: string.h:177
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:185
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:249
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:172
std::string RemovePrefix(std::string_view str, std::string_view prefix)
Definition: string.h:193
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:208
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:237
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:14
std::vector< T > Split(const std::span< const char > &sp, std::string_view separators, bool include_sep=false)
Split a string on any char found in separators, returning a vector.
Definition: string.h:119
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
const char * prefix
Definition: rest.cpp:1143
A wrapper for a compile-time partially validated format string.
Definition: string.h:96
consteval ConstevalFormatString(const char *str)
Definition: string.h:98
const char *const fmt
Definition: string.h:97
size_t Consumed() const
Returns number of bytes already read from buffer.
Definition: string.cpp:74
std::optional< std::string_view > ReadLine() LIFETIMEBOUND
Returns a string from current iterator position up to (but not including) next and advances iterator...
Definition: string.cpp:23
size_t Remaining() const
Returns remaining size of bytes in buffer.
Definition: string.cpp:69
std::span< conststd::byte >::iterator it
Definition: string.h:272
LineReader(std::span< const std::byte > buffer, size_t max_line_length)
Definition: string.cpp:20
const std::span< conststd::byte >::iterator end
Definition: string.h:270
LineReader(std::string_view str, size_t max_line_length)
Definition: string.h:275
const size_t max_line_length
Definition: string.h:271
const std::span< conststd::byte >::iterator start
Definition: string.h:269
std::string_view ReadLength(size_t len) LIFETIMEBOUND
Returns string from current iterator position of specified length if possible and advances iterator o...
Definition: string.cpp:60
static int count