rippled
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 <ripple/basics/Blob.h>
24 #include <ripple/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 
35 namespace ripple {
36 
48 sqlBlobLiteral(Blob const& blob);
49 
50 template <class Iterator>
52 strUnHex(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 
106 inline std::optional<Blob>
107 strUnHex(std::string const& strSrc)
108 {
109  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
110 }
111 
112 inline std::optional<Blob>
113 strViewUnHex(boost::string_view const& strSrc)
114 {
115  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
116 }
117 
118 struct parsedURL
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 
137 bool
138 parseUrl(parsedURL& pUrl, std::string const& strUrl);
139 
142 
144 to_uint64(std::string const& s);
145 
152 bool
154 
155 } // namespace ripple
156 
157 #endif
sstream
ripple::Blob
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition: Blob.h:30
std::string
STL class.
ripple::parsedURL
Definition: StringUtilities.h:118
ripple::parsedURL::password
std::string password
Definition: StringUtilities.h:124
std::vector< unsigned char >
std::string::size
T size(T... args)
ripple::strViewUnHex
std::optional< Blob > strViewUnHex(boost::string_view const &strSrc)
Definition: StringUtilities.h:113
ripple::parsedURL::operator==
bool operator==(parsedURL const &other) const
Definition: StringUtilities.h:130
ripple::parsedURL::username
std::string username
Definition: StringUtilities.h:123
ripple::parsedURL::path
std::string path
Definition: StringUtilities.h:127
ripple::trim_whitespace
std::string trim_whitespace(std::string str)
ripple::QualityDirection::out
@ out
ripple::parsedURL::port
std::optional< std::uint16_t > port
Definition: StringUtilities.h:126
ripple::to_uint64
std::optional< std::uint64_t > to_uint64(std::string const &s)
array
ripple::parseUrl
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
Definition: StringUtilities.cpp:47
cstdint
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::parsedURL::parsedURL
parsedURL()=default
std::string::cbegin
T cbegin(T... args)
optional
ripple::parsedURL::scheme
std::string scheme
Definition: StringUtilities.h:122
std::size_t
ripple::isProperlyFormedTomlDomain
bool isProperlyFormedTomlDomain(std::string const &domain)
Determines if the given string looks like a TOML-file hosting domain.
ripple::parsedURL::domain
std::string domain
Definition: StringUtilities.h:125
std::string::cend
T cend(T... args)
ripple::strUnHex
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition: StringUtilities.h:52
ripple::sqlBlobLiteral
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
Definition: StringUtilities.cpp:33
string