Changeset View
Changeset View
Standalone View
Standalone View
src/util/string.h
// Copyright (c) 2019 The Bitcoin Core developers | // Copyright (c) 2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_UTIL_STRING_H | #ifndef BITCOIN_UTIL_STRING_H | ||||
#define BITCOIN_UTIL_STRING_H | #define BITCOIN_UTIL_STRING_H | ||||
#include <attributes.h> | #include <attributes.h> | ||||
#include <algorithm> | |||||
#include <array> | |||||
#include <cstring> | #include <cstring> | ||||
#include <locale> | #include <locale> | ||||
#include <sstream> | #include <sstream> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
NODISCARD inline std::string | NODISCARD inline std::string | ||||
TrimString(const std::string &str, const std::string &pattern = " \f\n\r\t\v") { | TrimString(const std::string &str, const std::string &pattern = " \f\n\r\t\v") { | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
template <typename T> std::string ToString(const T &t) { | template <typename T> std::string ToString(const T &t) { | ||||
std::ostringstream oss; | std::ostringstream oss; | ||||
oss.imbue(std::locale::classic()); | oss.imbue(std::locale::classic()); | ||||
oss << t; | oss << t; | ||||
return oss.str(); | return oss.str(); | ||||
} | } | ||||
/** | |||||
* Check whether a container begins with the given prefix. | |||||
*/ | |||||
template <typename T1, size_t PREFIX_LEN> | |||||
NODISCARD inline bool HasPrefix(const T1 &obj, | |||||
const std::array<uint8_t, PREFIX_LEN> &prefix) { | |||||
return obj.size() >= PREFIX_LEN && | |||||
std::equal(std::begin(prefix), std::end(prefix), std::begin(obj)); | |||||
} | |||||
#endif // BITCOIN_UTIL_STRING_H | #endif // BITCOIN_UTIL_STRING_H |