Commit 9e30de80326ad88c155725c66e3d444232119deb
1 parent
0db65e79
Remove temporary OHArray::erase, insert and copy
Showing
3 changed files
with
29 additions
and
51 deletions
libqpdf/OHArray.cc
| 1 | #include <qpdf/OHArray.hh> | 1 | #include <qpdf/OHArray.hh> |
| 2 | 2 | ||
| 3 | -#include <qpdf/QPDFObjectHandle.hh> | ||
| 4 | -#include <qpdf/QPDFObject_private.hh> | ||
| 5 | - | ||
| 6 | -#include <stdexcept> | ||
| 7 | - | ||
| 8 | -static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); | ||
| 9 | - | ||
| 10 | OHArray::OHArray() | 3 | OHArray::OHArray() |
| 11 | { | 4 | { |
| 12 | } | 5 | } |
| 13 | - | ||
| 14 | -void | ||
| 15 | -OHArray::erase(size_t idx) | ||
| 16 | -{ | ||
| 17 | - if (idx >= elements.size()) { | ||
| 18 | - throw std::logic_error("bounds error erasing item from OHArray"); | ||
| 19 | - } | ||
| 20 | - int n = int(idx); | ||
| 21 | - elements.erase(elements.cbegin() + n); | ||
| 22 | -} | ||
| 23 | - | ||
| 24 | -void | ||
| 25 | -OHArray::insert(size_t idx, QPDFObjectHandle oh) | ||
| 26 | -{ | ||
| 27 | - if (idx > elements.size()) { | ||
| 28 | - throw std::logic_error("bounds error inserting item to OHArray"); | ||
| 29 | - } else if (idx == elements.size()) { | ||
| 30 | - // Allow inserting to the last position | ||
| 31 | - elements.push_back(oh.getObj()); | ||
| 32 | - } else { | ||
| 33 | - int n = int(idx); | ||
| 34 | - elements.insert(elements.cbegin() + n, oh.getObj()); | ||
| 35 | - } | ||
| 36 | -} | ||
| 37 | - | ||
| 38 | -OHArray | ||
| 39 | -OHArray::copy() | ||
| 40 | -{ | ||
| 41 | - OHArray result; | ||
| 42 | - result.elements.reserve(elements.size()); | ||
| 43 | - for (auto const& element: elements) { | ||
| 44 | - result.elements.push_back( | ||
| 45 | - element ? (element->getObjGen().isIndirect() ? element | ||
| 46 | - : element->copy()) | ||
| 47 | - : element); | ||
| 48 | - } | ||
| 49 | - return result; | ||
| 50 | -} |
libqpdf/QPDF_Array.cc
| @@ -67,7 +67,20 @@ QPDF_Array::copy(bool shallow) | @@ -67,7 +67,20 @@ QPDF_Array::copy(bool shallow) | ||
| 67 | if (sparse) { | 67 | if (sparse) { |
| 68 | return create(shallow ? sp_elements : sp_elements.copy()); | 68 | return create(shallow ? sp_elements : sp_elements.copy()); |
| 69 | } else { | 69 | } else { |
| 70 | - return create(shallow ? elements : elements.copy()); | 70 | + if (shallow) { |
| 71 | + return create(elements); | ||
| 72 | + } else { | ||
| 73 | + OHArray result; | ||
| 74 | + result.elements.reserve(elements.elements.size()); | ||
| 75 | + for (auto const& element: elements.elements) { | ||
| 76 | + result.elements.push_back( | ||
| 77 | + element | ||
| 78 | + ? (element->getObjGen().isIndirect() ? element | ||
| 79 | + : element->copy()) | ||
| 80 | + : element); | ||
| 81 | + } | ||
| 82 | + return create(result); | ||
| 83 | + } | ||
| 71 | } | 84 | } |
| 72 | } | 85 | } |
| 73 | 86 | ||
| @@ -237,11 +250,19 @@ QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) | @@ -237,11 +250,19 @@ QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) | ||
| 237 | sp_elements.insert(QIntC::to_size(at), item); | 250 | sp_elements.insert(QIntC::to_size(at), item); |
| 238 | } else { | 251 | } else { |
| 239 | // As special case, also allow insert beyond the end | 252 | // As special case, also allow insert beyond the end |
| 253 | + size_t idx = QIntC::to_size(at); | ||
| 240 | if ((at < 0) || (at > QIntC::to_int(elements.elements.size()))) { | 254 | if ((at < 0) || (at > QIntC::to_int(elements.elements.size()))) { |
| 241 | throw std::logic_error( | 255 | throw std::logic_error( |
| 242 | "INTERNAL ERROR: bounds error accessing QPDF_Array element"); | 256 | "INTERNAL ERROR: bounds error accessing QPDF_Array element"); |
| 243 | } | 257 | } |
| 244 | - elements.insert(QIntC::to_size(at), item); | 258 | + if (idx == elements.elements.size()) { |
| 259 | + // Allow inserting to the last position | ||
| 260 | + elements.elements.push_back(item.getObj()); | ||
| 261 | + } else { | ||
| 262 | + int n = int(idx); | ||
| 263 | + elements.elements.insert( | ||
| 264 | + elements.elements.cbegin() + n, item.getObj()); | ||
| 265 | + } | ||
| 245 | } | 266 | } |
| 246 | } | 267 | } |
| 247 | 268 | ||
| @@ -261,6 +282,11 @@ QPDF_Array::eraseItem(int at) | @@ -261,6 +282,11 @@ QPDF_Array::eraseItem(int at) | ||
| 261 | if (sparse) { | 282 | if (sparse) { |
| 262 | sp_elements.erase(QIntC::to_size(at)); | 283 | sp_elements.erase(QIntC::to_size(at)); |
| 263 | } else { | 284 | } else { |
| 264 | - elements.erase(QIntC::to_size(at)); | 285 | + size_t idx = QIntC::to_size(at); |
| 286 | + if (idx >= elements.elements.size()) { | ||
| 287 | + throw std::logic_error("bounds error erasing item from OHArray"); | ||
| 288 | + } | ||
| 289 | + int n = int(idx); | ||
| 290 | + elements.elements.erase(elements.elements.cbegin() + n); | ||
| 265 | } | 291 | } |
| 266 | } | 292 | } |
libqpdf/qpdf/OHArray.hh
| @@ -12,9 +12,6 @@ class OHArray | @@ -12,9 +12,6 @@ class OHArray | ||
| 12 | { | 12 | { |
| 13 | public: | 13 | public: |
| 14 | OHArray(); | 14 | OHArray(); |
| 15 | - void erase(size_t idx); | ||
| 16 | - void insert(size_t idx, QPDFObjectHandle oh); | ||
| 17 | - OHArray copy(); | ||
| 18 | 15 | ||
| 19 | private: | 16 | private: |
| 20 | friend class QPDF_Array; | 17 | friend class QPDF_Array; |