NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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 Buffer;
30 
31 class StringHelperError : public std::invalid_argument
32 {
33 public:
34  using std::invalid_argument::invalid_argument;
35 };
36 
48 class AsHex
49 {
50 public:
51  constexpr explicit
52  AsHex(uint64_t val) noexcept
53  : m_value(val)
54  {
55  }
56 
57 private:
58  uint64_t m_value;
59 
60  friend std::ostream& operator<<(std::ostream&, const AsHex&);
61 };
62 
63 std::ostream&
64 operator<<(std::ostream& os, const AsHex& hex);
65 
75 void
76 printHex(std::ostream& os, uint64_t num, bool wantUpperCase = false);
77 
97 void
98 printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase = true);
99 
107 void
108 printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase = true);
109 
128 NDN_CXX_NODISCARD std::string
129 toHex(const uint8_t* buffer, size_t length, bool wantUpperCase = true);
130 
137 NDN_CXX_NODISCARD std::string
138 toHex(const Buffer& buffer, bool wantUpperCase = true);
139 
146 shared_ptr<Buffer>
147 fromHex(const std::string& hexString);
148 
152 NDN_CXX_NODISCARD constexpr char
153 toHexChar(unsigned int n, bool wantUpperCase = true) noexcept
154 {
155  return wantUpperCase ?
156  "0123456789ABCDEF"[n & 0xf] :
157  "0123456789abcdef"[n & 0xf];
158 }
159 
163 NDN_CXX_NODISCARD constexpr int
164 fromHexChar(char c) noexcept
165 {
166  return (c >= '0' && c <= '9') ? int(c - '0') :
167  (c >= 'A' && c <= 'F') ? int(c - 'A' + 10) :
168  (c >= 'a' && c <= 'f') ? int(c - 'a' + 10) :
169  -1;
170 }
171 
188 NDN_CXX_NODISCARD std::string
189 escape(const std::string& str);
190 
191 void
192 escape(std::ostream& os, const char* str, size_t len);
193 
207 NDN_CXX_NODISCARD std::string
208 unescape(const std::string& str);
209 
210 void
211 unescape(std::ostream& os, const char* str, size_t len);
212 
213 } // namespace ndn
214 
215 #endif // NDN_UTIL_STRING_HELPER_HPP
common.hpp
Common includes and macros used throughout the library.
ndn::fromHex
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
Definition: string-helper.cpp:81
ndn::AsHex
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
Definition: string-helper.hpp:49
ndn::toHexChar
constexpr NDN_CXX_NODISCARD char toHexChar(unsigned int n, bool wantUpperCase=true) noexcept
Convert (the least significant nibble of) n to the corresponding hex character.
Definition: string-helper.hpp:153
NDN_CXX_NODISCARD
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
ndn::printHex
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
Definition: string-helper.cpp:42
ndn::unescape
std::string unescape(const std::string &str)
Decode a percent-encoded string.
Definition: string-helper.cpp:126
ndn::toHex
std::string toHex(const uint8_t *buffer, size_t length, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
Definition: string-helper.cpp:67
ndn::StringHelperError
Definition: string-helper.hpp:32
ndn::operator<<
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
ndn::escape
std::string escape(const std::string &str)
Percent-encode a string.
Definition: string-helper.cpp:97
ndn::AsHex::operator<<
friend std::ostream & operator<<(std::ostream &, const AsHex &)
Definition: string-helper.cpp:35
ndn::AsHex::AsHex
constexpr AsHex(uint64_t val) noexcept
Definition: string-helper.hpp:52
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::fromHexChar
constexpr NDN_CXX_NODISCARD 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.
Definition: string-helper.hpp:164