@@ -1239,44 +1239,33 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
1239
1239
}
1240
1240
1241
1241
namespace {
1242
- Maybe<icu::UnicodeString > IcuFormatNumber (
1242
+ Maybe<bool > IcuFormatNumber (
1243
1243
Isolate* isolate,
1244
1244
const icu::number::LocalizedNumberFormatter& number_format,
1245
- Handle <Object> numeric_obj, icu::FieldPositionIterator* fp_iter ) {
1245
+ Handle <Object> numeric_obj, icu::number::FormattedNumber* formatted ) {
1246
1246
// If it is BigInt, handle it differently.
1247
1247
UErrorCode status = U_ZERO_ERROR;
1248
- icu::number::FormattedNumber formatted;
1249
1248
if (numeric_obj->IsBigInt ()) {
1250
1249
Handle <BigInt> big_int = Handle <BigInt>::cast (numeric_obj);
1251
1250
Handle <String> big_int_string;
1252
1251
ASSIGN_RETURN_ON_EXCEPTION_VALUE (isolate, big_int_string,
1253
1252
BigInt::ToString (isolate, big_int),
1254
- Nothing<icu::UnicodeString >());
1255
- formatted = number_format.formatDecimal (
1253
+ Nothing<bool >());
1254
+ * formatted = number_format.formatDecimal (
1256
1255
{big_int_string->ToCString ().get (), big_int_string->length ()}, status);
1257
1256
} else {
1258
1257
double number = numeric_obj->IsNaN ()
1259
1258
? std::numeric_limits<double >::quiet_NaN ()
1260
1259
: numeric_obj->Number ();
1261
- formatted = number_format.formatDouble (number, status);
1260
+ * formatted = number_format.formatDouble (number, status);
1262
1261
}
1263
1262
if (U_FAILURE (status)) {
1264
1263
// This happen because of icu data trimming trim out "unit".
1265
1264
// See https://bugs.chromium.org/p/v8/issues/detail?id=8641
1266
- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1267
- NewTypeError (MessageTemplate::kIcuError ),
1268
- Nothing<icu::UnicodeString>());
1269
- }
1270
- if (fp_iter) {
1271
- formatted.getAllFieldPositions (*fp_iter, status);
1272
- }
1273
- icu::UnicodeString result = formatted.toString (status);
1274
- if (U_FAILURE (status)) {
1275
- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1276
- NewTypeError (MessageTemplate::kIcuError ),
1277
- Nothing<icu::UnicodeString>());
1265
+ THROW_NEW_ERROR_RETURN_VALUE (
1266
+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<bool >());
1278
1267
}
1279
- return Just (result );
1268
+ return Just (true );
1280
1269
}
1281
1270
1282
1271
} // namespace
@@ -1287,10 +1276,16 @@ MaybeHandle<String> JSNumberFormat::FormatNumeric(
1287
1276
Handle <Object> numeric_obj) {
1288
1277
DCHECK (numeric_obj->IsNumeric ());
1289
1278
1290
- Maybe<icu::UnicodeString> maybe_format =
1291
- IcuFormatNumber (isolate, number_format, numeric_obj, nullptr );
1279
+ icu::number::FormattedNumber formatted;
1280
+ Maybe<bool > maybe_format =
1281
+ IcuFormatNumber (isolate, number_format, numeric_obj, &formatted);
1292
1282
MAYBE_RETURN (maybe_format, Handle <String>());
1293
- return Intl::ToString (isolate, maybe_format.FromJust ());
1283
+ UErrorCode status = U_ZERO_ERROR;
1284
+ icu::UnicodeString result = formatted.toString (status);
1285
+ if (U_FAILURE (status)) {
1286
+ THROW_NEW_ERROR (isolate, NewTypeError (MessageTemplate::kIcuError ), String);
1287
+ }
1288
+ return Intl::ToString (isolate, result);
1294
1289
}
1295
1290
1296
1291
namespace {
@@ -1403,12 +1398,18 @@ std::vector<NumberFormatSpan> FlattenRegionsToParts(
1403
1398
}
1404
1399
1405
1400
namespace {
1406
- Maybe<int > ConstructParts (Isolate* isolate, const icu::UnicodeString& formatted,
1407
- icu::FieldPositionIterator* fp_iter ,
1401
+ Maybe<int > ConstructParts (Isolate* isolate,
1402
+ icu::number::FormattedNumber* formatted ,
1408
1403
Handle <JSArray> result, int start_index,
1409
1404
Handle <Object> numeric_obj, bool style_is_unit) {
1405
+ UErrorCode status = U_ZERO_ERROR;
1406
+ icu::UnicodeString formatted_text = formatted->toString (status);
1407
+ if (U_FAILURE (status)) {
1408
+ THROW_NEW_ERROR_RETURN_VALUE (
1409
+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<int >());
1410
+ }
1410
1411
DCHECK (numeric_obj->IsNumeric ());
1411
- int32_t length = formatted .length ();
1412
+ int32_t length = formatted_text .length ();
1412
1413
int index = start_index;
1413
1414
if (length == 0 ) return Just (index );
1414
1415
@@ -1417,13 +1418,14 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1417
1418
// other region covers some part of the formatted string. It's possible
1418
1419
// there's another field with exactly the same begin and end as this backdrop,
1419
1420
// in which case the backdrop's field_id of -1 will give it lower priority.
1420
- regions.push_back (NumberFormatSpan (-1 , 0 , formatted .length ()));
1421
+ regions.push_back (NumberFormatSpan (-1 , 0 , formatted_text .length ()));
1421
1422
1422
1423
{
1423
- icu::FieldPosition fp;
1424
- while (fp_iter->next (fp)) {
1425
- regions.push_back (NumberFormatSpan (fp.getField (), fp.getBeginIndex (),
1426
- fp.getEndIndex ()));
1424
+ icu::ConstrainedFieldPosition cfp;
1425
+ cfp.constrainCategory (UFIELD_CATEGORY_NUMBER);
1426
+ while (formatted->nextPosition (cfp, status)) {
1427
+ regions.push_back (
1428
+ NumberFormatSpan (cfp.getField (), cfp.getStart (), cfp.getLimit ()));
1427
1429
}
1428
1430
}
1429
1431
@@ -1445,7 +1447,7 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1445
1447
Handle <String> substring;
1446
1448
ASSIGN_RETURN_ON_EXCEPTION_VALUE (
1447
1449
isolate, substring,
1448
- Intl::ToString (isolate, formatted , part.begin_pos , part.end_pos ),
1450
+ Intl::ToString (isolate, formatted_text , part.begin_pos , part.end_pos ),
1449
1451
Nothing<int >());
1450
1452
Intl::AddElement (isolate, result, index , field_type_string, substring);
1451
1453
++index ;
@@ -1465,20 +1467,19 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
1465
1467
number_format->icu_number_formatter ().raw ();
1466
1468
CHECK_NOT_NULL (fmt);
1467
1469
1468
- icu::FieldPositionIterator fp_iter ;
1469
- Maybe<icu::UnicodeString > maybe_format =
1470
- IcuFormatNumber (isolate, *fmt, numeric_obj, &fp_iter );
1470
+ icu::number::FormattedNumber formatted ;
1471
+ Maybe<bool > maybe_format =
1472
+ IcuFormatNumber (isolate, *fmt, numeric_obj, &formatted );
1471
1473
MAYBE_RETURN (maybe_format, Handle <JSArray>());
1472
-
1473
1474
UErrorCode status = U_ZERO_ERROR;
1475
+
1474
1476
bool style_is_unit =
1475
1477
Style ::UNIT == StyleFromSkeleton (fmt->toSkeleton (status));
1476
1478
CHECK (U_SUCCESS (status));
1477
1479
1478
1480
Handle <JSArray> result = factory->NewJSArray (0 );
1479
- Maybe<int > maybe_format_to_parts =
1480
- ConstructParts (isolate, maybe_format.FromJust (), &fp_iter, result, 0 ,
1481
- numeric_obj, style_is_unit);
1481
+ Maybe<int > maybe_format_to_parts = ConstructParts (
1482
+ isolate, &formatted, result, 0 , numeric_obj, style_is_unit);
1482
1483
MAYBE_RETURN (maybe_format_to_parts, Handle <JSArray>());
1483
1484
1484
1485
return result;
0 commit comments