2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-02-02 11:58:25 +00:00

Refactor QPDF_Array::insertItem and rename to insert

This commit is contained in:
m-holger 2022-12-10 19:48:07 +00:00
parent cedb37caa1
commit 1bb23d0545
8 changed files with 34 additions and 34 deletions

View File

@ -932,9 +932,12 @@ QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
void void
QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item) QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)
{ {
auto array = asArray(); if (auto array = asArray()) {
if (array) { if (!array->insert(at, item)) {
array->insertItem(at, item); objectWarning(
"ignoring attempt to insert out of bounds array item");
QTC::TC("qpdf", "QPDFObjectHandle insert array bounds");
}
} else { } else {
typeWarning("array", "ignoring attempt to insert item"); typeWarning("array", "ignoring attempt to insert item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item"); QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");

View File

@ -236,31 +236,24 @@ QPDF_Array::setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& v)
} }
} }
void bool
QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) 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 // As special case, also allow insert beyond the end
if ((at < 0) || (at > sp_elements.size())) { return false;
throw std::logic_error( } else if (at == sz) {
"INTERNAL ERROR: bounds error accessing QPDF_Array element"); push_back(item);
}
sp_elements.insert(at, item);
} else { } else {
// As special case, also allow insert beyond the end checkOwnership(item);
size_t idx = QIntC::to_size(at); if (sparse) {
if ((at < 0) || (at > QIntC::to_int(elements.size()))) { sp_elements.insert(at, item);
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());
} else { } else {
int n = int(idx); elements.insert(elements.cbegin() + at, item.getObj());
elements.insert(elements.cbegin() + n, item.getObj());
} }
} }
return true;
} }
void void

View File

@ -64,23 +64,23 @@ SparseOHArray::erase(int idx)
void void
SparseOHArray::insert(int idx, QPDFObjectHandle oh) SparseOHArray::insert(int idx, QPDFObjectHandle oh)
{ {
if (idx > this->n_elements) { if (idx == n_elements) {
throw std::logic_error("bounds error inserting item to SparseOHArray");
} else if (idx == this->n_elements) {
// Allow inserting to the last position // Allow inserting to the last position
append(oh); append(oh);
} else { } else {
decltype(this->elements) dest; auto iter = elements.crbegin();
for (auto const& iter: this->elements) { while (iter != elements.crend()) {
if (iter.first < idx) { auto key = (iter++)->first;
dest.insert(iter); if (key >= idx) {
auto nh = elements.extract(key);
++nh.key();
elements.insert(std::move(nh));
} else { } else {
dest[iter.first + 1] = iter.second; break;
} }
} }
this->elements = dest; elements[idx] = oh.getObj();
this->elements[idx] = oh.getObj(); ++n_elements;
++this->n_elements;
} }
} }

View File

@ -33,7 +33,7 @@ class QPDF_Array: public QPDFValue
void setItem(int, QPDFObjectHandle const&); void setItem(int, QPDFObjectHandle const&);
void setFromVector(std::vector<QPDFObjectHandle> const& items); void setFromVector(std::vector<QPDFObjectHandle> const& items);
void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items); void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items);
void insertItem(int at, QPDFObjectHandle const& item); bool insert(int at, QPDFObjectHandle const& item);
void push_back(QPDFObjectHandle const& item); void push_back(QPDFObjectHandle const& item);
void eraseItem(int at); void eraseItem(int at);

View File

@ -305,6 +305,7 @@ QPDFObjectHandle array treating as empty vector 0
QPDFObjectHandle array ignoring set item 0 QPDFObjectHandle array ignoring set item 0
QPDFObjectHandle array ignoring replace items 0 QPDFObjectHandle array ignoring replace items 0
QPDFObjectHandle array ignoring insert item 0 QPDFObjectHandle array ignoring insert item 0
QPDFObjectHandle insert array bounds 0
QPDFObjectHandle array ignoring append item 0 QPDFObjectHandle array ignoring append item 0
QPDFObjectHandle array ignoring erase item 0 QPDFObjectHandle array ignoring erase item 0
QPDFObjectHandle dictionary false for hasKey 0 QPDFObjectHandle dictionary false for hasKey 0

View File

@ -5,6 +5,7 @@ WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operatio
WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to append item WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to append item
WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item
WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item
WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to insert out of bounds array item
WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to erase item WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to erase item
WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to insert item WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to insert item
WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to replace items WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to replace items

View File

@ -5,6 +5,7 @@ WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempt
WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to append item WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to append item
WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item
WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item
WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to insert out of bounds array item
WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to erase item WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to erase item
WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to insert item WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to insert item
WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to replace items WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to replace items

View File

@ -1506,6 +1506,7 @@ test_42(QPDF& pdf, char const* arg2)
integer.appendItem(null); integer.appendItem(null);
array.eraseItem(-1); array.eraseItem(-1);
array.eraseItem(16059); array.eraseItem(16059);
array.insertItem(42, "/Dontpanic"_qpdf);
integer.eraseItem(0); integer.eraseItem(0);
integer.insertItem(0, null); integer.insertItem(0, null);
integer.setArrayFromVector(std::vector<QPDFObjectHandle>()); integer.setArrayFromVector(std::vector<QPDFObjectHandle>());