[Intl] move isWellFormedCurrencyCode to C++

Bug: v8:7979
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: Ib02a634dd82b6cef647e23464401af4acccc21f1
Reviewed-on: https://chromium-review.googlesource.com/1151049
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54735}
This commit is contained in:
Frank Tang 2018-07-26 11:51:10 -07:00 committed by Commit Bot
parent d27aaa759d
commit 31b6faed38
5 changed files with 42 additions and 12 deletions

View File

@ -1228,17 +1228,6 @@ DEFINE_METHOD(
}
);
/**
* Verifies that the input is a well-formed ISO 4217 currency code.
* Don't uppercase to test. It could convert invalid code into a valid one.
* For example \u00DFP (Eszett+P) becomes SSP.
*/
function isWellFormedCurrencyCode(currency) {
return typeof currency === "string" && currency.length === 3 &&
IS_NULL(%regexp_internal_match(/[^A-Za-z]/, currency));
}
function defaultNumberOption(value, min, max, fallback, property) {
if (!IS_UNDEFINED(value)) {
value = TO_NUMBER(value);
@ -1308,7 +1297,7 @@ function CreateNumberFormat(locales, options) {
'style', 'string', ['decimal', 'percent', 'currency'], 'decimal'));
var currency = getOption('currency', 'string');
if (!IS_UNDEFINED(currency) && !isWellFormedCurrencyCode(currency)) {
if (!IS_UNDEFINED(currency) && !%IsWellFormedCurrencyCode(currency)) {
throw %make_range_error(kInvalidCurrencyCode, currency);
}

View File

@ -1761,5 +1761,33 @@ MaybeHandle<JSObject> Intl::CreateNumberFormat(Isolate* isolate,
return local_object;
}
namespace {
bool IsAToZ(char ch) {
return (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z'));
}
} // namespace
// Verifies that the input is a well-formed ISO 4217 currency code.
// ecma402/#sec-currency-codes
bool Intl::IsWellFormedCurrencyCode(Isolate* isolate, Handle<String> currency) {
// 2. If the number of elements in normalized is not 3, return false.
if (currency->length() != 3) return false;
currency = String::Flatten(isolate, currency);
{
DisallowHeapAllocation no_gc;
String::FlatContent flat = currency->GetFlatContent();
// 1. Let normalized be the result of mapping currency to upper case as
// described in 6.1. 3. If normalized contains any character that is not in
// the range "A" to "Z" (U+0041 to U+005A), return false. 4. Return true.
// Don't uppercase to test. It could convert invalid code into a valid one.
// For example \u00DFP (Eszett+P) becomes SSP.
return (IsAToZ(flat.Get(0)) && IsAToZ(flat.Get(1)) && IsAToZ(flat.Get(2)));
}
}
} // namespace internal
} // namespace v8

View File

@ -310,6 +310,10 @@ class Intl {
V8_WARN_UNUSED_RESULT static MaybeHandle<JSObject> CreateNumberFormat(
Isolate* isolate, Handle<String> locale, Handle<JSObject> options,
Handle<JSObject> resolved);
// ecma402/#sec-iswellformedcurrencycode
static bool IsWellFormedCurrencyCode(Isolate* isolate,
Handle<String> currency);
};
} // namespace internal

View File

@ -116,6 +116,14 @@ RUNTIME_FUNCTION(Runtime_GetDefaultICULocale) {
return *Intl::DefaultLocale(isolate);
}
RUNTIME_FUNCTION(Runtime_IsWellFormedCurrencyCode) {
HandleScope scope(isolate);
DCHECK_EQ(1, args.length());
CONVERT_ARG_HANDLE_CHECKED(String, currency, 0);
return *(isolate->factory()->ToBoolean(
Intl::IsWellFormedCurrencyCode(isolate, currency)));
}
RUNTIME_FUNCTION(Runtime_IsInitializedIntlObjectOfType) {
HandleScope scope(isolate);

View File

@ -222,6 +222,7 @@ namespace internal {
F(InternalNumberFormat, 2, 1) \
F(IntlUnwrapReceiver, 5, 1) \
F(IsInitializedIntlObjectOfType, 2, 1) \
F(IsWellFormedCurrencyCode, 1, 1) \
F(MarkAsInitializedIntlObjectOfType, 2, 1) \
F(PluralRulesSelect, 2, 1) \
F(StringLocaleConvertCase, 3, 1) \