AK: Don't iterate through entire String for String::ends_with
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

This commit is contained in:
Shannon Booth 2025-04-29 01:45:40 +12:00 committed by Tim Flynn
commit 23f3ecbe98
Notes: github-actions[bot] 2025-04-28 14:51:37 +00:00

View file

@ -352,14 +352,19 @@ bool String::starts_with_bytes(StringView bytes, CaseSensitivity case_sensitivit
bool String::ends_with(u32 code_point) const bool String::ends_with(u32 code_point) const
{ {
ASSERT(is_unicode(code_point));
if (is_empty()) if (is_empty())
return false; return false;
u32 last_code_point = 0; Array<u8, 4> code_point_as_utf8;
for (auto it = code_points().begin(); it != code_points().end(); ++it) size_t i = 0;
last_code_point = *it;
return last_code_point == code_point; size_t code_point_byte_length = UnicodeUtils::code_point_to_utf8(code_point, [&](auto byte) {
code_point_as_utf8[i++] = static_cast<u8>(byte);
});
return ends_with_bytes(StringView { code_point_as_utf8.data(), code_point_byte_length });
} }
bool String::ends_with_bytes(StringView bytes, CaseSensitivity case_sensitivity) const bool String::ends_with_bytes(StringView bytes, CaseSensitivity case_sensitivity) const