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 ==
'~') {
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);
An output stream that writes to a Buffer.
shared_ptr< Buffer > buf()
Return a shared pointer to the underlying buffer.
#define NDN_THROW_NESTED(e)
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.