string-helper.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_UTIL_STRING_HELPER_HPP
23 #define NDN_UTIL_STRING_HELPER_HPP
24 
26 
27 namespace ndn {
28 
29 class StringHelperError : public std::invalid_argument
30 {
31 public:
32  using std::invalid_argument::invalid_argument;
33 };
34 
44 void
45 printHex(std::ostream& os, uint64_t num, bool wantUpperCase = false);
46 
66 void
67 printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase = true);
68 
76 inline void
77 printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase = true)
78 {
79  return printHex(os, buffer.data(), buffer.size(), wantUpperCase);
80 }
81 
93 class AsHex
94 {
95 public:
96  constexpr explicit
97  AsHex(uint64_t val) noexcept
98  : m_value(val)
99  {
100  }
101 
102 private:
103  friend std::ostream&
104  operator<<(std::ostream& os, const AsHex& hex)
105  {
106  printHex(os, hex.m_value, os.flags() & std::ostream::uppercase);
107  return os;
108  }
109 
110 private:
111  uint64_t m_value;
112 };
113 
132 NDN_CXX_NODISCARD std::string
133 toHex(const uint8_t* buffer, size_t length, bool wantUpperCase = true);
134 
141 NDN_CXX_NODISCARD inline std::string
142 toHex(const Buffer& buffer, bool wantUpperCase = true)
143 {
144  return toHex(buffer.data(), buffer.size(), wantUpperCase);
145 }
146 
153 shared_ptr<Buffer>
154 fromHex(const std::string& hexString);
155 
159 NDN_CXX_NODISCARD constexpr char
160 toHexChar(unsigned int n, bool wantUpperCase = true) noexcept
161 {
162  return wantUpperCase ?
163  "0123456789ABCDEF"[n & 0xf] :
164  "0123456789abcdef"[n & 0xf];
165 }
166 
170 NDN_CXX_NODISCARD constexpr int
171 fromHexChar(char c) noexcept
172 {
173  return (c >= '0' && c <= '9') ? int(c - '0') :
174  (c >= 'A' && c <= 'F') ? int(c - 'A' + 10) :
175  (c >= 'a' && c <= 'f') ? int(c - 'a' + 10) :
176  -1;
177 }
178 
195 NDN_CXX_NODISCARD std::string
196 escape(const std::string& str);
197 
198 void
199 escape(std::ostream& os, const char* str, size_t len);
200 
214 NDN_CXX_NODISCARD std::string
215 unescape(const std::string& str);
216 
217 void
218 unescape(std::ostream& os, const char* str, size_t len);
219 
220 } // namespace ndn
221 
222 #endif // NDN_UTIL_STRING_HELPER_HPP
Definition: data.cpp:26
std::string toHex(const uint8_t *buffer, size_t length, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:383
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
std::string unescape(const std::string &str)
Decode a percent-encoded string.
constexpr char toHexChar(unsigned int n, bool wantUpperCase=true) noexcept
Convert (the least significant nibble of) n to the corresponding hex character.
std::string escape(const std::string &str)
Percent-encode a string.
constexpr int fromHexChar(char c) noexcept
Convert the hex character c to an integer in [0, 15], or -1 if it&#39;s not a hex character.
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
constexpr AsHex(uint64_t val) noexcept