From a46e9b2adbd9907804b023e2bcbfc1de2276b8e2 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 25 Jul 2025 09:07:22 -0400 Subject: [PATCH] AK: Compute the correct capacity in StringBuilder::try_append_repeated This was mistakenly broken in 2803d66d87dd177695db11ae46a2b5f1a89c2e1b. --- AK/StringBuilder.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index b15b01ee615..8547e2502c9 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -179,9 +179,17 @@ ErrorOr StringBuilder::try_append_repeated(StringView string, size_t n) { if (string.is_empty()) return {}; - TRY(will_append(string.length() * (m_mode == Mode::UTF8 ? 1 : 2))); + + if (m_mode == Mode::UTF8) { + TRY(will_append(string.length() * n)); + } else { + auto utf16_length = simdutf::utf16_length_from_utf8(string.characters_without_null_termination(), string.length()); + TRY(will_append(utf16_length * n * 2)); + } + for (size_t i = 0; i < n; ++i) TRY(try_append(string)); + return {}; }