From 75067368693f44bb77f7cd52e4bb2e568684318b Mon Sep 17 00:00:00 2001 From: Dan Klishch Date: Sat, 28 Oct 2023 17:06:51 -0400 Subject: [PATCH] AK: Stop using ShortString in String::from_code_point Refactor it to use StringBase::replace_with_new_short_string instead. --- AK/String.h | 14 +++++++------- AK/UnicodeUtils.h | 13 +++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/AK/String.h b/AK/String.h index 073b47cb4f5..f7cc70523b4 100644 --- a/AK/String.h +++ b/AK/String.h @@ -64,15 +64,15 @@ public: { VERIFY(is_unicode(code_point)); - ShortString short_string; - size_t i = 0; - - auto length = UnicodeUtils::code_point_to_utf8(code_point, [&](auto byte) { - short_string.storage[i++] = static_cast(byte); + String string; + string.replace_with_new_short_string(UnicodeUtils::bytes_to_store_code_point_in_utf8(code_point), [&](Bytes buffer) { + size_t i = 0; + (void)UnicodeUtils::code_point_to_utf8(code_point, [&](auto byte) { + buffer[i++] = static_cast(byte); + }); }); - short_string.byte_count_and_short_string_flag = (length << 1) | SHORT_STRING_FLAG; - return String { short_string }; + return string; } // Creates a new String with a single code point repeated N times. diff --git a/AK/UnicodeUtils.h b/AK/UnicodeUtils.h index 691ad0fb6f4..44045507209 100644 --- a/AK/UnicodeUtils.h +++ b/AK/UnicodeUtils.h @@ -12,6 +12,19 @@ namespace AK::UnicodeUtils { +constexpr int bytes_to_store_code_point_in_utf8(u32 code_point) +{ + if (code_point <= 0x7f) + return 1; + if (code_point <= 0x7ff) + return 2; + if (code_point <= 0xffff) + return 3; + if (code_point <= 0x10ffff) + return 4; + return 0; +} + template [[nodiscard]] constexpr int code_point_to_utf8(u32 code_point, Callback callback) {