From 9e30de80326ad88c155725c66e3d444232119deb Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 25 Mar 2023 18:05:54 +0000 Subject: [PATCH] Remove temporary OHArray::erase, insert and copy --- libqpdf/OHArray.cc | 45 ----------------------------------------- libqpdf/QPDF_Array.cc | 32 ++++++++++++++++++++++++++--- libqpdf/qpdf/OHArray.hh | 3 --- 3 files changed, 29 insertions(+), 51 deletions(-) diff --git a/libqpdf/OHArray.cc b/libqpdf/OHArray.cc index ce6a595e..377b1a36 100644 --- a/libqpdf/OHArray.cc +++ b/libqpdf/OHArray.cc @@ -1,50 +1,5 @@ #include -#include -#include - -#include - -static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); - OHArray::OHArray() { } - -void -OHArray::erase(size_t idx) -{ - if (idx >= elements.size()) { - throw std::logic_error("bounds error erasing item from OHArray"); - } - int n = int(idx); - elements.erase(elements.cbegin() + n); -} - -void -OHArray::insert(size_t idx, QPDFObjectHandle oh) -{ - if (idx > elements.size()) { - throw std::logic_error("bounds error inserting item to OHArray"); - } else if (idx == elements.size()) { - // Allow inserting to the last position - elements.push_back(oh.getObj()); - } else { - int n = int(idx); - elements.insert(elements.cbegin() + n, oh.getObj()); - } -} - -OHArray -OHArray::copy() -{ - OHArray result; - result.elements.reserve(elements.size()); - for (auto const& element: elements) { - result.elements.push_back( - element ? (element->getObjGen().isIndirect() ? element - : element->copy()) - : element); - } - return result; -} diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index ce08332a..c7a9f315 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -67,7 +67,20 @@ QPDF_Array::copy(bool shallow) if (sparse) { return create(shallow ? sp_elements : sp_elements.copy()); } else { - return create(shallow ? elements : elements.copy()); + if (shallow) { + return create(elements); + } else { + OHArray result; + result.elements.reserve(elements.elements.size()); + for (auto const& element: elements.elements) { + result.elements.push_back( + element + ? (element->getObjGen().isIndirect() ? element + : element->copy()) + : element); + } + return create(result); + } } } @@ -237,11 +250,19 @@ QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) sp_elements.insert(QIntC::to_size(at), item); } else { // As special case, also allow insert beyond the end + size_t idx = QIntC::to_size(at); if ((at < 0) || (at > QIntC::to_int(elements.elements.size()))) { throw std::logic_error( "INTERNAL ERROR: bounds error accessing QPDF_Array element"); } - elements.insert(QIntC::to_size(at), item); + if (idx == elements.elements.size()) { + // Allow inserting to the last position + elements.elements.push_back(item.getObj()); + } else { + int n = int(idx); + elements.elements.insert( + elements.elements.cbegin() + n, item.getObj()); + } } } @@ -261,6 +282,11 @@ QPDF_Array::eraseItem(int at) if (sparse) { sp_elements.erase(QIntC::to_size(at)); } else { - elements.erase(QIntC::to_size(at)); + size_t idx = QIntC::to_size(at); + if (idx >= elements.elements.size()) { + throw std::logic_error("bounds error erasing item from OHArray"); + } + int n = int(idx); + elements.elements.erase(elements.elements.cbegin() + n); } } diff --git a/libqpdf/qpdf/OHArray.hh b/libqpdf/qpdf/OHArray.hh index e4d880db..563145c2 100644 --- a/libqpdf/qpdf/OHArray.hh +++ b/libqpdf/qpdf/OHArray.hh @@ -12,9 +12,6 @@ class OHArray { public: OHArray(); - void erase(size_t idx); - void insert(size_t idx, QPDFObjectHandle oh); - OHArray copy(); private: friend class QPDF_Array;