rippled
Loading...
Searching...
No Matches
StringUtilities.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_STRINGUTILITIES_H_INCLUDED
21#define RIPPLE_BASICS_STRINGUTILITIES_H_INCLUDED
22
23#include <xrpl/basics/Blob.h>
24#include <xrpl/basics/strHex.h>
25
26#include <boost/format.hpp>
27#include <boost/utility/string_view.hpp>
28
29#include <array>
30#include <cstdint>
31#include <optional>
32#include <string>
33
34namespace ripple {
35
47sqlBlobLiteral(Blob const& blob);
48
49template <class Iterator>
51strUnHex(std::size_t strSize, Iterator begin, Iterator end)
52{
53 static constexpr std::array<int, 256> const unxtab = []() {
55
56 for (auto& x : t)
57 x = -1;
58
59 for (int i = 0; i < 10; ++i)
60 t['0' + i] = i;
61
62 for (int i = 0; i < 6; ++i)
63 {
64 t['A' + i] = 10 + i;
65 t['a' + i] = 10 + i;
66 }
67
68 return t;
69 }();
70
71 Blob out;
72
73 out.reserve((strSize + 1) / 2);
74
75 auto iter = begin;
76
77 if (strSize & 1)
78 {
79 int c = unxtab[*iter++];
80
81 if (c < 0)
82 return {};
83
84 out.push_back(c);
85 }
86
87 while (iter != end)
88 {
89 int cHigh = unxtab[*iter++];
90
91 if (cHigh < 0)
92 return {};
93
94 int cLow = unxtab[*iter++];
95
96 if (cLow < 0)
97 return {};
98
99 out.push_back(static_cast<unsigned char>((cHigh << 4) | cLow));
100 }
101
102 return {std::move(out)};
103}
104
106strUnHex(std::string const& strSrc)
107{
108 return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
109}
110
113{
114 return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
115}
116
118{
119 explicit parsedURL() = default;
120
127
128 bool
129 operator==(parsedURL const& other) const
130 {
131 return scheme == other.scheme && domain == other.domain &&
132 port == other.port && path == other.path;
133 }
134};
135
136bool
137parseUrl(parsedURL& pUrl, std::string const& strUrl);
138
141
143to_uint64(std::string const& s);
144
151bool
153
154} // namespace ripple
155
156#endif
T cbegin(T... args)
T cend(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
std::string trim_whitespace(std::string str)
std::optional< std::uint64_t > to_uint64(std::string const &s)
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
std::optional< Blob > strViewUnHex(std::string_view strSrc)
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:30
bool isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
T reserve(T... args)
T size(T... args)
parsedURL()=default
bool operator==(parsedURL const &other) const
std::optional< std::uint16_t > port