Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Replace to_string compare with == object compare
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Nov 29, 2018
1 parent 80fd573 commit 41e0775
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
19 changes: 15 additions & 4 deletions src/ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,21 +365,29 @@ namespace Sass {
return ex.isNull() ? 0 : ex->hash();
}
};
template <class T>
bool OrderFunction(const T& lhs, const T& rhs) {
return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs;
};
struct OrderNodes {
template <class T>
bool operator() (const T& lhs, const T& rhs) const {
return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs;
return OrderFunction<T>(lhs, rhs);
}
};
struct CompareNodes {
template <class T>
bool operator() (const T& lhs, const T& rhs) const {
template <class T>
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<Number*>(lhs.ptr()))
if (dynamic_cast<Number*>(rhs.ptr()))
return lhs->hash() == rhs->hash();
return !lhs.isNull() && !rhs.isNull() && *lhs == *rhs;
}
struct CompareNodes {
template <class T>
bool operator() (const T& lhs, const T& rhs) const {
return CompareFunction<T>(lhs, rhs);
}
};

Expand Down Expand Up @@ -423,6 +431,9 @@ namespace Sass {
typedef std::pair<Complex_Selector_Obj, SubSetMapPairs> SubSetMapResult;
typedef std::vector<SubSetMapResult> SubSetMapResults;

#define OrderSelectors OrderFunction<Selector_Obj>
typedef std::set<Selector_Obj, OrderNodes> SelectorSet;

typedef std::deque<Complex_Selector_Obj> ComplexSelectorDeque;
typedef std::set<Simple_Selector_Obj, OrderNodes> SimpleSelectorSet;
typedef std::set<Complex_Selector_Obj, OrderNodes> ComplexSelectorSet;
Expand Down
2 changes: 1 addition & 1 deletion src/ast_sel_unify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 13 additions & 14 deletions src/ast_selectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "emitter.hpp"
#include "color_maps.hpp"
#include "ast_fwd_decl.hpp"
#include "ast_selectors.hpp"
#include <set>
#include <iomanip>
#include <iostream>
Expand Down Expand Up @@ -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<std::string> 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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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);

}

Expand Down Expand Up @@ -906,4 +905,4 @@ namespace Sass {
IMPLEMENT_AST_OPERATORS(Wrapped_Selector);
IMPLEMENT_AST_OPERATORS(Selector_List);

}
}

0 comments on commit 41e0775

Please sign in to comment.