Commit 942a2c3f689b46107538d49a67dbf74c76e672b8
Committed by
Jay Berkenbilt
1 parent
1326ff7f
Add new function QUtil::hex_encode_char
Showing
2 changed files
with
14 additions
and
1 deletions
include/qpdf/QUtil.hh
| @@ -210,6 +210,11 @@ namespace QUtil | @@ -210,6 +210,11 @@ namespace QUtil | ||
| 210 | QPDF_DLL | 210 | QPDF_DLL |
| 211 | std::string hex_encode(std::string const&); | 211 | std::string hex_encode(std::string const&); |
| 212 | 212 | ||
| 213 | + // Returns lower-case hex-encoded version of the char including a leading | ||
| 214 | + // "#". | ||
| 215 | + QPDF_DLL | ||
| 216 | + inline std::string hex_encode_char(char); | ||
| 217 | + | ||
| 213 | // Returns a string that is the result of decoding the input | 218 | // Returns a string that is the result of decoding the input |
| 214 | // string. The input string may consist of mixed case hexadecimal | 219 | // string. The input string may consist of mixed case hexadecimal |
| 215 | // digits. Any characters that are not hexadecimal digits will be | 220 | // digits. Any characters that are not hexadecimal digits will be |
| @@ -583,4 +588,12 @@ QUtil::is_number(char const* p) | @@ -583,4 +588,12 @@ QUtil::is_number(char const* p) | ||
| 583 | return found_digit; | 588 | return found_digit; |
| 584 | } | 589 | } |
| 585 | 590 | ||
| 591 | +inline std::string | ||
| 592 | +QUtil::hex_encode_char(char c) | ||
| 593 | +{ | ||
| 594 | + static auto constexpr hexchars = "0123456789abcdef"; | ||
| 595 | + return { | ||
| 596 | + '#', hexchars[static_cast<unsigned char>(c) >> 4], hexchars[c & 0x0f]}; | ||
| 597 | +} | ||
| 598 | + | ||
| 586 | #endif // QUTIL_HH | 599 | #endif // QUTIL_HH |
libqpdf/QPDF_Name.cc
| @@ -38,7 +38,7 @@ QPDF_Name::normalizeName(std::string const& name) | @@ -38,7 +38,7 @@ QPDF_Name::normalizeName(std::string const& name) | ||
| 38 | // invalid #. | 38 | // invalid #. |
| 39 | result += "#"; | 39 | result += "#"; |
| 40 | } else if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126)) { | 40 | } else if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126)) { |
| 41 | - result += "#" + QUtil::hex_encode(std::string(&ch, 1)); | 41 | + result += QUtil::hex_encode_char(ch); |
| 42 | } else { | 42 | } else { |
| 43 | result += ch; | 43 | result += ch; |
| 44 | } | 44 | } |