Commit 942a2c3f689b46107538d49a67dbf74c76e672b8

Authored by m-holger
Committed by Jay Berkenbilt
1 parent 1326ff7f

Add new function QUtil::hex_encode_char

include/qpdf/QUtil.hh
... ... @@ -210,6 +210,11 @@ namespace QUtil
210 210 QPDF_DLL
211 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 218 // Returns a string that is the result of decoding the input
214 219 // string. The input string may consist of mixed case hexadecimal
215 220 // digits. Any characters that are not hexadecimal digits will be
... ... @@ -583,4 +588,12 @@ QUtil::is_number(char const* p)
583 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 599 #endif // QUTIL_HH
... ...
libqpdf/QPDF_Name.cc
... ... @@ -38,7 +38,7 @@ QPDF_Name::normalizeName(std::string const&amp; name)
38 38 // invalid #.
39 39 result += "#";
40 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 42 } else {
43 43 result += ch;
44 44 }
... ...