Commit 091580cc2d9cced9772704cabf95a62bc234d5d9
1 parent
92de8397
Refactor `Array` API: replace `at` with `get` for improved clarity, update tests…
… to use subscript operators, and simplify object access logic.
Showing
4 changed files
with
60 additions
and
50 deletions
libqpdf/QPDF_Array.cc
| ... | ... | @@ -206,18 +206,27 @@ Array::operator[](int n) const |
| 206 | 206 | return (*this)[static_cast<size_t>(n)]; |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | -std::pair<bool, QPDFObjectHandle> | |
| 210 | -Array::at(int n) const | |
| 209 | +QPDFObjectHandle | |
| 210 | +Array::get(size_t n) const | |
| 211 | 211 | { |
| 212 | - auto a = array(); | |
| 213 | - if (n < 0 || std::cmp_greater_equal(n, size())) { | |
| 214 | - return {false, {}}; | |
| 212 | + if (n >= size()) { | |
| 213 | + return {}; | |
| 215 | 214 | } |
| 215 | + auto a = array(); | |
| 216 | 216 | if (!a->sp) { |
| 217 | - return {true, a->elements[to_s(n)]}; | |
| 217 | + return a->elements[n]; | |
| 218 | + } | |
| 219 | + auto const& iter = a->sp->elements.find(n); | |
| 220 | + return iter == a->sp->elements.end() ? null() : iter->second; | |
| 221 | +} | |
| 222 | + | |
| 223 | +QPDFObjectHandle | |
| 224 | +Array::get(int n) const | |
| 225 | +{ | |
| 226 | + if (n < 0) { | |
| 227 | + return {}; | |
| 218 | 228 | } |
| 219 | - auto const& iter = a->sp->elements.find(to_s(n)); | |
| 220 | - return {true, iter == a->sp->elements.end() ? null() : iter->second}; | |
| 229 | + return get(to_s(n)); | |
| 221 | 230 | } |
| 222 | 231 | |
| 223 | 232 | std::vector<QPDFObjectHandle> | ... | ... |
libqpdf/QPDF_Stream.cc
| ... | ... | @@ -442,7 +442,7 @@ Stream::filterable( |
| 442 | 442 | |
| 443 | 443 | int i = -1; |
| 444 | 444 | for (auto& filter: filters) { |
| 445 | - auto d_obj = decode_array.at(++i).second; | |
| 445 | + auto d_obj = decode_array.get(++i); | |
| 446 | 446 | if (!can_filter(decode_level, *filter, d_obj)) { |
| 447 | 447 | return false; |
| 448 | 448 | } | ... | ... |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| ... | ... | @@ -92,7 +92,8 @@ namespace qpdf |
| 92 | 92 | |
| 93 | 93 | // Return the number of elements in the array. Return 0 if the object is not an array. |
| 94 | 94 | size_t size() const; |
| 95 | - std::pair<bool, QPDFObjectHandle> at(int n) const; | |
| 95 | + QPDFObjectHandle get(size_t n) const; | |
| 96 | + QPDFObjectHandle get(int n) const; | |
| 96 | 97 | bool setAt(int at, QPDFObjectHandle const& oh); |
| 97 | 98 | bool insert(int at, QPDFObjectHandle const& item); |
| 98 | 99 | void push_back(QPDFObjectHandle const& item); | ... | ... |
libtests/sparse_array.cc
| ... | ... | @@ -26,69 +26,69 @@ main() |
| 26 | 26 | a.push_back(QPDFObjectHandle::parse("null")); |
| 27 | 27 | a.push_back(QPDFObjectHandle::parse("/Quack")); |
| 28 | 28 | assert(a.size() == 5); |
| 29 | - assert(a.at(0).second.isInteger() && (a.at(0).second.getIntValue() == 1)); | |
| 30 | - assert(a.at(1).second.isString() && (a.at(1).second.getStringValue() == "potato")); | |
| 31 | - assert(a.at(2).second.isNull()); | |
| 32 | - assert(a.at(3).second.isNull()); | |
| 33 | - assert(a.at(4).second.isName() && (a.at(4).second.getName() == "/Quack")); | |
| 29 | + assert(a[0].isInteger() && (a[0].getIntValue() == 1)); | |
| 30 | + assert(a[1].isString() && (a[1].getStringValue() == "potato")); | |
| 31 | + assert(a[2].isNull()); | |
| 32 | + assert(a[3].isNull()); | |
| 33 | + assert(a[4].isName() && (a[4].getName() == "/Quack")); | |
| 34 | 34 | |
| 35 | 35 | a.insert(4, QPDFObjectHandle::parse("/BeforeQuack")); |
| 36 | 36 | assert(a.size() == 6); |
| 37 | - assert(a.at(0).second.isInteger() && (a.at(0).second.getIntValue() == 1)); | |
| 38 | - assert(a.at(4).second.isName() && (a.at(4).second.getName() == "/BeforeQuack")); | |
| 39 | - assert(a.at(5).second.isName() && (a.at(5).second.getName() == "/Quack")); | |
| 37 | + assert(a[0].isInteger() && (a[0].getIntValue() == 1)); | |
| 38 | + assert(a[4].isName() && (a[4].getName() == "/BeforeQuack")); | |
| 39 | + assert(a[5].isName() && (a[5].getName() == "/Quack")); | |
| 40 | 40 | |
| 41 | 41 | a.insert(2, QPDFObjectHandle::parse("/Third")); |
| 42 | 42 | assert(a.size() == 7); |
| 43 | - assert(a.at(1).second.isString() && (a.at(1).second.getStringValue() == "potato")); | |
| 44 | - assert(a.at(2).second.isName() && (a.at(2).second.getName() == "/Third")); | |
| 45 | - assert(a.at(3).second.isNull()); | |
| 46 | - assert(a.at(6).second.isName() && (a.at(6).second.getName() == "/Quack")); | |
| 43 | + assert(a[1].isString() && (a[1].getStringValue() == "potato")); | |
| 44 | + assert(a[2].isName() && (a[2].getName() == "/Third")); | |
| 45 | + assert(a[3].isNull()); | |
| 46 | + assert(a[6].isName() && (a[6].getName() == "/Quack")); | |
| 47 | 47 | |
| 48 | 48 | a.insert(0, QPDFObjectHandle::parse("/First")); |
| 49 | 49 | assert(a.size() == 8); |
| 50 | - assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First")); | |
| 51 | - assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1)); | |
| 52 | - assert(a.at(7).second.isName() && (a.at(7).second.getName() == "/Quack")); | |
| 50 | + assert(a[0].isName() && (a[0].getName() == "/First")); | |
| 51 | + assert(a[1].isInteger() && (a[1].getIntValue() == 1)); | |
| 52 | + assert(a[7].isName() && (a[7].getName() == "/Quack")); | |
| 53 | 53 | |
| 54 | 54 | a.erase(6); |
| 55 | 55 | assert(a.size() == 7); |
| 56 | - assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First")); | |
| 57 | - assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1)); | |
| 58 | - assert(a.at(5).second.isNull()); | |
| 59 | - assert(a.at(6).second.isName() && (a.at(6).second.getName() == "/Quack")); | |
| 56 | + assert(a[0].isName() && (a[0].getName() == "/First")); | |
| 57 | + assert(a[1].isInteger() && (a[1].getIntValue() == 1)); | |
| 58 | + assert(a[5].isNull()); | |
| 59 | + assert(a[6].isName() && (a[6].getName() == "/Quack")); | |
| 60 | 60 | |
| 61 | 61 | a.erase(6); |
| 62 | 62 | assert(a.size() == 6); |
| 63 | - assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First")); | |
| 64 | - assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1)); | |
| 65 | - assert(a.at(3).second.isName() && (a.at(3).second.getName() == "/Third")); | |
| 66 | - assert(a.at(4).second.isNull()); | |
| 67 | - assert(a.at(5).second.isNull()); | |
| 63 | + assert(a[0].isName() && (a[0].getName() == "/First")); | |
| 64 | + assert(a[1].isInteger() && (a[1].getIntValue() == 1)); | |
| 65 | + assert(a[3].isName() && (a[3].getName() == "/Third")); | |
| 66 | + assert(a[4].isNull()); | |
| 67 | + assert(a[5].isNull()); | |
| 68 | 68 | |
| 69 | 69 | a.setAt(4, QPDFObjectHandle::parse("12")); |
| 70 | - assert(a.at(4).second.isInteger() && (a.at(4).second.getIntValue() == 12)); | |
| 70 | + assert(a[4].isInteger() && (a[4].getIntValue() == 12)); | |
| 71 | 71 | a.setAt(4, QPDFObjectHandle::newNull()); |
| 72 | - assert(a.at(4).second.isNull()); | |
| 72 | + assert(a[4].isNull()); | |
| 73 | 73 | |
| 74 | 74 | a.erase(to_i(a.size()) - 1); |
| 75 | 75 | assert(a.size() == 5); |
| 76 | - assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First")); | |
| 77 | - assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1)); | |
| 78 | - assert(a.at(3).second.isName() && (a.at(3).second.getName() == "/Third")); | |
| 79 | - assert(a.at(4).second.isNull()); | |
| 76 | + assert(a[0].isName() && (a[0].getName() == "/First")); | |
| 77 | + assert(a[1].isInteger() && (a[1].getIntValue() == 1)); | |
| 78 | + assert(a[3].isName() && (a[3].getName() == "/Third")); | |
| 79 | + assert(a[4].isNull()); | |
| 80 | 80 | |
| 81 | 81 | a.erase(to_i(a.size()) - 1); |
| 82 | 82 | assert(a.size() == 4); |
| 83 | - assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First")); | |
| 84 | - assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1)); | |
| 85 | - assert(a.at(3).second.isName() && (a.at(3).second.getName() == "/Third")); | |
| 83 | + assert(a[0].isName() && (a[0].getName() == "/First")); | |
| 84 | + assert(a[1].isInteger() && (a[1].getIntValue() == 1)); | |
| 85 | + assert(a[3].isName() && (a[3].getName() == "/Third")); | |
| 86 | 86 | |
| 87 | 87 | a.erase(to_i(a.size()) - 1); |
| 88 | 88 | assert(a.size() == 3); |
| 89 | - assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First")); | |
| 90 | - assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1)); | |
| 91 | - assert(a.at(2).second.isString() && (a.at(2).second.getStringValue() == "potato")); | |
| 89 | + assert(a[0].isName() && (a[0].getName() == "/First")); | |
| 90 | + assert(a[1].isInteger() && (a[1].getIntValue() == 1)); | |
| 91 | + assert(a[2].isString() && (a[2].getStringValue() == "potato")); | |
| 92 | 92 | |
| 93 | 93 | QPDF pdf; |
| 94 | 94 | pdf.emptyPDF(); |
| ... | ... | @@ -98,15 +98,15 @@ main() |
| 98 | 98 | auto b = qpdf::Array(obj); |
| 99 | 99 | b.setAt(5, pdf.newIndirectNull()); |
| 100 | 100 | b.setAt(7, "[0 1 2 3]"_qpdf); |
| 101 | - assert(b.at(3).second.isNull()); | |
| 102 | - assert(b.at(8).second.isNull()); | |
| 103 | - assert(b.at(5).second.isIndirect()); | |
| 101 | + assert(b[3].null()); | |
| 102 | + assert(b[8].null()); | |
| 103 | + assert(b[5].indirect()); | |
| 104 | 104 | assert( |
| 105 | 105 | QPDFObjectHandle(obj).unparse() == |
| 106 | 106 | "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]"); |
| 107 | 107 | auto c = QPDFObjectHandle(obj).unsafeShallowCopy(); |
| 108 | 108 | auto d = QPDFObjectHandle(obj).shallowCopy(); |
| 109 | - b.at(7).second.setArrayItem(2, "42"_qpdf); | |
| 109 | + b.get(7).setArrayItem(2, "42"_qpdf); | |
| 110 | 110 | assert(c.unparse() == "[ null null null null null 3 0 R null [ 0 1 42 3 ] null null ]"); |
| 111 | 111 | assert(d.unparse() == "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]"); |
| 112 | 112 | ... | ... |