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-2022 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_CXX_UTIL_STRING_HELPER_HPP
23 #define NDN_CXX_UTIL_STRING_HELPER_HPP
24 
26 #include "ndn-cxx/util/span.hpp"
27 
28 namespace ndn {
29 
30 class StringHelperError : public std::invalid_argument
31 {
32 public:
33  using std::invalid_argument::invalid_argument;
34 };
35 
45 void
46 printHex(std::ostream& os, uint64_t num, bool wantUpperCase = false);
47 
58 void
59 printHex(std::ostream& os, span<const uint8_t> buffer, bool wantUpperCase = true);
60 
72 class AsHex
73 {
74 public:
75  constexpr explicit
76  AsHex(uint64_t val) noexcept
77  : m_value(val)
78  {
79  }
80 
81 private:
82  friend std::ostream&
83  operator<<(std::ostream& os, const AsHex& hex)
84  {
85  printHex(os, hex.m_value, os.flags() & std::ostream::uppercase);
86  return os;
87  }
88 
89 private:
90  uint64_t m_value;
91 };
92 
102 NDN_CXX_NODISCARD std::string
103 toHex(span<const uint8_t> buffer, bool wantUpperCase = true);
104 
111 shared_ptr<Buffer>
112 fromHex(const std::string& hexString);
113 
117 NDN_CXX_NODISCARD constexpr char
118 toHexChar(unsigned int n, bool wantUpperCase = true) noexcept
119 {
120  return wantUpperCase ?
121  "0123456789ABCDEF"[n & 0xf] :
122  "0123456789abcdef"[n & 0xf];
123 }
124 
128 NDN_CXX_NODISCARD constexpr int
129 fromHexChar(char c) noexcept
130 {
131  return (c >= '0' && c <= '9') ? int(c - '0') :
132  (c >= 'A' && c <= 'F') ? int(c - 'A' + 10) :
133  (c >= 'a' && c <= 'f') ? int(c - 'a' + 10) :
134  -1;
135 }
136 
154 NDN_CXX_NODISCARD std::string
155 escape(const std::string& str);
156 
157 void
158 escape(std::ostream& os, const char* str, size_t len);
159 
174 NDN_CXX_NODISCARD std::string
175 unescape(const std::string& str);
176 
177 void
178 unescape(std::ostream& os, const char* str, size_t len);
179 
180 } // namespace ndn
181 
182 #endif // NDN_CXX_UTIL_STRING_HELPER_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
constexpr AsHex(uint64_t val) noexcept
friend std::ostream & operator<<(std::ostream &os, const AsHex &hex)
Definition: data.cpp:25
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert a hex string to a raw byte buffer.
std::string toHex(span< const uint8_t > buffer, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
std::string unescape(const std::string &str)
Decode a percent-encoded string.
constexpr int fromHexChar(char c) noexcept
Convert the hex character c to an integer in [0, 15], or -1 if it's not a hex character.
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.