Commit 31b6faed authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

[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: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54735}
parent d27aaa75
......@@ -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);
}
......
......@@ -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
......@@ -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
......
......@@ -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);
......
......@@ -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) \
......
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