22 #ifndef NDN_CXX_UTIL_STRING_HELPER_HPP
23 #define NDN_CXX_UTIL_STRING_HELPER_HPP
31 #include <string_view>
38 using std::invalid_argument::invalid_argument;
51 printHex(std::ostream& os, uint64_t num,
bool wantUpperCase =
false);
64 printHex(std::ostream& os, span<const uint8_t> buffer,
bool wantUpperCase =
true);
90 printHex(os, hex.m_value, os.flags() & std::ostream::uppercase);
107 [[nodiscard]] std::string
108 toHex(span<const uint8_t> buffer,
bool wantUpperCase =
true);
116 std::shared_ptr<Buffer>
117 fromHex(std::string_view hexString);
122 [[nodiscard]] constexpr
char
123 toHexChar(
unsigned int n,
bool wantUpperCase =
true) noexcept
125 return wantUpperCase ?
126 "0123456789ABCDEF"[n & 0xf] :
127 "0123456789abcdef"[n & 0xf];
133 [[nodiscard]] constexpr
int
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) :
159 [[nodiscard]] std::string
160 escape(std::string_view str);
163 escape(std::ostream& os, std::string_view str);
179 [[nodiscard]] std::string
183 unescape(std::ostream& os, std::string_view str);
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.
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.