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
|
|
|
|
|
2021-09-11 13:02:47 +02:00
|
|
|
#include <util/spanparsing.h>
|
2019-12-11 11:00:52 +00:00
|
|
|
|
2020-08-31 10:39:00 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2022-05-05 08:28:29 +02:00
|
|
|
#include <cstdint>
|
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>
|
2022-05-02 21:44:09 +02:00
|
|
|
#include <string_view>
|
2019-08-20 14:51:43 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2022-08-08 11:44:27 +01:00
|
|
|
void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
|
2022-05-05 08:28:29 +02:00
|
|
|
|
2021-09-11 13:02:47 +02:00
|
|
|
[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, char sep)
|
|
|
|
{
|
|
|
|
return spanparsing::Split<std::string>(str, sep);
|
|
|
|
}
|
|
|
|
|
2022-05-02 21:44:09 +02:00
|
|
|
[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, std::string_view separators)
|
|
|
|
{
|
|
|
|
return spanparsing::Split<std::string>(str, separators);
|
|
|
|
}
|
|
|
|
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view 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) {
|
2022-04-04 15:05:47 -04:00
|
|
|
return {};
|
2019-12-11 09:55:00 +00:00
|
|
|
}
|
|
|
|
std::string::size_type end = str.find_last_not_of(pattern);
|
|
|
|
return str.substr(front, end - front + 1);
|
|
|
|
}
|
|
|
|
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] inline std::string TrimString(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
|
|
|
|
{
|
|
|
|
return std::string(TrimStringView(str, pattern));
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] inline std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
|
2020-08-25 20:22:28 +00:00
|
|
|
{
|
|
|
|
if (str.substr(0, prefix.size()) == prefix) {
|
|
|
|
return str.substr(prefix.size());
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] inline std::string RemovePrefix(std::string_view str, std::string_view prefix)
|
|
|
|
{
|
|
|
|
return std::string(RemovePrefixView(str, prefix));
|
|
|
|
}
|
|
|
|
|
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)
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-04 15:05:47 -04:00
|
|
|
template <typename T, typename T2>
|
|
|
|
T Join(const std::vector<T>& list, const T2& separator)
|
2020-05-09 14:45:47 +03:00
|
|
|
{
|
|
|
|
return Join(list, separator, [](const T& i) { return i; });
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2022-04-27 14:23:29 +02:00
|
|
|
[[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept
|
2019-12-11 11:00:52 +00:00
|
|
|
{
|
2022-04-04 15:05:47 -04:00
|
|
|
for (auto c : str) {
|
|
|
|
if (c == 0) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-12-11 11:00:52 +00:00
|
|
|
}
|
|
|
|
|
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
|