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