From 41001d1bf9c579598d10ce7b2a3552bebe8a7155 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Mon, 15 Jun 2020 13:10:09 -0700 Subject: [PATCH] manual algorithm conversions (#891) Signed-off-by: Rosen Penev --- include/yaml-cpp/node/convert.h | 35 +++++++++++--------------- include/yaml-cpp/node/detail/impl.h | 38 +++++++++++++++++------------ src/convert.cpp | 6 +---- src/emitterutils.cpp | 23 ++++++----------- src/node_data.cpp | 14 +++++++---- 5 files changed, 54 insertions(+), 62 deletions(-) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 532d349..e22044f 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -225,9 +225,8 @@ template struct convert> { static Node encode(const std::map& rhs) { Node node(NodeType::Map); - for (typename std::map::const_iterator it = rhs.begin(); - it != rhs.end(); ++it) - node.force_insert(it->first, it->second); + for (const auto& it : rhs) + node.force_insert(it.first, it.second); return node; } @@ -236,12 +235,12 @@ struct convert> { return false; rhs.clear(); - for (const_iterator it = node.begin(); it != node.end(); ++it) + for (const auto& it : node) #if defined(__GNUC__) && __GNUC__ < 4 // workaround for GCC 3: - rhs[it->first.template as()] = it->second.template as(); + rhs[it.first.template as()] = it.second.template as(); #else - rhs[it->first.as()] = it->second.as(); + rhs[it.first.as()] = it.second.as(); #endif return true; } @@ -252,9 +251,7 @@ template struct convert> { static Node encode(const std::vector& rhs) { Node node(NodeType::Sequence); - for (typename std::vector::const_iterator it = rhs.begin(); - it != rhs.end(); ++it) - node.push_back(*it); + std::copy(rhs.begin(), rhs.end(), std::back_inserter(rhs)); return node; } @@ -263,12 +260,12 @@ struct convert> { return false; rhs.clear(); - for (const_iterator it = node.begin(); it != node.end(); ++it) + for (const auto& it : node) #if defined(__GNUC__) && __GNUC__ < 4 // workaround for GCC 3: - rhs.push_back(it->template as()); + rhs.push_back(it.template as()); #else - rhs.push_back(it->as()); + rhs.push_back(it.as()); #endif return true; } @@ -279,9 +276,7 @@ template struct convert> { static Node encode(const std::list& rhs) { Node node(NodeType::Sequence); - for (typename std::list::const_iterator it = rhs.begin(); - it != rhs.end(); ++it) - node.push_back(*it); + std::copy(rhs.begin(), rhs.end(), std::back_inserter(rhs)); return node; } @@ -290,12 +285,12 @@ struct convert> { return false; rhs.clear(); - for (const_iterator it = node.begin(); it != node.end(); ++it) + for (const auto& it : node) #if defined(__GNUC__) && __GNUC__ < 4 // workaround for GCC 3: - rhs.push_back(it->template as()); + rhs.push_back(it.template as()); #else - rhs.push_back(it->as()); + rhs.push_back(it.as()); #endif return true; } @@ -306,9 +301,7 @@ template struct convert> { static Node encode(const std::array& rhs) { Node node(NodeType::Sequence); - for (const auto& element : rhs) { - node.push_back(element); - } + std::copy(rhs.begin(), rhs.end(), std::back_inserter(rhs)); return node; } diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index bbefd59..f8bfe38 100644 --- a/include/yaml-cpp/node/detail/impl.h +++ b/include/yaml-cpp/node/detail/impl.h @@ -9,6 +9,8 @@ #include "yaml-cpp/node/detail/node.h" #include "yaml-cpp/node/detail/node_data.h" + +#include #include namespace YAML { @@ -125,13 +127,11 @@ inline node* node_data::get(const Key& key, throw BadSubscript(m_mark, key); } - for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { - if (it->first->equals(key, pMemory)) { - return it->second; - } - } + auto it = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) { + return m.first->equals(key, pMemory); + }); - return nullptr; + return it != m_map.end() ? it->second : nullptr; } template @@ -153,10 +153,12 @@ inline node& node_data::get(const Key& key, shared_memory_holder pMemory) { throw BadSubscript(m_mark, key); } - for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { - if (it->first->equals(key, pMemory)) { - return *it->second; - } + auto it = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) { + return m.first->equals(key, pMemory); + }); + + if (it != m_map.end()) { + return it->second; } node& k = convert_to_node(key, pMemory); @@ -169,7 +171,9 @@ template inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) { if (m_type == NodeType::Sequence) { return remove_idx::remove(m_sequence, key, m_seqSize); - } else if (m_type == NodeType::Map) { + } + + if (m_type == NodeType::Map) { kv_pairs::iterator it = m_undefinedPairs.begin(); while (it != m_undefinedPairs.end()) { kv_pairs::iterator jt = std::next(it); @@ -179,11 +183,13 @@ inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) { it = jt; } - for (node_map::iterator iter = m_map.begin(); iter != m_map.end(); ++iter) { - if (iter->first->equals(key, pMemory)) { - m_map.erase(iter); - return true; - } + auto iter = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) { + return m.first->equals(key, pMemory); + }); + + if (iter != m_map.end()) { + m_map.erase(iter); + return true; } } diff --git a/src/convert.cpp b/src/convert.cpp index 0ab12b1..9658b36 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -16,11 +16,7 @@ std::string tolower(const std::string& str) { template bool IsEntirely(const std::string& str, T func) { - for (char ch : str) - if (!func(ch)) - return false; - - return true; + return std::all_of(str.begin(), str.end(), [=](char ch) { return func(ch); }); } // IsFlexibleCase diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 3764c2b..ccacc2a 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -199,15 +200,10 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType, bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) { // TODO: check for non-printable characters? - for (char ch : str) { - if (escapeNonAscii && (0x80 <= static_cast(ch))) { - return false; - } - if (ch == '\n') { - return false; - } - } - return true; + return std::none_of(str.begin(), str.end(), [=](char ch) { + return (escapeNonAscii && (0x80 <= static_cast(ch))) || + (ch == '\n'); + }); } bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType, @@ -217,12 +213,9 @@ bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType, } // TODO: check for non-printable characters? - for (char ch : str) { - if (escapeNonAscii && (0x80 <= static_cast(ch))) { - return false; - } - } - return true; + return std::none_of(str.begin(), str.end(), [=](char ch) { + return (escapeNonAscii && (0x80 <= static_cast(ch))); + }); } void WriteDoubleQuoteEscapeSequence(ostream_wrapper& out, int codePoint) { diff --git a/src/node_data.cpp b/src/node_data.cpp index cc45f21..711439f 100644 --- a/src/node_data.cpp +++ b/src/node_data.cpp @@ -252,11 +252,15 @@ bool node_data::remove(node& key, shared_memory_holder /* pMemory */) { it = jt; } - for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { - if (it->first->is(key)) { - m_map.erase(it); - return true; - } + auto it = + std::find_if(m_map.begin(), m_map.end(), + [&](std::pair j) { + return (j.first->is(key)); + }); + + if (it != m_map.end()) { + m_map.erase(it); + return true; } return false;