diff options
author | m-holger <m-holger@kubitscheck.org> | 2022-12-10 20:48:07 +0100 |
---|---|---|
committer | m-holger <m-holger@kubitscheck.org> | 2023-04-01 14:56:10 +0200 |
commit | 1bb23d0545dfe2d651cb22b6135d99c1c9ef85d5 (patch) | |
tree | 8d9a96312225de8fa70d768bf5f6249e779fbe22 /libqpdf/QPDF_Array.cc | |
parent | cedb37caa153abfd92a91b2e39a6f32601be826c (diff) | |
download | qpdf-1bb23d0545dfe2d651cb22b6135d99c1c9ef85d5.tar.zst |
Refactor QPDF_Array::insertItem and rename to insert
Diffstat (limited to 'libqpdf/QPDF_Array.cc')
-rw-r--r-- | libqpdf/QPDF_Array.cc | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 5a80cdf5..7633266e 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -236,31 +236,24 @@ QPDF_Array::setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& v) } } -void -QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) +bool +QPDF_Array::insert(int at, QPDFObjectHandle const& item) { - if (sparse) { + int sz = size(); + if (at < 0 || at > sz) { // As special case, also allow insert beyond the end - if ((at < 0) || (at > sp_elements.size())) { - throw std::logic_error( - "INTERNAL ERROR: bounds error accessing QPDF_Array element"); - } - sp_elements.insert(at, item); + return false; + } else if (at == sz) { + push_back(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.size()))) { - throw std::logic_error( - "INTERNAL ERROR: bounds error accessing QPDF_Array element"); - } - if (idx == elements.size()) { - // Allow inserting to the last position - elements.push_back(item.getObj()); + checkOwnership(item); + if (sparse) { + sp_elements.insert(at, item); } else { - int n = int(idx); - elements.insert(elements.cbegin() + n, item.getObj()); + elements.insert(elements.cbegin() + at, item.getObj()); } } + return true; } void |