From b1d19b5917e42022669403b6983215e2680b4067 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Mon, 31 Jan 2022 18:41:44 +0200 Subject: [PATCH] LibJS: Convert Intl.Segmenter, Segments and SegmentIterator to Utf16 This is a precursor to implementing the FindBoundary AO, which operates on Utf16 code unit indexes. --- .../Libraries/LibJS/Runtime/Intl/SegmentIterator.cpp | 6 +++--- .../Libraries/LibJS/Runtime/Intl/SegmentIterator.h | 9 +++++---- .../LibJS/Runtime/Intl/SegmenterPrototype.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Intl/Segments.cpp | 4 ++-- Userland/Libraries/LibJS/Runtime/Intl/Segments.h | 10 +++++----- .../Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.cpp b/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.cpp index 4c3be7e6de3..2d848e8adbe 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.cpp @@ -10,7 +10,7 @@ namespace JS::Intl { // 18.6.1 CreateSegmentIterator ( segmenter, string ), https://tc39.es/ecma402/#sec-createsegmentsobject -SegmentIterator* SegmentIterator::create(GlobalObject& global_object, Segmenter& segmenter, String string) +SegmentIterator* SegmentIterator::create(GlobalObject& global_object, Segmenter& segmenter, Utf16View const& string) { // 1. Let internalSlotsList be « [[IteratingSegmenter]], [[IteratedString]], [[IteratedStringNextSegmentCodeUnitIndex]] ». // 2. Let iterator be ! OrdinaryObjectCreate(%SegmentIteratorPrototype%, internalSlotsList). @@ -22,10 +22,10 @@ SegmentIterator* SegmentIterator::create(GlobalObject& global_object, Segmenter& } // 18.6 Segment Iterator Objects, https://tc39.es/ecma402/#sec-segment-iterator-objects -SegmentIterator::SegmentIterator(GlobalObject& global_object, Segmenter& segmenter, String string) +SegmentIterator::SegmentIterator(GlobalObject& global_object, Segmenter& segmenter, Utf16View const& string) : Object(*global_object.intl_segment_iterator_prototype()) , m_iterating_segmenter(segmenter) - , m_iterated_string(move(string)) + , m_iterated_string(string) { } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.h b/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.h index 30cd05488b2..953141b352c 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include @@ -15,20 +16,20 @@ class SegmentIterator final : public Object { JS_OBJECT(SegmentIterator, Object); public: - static SegmentIterator* create(GlobalObject&, Segmenter&, String); + static SegmentIterator* create(GlobalObject&, Segmenter&, Utf16View const&); - SegmentIterator(GlobalObject&, Segmenter&, String); + SegmentIterator(GlobalObject&, Segmenter&, Utf16View const&); virtual ~SegmentIterator() override = default; Segmenter const& iterating_segmenter() const { return m_iterating_segmenter; } - String const& iterated_string() const { return m_iterated_string; } + Utf16View const& iterated_string() const { return m_iterated_string; } size_t iterated_string_next_segment_code_unit_index() const { return m_iterated_string_next_segment_code_unit_index; } private: virtual void visit_edges(Cell::Visitor&) override; Segmenter& m_iterating_segmenter; // [[IteratingSegmenter]] - String m_iterated_string; // [[IteratedString]] + Utf16View m_iterated_string; // [[IteratedString]] size_t m_iterated_string_next_segment_code_unit_index { 0 }; // [[IteratedStringNextSegmentCodeUnitIndex]] }; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp index 6b1d3ffcaef..2dd67d0cb7d 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp @@ -61,7 +61,7 @@ JS_DEFINE_NATIVE_FUNCTION(SegmenterPrototype::segment) auto* segmenter = TRY(typed_this_object(global_object)); // 3. Let string be ? ToString(string). - auto string = TRY(vm.argument(0).to_string(global_object)); + auto string = TRY(vm.argument(0).to_utf16_string(global_object)); // 4. Return ! CreateSegmentsObject(segmenter, string). return Segments::create(global_object, *segmenter, move(string)); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Segments.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Segments.cpp index 31907e1edb4..dd53cc3d275 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Segments.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/Segments.cpp @@ -11,7 +11,7 @@ namespace JS::Intl { // 18.5.1 CreateSegmentsObject ( segmenter, string ), https://tc39.es/ecma402/#sec-createsegmentsobject -Segments* Segments::create(GlobalObject& global_object, Segmenter& segmenter, String string) +Segments* Segments::create(GlobalObject& global_object, Segmenter& segmenter, Utf16String string) { // 1. Let internalSlotsList be « [[SegmentsSegmenter]], [[SegmentsString]] ». // 2. Let segments be ! OrdinaryObjectCreate(%SegmentsPrototype%, internalSlotsList). @@ -22,7 +22,7 @@ Segments* Segments::create(GlobalObject& global_object, Segmenter& segmenter, St } // 18.5 Segments Objects, https://tc39.es/ecma402/#sec-segments-objects -Segments::Segments(GlobalObject& global_object, Segmenter& segmenter, String string) +Segments::Segments(GlobalObject& global_object, Segmenter& segmenter, Utf16String string) : Object(*global_object.intl_segments_prototype()) , m_segments_segmenter(segmenter) , m_segments_string(move(string)) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Segments.h b/Userland/Libraries/LibJS/Runtime/Intl/Segments.h index 26e14244955..970bcdaba06 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Segments.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/Segments.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include #include @@ -16,20 +16,20 @@ class Segments final : public Object { JS_OBJECT(Segments, Object); public: - static Segments* create(GlobalObject&, Segmenter&, String); + static Segments* create(GlobalObject&, Segmenter&, Utf16String); - Segments(GlobalObject&, Segmenter&, String); + Segments(GlobalObject&, Segmenter&, Utf16String); virtual ~Segments() override = default; Segmenter& segments_segmenter() const { return m_segments_segmenter; } - String const& segments_string() const { return m_segments_string; } + Utf16View segments_string() const { return m_segments_string.view(); } private: virtual void visit_edges(Cell::Visitor&) override; Segmenter& m_segments_segmenter; // [[SegmentsSegmenter]] - String m_segments_string; // [[SegmentsString]] + Utf16String m_segments_string; // [[SegmentsString]] }; } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp index 3141b65ea1c..6d5870ff7f9 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp @@ -38,7 +38,7 @@ JS_DEFINE_NATIVE_FUNCTION(SegmentsPrototype::symbol_iterator) auto& segmenter = segments->segments_segmenter(); // 4. Let string be segments.[[SegmentsString]]. - auto& string = segments->segments_string(); + auto string = segments->segments_string(); // 5. Return ! CreateSegmentIterator(segmenter, string). return SegmentIterator::create(global_object, segmenter, string);