2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2019-2021 The Bitcoin Core developers
|
2019-08-20 14:51:43 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_UTIL_STRING_H
|
|
|
|
#define BITCOIN_UTIL_STRING_H
|
|
|
|
|
2019-12-11 11:00:52 +00:00
|
|
|
#include <attributes.h>
|
|
|
|
|
2020-08-31 10:39:00 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2019-12-11 11:00:52 +00:00
|
|
|
#include <cstring>
|
2020-02-12 23:01:45 -05:00
|
|
|
#include <locale>
|
|
|
|
#include <sstream>
|
2019-08-20 14:51:43 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-11-26 09:05:59 +00:00
|
|
|
[[nodiscard]] inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v")
|
2019-12-11 09:55:00 +00:00
|
|
|
{
|
|
|
|
std::string::size_type front = str.find_first_not_of(pattern);
|
|
|
|
if (front == std::string::npos) {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
std::string::size_type end = str.find_last_not_of(pattern);
|
|
|
|
return str.substr(front, end - front + 1);
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:22:28 +00:00
|
|
|
[[nodiscard]] inline std::string RemovePrefix(const std::string& str, const std::string& prefix)
|
|
|
|
{
|
|
|
|
if (str.substr(0, prefix.size()) == prefix) {
|
|
|
|
return str.substr(prefix.size());
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:51:43 -04:00
|
|
|
/**
|
|
|
|
* Join a list of items
|
|
|
|
*
|
|
|
|
* @param list The list to join
|
|
|
|
* @param separator The separator
|
|
|
|
* @param unary_op Apply this operator to each item in the list
|
|
|
|
*/
|
2020-05-09 14:45:47 +03:00
|
|
|
template <typename T, typename BaseType, typename UnaryOp>
|
|
|
|
auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_op)
|
|
|
|
-> decltype(unary_op(list.at(0)))
|
2019-08-20 14:51:43 -04:00
|
|
|
{
|
2020-05-09 14:45:47 +03:00
|
|
|
decltype(unary_op(list.at(0))) ret;
|
2019-08-20 14:51:43 -04:00
|
|
|
for (size_t i = 0; i < list.size(); ++i) {
|
|
|
|
if (i > 0) ret += separator;
|
|
|
|
ret += unary_op(list.at(i));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:47 +03:00
|
|
|
template <typename T>
|
|
|
|
T Join(const std::vector<T>& list, const T& separator)
|
|
|
|
{
|
|
|
|
return Join(list, separator, [](const T& i) { return i; });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above.
|
2019-08-20 14:51:43 -04:00
|
|
|
inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
|
|
|
|
{
|
2020-05-09 14:45:47 +03:00
|
|
|
return Join<std::string>(list, separator);
|
2019-08-20 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:51:57 +03:00
|
|
|
/**
|
|
|
|
* Create an unordered multi-line list of items.
|
|
|
|
*/
|
|
|
|
inline std::string MakeUnorderedList(const std::vector<std::string>& items)
|
|
|
|
{
|
|
|
|
return Join(items, "\n", [](const std::string& item) { return "- " + item; });
|
|
|
|
}
|
|
|
|
|
2019-12-11 11:00:52 +00:00
|
|
|
/**
|
|
|
|
* Check if a string does not contain any embedded NUL (\0) characters
|
|
|
|
*/
|
2020-11-26 09:05:59 +00:00
|
|
|
[[nodiscard]] inline bool ValidAsCString(const std::string& str) noexcept
|
2019-12-11 11:00:52 +00:00
|
|
|
{
|
|
|
|
return str.size() == strlen(str.c_str());
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:01:45 -05:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2020-08-31 10:39:00 +02:00
|
|
|
/**
|
|
|
|
* Check whether a container begins with the given prefix.
|
|
|
|
*/
|
|
|
|
template <typename T1, size_t PREFIX_LEN>
|
2020-11-26 09:05:59 +00:00
|
|
|
[[nodiscard]] inline bool HasPrefix(const T1& obj,
|
2020-08-31 10:39:00 +02:00
|
|
|
const std::array<uint8_t, PREFIX_LEN>& prefix)
|
|
|
|
{
|
|
|
|
return obj.size() >= PREFIX_LEN &&
|
|
|
|
std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:19:44 +01:00
|
|
|
#endif // BITCOIN_UTIL_STRING_H
|