LibJS: Remove ByteString internals from PrimitiveString

PrimitiveString is now internally either UTF-8, UTF-16, or both.
We no longer convert them to/from ByteString anywhere, nor does VM have
a ByteString cache.
This commit is contained in:
Andreas Kling 2025-03-28 13:55:39 +00:00 committed by Tim Flynn
commit c71772126f
Notes: github-actions[bot] 2025-03-28 16:32:54 +00:00
14 changed files with 49 additions and 118 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2025, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -33,11 +33,6 @@ PrimitiveString::PrimitiveString(String string)
{
}
PrimitiveString::PrimitiveString(ByteString string)
: m_byte_string(move(string))
{
}
PrimitiveString::PrimitiveString(Utf16String string)
: m_utf16_string(move(string))
{
@ -49,8 +44,6 @@ PrimitiveString::~PrimitiveString()
vm().string_cache().remove(*m_utf8_string);
if (has_utf16_string())
vm().utf16_string_cache().remove(*m_utf16_string);
if (has_byte_string())
vm().byte_string_cache().remove(*m_byte_string);
}
void PrimitiveString::visit_edges(Cell::Visitor& visitor)
@ -73,8 +66,6 @@ bool PrimitiveString::is_empty() const
return m_utf16_string->is_empty();
if (has_utf8_string())
return m_utf8_string->is_empty();
if (has_byte_string())
return m_byte_string->is_empty();
VERIFY_NOT_REACHED();
}
@ -83,12 +74,8 @@ String PrimitiveString::utf8_string() const
resolve_rope_if_needed(EncodingPreference::UTF8);
if (!has_utf8_string()) {
if (has_byte_string())
m_utf8_string = MUST(String::from_byte_string(*m_byte_string));
else if (has_utf16_string())
m_utf8_string = m_utf16_string->to_utf8();
else
VERIFY_NOT_REACHED();
VERIFY(has_utf16_string());
m_utf8_string = m_utf16_string->to_utf8();
}
return *m_utf8_string;
@ -100,33 +87,13 @@ StringView PrimitiveString::utf8_string_view() const
return m_utf8_string->bytes_as_string_view();
}
ByteString PrimitiveString::byte_string() const
{
resolve_rope_if_needed(EncodingPreference::UTF8);
if (!has_byte_string()) {
if (has_utf8_string())
m_byte_string = m_utf8_string->to_byte_string();
else if (has_utf16_string())
m_byte_string = m_utf16_string->to_byte_string();
else
VERIFY_NOT_REACHED();
}
return *m_byte_string;
}
Utf16String PrimitiveString::utf16_string() const
{
resolve_rope_if_needed(EncodingPreference::UTF16);
if (!has_utf16_string()) {
if (has_utf8_string()) {
m_utf16_string = Utf16String::create(m_utf8_string->bytes_as_string_view());
} else {
VERIFY(has_byte_string());
m_utf16_string = Utf16String::create(*m_byte_string);
}
VERIFY(has_utf8_string());
m_utf16_string = Utf16String::create(m_utf8_string->bytes_as_string_view());
}
return *m_utf16_string;
@ -208,27 +175,6 @@ GC::Ref<PrimitiveString> PrimitiveString::create(VM& vm, StringView string)
return create(vm, String::from_utf8(string).release_value());
}
GC::Ref<PrimitiveString> PrimitiveString::create(VM& vm, ByteString string)
{
if (string.is_empty())
return vm.empty_string();
if (string.length() == 1) {
auto ch = static_cast<u8>(string.characters()[0]);
if (is_ascii(ch))
return vm.single_ascii_character_string(ch);
}
auto& string_cache = vm.byte_string_cache();
auto it = string_cache.find(string);
if (it == string_cache.end()) {
auto new_string = vm.heap().allocate<PrimitiveString>(string);
string_cache.set(move(string), new_string);
return *new_string;
}
return *it->value;
}
GC::Ref<PrimitiveString> PrimitiveString::create(VM& vm, PrimitiveString& lhs, PrimitiveString& rhs)
{
// We're here to concatenate two strings into a new rope string.