From 425c168dedf0138cb5d5a96c1d5cccce80afe030 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 6 Jan 2023 11:00:24 -0500 Subject: [PATCH] AK+LibJS+LibRegex: Define an alias for UTF-16 string data storage Instead of writing out "Vector" everywhere, let's have a name for it. --- AK/Utf16View.cpp | 14 +++++++------- AK/Utf16View.h | 13 ++++++++----- .../LibJS/Runtime/AbstractOperations.cpp | 2 +- .../Libraries/LibJS/Runtime/PrimitiveString.cpp | 2 +- .../Libraries/LibJS/Runtime/StringConstructor.cpp | 4 ++-- .../Libraries/LibJS/Runtime/StringPrototype.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Utf16String.cpp | 15 +++++++-------- Userland/Libraries/LibJS/Runtime/Utf16String.h | 15 ++++++++------- Userland/Libraries/LibRegex/RegexByteCode.cpp | 2 +- Userland/Libraries/LibRegex/RegexMatch.h | 2 +- 10 files changed, 37 insertions(+), 34 deletions(-) diff --git a/AK/Utf16View.cpp b/AK/Utf16View.cpp index 747417a61ff..1dc0b55d4b9 100644 --- a/AK/Utf16View.cpp +++ b/AK/Utf16View.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Tim Flynn + * Copyright (c) 2021-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -21,10 +21,10 @@ static constexpr u32 replacement_code_point = 0xfffd; static constexpr u32 first_supplementary_plane_code_point = 0x10000; template -static Vector to_utf16_impl(UtfViewType const& view) +static Utf16Data to_utf16_impl(UtfViewType const& view) requires(IsSame || IsSame) { - Vector utf16_data; + Utf16Data utf16_data; utf16_data.ensure_capacity(view.length()); for (auto code_point : view) @@ -33,22 +33,22 @@ requires(IsSame || IsSame) return utf16_data; } -Vector utf8_to_utf16(StringView utf8_view) +Utf16Data utf8_to_utf16(StringView utf8_view) { return to_utf16_impl(Utf8View { utf8_view }); } -Vector utf8_to_utf16(Utf8View const& utf8_view) +Utf16Data utf8_to_utf16(Utf8View const& utf8_view) { return to_utf16_impl(utf8_view); } -Vector utf32_to_utf16(Utf32View const& utf32_view) +Utf16Data utf32_to_utf16(Utf32View const& utf32_view) { return to_utf16_impl(utf32_view); } -void code_point_to_utf16(Vector& string, u32 code_point) +void code_point_to_utf16(Utf16Data& string, u32 code_point) { VERIFY(is_unicode(code_point)); diff --git a/AK/Utf16View.h b/AK/Utf16View.h index 1c8c0d76dbc..cb43660b010 100644 --- a/AK/Utf16View.h +++ b/AK/Utf16View.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Tim Flynn + * Copyright (c) 2021-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -16,10 +16,12 @@ namespace AK { -Vector utf8_to_utf16(StringView); -Vector utf8_to_utf16(Utf8View const&); -Vector utf32_to_utf16(Utf32View const&); -void code_point_to_utf16(Vector&, u32); +using Utf16Data = Vector; + +Utf16Data utf8_to_utf16(StringView); +Utf16Data utf8_to_utf16(Utf8View const&); +Utf16Data utf32_to_utf16(Utf32View const&); +void code_point_to_utf16(Utf16Data&, u32); class Utf16View; @@ -126,5 +128,6 @@ struct AK::Formatter : Formatter { }; #if USING_AK_GLOBALLY +using AK::Utf16Data; using AK::Utf16View; #endif diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 4db6c8529b4..ddf89aee187 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -1233,7 +1233,7 @@ ThrowCompletionOr get_substitution(VM& vm, Utf16View const& ma auto replace_string = TRY(replacement_template.to_utf16_string(vm)); auto replace_view = replace_string.view(); - Vector result; + Utf16Data result; for (size_t i = 0; i < replace_view.length_in_code_units(); ++i) { u16 curr = replace_view.code_unit_at(i); diff --git a/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp b/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp index cecc841b6e9..c29c4b0cc8c 100644 --- a/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp +++ b/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp @@ -180,7 +180,7 @@ void PrimitiveString::resolve_rope_if_needed() const auto const& lhs_string = m_lhs->utf16_string(); auto const& rhs_string = m_rhs->utf16_string(); - Vector combined; + Utf16Data combined; combined.ensure_capacity(lhs_string.length_in_code_units() + rhs_string.length_in_code_units()); combined.extend(lhs_string.string()); combined.extend(rhs_string.string()); diff --git a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp index 9fd6b22f3c1..ca6e5130ea5 100644 --- a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -100,7 +100,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw) // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code) { - Vector string; + Utf16Data string; string.ensure_capacity(vm.argument_count()); for (size_t i = 0; i < vm.argument_count(); ++i) @@ -112,7 +112,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code) // 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point) { - Vector string; + Utf16Data string; string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff. for (size_t i = 0; i < vm.argument_count(); ++i) { diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 9f2ccd6a2cb..c43706bbb7e 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -518,7 +518,7 @@ static ThrowCompletionOr pad_string(VM& vm, Utf16String string, PadPlacem if (max_length <= string_length) return PrimitiveString::create(vm, move(string)); - Utf16String fill_string(Vector { 0x20 }); + Utf16String fill_string(Utf16Data { 0x20 }); if (!vm.argument(1).is_undefined()) { fill_string = TRY(vm.argument(1).to_utf16_string(vm)); if (fill_string.is_empty()) diff --git a/Userland/Libraries/LibJS/Runtime/Utf16String.cpp b/Userland/Libraries/LibJS/Runtime/Utf16String.cpp index b9aacbaa5e1..821dc88db46 100644 --- a/Userland/Libraries/LibJS/Runtime/Utf16String.cpp +++ b/Userland/Libraries/LibJS/Runtime/Utf16String.cpp @@ -1,11 +1,10 @@ /* - * Copyright (c) 2021, Tim Flynn + * Copyright (c) 2021-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include -#include #include namespace JS { @@ -17,7 +16,7 @@ static NonnullRefPtr the_empty_utf16_string() return empty_string; } -Utf16StringImpl::Utf16StringImpl(Vector string) +Utf16StringImpl::Utf16StringImpl(Utf16Data string) : m_string(move(string)) { } @@ -27,7 +26,7 @@ NonnullRefPtr Utf16StringImpl::create() return adopt_ref(*new Utf16StringImpl()); } -NonnullRefPtr Utf16StringImpl::create(Vector string) +NonnullRefPtr Utf16StringImpl::create(Utf16Data string) { return adopt_ref(*new Utf16StringImpl(move(string))); } @@ -39,13 +38,13 @@ NonnullRefPtr Utf16StringImpl::create(StringView string) NonnullRefPtr Utf16StringImpl::create(Utf16View const& view) { - Vector string; + Utf16Data string; string.ensure_capacity(view.length_in_code_units()); string.append(view.data(), view.length_in_code_units()); return create(move(string)); } -Vector const& Utf16StringImpl::string() const +Utf16Data const& Utf16StringImpl::string() const { return m_string; } @@ -62,7 +61,7 @@ Utf16String::Utf16String() { } -Utf16String::Utf16String(Vector string) +Utf16String::Utf16String(Utf16Data string) : m_string(Detail::Utf16StringImpl::create(move(string))) { } @@ -77,7 +76,7 @@ Utf16String::Utf16String(Utf16View const& string) { } -Vector const& Utf16String::string() const +Utf16Data const& Utf16String::string() const { return m_string->string(); } diff --git a/Userland/Libraries/LibJS/Runtime/Utf16String.h b/Userland/Libraries/LibJS/Runtime/Utf16String.h index 32ffa733c8c..fdcc0142527 100644 --- a/Userland/Libraries/LibJS/Runtime/Utf16String.h +++ b/Userland/Libraries/LibJS/Runtime/Utf16String.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Tim Flynn + * Copyright (c) 2021-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace JS { @@ -20,18 +21,18 @@ public: ~Utf16StringImpl() = default; static NonnullRefPtr create(); - static NonnullRefPtr create(Vector); + static NonnullRefPtr create(Utf16Data); static NonnullRefPtr create(StringView); static NonnullRefPtr create(Utf16View const&); - Vector const& string() const; + Utf16Data const& string() const; Utf16View view() const; private: Utf16StringImpl() = default; - explicit Utf16StringImpl(Vector string); + explicit Utf16StringImpl(Utf16Data string); - Vector m_string; + Utf16Data m_string; }; } @@ -39,11 +40,11 @@ private: class Utf16String { public: Utf16String(); - explicit Utf16String(Vector); + explicit Utf16String(Utf16Data); explicit Utf16String(StringView); explicit Utf16String(Utf16View const&); - Vector const& string() const; + Utf16Data const& string() const; Utf16View view() const; Utf16View substring_view(size_t code_unit_offset, size_t code_unit_length) const; Utf16View substring_view(size_t code_unit_offset) const; diff --git a/Userland/Libraries/LibRegex/RegexByteCode.cpp b/Userland/Libraries/LibRegex/RegexByteCode.cpp index ede861eecbe..07e22f5ae8c 100644 --- a/Userland/Libraries/LibRegex/RegexByteCode.cpp +++ b/Userland/Libraries/LibRegex/RegexByteCode.cpp @@ -517,7 +517,7 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M return ExecutionResult::Failed_ExecuteLowPrioForks; Optional str; - Vector utf16; + Utf16Data utf16; Vector data; data.ensure_capacity(length); for (size_t i = offset; i < offset + length; ++i) diff --git a/Userland/Libraries/LibRegex/RegexMatch.h b/Userland/Libraries/LibRegex/RegexMatch.h index 619fdd3c845..4f36cf6fb7a 100644 --- a/Userland/Libraries/LibRegex/RegexMatch.h +++ b/Userland/Libraries/LibRegex/RegexMatch.h @@ -266,7 +266,7 @@ public: return view; } - RegexStringView construct_as_same(Span data, Optional& optional_string_storage, Vector& optional_utf16_storage) const + RegexStringView construct_as_same(Span data, Optional& optional_string_storage, Utf16Data& optional_utf16_storage) const { auto view = m_view.visit( [&](T const&) {