2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-12 02:17:40 +00:00
|
|
|
|
2023-10-19 21:59:57 +00:00
|
|
|
#include <fmt/format.h>
|
2023-10-24 13:18:03 +00:00
|
|
|
|
|
|
|
#include <charconv>
|
2020-08-31 20:07:17 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <iterator>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
2022-07-18 20:04:30 +00:00
|
|
|
|
2018-12-12 02:17:40 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2020-08-31 20:07:17 +00:00
|
|
|
/// Returns true if the first argument begins with the second argument
|
2022-07-16 00:41:14 +00:00
|
|
|
inline constexpr bool
|
2020-08-31 20:07:17 +00:00
|
|
|
starts_with(std::string_view str, std::string_view prefix)
|
|
|
|
{
|
|
|
|
return str.substr(0, prefix.size()) == prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the first argument ends with the second argument
|
2022-07-16 00:41:14 +00:00
|
|
|
inline constexpr bool
|
2020-08-31 20:07:17 +00:00
|
|
|
ends_with(std::string_view str, std::string_view suffix)
|
|
|
|
{
|
|
|
|
return str.size() >= suffix.size() && str.substr(str.size() - suffix.size()) == suffix;
|
|
|
|
}
|
|
|
|
|
2021-05-11 11:22:36 +00:00
|
|
|
/// removes a prefix from a string if it exists
|
2022-07-16 00:41:14 +00:00
|
|
|
inline constexpr std::string_view
|
2021-05-11 11:22:36 +00:00
|
|
|
strip_prefix(std::string_view str, std::string_view prefix)
|
|
|
|
{
|
|
|
|
if (starts_with(str, prefix))
|
|
|
|
return str.substr(prefix.size());
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:07:17 +00:00
|
|
|
/// Splits a string on some delimiter string and returns a vector of string_view's pointing into
|
|
|
|
/// the pieces of the original string. The pieces are valid only as long as the original string
|
|
|
|
/// remains valid. Leading and trailing empty substrings are not removed. If delim is empty you
|
|
|
|
/// get back a vector of string_views each viewing one character. If `trim` is true then leading
|
|
|
|
/// and trailing empty values will be suppressed.
|
|
|
|
///
|
|
|
|
/// auto v = split("ab--c----de", "--"); // v is {"ab", "c", "", "de"}
|
|
|
|
/// auto v = split("abc", ""); // v is {"a", "b", "c"}
|
|
|
|
/// auto v = split("abc", "c"); // v is {"ab", ""}
|
|
|
|
/// auto v = split("abc", "c", true); // v is {"ab"}
|
|
|
|
/// auto v = split("-a--b--", "-"); // v is {"", "a", "", "b", "", ""}
|
|
|
|
/// auto v = split("-a--b--", "-", true); // v is {"a", "", "b"}
|
|
|
|
///
|
|
|
|
std::vector<std::string_view>
|
|
|
|
split(std::string_view str, std::string_view delim, bool trim = false);
|
|
|
|
|
|
|
|
/// Splits a string on any 1 or more of the given delimiter characters and returns a vector of
|
|
|
|
/// string_view's pointing into the pieces of the original string. If delims is empty this works
|
|
|
|
/// the same as split(). `trim` works like split (suppresses leading and trailing empty string
|
|
|
|
/// pieces).
|
|
|
|
///
|
|
|
|
/// auto v = split_any("abcdedf", "dcx"); // v is {"ab", "e", "f"}
|
|
|
|
std::vector<std::string_view>
|
|
|
|
split_any(std::string_view str, std::string_view delims, bool trim = false);
|
|
|
|
|
|
|
|
/// Joins [begin, end) with a delimiter and returns the resulting string. Elements can be
|
2022-07-18 20:04:30 +00:00
|
|
|
/// anything that is fmt formattable.
|
2020-08-31 20:07:17 +00:00
|
|
|
template <typename It>
|
|
|
|
std::string
|
|
|
|
join(std::string_view delimiter, It begin, It end)
|
|
|
|
{
|
2022-07-18 20:04:30 +00:00
|
|
|
return fmt::format("{}", fmt::join(delimiter, begin, end));
|
2020-08-31 20:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wrapper around the above that takes a container and passes c.begin(), c.end() to the above.
|
|
|
|
template <typename Container>
|
|
|
|
std::string
|
|
|
|
join(std::string_view delimiter, const Container& c)
|
|
|
|
{
|
|
|
|
return join(delimiter, c.begin(), c.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an integer of some sort from a string, requiring that the entire string be consumed
|
|
|
|
/// during parsing. Return false if parsing failed, sets `value` and returns true if the entire
|
|
|
|
/// string was consumed.
|
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
parse_int(const std::string_view str, T& value, int base = 10)
|
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
auto* strend = str.data() + str.size();
|
|
|
|
auto [p, ec] = std::from_chars(str.data(), strend, tmp, base);
|
|
|
|
if (ec != std::errc() || p != strend)
|
|
|
|
return false;
|
|
|
|
value = tmp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
lowercase_ascii_string(std::string src);
|
2023-10-19 11:49:46 +00:00
|
|
|
|
|
|
|
std::string_view
|
|
|
|
TrimWhitespace(std::string_view str);
|
2018-12-12 02:17:40 +00:00
|
|
|
} // namespace llarp
|