Commit 218a679d authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

Reland "Avoid UTF8 conversion in JSNumberFormat"

This is a reland of f70e5abf

Fix by casting std::strlen() to int32_t
Original change's description:
> Avoid UTF8 conversion in JSNumberFormat
>
> Change-Id: Idf4e95e6979adfbca24f8fd213967f821a136d87
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2057930
> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
> Commit-Queue: Frank Tang <ftang@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#66353}

Change-Id: I764265217202151b6c02d1d7b01b7c71af677e36
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2062743
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66359}
parent 01331258
...@@ -353,28 +353,27 @@ bool UseGroupingFromSkeleton(const icu::UnicodeString& skeleton) { ...@@ -353,28 +353,27 @@ bool UseGroupingFromSkeleton(const icu::UnicodeString& skeleton) {
// Parse currency code from skeleton. For example, skeleton as // Parse currency code from skeleton. For example, skeleton as
// "currency/TWD .00 rounding-mode-half-up unit-width-full-name;" // "currency/TWD .00 rounding-mode-half-up unit-width-full-name;"
std::string CurrencyFromSkeleton(const icu::UnicodeString& skeleton) { const icu::UnicodeString CurrencyFromSkeleton(
std::string str; const icu::UnicodeString& skeleton) {
str = skeleton.toUTF8String<std::string>(str); const char currency[] = "currency/";
std::string search("currency/"); int32_t index = skeleton.indexOf(currency);
size_t index = str.find(search); if (index < 0) return "";
if (index == str.npos) return ""; index += static_cast<int32_t>(std::strlen(currency));
return str.substr(index + search.size(), 3); return skeleton.tempSubString(index, 3);
} }
std::string NumberingSystemFromSkeleton(const icu::UnicodeString& skeleton) { const icu::UnicodeString NumberingSystemFromSkeleton(
std::string str; const icu::UnicodeString& skeleton) {
str = skeleton.toUTF8String<std::string>(str); int32_t index = skeleton.indexOf("latin");
size_t index = str.find("latin"); if (index >= 0) return "latn";
if (index != str.npos) return "latn"; const char numbering_system[] = "numbering-system/";
std::string search("numbering-system/"); index = skeleton.indexOf(numbering_system);
index = str.find(search); if (index < 0) return "";
if (index == str.npos) return ""; index += static_cast<int32_t>(std::strlen(numbering_system));
size_t space_index = str.find(" ", index + search.size()); const icu::UnicodeString res = skeleton.tempSubString(index);
if (space_index != str.npos) { index = res.indexOf(" ");
space_index -= index + search.size(); if (index < 0) return res;
} return res.tempSubString(0, index);
return str.substr(index + search.size(), space_index);
} }
// Return CurrencySign as string based on skeleton. // Return CurrencySign as string based on skeleton.
...@@ -689,7 +688,8 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions( ...@@ -689,7 +688,8 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions(
Handle<JSObject> options = factory->NewJSObject(isolate->object_function()); Handle<JSObject> options = factory->NewJSObject(isolate->object_function());
Handle<String> locale = Handle<String>(number_format->locale(), isolate); Handle<String> locale = Handle<String>(number_format->locale(), isolate);
std::string numberingSystem = NumberingSystemFromSkeleton(skeleton); const icu::UnicodeString numberingSystem_ustr =
NumberingSystemFromSkeleton(skeleton);
// 5. For each row of Table 4, except the header row, in table order, do // 5. For each row of Table 4, except the header row, in table order, do
// Table 4: Resolved Options of NumberFormat Instances // Table 4: Resolved Options of NumberFormat Instances
// Internal Slot Property // Internal Slot Property
...@@ -708,22 +708,25 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions( ...@@ -708,22 +708,25 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions(
factory->locale_string(), locale, factory->locale_string(), locale,
Just(kDontThrow)) Just(kDontThrow))
.FromJust()); .FromJust());
CHECK(JSReceiver::CreateDataProperty( Handle<String> numberingSystem_string;
isolate, options, factory->numberingSystem_string(), CHECK(Intl::ToString(isolate, numberingSystem_ustr)
factory->NewStringFromAsciiChecked(numberingSystem.c_str()), .ToHandle(&numberingSystem_string));
Just(kDontThrow)) CHECK(JSReceiver::CreateDataProperty(isolate, options,
factory->numberingSystem_string(),
numberingSystem_string, Just(kDontThrow))
.FromJust()); .FromJust());
Style style = StyleFromSkeleton(skeleton); Style style = StyleFromSkeleton(skeleton);
CHECK(JSReceiver::CreateDataProperty( CHECK(JSReceiver::CreateDataProperty(
isolate, options, factory->style_string(), isolate, options, factory->style_string(),
StyleAsString(isolate, style), Just(kDontThrow)) StyleAsString(isolate, style), Just(kDontThrow))
.FromJust()); .FromJust());
std::string currency = CurrencyFromSkeleton(skeleton); const icu::UnicodeString currency_ustr = CurrencyFromSkeleton(skeleton);
if (!currency.empty()) { if (!currency_ustr.isEmpty()) {
CHECK(JSReceiver::CreateDataProperty( Handle<String> currency_string;
isolate, options, factory->currency_string(), CHECK(Intl::ToString(isolate, currency_ustr).ToHandle(&currency_string));
factory->NewStringFromAsciiChecked(currency.c_str()), CHECK(JSReceiver::CreateDataProperty(isolate, options,
Just(kDontThrow)) factory->currency_string(),
currency_string, Just(kDontThrow))
.FromJust()); .FromJust());
CHECK(JSReceiver::CreateDataProperty( CHECK(JSReceiver::CreateDataProperty(
......
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