Skip to content

Commit 3b68370

Browse files
trflynn89linusg
authored andcommitted
LibJS+LibUnicode: Rename the generated compact_identifier to identifier
This field is currently used to store the StringView into the compact name/symbol in the format string. Units will need to store a similar field, so rename the field to be more generic, and extract the parser for it.
1 parent 6d34a0b commit 3b68370

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct NumberFormat : public Unicode::NumberFormat {
5757
StringIndexType zero_format_index { 0 };
5858
StringIndexType positive_format_index { 0 };
5959
StringIndexType negative_format_index { 0 };
60-
StringIndexType compact_identifier_index { 0 };
60+
StringIndexType identifier_index { 0 };
6161
};
6262

6363
struct NumberSystem {
@@ -90,6 +90,46 @@ struct UnicodeLocaleData {
9090
Vector<String> numeric_symbols;
9191
};
9292

93+
static String parse_identifier(String pattern, StringView replacement, UnicodeLocaleData& locale_data, NumberFormat& format)
94+
{
95+
static Utf8View whitespace { "\u0020\u00a0"sv };
96+
97+
Utf8View utf8_pattern { pattern };
98+
Optional<size_t> start_index;
99+
Optional<size_t> end_index;
100+
bool inside_replacement = false;
101+
102+
for (auto it = utf8_pattern.begin(); it != utf8_pattern.end(); ++it) {
103+
if (*it == '{') {
104+
if (start_index.has_value()) {
105+
end_index = utf8_pattern.byte_offset_of(it);
106+
break;
107+
}
108+
109+
inside_replacement = true;
110+
} else if (*it == '}') {
111+
inside_replacement = false;
112+
} else if (!inside_replacement && !start_index.has_value() && !whitespace.contains(*it)) {
113+
start_index = utf8_pattern.byte_offset_of(it);
114+
}
115+
}
116+
117+
if (!start_index.has_value())
118+
return pattern;
119+
end_index = end_index.value_or(pattern.length());
120+
121+
utf8_pattern = utf8_pattern.substring_view(*start_index, *end_index - *start_index);
122+
utf8_pattern = utf8_pattern.trim(whitespace);
123+
124+
auto identifier = utf8_pattern.as_string().replace("'.'"sv, "."sv);
125+
format.identifier_index = locale_data.unique_strings.ensure(move(identifier));
126+
127+
return String::formatted("{}{}{}",
128+
*start_index > 0 ? pattern.substring_view(0, *start_index) : ""sv,
129+
replacement,
130+
pattern.substring_view(*start_index + utf8_pattern.byte_length()));
131+
}
132+
93133
static void parse_number_pattern(Vector<String> patterns, UnicodeLocaleData& locale_data, NumberFormatType type, NumberFormat& format, NumberSystem* number_system_for_groupings = nullptr)
94134
{
95135
// https://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns
@@ -147,40 +187,8 @@ static void parse_number_pattern(Vector<String> patterns, UnicodeLocaleData& loc
147187
pattern = pattern.replace("0"sv, "{scientificExponent}"sv);
148188
}
149189

150-
if (type == NumberFormatType::Compact) {
151-
static Utf8View whitespace { "\u0020\u00a0"sv };
152-
153-
Utf8View utf8_pattern { pattern };
154-
Optional<size_t> start_compact_index;
155-
Optional<size_t> end_compact_index;
156-
bool inside_replacement = false;
157-
158-
for (auto it = utf8_pattern.begin(); it != utf8_pattern.end(); ++it) {
159-
if (*it == '{') {
160-
if (start_compact_index.has_value()) {
161-
end_compact_index = utf8_pattern.byte_offset_of(it);
162-
break;
163-
}
164-
165-
inside_replacement = true;
166-
} else if (*it == '}') {
167-
inside_replacement = false;
168-
} else if (!inside_replacement && !start_compact_index.has_value() && !whitespace.contains(*it)) {
169-
start_compact_index = utf8_pattern.byte_offset_of(it);
170-
}
171-
}
172-
173-
if (!start_compact_index.has_value())
174-
return pattern;
175-
176-
utf8_pattern = utf8_pattern.substring_view(*start_compact_index, end_compact_index.value_or(pattern.length()) - *start_compact_index);
177-
utf8_pattern = utf8_pattern.trim(whitespace);
178-
179-
auto identifier = utf8_pattern.as_string().replace("'.'"sv, "."sv);
180-
format.compact_identifier_index = locale_data.unique_strings.ensure(move(identifier));
181-
182-
pattern = pattern.replace(utf8_pattern.as_string(), "{compactIdentifier}");
183-
}
190+
if (type == NumberFormatType::Compact)
191+
return parse_identifier(move(pattern), "{compactIdentifier}"sv, locale_data, format);
184192

185193
return pattern;
186194
};
@@ -429,7 +437,7 @@ struct NumberFormat {
429437
number_format.zero_format = s_string_list[zero_format];
430438
number_format.positive_format = s_string_list[positive_format];
431439
number_format.negative_format = s_string_list[negative_format];
432-
number_format.compact_identifier = s_string_list[compact_identifier];
440+
number_format.identifier = s_string_list[identifier];
433441
434442
return number_format;
435443
}
@@ -440,7 +448,7 @@ struct NumberFormat {
440448
@string_index_type@ zero_format { 0 };
441449
@string_index_type@ positive_format { 0 };
442450
@string_index_type@ negative_format { 0 };
443-
@string_index_type@ compact_identifier { 0 };
451+
@string_index_type@ identifier { 0 };
444452
};
445453
446454
struct NumberSystem {
@@ -471,8 +479,8 @@ struct NumberSystem {
471479
generator.set("zero_format"sv, String::number(number_format.zero_format_index));
472480
generator.set("positive_format"sv, String::number(number_format.positive_format_index));
473481
generator.set("negative_format"sv, String::number(number_format.negative_format_index));
474-
generator.set("compact_identifier"sv, String::number(number_format.compact_identifier_index));
475-
generator.append("{ @magnitude@, @exponent@, @plurality@, @zero_format@, @positive_format@, @negative_format@, @compact_identifier@ },");
482+
generator.set("identifier"sv, String::number(number_format.identifier_index));
483+
generator.append("{ @magnitude@, @exponent@, @plurality@, @zero_format@, @positive_format@, @negative_format@, @identifier@ },");
476484
};
477485

478486
auto append_number_formats = [&](String name, auto const& number_formats) {

Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_for
966966
// Note: Our implementation combines "compactSymbol" and "compactName" into one field, "compactIdentifier".
967967

968968
// 1. Let compactSymbol be an ILD string representing exponent in short form, which may depend on x in languages having different plural forms. The implementation must be able to provide this string, or else the pattern would not have a "{compactSymbol}" placeholder.
969-
auto compact_identifier = number_format.compact_format().compact_identifier;
969+
auto compact_identifier = number_format.compact_format().identifier;
970970

971971
// 2. Append a new Record { [[Type]]: "compact", [[Value]]: compactSymbol } as the last element of result.
972972
result.append({ "compact"sv, compact_identifier });

Userland/Libraries/LibUnicode/Locale.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct NumberFormat {
122122
StringView zero_format {};
123123
StringView positive_format {};
124124
StringView negative_format {};
125-
StringView compact_identifier {};
125+
StringView identifier {};
126126
};
127127

128128
struct ListPatterns {

0 commit comments

Comments
 (0)