Commit b1d5a928b589140f430e11e016de3648a2920866

Authored by m-holger
1 parent 091580cc

Refactor `Array::setAt` to `Array::set`, update method signatures for consistenc…

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