diff --git a/include/qpdf/ObjectHandle.hh b/include/qpdf/ObjectHandle.hh index 83a5f4b..094a1cf 100644 --- a/include/qpdf/ObjectHandle.hh +++ b/include/qpdf/ObjectHandle.hh @@ -84,6 +84,7 @@ namespace qpdf QPDFObjectHandle operator[](int n) const; bool contains(std::string const& key) const; + size_t erase(std::string const& key); QPDFObjectHandle const& operator[](std::string const& key) const; std::shared_ptr copy(bool shallow = false) const; diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index b416a6a..501ab61 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -53,11 +53,14 @@ BaseDictionary::getAsMap() const return dict()->items; } -void -BaseDictionary::removeKey(std::string const& key) +size_t +BaseHandle::erase(const std::string& key) { // no-op if key does not exist - dict()->items.erase(key); + if (auto d = as()) { + return d->items.erase(key); + } + return 0; } void @@ -188,18 +191,16 @@ QPDFObjectHandle::replaceKeyAndGetOld(std::string const& key, QPDFObjectHandle c void QPDFObjectHandle::removeKey(std::string const& key) { - if (auto dict = as_dictionary(strict)) { - dict.removeKey(key); + if (erase(key) || isDictionary()) { return; } typeWarning("dictionary", "ignoring key removal request"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring removeKey"); } QPDFObjectHandle QPDFObjectHandle::removeKeyAndGetOld(std::string const& key) { auto result = (*this)[key]; - removeKey(key); + erase(key); return result ? result : newNull(); } diff --git a/libqpdf/qpdf/QPDFObjectHandle_private.hh b/libqpdf/qpdf/QPDFObjectHandle_private.hh index 48f12c7..cd71327 100644 --- a/libqpdf/qpdf/QPDFObjectHandle_private.hh +++ b/libqpdf/qpdf/QPDFObjectHandle_private.hh @@ -122,7 +122,6 @@ namespace qpdf // The following methods are not part of the public API. std::set getKeys(); std::map const& getAsMap() const; - void removeKey(std::string const& key); void replaceKey(std::string const& key, QPDFObjectHandle value); using iterator = std::map::iterator; diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index 1d83c89..a4136fb 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -307,7 +307,6 @@ QPDFObjectHandle dictionary false for hasKey 0 QPDFObjectHandle dictionary empty set for getKeys 0 QPDFObjectHandle dictionary empty map for asMap 0 QPDFObjectHandle dictionary ignoring replaceKey 0 -QPDFObjectHandle dictionary ignoring removeKey 0 QPDFObjectHandle numeric non-numeric 0 QPDFObjectHandle erase array bounds 0 qpdf-c called qpdf_check_pdf 0