Skip to content

Commit

Permalink
style: Get rid of allocations in class selector matching
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Jan 5, 2025
1 parent a605fcf commit 6b11330
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions style/style.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2025 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

Expand All @@ -18,6 +18,7 @@
#include <algorithm>
#include <iterator>
#include <memory>
#include <ranges>
#include <string>
#include <string_view>
#include <utility>
Expand All @@ -34,8 +35,13 @@ bool has_class(dom::Element const &element, std::string_view needle_class) {
return false;
}

auto classes = util::split(it->second, " ");
return std::ranges::any_of(classes, [&](auto const &c) { return c == needle_class; });
for (auto cls : std::string_view{it->second} | std::views::split(' ')) {
if (std::string_view{cls} == needle_class) {
return true;
}
}

return false;
}
} // namespace

Expand Down Expand Up @@ -145,8 +151,13 @@ bool is_match(style::StyledNode const &node, std::string_view selector) {
}

class_string.remove_prefix(1);
auto classes = util::split(class_string, ".");
return std::ranges::all_of(classes, [&](auto const &c) { return has_class(element, c); });
for (auto cls : class_string | std::views::split('.')) {
if (!has_class(element, std::string_view{cls})) {
return false;
}
}

return true;
}

if (selector_.starts_with('#')) {
Expand Down

0 comments on commit 6b11330

Please sign in to comment.