Commit 3a2e67640b6650b9751512a1f18f99c7e29fa775
1 parent
979dc51b
Refactor `Array` class to introduce `empty()` factory method and remove unused constructors
- Added `Array::empty()` as a static method for creating empty arrays. - Removed unused `Array(bool empty)` constructor and defaulted `Array` constructor. - Updated `NNTree` methods to use `Array::empty()` for clarity and consistency.
Showing
3 changed files
with
19 additions
and
14 deletions
libqpdf/NNTree.cc
| @@ -280,7 +280,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato | @@ -280,7 +280,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato | ||
| 280 | 280 | ||
| 281 | auto first_node = impl.qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary()); | 281 | auto first_node = impl.qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary()); |
| 282 | first_node.replaceKey(key, first_half); | 282 | first_node.replaceKey(key, first_half); |
| 283 | - Array new_kids; | 283 | + auto new_kids = Array::empty(); |
| 284 | new_kids.push_back(first_node); | 284 | new_kids.push_back(first_node); |
| 285 | to_split.removeKey("/Limits"); // already shouldn't be there for root | 285 | to_split.removeKey("/Limits"); // already shouldn't be there for root |
| 286 | to_split.removeKey(impl.itemsKey()); | 286 | to_split.removeKey(impl.itemsKey()); |
| @@ -301,7 +301,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato | @@ -301,7 +301,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato | ||
| 301 | 301 | ||
| 302 | // Create a second half array, and transfer the second half of the items into the second half | 302 | // Create a second half array, and transfer the second half of the items into the second half |
| 303 | // array. | 303 | // array. |
| 304 | - Array second_half; | 304 | + auto second_half = Array::empty(); |
| 305 | auto start_idx = static_cast<int>((n / 2) & ~1u); | 305 | auto start_idx = static_cast<int>((n / 2) & ~1u); |
| 306 | while (std::cmp_greater(first_half.size(), start_idx)) { | 306 | while (std::cmp_greater(first_half.size(), start_idx)) { |
| 307 | second_half.push_back(first_half[start_idx]); | 307 | second_half.push_back(first_half[start_idx]); |
| @@ -323,6 +323,9 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato | @@ -323,6 +323,9 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato | ||
| 323 | // this is a leaf, so that the kid/item number points to the right place. | 323 | // this is a leaf, so that the kid/item number points to the right place. |
| 324 | 324 | ||
| 325 | Array parent_kids = parent->node.getKey("/Kids"); | 325 | Array parent_kids = parent->node.getKey("/Kids"); |
| 326 | + if (!parent_kids) { | ||
| 327 | + impl.error(parent->node, "parent node has no /Kids array"); | ||
| 328 | + } | ||
| 326 | parent_kids.insert(parent->kid_number + 1, second_node); | 329 | parent_kids.insert(parent->kid_number + 1, second_node); |
| 327 | auto cur_elem = parent; | 330 | auto cur_elem = parent; |
| 328 | ++cur_elem; // points to end() for leaf nodes | 331 | ++cur_elem; // points to end() for leaf nodes |
| @@ -466,7 +469,7 @@ NNTreeIterator::remove() | @@ -466,7 +469,7 @@ NNTreeIterator::remove() | ||
| 466 | if (parent == path.end()) { | 469 | if (parent == path.end()) { |
| 467 | // We erased the very last item. Convert the root to an empty items array. | 470 | // We erased the very last item. Convert the root to an empty items array. |
| 468 | element->node.removeKey("/Kids"); | 471 | element->node.removeKey("/Kids"); |
| 469 | - element->node.replaceKey(impl.itemsKey(), Array()); | 472 | + element->node.replaceKey(impl.itemsKey(), Array::empty()); |
| 470 | path.clear(); | 473 | path.clear(); |
| 471 | setItemNumber(impl.oh, -1); | 474 | setItemNumber(impl.oh, -1); |
| 472 | return; | 475 | return; |
| @@ -676,7 +679,7 @@ void | @@ -676,7 +679,7 @@ void | ||
| 676 | NNTreeImpl::repair() | 679 | NNTreeImpl::repair() |
| 677 | { | 680 | { |
| 678 | auto new_node = QPDFObjectHandle::newDictionary(); | 681 | auto new_node = QPDFObjectHandle::newDictionary(); |
| 679 | - new_node.replaceKey(itemsKey(), Array()); | 682 | + new_node.replaceKey(itemsKey(), Array::empty()); |
| 680 | NNTreeImpl repl(qpdf, new_node, key_type, value_valid, false); | 683 | NNTreeImpl repl(qpdf, new_node, key_type, value_valid, false); |
| 681 | for (auto const& [key, value]: *this) { | 684 | for (auto const& [key, value]: *this) { |
| 682 | if (key && value) { | 685 | if (key && value) { |
| @@ -770,7 +773,7 @@ NNTreeImpl::iterator | @@ -770,7 +773,7 @@ NNTreeImpl::iterator | ||
| 770 | NNTreeImpl::insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& value) | 773 | NNTreeImpl::insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& value) |
| 771 | { | 774 | { |
| 772 | auto iter = begin(); | 775 | auto iter = begin(); |
| 773 | - Array items(nullptr); | 776 | + Array items; |
| 774 | if (iter.node.isDictionary()) { | 777 | if (iter.node.isDictionary()) { |
| 775 | items = iter.node.getKey(itemsKey()); | 778 | items = iter.node.getKey(itemsKey()); |
| 776 | } | 779 | } |
libqpdf/QPDF_Array.cc
| @@ -61,11 +61,6 @@ Array::array() const | @@ -61,11 +61,6 @@ Array::array() const | ||
| 61 | return nullptr; // unreachable | 61 | return nullptr; // unreachable |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | -Array::Array(bool empty) : | ||
| 65 | - BaseHandle(empty ? QPDFObject::create<QPDF_Array>() : nullptr) | ||
| 66 | -{ | ||
| 67 | -} | ||
| 68 | - | ||
| 69 | Array::Array(std::vector<QPDFObjectHandle> const& items) : | 64 | Array::Array(std::vector<QPDFObjectHandle> const& items) : |
| 70 | BaseHandle(QPDFObject::create<QPDF_Array>(items)) | 65 | BaseHandle(QPDFObject::create<QPDF_Array>(items)) |
| 71 | { | 66 | { |
| @@ -76,6 +71,12 @@ Array::Array(std::vector<QPDFObjectHandle>&& items) : | @@ -76,6 +71,12 @@ Array::Array(std::vector<QPDFObjectHandle>&& items) : | ||
| 76 | { | 71 | { |
| 77 | } | 72 | } |
| 78 | 73 | ||
| 74 | +Array | ||
| 75 | +Array::empty() | ||
| 76 | +{ | ||
| 77 | + return Array(std::vector<QPDFObjectHandle>()); | ||
| 78 | +} | ||
| 79 | + | ||
| 79 | Array::iterator | 80 | Array::iterator |
| 80 | Array::begin() | 81 | Array::begin() |
| 81 | { | 82 | { |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| @@ -12,18 +12,19 @@ namespace qpdf | @@ -12,18 +12,19 @@ namespace qpdf | ||
| 12 | class Array final: public BaseHandle | 12 | class Array final: public BaseHandle |
| 13 | { | 13 | { |
| 14 | public: | 14 | public: |
| 15 | - // Create an empty Array. If 'empty' is false, create a null Array. | ||
| 16 | - Array(bool empty = true); | 15 | + Array() = default; |
| 17 | 16 | ||
| 18 | - Array(std::vector<QPDFObjectHandle> const& items); | 17 | + explicit Array(std::vector<QPDFObjectHandle> const& items); |
| 19 | 18 | ||
| 20 | - Array(std::vector<QPDFObjectHandle>&& items); | 19 | + explicit Array(std::vector<QPDFObjectHandle>&& items); |
| 21 | 20 | ||
| 22 | Array(Array const& other) : | 21 | Array(Array const& other) : |
| 23 | BaseHandle(other.obj) | 22 | BaseHandle(other.obj) |
| 24 | { | 23 | { |
| 25 | } | 24 | } |
| 26 | 25 | ||
| 26 | + static Array empty(); | ||
| 27 | + | ||
| 27 | Array& | 28 | Array& |
| 28 | operator=(Array const& other) | 29 | operator=(Array const& other) |
| 29 | { | 30 | { |