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 <cstring> | #include <cstring> | ||||
#include <locale> | |||||
#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") { | ||||
std::string::size_type front = str.find_first_not_of(pattern); | std::string::size_type front = str.find_first_not_of(pattern); | ||||
if (front == std::string::npos) { | if (front == std::string::npos) { | ||||
return std::string(); | return std::string(); | ||||
Show All 35 Lines | |||||
/** | /** | ||||
* Check if a string does not contain any embedded NUL (\0) characters | * Check if a string does not contain any embedded NUL (\0) characters | ||||
*/ | */ | ||||
NODISCARD inline bool ValidAsCString(const std::string &str) noexcept { | NODISCARD inline bool ValidAsCString(const std::string &str) noexcept { | ||||
return str.size() == strlen(str.c_str()); | return str.size() == strlen(str.c_str()); | ||||
} | } | ||||
/** | |||||
* Locale-independent version of std::to_string | |||||
*/ | |||||
template <typename T> std::string ToString(const T &t) { | |||||
std::ostringstream oss; | |||||
oss.imbue(std::locale::classic()); | |||||
oss << t; | |||||
return oss.str(); | |||||
} | |||||
#endif // BITCOIN_UTIL_STRING_H | #endif // BITCOIN_UTIL_STRING_H |