34 printHex(std::ostream& os, uint64_t num,
bool wantUpperCase)
36 auto osFlags = os.flags();
38 os <<
"0x" << std::noshowbase << std::noshowpos
39 << (wantUpperCase ? std::uppercase : std::nouppercase)
45 printHex(std::ostream& os, span<const uint8_t> buffer,
bool wantUpperCase)
47 namespace tr = security::transform;
52 toHex(span<const uint8_t> buffer,
bool wantUpperCase)
54 std::ostringstream result;
55 printHex(result, buffer, wantUpperCase);
62 namespace tr = security::transform;
68 catch (
const tr::Error& e) {
78 std::ostringstream os;
79 escape(os, str.data(), str.size());
84 escape(std::ostream& os,
const char* str,
size_t len)
86 for (
size_t i = 0; i < len; ++i) {
89 if ((c >=
'a' && c <=
'z') ||
90 (c >=
'A' && c <=
'Z') ||
91 (c >=
'0' && c <=
'9') ||
92 c ==
'-' || c ==
'.' ||
93 c ==
'_' || c ==
'~') {
98 os << toHexChar((c & 0xf0) >> 4);
107 std::ostringstream os;
108 unescape(os, str.data(), str.size());
113 unescape(std::ostream& os,
const char* str,
size_t len)
115 for (
size_t i = 0; i < len; ++i) {
116 if (str[i] ==
'%' && i + 2 < len) {
120 if (hi < 0 || lo < 0)
122 os << str[i] << str[i + 1] << str[i + 2];
124 os << static_cast<char>((hi << 4) | lo);
#define NDN_THROW_NESTED(e)
Copyright (c) 2011-2015 Regents of the University of California.
std::string toHex(span< const uint8_t > buffer, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
NDN_CXX_NODISCARD constexpr char toHexChar(unsigned int n, bool wantUpperCase=true) noexcept
Convert (the least significant nibble of) n to the corresponding hex character.
NDN_CXX_NODISCARD 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.
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
std::string escape(const std::string &str)
Percent-encode a string.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
implements an output stream that constructs ndn::Buffer
std::string unescape(const std::string &str)
Decode a percent-encoded string.