2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-11 07:30:57 +00:00

Refactor QPDF_Array::setItem and rename to setAt

This commit is contained in:
m-holger 2023-01-01 12:47:03 +00:00
parent 4d37389bef
commit 182c2480df
9 changed files with 26 additions and 34 deletions

View File

@ -904,16 +904,16 @@ QPDFObjectHandle::getArrayAsVector()
void
QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
{
auto array = asArray();
if (array) {
checkOwnership(item);
array->setItem(n, item);
if (auto array = asArray()) {
if (!array->setAt(n, item)) {
objectWarning("ignoring attempt to set out of bounds array item");
QTC::TC("qpdf", "QPDFObjectHandle set array bounds");
}
} else {
typeWarning("array", "ignoring attempt to set item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");
}
}
void
QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
{

View File

@ -1,9 +1,6 @@
#include <qpdf/QPDF_Array.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QUtil.hh>
#include <stdexcept>
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
@ -188,18 +185,19 @@ QPDF_Array::getAsVector(std::vector<QPDFObjectHandle>& v) const
}
}
void
QPDF_Array::setItem(int n, QPDFObjectHandle const& oh)
bool
QPDF_Array::setAt(int at, QPDFObjectHandle const& oh)
{
if (sparse) {
sp_elements.setAt(n, oh);
} else {
size_t idx = size_t(n);
if (n < 0 || idx >= elements.size()) {
throw std::logic_error("bounds error setting item in QPDF_Array");
}
elements[idx] = oh.getObj();
if (at < 0 || at >= size()) {
return false;
}
checkOwnership(oh);
if (sparse) {
sp_elements.setAt(at, oh);
} else {
elements[size_t(at)] = oh.getObj();
}
return true;
}
void

View File

@ -30,19 +30,6 @@ SparseOHArray::disconnect()
}
}
void
SparseOHArray::setAt(int idx, QPDFObjectHandle oh)
{
if (idx >= this->n_elements) {
throw std::logic_error("bounds error setting item in SparseOHArray");
}
if (oh.isDirectNull()) {
this->elements.erase(idx);
} else {
this->elements[idx] = oh.getObj();
}
}
void
SparseOHArray::erase(int at)
{

View File

@ -28,9 +28,8 @@ class QPDF_Array: public QPDFValue
return sparse ? sp_elements.size() : int(elements.size());
}
QPDFObjectHandle at(int n) const noexcept;
bool setAt(int n, QPDFObjectHandle const& oh);
void getAsVector(std::vector<QPDFObjectHandle>&) const;
void setItem(int, QPDFObjectHandle const&);
void setFromVector(std::vector<QPDFObjectHandle> const& items);
void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items);
bool insert(int at, QPDFObjectHandle const& item);

View File

@ -28,7 +28,11 @@ class SparseOHArray
}
QPDFObjectHandle at(int idx) const;
void remove_last();
void setAt(int idx, QPDFObjectHandle oh);
void
setAt(int idx, QPDFObjectHandle oh)
{
elements[idx] = oh.getObj();
}
void erase(int idx);
void insert(int idx, QPDFObjectHandle oh);
SparseOHArray copy();

View File

@ -303,6 +303,7 @@ QPDFObjectHandle array treating as empty 0
QPDFObjectHandle array null for non-array 0
QPDFObjectHandle array treating as empty vector 0
QPDFObjectHandle array ignoring set item 0
QPDFObjectHandle set array bounds 0
QPDFObjectHandle array ignoring replace items 0
QPDFObjectHandle array ignoring insert item 0
QPDFObjectHandle insert array bounds 0

View File

@ -6,6 +6,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 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 384: ignoring attempt to set 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 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

View File

@ -6,6 +6,7 @@ WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempt
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 717: ignoring attempt to set 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 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

View File

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