diff --git a/include/internal/cef_string_map.h b/include/internal/cef_string_map.h index 40777c118..031a6bf10 100644 --- a/include/internal/cef_string_map.h +++ b/include/internal/cef_string_map.h @@ -75,7 +75,8 @@ CEF_EXPORT int cef_string_map_value(cef_string_map_t map, cef_string_t* value); /// -/// Append a new key/value pair at the end of the string map. +/// Append a new key/value pair at the end of the string map. If the key exists, +/// overwrite the existing value with a new value w/o changing the pair order. /// CEF_EXPORT int cef_string_map_append(cef_string_map_t map, const cef_string_t* key, diff --git a/libcef/common/string_map_impl.cc b/libcef/common/string_map_impl.cc index 695e92745..3dd71ea90 100644 --- a/libcef/common/string_map_impl.cc +++ b/libcef/common/string_map_impl.cc @@ -3,13 +3,44 @@ // can be found in the LICENSE file. #include +#include #include "include/internal/cef_string_map.h" #include "base/logging.h" namespace { -using StringMap = std::map; + +class StringMap { + using Map = std::map; + using value_type = Map::value_type; + + public: + using const_iterator = Map::const_iterator; + + size_t size() const { return map_ref_.size(); } + size_t count(const CefString& key) const { return map_.count(key); } + const_iterator find(const CefString& value) const { return map_.find(value); } + const_iterator cend() const { return map_.cend(); } + const value_type& operator[](size_t pos) const { return *map_ref_[pos]; } + + void insert(value_type&& value) { + // does not invalidate iterators + const auto [it, inserted] = map_.insert(std::move(value)); + if (inserted) { + map_ref_.push_back(std::move(it)); + } + } + void clear() { + map_ref_.clear(); + map_.clear(); + } + + private: + Map map_; + std::vector map_ref_; +}; + } // namespace CEF_EXPORT cef_string_map_t cef_string_map_alloc() { @@ -29,7 +60,7 @@ CEF_EXPORT int cef_string_map_find(cef_string_map_t map, DCHECK(value); StringMap* impl = reinterpret_cast(map); StringMap::const_iterator it = impl->find(CefString(key)); - if (it == impl->end()) { + if (it == impl->cend()) { return 0; } @@ -48,13 +79,8 @@ CEF_EXPORT int cef_string_map_key(cef_string_map_t map, return 0; } - StringMap::const_iterator it = impl->begin(); - for (size_t ct = 0; it != impl->end(); ++it, ct++) { - if (ct == index) { - return cef_string_set(it->first.c_str(), it->first.length(), key, true); - } - } - return 0; + const auto& [k, _] = (*impl)[index]; + return cef_string_set(k.c_str(), k.length(), key, true); } CEF_EXPORT int cef_string_map_value(cef_string_map_t map, @@ -68,14 +94,8 @@ CEF_EXPORT int cef_string_map_value(cef_string_map_t map, return 0; } - StringMap::const_iterator it = impl->begin(); - for (size_t ct = 0; it != impl->end(); ++it, ct++) { - if (ct == index) { - return cef_string_set(it->second.c_str(), it->second.length(), value, - true); - } - } - return 0; + const auto& [_, v] = (*impl)[index]; + return cef_string_set(v.c_str(), v.length(), value, true); } CEF_EXPORT int cef_string_map_append(cef_string_map_t map, diff --git a/libcef/common/string_multimap_impl.cc b/libcef/common/string_multimap_impl.cc index f35cdd8cf..56e6d5582 100644 --- a/libcef/common/string_multimap_impl.cc +++ b/libcef/common/string_multimap_impl.cc @@ -3,13 +3,43 @@ // can be found in the LICENSE file. #include +#include #include "include/internal/cef_string_multimap.h" #include "base/logging.h" namespace { -using StringMultimap = std::multimap; + +class StringMultimap { + using Map = std::multimap; + using value_type = Map::value_type; + + public: + using const_iterator = Map::const_iterator; + + size_t size() const { return map_ref_.size(); } + size_t count(const CefString& key) const { return map_.count(key); } + const value_type operator[](size_t pos) const { return *map_ref_[pos]; } + + std::pair equal_range( + const CefString& key) const { + return map_.equal_range(key); + } + void insert(value_type&& value) { + auto it = map_.insert(std::move(value)); // does not invalidate iterators + map_ref_.push_back(std::move(it)); + } + void clear() { + map_ref_.clear(); + map_.clear(); + } + + private: + Map map_; + std::vector map_ref_; +}; + } // namespace CEF_EXPORT cef_string_multimap_t cef_string_multimap_alloc() { @@ -46,8 +76,8 @@ CEF_EXPORT int cef_string_multimap_enumerate(cef_string_multimap_t map, return 0; } - std::pair range_it = - impl->equal_range(key_str); + std::pair + range_it = impl->equal_range(key_str); size_t count = value_index; while (count-- && range_it.first != range_it.second) { @@ -73,13 +103,8 @@ CEF_EXPORT int cef_string_multimap_key(cef_string_multimap_t map, return 0; } - StringMultimap::const_iterator it = impl->begin(); - for (size_t ct = 0; it != impl->end(); ++it, ct++) { - if (ct == index) { - return cef_string_set(it->first.c_str(), it->first.length(), key, true); - } - } - return 0; + const auto& [k, _] = (*impl)[index]; + return cef_string_set(k.c_str(), k.length(), key, true); } CEF_EXPORT int cef_string_multimap_value(cef_string_multimap_t map, @@ -93,14 +118,8 @@ CEF_EXPORT int cef_string_multimap_value(cef_string_multimap_t map, return 0; } - StringMultimap::const_iterator it = impl->begin(); - for (size_t ct = 0; it != impl->end(); ++it, ct++) { - if (ct == index) { - return cef_string_set(it->second.c_str(), it->second.length(), value, - true); - } - } - return 0; + const auto& [_, v] = (*impl)[index]; + return cef_string_set(v.c_str(), v.length(), value, true); } CEF_EXPORT int cef_string_multimap_append(cef_string_multimap_t map,