Commit 32e0bbe4140c040a8d5a5569bb6939f0b84abe11

Authored by m-holger
1 parent 36e9b370

Refactor `NNTreeIterator::split`: update to use `Dictionary` API, replace `getKe…

…y` with subscript operator, and simplify logic.
libqpdf/NNTree.cc
@@ -187,7 +187,7 @@ NNTreeIterator::resetLimits(Dictionary a_node, std::list<PathElement>::iterator @@ -187,7 +187,7 @@ NNTreeIterator::resetLimits(Dictionary a_node, std::list<PathElement>::iterator
187 } 187 }
188 188
189 void 189 void
190 -NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterator parent) 190 +NNTreeIterator::split(Dictionary to_split, std::list<PathElement>::iterator parent)
191 { 191 {
192 // Split some node along the path to the item pointed to by this iterator, and adjust the 192 // Split some node along the path to the item pointed to by this iterator, and adjust the
193 // iterator so it points to the same item. 193 // iterator so it points to the same item.
@@ -221,9 +221,9 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato @@ -221,9 +221,9 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
221 } 221 }
222 222
223 // Find the array we actually need to split, which is either this node's kids or items. 223 // Find the array we actually need to split, which is either this node's kids or items.
224 - Array kids = to_split.getKey("/Kids"); 224 + Array kids = to_split["/Kids"];
225 size_t nkids = kids.size(); 225 size_t nkids = kids.size();
226 - Array items = to_split.getKey(impl.itemsKey()); 226 + Array items = to_split[impl.itemsKey()];
227 size_t nitems = items.size(); 227 size_t nitems = items.size();
228 228
229 Array first_half; 229 Array first_half;
@@ -267,12 +267,11 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato @@ -267,12 +267,11 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
267 // is the new first half. In this way, we make the root case identical to the non-root case 267 // is the new first half. In this way, we make the root case identical to the non-root case
268 // so remaining logic can handle them in the same way. 268 // so remaining logic can handle them in the same way.
269 269
270 - auto first_node = impl.qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary());  
271 - first_node.replaceKey(key, first_half); 270 + Dictionary first_node = impl.qpdf.makeIndirectObject(Dictionary({{key, first_half}}));
272 auto new_kids = Array::empty(); 271 auto new_kids = Array::empty();
273 new_kids.push_back(first_node); 272 new_kids.push_back(first_node);
274 - to_split.removeKey("/Limits"); // already shouldn't be there for root  
275 - to_split.removeKey(impl.itemsKey()); 273 + to_split.erase("/Limits"); // already shouldn't be there for root
  274 + to_split.erase(impl.itemsKey());
276 to_split.replaceKey("/Kids", new_kids); 275 to_split.replaceKey("/Kids", new_kids);
277 if (is_leaf) { 276 if (is_leaf) {
278 node = first_node; 277 node = first_node;
@@ -299,8 +298,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato @@ -299,8 +298,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
299 resetLimits(to_split, parent); 298 resetLimits(to_split, parent);
300 299
301 // Create a new node to contain the second half 300 // Create a new node to contain the second half
302 - QPDFObjectHandle second_node = impl.qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary());  
303 - second_node.replaceKey(key, second_half); 301 + Dictionary second_node = impl.qpdf.makeIndirectObject(Dictionary({{key, second_half}}));
304 resetLimits(second_node, parent); 302 resetLimits(second_node, parent);
305 303
306 // CURRENT STATE: half the items from the kids or items array in the node being split have been 304 // CURRENT STATE: half the items from the kids or items array in the node being split have been
@@ -311,7 +309,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato @@ -311,7 +309,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
311 // kid_number to traverse through it. We need to update to_split's path element, or the node if 309 // kid_number to traverse through it. We need to update to_split's path element, or the node if
312 // this is a leaf, so that the kid/item number points to the right place. 310 // this is a leaf, so that the kid/item number points to the right place.
313 311
314 - Array parent_kids = parent->node.getKey("/Kids"); 312 + Array parent_kids = parent->node["/Kids"];
315 if (!parent_kids) { 313 if (!parent_kids) {
316 impl.error(parent->node, "parent node has no /Kids array"); 314 impl.error(parent->node, "parent node has no /Kids array");
317 } 315 }
libqpdf/qpdf/NNTree.hh
@@ -109,7 +109,7 @@ class NNTreeIterator final @@ -109,7 +109,7 @@ class NNTreeIterator final
109 void increment(bool backward); 109 void increment(bool backward);
110 void resetLimits(qpdf::Dictionary node, std::list<PathElement>::iterator parent); 110 void resetLimits(qpdf::Dictionary node, std::list<PathElement>::iterator parent);
111 111
112 - void split(QPDFObjectHandle to_split, std::list<PathElement>::iterator parent); 112 + void split(qpdf::Dictionary to_split, std::list<PathElement>::iterator parent);
113 std::list<PathElement>::iterator lastPathElement(); 113 std::list<PathElement>::iterator lastPathElement();
114 114
115 NNTreeImpl& impl; 115 NNTreeImpl& impl;