Commit 0690a6bd authored by Frank Tang's avatar Frank Tang Committed by V8 LUCI CQ

Call uloc_toUnicodeLocaleType to convert key

Add tests for Intl Locale Info API to ensure the return items fit the
type definition in UTS35

Bug: v8:11887
Change-Id: Ie92d80518909df9472ffd887800832a656807b5c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2964597Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#75190}
parent 6169cbf5
......@@ -460,22 +460,23 @@ MaybeHandle<JSLocale> JSLocale::Minimize(Isolate* isolate,
return Construct(isolate, result);
}
MaybeHandle<JSArray> ToJSArray(
Isolate* isolate, icu::StringEnumeration* enumeration,
const std::map<std::string, std::string>& substitutions, bool may_remove) {
MaybeHandle<JSArray> ToJSArray(Isolate* isolate, const char* unicode_key,
icu::StringEnumeration* enumeration,
const std::set<std::string>& removes) {
UErrorCode status = U_ZERO_ERROR;
Factory* factory = isolate->factory();
int32_t count = 0;
if (may_remove) {
if (!removes.empty()) {
// If we may remove items, then we need to go one pass first to count how
// many items we will insert before we allocate the fixed array.
for (const char* item = enumeration->next(nullptr, status);
U_SUCCESS(status) && item != nullptr;
item = enumeration->next(nullptr, status)) {
auto mapped = substitutions.find(item);
if ((mapped == substitutions.end()) ||
(*(mapped->second.c_str()) != '\0')) {
if (unicode_key != nullptr) {
item = uloc_toUnicodeLocaleType(unicode_key, item);
}
if (removes.find(item) == removes.end()) {
count++;
}
}
......@@ -489,12 +490,11 @@ MaybeHandle<JSArray> ToJSArray(
for (const char* item = enumeration->next(nullptr, status);
U_SUCCESS(status) && item != nullptr;
item = enumeration->next(nullptr, status)) {
auto mapped = substitutions.find(item);
if (mapped != substitutions.end()) {
item = mapped->second.c_str();
if (*item == '\0') {
continue;
if (unicode_key != nullptr) {
item = uloc_toUnicodeLocaleType(unicode_key, item);
}
if (removes.find(item) != removes.end()) {
continue;
}
Handle<String> str = factory->NewStringFromAsciiChecked(item);
fixed_array->set(index++, *str);
......@@ -510,8 +510,7 @@ MaybeHandle<JSArray> ToJSArray(
template <typename T>
MaybeHandle<JSArray> GetKeywordValuesFromLocale(
Isolate* isolate, const char* key, const char* unicode_key,
const icu::Locale& locale,
const std::map<std::string, std::string>& substitutions, bool may_remove) {
const icu::Locale& locale, const std::set<std::string>& removes) {
Factory* factory = isolate->factory();
UErrorCode status = U_ZERO_ERROR;
std::string ext =
......@@ -529,25 +528,22 @@ MaybeHandle<JSArray> GetKeywordValuesFromLocale(
THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError),
JSArray);
}
return ToJSArray(isolate, enumeration.get(), substitutions, may_remove);
return ToJSArray(isolate, unicode_key, enumeration.get(), removes);
}
MaybeHandle<JSArray> JSLocale::Calendars(Isolate* isolate,
Handle<JSLocale> locale) {
icu::Locale icu_locale(*(locale->icu_locale().raw()));
const std::map<std::string, std::string> substitutions(
{{"gregorian", "gregory"}, {"ethiopic-amete-alem", "ethioaa"}});
return GetKeywordValuesFromLocale<icu::Calendar>(
isolate, "calendar", "ca", icu_locale, substitutions, false);
isolate, "calendar", "ca", icu_locale, std::set<std::string>());
}
MaybeHandle<JSArray> JSLocale::Collations(Isolate* isolate,
Handle<JSLocale> locale) {
icu::Locale icu_locale(*(locale->icu_locale().raw()));
const std::map<std::string, std::string> substitutions(
{{"standard", ""}, {"search", ""}});
return GetKeywordValuesFromLocale<icu::Collator>(
isolate, "collations", "co", icu_locale, substitutions, true);
const std::set<std::string> removes({"standard", "search"});
return GetKeywordValuesFromLocale<icu::Collator>(isolate, "collations", "co",
icu_locale, removes);
}
MaybeHandle<JSArray> JSLocale::HourCycles(Isolate* isolate,
......@@ -676,8 +672,8 @@ MaybeHandle<Object> JSLocale::TimeZones(Isolate* isolate,
THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError),
JSArray);
}
const std::map<std::string, std::string> substitutions({});
return ToJSArray(isolate, enumeration.get(), substitutions, false);
return ToJSArray(isolate, nullptr, enumeration.get(),
std::set<std::string>());
}
MaybeHandle<JSObject> JSLocale::TextInfo(Isolate* isolate,
......
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony_intl_locale_info
// Test the return items of calendars fit 'type'
let a_to_z = "abcdefghijklmnopqrstuvwxyz";
let regex = /^[a-zA-Z0-9]{3,8}(-[a-zA-Z0-9]{3,8})*$/;
for (var i = 0; i < a_to_z.length; i++) {
for (var j = 0; j < a_to_z.length; j++) {
let l = a_to_z[i] + a_to_z[j];
let locale = new Intl.Locale(l);
locale.calendars.forEach(function(tokens) {
assertTrue(regex.test(tokens),
locale + ".calendars [" + locale.calendars +
"] does not meet 'type: alphanum{3,8}(sep alphanum{3,8})*'");
});
}
}
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony_intl_locale_info
// Test the return items of collations fit 'type'
let a_to_z = "abcdefghijklmnopqrstuvwxyz";
let regex = /^[a-zA-Z0-9]{3,8}(-[a-zA-Z0-9]{3,8})*$/;
for (var i = 0; i < a_to_z.length; i++) {
for (var j = 0; j < a_to_z.length; j++) {
let l = a_to_z[i] + a_to_z[j];
let locale = new Intl.Locale(l);
print(locale);
locale.collations.forEach(function(tokens) {
assertTrue(regex.test(tokens),
locale + ".collations [" + locale.collations +
"] does not meet 'type: alphanum{3,8}(sep alphanum{3,8})*'");
});
}
}
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony_intl_locale_info
// Test the return items of numberingSystems fit 'type'
let a_to_z = "abcdefghijklmnopqrstuvwxyz";
let regex = /^[a-zA-Z0-9]{3,8}(-[a-zA-Z0-9]{3,8})*$/;
for (var i = 0; i < a_to_z.length; i++) {
for (var j = 0; j < a_to_z.length; j++) {
let l = a_to_z[i] + a_to_z[j];
let locale = new Intl.Locale(l);
locale.numberingSystems.forEach(function(tokens) {
assertTrue(regex.test(tokens),
locale + ".numberingSystems [" + locale.numberingSystems +
"] does not meet 'type: alphanum{3,8}(sep alphanum{3,8})*'");
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment