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 <sstream>
33#include <string>
34
35namespace ripple {
36
48sqlBlobLiteral(Blob const& blob);
49
50template <class Iterator>
52strUnHex(std::size_t strSize, Iterator begin, Iterator end)
53{
54 static constexpr std::array<int, 256> const unxtab = []() {
56
57 for (auto& x : t)
58 x = -1;
59
60 for (int i = 0; i < 10; ++i)
61 t['0' + i] = i;
62
63 for (int i = 0; i < 6; ++i)
64 {
65 t['A' + i] = 10 + i;
66 t['a' + i] = 10 + i;
67 }
68
69 return t;
70 }();
71
72 Blob out;
73
74 out.reserve((strSize + 1) / 2);
75
76 auto iter = begin;
77
78 if (strSize & 1)
79 {
80 int c = unxtab[*iter++];
81
82 if (c < 0)
83 return {};
84
85 out.push_back(c);
86 }
87
88 while (iter != end)
89 {
90 int cHigh = unxtab[*iter++];
91
92 if (cHigh < 0)
93 return {};
94
95 int cLow = unxtab[*iter++];
96
97 if (cLow < 0)
98 return {};
99
100 out.push_back(static_cast<unsigned char>((cHigh << 4) | cLow));
101 }
102
103 return {std::move(out)};
104}
105
107strUnHex(std::string const& strSrc)
108{
109 return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
110}
111
114{
115 return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
116}
117
119{
120 explicit parsedURL() = default;
121
128
129 bool
130 operator==(parsedURL const& other) const
131 {
132 return scheme == other.scheme && domain == other.domain &&
133 port == other.port && path == other.path;
134 }
135};
136
137bool
138parseUrl(parsedURL& pUrl, std::string const& strUrl);
139
142
144to_uint64(std::string const& s);
145
152bool
154
155} // namespace ripple
156
157#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:26
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 size(T... args)
parsedURL()=default
std::string username
bool operator==(parsedURL const &other) const
std::string password
std::optional< std::uint16_t > port