From f42de31c9ba947f2c991265e0bef4bb54ce92222 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 16 Aug 2025 20:20:59 +0100 Subject: [PATCH] Add private-API subscript operators to `QPDFObjectHandle` for size_t and int --- include/qpdf/ObjectHandle.hh | 4 ++++ libqpdf/QPDF_Array.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 0 deletions(-) diff --git a/include/qpdf/ObjectHandle.hh b/include/qpdf/ObjectHandle.hh index 213124b..296da03 100644 --- a/include/qpdf/ObjectHandle.hh +++ b/include/qpdf/ObjectHandle.hh @@ -73,6 +73,10 @@ namespace qpdf return size() == 0; } + QPDFObjectHandle operator[](size_t n) const; + + QPDFObjectHandle operator[](int n) const; + std::shared_ptr copy(bool shallow = false) const; // Recursively remove association with any QPDF object. This method may only be called // during final destruction. diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 1057528..a88e0a9 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -528,3 +528,47 @@ BaseHandle::size() const return 0; // unreachable } } + +QPDFObjectHandle +BaseHandle::operator[](size_t n) const +{ + switch (resolved_type_code()) { + case ::ot_array: + { + auto a = as(); + if (n >= a->size()) { + return {}; + } + return Array(obj).at(static_cast(n)).second; + } + case ::ot_uninitialized: + case ::ot_reserved: + case ::ot_null: + case ::ot_destroyed: + case ::ot_unresolved: + case ::ot_reference: + return {}; + case ::ot_boolean: + case ::ot_integer: + case ::ot_real: + case ::ot_string: + case ::ot_name: + case ::ot_dictionary: + case ::ot_stream: + case ::ot_inlineimage: + case ::ot_operator: + return {obj}; + default: + throw std::logic_error("Unexpected type code in size"); // unreachable + return {}; // unreachable + } +} + +QPDFObjectHandle +BaseHandle::operator[](int n) const +{ + if (n < 0) { + return {}; + } + return (*this)[static_cast(n)]; +} -- libgit2 0.21.4