Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: src/js/i18n.js

Issue 1812673005: Use ICU case conversion/transliterator for case conversion behind a flag (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: working with icu-case-mapping.js manually compiled in; gyp change not picking it up Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ECMAScript 402 API implementation. 5 // ECMAScript 402 API implementation.
6 6
7 /** 7 /**
8 * Intl object is a single object that has some named properties, 8 * Intl object is a single object that has some named properties,
9 * all of which are constructors. 9 * all of which are constructors.
10 */ 10 */
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 'numberformat': UNDEFINED, 135 'numberformat': UNDEFINED,
136 'dateformat': UNDEFINED, 136 'dateformat': UNDEFINED,
137 'breakiterator': UNDEFINED 137 'breakiterator': UNDEFINED
138 }; 138 };
139 139
140 /** 140 /**
141 * Caches default ICU locale. 141 * Caches default ICU locale.
142 */ 142 */
143 var DEFAULT_ICU_LOCALE = UNDEFINED; 143 var DEFAULT_ICU_LOCALE = UNDEFINED;
144 144
145 function GetDefaultICULocaleJS() {
146 if (IS_UNDEFINED(DEFAULT_ICU_LOCALE)) {
147 DEFAULT_ICU_LOCALE = %GetDefaultICULocale();
148 }
149 return DEFAULT_ICU_LOCALE;
150 }
151
145 /** 152 /**
146 * Unicode extension regular expression. 153 * Unicode extension regular expression.
147 */ 154 */
148 var UNICODE_EXTENSION_RE = UNDEFINED; 155 var UNICODE_EXTENSION_RE = UNDEFINED;
149 156
150 function GetUnicodeExtensionRE() { 157 function GetUnicodeExtensionRE() {
151 if (IS_UNDEFINED(UNDEFINED)) { 158 if (IS_UNDEFINED(UNDEFINED)) {
152 UNICODE_EXTENSION_RE = new GlobalRegExp('-u(-[a-z0-9]{2,8})+', 'g'); 159 UNICODE_EXTENSION_RE = new GlobalRegExp('-u(-[a-z0-9]{2,8})+', 'g');
153 } 160 }
154 return UNICODE_EXTENSION_RE; 161 return UNICODE_EXTENSION_RE;
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 // Truncate locale if possible. 446 // Truncate locale if possible.
440 var pos = %_Call(StringLastIndexOf, locale, '-'); 447 var pos = %_Call(StringLastIndexOf, locale, '-');
441 if (pos === -1) { 448 if (pos === -1) {
442 break; 449 break;
443 } 450 }
444 locale = %_Call(StringSubstring, locale, 0, pos); 451 locale = %_Call(StringSubstring, locale, 0, pos);
445 } while (true); 452 } while (true);
446 } 453 }
447 454
448 // Didn't find a match, return default. 455 // Didn't find a match, return default.
449 if (IS_UNDEFINED(DEFAULT_ICU_LOCALE)) { 456 return {'locale': GetDefaultICULocaleJS(), 'extension': '', 'position': -1};
450 DEFAULT_ICU_LOCALE = %GetDefaultICULocale();
451 }
452
453 return {'locale': DEFAULT_ICU_LOCALE, 'extension': '', 'position': -1};
454 } 457 }
455 458
456 459
457 /** 460 /**
458 * Returns best matched supported locale and extension info using 461 * Returns best matched supported locale and extension info using
459 * implementation dependend algorithm. 462 * implementation dependend algorithm.
460 */ 463 */
461 function bestFitMatcher(service, requestedLocales) { 464 function bestFitMatcher(service, requestedLocales) {
462 // TODO(cira): implement better best fit algorithm. 465 // TODO(cira): implement better best fit algorithm.
463 return lookupMatcher(service, requestedLocales); 466 return lookupMatcher(service, requestedLocales);
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 } 716 }
714 } 717 }
715 return result; 718 return result;
716 } 719 }
717 720
718 /** 721 /**
719 * Canonicalizes the language tag, or throws in case the tag is invalid. 722 * Canonicalizes the language tag, or throws in case the tag is invalid.
720 */ 723 */
721 function canonicalizeLanguageTag(localeID) { 724 function canonicalizeLanguageTag(localeID) {
722 // null is typeof 'object' so we have to do extra check. 725 // null is typeof 'object' so we have to do extra check.
723 if (typeof localeID !== 'string' && typeof localeID !== 'object' || 726 if ((!IS_STRING(localeID) && !IS_RECEIVER(localeID)) ||
724 IS_NULL(localeID)) { 727 IS_NULL(localeID)) {
725 throw MakeTypeError(kLanguageID); 728 throw MakeTypeError(kLanguageID);
726 } 729 }
727 730
731 // Optimize for the most common case; a language code alone in
732 // the canonical form/lowercase (e.g. "en", "fil").
733 if (IS_STRING(localeID) &&
734 !IS_NULL(InternalRegExpMatch(/^[a-z]{2,3}$/, localeID)))
Yang 2016/04/29 07:38:47 brackets
jungshik at Google 2016/04/29 18:03:23 Done.
735 return localeID;
736
728 var localeString = GlobalString(localeID); 737 var localeString = GlobalString(localeID);
729 738
730 if (isValidLanguageTag(localeString) === false) { 739 if (isValidLanguageTag(localeString) === false) {
731 throw MakeRangeError(kInvalidLanguageTag, localeString); 740 throw MakeRangeError(kInvalidLanguageTag, localeString);
732 } 741 }
733 742
734 // This call will strip -kn but not -kn-true extensions.
735 // ICU bug filled - http://bugs.icu-project.org/trac/ticket/9265.
736 // TODO(cira): check if -u-kn-true-kc-true-kh-true still throws after
737 // upgrade to ICU 4.9.
738 var tag = %CanonicalizeLanguageTag(localeString); 743 var tag = %CanonicalizeLanguageTag(localeString);
739 if (tag === 'invalid-tag') { 744 if (tag === 'invalid-tag') {
740 throw MakeRangeError(kInvalidLanguageTag, localeString); 745 throw MakeRangeError(kInvalidLanguageTag, localeString);
741 } 746 }
742 747
743 return tag; 748 return tag;
744 } 749 }
745 750
746 751
747 /** 752 /**
(...skipping 1235 matching lines...) Expand 10 before | Expand all | Expand 10 after
1983 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults; 1988 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults;
1984 if (IS_UNDEFINED(locales) && IS_UNDEFINED(options)) { 1989 if (IS_UNDEFINED(locales) && IS_UNDEFINED(options)) {
1985 if (IS_UNDEFINED(defaultObjects[service])) { 1990 if (IS_UNDEFINED(defaultObjects[service])) {
1986 defaultObjects[service] = new savedObjects[service](locales, useOptions); 1991 defaultObjects[service] = new savedObjects[service](locales, useOptions);
1987 } 1992 }
1988 return defaultObjects[service]; 1993 return defaultObjects[service];
1989 } 1994 }
1990 return new savedObjects[service](locales, useOptions); 1995 return new savedObjects[service](locales, useOptions);
1991 } 1996 }
1992 1997
1998 function LocaleConvertCase(s, locales, isToUpper) {
1999 // ECMA 402 section 13.1.2 steps 1 through 12.
2000 var language;
2001 // Optimize for the most common two cases. initializeLocaleList() can handle
2002 // them as well, but it's rather slow accounting for over 60% of
2003 // toLocale{U,L}Case() and about 40% of toLocale{U,L}Case("<locale>").
2004 if (IS_UNDEFINED(locales)) {
2005 language = GetDefaultICULocaleJS();
2006 } else if (IS_STRING(locales)) {
2007 language = canonicalizeLanguageTag(locales);
2008 } else {
2009 var locales = initializeLocaleList(locales);
2010 language = locales.length > 0 ? locales[0] : GetDefaultICULocaleJS();
2011 }
2012
2013 // StringSplit is slwoer than this.
Yang 2016/04/29 07:38:47 "slower"
jungshik at Google 2016/04/29 18:03:23 Done.
2014 var pos = %_Call(StringIndexOf, language, '-');
2015 if (pos != -1) {
2016 language = %_Call(StringSubstring, language, 0, pos);
2017 }
2018
2019 var CUSTOM_CASE_LANGUAGES = ['az', 'el', 'lt', 'tr'];
2020 var langIndex = %_Call(ArrayIndexOf, CUSTOM_CASE_LANGUAGES, language);
2021 if (langIndex == -1) {
2022 // language-independent case conversion.
2023 return isToUpper ? %StringToUpperCaseI18N(s) : %StringToLowerCaseI18N(s);
2024 }
2025 return %StringLocaleConvertCase(s, isToUpper,
2026 CUSTOM_CASE_LANGUAGES[langIndex]);
2027 }
2028
1993 /** 2029 /**
1994 * Compares this and that, and returns less than 0, 0 or greater than 0 value. 2030 * Compares this and that, and returns less than 0, 0 or greater than 0 value.
1995 * Overrides the built-in method. 2031 * Overrides the built-in method.
1996 */ 2032 */
1997 OverrideFunction(GlobalString.prototype, 'localeCompare', function(that) { 2033 OverrideFunction(GlobalString.prototype, 'localeCompare', function(that) {
1998 if (!IS_UNDEFINED(new.target)) { 2034 if (!IS_UNDEFINED(new.target)) {
1999 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 2035 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
2000 } 2036 }
2001 2037
2002 if (IS_NULL_OR_UNDEFINED(this)) { 2038 if (IS_NULL_OR_UNDEFINED(this)) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2035 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form); 2071 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form);
2036 if (normalizationForm === -1) { 2072 if (normalizationForm === -1) {
2037 throw MakeRangeError(kNormalizationForm, 2073 throw MakeRangeError(kNormalizationForm,
2038 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', ')); 2074 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', '));
2039 } 2075 }
2040 2076
2041 return %StringNormalize(s, normalizationForm); 2077 return %StringNormalize(s, normalizationForm);
2042 } 2078 }
2043 ); 2079 );
2044 2080
2081 /*
2082 OverrideFunction(GlobalString.prototype, 'toLowerCase', function() {
2083 if (!IS_UNDEFINED(new.target)) {
2084 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
2085 }
2086 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLowerCase");
2087 var s = TO_STRING(this);
2088 return %StringToLowerCaseI18N(s);
2089 }
2090 );
2091
2092 OverrideFunction(GlobalString.prototype, 'toUpperCase', function() {
2093 if (!IS_UNDEFINED(new.target)) {
2094 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
2095 }
2096 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLowerCase");
2097 var s = TO_STRING(this);
2098 return %StringToUpperCaseI18N(s);
2099 }
2100 );
2101 */
2102
2103 OverrideFunction(GlobalString.prototype, 'toLocaleLowerCase', function() {
2104 if (!IS_UNDEFINED(new.target)) {
2105 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
2106 }
2107 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLocaleLowerCase");
2108 return LocaleConvertCase(TO_STRING(this), arguments[0], false);
2109 }
2110 );
2111
2112
2113 OverrideFunction(GlobalString.prototype, 'toLocaleUpperCase', function() {
2114 if (!IS_UNDEFINED(new.target)) {
2115 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
2116 }
2117 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLocaleUpperCase");
2118 return LocaleConvertCase(TO_STRING(this), arguments[0], true);
2119 }
2120 );
2121
2045 2122
2046 /** 2123 /**
2047 * Formats a Number object (this) using locale and options values. 2124 * Formats a Number object (this) using locale and options values.
2048 * If locale or options are omitted, defaults are used. 2125 * If locale or options are omitted, defaults are used.
2049 */ 2126 */
2050 OverrideFunction(GlobalNumber.prototype, 'toLocaleString', function() { 2127 OverrideFunction(GlobalNumber.prototype, 'toLocaleString', function() {
2051 if (!IS_UNDEFINED(new.target)) { 2128 if (!IS_UNDEFINED(new.target)) {
2052 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 2129 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
2053 } 2130 }
2054 2131
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
2130 } 2207 }
2131 2208
2132 var locales = arguments[0]; 2209 var locales = arguments[0];
2133 var options = arguments[1]; 2210 var options = arguments[1];
2134 return toLocaleDateTime( 2211 return toLocaleDateTime(
2135 this, locales, options, 'time', 'time', 'dateformattime'); 2212 this, locales, options, 'time', 'time', 'dateformattime');
2136 } 2213 }
2137 ); 2214 );
2138 2215
2139 }) 2216 })
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698