diff --git a/AK/Utf16View.h b/AK/Utf16View.h index e5685971bc6..fadc5bd5047 100644 --- a/AK/Utf16View.h +++ b/AK/Utf16View.h @@ -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; diff --git a/Tests/AK/TestUtf16View.cpp b/Tests/AK/TestUtf16View.cpp index 08c2d28e238..1bcf606453f 100644 --- a/Tests/AK/TestUtf16View.cpp +++ b/Tests/AK/TestUtf16View.cpp @@ -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) { {