ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2024 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#include <ostream>
29#include <stdexcept>
30#include <string>
31#include <string_view>
32
33namespace ndn {
34
35class StringHelperError : public std::invalid_argument
36{
37public:
38 using std::invalid_argument::invalid_argument;
39};
40
50void
51printHex(std::ostream& os, uint64_t num, bool wantUpperCase = false);
52
63void
64printHex(std::ostream& os, span<const uint8_t> buffer, bool wantUpperCase = true);
65
77class AsHex
78{
79public:
80 constexpr explicit
81 AsHex(uint64_t val) noexcept
82 : m_value(val)
83 {
84 }
85
86private:
87 friend std::ostream&
88 operator<<(std::ostream& os, const AsHex& hex)
89 {
90 printHex(os, hex.m_value, os.flags() & std::ostream::uppercase);
91 return os;
92 }
93
94private:
95 uint64_t m_value;
96};
97
107[[nodiscard]] std::string
108toHex(span<const uint8_t> buffer, bool wantUpperCase = true);
109
116std::shared_ptr<Buffer>
117fromHex(std::string_view hexString);
118
122[[nodiscard]] constexpr char
123toHexChar(unsigned int n, bool wantUpperCase = true) noexcept
124{
125 return wantUpperCase ?
126 "0123456789ABCDEF"[n & 0xf] :
127 "0123456789abcdef"[n & 0xf];
128}
129
133[[nodiscard]] constexpr int
134fromHexChar(char c) noexcept
135{
136 return (c >= '0' && c <= '9') ? int(c - '0') :
137 (c >= 'A' && c <= 'F') ? int(c - 'A' + 10) :
138 (c >= 'a' && c <= 'f') ? int(c - 'a' + 10) :
139 -1;
140}
141
159[[nodiscard]] std::string
160escape(std::string_view str);
161
162void
163escape(std::ostream& os, std::string_view str);
164
179[[nodiscard]] std::string
180unescape(std::string_view str);
181
182void
183unescape(std::ostream& os, std::string_view str);
184
185} // namespace ndn
186
187#endif // NDN_CXX_UTIL_STRING_HELPER_HPP
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.
std::string escape(std::string_view str)
Percent-encode a string.
std::string toHex(span< const uint8_t > buffer, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
shared_ptr< Buffer > fromHex(std::string_view hexString)
Convert a hex string to a raw byte buffer.
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.
std::string unescape(std::string_view 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.