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 #include <array>
29 #include <optional>
30 #include <sstream>
31 #include <string>
32 
33 namespace ripple {
34 
46 sqlBlobLiteral(Blob const& blob);
47 
48 template <class Iterator>
50 strUnHex(std::size_t strSize, Iterator begin, Iterator end)
51 {
52  static constexpr std::array<int, 256> const unxtab = []() {
54 
55  for (auto& x : t)
56  x = -1;
57 
58  for (int i = 0; i < 10; ++i)
59  t['0' + i] = i;
60 
61  for (int i = 0; i < 6; ++i)
62  {
63  t['A' + i] = 10 + i;
64  t['a' + i] = 10 + i;
65  }
66 
67  return t;
68  }();
69 
70  Blob out;
71 
72  out.reserve((strSize + 1) / 2);
73 
74  auto iter = begin;
75 
76  if (strSize & 1)
77  {
78  int c = unxtab[*iter++];
79 
80  if (c < 0)
81  return {};
82 
83  out.push_back(c);
84  }
85 
86  while (iter != end)
87  {
88  int cHigh = unxtab[*iter++];
89 
90  if (cHigh < 0)
91  return {};
92 
93  int cLow = unxtab[*iter++];
94 
95  if (cLow < 0)
96  return {};
97 
98  out.push_back(static_cast<unsigned char>((cHigh << 4) | cLow));
99  }
100 
101  return {std::move(out)};
102 }
103 
104 inline std::optional<Blob>
105 strUnHex(std::string const& strSrc)
106 {
107  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
108 }
109 
110 inline std::optional<Blob>
111 strViewUnHex(boost::string_view const& strSrc)
112 {
113  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
114 }
115 
116 struct parsedURL
117 {
118  explicit parsedURL() = default;
119 
126 
127  bool
128  operator==(parsedURL const& other) const
129  {
130  return scheme == other.scheme && domain == other.domain &&
131  port == other.port && path == other.path;
132  }
133 };
134 
135 bool
136 parseUrl(parsedURL& pUrl, std::string const& strUrl);
137 
140 
142 to_uint64(std::string const& s);
143 
150 bool
152 
153 } // namespace ripple
154 
155 #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:116
ripple::parsedURL::password
std::string password
Definition: StringUtilities.h:122
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:111
ripple::parsedURL::operator==
bool operator==(parsedURL const &other) const
Definition: StringUtilities.h:128
ripple::parsedURL::username
std::string username
Definition: StringUtilities.h:121
ripple::parsedURL::path
std::string path
Definition: StringUtilities.h:125
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:124
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
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:120
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:123
std::string::cend
T cend(T... args)
ripple::strUnHex
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition: StringUtilities.h:50
ripple::sqlBlobLiteral
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
Definition: StringUtilities.cpp:33
string