mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 09:21:57 -05: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
2 changed files with 20 additions and 7 deletions
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 a new issue