Browse Source

LibJS: Change Intl's PatternPartition record to hold a String value

It was previously holding a StringView, which was either a view into a
LibUnicode-generated string or a string passed from the user.

Intl.NumberFormat will need this record to hold internally-created
strings, so a StringView will not suffice (the way the steps are laid
out, that view will ultimately end up dangling).

This shouldn't be too wasteful since the StringView it was holding was
converted to a String eventually anyways.
Timothy Flynn 3 năm trước cách đây
mục cha
commit
0469006263

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h

@@ -31,7 +31,7 @@ struct LocaleResult {
 
 
 struct PatternPartition {
 struct PatternPartition {
     StringView type;
     StringView type;
-    StringView value;
+    String value;
 };
 };
 
 
 Optional<Unicode::LocaleID> is_structurally_valid_language_tag(StringView locale);
 Optional<Unicode::LocaleID> is_structurally_valid_language_tag(StringView locale);

+ 6 - 6
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp

@@ -83,14 +83,14 @@ Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables plac
     Vector<PatternPartition> result {};
     Vector<PatternPartition> result {};
 
 
     // 3. For each Record { [[Type]], [[Value]] } patternPart of patternParts, do
     // 3. For each Record { [[Type]], [[Value]] } patternPart of patternParts, do
-    for (auto const& pattern_part : pattern_parts) {
+    for (auto& pattern_part : pattern_parts) {
         // a. Let part be patternPart.[[Type]].
         // a. Let part be patternPart.[[Type]].
         auto part = pattern_part.type;
         auto part = pattern_part.type;
 
 
         // b. If part is "literal", then
         // b. If part is "literal", then
         if (part == "literal"sv) {
         if (part == "literal"sv) {
             // i. Append Record { [[Type]]: "literal", [[Value]]: patternPart.[[Value]] } to result.
             // i. Append Record { [[Type]]: "literal", [[Value]]: patternPart.[[Value]] } to result.
-            result.append({ part, pattern_part.value });
+            result.append({ part, move(pattern_part.value) });
         }
         }
         // c. Else,
         // c. Else,
         else {
         else {
@@ -219,9 +219,9 @@ String format_list(ListFormat const& list_format, Vector<String> const& list)
     StringBuilder result;
     StringBuilder result;
 
 
     // 3. For each Record { [[Type]], [[Value]] } part in parts, do
     // 3. For each Record { [[Type]], [[Value]] } part in parts, do
-    for (auto const& part : parts) {
+    for (auto& part : parts) {
         // a. Set result to the string-concatenation of result and part.[[Value]].
         // a. Set result to the string-concatenation of result and part.[[Value]].
-        result.append(part.value);
+        result.append(move(part.value));
     }
     }
 
 
     // 4. Return result.
     // 4. Return result.
@@ -243,7 +243,7 @@ Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_
     size_t n = 0;
     size_t n = 0;
 
 
     // 4. For each Record { [[Type]], [[Value]] } part in parts, do
     // 4. For each Record { [[Type]], [[Value]] } part in parts, do
-    for (auto const& part : parts) {
+    for (auto& part : parts) {
         // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
         // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
         auto* object = Object::create(global_object, global_object.object_prototype());
         auto* object = Object::create(global_object, global_object.object_prototype());
 
 
@@ -251,7 +251,7 @@ Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_
         MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
         MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
 
 
         // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
         // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
-        MUST(object->create_data_property_or_throw(vm.names.value, js_string(vm, part.value)));
+        MUST(object->create_data_property_or_throw(vm.names.value, js_string(vm, move(part.value))));
 
 
         // d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
         // d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
         MUST(result->create_data_property_or_throw(n, object));
         MUST(result->create_data_property_or_throw(n, object));