Commit b8bc3ca3aec57e3ebeabf0f617d6cef0e49f656a

Authored by m-holger
1 parent 67796b3f

Remove `withinLimits` method and inline its logic into `compareKeyKid` in `NNTreeImpl`

- Simplified the code by directly handling limits validation in `compareKeyKid`.
- Improved clarity by reducing unnecessary method indirection.
libqpdf/NNTree.cc
@@ -667,9 +667,9 @@ NNTreeImpl::last() @@ -667,9 +667,9 @@ NNTreeImpl::last()
667 int 667 int
668 NNTreeImpl::compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const 668 NNTreeImpl::compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const
669 { 669 {
670 - // We don't call this without calling keyValid first  
671 - qpdf_assert_debug(keyValid(a));  
672 - qpdf_assert_debug(keyValid(b)); 670 + // We don't call this without calling keyValid first
  671 + qpdf_assert_debug(keyValid(a));
  672 + qpdf_assert_debug(keyValid(b));
673 if (key_type == ::ot_string) { 673 if (key_type == ::ot_string) {
674 auto as = a.getUTF8Value(); 674 auto as = a.getUTF8Value();
675 auto bs = b.getUTF8Value(); 675 auto bs = b.getUTF8Value();
@@ -681,22 +681,6 @@ NNTreeImpl::compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const @@ -681,22 +681,6 @@ NNTreeImpl::compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const
681 } 681 }
682 682
683 int 683 int
684 -NNTreeImpl::withinLimits(QPDFObjectHandle const& key, QPDFObjectHandle const& node)  
685 -{  
686 - Array limits = node.getKey("/Limits");  
687 - if (!(keyValid(limits[0]) && keyValid(limits[1]))) {  
688 - error(node, "node is missing /Limits");  
689 - }  
690 - if (compareKeys(key, limits[0]) < 0) {  
691 - return -1;  
692 - }  
693 - if (compareKeys(key, limits[1]) > 0) {  
694 - return 1;  
695 - }  
696 - return 0;  
697 -}  
698 -  
699 -int  
700 NNTreeImpl::binarySearch( 684 NNTreeImpl::binarySearch(
701 QPDFObjectHandle key, 685 QPDFObjectHandle key,
702 QPDFObjectHandle items, 686 QPDFObjectHandle items,
@@ -755,7 +739,17 @@ NNTreeImpl::compareKeyKid(QPDFObjectHandle&amp; key, QPDFObjectHandle&amp; kids, int idx @@ -755,7 +739,17 @@ NNTreeImpl::compareKeyKid(QPDFObjectHandle&amp; key, QPDFObjectHandle&amp; kids, int idx
755 if (!(std::cmp_less(idx, kids.size()) && kids[idx].isDictionary())) { 739 if (!(std::cmp_less(idx, kids.size()) && kids[idx].isDictionary())) {
756 error(oh, "invalid kid at index " + std::to_string(idx)); 740 error(oh, "invalid kid at index " + std::to_string(idx));
757 } 741 }
758 - return withinLimits(key, kids[idx]); 742 + Array limits = kids[idx].getKey("/Limits");
  743 + if (!(keyValid(limits[0]) && keyValid(limits[1]))) {
  744 + error(kids[idx], "node is missing /Limits");
  745 + }
  746 + if (compareKeys(key, limits[0]) < 0) {
  747 + return -1;
  748 + }
  749 + if (compareKeys(key, limits[1]) > 0) {
  750 + return 1;
  751 + }
  752 + return 0;
759 } 753 }
760 754
761 void 755 void
libqpdf/qpdf/NNTree.hh
@@ -110,7 +110,6 @@ class NNTreeImpl @@ -110,7 +110,6 @@ class NNTreeImpl
110 private: 110 private:
111 void repair(); 111 void repair();
112 iterator findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_found = false); 112 iterator findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_found = false);
113 - int withinLimits(QPDFObjectHandle const& key, QPDFObjectHandle const& node);  
114 int binarySearch( 113 int binarySearch(
115 QPDFObjectHandle key, 114 QPDFObjectHandle key,
116 QPDFObjectHandle items, 115 QPDFObjectHandle items,
@@ -132,8 +131,7 @@ class NNTreeImpl @@ -132,8 +131,7 @@ class NNTreeImpl
132 { 131 {
133 return o.resolved_type_code() == key_type; 132 return o.resolved_type_code() == key_type;
134 } 133 }
135 - int  
136 - compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const; 134 + int compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const;
137 135
138 QPDF& qpdf; 136 QPDF& qpdf;
139 int split_threshold{32}; 137 int split_threshold{32};