2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-29 00:10:54 +00:00

Refactor QPDF_Array::getAsVector

This commit is contained in:
m-holger 2023-01-02 19:49:42 +00:00
parent e186da1721
commit 5072238867
3 changed files with 13 additions and 10 deletions

View File

@ -888,15 +888,14 @@ QPDFObjectHandle::getArrayAsMatrix()
std::vector<QPDFObjectHandle>
QPDFObjectHandle::getArrayAsVector()
{
std::vector<QPDFObjectHandle> result;
auto array = asArray();
if (array) {
array->getAsVector(result);
return array->getAsVector();
} else {
typeWarning("array", "treating as empty");
QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
}
return result;
return {};
}
// Array mutators

View File

@ -172,16 +172,20 @@ QPDF_Array::at(int n) const noexcept
}
}
void
QPDF_Array::getAsVector(std::vector<QPDFObjectHandle>& v) const
std::vector<QPDFObjectHandle>
QPDF_Array::getAsVector() const
{
if (sparse) {
int size = sp_elements.size();
for (int i = 0; i < size; ++i) {
v.push_back(at(i));
std::vector<QPDFObjectHandle> v;
v.reserve(size_t(size()));
for (auto const& item: sp_elements.elements) {
v.resize(size_t(item.first), null_oh);
v.push_back(item.second);
}
v.resize(size_t(size()), null_oh);
return v;
} else {
v = std::vector<QPDFObjectHandle>(elements.cbegin(), elements.cend());
return {elements.cbegin(), elements.cend()};
}
}

View File

@ -29,7 +29,7 @@ class QPDF_Array: public QPDFValue
}
QPDFObjectHandle at(int n) const noexcept;
bool setAt(int n, QPDFObjectHandle const& oh);
void getAsVector(std::vector<QPDFObjectHandle>&) const;
std::vector<QPDFObjectHandle> getAsVector() const;
void setFromVector(std::vector<QPDFObjectHandle> const& items);
void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items);
bool insert(int at, QPDFObjectHandle const& item);