Skip to content

Commit 49d2e92

Browse files
committed
Refactor loops in Selector::unify_with
1. Use range loops for readability. 2. Iterate over references to avoid needlessly copying SharedImpl.
1 parent c450582 commit 49d2e92

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

src/ast_sel_unify.cpp

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,20 @@ namespace Sass {
2323
{
2424
if (empty()) return rhs;
2525
Compound_Selector_Obj unified = SASS_MEMORY_COPY(rhs);
26-
for (size_t i = 0, L = length(); i < L; ++i)
27-
{
26+
for (const Simple_Selector_Obj& sel : elements()) {
2827
if (unified.isNull()) break;
29-
unified = at(i)->unify_with(unified);
28+
unified = sel->unify_with(unified);
3029
}
3130
return unified.detach();
3231
}
3332

3433
Compound_Selector_Ptr Simple_Selector::unify_with(Compound_Selector_Ptr rhs)
3534
{
36-
const size_t rsize = rhs->length();
37-
for (size_t i = 0; i < rsize; ++i)
38-
{ if (*this == *rhs->get(i)) return rhs; }
35+
for (const Simple_Selector_Obj& sel : rhs->elements()) {
36+
if (*this == *sel) return rhs;
37+
}
3938
const int lhs_order = this->unification_order();
40-
size_t i = rsize;
39+
size_t i = rhs->length();
4140
while (i > 0 && lhs_order < rhs->at(i - 1)->unification_order()) --i;
4241
rhs->elements().insert(rhs->elements().begin() + i, this);
4342
return rhs;
@@ -128,10 +127,9 @@ namespace Sass {
128127

129128
Compound_Selector_Ptr Id_Selector::unify_with(Compound_Selector_Ptr rhs)
130129
{
131-
for (size_t i = 0, L = rhs->length(); i < L; ++i)
132-
{
133-
if (Id_Selector_Ptr sel = Cast<Id_Selector>(rhs->at(i))) {
134-
if (sel->name() != name()) return 0;
130+
for (const Simple_Selector_Obj& sel : rhs->elements()) {
131+
if (Id_Selector_Ptr id_sel = Cast<Id_Selector>(sel)) {
132+
if (id_sel->name() != name()) return nullptr;
135133
}
136134
}
137135
rhs->has_line_break(has_line_break());
@@ -140,12 +138,10 @@ namespace Sass {
140138

141139
Compound_Selector_Ptr Pseudo_Selector::unify_with(Compound_Selector_Ptr rhs)
142140
{
143-
if (is_pseudo_element())
144-
{
145-
for (size_t i = 0, L = rhs->length(); i < L; ++i)
146-
{
147-
if (Pseudo_Selector_Ptr sel = Cast<Pseudo_Selector>(rhs->at(i))) {
148-
if (sel->is_pseudo_element() && sel->name() != name()) return 0;
141+
if (is_pseudo_element()) {
142+
for (const Simple_Selector_Obj& sel : rhs->elements()) {
143+
if (Pseudo_Selector_Ptr pseudo_sel = Cast<Pseudo_Selector>(sel)) {
144+
if (pseudo_sel->is_pseudo_element() && pseudo_sel->name() != name()) return nullptr;
149145
}
150146
}
151147
}
@@ -221,26 +217,22 @@ namespace Sass {
221217
}
222218

223219
Selector_List_Ptr Selector_List::unify_with(Selector_List_Ptr rhs) {
224-
std::vector<Complex_Selector_Obj> unified_complex_selectors;
220+
std::vector<Complex_Selector_Obj> result;
225221
// Unify all of children with RHS's children, storing the results in `unified_complex_selectors`
226-
for (size_t lhs_i = 0, lhs_L = length(); lhs_i < lhs_L; ++lhs_i) {
227-
Complex_Selector_Obj seq1 = (*this)[lhs_i];
228-
for(size_t rhs_i = 0, rhs_L = rhs->length(); rhs_i < rhs_L; ++rhs_i) {
229-
Complex_Selector_Ptr seq2 = rhs->at(rhs_i);
230-
231-
Selector_List_Obj result = seq1->unify_with(seq2);
232-
if( result ) {
233-
for(size_t i = 0, L = result->length(); i < L; ++i) {
234-
unified_complex_selectors.push_back( (*result)[i] );
235-
}
222+
for (Complex_Selector_Obj& seq1 : elements()) {
223+
for (Complex_Selector_Obj& seq2 : rhs->elements()) {
224+
Selector_List_Obj unified = seq1->unify_with(seq2);
225+
if (unified) {
226+
result.reserve(result.size() + unified->length());
227+
std::copy(unified->begin(), unified->end(), std::back_inserter(result));
236228
}
237229
}
238230
}
239231

240232
// Creates the final Selector_List by combining all the complex selectors
241-
Selector_List_Ptr final_result = SASS_MEMORY_NEW(Selector_List, pstate());
242-
for (auto itr = unified_complex_selectors.begin(); itr != unified_complex_selectors.end(); ++itr) {
243-
final_result->append(*itr);
233+
Selector_List_Ptr final_result = SASS_MEMORY_NEW(Selector_List, pstate(), result.size());
234+
for (Complex_Selector_Obj& sel : result) {
235+
final_result->append(sel);
244236
}
245237
return final_result;
246238
}

0 commit comments

Comments
 (0)