From 41e0775ddd51e51b7bf0131e234f09c7ec5de541 Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Sun, 18 Mar 2018 04:05:30 +0100 Subject: [PATCH] Replace `to_string` compare with `==` object compare --- src/ast_fwd_decl.hpp | 19 +++++++++++++++---- src/ast_sel_unify.cpp | 2 +- src/ast_selectors.cpp | 27 +++++++++++++-------------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/ast_fwd_decl.hpp b/src/ast_fwd_decl.hpp index 6fb30dff7..d92e8d7d9 100644 --- a/src/ast_fwd_decl.hpp +++ b/src/ast_fwd_decl.hpp @@ -365,21 +365,29 @@ namespace Sass { return ex.isNull() ? 0 : ex->hash(); } }; + template + bool OrderFunction(const T& lhs, const T& rhs) { + return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs; + }; struct OrderNodes { template bool operator() (const T& lhs, const T& rhs) const { - return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs; + return OrderFunction(lhs, rhs); } }; - struct CompareNodes { - template - bool operator() (const T& lhs, const T& rhs) const { + template + bool CompareFunction(const T& lhs, const T& rhs) { // code around sass logic issue. 1px == 1 is true // but both items are still different keys in maps if (dynamic_cast(lhs.ptr())) if (dynamic_cast(rhs.ptr())) return lhs->hash() == rhs->hash(); return !lhs.isNull() && !rhs.isNull() && *lhs == *rhs; + } + struct CompareNodes { + template + bool operator() (const T& lhs, const T& rhs) const { + return CompareFunction(lhs, rhs); } }; @@ -423,6 +431,9 @@ namespace Sass { typedef std::pair SubSetMapResult; typedef std::vector SubSetMapResults; + #define OrderSelectors OrderFunction + typedef std::set SelectorSet; + typedef std::deque ComplexSelectorDeque; typedef std::set SimpleSelectorSet; typedef std::set ComplexSelectorSet; diff --git a/src/ast_sel_unify.cpp b/src/ast_sel_unify.cpp index de6a62150..ca752ab1a 100644 --- a/src/ast_sel_unify.cpp +++ b/src/ast_sel_unify.cpp @@ -35,7 +35,7 @@ namespace Sass { { const size_t rsize = rhs->length(); for (size_t i = 0; i < rsize; ++i) - { if (*this == *rhs->at(i)) return rhs; } + { if (*this == *rhs->get(i)) return rhs; } const int lhs_order = this->unification_order(); size_t i = rsize; while (i > 0 && lhs_order < rhs->at(i - 1)->unification_order()) --i; diff --git a/src/ast_selectors.cpp b/src/ast_selectors.cpp index fcb8fc2cb..dc030593c 100644 --- a/src/ast_selectors.cpp +++ b/src/ast_selectors.cpp @@ -7,6 +7,7 @@ #include "emitter.hpp" #include "color_maps.hpp" #include "ast_fwd_decl.hpp" +#include "ast_selectors.hpp" #include #include #include @@ -154,19 +155,17 @@ namespace Sass { return false; } - // would like to replace this without stringification + // replaced compare without stringification // https://github.com/sass/sass/issues/2229 - // SimpleSelectorSet lset, rset; - std::set lset, rset; + SelectorSet lset, rset; if (lbase && rbase) { - if (lbase->to_string() == rbase->to_string()) { - for (size_t i = 1, L = length(); i < L; ++i) - { lset.insert((*this)[i]->to_string()); } - for (size_t i = 1, L = rhs->length(); i < L; ++i) - { rset.insert((*rhs)[i]->to_string()); } - return includes(rset.begin(), rset.end(), lset.begin(), lset.end()); + if (*lbase == *rbase) { + // create ordered sets for includes query + lset.insert(this->begin(), this->end()); + rset.insert(rhs->begin(), rhs->end()); + return std::includes(rset.begin(), rset.end(), lset.begin(), lset.end(), OrderSelectors); } return false; } @@ -203,8 +202,7 @@ namespace Sass { }} } } - // match from here on as strings - lset.insert(wlhs->to_string()); + lset.insert(wlhs); } for (size_t n = 0, nL = rhs->length(); n < nL; ++n) @@ -227,15 +225,16 @@ namespace Sass { } } } - rset.insert(r->to_string()); + rset.insert(r); } //for (auto l : lset) { cerr << "l: " << l << endl; } //for (auto r : rset) { cerr << "r: " << r << endl; } if (lset.empty()) return true; + // return true if rset contains all the elements of lset - return includes(rset.begin(), rset.end(), lset.begin(), lset.end()); + return std::includes(rset.begin(), rset.end(), lset.begin(), lset.end(), OrderSelectors); } @@ -906,4 +905,4 @@ namespace Sass { IMPLEMENT_AST_OPERATORS(Wrapped_Selector); IMPLEMENT_AST_OPERATORS(Selector_List); -} \ No newline at end of file +}