Commit 14f445b197beaa2456faa788566d560d316fa13b
1 parent
696802a7
Refactor `Array::erase` to add `size_t` overload, simplify logic, improve bounds…
… checking, and align with `Array` API conventions.
Showing
2 changed files
with
26 additions
and
20 deletions
libqpdf/QPDF_Array.cc
| ... | ... | @@ -340,37 +340,42 @@ Array::push_back(QPDFObjectHandle const& item) |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | bool |
| 343 | -Array::erase(int at_i) | |
| 343 | +Array::erase(size_t at) | |
| 344 | 344 | { |
| 345 | 345 | auto a = array(); |
| 346 | - if (at_i < 0) { | |
| 347 | - return false; | |
| 348 | - } | |
| 349 | - size_t at = to_s(at_i); | |
| 350 | 346 | if (at >= size()) { |
| 351 | 347 | return false; |
| 352 | 348 | } |
| 353 | - if (a->sp) { | |
| 354 | - auto end = a->sp->elements.end(); | |
| 355 | - if (auto iter = a->sp->elements.lower_bound(at); iter != end) { | |
| 356 | - if (iter->first == at) { | |
| 357 | - iter++; | |
| 358 | - a->sp->elements.erase(at); | |
| 359 | - } | |
| 349 | + if (!a->sp) { | |
| 350 | + a->elements.erase(a->elements.cbegin() + to_i(at)); | |
| 351 | + return true; | |
| 352 | + } | |
| 353 | + auto end = a->sp->elements.end(); | |
| 354 | + if (auto iter = a->sp->elements.lower_bound(at); iter != end) { | |
| 355 | + if (iter->first == at) { | |
| 356 | + iter++; | |
| 357 | + a->sp->elements.erase(at); | |
| 358 | + } | |
| 360 | 359 | |
| 361 | - while (iter != end) { | |
| 362 | - auto nh = a->sp->elements.extract(iter++); | |
| 363 | - --nh.key(); | |
| 364 | - a->sp->elements.insert(std::move(nh)); | |
| 365 | - } | |
| 360 | + while (iter != end) { | |
| 361 | + auto nh = a->sp->elements.extract(iter++); | |
| 362 | + --nh.key(); | |
| 363 | + a->sp->elements.insert(std::move(nh)); | |
| 366 | 364 | } |
| 367 | - --(a->sp->size); | |
| 368 | - } else { | |
| 369 | - a->elements.erase(a->elements.cbegin() + at_i); | |
| 370 | 365 | } |
| 366 | + --(a->sp->size); | |
| 371 | 367 | return true; |
| 372 | 368 | } |
| 373 | 369 | |
| 370 | +bool | |
| 371 | +Array::erase(int at_i) | |
| 372 | +{ | |
| 373 | + if (at_i < 0) { | |
| 374 | + return false; | |
| 375 | + } | |
| 376 | + return erase(to_s(at_i)); | |
| 377 | +} | |
| 378 | + | |
| 374 | 379 | int |
| 375 | 380 | QPDFObjectHandle::getArrayNItems() const |
| 376 | 381 | { | ... | ... |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| ... | ... | @@ -99,6 +99,7 @@ namespace qpdf |
| 99 | 99 | bool insert(size_t at, QPDFObjectHandle const& item); |
| 100 | 100 | bool insert(int at, QPDFObjectHandle const& item); |
| 101 | 101 | void push_back(QPDFObjectHandle const& item); |
| 102 | + bool erase(size_t at); | |
| 102 | 103 | bool erase(int at); |
| 103 | 104 | |
| 104 | 105 | std::vector<QPDFObjectHandle> getAsVector() const; | ... | ... |