diff options
author | m-holger <m-holger@kubitscheck.org> | 2022-12-12 14:29:52 +0100 |
---|---|---|
committer | m-holger <m-holger@kubitscheck.org> | 2023-04-01 14:56:16 +0200 |
commit | 4d37389befc705b671d8fa7a1da2b7117b50f454 (patch) | |
tree | 0fd25b3c7d60a75d5873b430fe801ac26e0f4a58 /libqpdf | |
parent | 1bb23d0545dfe2d651cb22b6135d99c1c9ef85d5 (diff) | |
download | qpdf-4d37389befc705b671d8fa7a1da2b7117b50f454.tar.zst |
Refactor QPDF_Array::eraseItem and rename to erase
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QPDFObjectHandle.cc | 13 | ||||
-rw-r--r-- | libqpdf/QPDF_Array.cc | 15 | ||||
-rw-r--r-- | libqpdf/SparseOHArray.cc | 25 | ||||
-rw-r--r-- | libqpdf/qpdf/QPDF_Array.hh | 2 |
4 files changed, 26 insertions, 29 deletions
diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index fa8b9136..e113089a 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -972,17 +972,14 @@ QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item) void QPDFObjectHandle::eraseItem(int at) { - auto array = asArray(); - if (array && at < array->size() && at >= 0) { - array->eraseItem(at); - } else { - if (array) { + if (auto array = asArray()) { + if (!array->erase(at)) { objectWarning("ignoring attempt to erase out of bounds array item"); QTC::TC("qpdf", "QPDFObjectHandle erase array bounds"); - } else { - typeWarning("array", "ignoring attempt to erase item"); - QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item"); } + } else { + typeWarning("array", "ignoring attempt to erase item"); + QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item"); } } diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 7633266e..e4e1668e 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -267,17 +267,16 @@ QPDF_Array::push_back(QPDFObjectHandle const& item) } } -void -QPDF_Array::eraseItem(int at) +bool +QPDF_Array::erase(int at) { + if (at < 0 || at >= size()) { + return false; + } if (sparse) { sp_elements.erase(at); } else { - size_t idx = QIntC::to_size(at); - if (idx >= elements.size()) { - throw std::logic_error("bounds error erasing item from OHArray"); - } - int n = int(idx); - elements.erase(elements.cbegin() + n); + elements.erase(elements.cbegin() + at); } + return true; } diff --git a/libqpdf/SparseOHArray.cc b/libqpdf/SparseOHArray.cc index 8f6c02d7..773d7309 100644 --- a/libqpdf/SparseOHArray.cc +++ b/libqpdf/SparseOHArray.cc @@ -44,21 +44,22 @@ SparseOHArray::setAt(int idx, QPDFObjectHandle oh) } void -SparseOHArray::erase(int idx) +SparseOHArray::erase(int at) { - if (idx >= this->n_elements) { - throw std::logic_error("bounds error erasing item from SparseOHArray"); - } - decltype(this->elements) dest; - for (auto const& iter: this->elements) { - if (iter.first < idx) { - dest.insert(iter); - } else if (iter.first > idx) { - dest[iter.first - 1] = iter.second; + auto end = elements.end(); + if (auto iter = elements.lower_bound(at); iter != end) { + if (iter->first == at) { + iter++; + elements.erase(at); + } + + while (iter != end) { + auto nh = elements.extract(iter++); + --nh.key(); + elements.insert(std::move(nh)); } } - this->elements = dest; - --this->n_elements; + --n_elements; } void diff --git a/libqpdf/qpdf/QPDF_Array.hh b/libqpdf/qpdf/QPDF_Array.hh index 8768c9ad..edda54ec 100644 --- a/libqpdf/qpdf/QPDF_Array.hh +++ b/libqpdf/qpdf/QPDF_Array.hh @@ -35,7 +35,7 @@ class QPDF_Array: public QPDFValue void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items); bool insert(int at, QPDFObjectHandle const& item); void push_back(QPDFObjectHandle const& item); - void eraseItem(int at); + bool erase(int at); private: QPDF_Array(std::vector<QPDFObjectHandle> const& items); |