From c72de58d6c80475316bb0767e73fda0ed2ca2f9f Mon Sep 17 00:00:00 2001 From: m-holger Date: Wed, 20 Aug 2025 16:47:36 +0100 Subject: [PATCH] Remove `getKey` from `BaseDictionary`, refactor dictionary key access to use `operator[]`, and simplify null handling logic. --- libqpdf/QPDF_Dictionary.cc | 31 ++++++++----------------------- libqpdf/qpdf/QPDFObjectHandle_private.hh | 1 - qpdf/qpdf.testcov | 1 - 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index 4a7e4e7..b416a6a 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -35,21 +35,6 @@ BaseHandle::contains(std::string const& key) const return !(*this)[key].null(); } -QPDFObjectHandle -BaseDictionary::getKey(std::string const& key) const -{ - auto d = dict(); - - // PDF spec says fetching a non-existent key from a dictionary returns the null object. - auto item = d->items.find(key); - if (item != d->items.end()) { - // May be a null object - return item->second; - } - static auto constexpr msg = " -> dictionary key $VD"sv; - return QPDF_Null::create(obj, msg, key); -} - std::set BaseDictionary::getKeys() { @@ -133,11 +118,14 @@ QPDFObjectHandle::hasKey(std::string const& key) const QPDFObjectHandle QPDFObjectHandle::getKey(std::string const& key) const { - if (auto dict = as_dictionary(strict)) { - return dict.getKey(key); + if (auto result = (*this)[key]) { + return result; + } + if (isDictionary()) { + static auto constexpr msg = " -> dictionary key $VD"sv; + return QPDF_Null::create(obj, msg, key); } typeWarning("dictionary", "returning null for attempted key retrieval"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey"); static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv; return QPDF_Null::create(obj, msg, ""); } @@ -211,10 +199,7 @@ QPDFObjectHandle::removeKey(std::string const& key) QPDFObjectHandle QPDFObjectHandle::removeKeyAndGetOld(std::string const& key) { - auto result = QPDFObjectHandle::newNull(); - if (auto dict = as_dictionary(strict)) { - result = dict.getKey(key); - } + auto result = (*this)[key]; removeKey(key); - return result; + return result ? result : newNull(); } diff --git a/libqpdf/qpdf/QPDFObjectHandle_private.hh b/libqpdf/qpdf/QPDFObjectHandle_private.hh index 4d2f972..48f12c7 100644 --- a/libqpdf/qpdf/QPDFObjectHandle_private.hh +++ b/libqpdf/qpdf/QPDFObjectHandle_private.hh @@ -120,7 +120,6 @@ namespace qpdf { public: // The following methods are not part of the public API. - QPDFObjectHandle getKey(std::string const& key) const; std::set getKeys(); std::map const& getAsMap() const; void removeKey(std::string const& key); diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index 3cfd74a..1d83c89 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -304,7 +304,6 @@ QPDFObjectHandle insert array bounds 0 QPDFObjectHandle array ignoring append item 0 QPDFObjectHandle array ignoring erase item 0 QPDFObjectHandle dictionary false for hasKey 0 -QPDFObjectHandle dictionary null for getKey 0 QPDFObjectHandle dictionary empty set for getKeys 0 QPDFObjectHandle dictionary empty map for asMap 0 QPDFObjectHandle dictionary ignoring replaceKey 0 -- libgit2 0.21.4