Bitcoin Core  0.19.99
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 <attributes.h>
9 
10 #include <cstring>
11 #include <string>
12 #include <vector>
13 
14 NODISCARD inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v")
15 {
16  std::string::size_type front = str.find_first_not_of(pattern);
17  if (front == std::string::npos) {
18  return std::string();
19  }
20  std::string::size_type end = str.find_last_not_of(pattern);
21  return str.substr(front, end - front + 1);
22 }
23 
31 template <typename T, typename UnaryOp>
32 std::string Join(const std::vector<T>& list, const std::string& separator, UnaryOp unary_op)
33 {
34  std::string ret;
35  for (size_t i = 0; i < list.size(); ++i) {
36  if (i > 0) ret += separator;
37  ret += unary_op(list.at(i));
38  }
39  return ret;
40 }
41 
42 inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
43 {
44  return Join(list, separator, [](const std::string& i) { return i; });
45 }
46 
50 NODISCARD inline bool ValidAsCString(const std::string& str) noexcept
51 {
52  return str.size() == strlen(str.c_str());
53 }
54 
55 #endif // BITCOIN_UTIL_STRENCODINGS_H
NODISCARD bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:50
std::string Join(const std::vector< T > &list, const std::string &separator, UnaryOp unary_op)
Join a list of items.
Definition: string.h:32
NODISCARD std::string TrimString(const std::string &str, const std::string &pattern=" \\\)
Definition: string.h:14
#define NODISCARD
Definition: attributes.h:18