@@ -1257,42 +1257,31 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
12571257}
12581258
12591259namespace {
1260- Maybe<icu::UnicodeString > IcuFormatNumber (
1260+ Maybe<bool > IcuFormatNumber (
12611261 Isolate* isolate,
12621262 const icu::number::LocalizedNumberFormatter& number_format,
1263- Handle<Object> numeric_obj, icu::FieldPositionIterator* fp_iter ) {
1263+ Handle<Object> numeric_obj, icu::number::FormattedNumber* formatted ) {
12641264 // If it is BigInt, handle it differently.
12651265 UErrorCode status = U_ZERO_ERROR;
1266- icu::number::FormattedNumber formatted;
12671266 if (numeric_obj->IsBigInt ()) {
12681267 Handle<BigInt> big_int = Handle<BigInt>::cast (numeric_obj);
12691268 Handle<String> big_int_string;
12701269 ASSIGN_RETURN_ON_EXCEPTION_VALUE (isolate, big_int_string,
12711270 BigInt::ToString (isolate, big_int),
1272- Nothing<icu::UnicodeString >());
1273- formatted = number_format.formatDecimal (
1271+ Nothing<bool >());
1272+ * formatted = number_format.formatDecimal (
12741273 {big_int_string->ToCString ().get (), big_int_string->length ()}, status);
12751274 } else {
12761275 double number = numeric_obj->Number ();
1277- formatted = number_format.formatDouble (number, status);
1276+ * formatted = number_format.formatDouble (number, status);
12781277 }
12791278 if (U_FAILURE (status)) {
12801279 // This happen because of icu data trimming trim out "unit".
12811280 // See https://bugs.chromium.org/p/v8/issues/detail?id=8641
1282- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1283- NewTypeError (MessageTemplate::kIcuError ),
1284- Nothing<icu::UnicodeString>());
1285- }
1286- if (fp_iter) {
1287- formatted.getAllFieldPositions (*fp_iter, status);
1281+ THROW_NEW_ERROR_RETURN_VALUE (
1282+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<bool >());
12881283 }
1289- icu::UnicodeString result = formatted.toString (status);
1290- if (U_FAILURE (status)) {
1291- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1292- NewTypeError (MessageTemplate::kIcuError ),
1293- Nothing<icu::UnicodeString>());
1294- }
1295- return Just (result);
1284+ return Just (true );
12961285}
12971286
12981287} // namespace
@@ -1303,10 +1292,16 @@ MaybeHandle<String> JSNumberFormat::FormatNumeric(
13031292 Handle<Object> numeric_obj) {
13041293 DCHECK (numeric_obj->IsNumeric ());
13051294
1306- Maybe<icu::UnicodeString> maybe_format =
1307- IcuFormatNumber (isolate, number_format, numeric_obj, nullptr );
1295+ icu::number::FormattedNumber formatted;
1296+ Maybe<bool > maybe_format =
1297+ IcuFormatNumber (isolate, number_format, numeric_obj, &formatted);
13081298 MAYBE_RETURN (maybe_format, Handle<String>());
1309- return Intl::ToString (isolate, maybe_format.FromJust ());
1299+ UErrorCode status = U_ZERO_ERROR;
1300+ icu::UnicodeString result = formatted.toString (status);
1301+ if (U_FAILURE (status)) {
1302+ THROW_NEW_ERROR (isolate, NewTypeError (MessageTemplate::kIcuError ), String);
1303+ }
1304+ return Intl::ToString (isolate, result);
13101305}
13111306
13121307namespace {
@@ -1419,12 +1414,18 @@ std::vector<NumberFormatSpan> FlattenRegionsToParts(
14191414}
14201415
14211416namespace {
1422- Maybe<int > ConstructParts (Isolate* isolate, const icu::UnicodeString& formatted,
1423- icu::FieldPositionIterator* fp_iter ,
1417+ Maybe<int > ConstructParts (Isolate* isolate,
1418+ icu::number::FormattedNumber* formatted ,
14241419 Handle<JSArray> result, int start_index,
14251420 Handle<Object> numeric_obj, bool style_is_unit) {
1421+ UErrorCode status = U_ZERO_ERROR;
1422+ icu::UnicodeString formatted_text = formatted->toString (status);
1423+ if (U_FAILURE (status)) {
1424+ THROW_NEW_ERROR_RETURN_VALUE (
1425+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<int >());
1426+ }
14261427 DCHECK (numeric_obj->IsNumeric ());
1427- int32_t length = formatted .length ();
1428+ int32_t length = formatted_text .length ();
14281429 int index = start_index;
14291430 if (length == 0 ) return Just (index);
14301431
@@ -1433,13 +1434,14 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
14331434 // other region covers some part of the formatted string. It's possible
14341435 // there's another field with exactly the same begin and end as this backdrop,
14351436 // in which case the backdrop's field_id of -1 will give it lower priority.
1436- regions.push_back (NumberFormatSpan (-1 , 0 , formatted .length ()));
1437+ regions.push_back (NumberFormatSpan (-1 , 0 , formatted_text .length ()));
14371438
14381439 {
1439- icu::FieldPosition fp;
1440- while (fp_iter->next (fp)) {
1441- regions.push_back (NumberFormatSpan (fp.getField (), fp.getBeginIndex (),
1442- fp.getEndIndex ()));
1440+ icu::ConstrainedFieldPosition cfp;
1441+ cfp.constrainCategory (UFIELD_CATEGORY_NUMBER);
1442+ while (formatted->nextPosition (cfp, status)) {
1443+ regions.push_back (
1444+ NumberFormatSpan (cfp.getField (), cfp.getStart (), cfp.getLimit ()));
14431445 }
14441446 }
14451447
@@ -1461,7 +1463,7 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
14611463 Handle<String> substring;
14621464 ASSIGN_RETURN_ON_EXCEPTION_VALUE (
14631465 isolate, substring,
1464- Intl::ToString (isolate, formatted , part.begin_pos , part.end_pos ),
1466+ Intl::ToString (isolate, formatted_text , part.begin_pos , part.end_pos ),
14651467 Nothing<int >());
14661468 Intl::AddElement (isolate, result, index, field_type_string, substring);
14671469 ++index;
@@ -1481,14 +1483,14 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
14811483 number_format->icu_number_formatter ().raw ();
14821484 CHECK_NOT_NULL (fmt);
14831485
1484- icu::FieldPositionIterator fp_iter ;
1485- Maybe<icu::UnicodeString > maybe_format =
1486- IcuFormatNumber (isolate, *fmt, numeric_obj, &fp_iter );
1486+ icu::number::FormattedNumber formatted ;
1487+ Maybe<bool > maybe_format =
1488+ IcuFormatNumber (isolate, *fmt, numeric_obj, &formatted );
14871489 MAYBE_RETURN (maybe_format, Handle<JSArray>());
14881490
14891491 Handle<JSArray> result = factory->NewJSArray (0 );
14901492 Maybe<int > maybe_format_to_parts = ConstructParts (
1491- isolate, maybe_format. FromJust (), &fp_iter , result, 0 , numeric_obj,
1493+ isolate, &formatted , result, 0 , numeric_obj,
14921494 number_format->style () == JSNumberFormat::Style::UNIT);
14931495 MAYBE_RETURN (maybe_format_to_parts, Handle<JSArray>());
14941496
0 commit comments