AK: Remove null state from Utf16View

The default constructor now initializes to an empty ASCII string rather
than a null pointer.

Also add a VERIFY in the utf16 constructor to assert the pointer is
non null, and remove the now unneeded is_null() method.
This commit is contained in:
Shannon Booth
2026-03-31 01:54:45 +02:00
committed by Sam Atkins
parent a066ee720e
commit 7ab9454f51
Notes: github-actions[bot] 2026-03-31 12:50:11 +00:00
2 changed files with 14 additions and 8 deletions

View File

@@ -151,13 +151,17 @@ class Utf16View {
public:
using Iterator = Utf16CodePointIterator;
Utf16View() = default;
constexpr Utf16View()
: m_string { .ascii = "" }
{
}
~Utf16View() = default;
constexpr Utf16View(char16_t const* string, size_t length_in_code_units)
: m_string { .utf16 = string }
, m_length_in_code_units(length_in_code_units)
{
VERIFY(string != nullptr);
m_length_in_code_units |= 1uz << Detail::UTF16_FLAG;
}
@@ -326,13 +330,6 @@ public:
return string_hash(m_string.utf16, length_in_code_units());
}
[[nodiscard]] constexpr bool is_null() const
{
if (has_ascii_storage())
return m_string.ascii == nullptr;
return m_string.utf16 == nullptr;
}
[[nodiscard]] constexpr bool is_empty() const { return length_in_code_units() == 0; }
[[nodiscard]] bool is_ascii() const;

View File

@@ -99,6 +99,15 @@ TEST_CASE(null_view)
FAIL("Iterating a null UTF-16 string should not produce any values");
}
TEST_CASE(default_empty_view_is_empty_ascii_string)
{
Utf16View view;
EXPECT(view.has_ascii_storage());
EXPECT_EQ(view.ascii_span().data(), "");
EXPECT_EQ(view.length_in_code_units(), 0zu);
EXPECT_EQ(view, ""sv);
}
TEST_CASE(utf16_literal)
{
{