mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 05:35:52 +03:00
AK+LibJS+LibRegex: Define an alias for UTF-16 string data storage
Instead of writing out "Vector<u16, 1>" everywhere, let's have a name for it.
This commit is contained in:
parent
39bda0073e
commit
425c168ded
Notes:
sideshowbarker
2024-07-17 02:21:14 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/425c168ded Pull-request: https://github.com/SerenityOS/serenity/pull/16895 Reviewed-by: https://github.com/linusg
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* 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<typename UtfViewType>
|
||||
static Vector<u16, 1> to_utf16_impl(UtfViewType const& view)
|
||||
static Utf16Data to_utf16_impl(UtfViewType const& view)
|
||||
requires(IsSame<UtfViewType, Utf8View> || IsSame<UtfViewType, Utf32View>)
|
||||
{
|
||||
Vector<u16, 1> utf16_data;
|
||||
Utf16Data utf16_data;
|
||||
utf16_data.ensure_capacity(view.length());
|
||||
|
||||
for (auto code_point : view)
|
||||
@ -33,22 +33,22 @@ requires(IsSame<UtfViewType, Utf8View> || IsSame<UtfViewType, Utf32View>)
|
||||
return utf16_data;
|
||||
}
|
||||
|
||||
Vector<u16, 1> utf8_to_utf16(StringView utf8_view)
|
||||
Utf16Data utf8_to_utf16(StringView utf8_view)
|
||||
{
|
||||
return to_utf16_impl(Utf8View { utf8_view });
|
||||
}
|
||||
|
||||
Vector<u16, 1> utf8_to_utf16(Utf8View const& utf8_view)
|
||||
Utf16Data utf8_to_utf16(Utf8View const& utf8_view)
|
||||
{
|
||||
return to_utf16_impl(utf8_view);
|
||||
}
|
||||
|
||||
Vector<u16, 1> 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<u16, 1>& string, u32 code_point)
|
||||
void code_point_to_utf16(Utf16Data& string, u32 code_point)
|
||||
{
|
||||
VERIFY(is_unicode(code_point));
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
@ -16,10 +16,12 @@
|
||||
|
||||
namespace AK {
|
||||
|
||||
Vector<u16, 1> utf8_to_utf16(StringView);
|
||||
Vector<u16, 1> utf8_to_utf16(Utf8View const&);
|
||||
Vector<u16, 1> utf32_to_utf16(Utf32View const&);
|
||||
void code_point_to_utf16(Vector<u16, 1>&, u32);
|
||||
using Utf16Data = Vector<u16, 1>;
|
||||
|
||||
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<AK::Utf16View> : Formatter<FormatString> {
|
||||
};
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::Utf16Data;
|
||||
using AK::Utf16View;
|
||||
#endif
|
||||
|
@ -1233,7 +1233,7 @@ ThrowCompletionOr<DeprecatedString> get_substitution(VM& vm, Utf16View const& ma
|
||||
auto replace_string = TRY(replacement_template.to_utf16_string(vm));
|
||||
auto replace_view = replace_string.view();
|
||||
|
||||
Vector<u16, 1> result;
|
||||
Utf16Data result;
|
||||
|
||||
for (size_t i = 0; i < replace_view.length_in_code_units(); ++i) {
|
||||
u16 curr = replace_view.code_unit_at(i);
|
||||
|
@ -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<u16, 1> 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());
|
||||
|
@ -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<u16, 1> 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<u16, 1> 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) {
|
||||
|
@ -518,7 +518,7 @@ static ThrowCompletionOr<Value> pad_string(VM& vm, Utf16String string, PadPlacem
|
||||
if (max_length <= string_length)
|
||||
return PrimitiveString::create(vm, move(string));
|
||||
|
||||
Utf16String fill_string(Vector<u16, 1> { 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())
|
||||
|
@ -1,11 +1,10 @@
|
||||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Utf16View.h>
|
||||
#include <LibJS/Runtime/Utf16String.h>
|
||||
|
||||
namespace JS {
|
||||
@ -17,7 +16,7 @@ static NonnullRefPtr<Utf16StringImpl> the_empty_utf16_string()
|
||||
return empty_string;
|
||||
}
|
||||
|
||||
Utf16StringImpl::Utf16StringImpl(Vector<u16, 1> string)
|
||||
Utf16StringImpl::Utf16StringImpl(Utf16Data string)
|
||||
: m_string(move(string))
|
||||
{
|
||||
}
|
||||
@ -27,7 +26,7 @@ NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create()
|
||||
return adopt_ref(*new Utf16StringImpl());
|
||||
}
|
||||
|
||||
NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(Vector<u16, 1> string)
|
||||
NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(Utf16Data string)
|
||||
{
|
||||
return adopt_ref(*new Utf16StringImpl(move(string)));
|
||||
}
|
||||
@ -39,13 +38,13 @@ NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(StringView string)
|
||||
|
||||
NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(Utf16View const& view)
|
||||
{
|
||||
Vector<u16, 1> 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<u16, 1> const& Utf16StringImpl::string() const
|
||||
Utf16Data const& Utf16StringImpl::string() const
|
||||
{
|
||||
return m_string;
|
||||
}
|
||||
@ -62,7 +61,7 @@ Utf16String::Utf16String()
|
||||
{
|
||||
}
|
||||
|
||||
Utf16String::Utf16String(Vector<u16, 1> string)
|
||||
Utf16String::Utf16String(Utf16Data string)
|
||||
: m_string(Detail::Utf16StringImpl::create(move(string)))
|
||||
{
|
||||
}
|
||||
@ -77,7 +76,7 @@ Utf16String::Utf16String(Utf16View const& string)
|
||||
{
|
||||
}
|
||||
|
||||
Vector<u16, 1> const& Utf16String::string() const
|
||||
Utf16Data const& Utf16String::string() const
|
||||
{
|
||||
return m_string->string();
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
@ -10,6 +10,7 @@
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Utf16View.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
namespace JS {
|
||||
@ -20,18 +21,18 @@ public:
|
||||
~Utf16StringImpl() = default;
|
||||
|
||||
static NonnullRefPtr<Utf16StringImpl> create();
|
||||
static NonnullRefPtr<Utf16StringImpl> create(Vector<u16, 1>);
|
||||
static NonnullRefPtr<Utf16StringImpl> create(Utf16Data);
|
||||
static NonnullRefPtr<Utf16StringImpl> create(StringView);
|
||||
static NonnullRefPtr<Utf16StringImpl> create(Utf16View const&);
|
||||
|
||||
Vector<u16, 1> const& string() const;
|
||||
Utf16Data const& string() const;
|
||||
Utf16View view() const;
|
||||
|
||||
private:
|
||||
Utf16StringImpl() = default;
|
||||
explicit Utf16StringImpl(Vector<u16, 1> string);
|
||||
explicit Utf16StringImpl(Utf16Data string);
|
||||
|
||||
Vector<u16, 1> m_string;
|
||||
Utf16Data m_string;
|
||||
};
|
||||
|
||||
}
|
||||
@ -39,11 +40,11 @@ private:
|
||||
class Utf16String {
|
||||
public:
|
||||
Utf16String();
|
||||
explicit Utf16String(Vector<u16, 1>);
|
||||
explicit Utf16String(Utf16Data);
|
||||
explicit Utf16String(StringView);
|
||||
explicit Utf16String(Utf16View const&);
|
||||
|
||||
Vector<u16, 1> 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;
|
||||
|
@ -517,7 +517,7 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
||||
|
||||
Optional<DeprecatedString> str;
|
||||
Vector<u16, 1> utf16;
|
||||
Utf16Data utf16;
|
||||
Vector<u32> data;
|
||||
data.ensure_capacity(length);
|
||||
for (size_t i = offset; i < offset + length; ++i)
|
||||
|
@ -266,7 +266,7 @@ public:
|
||||
return view;
|
||||
}
|
||||
|
||||
RegexStringView construct_as_same(Span<u32> data, Optional<DeprecatedString>& optional_string_storage, Vector<u16, 1>& optional_utf16_storage) const
|
||||
RegexStringView construct_as_same(Span<u32> data, Optional<DeprecatedString>& optional_string_storage, Utf16Data& optional_utf16_storage) const
|
||||
{
|
||||
auto view = m_view.visit(
|
||||
[&]<typename T>(T const&) {
|
||||
|
Loading…
Reference in New Issue
Block a user