Skip to content

Commit 41e0775

Browse files
committed
Replace to_string compare with == object compare
1 parent 80fd573 commit 41e0775

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/ast_fwd_decl.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,29 @@ namespace Sass {
365365
return ex.isNull() ? 0 : ex->hash();
366366
}
367367
};
368+
template <class T>
369+
bool OrderFunction(const T& lhs, const T& rhs) {
370+
return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs;
371+
};
368372
struct OrderNodes {
369373
template <class T>
370374
bool operator() (const T& lhs, const T& rhs) const {
371-
return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs;
375+
return OrderFunction<T>(lhs, rhs);
372376
}
373377
};
374-
struct CompareNodes {
375-
template <class T>
376-
bool operator() (const T& lhs, const T& rhs) const {
378+
template <class T>
379+
bool CompareFunction(const T& lhs, const T& rhs) {
377380
// code around sass logic issue. 1px == 1 is true
378381
// but both items are still different keys in maps
379382
if (dynamic_cast<Number*>(lhs.ptr()))
380383
if (dynamic_cast<Number*>(rhs.ptr()))
381384
return lhs->hash() == rhs->hash();
382385
return !lhs.isNull() && !rhs.isNull() && *lhs == *rhs;
386+
}
387+
struct CompareNodes {
388+
template <class T>
389+
bool operator() (const T& lhs, const T& rhs) const {
390+
return CompareFunction<T>(lhs, rhs);
383391
}
384392
};
385393

@@ -423,6 +431,9 @@ namespace Sass {
423431
typedef std::pair<Complex_Selector_Obj, SubSetMapPairs> SubSetMapResult;
424432
typedef std::vector<SubSetMapResult> SubSetMapResults;
425433

434+
#define OrderSelectors OrderFunction<Selector_Obj>
435+
typedef std::set<Selector_Obj, OrderNodes> SelectorSet;
436+
426437
typedef std::deque<Complex_Selector_Obj> ComplexSelectorDeque;
427438
typedef std::set<Simple_Selector_Obj, OrderNodes> SimpleSelectorSet;
428439
typedef std::set<Complex_Selector_Obj, OrderNodes> ComplexSelectorSet;

src/ast_sel_unify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Sass {
3535
{
3636
const size_t rsize = rhs->length();
3737
for (size_t i = 0; i < rsize; ++i)
38-
{ if (*this == *rhs->at(i)) return rhs; }
38+
{ if (*this == *rhs->get(i)) return rhs; }
3939
const int lhs_order = this->unification_order();
4040
size_t i = rsize;
4141
while (i > 0 && lhs_order < rhs->at(i - 1)->unification_order()) --i;

src/ast_selectors.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "emitter.hpp"
88
#include "color_maps.hpp"
99
#include "ast_fwd_decl.hpp"
10+
#include "ast_selectors.hpp"
1011
#include <set>
1112
#include <iomanip>
1213
#include <iostream>
@@ -154,19 +155,17 @@ namespace Sass {
154155
return false;
155156
}
156157

157-
// would like to replace this without stringification
158+
// replaced compare without stringification
158159
// https://github.com/sass/sass/issues/2229
159-
// SimpleSelectorSet lset, rset;
160-
std::set<std::string> lset, rset;
160+
SelectorSet lset, rset;
161161

162162
if (lbase && rbase)
163163
{
164-
if (lbase->to_string() == rbase->to_string()) {
165-
for (size_t i = 1, L = length(); i < L; ++i)
166-
{ lset.insert((*this)[i]->to_string()); }
167-
for (size_t i = 1, L = rhs->length(); i < L; ++i)
168-
{ rset.insert((*rhs)[i]->to_string()); }
169-
return includes(rset.begin(), rset.end(), lset.begin(), lset.end());
164+
if (*lbase == *rbase) {
165+
// create ordered sets for includes query
166+
lset.insert(this->begin(), this->end());
167+
rset.insert(rhs->begin(), rhs->end());
168+
return std::includes(rset.begin(), rset.end(), lset.begin(), lset.end(), OrderSelectors);
170169
}
171170
return false;
172171
}
@@ -203,8 +202,7 @@ namespace Sass {
203202
}}
204203
}
205204
}
206-
// match from here on as strings
207-
lset.insert(wlhs->to_string());
205+
lset.insert(wlhs);
208206
}
209207

210208
for (size_t n = 0, nL = rhs->length(); n < nL; ++n)
@@ -227,15 +225,16 @@ namespace Sass {
227225
}
228226
}
229227
}
230-
rset.insert(r->to_string());
228+
rset.insert(r);
231229
}
232230

233231
//for (auto l : lset) { cerr << "l: " << l << endl; }
234232
//for (auto r : rset) { cerr << "r: " << r << endl; }
235233

236234
if (lset.empty()) return true;
235+
237236
// return true if rset contains all the elements of lset
238-
return includes(rset.begin(), rset.end(), lset.begin(), lset.end());
237+
return std::includes(rset.begin(), rset.end(), lset.begin(), lset.end(), OrderSelectors);
239238

240239
}
241240

@@ -906,4 +905,4 @@ namespace Sass {
906905
IMPLEMENT_AST_OPERATORS(Wrapped_Selector);
907906
IMPLEMENT_AST_OPERATORS(Selector_List);
908907

909-
}
908+
}

0 commit comments

Comments
 (0)