From 5cf87dcfdccd46f3163f623908aaefa2c9a4fa62 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Thu, 15 May 2025 17:56:33 +1200 Subject: [PATCH] AK: Add a Utf16View::is_code_unit_less_than helper This seems like the natural place to put this since it is specific to UTF-16. --- AK/Utf16View.cpp | 16 ++++++++++++++++ AK/Utf16View.h | 1 + 2 files changed, 17 insertions(+) diff --git a/AK/Utf16View.cpp b/AK/Utf16View.cpp index 232dde3e063..ea2234b9ff9 100644 --- a/AK/Utf16View.cpp +++ b/AK/Utf16View.cpp @@ -300,6 +300,22 @@ bool Utf16View::starts_with(Utf16View const& needle) const return true; } +// https://infra.spec.whatwg.org/#code-unit-less-than +bool Utf16View::is_code_unit_less_than(Utf16View const& other) const +{ + auto a = m_code_units; + auto b = other.m_code_units; + + auto common_length = min(a.size(), b.size()); + + for (size_t position = 0; position < common_length; ++position) { + if (a[position] != b[position]) + return a[position] < b[position]; + } + + return a.size() < b.size(); +} + bool Utf16View::validate() const { return simdutf::validate_utf16(char_data(), length_in_code_units()); diff --git a/AK/Utf16View.h b/AK/Utf16View.h index 9be66ced810..26d80ee4098 100644 --- a/AK/Utf16View.h +++ b/AK/Utf16View.h @@ -135,6 +135,7 @@ public: Utf16View unicode_substring_view(size_t code_point_offset) const { return unicode_substring_view(code_point_offset, length_in_code_points() - code_point_offset); } bool starts_with(Utf16View const&) const; + bool is_code_unit_less_than(Utf16View const& other) const; bool validate() const; bool validate(size_t& valid_code_units) const;