@@ -1187,42 +1187,31 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
1187
1187
}
1188
1188
1189
1189
namespace {
1190
- Maybe<icu::UnicodeString > IcuFormatNumber (
1190
+ Maybe<bool > IcuFormatNumber (
1191
1191
Isolate* isolate,
1192
1192
const icu::number::LocalizedNumberFormatter& number_format,
1193
- Handle <Object> numeric_obj, icu::FieldPositionIterator* fp_iter ) {
1193
+ Handle <Object> numeric_obj, icu::number::FormattedNumber* formatted ) {
1194
1194
// If it is BigInt, handle it differently.
1195
1195
UErrorCode status = U_ZERO_ERROR;
1196
- icu::number::FormattedNumber formatted;
1197
1196
if (numeric_obj->IsBigInt ()) {
1198
1197
Handle <BigInt> big_int = Handle <BigInt>::cast (numeric_obj);
1199
1198
Handle <String> big_int_string;
1200
1199
ASSIGN_RETURN_ON_EXCEPTION_VALUE (isolate, big_int_string,
1201
1200
BigInt::ToString (isolate, big_int),
1202
- Nothing<icu::UnicodeString >());
1203
- formatted = number_format.formatDecimal (
1201
+ Nothing<bool >());
1202
+ * formatted = number_format.formatDecimal (
1204
1203
{big_int_string->ToCString ().get (), big_int_string->length ()}, status);
1205
1204
} else {
1206
1205
double number = numeric_obj->Number ();
1207
- formatted = number_format.formatDouble (number, status);
1206
+ * formatted = number_format.formatDouble (number, status);
1208
1207
}
1209
1208
if (U_FAILURE (status)) {
1210
1209
// This happen because of icu data trimming trim out "unit".
1211
1210
// See https://bugs.chromium.org/p/v8/issues/detail?id=8641
1212
- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1213
- NewTypeError (MessageTemplate::kIcuError ),
1214
- Nothing<icu::UnicodeString>());
1215
- }
1216
- if (fp_iter) {
1217
- formatted.getAllFieldPositions (*fp_iter, status);
1211
+ THROW_NEW_ERROR_RETURN_VALUE (
1212
+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<bool >());
1218
1213
}
1219
- icu::UnicodeString result = formatted.toString (status);
1220
- if (U_FAILURE (status)) {
1221
- THROW_NEW_ERROR_RETURN_VALUE (isolate,
1222
- NewTypeError (MessageTemplate::kIcuError ),
1223
- Nothing<icu::UnicodeString>());
1224
- }
1225
- return Just (result);
1214
+ return Just (true );
1226
1215
}
1227
1216
1228
1217
} // namespace
@@ -1233,10 +1222,16 @@ MaybeHandle<String> JSNumberFormat::FormatNumeric(
1233
1222
Handle <Object> numeric_obj) {
1234
1223
DCHECK (numeric_obj->IsNumeric ());
1235
1224
1236
- Maybe<icu::UnicodeString> maybe_format =
1237
- IcuFormatNumber (isolate, number_format, numeric_obj, nullptr );
1225
+ icu::number::FormattedNumber formatted;
1226
+ Maybe<bool > maybe_format =
1227
+ IcuFormatNumber (isolate, number_format, numeric_obj, &formatted);
1238
1228
MAYBE_RETURN (maybe_format, Handle <String>());
1239
- return Intl::ToString (isolate, maybe_format.FromJust ());
1229
+ UErrorCode status = U_ZERO_ERROR;
1230
+ icu::UnicodeString result = formatted.toString (status);
1231
+ if (U_FAILURE (status)) {
1232
+ THROW_NEW_ERROR (isolate, NewTypeError (MessageTemplate::kIcuError ), String);
1233
+ }
1234
+ return Intl::ToString (isolate, result);
1240
1235
}
1241
1236
1242
1237
namespace {
@@ -1349,12 +1344,18 @@ std::vector<NumberFormatSpan> FlattenRegionsToParts(
1349
1344
}
1350
1345
1351
1346
namespace {
1352
- Maybe<int > ConstructParts (Isolate* isolate, const icu::UnicodeString& formatted,
1353
- icu::FieldPositionIterator* fp_iter ,
1347
+ Maybe<int > ConstructParts (Isolate* isolate,
1348
+ icu::number::FormattedNumber* formatted ,
1354
1349
Handle <JSArray> result, int start_index,
1355
1350
Handle <Object> numeric_obj, bool style_is_unit) {
1351
+ UErrorCode status = U_ZERO_ERROR;
1352
+ icu::UnicodeString formatted_text = formatted->toString (status);
1353
+ if (U_FAILURE (status)) {
1354
+ THROW_NEW_ERROR_RETURN_VALUE (
1355
+ isolate, NewTypeError (MessageTemplate::kIcuError ), Nothing<int >());
1356
+ }
1356
1357
DCHECK (numeric_obj->IsNumeric ());
1357
- int32_t length = formatted .length ();
1358
+ int32_t length = formatted_text .length ();
1358
1359
int index = start_index;
1359
1360
if (length == 0 ) return Just (index );
1360
1361
@@ -1363,13 +1364,14 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1363
1364
// other region covers some part of the formatted string. It's possible
1364
1365
// there's another field with exactly the same begin and end as this backdrop,
1365
1366
// in which case the backdrop's field_id of -1 will give it lower priority.
1366
- regions.push_back (NumberFormatSpan (-1 , 0 , formatted .length ()));
1367
+ regions.push_back (NumberFormatSpan (-1 , 0 , formatted_text .length ()));
1367
1368
1368
1369
{
1369
- icu::FieldPosition fp;
1370
- while (fp_iter->next (fp)) {
1371
- regions.push_back (NumberFormatSpan (fp.getField (), fp.getBeginIndex (),
1372
- fp.getEndIndex ()));
1370
+ icu::ConstrainedFieldPosition cfp;
1371
+ cfp.constrainCategory (UFIELD_CATEGORY_NUMBER);
1372
+ while (formatted->nextPosition (cfp, status)) {
1373
+ regions.push_back (
1374
+ NumberFormatSpan (cfp.getField (), cfp.getStart (), cfp.getLimit ()));
1373
1375
}
1374
1376
}
1375
1377
@@ -1391,7 +1393,7 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
1391
1393
Handle <String> substring;
1392
1394
ASSIGN_RETURN_ON_EXCEPTION_VALUE (
1393
1395
isolate, substring,
1394
- Intl::ToString (isolate, formatted , part.begin_pos , part.end_pos ),
1396
+ Intl::ToString (isolate, formatted_text , part.begin_pos , part.end_pos ),
1395
1397
Nothing<int >());
1396
1398
Intl::AddElement (isolate, result, index , field_type_string, substring);
1397
1399
++index ;
@@ -1411,14 +1413,14 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
1411
1413
number_format->icu_number_formatter ().raw ();
1412
1414
CHECK_NOT_NULL (fmt);
1413
1415
1414
- icu::FieldPositionIterator fp_iter ;
1415
- Maybe<icu::UnicodeString > maybe_format =
1416
- IcuFormatNumber (isolate, *fmt, numeric_obj, &fp_iter );
1416
+ icu::number::FormattedNumber formatted ;
1417
+ Maybe<bool > maybe_format =
1418
+ IcuFormatNumber (isolate, *fmt, numeric_obj, &formatted );
1417
1419
MAYBE_RETURN (maybe_format, Handle <JSArray>());
1418
1420
1419
1421
Handle <JSArray> result = factory->NewJSArray (0 );
1420
1422
Maybe<int > maybe_format_to_parts = ConstructParts (
1421
- isolate, maybe_format. FromJust (), &fp_iter , result, 0 , numeric_obj,
1423
+ isolate, &formatted , result, 0 , numeric_obj,
1422
1424
number_format->style () == JSNumberFormat::Style ::UNIT);
1423
1425
MAYBE_RETURN (maybe_format_to_parts, Handle <JSArray>());
1424
1426
0 commit comments