-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: Get rid of allocations in class selector matching
- Loading branch information
1 parent
a605fcf
commit 6b11330
Showing
1 changed file
with
16 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -18,6 +18,7 @@ | |
#include <algorithm> | ||
#include <iterator> | ||
#include <memory> | ||
#include <ranges> | ||
#include <string> | ||
#include <string_view> | ||
#include <utility> | ||
|
@@ -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 | ||
|
||
|
@@ -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('#')) { | ||
|