Commit c72de58d6c80475316bb0767e73fda0ed2ca2f9f
1 parent
08dcf84c
Remove `getKey` from `BaseDictionary`, refactor dictionary key access to use `op…
…erator[]`, and simplify null handling logic.
Showing
3 changed files
with
8 additions
and
25 deletions
libqpdf/QPDF_Dictionary.cc
| @@ -35,21 +35,6 @@ BaseHandle::contains(std::string const& key) const | @@ -35,21 +35,6 @@ BaseHandle::contains(std::string const& key) const | ||
| 35 | return !(*this)[key].null(); | 35 | return !(*this)[key].null(); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | -QPDFObjectHandle | ||
| 39 | -BaseDictionary::getKey(std::string const& key) const | ||
| 40 | -{ | ||
| 41 | - auto d = dict(); | ||
| 42 | - | ||
| 43 | - // PDF spec says fetching a non-existent key from a dictionary returns the null object. | ||
| 44 | - auto item = d->items.find(key); | ||
| 45 | - if (item != d->items.end()) { | ||
| 46 | - // May be a null object | ||
| 47 | - return item->second; | ||
| 48 | - } | ||
| 49 | - static auto constexpr msg = " -> dictionary key $VD"sv; | ||
| 50 | - return QPDF_Null::create(obj, msg, key); | ||
| 51 | -} | ||
| 52 | - | ||
| 53 | std::set<std::string> | 38 | std::set<std::string> |
| 54 | BaseDictionary::getKeys() | 39 | BaseDictionary::getKeys() |
| 55 | { | 40 | { |
| @@ -133,11 +118,14 @@ QPDFObjectHandle::hasKey(std::string const& key) const | @@ -133,11 +118,14 @@ QPDFObjectHandle::hasKey(std::string const& key) const | ||
| 133 | QPDFObjectHandle | 118 | QPDFObjectHandle |
| 134 | QPDFObjectHandle::getKey(std::string const& key) const | 119 | QPDFObjectHandle::getKey(std::string const& key) const |
| 135 | { | 120 | { |
| 136 | - if (auto dict = as_dictionary(strict)) { | ||
| 137 | - return dict.getKey(key); | 121 | + if (auto result = (*this)[key]) { |
| 122 | + return result; | ||
| 123 | + } | ||
| 124 | + if (isDictionary()) { | ||
| 125 | + static auto constexpr msg = " -> dictionary key $VD"sv; | ||
| 126 | + return QPDF_Null::create(obj, msg, key); | ||
| 138 | } | 127 | } |
| 139 | typeWarning("dictionary", "returning null for attempted key retrieval"); | 128 | typeWarning("dictionary", "returning null for attempted key retrieval"); |
| 140 | - QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey"); | ||
| 141 | static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv; | 129 | static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv; |
| 142 | return QPDF_Null::create(obj, msg, ""); | 130 | return QPDF_Null::create(obj, msg, ""); |
| 143 | } | 131 | } |
| @@ -211,10 +199,7 @@ QPDFObjectHandle::removeKey(std::string const& key) | @@ -211,10 +199,7 @@ QPDFObjectHandle::removeKey(std::string const& key) | ||
| 211 | QPDFObjectHandle | 199 | QPDFObjectHandle |
| 212 | QPDFObjectHandle::removeKeyAndGetOld(std::string const& key) | 200 | QPDFObjectHandle::removeKeyAndGetOld(std::string const& key) |
| 213 | { | 201 | { |
| 214 | - auto result = QPDFObjectHandle::newNull(); | ||
| 215 | - if (auto dict = as_dictionary(strict)) { | ||
| 216 | - result = dict.getKey(key); | ||
| 217 | - } | 202 | + auto result = (*this)[key]; |
| 218 | removeKey(key); | 203 | removeKey(key); |
| 219 | - return result; | 204 | + return result ? result : newNull(); |
| 220 | } | 205 | } |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| @@ -120,7 +120,6 @@ namespace qpdf | @@ -120,7 +120,6 @@ namespace qpdf | ||
| 120 | { | 120 | { |
| 121 | public: | 121 | public: |
| 122 | // The following methods are not part of the public API. | 122 | // The following methods are not part of the public API. |
| 123 | - QPDFObjectHandle getKey(std::string const& key) const; | ||
| 124 | std::set<std::string> getKeys(); | 123 | std::set<std::string> getKeys(); |
| 125 | std::map<std::string, QPDFObjectHandle> const& getAsMap() const; | 124 | std::map<std::string, QPDFObjectHandle> const& getAsMap() const; |
| 126 | void removeKey(std::string const& key); | 125 | void removeKey(std::string const& key); |
qpdf/qpdf.testcov
| @@ -304,7 +304,6 @@ QPDFObjectHandle insert array bounds 0 | @@ -304,7 +304,6 @@ QPDFObjectHandle insert array bounds 0 | ||
| 304 | QPDFObjectHandle array ignoring append item 0 | 304 | QPDFObjectHandle array ignoring append item 0 |
| 305 | QPDFObjectHandle array ignoring erase item 0 | 305 | QPDFObjectHandle array ignoring erase item 0 |
| 306 | QPDFObjectHandle dictionary false for hasKey 0 | 306 | QPDFObjectHandle dictionary false for hasKey 0 |
| 307 | -QPDFObjectHandle dictionary null for getKey 0 | ||
| 308 | QPDFObjectHandle dictionary empty set for getKeys 0 | 307 | QPDFObjectHandle dictionary empty set for getKeys 0 |
| 309 | QPDFObjectHandle dictionary empty map for asMap 0 | 308 | QPDFObjectHandle dictionary empty map for asMap 0 |
| 310 | QPDFObjectHandle dictionary ignoring replaceKey 0 | 309 | QPDFObjectHandle dictionary ignoring replaceKey 0 |