Commit c0137dac89116342878d33afccacd46ca73e1cf0
1 parent
b8bc3ca3
Refactor `NNTree` methods to improve const-correctness and simplify parameter types
- Added `const` qualifiers to member functions and parameters where applicable. - Replaced `QPDFObjectHandle` arrays with `qpdf::Array` for improved type clarity. - Simplified logic in `compareKeyItem` and `compareKeyKid` by removing redundant checks.
Showing
2 changed files
with
15 additions
and
14 deletions
libqpdf/NNTree.cc
| ... | ... | @@ -33,7 +33,7 @@ NNTreeImpl::warn(QPDFObjectHandle const& node, std::string const& msg) |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | void |
| 36 | -NNTreeImpl::error(QPDFObjectHandle const& node, std::string const& msg) | |
| 36 | +NNTreeImpl::error(QPDFObjectHandle const& node, std::string const& msg) const | |
| 37 | 37 | { |
| 38 | 38 | throw QPDFExc(qpdf_e_damaged_pdf, qpdf.getFilename(), get_description(node), 0, msg); |
| 39 | 39 | } |
| ... | ... | @@ -682,11 +682,11 @@ NNTreeImpl::compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const |
| 682 | 682 | |
| 683 | 683 | int |
| 684 | 684 | NNTreeImpl::binarySearch( |
| 685 | - QPDFObjectHandle key, | |
| 686 | - QPDFObjectHandle items, | |
| 685 | + QPDFObjectHandle const& key, | |
| 686 | + Array const& items, | |
| 687 | 687 | size_t num_items, |
| 688 | 688 | bool return_prev_if_not_found, |
| 689 | - int (NNTreeImpl::*compare)(QPDFObjectHandle& key, QPDFObjectHandle& arr, int item)) | |
| 689 | + int (NNTreeImpl::*compare)(QPDFObjectHandle const& key, Array const& arr, int item) const) const | |
| 690 | 690 | { |
| 691 | 691 | size_t max_idx = std::bit_ceil(num_items); |
| 692 | 692 | |
| ... | ... | @@ -725,18 +725,18 @@ NNTreeImpl::binarySearch( |
| 725 | 725 | } |
| 726 | 726 | |
| 727 | 727 | int |
| 728 | -NNTreeImpl::compareKeyItem(QPDFObjectHandle& key, QPDFObjectHandle& items, int idx) | |
| 728 | +NNTreeImpl::compareKeyItem(QPDFObjectHandle const& key, Array const& items, int idx) const | |
| 729 | 729 | { |
| 730 | - if (!(std::cmp_greater(items.size(), 2 * idx) && keyValid(items[2 * idx]))) { | |
| 730 | + if (!keyValid(items[2 * idx])) { | |
| 731 | 731 | error(oh, ("item at index " + std::to_string(2 * idx) + " is not the right type")); |
| 732 | 732 | } |
| 733 | 733 | return compareKeys(key, items[2 * idx]); |
| 734 | 734 | } |
| 735 | 735 | |
| 736 | 736 | int |
| 737 | -NNTreeImpl::compareKeyKid(QPDFObjectHandle& key, QPDFObjectHandle& kids, int idx) | |
| 737 | +NNTreeImpl::compareKeyKid(QPDFObjectHandle const& key, Array const& kids, int idx) const | |
| 738 | 738 | { |
| 739 | - if (!(std::cmp_less(idx, kids.size()) && kids[idx].isDictionary())) { | |
| 739 | + if (!kids[idx].isDictionary()) { | |
| 740 | 740 | error(oh, "invalid kid at index " + std::to_string(idx)); |
| 741 | 741 | } |
| 742 | 742 | Array limits = kids[idx].getKey("/Limits"); | ... | ... |
libqpdf/qpdf/NNTree.hh
| ... | ... | @@ -111,15 +111,16 @@ class NNTreeImpl |
| 111 | 111 | void repair(); |
| 112 | 112 | iterator findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_found = false); |
| 113 | 113 | int binarySearch( |
| 114 | - QPDFObjectHandle key, | |
| 115 | - QPDFObjectHandle items, | |
| 114 | + QPDFObjectHandle const& key, | |
| 115 | + qpdf::Array const& items, | |
| 116 | 116 | size_t num_items, |
| 117 | 117 | bool return_prev_if_not_found, |
| 118 | - int (NNTreeImpl::*compare)(QPDFObjectHandle& key, QPDFObjectHandle& arr, int item)); | |
| 119 | - int compareKeyItem(QPDFObjectHandle& key, QPDFObjectHandle& items, int idx); | |
| 120 | - int compareKeyKid(QPDFObjectHandle& key, QPDFObjectHandle& items, int idx); | |
| 118 | + int (NNTreeImpl::*compare)(QPDFObjectHandle const& key, qpdf::Array const& arr, int item) | |
| 119 | + const) const; | |
| 120 | + int compareKeyItem(QPDFObjectHandle const& key, qpdf::Array const& items, int idx) const; | |
| 121 | + int compareKeyKid(QPDFObjectHandle const& key, qpdf::Array const& items, int idx) const; | |
| 121 | 122 | void warn(QPDFObjectHandle const& node, std::string const& msg); |
| 122 | - void error(QPDFObjectHandle const& node, std::string const& msg); | |
| 123 | + void error(QPDFObjectHandle const& node, std::string const& msg) const; | |
| 123 | 124 | |
| 124 | 125 | std::string const& |
| 125 | 126 | itemsKey() const | ... | ... |