From b685584e4230b59eed6198f37f6c910721a1bc86 Mon Sep 17 00:00:00 2001 From: Pras Velagapudi Date: Sun, 13 Feb 2022 06:28:03 -0500 Subject: [PATCH] Add optimization for find implementation. --- .clang-format | 1 - include/valijson/adapters/yaml_cpp_adapter.hpp | 16 ++++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.clang-format b/.clang-format index 62b0817..49cd38d 100644 --- a/.clang-format +++ b/.clang-format @@ -14,5 +14,4 @@ BraceWrapping: IndentBraces: false BreakBeforeBraces: Custom IndentWidth: 4 -ColumnLimit: 0 AllowShortFunctionsOnASingleLine: Empty diff --git a/include/valijson/adapters/yaml_cpp_adapter.hpp b/include/valijson/adapters/yaml_cpp_adapter.hpp index 76f7e7d..a0c5d0a 100644 --- a/include/valijson/adapters/yaml_cpp_adapter.hpp +++ b/include/valijson/adapters/yaml_cpp_adapter.hpp @@ -681,12 +681,16 @@ inline YamlCppObjectMemberIterator YamlCppObject::end() const inline YamlCppObjectMemberIterator YamlCppObject::find(const std::string &propertyName) const { - // TODO: I'm not sure how to do this in yaml-cpp - YAML::const_iterator itr; - for (itr = m_value.begin(); itr != m_value.end(); ++itr) - if (itr->first.as() == propertyName) - return itr; - return itr; + YAML::Node result = m_value[propertyName]; + if (!result.IsDefined()) + return end(); + + // yaml-cpp does not offer an iterator-based lookup, + // so instead we create a new placeholder object and + // return an iterator to that container instead. + YAML::Node wrapper = YAML::Node(YAML::NodeType::Map); + wrapper[propertyName] = result; + return YamlCppObjectMemberIterator(wrapper.begin()); } } // namespace adapters