Commit 4e58d115b0654e65d7c73edec025fc1d70ba7d16
1 parent
9ca80c23
Refactor `NNTreeImpl::repair`: change to two-pass repair storing valid nodes in …
…a map during the first pass, add custom comparator for sorting keys.
Showing
1 changed file
with
22 additions
and
1 deletions
libqpdf/NNTree.cc
| @@ -642,16 +642,37 @@ NNTreeImpl::compareKeyKid(QPDFObjectHandle const& key, Array const& kids, int id | @@ -642,16 +642,37 @@ NNTreeImpl::compareKeyKid(QPDFObjectHandle const& key, Array const& kids, int id | ||
| 642 | return 0; | 642 | return 0; |
| 643 | } | 643 | } |
| 644 | 644 | ||
| 645 | +namespace | ||
| 646 | +{ | ||
| 647 | + struct Cmp | ||
| 648 | + { | ||
| 649 | + bool | ||
| 650 | + operator()(const QPDFObjectHandle& lhs, const QPDFObjectHandle& rhs) const | ||
| 651 | + { | ||
| 652 | + Integer l = lhs; | ||
| 653 | + Integer r = rhs; | ||
| 654 | + if (l && r) { | ||
| 655 | + return l.value() < r.value(); | ||
| 656 | + } | ||
| 657 | + return lhs.getUTF8Value() < rhs.getUTF8Value(); | ||
| 658 | + } | ||
| 659 | + }; | ||
| 660 | +} // namespace | ||
| 661 | + | ||
| 645 | void | 662 | void |
| 646 | NNTreeImpl::repair() | 663 | NNTreeImpl::repair() |
| 647 | { | 664 | { |
| 648 | auto new_node = Dictionary({{itemsKey(), Array::empty()}}); | 665 | auto new_node = Dictionary({{itemsKey(), Array::empty()}}); |
| 649 | NNTreeImpl repl(qpdf, new_node, key_type, value_valid, false); | 666 | NNTreeImpl repl(qpdf, new_node, key_type, value_valid, false); |
| 667 | + std::map<QPDFObjectHandle, QPDFObjectHandle, Cmp> items; | ||
| 650 | for (auto const& [key, value]: *this) { | 668 | for (auto const& [key, value]: *this) { |
| 651 | if (key && value) { | 669 | if (key && value) { |
| 652 | - repl.insert(key, value); | 670 | + items.insert_or_assign(key, value); |
| 653 | } | 671 | } |
| 654 | } | 672 | } |
| 673 | + for (auto const& [key, value]: items) { | ||
| 674 | + repl.insert(key, value); | ||
| 675 | + } | ||
| 655 | tree_root.replaceKey("/Kids", new_node["/Kids"]); | 676 | tree_root.replaceKey("/Kids", new_node["/Kids"]); |
| 656 | tree_root.replaceKey(itemsKey(), new_node[itemsKey()]); | 677 | tree_root.replaceKey(itemsKey(), new_node[itemsKey()]); |
| 657 | } | 678 | } |