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/optional.hpp>
28 #include <boost/utility/string_view.hpp>
29 #include <sstream>
30 #include <string>
31 
32 namespace ripple {
33 
45 sqlBlobLiteral(Blob const& blob);
46 
47 template <class Iterator>
48 boost::optional<Blob>
49 strUnHex(std::size_t strSize, Iterator begin, Iterator end)
50 {
51  Blob out;
52 
53  out.reserve((strSize + 1) / 2);
54 
55  auto iter = begin;
56 
57  if (strSize & 1)
58  {
59  int c = charUnHex(*iter);
60 
61  if (c < 0)
62  return {};
63 
64  out.push_back(c);
65  ++iter;
66  }
67 
68  while (iter != end)
69  {
70  int cHigh = charUnHex(*iter);
71  ++iter;
72 
73  if (cHigh < 0)
74  return {};
75 
76  int cLow = charUnHex(*iter);
77  ++iter;
78 
79  if (cLow < 0)
80  return {};
81 
82  out.push_back(static_cast<unsigned char>((cHigh << 4) | cLow));
83  }
84 
85  return {std::move(out)};
86 }
87 
88 inline boost::optional<Blob>
89 strUnHex(std::string const& strSrc)
90 {
91  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
92 }
93 
94 inline boost::optional<Blob>
95 strViewUnHex(boost::string_view const& strSrc)
96 {
97  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
98 }
99 
100 struct parsedURL
101 {
102  explicit parsedURL() = default;
103 
108  boost::optional<std::uint16_t> port;
110 
111  bool
112  operator==(parsedURL const& other) const
113  {
114  return scheme == other.scheme && domain == other.domain &&
115  port == other.port && path == other.path;
116  }
117 };
118 
119 bool
120 parseUrl(parsedURL& pUrl, std::string const& strUrl);
121 
124 
125 boost::optional<std::uint64_t>
126 to_uint64(std::string const& s);
127 
134 bool
136 
137 } // namespace ripple
138 
139 #endif
sstream
ripple::Blob
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition: Blob.h:30
std::string
STL class.
ripple::charUnHex
int charUnHex(unsigned char c)
Converts a hex digit to the corresponding integer.
Definition: strHex.cpp:26
ripple::parsedURL
Definition: StringUtilities.h:100
ripple::parsedURL::password
std::string password
Definition: StringUtilities.h:106
std::vector< unsigned char >
std::string::size
T size(T... args)
ripple::to_uint64
boost::optional< std::uint64_t > to_uint64(std::string const &s)
ripple::strUnHex
boost::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition: StringUtilities.h:49
ripple::parsedURL::operator==
bool operator==(parsedURL const &other) const
Definition: StringUtilities.h:112
ripple::parsedURL::username
std::string username
Definition: StringUtilities.h:105
ripple::parsedURL::path
std::string path
Definition: StringUtilities.h:109
ripple::trim_whitespace
std::string trim_whitespace(std::string str)
ripple::QualityDirection::out
@ out
ripple::parseUrl
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
Definition: StringUtilities.cpp:47
ripple::parsedURL::port
boost::optional< std::uint16_t > port
Definition: StringUtilities.h:108
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)
ripple::strViewUnHex
boost::optional< Blob > strViewUnHex(boost::string_view const &strSrc)
Definition: StringUtilities.h:95
ripple::parsedURL::scheme
std::string scheme
Definition: StringUtilities.h:104
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:107
std::string::cend
T cend(T... args)
ripple::sqlBlobLiteral
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
Definition: StringUtilities.cpp:33
string