From 4a166a45ec9db910542893133f08cc7de4ec974f Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 30 Jul 2024 06:46:30 -0400 Subject: [PATCH] LibWeb: Avoid allocating UTF-16 strings only the UTF-16 length is needed --- Userland/Libraries/LibWeb/DOM/CharacterData.h | 5 ++--- Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.h b/Userland/Libraries/LibWeb/DOM/CharacterData.h index 431b45b655d..0598c8d88db 100644 --- a/Userland/Libraries/LibWeb/DOM/CharacterData.h +++ b/Userland/Libraries/LibWeb/DOM/CharacterData.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -30,9 +31,7 @@ public: unsigned length_in_utf16_code_units() const { - // FIXME: This is inefficient! - auto utf16_data = MUST(AK::utf8_to_utf16(m_data)); - return Utf16View { utf16_data }.length_in_code_units(); + return AK::utf16_code_unit_length_from_utf8(m_data); } WebIDL::ExceptionOr substring_data(size_t offset_in_utf16_code_units, size_t count_in_utf16_code_units) const; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp index bcbb56e4adc..71f100e2108 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp @@ -6,6 +6,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -189,9 +190,7 @@ String HTMLTextAreaElement::api_value() const u32 HTMLTextAreaElement::text_length() const { // The textLength IDL attribute must return the length of the element's API value. - // FIXME: This is inefficient! - auto utf16_data = MUST(AK::utf8_to_utf16(api_value())); - return Utf16View { utf16_data }.length_in_code_units(); + return AK::utf16_code_unit_length_from_utf8(api_value()); } // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity