xrpld
Loading...
Searching...
No Matches
StringUtilities.h
1#pragma once
2
3#include <xrpl/basics/Blob.h>
4
5#include <boost/utility/string_view.hpp>
6
7#include <array>
8#include <concepts>
9#include <cstddef>
10#include <cstdint>
11#include <optional>
12#include <string>
13#include <string_view>
14#include <type_traits>
15#include <utility>
16
17namespace xrpl {
18
30std::string
31sqlBlobLiteral(Blob const& blob);
32
33namespace detail {
34
35template <typename T>
36concept SomeChar = std::same_as<std::remove_cvref_t<T>, int8_t> ||
37 std::same_as<std::remove_cvref_t<T>, char> || std::same_as<std::remove_cvref_t<T>, uint8_t>;
38
39inline constexpr std::array<std::optional<int>, 256> const kDigitLookupTable = []() {
41
42 for (int i = 0; i < 10; ++i)
43 t['0' + i] = i;
44
45 for (int i = 0; i < 6; ++i)
46 {
47 t['A' + i] = 10 + i;
48 t['a' + i] = 10 + i;
49 }
50
51 return t;
52}();
53
56{
57 return kDigitLookupTable[static_cast<uint8_t>(hexChar)];
58}
59
60} // namespace detail
61
62template <class Iterator>
64strUnHex(std::size_t strSize, Iterator begin, Iterator end)
65{
66 Blob out;
67
68 out.reserve((strSize + 1) / 2);
69
70 auto iter = begin;
71
72 if (strSize & 1)
73 {
74 auto const c = detail::hexCharToInt(*iter++);
75 if (!c.has_value())
76 return {};
77
78 out.push_back(static_cast<unsigned char>(*c));
79 }
80
81 while (iter != end)
82 {
83 auto const cHigh = detail::hexCharToInt(*iter++);
84
85 if (!cHigh.has_value())
86 return {};
87
88 auto const cLow = detail::hexCharToInt(*iter++);
89
90 if (!cLow.has_value())
91 return {};
92
93 out.push_back(static_cast<unsigned char>((*cHigh << 4) | *cLow));
94 }
95
96 return {std::move(out)};
97}
98
101{
102 return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
103}
104
106{
107 explicit ParsedUrl() = default;
108
115
116 bool
117 operator==(ParsedUrl const& other) const
118 {
119 return scheme == other.scheme && domain == other.domain && port == other.port &&
120 path == other.path;
121 }
122};
123
124bool
125parseUrl(ParsedUrl& pUrl, std::string const& strUrl);
126
138
151
153toUInt64(std::string const& s);
154
162bool
164
165} // 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
std::string toLower(std::string str)
Fold ASCII upper case letters to lower case.
bool isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
std::string trimWhitespace(std::string str)
Remove leading and trailing ASCII whitespace.
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:11
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