@@ -1257,42 +1257,31 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
1257
1257
}
1258
1258
1259
1259
namespace {
1260
- Maybe<icu::UnicodeString > IcuFormatNumber (
1260
+ Maybe<bool > IcuFormatNumber (
1261
1261
Isolate* isolate,
1262
1262
const icu::number::LocalizedNumberFormatter& number_format,
1263
- Handle<Object> numeric_obj, icu::FieldPositionIterator* fp_iter ) {
1263
+ Handle<Object> numeric_obj, icu::number::FormattedNumber* formatted ) {
1264
1264
// If it is BigInt, handle it differently.
1265
1265
UErrorCode status = U_ZERO_ERROR;
1266
- icu::number::FormattedNumber formatted;
1267
1266
if (numeric_obj->IsBigInt ()) {
1268
1267
Handle<BigInt> big_int = Handle<BigInt>::cast (numeric_obj);
1269
1268
Handle<String> big_int_string;
1270
1269
ASSIGN_RETURN_ON_EXCEPTION_VALUE (isolate, big_int_string,
1271
1270
BigInt::ToString (isolate, big_int),
1272
- Nothing<icu::UnicodeString >());
1273
- formatted = number_format.formatDecimal (
1271
+ Nothing<bool >());
1272
+ * formatted = number_format.formatDecimal (
1274
1273
{big_int_string->ToCString ().get (), big_int_string->length ()}, status);
1275
1274
} else {
1276
1275
double number = numeric_obj->Number ();
1277
- formatted = number_format.formatDouble (number, status);
1276
+ * formatted = number_format.formatDouble (number, status);
1278
1277
}
1279
1278
if (U_FAILURE (status)) {
1280
1279
// This happen because of icu data trimming trim out "unit".
1281
1280
// 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 >());
1288
1283
}
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 );
1296
1285
}
1297
1286
1298
1287
} // namespace
@@ -1303,10 +1292,16 @@ MaybeHandle<String> JSNumberFormat::FormatNumeric(
1303
1292
Handle<Object> numeric_obj) {
1304
1293
DCHECK (numeric_obj->IsNumeric ());
1305
1294
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);
1308
1298
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);
1310
1305
}
1311
1306
1312
1307
namespace {
@@ -1419,12 +1414,18 @@ std::vector<NumberFormatSpan> FlattenRegionsToParts(
1419
1414
}
1420
1415
1421
1416
namespace {
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 ,
1424
1419
Handle<JSArray> result, int start_index,
1425
1420
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
+ }
1426
1427
DCHECK (numeric_obj->IsNumeric ());
1427
- int32_t length = formatted .length ();
1428
+ int32_t length = formatted_text .length ();
1428
1429
int index = start_index;
1429
1430
if (length == 0 ) return Just (index);
1430
1431
@@ -1433,13 +1434,14 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1433
1434
// other region covers some part of the formatted string. It's possible
1434
1435
// there's another field with exactly the same begin and end as this backdrop,
1435
1436
// 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 ()));
1437
1438
1438
1439
{
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 ()));
1443
1445
}
1444
1446
}
1445
1447
@@ -1461,7 +1463,7 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1461
1463
Handle<String> substring;
1462
1464
ASSIGN_RETURN_ON_EXCEPTION_VALUE (
1463
1465
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 ),
1465
1467
Nothing<int >());
1466
1468
Intl::AddElement (isolate, result, index, field_type_string, substring);
1467
1469
++index;
@@ -1481,14 +1483,14 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
1481
1483
number_format->icu_number_formatter ().raw ();
1482
1484
CHECK_NOT_NULL (fmt);
1483
1485
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 );
1487
1489
MAYBE_RETURN (maybe_format, Handle<JSArray>());
1488
1490
1489
1491
Handle<JSArray> result = factory->NewJSArray (0 );
1490
1492
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,
1492
1494
number_format->style () == JSNumberFormat::Style::UNIT);
1493
1495
MAYBE_RETURN (maybe_format_to_parts, Handle<JSArray>());
1494
1496
0 commit comments