AK: Add ASCII fast path for Utf8View::contains

This also makes `Utf8View::trim` significantly faster, since most
strings start and end with ASCII.
This commit is contained in:
Jonne Ransijn 2024-10-27 14:03:16 +01:00 committed by Andreas Kling
parent b953b5cc71
commit 7f3269fb02
Notes: github-actions[bot] 2024-10-27 15:14:24 +00:00

View file

@ -104,10 +104,19 @@ bool Utf8View::starts_with(Utf8View const& start) const
bool Utf8View::contains(u32 needle) const bool Utf8View::contains(u32 needle) const
{ {
if (needle <= 0x7f) {
// OPTIMIZATION: Fast path for ASCII
for (u8 code_point : as_string()) {
if (code_point == needle)
return true;
}
} else {
for (u32 code_point : *this) { for (u32 code_point : *this) {
if (code_point == needle) if (code_point == needle)
return true; return true;
} }
}
return false; return false;
} }