diff --git a/AK/Utf8View.cpp b/AK/Utf8View.cpp index bfd7fd95e3..791f1e2c95 100644 --- a/AK/Utf8View.cpp +++ b/AK/Utf8View.cpp @@ -126,18 +126,15 @@ bool Utf8View::starts_with(Utf8View const& start) 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) { - if (code_point == needle) - return true; - } + // OPTIMIZATION: An ASCII byte can only appear as itself in valid UTF-8, so memchr is safe here. + auto bytes = as_string(); + return memchr(bytes.characters_without_null_termination(), needle, bytes.length()) != nullptr; } + for (u32 code_point : *this) { + if (code_point == needle) + return true; + } return false; }