Commit 3a2e67640b6650b9751512a1f18f99c7e29fa775

Authored by m-holger
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.
libqpdf/NNTree.cc
... ... @@ -280,7 +280,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato
280 280  
281 281 auto first_node = impl.qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
282 282 first_node.replaceKey(key, first_half);
283   - Array new_kids;
  283 + auto new_kids = Array::empty();
284 284 new_kids.push_back(first_node);
285 285 to_split.removeKey("/Limits"); // already shouldn't be there for root
286 286 to_split.removeKey(impl.itemsKey());
... ... @@ -301,7 +301,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato
301 301  
302 302 // Create a second half array, and transfer the second half of the items into the second half
303 303 // array.
304   - Array second_half;
  304 + auto second_half = Array::empty();
305 305 auto start_idx = static_cast<int>((n / 2) & ~1u);
306 306 while (std::cmp_greater(first_half.size(), start_idx)) {
307 307 second_half.push_back(first_half[start_idx]);
... ... @@ -323,6 +323,9 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
323 323 // this is a leaf, so that the kid/item number points to the right place.
324 324  
325 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 329 parent_kids.insert(parent->kid_number + 1, second_node);
327 330 auto cur_elem = parent;
328 331 ++cur_elem; // points to end() for leaf nodes
... ... @@ -466,7 +469,7 @@ NNTreeIterator::remove()
466 469 if (parent == path.end()) {
467 470 // We erased the very last item. Convert the root to an empty items array.
468 471 element->node.removeKey("/Kids");
469   - element->node.replaceKey(impl.itemsKey(), Array());
  472 + element->node.replaceKey(impl.itemsKey(), Array::empty());
470 473 path.clear();
471 474 setItemNumber(impl.oh, -1);
472 475 return;
... ... @@ -676,7 +679,7 @@ void
676 679 NNTreeImpl::repair()
677 680 {
678 681 auto new_node = QPDFObjectHandle::newDictionary();
679   - new_node.replaceKey(itemsKey(), Array());
  682 + new_node.replaceKey(itemsKey(), Array::empty());
680 683 NNTreeImpl repl(qpdf, new_node, key_type, value_valid, false);
681 684 for (auto const& [key, value]: *this) {
682 685 if (key && value) {
... ... @@ -770,7 +773,7 @@ NNTreeImpl::iterator
770 773 NNTreeImpl::insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& value)
771 774 {
772 775 auto iter = begin();
773   - Array items(nullptr);
  776 + Array items;
774 777 if (iter.node.isDictionary()) {
775 778 items = iter.node.getKey(itemsKey());
776 779 }
... ...
libqpdf/QPDF_Array.cc
... ... @@ -61,11 +61,6 @@ Array::array() const
61 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 64 Array::Array(std::vector<QPDFObjectHandle> const& items) :
70 65 BaseHandle(QPDFObject::create<QPDF_Array>(items))
71 66 {
... ... @@ -76,6 +71,12 @@ Array::Array(std::vector&lt;QPDFObjectHandle&gt;&amp;&amp; items) :
76 71 {
77 72 }
78 73  
  74 +Array
  75 +Array::empty()
  76 +{
  77 + return Array(std::vector<QPDFObjectHandle>());
  78 +}
  79 +
79 80 Array::iterator
80 81 Array::begin()
81 82 {
... ...
libqpdf/qpdf/QPDFObjectHandle_private.hh
... ... @@ -12,18 +12,19 @@ namespace qpdf
12 12 class Array final: public BaseHandle
13 13 {
14 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 21 Array(Array const& other) :
23 22 BaseHandle(other.obj)
24 23 {
25 24 }
26 25  
  26 + static Array empty();
  27 +
27 28 Array&
28 29 operator=(Array const& other)
29 30 {
... ...