xrpld
Loading...
Searching...
No Matches
StringUtilities.h
1#pragma once
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/strHex.h>
5
6#include <boost/format.hpp>
7#include <boost/utility/string_view.hpp>
8
9#include <array>
10#include <concepts>
11#include <cstdint>
12#include <optional>
13#include <string>
14#include <string_view>
15#include <type_traits>
16
17namespace xrpl {
18
29std::string
30sqlBlobLiteral(Blob const& blob);
31
32namespace detail {
33
34template <typename T>
35concept SomeChar = std::same_as<std::remove_cvref_t<T>, int8_t> ||
36 std::same_as<std::remove_cvref_t<T>, char> || std::same_as<std::remove_cvref_t<T>, uint8_t>;
37
38inline constexpr std::array<std::optional<int>, 256> const kDigitLookupTable = []() {
40
41 for (int i = 0; i < 10; ++i)
42 t['0' + i] = i;
43
44 for (int i = 0; i < 6; ++i)
45 {
46 t['A' + i] = 10 + i;
47 t['a' + i] = 10 + i;
48 }
49
50 return t;
51}();
52
55{
56 return kDigitLookupTable[static_cast<uint8_t>(hexChar)];
57}
58
59} // namespace detail
60
61template <class Iterator>
63strUnHex(std::size_t strSize, Iterator begin, Iterator end)
64{
65 Blob out;
66
67 out.reserve((strSize + 1) / 2);
68
69 auto iter = begin;
70
71 if (strSize & 1)
72 {
73 auto const c = detail::hexCharToInt(*iter++);
74 if (!c.has_value())
75 return {};
76
77 out.push_back(static_cast<unsigned char>(*c));
78 }
79
80 while (iter != end)
81 {
82 auto const cHigh = detail::hexCharToInt(*iter++);
83
84 if (!cHigh.has_value())
85 return {};
86
87 auto const cLow = detail::hexCharToInt(*iter++);
88
89 if (!cLow.has_value())
90 return {};
91
92 out.push_back(static_cast<unsigned char>((*cHigh << 4) | *cLow));
93 }
94
95 return {std::move(out)};
96}
97
100{
101 return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
102}
103
105{
106 explicit ParsedUrl() = default;
107
114
115 bool
116 operator==(ParsedUrl const& other) const
117 {
118 return scheme == other.scheme && domain == other.domain && port == other.port &&
119 path == other.path;
120 }
121};
122
123bool
124parseUrl(ParsedUrl& pUrl, std::string const& strUrl);
125
128
130toUInt64(std::string const& s);
131
138bool
140
141} // namespace xrpl
T cbegin(T... args)
T cend(T... args)
std::optional< int > hexCharToInt(SomeChar auto hexChar)
constexpr std::array< std::optional< int >, 256 > const kDigitLookupTable
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
std::string trimWhitespace(std::string str)
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
std::optional< std::uint64_t > toUInt64(std::string const &s)
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
bool parseUrl(ParsedUrl &pUrl, std::string const &strUrl)
T push_back(T... args)
T reserve(T... args)
T size(T... args)
bool operator==(ParsedUrl const &other) const
ParsedUrl()=default
std::optional< std::uint16_t > port
std::string password
std::string username