mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-10-03 16:48:40 +03:00
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:
parent
dcd1fda9c8
commit
7506736869
Notes:
sideshowbarker
2024-07-17 03:35:24 +09:00
Author: https://github.com/DanShaders Commit: https://github.com/SerenityOS/serenity/commit/7506736869 Pull-request: https://github.com/SerenityOS/serenity/pull/21661 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/kleinesfilmroellchen Reviewed-by: https://github.com/trflynn89
14
AK/String.h
14
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<u8>(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<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.
|
||||
|
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user