Commit 3e555235 authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

Fix m(in|ax)imize() with long locale

Bug: v8:11350
Change-Id: Ic34b40c4d88d6c2a0ac62bbebee4e2a95ebba826
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2648973
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72417}
parent 8432c46a
......@@ -392,22 +392,62 @@ MaybeHandle<JSLocale> Construct(Isolate* isolate,
MaybeHandle<JSLocale> JSLocale::Maximize(Isolate* isolate,
Handle<JSLocale> locale) {
icu::Locale icu_locale(*(locale->icu_locale().raw()));
// ICU has limitation on the length of the locale while addLikelySubtags
// is called. Work around the issue by only perform addLikelySubtags
// on the base locale and merge the extension if needed.
icu::Locale source(*(locale->icu_locale().raw()));
icu::Locale result = icu::Locale::createFromName(source.getBaseName());
UErrorCode status = U_ZERO_ERROR;
icu_locale.addLikelySubtags(status);
result.addLikelySubtags(status);
if (strlen(source.getBaseName()) != strlen(result.getBaseName())) {
// Base name is changed
if (strlen(source.getBaseName()) != strlen(source.getName())) {
// the source has extensions, get the extensions from the source.
result = icu::LocaleBuilder()
.setLocale(source)
.setLanguage(result.getLanguage())
.setRegion(result.getCountry())
.setScript(result.getScript())
.setVariant(result.getVariant())
.build(status);
}
} else {
// Base name is not changed
result = source;
}
DCHECK(U_SUCCESS(status));
DCHECK(!icu_locale.isBogus());
return Construct(isolate, icu_locale);
DCHECK(!result.isBogus());
return Construct(isolate, result);
}
MaybeHandle<JSLocale> JSLocale::Minimize(Isolate* isolate,
Handle<JSLocale> locale) {
icu::Locale icu_locale(*(locale->icu_locale().raw()));
// ICU has limitation on the length of the locale while minimizeSubtags
// is called. Work around the issue by only perform addLikelySubtags
// on the base locale and merge the extension if needed.
icu::Locale source(*(locale->icu_locale().raw()));
icu::Locale result = icu::Locale::createFromName(source.getBaseName());
UErrorCode status = U_ZERO_ERROR;
icu_locale.minimizeSubtags(status);
result.minimizeSubtags(status);
if (strlen(source.getBaseName()) != strlen(result.getBaseName())) {
// Base name is changed
if (strlen(source.getBaseName()) != strlen(source.getName())) {
// the source has extensions, get the extensions from the source.
result = icu::LocaleBuilder()
.setLocale(source)
.setLanguage(result.getLanguage())
.setRegion(result.getCountry())
.setScript(result.getScript())
.setVariant(result.getVariant())
.build(status);
}
} else {
// Base name is not changed
result = source;
}
DCHECK(U_SUCCESS(status));
DCHECK(!icu_locale.isBogus());
return Construct(isolate, icu_locale);
DCHECK(!result.isBogus());
return Construct(isolate, result);
}
Handle<Object> JSLocale::Language(Isolate* isolate, Handle<JSLocale> locale) {
......
// 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.
// Test Long Locale handle minimize and maximize correctly.
let ext = "-u-cu-eur-em-default-hc-h23-ks-level1-lb-strict-lw-normal-" +
"ms-metric-nu-latn-rg-atzzzz-sd-atat1-ss-none-tz-atvie-va-posix";
// Test maximize()
assertEquals ("de-Latn-DE" + ext,
(new Intl.Locale("de" + ext)).maximize().toString());
assertEquals ("de-Latn-DE" + ext,
(new Intl.Locale("de-DE" + ext)).maximize().toString());
assertEquals ("de-Latn-DE" + ext,
(new Intl.Locale("de-Latn" + ext)).maximize().toString());
assertEquals ("de-Latn-DE" + ext,
(new Intl.Locale("de-Latn-DE" + ext)).maximize().toString());
assertEquals ("de-Hant-DE" + ext,
(new Intl.Locale("de-Hant" + ext)).maximize().toString());
assertEquals ("de-Hant-AT" + ext,
(new Intl.Locale("de-Hant-AT" + ext)).maximize().toString());
assertEquals ("de-Latn-AT" + ext,
(new Intl.Locale("de-AT" + ext)).maximize().toString());
// Test minimize()
assertEquals ("de" + ext,
(new Intl.Locale("de-Latn-DE" + ext)).minimize().toString());
assertEquals ("de" + ext,
(new Intl.Locale("de-Latn" + ext)).minimize().toString());
assertEquals ("de" + ext,
(new Intl.Locale("de-DE" + ext)).minimize().toString());
assertEquals ("de-AT" + ext,
(new Intl.Locale("de-Latn-AT" + ext)).minimize().toString());
assertEquals ("de-Hant" + ext,
(new Intl.Locale("de-Hant" + ext)).minimize().toString());
assertEquals ("de-Hant-AT" + ext,
(new Intl.Locale("de-Hant-AT" + ext)).minimize().toString());
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