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 inline static std::string
34 sqlEscape(std::string const& strSrc)
35 {
36  static boost::format f("X'%s'");
37  return str(boost::format(f) % strHex(strSrc));
38 }
39 
40 inline static std::string
41 sqlEscape(Blob const& vecSrc)
42 {
43  size_t size = vecSrc.size();
44 
45  if (size == 0)
46  return "X''";
47 
48  std::string j(size * 2 + 3, 0);
49 
50  unsigned char* oPtr = reinterpret_cast<unsigned char*>(&*j.begin());
51  const unsigned char* iPtr = &vecSrc[0];
52 
53  *oPtr++ = 'X';
54  *oPtr++ = '\'';
55 
56  for (int i = size; i != 0; --i)
57  {
58  unsigned char c = *iPtr++;
59  *oPtr++ = charHex(c >> 4);
60  *oPtr++ = charHex(c & 15);
61  }
62 
63  *oPtr++ = '\'';
64  return j;
65 }
66 
67 uint64_t
68 uintFromHex(std::string const& strSrc);
69 
70 template <class Iterator>
71 boost::optional<Blob>
72 strUnHex(std::size_t strSize, Iterator begin, Iterator end)
73 {
74  Blob out;
75 
76  out.reserve((strSize + 1) / 2);
77 
78  auto iter = begin;
79 
80  if (strSize & 1)
81  {
82  int c = charUnHex(*iter);
83 
84  if (c < 0)
85  return {};
86 
87  out.push_back(c);
88  ++iter;
89  }
90 
91  while (iter != end)
92  {
93  int cHigh = charUnHex(*iter);
94  ++iter;
95 
96  if (cHigh < 0)
97  return {};
98 
99  int cLow = charUnHex(*iter);
100  ++iter;
101 
102  if (cLow < 0)
103  return {};
104 
105  out.push_back(static_cast<unsigned char>((cHigh << 4) | cLow));
106  }
107 
108  return {std::move(out)};
109 }
110 
111 inline boost::optional<Blob>
112 strUnHex(std::string const& strSrc)
113 {
114  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
115 }
116 
117 inline boost::optional<Blob>
118 strViewUnHex(boost::string_view const& strSrc)
119 {
120  return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
121 }
122 
123 struct parsedURL
124 {
125  explicit parsedURL() = default;
126 
131  boost::optional<std::uint16_t> port;
133 
134  bool
135  operator==(parsedURL const& other) const
136  {
137  return scheme == other.scheme && domain == other.domain &&
138  port == other.port && path == other.path;
139  }
140 };
141 
142 bool
143 parseUrl(parsedURL& pUrl, std::string const& strUrl);
144 
147 
148 boost::optional<std::uint64_t>
149 to_uint64(std::string const& s);
150 
151 } // namespace ripple
152 
153 #endif
sstream
std::string
STL class.
ripple::charUnHex
int charUnHex(unsigned char c)
Converts a hex digit to the corresponding integer.
Definition: strHex.cpp:27
ripple::parsedURL
Definition: StringUtilities.h:123
ripple::parsedURL::password
std::string password
Definition: StringUtilities.h:129
ripple::uintFromHex
uint64_t uintFromHex(std::string const &strSrc)
Definition: StringUtilities.cpp:34
std::vector< unsigned char >
std::vector::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:72
ripple::parsedURL::operator==
bool operator==(parsedURL const &other) const
Definition: StringUtilities.h:135
ripple::parsedURL::username
std::string username
Definition: StringUtilities.h:128
ripple::sqlEscape
static std::string sqlEscape(std::string const &strSrc)
Definition: StringUtilities.h:34
ripple::parsedURL::path
std::string path
Definition: StringUtilities.h:132
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:55
ripple::parsedURL::port
boost::optional< std::uint16_t > port
Definition: StringUtilities.h:131
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::begin
T begin(T... args)
ripple::strViewUnHex
boost::optional< Blob > strViewUnHex(boost::string_view const &strSrc)
Definition: StringUtilities.h:118
ripple::charHex
char charHex(unsigned int digit)
Converts an integer to the corresponding hex digit.
Definition: strHex.h:41
ripple::parsedURL::scheme
std::string scheme
Definition: StringUtilities.h:127
std::size_t
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:67
ripple::parsedURL::domain
std::string domain
Definition: StringUtilities.h:130
std::string::cend
T cend(T... args)
string