Commit b1d5a928b589140f430e11e016de3648a2920866
1 parent
091580cc
Refactor `Array::setAt` to `Array::set`, update method signatures for consistenc…
…y, modify bounds checks, and adjust affected tests.
Showing
4 changed files
with
22 additions
and
16 deletions
libqpdf/QPDF_Array.cc
| ... | ... | @@ -248,21 +248,30 @@ Array::getAsVector() const |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | bool |
| 251 | -Array::setAt(int at, QPDFObjectHandle const& oh) | |
| 251 | +Array::set(size_t at, QPDFObjectHandle const& oh) | |
| 252 | 252 | { |
| 253 | - if (at < 0 || std::cmp_greater_equal(at, size())) { | |
| 253 | + if (at >= size()) { | |
| 254 | 254 | return false; |
| 255 | 255 | } |
| 256 | 256 | auto a = array(); |
| 257 | 257 | checkOwnership(oh); |
| 258 | 258 | if (a->sp) { |
| 259 | - a->sp->elements[to_s(at)] = oh; | |
| 259 | + a->sp->elements[at] = oh; | |
| 260 | 260 | } else { |
| 261 | - a->elements[to_s(at)] = oh; | |
| 261 | + a->elements[at] = oh; | |
| 262 | 262 | } |
| 263 | 263 | return true; |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | +bool | |
| 267 | +Array::set(int at, QPDFObjectHandle const& oh) | |
| 268 | +{ | |
| 269 | + if (at < 0) { | |
| 270 | + return false; | |
| 271 | + } | |
| 272 | + return set(to_s(at), oh); | |
| 273 | +} | |
| 274 | + | |
| 266 | 275 | void |
| 267 | 276 | Array::setFromVector(std::vector<QPDFObjectHandle> const& v) |
| 268 | 277 | { |
| ... | ... | @@ -463,13 +472,11 @@ void |
| 463 | 472 | QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) |
| 464 | 473 | { |
| 465 | 474 | if (auto array = as_array(strict)) { |
| 466 | - if (!array.setAt(n, item)) { | |
| 475 | + if (!array.set(n, item)) { | |
| 467 | 476 | objectWarning("ignoring attempt to set out of bounds array item"); |
| 468 | - QTC::TC("qpdf", "QPDFObjectHandle set array bounds"); | |
| 469 | 477 | } |
| 470 | 478 | } else { |
| 471 | 479 | typeWarning("array", "ignoring attempt to set item"); |
| 472 | - QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item"); | |
| 473 | 480 | } |
| 474 | 481 | } |
| 475 | 482 | void | ... | ... |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| ... | ... | @@ -94,7 +94,8 @@ namespace qpdf |
| 94 | 94 | size_t size() const; |
| 95 | 95 | QPDFObjectHandle get(size_t n) const; |
| 96 | 96 | QPDFObjectHandle get(int n) const; |
| 97 | - bool setAt(int at, QPDFObjectHandle const& oh); | |
| 97 | + bool set(size_t at, QPDFObjectHandle const& oh); | |
| 98 | + bool set(int at, QPDFObjectHandle const& oh); | |
| 98 | 99 | bool insert(int at, QPDFObjectHandle const& item); |
| 99 | 100 | void push_back(QPDFObjectHandle const& item); |
| 100 | 101 | bool erase(int at); | ... | ... |
libtests/sparse_array.cc
| ... | ... | @@ -66,9 +66,9 @@ main() |
| 66 | 66 | assert(a[4].isNull()); |
| 67 | 67 | assert(a[5].isNull()); |
| 68 | 68 | |
| 69 | - a.setAt(4, QPDFObjectHandle::parse("12")); | |
| 69 | + a.set(4, QPDFObjectHandle::parse("12")); | |
| 70 | 70 | assert(a[4].isInteger() && (a[4].getIntValue() == 12)); |
| 71 | - a.setAt(4, QPDFObjectHandle::newNull()); | |
| 71 | + a.set(4, QPDFObjectHandle::newNull()); | |
| 72 | 72 | assert(a[4].isNull()); |
| 73 | 73 | |
| 74 | 74 | a.erase(to_i(a.size()) - 1); |
| ... | ... | @@ -96,8 +96,8 @@ main() |
| 96 | 96 | obj = QPDFObject::create<QPDF_Array>( |
| 97 | 97 | std::vector<QPDFObjectHandle>{10, "null"_qpdf.getObj()}, true); |
| 98 | 98 | auto b = qpdf::Array(obj); |
| 99 | - b.setAt(5, pdf.newIndirectNull()); | |
| 100 | - b.setAt(7, "[0 1 2 3]"_qpdf); | |
| 99 | + b.set(5, pdf.newIndirectNull()); | |
| 100 | + b.set(7, "[0 1 2 3]"_qpdf); | |
| 101 | 101 | assert(b[3].null()); |
| 102 | 102 | assert(b[8].null()); |
| 103 | 103 | assert(b[5].indirect()); |
| ... | ... | @@ -111,7 +111,7 @@ main() |
| 111 | 111 | assert(d.unparse() == "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]"); |
| 112 | 112 | |
| 113 | 113 | try { |
| 114 | - b.setAt(3, {}); | |
| 114 | + b.set(3, {}); | |
| 115 | 115 | std::cout << "inserted uninitialized object\n"; |
| 116 | 116 | } catch (std::logic_error&) { |
| 117 | 117 | } |
| ... | ... | @@ -119,7 +119,7 @@ main() |
| 119 | 119 | pdf2.emptyPDF(); |
| 120 | 120 | try { |
| 121 | 121 | pdf.makeIndirectObject(obj); |
| 122 | - b.setAt(3, pdf2.getObject(1, 0)); | |
| 122 | + b.set(3, pdf2.getObject(1, 0)); | |
| 123 | 123 | std::cout << "inserted uninitialized object\n"; |
| 124 | 124 | } catch (std::logic_error&) { |
| 125 | 125 | } | ... | ... |
qpdf/qpdf.testcov
| ... | ... | @@ -298,8 +298,6 @@ QPDFObjectHandle string returning empty utf8 0 |
| 298 | 298 | QPDFObjectHandle operator returning fake value 0 |
| 299 | 299 | QPDFObjectHandle inlineimage returning empty data 0 |
| 300 | 300 | QPDFObjectHandle array treating as empty vector 0 |
| 301 | -QPDFObjectHandle array ignoring set item 0 | |
| 302 | -QPDFObjectHandle set array bounds 0 | |
| 303 | 301 | QPDFObjectHandle array ignoring replace items 0 |
| 304 | 302 | QPDFObjectHandle array ignoring insert item 0 |
| 305 | 303 | QPDFObjectHandle insert array bounds 0 | ... | ... |