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, ...@@ -460,22 +460,23 @@ MaybeHandle<JSLocale> JSLocale::Minimize(Isolate* isolate,
return Construct(isolate, result); return Construct(isolate, result);
} }
MaybeHandle<JSArray> ToJSArray( MaybeHandle<JSArray> ToJSArray(Isolate* isolate, const char* unicode_key,
Isolate* isolate, icu::StringEnumeration* enumeration, icu::StringEnumeration* enumeration,
const std::map<std::string, std::string>& substitutions, bool may_remove) { const std::set<std::string>& removes) {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
Factory* factory = isolate->factory(); Factory* factory = isolate->factory();
int32_t count = 0; 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 // 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. // many items we will insert before we allocate the fixed array.
for (const char* item = enumeration->next(nullptr, status); for (const char* item = enumeration->next(nullptr, status);
U_SUCCESS(status) && item != nullptr; U_SUCCESS(status) && item != nullptr;
item = enumeration->next(nullptr, status)) { item = enumeration->next(nullptr, status)) {
auto mapped = substitutions.find(item); if (unicode_key != nullptr) {
if ((mapped == substitutions.end()) || item = uloc_toUnicodeLocaleType(unicode_key, item);
(*(mapped->second.c_str()) != '\0')) { }
if (removes.find(item) == removes.end()) {
count++; count++;
} }
} }
...@@ -489,12 +490,11 @@ MaybeHandle<JSArray> ToJSArray( ...@@ -489,12 +490,11 @@ MaybeHandle<JSArray> ToJSArray(
for (const char* item = enumeration->next(nullptr, status); for (const char* item = enumeration->next(nullptr, status);
U_SUCCESS(status) && item != nullptr; U_SUCCESS(status) && item != nullptr;
item = enumeration->next(nullptr, status)) { item = enumeration->next(nullptr, status)) {
auto mapped = substitutions.find(item); if (unicode_key != nullptr) {
if (mapped != substitutions.end()) { item = uloc_toUnicodeLocaleType(unicode_key, item);
item = mapped->second.c_str(); }
if (*item == '\0') { if (removes.find(item) != removes.end()) {
continue; continue;
}
} }
Handle<String> str = factory->NewStringFromAsciiChecked(item); Handle<String> str = factory->NewStringFromAsciiChecked(item);
fixed_array->set(index++, *str); fixed_array->set(index++, *str);
...@@ -510,8 +510,7 @@ MaybeHandle<JSArray> ToJSArray( ...@@ -510,8 +510,7 @@ MaybeHandle<JSArray> ToJSArray(
template <typename T> template <typename T>
MaybeHandle<JSArray> GetKeywordValuesFromLocale( MaybeHandle<JSArray> GetKeywordValuesFromLocale(
Isolate* isolate, const char* key, const char* unicode_key, Isolate* isolate, const char* key, const char* unicode_key,
const icu::Locale& locale, const icu::Locale& locale, const std::set<std::string>& removes) {
const std::map<std::string, std::string>& substitutions, bool may_remove) {
Factory* factory = isolate->factory(); Factory* factory = isolate->factory();
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
std::string ext = std::string ext =
...@@ -529,25 +528,22 @@ MaybeHandle<JSArray> GetKeywordValuesFromLocale( ...@@ -529,25 +528,22 @@ MaybeHandle<JSArray> GetKeywordValuesFromLocale(
THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError), THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError),
JSArray); JSArray);
} }
return ToJSArray(isolate, enumeration.get(), substitutions, may_remove); return ToJSArray(isolate, unicode_key, enumeration.get(), removes);
} }
MaybeHandle<JSArray> JSLocale::Calendars(Isolate* isolate, MaybeHandle<JSArray> JSLocale::Calendars(Isolate* isolate,
Handle<JSLocale> locale) { Handle<JSLocale> locale) {
icu::Locale icu_locale(*(locale->icu_locale().raw())); 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>( 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, MaybeHandle<JSArray> JSLocale::Collations(Isolate* isolate,
Handle<JSLocale> locale) { Handle<JSLocale> locale) {
icu::Locale icu_locale(*(locale->icu_locale().raw())); icu::Locale icu_locale(*(locale->icu_locale().raw()));
const std::map<std::string, std::string> substitutions( const std::set<std::string> removes({"standard", "search"});
{{"standard", ""}, {"search", ""}}); return GetKeywordValuesFromLocale<icu::Collator>(isolate, "collations", "co",
return GetKeywordValuesFromLocale<icu::Collator>( icu_locale, removes);
isolate, "collations", "co", icu_locale, substitutions, true);
} }
MaybeHandle<JSArray> JSLocale::HourCycles(Isolate* isolate, MaybeHandle<JSArray> JSLocale::HourCycles(Isolate* isolate,
...@@ -676,8 +672,8 @@ MaybeHandle<Object> JSLocale::TimeZones(Isolate* isolate, ...@@ -676,8 +672,8 @@ MaybeHandle<Object> JSLocale::TimeZones(Isolate* isolate,
THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError), THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError),
JSArray); JSArray);
} }
const std::map<std::string, std::string> substitutions({}); return ToJSArray(isolate, nullptr, enumeration.get(),
return ToJSArray(isolate, enumeration.get(), substitutions, false); std::set<std::string>());
} }
MaybeHandle<JSObject> JSLocale::TextInfo(Isolate* isolate, 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