From a89d8a0677f726ea029a67db164e0116971ffafc Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 17 Aug 2019 19:01:32 -0400 Subject: [PATCH] Refactor QPDF_Array in preparation for using SparseOHArray --- libqpdf/QPDFObjectHandle.cc | 2 +- libqpdf/QPDF_Array.cc | 35 ++++++++++++++++++++++------------- libqpdf/qpdf/QPDF_Array.hh | 2 +- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 853fdb89..051e5e97 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -722,7 +722,7 @@ QPDFObjectHandle::getArrayAsVector() std::vector result; if (isArray()) { - result = dynamic_cast(m->obj.getPointer())->getAsVector(); + dynamic_cast(m->obj.getPointer())->getAsVector(result); } else { diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index c22143a1..a912b2bd 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -3,9 +3,9 @@ #include #include -QPDF_Array::QPDF_Array(std::vector const& items) : - items(items) +QPDF_Array::QPDF_Array(std::vector const& v) { + setFromVector(v); } QPDF_Array::~QPDF_Array() @@ -26,10 +26,10 @@ std::string QPDF_Array::unparse() { std::string result = "[ "; - for (std::vector::iterator iter = this->items.begin(); - iter != this->items.end(); ++iter) + size_t size = this->items.size(); + for (size_t i = 0; i < size; ++i) { - result += (*iter).unparse(); + result += this->items.at(i).unparse(); result += " "; } result += "]"; @@ -40,10 +40,10 @@ JSON QPDF_Array::getJSON() { JSON j = JSON::makeArray(); - for (std::vector::iterator iter = this->items.begin(); - iter != this->items.end(); ++iter) + size_t size = this->items.size(); + for (size_t i = 0; i < size; ++i) { - j.addArrayElement((*iter).getJSON()); + j.addArrayElement(this->items.at(i).getJSON()); } return j; } @@ -85,10 +85,14 @@ QPDF_Array::getItem(int n) const return this->items.at(QIntC::to_size(n)); } -std::vector const& -QPDF_Array::getAsVector() const +void +QPDF_Array::getAsVector(std::vector& v) const { - return this->items; + size_t size = this->items.size(); + for (size_t i = 0; i < size; ++i) + { + v.push_back(this->items.at(i)); + } } void @@ -100,9 +104,14 @@ QPDF_Array::setItem(int n, QPDFObjectHandle const& oh) } void -QPDF_Array::setFromVector(std::vector const& items) +QPDF_Array::setFromVector(std::vector const& v) { - this->items = items; + this->items.clear(); + for (std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + { + this->items.push_back(*iter); + } } void diff --git a/libqpdf/qpdf/QPDF_Array.hh b/libqpdf/qpdf/QPDF_Array.hh index 695e6587..4d2cb005 100644 --- a/libqpdf/qpdf/QPDF_Array.hh +++ b/libqpdf/qpdf/QPDF_Array.hh @@ -19,7 +19,7 @@ class QPDF_Array: public QPDFObject int getNItems() const; QPDFObjectHandle getItem(int n) const; - std::vector const& getAsVector() const; + void getAsVector(std::vector&) const; void setItem(int, QPDFObjectHandle const&); void setFromVector(std::vector const& items);