mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-22 22:58:33 +00:00
Refactor QPDF_Array::insertItem and rename to insert
This commit is contained in:
parent
cedb37caa1
commit
1bb23d0545
@ -932,9 +932,12 @@ QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
|
||||
void
|
||||
QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)
|
||||
{
|
||||
auto array = asArray();
|
||||
if (array) {
|
||||
array->insertItem(at, item);
|
||||
if (auto array = asArray()) {
|
||||
if (!array->insert(at, item)) {
|
||||
objectWarning(
|
||||
"ignoring attempt to insert out of bounds array item");
|
||||
QTC::TC("qpdf", "QPDFObjectHandle insert array bounds");
|
||||
}
|
||||
} else {
|
||||
typeWarning("array", "ignoring attempt to insert item");
|
||||
QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");
|
||||
|
@ -236,31 +236,24 @@ QPDF_Array::setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& v)
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Array::insertItem(int at, QPDFObjectHandle const& item)
|
||||
bool
|
||||
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
|
||||
if ((at < 0) || (at > sp_elements.size())) {
|
||||
throw std::logic_error(
|
||||
"INTERNAL ERROR: bounds error accessing QPDF_Array element");
|
||||
}
|
||||
sp_elements.insert(at, item);
|
||||
return false;
|
||||
} else if (at == sz) {
|
||||
push_back(item);
|
||||
} else {
|
||||
// As special case, also allow insert beyond the end
|
||||
size_t idx = QIntC::to_size(at);
|
||||
if ((at < 0) || (at > QIntC::to_int(elements.size()))) {
|
||||
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());
|
||||
checkOwnership(item);
|
||||
if (sparse) {
|
||||
sp_elements.insert(at, item);
|
||||
} else {
|
||||
int n = int(idx);
|
||||
elements.insert(elements.cbegin() + n, item.getObj());
|
||||
elements.insert(elements.cbegin() + at, item.getObj());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -64,23 +64,23 @@ SparseOHArray::erase(int idx)
|
||||
void
|
||||
SparseOHArray::insert(int idx, QPDFObjectHandle oh)
|
||||
{
|
||||
if (idx > this->n_elements) {
|
||||
throw std::logic_error("bounds error inserting item to SparseOHArray");
|
||||
} else if (idx == this->n_elements) {
|
||||
if (idx == n_elements) {
|
||||
// Allow inserting to the last position
|
||||
append(oh);
|
||||
} else {
|
||||
decltype(this->elements) dest;
|
||||
for (auto const& iter: this->elements) {
|
||||
if (iter.first < idx) {
|
||||
dest.insert(iter);
|
||||
auto iter = elements.crbegin();
|
||||
while (iter != elements.crend()) {
|
||||
auto key = (iter++)->first;
|
||||
if (key >= idx) {
|
||||
auto nh = elements.extract(key);
|
||||
++nh.key();
|
||||
elements.insert(std::move(nh));
|
||||
} else {
|
||||
dest[iter.first + 1] = iter.second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
this->elements = dest;
|
||||
this->elements[idx] = oh.getObj();
|
||||
++this->n_elements;
|
||||
elements[idx] = oh.getObj();
|
||||
++n_elements;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ class QPDF_Array: public QPDFValue
|
||||
void setItem(int, QPDFObjectHandle const&);
|
||||
void setFromVector(std::vector<QPDFObjectHandle> const& 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 eraseItem(int at);
|
||||
|
||||
|
@ -305,6 +305,7 @@ QPDFObjectHandle array treating as empty vector 0
|
||||
QPDFObjectHandle array ignoring set item 0
|
||||
QPDFObjectHandle array ignoring replace items 0
|
||||
QPDFObjectHandle array ignoring insert item 0
|
||||
QPDFObjectHandle insert array bounds 0
|
||||
QPDFObjectHandle array ignoring append item 0
|
||||
QPDFObjectHandle array ignoring erase item 0
|
||||
QPDFObjectHandle dictionary false for hasKey 0
|
||||
|
@ -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 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 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
|
||||
|
@ -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 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 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
|
||||
|
@ -1506,6 +1506,7 @@ test_42(QPDF& pdf, char const* arg2)
|
||||
integer.appendItem(null);
|
||||
array.eraseItem(-1);
|
||||
array.eraseItem(16059);
|
||||
array.insertItem(42, "/Dontpanic"_qpdf);
|
||||
integer.eraseItem(0);
|
||||
integer.insertItem(0, null);
|
||||
integer.setArrayFromVector(std::vector<QPDFObjectHandle>());
|
||||
|
Loading…
x
Reference in New Issue
Block a user