Bitcoin ABC 0.30.5
P2P Digital Currency
string.h
Go to the documentation of this file.
1// Copyright (c) 2019 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 <util/spanparsing.h>
9
10#include <array>
11#include <cstdint>
12#include <cstring>
13#include <locale>
14#include <sstream>
15#include <string>
16#include <string_view>
17#include <vector>
18
19void ReplaceAll(std::string &in_out, const std::string &search,
20 const std::string &substitute);
21
22[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str,
23 char sep) {
24 return spanparsing::Split<std::string>(str, sep);
25}
26
27[[nodiscard]] inline std::string_view
28TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v") {
29 std::string::size_type front = str.find_first_not_of(pattern);
30 if (front == std::string::npos) {
31 return {};
32 }
33 std::string::size_type end = str.find_last_not_of(pattern);
34 return str.substr(front, end - front + 1);
35}
36
37[[nodiscard]] inline std::string
38TrimString(std::string_view str, std::string_view pattern = " \f\n\r\t\v") {
39 return std::string(TrimStringView(str, pattern));
40}
41
42[[nodiscard]] inline std::string_view
43RemovePrefixView(std::string_view str, std::string_view prefix) {
44 if (str.substr(0, prefix.size()) == prefix) {
45 return str.substr(prefix.size());
46 }
47 return str;
48}
49
50[[nodiscard]] inline std::string RemovePrefix(std::string_view str,
51 std::string_view prefix) {
52 return std::string(RemovePrefixView(str, prefix));
53}
54
62template <typename T, typename BaseType, typename UnaryOp>
63auto Join(const std::vector<T> &list, const BaseType &separator,
64 UnaryOp unary_op) -> decltype(unary_op(list.at(0))) {
65 decltype(unary_op(list.at(0))) ret;
66 for (size_t i = 0; i < list.size(); ++i) {
67 if (i > 0) {
68 ret += separator;
69 }
70 ret += unary_op(list.at(i));
71 }
72 return ret;
73}
74
75template <typename T, typename T2>
76T Join(const std::vector<T> &list, const T2 &separator) {
77 return Join(list, separator, [](const T &i) { return i; });
78}
79
80// Explicit overload needed for c_str arguments, which would otherwise cause a
81// substitution failure in the template above.
82inline std::string Join(const std::vector<std::string> &list,
83 std::string_view separator) {
84 return Join<std::string>(list, separator);
85}
86
90[[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept {
91 for (auto c : str) {
92 if (c == 0) return false;
93 }
94 return true;
95}
96
100template <typename T> std::string ToString(const T &t) {
101 std::ostringstream oss;
102 oss.imbue(std::locale::classic());
103 oss << t;
104 return oss.str();
105}
106
110template <typename T1, size_t PREFIX_LEN>
111[[nodiscard]] inline bool
112HasPrefix(const T1 &obj, const std::array<uint8_t, PREFIX_LEN> &prefix) {
113 return obj.size() >= PREFIX_LEN &&
114 std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
115}
116
117#endif // BITCOIN_UTIL_STRING_H
const char * prefix
Definition: rest.cpp:817
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:63
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:38
std::string RemovePrefix(std::string_view str, std::string_view prefix)
Definition: string.h:50
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:22
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:10
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:43
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:90
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:100
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:28
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:112