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 authored and Nico Weber committed 2024-11-12 07:14:25 -05:00
1 parent 8011896224
commit de1ecc17ce
1 file changed
+12 -3
+12 -3
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;
}