AK: Add ASCII fast path for Utf8View::contains

This also makes `Utf8View::trim` significantly faster, since most
strings start and end with ASCII.

(cherry picked from commit 7f3269fb025051c3eb2794b15e785b85b0ce37f3)
This commit is contained in:
Jonne Ransijn
2024-10-27 14:03:16 +01:00
committed by Nico Weber
parent 8011896224
commit de1ecc17ce

View File

@@ -104,10 +104,19 @@ bool Utf8View::starts_with(Utf8View const& start) const
bool Utf8View::contains(u32 needle) const
{
for (u32 code_point : *this) {
if (code_point == needle)
return true;
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) {
if (code_point == needle)
return true;
}
}
return false;
}