22 #ifndef NDN_CXX_UTIL_STRING_HELPER_HPP
23 #define NDN_CXX_UTIL_STRING_HELPER_HPP
33 using std::invalid_argument::invalid_argument;
46 printHex(std::ostream& os, uint64_t num,
bool wantUpperCase =
false);
59 printHex(std::ostream& os, span<const uint8_t> buffer,
bool wantUpperCase =
true);
85 printHex(os, hex.m_value, os.flags() & std::ostream::uppercase);
103 toHex(span<const uint8_t> buffer,
bool wantUpperCase =
true);
112 fromHex(
const std::string& hexString);
118 toHexChar(
unsigned int n,
bool wantUpperCase =
true) noexcept
120 return wantUpperCase ?
121 "0123456789ABCDEF"[n & 0xf] :
122 "0123456789abcdef"[n & 0xf];
131 return (c >=
'0' && c <=
'9') ? int(c -
'0') :
132 (c >=
'A' && c <=
'F') ?
int(c -
'A' + 10) :
133 (c >=
'a' && c <=
'f') ?
int(c -
'a' + 10) :
155 escape(
const std::string& str);
158 escape(std::ostream& os,
const char* str,
size_t len);
178 unescape(std::ostream& os,
const char* str,
size_t len);
#define NDN_CXX_NODISCARD
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)
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.