Branch data Line data Source code
# 1 : : // Copyright (c) 2019-2020 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 <algorithm> # 11 : : #include <array> # 12 : : #include <cstring> # 13 : : #include <locale> # 14 : : #include <sstream> # 15 : : #include <string> # 16 : : #include <vector> # 17 : : # 18 : : NODISCARD inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v") # 19 : 328395 : { # 20 : 328395 : std::string::size_type front = str.find_first_not_of(pattern); # 21 : 328395 : if (front == std::string::npos) { # 22 : 11 : return std::string(); # 23 : 11 : } # 24 : 328384 : std::string::size_type end = str.find_last_not_of(pattern); # 25 : 328384 : return str.substr(front, end - front + 1); # 26 : 328384 : } # 27 : : # 28 : : /** # 29 : : * Join a list of items # 30 : : * # 31 : : * @param list The list to join # 32 : : * @param separator The separator # 33 : : * @param unary_op Apply this operator to each item in the list # 34 : : */ # 35 : : template <typename T, typename BaseType, typename UnaryOp> # 36 : : auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_op) # 37 : : -> decltype(unary_op(list.at(0))) # 38 : 8085 : { # 39 : 8085 : decltype(unary_op(list.at(0))) ret; # 40 : 107827 : for (size_t i = 0; i < list.size(); ++i) { # 41 : 99742 : if (i > 0) ret += separator; # 42 : 99742 : ret += unary_op(list.at(i)); # 43 : 99742 : } # 44 : 8085 : return ret; # 45 : 8085 : } # 46 : : # 47 : : template <typename T> # 48 : : T Join(const std::vector<T>& list, const T& separator) # 49 : 3249 : { # 50 : 17767 : return Join(list, separator, [](const T& i) { return i; }); # 51 : 3249 : } # 52 : : # 53 : : // Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above. # 54 : : inline std::string Join(const std::vector<std::string>& list, const std::string& separator) # 55 : 2671 : { # 56 : 2671 : return Join<std::string>(list, separator); # 57 : 2671 : } # 58 : : # 59 : : /** # 60 : : * Check if a string does not contain any embedded NUL (\0) characters # 61 : : */ # 62 : : NODISCARD inline bool ValidAsCString(const std::string& str) noexcept # 63 : 1388175 : { # 64 : 1388175 : return str.size() == strlen(str.c_str()); # 65 : 1388175 : } # 66 : : # 67 : : /** # 68 : : * Locale-independent version of std::to_string # 69 : : */ # 70 : : template <typename T> # 71 : : std::string ToString(const T& t) # 72 : 305474 : { # 73 : 305474 : std::ostringstream oss; # 74 : 305474 : oss.imbue(std::locale::classic()); # 75 : 305474 : oss << t; # 76 : 305474 : return oss.str(); # 77 : 305474 : } # 78 : : # 79 : : /** # 80 : : * Check whether a container begins with the given prefix. # 81 : : */ # 82 : : template <typename T1, size_t PREFIX_LEN> # 83 : : NODISCARD inline bool HasPrefix(const T1& obj, # 84 : : const std::array<uint8_t, PREFIX_LEN>& prefix) # 85 : 1843001 : { # 86 : 1843001 : return obj.size() >= PREFIX_LEN && # 87 : 1843001 : std::equal(std::begin(prefix), std::end(prefix), std::begin(obj)); # 88 : 1843001 : } # 89 : : # 90 : : #endif // BITCOIN_UTIL_STRENCODINGS_H