Commit 4c65b0a80070bcc458f50641db4541164bfc567e

Authored by m-holger
1 parent c72de58d

Refactor `removeKey` to `erase` in `BaseHandle`, update logic for API consistenc…

…y, and simplify implementation.
include/qpdf/ObjectHandle.hh
... ... @@ -84,6 +84,7 @@ namespace qpdf
84 84 QPDFObjectHandle operator[](int n) const;
85 85  
86 86 bool contains(std::string const& key) const;
  87 + size_t erase(std::string const& key);
87 88 QPDFObjectHandle const& operator[](std::string const& key) const;
88 89  
89 90 std::shared_ptr<QPDFObject> copy(bool shallow = false) const;
... ...
libqpdf/QPDF_Dictionary.cc
... ... @@ -53,11 +53,14 @@ BaseDictionary::getAsMap() const
53 53 return dict()->items;
54 54 }
55 55  
56   -void
57   -BaseDictionary::removeKey(std::string const& key)
  56 +size_t
  57 +BaseHandle::erase(const std::string& key)
58 58 {
59 59 // no-op if key does not exist
60   - dict()->items.erase(key);
  60 + if (auto d = as<QPDF_Dictionary>()) {
  61 + return d->items.erase(key);
  62 + }
  63 + return 0;
61 64 }
62 65  
63 66 void
... ... @@ -188,18 +191,16 @@ QPDFObjectHandle::replaceKeyAndGetOld(std::string const&amp; key, QPDFObjectHandle c
188 191 void
189 192 QPDFObjectHandle::removeKey(std::string const& key)
190 193 {
191   - if (auto dict = as_dictionary(strict)) {
192   - dict.removeKey(key);
  194 + if (erase(key) || isDictionary()) {
193 195 return;
194 196 }
195 197 typeWarning("dictionary", "ignoring key removal request");
196   - QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring removeKey");
197 198 }
198 199  
199 200 QPDFObjectHandle
200 201 QPDFObjectHandle::removeKeyAndGetOld(std::string const& key)
201 202 {
202 203 auto result = (*this)[key];
203   - removeKey(key);
  204 + erase(key);
204 205 return result ? result : newNull();
205 206 }
... ...
libqpdf/qpdf/QPDFObjectHandle_private.hh
... ... @@ -122,7 +122,6 @@ namespace qpdf
122 122 // The following methods are not part of the public API.
123 123 std::set<std::string> getKeys();
124 124 std::map<std::string, QPDFObjectHandle> const& getAsMap() const;
125   - void removeKey(std::string const& key);
126 125 void replaceKey(std::string const& key, QPDFObjectHandle value);
127 126  
128 127 using iterator = std::map<std::string, QPDFObjectHandle>::iterator;
... ...
qpdf/qpdf.testcov
... ... @@ -307,7 +307,6 @@ QPDFObjectHandle dictionary false for hasKey 0
307 307 QPDFObjectHandle dictionary empty set for getKeys 0
308 308 QPDFObjectHandle dictionary empty map for asMap 0
309 309 QPDFObjectHandle dictionary ignoring replaceKey 0
310   -QPDFObjectHandle dictionary ignoring removeKey 0
311 310 QPDFObjectHandle numeric non-numeric 0
312 311 QPDFObjectHandle erase array bounds 0
313 312 qpdf-c called qpdf_check_pdf 0
... ...