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,
const uint8_t* buffer,
size_t length,
bool wantUpperCase)
47 namespace tr = security::transform;
48 BOOST_ASSERT(buffer !=
nullptr || length == 0);
53 toHex(
const uint8_t* buffer,
size_t length,
bool wantUpperCase)
55 std::ostringstream result;
56 printHex(result, buffer, length, wantUpperCase);
63 namespace tr = security::transform;
69 catch (
const tr::Error& e) {
79 std::ostringstream os;
80 escape(os, str.data(), str.size());
85 escape(std::ostream& os,
const char* str,
size_t len)
87 for (
size_t i = 0; i < len; ++i) {
90 if ((c >=
'a' && c <=
'z') ||
91 (c >=
'A' && c <=
'Z') ||
92 (c >=
'0' && c <=
'9') ||
93 c ==
'-' || c ==
'.' ||
94 c ==
'_' || c ==
'~') {
99 os << toHexChar((c & 0xf0) >> 4);
108 std::ostringstream os;
109 unescape(os, str.data(), str.size());
114 unescape(std::ostream& os,
const char* str,
size_t len)
116 for (
size_t i = 0; i < len; ++i) {
117 if (str[i] ==
'%' && i + 2 < len) {
121 if (hi < 0 || lo < 0)
123 os << str[i] << str[i + 1] << str[i + 2];
125 os << static_cast<char>((hi << 4) | lo);
#define NDN_THROW_NESTED(e)
std::string toHex(const uint8_t *buffer, size_t length, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
std::string unescape(const std::string &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.
std::string escape(const std::string &str)
Percent-encode a 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.
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