@@ -1197,42 +1197,31 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
1197
1197
}
1198
1198
1199
1199
namespace {
1200
- Maybe<icu::UnicodeString > IcuFormatNumber (
1200
+ Maybe<bool > IcuFormatNumber (
1201
1201
Isolate* isolate,
1202
1202
const icu::number::LocalizedNumberFormatter& number_format,
1203
- Handle <Object> numeric_obj, icu::FieldPositionIterator* fp_iter ) {
1203
+ Handle <Object> numeric_obj, icu::number::FormattedNumber* formatted ) {
1204
1204
// If it is BigInt, handle it differently.
1205
1205
UErrorCode status = U_ZERO_ERROR;
1206
- icu::number::FormattedNumber formatted;
1207
1206
if (numeric_obj->IsBigInt ()) {
1208
1207
Handle <BigInt> big_int = Handle <BigInt>::cast (numeric_obj);
1209
1208
Handle <String> big_int_string;
1210
1209
ASSIGN_RETURN_ON_EXCEPTION_VALUE (isolate, big_int_string,
1211
1210
BigInt::ToString (isolate, big_int),
1212
- Nothing<icu::UnicodeString >());
1213
- formatted = number_format.formatDecimal (
1211
+ Nothing<bool >());
1212
+ * formatted = number_format.formatDecimal (
1214
1213
{big_int_string->ToCString ().get (), big_int_string->length ()}, status);
1215
1214
} else {
1216
1215
double number = numeric_obj->Number ();
1217
- formatted = number_format.formatDouble (number, status);
1216
+ * formatted = number_format.formatDouble (number, status);
1218
1217
}
1219
1218
if (U_FAILURE (status)) {
1220
1219
// This happen because of icu data trimming trim out "unit".
1221
1220
// See https://bugs.chromium.org/p/v8/issues/detail?id=8641
1222
- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1223
- NewTypeError (MessageTemplate::kIcuError ),
1224
- Nothing<icu::UnicodeString>());
1225
- }
1226
- if (fp_iter) {
1227
- formatted.getAllFieldPositions (*fp_iter, status);
1221
+ THROW_NEW_ERROR_RETURN_VALUE (
1222
+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<bool >());
1228
1223
}
1229
- icu::UnicodeString result = formatted.toString (status);
1230
- if (U_FAILURE (status)) {
1231
- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1232
- NewTypeError (MessageTemplate::kIcuError ),
1233
- Nothing<icu::UnicodeString>());
1234
- }
1235
- return Just (result);
1224
+ return Just (true );
1236
1225
}
1237
1226
1238
1227
} // namespace
@@ -1243,10 +1232,16 @@ MaybeHandle<String> JSNumberFormat::FormatNumeric(
1243
1232
Handle <Object> numeric_obj) {
1244
1233
DCHECK (numeric_obj->IsNumeric ());
1245
1234
1246
- Maybe<icu::UnicodeString> maybe_format =
1247
- IcuFormatNumber (isolate, number_format, numeric_obj, nullptr );
1235
+ icu::number::FormattedNumber formatted;
1236
+ Maybe<bool > maybe_format =
1237
+ IcuFormatNumber (isolate, number_format, numeric_obj, &formatted);
1248
1238
MAYBE_RETURN (maybe_format, Handle <String>());
1249
- return Intl::ToString (isolate, maybe_format.FromJust ());
1239
+ UErrorCode status = U_ZERO_ERROR;
1240
+ icu::UnicodeString result = formatted.toString (status);
1241
+ if (U_FAILURE (status)) {
1242
+ THROW_NEW_ERROR (isolate, NewTypeError (MessageTemplate::kIcuError ), String);
1243
+ }
1244
+ return Intl::ToString (isolate, result);
1250
1245
}
1251
1246
1252
1247
namespace {
@@ -1359,12 +1354,18 @@ std::vector<NumberFormatSpan> FlattenRegionsToParts(
1359
1354
}
1360
1355
1361
1356
namespace {
1362
- Maybe<int > ConstructParts (Isolate* isolate, const icu::UnicodeString& formatted,
1363
- icu::FieldPositionIterator* fp_iter ,
1357
+ Maybe<int > ConstructParts (Isolate* isolate,
1358
+ icu::number::FormattedNumber* formatted ,
1364
1359
Handle <JSArray> result, int start_index,
1365
1360
Handle <Object> numeric_obj, bool style_is_unit) {
1361
+ UErrorCode status = U_ZERO_ERROR;
1362
+ icu::UnicodeString formatted_text = formatted->toString (status);
1363
+ if (U_FAILURE (status)) {
1364
+ THROW_NEW_ERROR_RETURN_VALUE (
1365
+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<int >());
1366
+ }
1366
1367
DCHECK (numeric_obj->IsNumeric ());
1367
- int32_t length = formatted .length ();
1368
+ int32_t length = formatted_text .length ();
1368
1369
int index = start_index;
1369
1370
if (length == 0 ) return Just (index );
1370
1371
@@ -1373,13 +1374,14 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1373
1374
// other region covers some part of the formatted string. It's possible
1374
1375
// there's another field with exactly the same begin and end as this backdrop,
1375
1376
// in which case the backdrop's field_id of -1 will give it lower priority.
1376
- regions.push_back (NumberFormatSpan (-1 , 0 , formatted .length ()));
1377
+ regions.push_back (NumberFormatSpan (-1 , 0 , formatted_text .length ()));
1377
1378
1378
1379
{
1379
- icu::FieldPosition fp;
1380
- while (fp_iter->next (fp)) {
1381
- regions.push_back (NumberFormatSpan (fp.getField (), fp.getBeginIndex (),
1382
- fp.getEndIndex ()));
1380
+ icu::ConstrainedFieldPosition cfp;
1381
+ cfp.constrainCategory (UFIELD_CATEGORY_NUMBER);
1382
+ while (formatted->nextPosition (cfp, status)) {
1383
+ regions.push_back (
1384
+ NumberFormatSpan (cfp.getField (), cfp.getStart (), cfp.getLimit ()));
1383
1385
}
1384
1386
}
1385
1387
@@ -1401,7 +1403,7 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1401
1403
Handle <String> substring;
1402
1404
ASSIGN_RETURN_ON_EXCEPTION_VALUE (
1403
1405
isolate, substring,
1404
- Intl::ToString (isolate, formatted , part.begin_pos , part.end_pos ),
1406
+ Intl::ToString (isolate, formatted_text , part.begin_pos , part.end_pos ),
1405
1407
Nothing<int >());
1406
1408
Intl::AddElement (isolate, result, index , field_type_string, substring);
1407
1409
++index ;
@@ -1421,14 +1423,14 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
1421
1423
number_format->icu_number_formatter ().raw ();
1422
1424
CHECK_NOT_NULL (fmt);
1423
1425
1424
- icu::FieldPositionIterator fp_iter ;
1425
- Maybe<icu::UnicodeString > maybe_format =
1426
- IcuFormatNumber (isolate, *fmt, numeric_obj, &fp_iter );
1426
+ icu::number::FormattedNumber formatted ;
1427
+ Maybe<bool > maybe_format =
1428
+ IcuFormatNumber (isolate, *fmt, numeric_obj, &formatted );
1427
1429
MAYBE_RETURN (maybe_format, Handle <JSArray>());
1428
1430
1429
1431
Handle <JSArray> result = factory->NewJSArray (0 );
1430
1432
Maybe<int > maybe_format_to_parts = ConstructParts (
1431
- isolate, maybe_format. FromJust (), &fp_iter , result, 0 , numeric_obj,
1433
+ isolate, &formatted , result, 0 , numeric_obj,
1432
1434
number_format->style () == JSNumberFormat::Style ::UNIT);
1433
1435
MAYBE_RETURN (maybe_format_to_parts, Handle <JSArray>());
1434
1436
0 commit comments