Commit 1f022358 authored by Frank Tang's avatar Frank Tang Committed by V8 LUCI CQ

[intl] Change internal function to use movable Maybe<>

1. Retun value w/ newly added movable Maybe<>
2. Change arg passing from pointer to const ref.
3. Change some CHECK to DCHECK

Change-Id: Ia4e6c6031d8873c86df7ec178165dd23e75c571b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3606390
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarFrank Tang <ftang@chromium.org>
Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80351}
parent 2dd3cc30
......@@ -249,11 +249,11 @@ UNumberGroupingStrategy ToUNumberGroupingStrategy(UseGrouping use_grouping) {
std::map<const std::string, icu::MeasureUnit> CreateUnitMap() {
UErrorCode status = U_ZERO_ERROR;
int32_t total = icu::MeasureUnit::getAvailable(nullptr, 0, status);
CHECK(U_FAILURE(status));
DCHECK(U_FAILURE(status));
status = U_ZERO_ERROR;
std::vector<icu::MeasureUnit> units(total);
total = icu::MeasureUnit::getAvailable(units.data(), total, status);
CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status));
std::map<const std::string, icu::MeasureUnit> map;
std::set<std::string> sanctioned(Intl::SanctionedSimpleUnits());
for (auto it = units.begin(); it != units.end(); ++it) {
......@@ -701,7 +701,7 @@ int32_t JSNumberFormat::MinimumIntegerDigitsFromSkeleton(
matched++;
index++;
}
CHECK_GT(matched, 0);
DCHECK_GT(matched, 0);
return matched;
}
......@@ -936,7 +936,7 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions(
icu::number::LocalizedNumberFormatter* icu_number_formatter =
number_format->icu_number_formatter().raw();
icu::UnicodeString skeleton = icu_number_formatter->toSkeleton(status);
CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status));
// 4. Let options be ! ObjectCreate(%ObjectPrototype%).
Handle<JSObject> options = factory->NewJSObject(isolate->object_function());
......@@ -1202,7 +1202,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
if (nu_extension_it != r.extensions.end() &&
nu_extension_it->second != numbering_system_str.get()) {
icu_locale.setUnicodeKeywordValue("nu", nullptr, status);
CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status));
}
}
......@@ -1215,7 +1215,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
if (numbering_system_str != nullptr &&
Intl::IsValidNumberingSystem(numbering_system_str.get())) {
icu_locale.setUnicodeKeywordValue("nu", numbering_system_str.get(), status);
CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status));
}
std::string numbering_system = Intl::GetNumberingSystem(icu_locale);
......@@ -1232,7 +1232,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
if (!numbering_system.empty() && numbering_system != "latn") {
settings = settings.adoptSymbols(icu::NumberingSystem::createInstanceByName(
numbering_system.c_str(), status));
CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status));
}
// ==== Start SetNumberFormatUnitOptions ====
......@@ -1370,14 +1370,14 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
settings =
settings.unit(icu::CurrencyUnit(currency_ustr.getBuffer(), status));
CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status));
// 14.c Set intlObj.[[CurrencyDisplay]] to currencyDisplay.
// The default unitWidth is SHORT in ICU and that mapped from
// Symbol so we can skip the setting for optimization.
if (currency_display != CurrencyDisplay::SYMBOL) {
settings = settings.unitWidth(ToUNumberUnitWidth(currency_display));
}
CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status));
}
}
......@@ -1679,10 +1679,11 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
namespace {
Maybe<bool> IcuFormatNumber(
Maybe<icu::number::FormattedNumber> IcuFormatNumber(
Isolate* isolate,
const icu::number::LocalizedNumberFormatter& number_format,
Handle<Object> numeric_obj, icu::number::FormattedNumber* formatted) {
Handle<Object> numeric_obj) {
icu::number::FormattedNumber formatted;
// If it is BigInt, handle it differently.
UErrorCode status = U_ZERO_ERROR;
if (numeric_obj->IsBigInt()) {
......@@ -1690,7 +1691,7 @@ Maybe<bool> IcuFormatNumber(
Handle<String> big_int_string;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, big_int_string,
BigInt::ToString(isolate, big_int),
Nothing<bool>());
Nothing<icu::number::FormattedNumber>());
big_int_string = String::Flatten(isolate, big_int_string);
DisallowGarbageCollection no_gc;
const String::FlatContent& flat = big_int_string->GetFlatContent(no_gc);
......@@ -1698,7 +1699,7 @@ Maybe<bool> IcuFormatNumber(
DCHECK(flat.IsOneByte());
const char* char_buffer =
reinterpret_cast<const char*>(flat.ToOneByteVector().begin());
*formatted = number_format.formatDecimal({char_buffer, length}, status);
formatted = number_format.formatDecimal({char_buffer, length}, status);
} else {
if (FLAG_harmony_intl_number_format_v3 && numeric_obj->IsString()) {
// TODO(ftang) Correct the handling of string after the resolution of
......@@ -1711,30 +1712,31 @@ Maybe<bool> IcuFormatNumber(
if (flat.IsOneByte()) {
const char* char_buffer =
reinterpret_cast<const char*>(flat.ToOneByteVector().begin());
*formatted = number_format.formatDecimal({char_buffer, length}, status);
formatted = number_format.formatDecimal({char_buffer, length}, status);
} else {
// We may have two bytes string such as "漢 123456789".substring(2)
// The value will be "123456789" only in ASCII range, but encoded
// in two bytes string.
// ICU accepts UTF8 string, so if the source is two-byte encoded,
// copy into a UTF8 string via ToCString.
*formatted = number_format.formatDecimal(
formatted = number_format.formatDecimal(
{string->ToCString().get(), string->length()}, status);
}
} else {
double number = numeric_obj->IsNaN()
? std::numeric_limits<double>::quiet_NaN()
: numeric_obj->Number();
*formatted = number_format.formatDouble(number, status);
formatted = number_format.formatDouble(number, status);
}
}
if (U_FAILURE(status)) {
// This happen because of icu data trimming trim out "unit".
// See https://bugs.chromium.org/p/v8/issues/detail?id=8641
THROW_NEW_ERROR_RETURN_VALUE(
isolate, NewTypeError(MessageTemplate::kIcuError), Nothing<bool>());
THROW_NEW_ERROR_RETURN_VALUE(isolate,
NewTypeError(MessageTemplate::kIcuError),
Nothing<icu::number::FormattedNumber>());
}
return Just(true);
return Just(std::move(formatted));
}
Maybe<icu::Formattable> ToFormattable(Isolate* isolate, Handle<Object> obj,
......@@ -1877,11 +1879,12 @@ std::vector<NumberFormatSpan> FlattenRegionsToParts(
}
namespace {
Maybe<int> ConstructParts(Isolate* isolate, icu::FormattedValue* formatted,
Maybe<int> ConstructParts(Isolate* isolate,
const icu::FormattedValue& formatted,
Handle<JSArray> result, int start_index,
bool style_is_unit, bool is_nan, bool output_source) {
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString formatted_text = formatted->toString(status);
icu::UnicodeString formatted_text = formatted.toString(status);
if (U_FAILURE(status)) {
THROW_NEW_ERROR_RETURN_VALUE(
isolate, NewTypeError(MessageTemplate::kIcuError), Nothing<int>());
......@@ -1899,7 +1902,7 @@ Maybe<int> ConstructParts(Isolate* isolate, icu::FormattedValue* formatted,
Intl::FormatRangeSourceTracker tracker;
{
icu::ConstrainedFieldPosition cfpos;
while (formatted->nextPosition(cfpos, status)) {
while (formatted.nextPosition(cfpos, status)) {
int32_t category = cfpos.getCategory();
int32_t field = cfpos.getField();
int32_t start = cfpos.getStart();
......@@ -1993,8 +1996,8 @@ bool IsFiniteNonMinusZeroNumberOrBigInt(Isolate* isolate, Handle<Object> v) {
// #sec-partitionnumberrangepattern
template <typename T, MaybeHandle<T> (*F)(
Isolate*, icu::FormattedValue*,
const icu::number::LocalizedNumberFormatter*, bool)>
Isolate*, const icu::FormattedValue&,
const icu::number::LocalizedNumberFormatter&, bool)>
MaybeHandle<T> PartitionNumberRangePattern(Isolate* isolate,
Handle<JSNumberFormat> number_format,
Handle<Object> x, Handle<Object> y,
......@@ -2093,16 +2096,16 @@ MaybeHandle<T> PartitionNumberRangePattern(Isolate* isolate,
isolate, NewTypeError(MessageTemplate::kIcuError), MaybeHandle<T>());
}
return F(isolate, &formatted, number_format->icu_number_formatter().raw(),
return F(isolate, formatted, *(number_format->icu_number_formatter().raw()),
false /* is_nan */);
}
MaybeHandle<String> FormatToString(Isolate* isolate,
icu::FormattedValue* formatted,
const icu::number::LocalizedNumberFormatter*,
const icu::FormattedValue& formatted,
const icu::number::LocalizedNumberFormatter&,
bool) {
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString result = formatted->toString(status);
icu::UnicodeString result = formatted.toString(status);
if (U_FAILURE(status)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kIcuError), String);
}
......@@ -2110,11 +2113,11 @@ MaybeHandle<String> FormatToString(Isolate* isolate,
}
MaybeHandle<JSArray> FormatToJSArray(
Isolate* isolate, icu::FormattedValue* formatted,
const icu::number::LocalizedNumberFormatter* nfmt, bool is_nan,
Isolate* isolate, const icu::FormattedValue& formatted,
const icu::number::LocalizedNumberFormatter& nfmt, bool is_nan,
bool output_source) {
UErrorCode status = U_ZERO_ERROR;
bool is_unit = Style::UNIT == StyleFromSkeleton(nfmt->toSkeleton(status));
bool is_unit = Style::UNIT == StyleFromSkeleton(nfmt.toSkeleton(status));
CHECK(U_SUCCESS(status));
Factory* factory = isolate->factory();
......@@ -2126,8 +2129,8 @@ MaybeHandle<JSArray> FormatToJSArray(
}
MaybeHandle<JSArray> FormatRangeToJSArray(
Isolate* isolate, icu::FormattedValue* formatted,
const icu::number::LocalizedNumberFormatter* nfmt, bool is_nan) {
Isolate* isolate, const icu::FormattedValue& formatted,
const icu::number::LocalizedNumberFormatter& nfmt, bool is_nan) {
return FormatToJSArray(isolate, formatted, nfmt, is_nan, true);
}
......@@ -2139,12 +2142,12 @@ MaybeHandle<String> JSNumberFormat::FormatNumeric(
Handle<Object> numeric_obj) {
DCHECK(numeric_obj->IsNumeric() || FLAG_harmony_intl_number_format_v3);
icu::number::FormattedNumber formatted;
Maybe<bool> maybe_format =
IcuFormatNumber(isolate, number_format, numeric_obj, &formatted);
Maybe<icu::number::FormattedNumber> maybe_format =
IcuFormatNumber(isolate, number_format, numeric_obj);
MAYBE_RETURN(maybe_format, Handle<String>());
return FormatToString(isolate, &formatted, &number_format,
icu::number::FormattedNumber formatted = std::move(maybe_format).FromJust();
return FormatToString(isolate, formatted, number_format,
numeric_obj->IsNaN());
}
......@@ -2156,12 +2159,12 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
number_format->icu_number_formatter().raw();
CHECK_NOT_NULL(fmt);
icu::number::FormattedNumber formatted;
Maybe<bool> maybe_format =
IcuFormatNumber(isolate, *fmt, numeric_obj, &formatted);
Maybe<icu::number::FormattedNumber> maybe_format =
IcuFormatNumber(isolate, *fmt, numeric_obj);
MAYBE_RETURN(maybe_format, Handle<JSArray>());
icu::number::FormattedNumber formatted = std::move(maybe_format).FromJust();
return FormatToJSArray(isolate, &formatted, fmt, numeric_obj->IsNaN(), false);
return FormatToJSArray(isolate, formatted, *fmt, numeric_obj->IsNaN(), false);
}
MaybeHandle<String> JSNumberFormat::FormatNumericRange(
......
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