Commit 5f4f553c3588f3ef3cb704c9d3c6db6bb78ccfa9

Authored by m-holger
1 parent 0795b695

Refactor QUtil::hex_decode

Showing 1 changed file with 14 additions and 17 deletions
libqpdf/QUtil.cc
@@ -783,28 +783,25 @@ std::string @@ -783,28 +783,25 @@ std::string
783 QUtil::hex_decode(std::string const& input) 783 QUtil::hex_decode(std::string const& input)
784 { 784 {
785 std::string result; 785 std::string result;
786 - size_t pos = 0; 786 + // We know result.size() <= 0.5 * input.size() + 1. However, reserving
  787 + // string space for this upper bound has a negative impact.
  788 + bool first = true;
  789 + char decoded;
787 for (auto ch: input) { 790 for (auto ch: input) {
788 - bool skip = false;  
789 - if ((ch >= 'A') && (ch <= 'F')) {  
790 - ch = QIntC::to_char(ch - 'A' + 10);  
791 - } else if ((ch >= 'a') && (ch <= 'f')) {  
792 - ch = QIntC::to_char(ch - 'a' + 10);  
793 - } else if ((ch >= '0') && (ch <= '9')) {  
794 - ch = QIntC::to_char(ch - '0');  
795 - } else {  
796 - skip = true;  
797 - }  
798 - if (!skip) {  
799 - if (pos == 0) {  
800 - result.push_back(static_cast<char>(ch << 4));  
801 - pos = 1; 791 + ch = hex_decode_char(ch);
  792 + if (ch < '\20') {
  793 + if (first) {
  794 + decoded = static_cast<char>(ch << 4);
  795 + first = false;
802 } else { 796 } else {
803 - result[result.length() - 1] |= ch;  
804 - pos = 0; 797 + result.push_back(decoded | ch);
  798 + first = true;
805 } 799 }
806 } 800 }
807 } 801 }
  802 + if (!first) {
  803 + result.push_back(decoded);
  804 + }
808 return result; 805 return result;
809 } 806 }
810 807