AK: Stop using ShortString in String::from_code_point

Refactor it to use StringBase::replace_with_new_short_string instead.
This commit is contained in:
Dan Klishch 2023-10-28 17:06:51 -04:00 committed by Andrew Kaster
parent dcd1fda9c8
commit 7506736869
2 changed files with 20 additions and 7 deletions

View file

@ -64,15 +64,15 @@ public:
{
VERIFY(is_unicode(code_point));
ShortString short_string;
String string;
string.replace_with_new_short_string(UnicodeUtils::bytes_to_store_code_point_in_utf8(code_point), [&](Bytes buffer) {
size_t i = 0;
auto length = UnicodeUtils::code_point_to_utf8(code_point, [&](auto byte) {
short_string.storage[i++] = static_cast<u8>(byte);
(void)UnicodeUtils::code_point_to_utf8(code_point, [&](auto byte) {
buffer[i++] = static_cast<u8>(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.

View file

@ -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<typename Callback>
[[nodiscard]] constexpr int code_point_to_utf8(u32 code_point, Callback callback)
{