1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
|
/*
* Copyright (C) 2015 Andy VanWagoner (andy@vanwagoner.family)
* Copyright (C) 2016 Sukolsak Sakshuwong (sukolsak@gmail.com)
* Copyright (C) 2016-2023 Apple Inc. All rights reserved.
* Copyright (C) 2020 Sony Interactive Entertainment Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "IntlNumberFormat.h"
#include "Error.h"
#include "IntlNumberFormatInlines.h"
#include "IntlObjectInlines.h"
#include "JSBoundFunction.h"
#include "JSCInlines.h"
#include "ObjectConstructor.h"
#include "ParseInt.h"
#include <wtf/Range.h>
#include <wtf/text/MakeString.h>
#include <wtf/unicode/icu/ICUHelpers.h>
#ifdef U_HIDE_DRAFT_API
#undef U_HIDE_DRAFT_API
#endif
#include <unicode/unumberformatter.h>
#include <unicode/unumberrangeformatter.h>
#define U_HIDE_DRAFT_API 1
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
const ClassInfo IntlNumberFormat::s_info = { "Object"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(IntlNumberFormat) };
namespace IntlNumberFormatInternal {
static constexpr bool verbose = false;
}
void UNumberFormatterDeleter::operator()(UNumberFormatter* formatter)
{
if (formatter)
unumf_close(formatter);
}
void UNumberRangeFormatterDeleter::operator()(UNumberRangeFormatter* formatter)
{
if (formatter)
unumrf_close(formatter);
}
IntlNumberFormat* IntlNumberFormat::create(VM& vm, Structure* structure)
{
IntlNumberFormat* format = new (NotNull, allocateCell<IntlNumberFormat>(vm)) IntlNumberFormat(vm, structure);
format->finishCreation(vm);
return format;
}
Structure* IntlNumberFormat::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
IntlNumberFormat::IntlNumberFormat(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
template<typename Visitor>
void IntlNumberFormat::visitChildrenImpl(JSCell* cell, Visitor& visitor)
{
IntlNumberFormat* thisObject = jsCast<IntlNumberFormat*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
visitor.append(thisObject->m_boundFormat);
}
DEFINE_VISIT_CHILDREN(IntlNumberFormat);
Vector<String> IntlNumberFormat::localeData(const String& locale, RelevantExtensionKey key)
{
// 9.1 Internal slots of Service Constructors & 11.2.3 Internal slots (ECMA-402 2.0)
ASSERT_UNUSED(key, key == RelevantExtensionKey::Nu);
return numberingSystemsForLocale(locale);
}
static inline unsigned computeCurrencySortKey(const String& currency)
{
ASSERT(currency.length() == 3);
ASSERT(currency.containsOnly<isASCIIUpper>());
return (currency[0] << 16) + (currency[1] << 8) + currency[2];
}
static inline unsigned computeCurrencySortKey(const std::array<const char, 3>& currency)
{
ASSERT(containsOnly<isASCIIUpper>(std::span { currency }));
return (currency[0] << 16) + (currency[1] << 8) + currency[2];
}
static unsigned extractCurrencySortKey(std::pair<std::array<const char, 3>, unsigned>* currencyMinorUnit)
{
return computeCurrencySortKey(currencyMinorUnit->first);
}
static unsigned computeCurrencyDigits(const String& currency)
{
// 11.1.1 The abstract operation CurrencyDigits (currency)
// "If the ISO 4217 currency and funds code list contains currency as an alphabetic code,
// then return the minor unit value corresponding to the currency from the list; else return 2.
static constexpr std::pair<std::array<const char, 3>, unsigned> currencyMinorUnits[] = {
{ { 'B', 'H', 'D' }, 3 },
{ { 'B', 'I', 'F' }, 0 },
{ { 'B', 'Y', 'R' }, 0 },
{ { 'C', 'L', 'F' }, 4 },
{ { 'C', 'L', 'P' }, 0 },
{ { 'D', 'J', 'F' }, 0 },
{ { 'G', 'N', 'F' }, 0 },
{ { 'I', 'Q', 'D' }, 3 },
{ { 'I', 'S', 'K' }, 0 },
{ { 'J', 'O', 'D' }, 3 },
{ { 'J', 'P', 'Y' }, 0 },
{ { 'K', 'M', 'F' }, 0 },
{ { 'K', 'R', 'W' }, 0 },
{ { 'K', 'W', 'D' }, 3 },
{ { 'L', 'Y', 'D' }, 3 },
{ { 'O', 'M', 'R' }, 3 },
{ { 'P', 'Y', 'G' }, 0 },
{ { 'R', 'W', 'F' }, 0 },
{ { 'T', 'N', 'D' }, 3 },
{ { 'U', 'G', 'X' }, 0 },
{ { 'U', 'Y', 'I' }, 0 },
{ { 'V', 'N', 'D' }, 0 },
{ { 'V', 'U', 'V' }, 0 },
{ { 'X', 'A', 'F' }, 0 },
{ { 'X', 'O', 'F' }, 0 },
{ { 'X', 'P', 'F' }, 0 }
};
auto* currencyMinorUnit = tryBinarySearch<std::pair<std::array<const char, 3>, unsigned>>(currencyMinorUnits, std::size(currencyMinorUnits), computeCurrencySortKey(currency), extractCurrencySortKey);
if (currencyMinorUnit)
return currencyMinorUnit->second;
return 2;
}
static std::optional<MeasureUnit> sanctionedSimpleUnitIdentifier(StringView unitIdentifier)
{
ASSERT(
std::is_sorted(std::begin(simpleUnits), std::end(simpleUnits),
[](const MeasureUnit& a, const MeasureUnit& b) {
return WTF::codePointCompare(StringView(a.subType), StringView(b.subType)) < 0;
}));
auto iterator = std::lower_bound(std::begin(simpleUnits), std::end(simpleUnits), unitIdentifier,
[](const MeasureUnit& unit, StringView unitIdentifier) {
return WTF::codePointCompare(StringView(unit.subType), unitIdentifier) < 0;
});
if (iterator != std::end(simpleUnits) && iterator->subType == unitIdentifier)
return *iterator;
return std::nullopt;
}
struct WellFormedUnit {
public:
explicit WellFormedUnit(MeasureUnit numerator)
: numerator(numerator)
{
}
WellFormedUnit(MeasureUnit numerator, MeasureUnit denominator)
: numerator(numerator)
, denominator(denominator)
{
}
MeasureUnit numerator;
std::optional<MeasureUnit> denominator;
};
static std::optional<WellFormedUnit> wellFormedUnitIdentifier(StringView unitIdentifier)
{
// https://tc39.es/ecma402/#sec-iswellformedunitidentifier
if (auto unit = sanctionedSimpleUnitIdentifier(unitIdentifier))
return WellFormedUnit(unit.value());
// If the substring "-per-" does not occur exactly once in unitIdentifier, then return false.
auto per = StringView("-per-"_s);
auto position = unitIdentifier.find(per);
if (position == WTF::notFound)
return std::nullopt;
if (unitIdentifier.find(per, position + per.length()) != WTF::notFound)
return std::nullopt;
// If the result of IsSanctionedSimpleUnitIdentifier(numerator) is false, then return false.
auto numerator = unitIdentifier.left(position);
auto numeratorUnit = sanctionedSimpleUnitIdentifier(numerator);
if (!numeratorUnit)
return std::nullopt;
// If the result of IsSanctionedSimpleUnitIdentifier(denominator) is false, then return false.
auto denominator = unitIdentifier.substring(position + per.length());
auto denominatorUnit = sanctionedSimpleUnitIdentifier(denominator);
if (!denominatorUnit)
return std::nullopt;
return WellFormedUnit(numeratorUnit.value(), denominatorUnit.value());
}
// We intentionally avoid using ICU's UNUM_APPROXIMATELY_SIGN_FIELD and define the same value here.
// UNUM_APPROXIMATELY_SIGN_FIELD can be defined in the header after ICU 71. But dylib ICU can be newer while ICU header version is old.
// We can define UNUM_APPROXIMATELY_SIGN_FIELD here so that we can support old ICU header + newer ICU library combination.
static constexpr UNumberFormatFields UNUM_APPROXIMATELY_SIGN_FIELD = static_cast<UNumberFormatFields>(UNUM_COMPACT_FIELD + 1);
static ASCIILiteral partTypeString(UNumberFormatFields field, IntlNumberFormat::Style style, bool sign, IntlMathematicalValue::NumberType type)
{
switch (field) {
case UNUM_INTEGER_FIELD:
switch (type) {
case IntlMathematicalValue::NumberType::NaN:
return "nan"_s;
case IntlMathematicalValue::NumberType::Infinity:
return "infinity"_s;
case IntlMathematicalValue::NumberType::Integer:
return "integer"_s;
}
ASSERT_NOT_REACHED();
return "unknown"_s;
case UNUM_FRACTION_FIELD:
return "fraction"_s;
case UNUM_DECIMAL_SEPARATOR_FIELD:
return "decimal"_s;
case UNUM_EXPONENT_SYMBOL_FIELD:
return "exponentSeparator"_s;
case UNUM_EXPONENT_SIGN_FIELD:
return "exponentMinusSign"_s;
case UNUM_EXPONENT_FIELD:
return "exponentInteger"_s;
case UNUM_GROUPING_SEPARATOR_FIELD:
return "group"_s;
case UNUM_CURRENCY_FIELD:
return "currency"_s;
case UNUM_PERCENT_FIELD:
// If the style is "unit", we should report as unit.
// JSTests/test262/test/intl402/NumberFormat/prototype/formatToParts/percent-en-US.js
return (style == IntlNumberFormat::Style::Unit) ? "unit"_s : "percentSign"_s;
case UNUM_SIGN_FIELD:
return sign ? "minusSign"_s : "plusSign"_s;
case UNUM_MEASURE_UNIT_FIELD:
return "unit"_s;
case UNUM_COMPACT_FIELD:
return "compact"_s;
IGNORE_GCC_WARNINGS_BEGIN("switch")
case UNUM_APPROXIMATELY_SIGN_FIELD:
return "approximatelySign"_s;
IGNORE_GCC_WARNINGS_END
// These should not show up because there is no way to specify them in NumberFormat options.
// If they do, they don't fit well into any of known part types, so consider it an "unknown".
case UNUM_PERMILL_FIELD:
// Any newer additions to the UNumberFormatFields enum should just be considered an "unknown" part.
default:
return "unknown"_s;
}
return "unknown"_s;
}
// https://tc39.github.io/ecma402/#sec-initializenumberformat
void IntlNumberFormat::initializeNumberFormat(JSGlobalObject* globalObject, JSValue locales, JSValue optionsValue)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto requestedLocales = canonicalizeLocaleList(globalObject, locales);
RETURN_IF_EXCEPTION(scope, void());
JSObject* options = intlCoerceOptionsToObject(globalObject, optionsValue);
RETURN_IF_EXCEPTION(scope, void());
ResolveLocaleOptions localeOptions;
LocaleMatcher localeMatcher = intlOption<LocaleMatcher>(globalObject, options, vm.propertyNames->localeMatcher, { { "lookup"_s, LocaleMatcher::Lookup }, { "best fit"_s, LocaleMatcher::BestFit } }, "localeMatcher must be either \"lookup\" or \"best fit\""_s, LocaleMatcher::BestFit);
RETURN_IF_EXCEPTION(scope, void());
String numberingSystem = intlStringOption(globalObject, options, vm.propertyNames->numberingSystem, { }, { }, { });
RETURN_IF_EXCEPTION(scope, void());
if (!numberingSystem.isNull()) {
if (!isUnicodeLocaleIdentifierType(numberingSystem)) {
throwRangeError(globalObject, scope, "numberingSystem is not a well-formed numbering system value"_s);
return;
}
localeOptions[static_cast<unsigned>(RelevantExtensionKey::Nu)] = numberingSystem;
}
const auto& availableLocales = intlNumberFormatAvailableLocales();
auto resolved = resolveLocale(globalObject, availableLocales, requestedLocales, localeMatcher, localeOptions, { RelevantExtensionKey::Nu }, localeData);
m_locale = resolved.locale;
if (m_locale.isEmpty()) {
throwTypeError(globalObject, scope, "failed to initialize NumberFormat due to invalid locale"_s);
return;
}
m_numberingSystem = resolved.extensions[static_cast<unsigned>(RelevantExtensionKey::Nu)];
m_style = intlOption<Style>(globalObject, options, vm.propertyNames->style, { { "decimal"_s, Style::Decimal }, { "percent"_s, Style::Percent }, { "currency"_s, Style::Currency }, { "unit"_s, Style::Unit } }, "style must be either \"decimal\", \"percent\", \"currency\", or \"unit\""_s, Style::Decimal);
RETURN_IF_EXCEPTION(scope, void());
String currency = intlStringOption(globalObject, options, Identifier::fromString(vm, "currency"_s), { }, { }, { });
RETURN_IF_EXCEPTION(scope, void());
if (!currency.isNull()) {
if (!isWellFormedCurrencyCode(currency)) {
throwException(globalObject, scope, createRangeError(globalObject, "currency is not a well-formed currency code"_s));
return;
}
}
unsigned currencyDigits = 0;
if (m_style == Style::Currency) {
if (currency.isNull()) {
throwTypeError(globalObject, scope, "currency must be a string"_s);
return;
}
currency = currency.convertToASCIIUppercase();
m_currency = currency;
currencyDigits = computeCurrencyDigits(currency);
}
if (Options::useMoreCurrencyDisplayChoices())
m_currencyDisplay = intlOption<CurrencyDisplay>(globalObject, options, Identifier::fromString(vm, "currencyDisplay"_s), { { "code"_s, CurrencyDisplay::Code }, { "symbol"_s, CurrencyDisplay::Symbol }, { "formalSymbol"_s, CurrencyDisplay::FormalSymbol }, { "narrowSymbol"_s, CurrencyDisplay::NarrowSymbol }, { "name"_s, CurrencyDisplay::Name }, { "never"_s, CurrencyDisplay::Never } }, "currencyDisplay must be either \"code\", \"symbol\", \"formalSymbol\", \"narrowSymbol\", \"name\" or \"never\""_s, CurrencyDisplay::Symbol);
else
m_currencyDisplay = intlOption<CurrencyDisplay>(globalObject, options, Identifier::fromString(vm, "currencyDisplay"_s), { { "code"_s, CurrencyDisplay::Code }, { "symbol"_s, CurrencyDisplay::Symbol }, { "narrowSymbol"_s, CurrencyDisplay::NarrowSymbol }, { "name"_s, CurrencyDisplay::Name } }, "currencyDisplay must be either \"code\", \"symbol\", or \"name\""_s, CurrencyDisplay::Symbol);
RETURN_IF_EXCEPTION(scope, void());
m_currencySign = intlOption<CurrencySign>(globalObject, options, Identifier::fromString(vm, "currencySign"_s), { { "standard"_s, CurrencySign::Standard }, { "accounting"_s, CurrencySign::Accounting } }, "currencySign must be either \"standard\" or \"accounting\""_s, CurrencySign::Standard);
RETURN_IF_EXCEPTION(scope, void());
String unit = intlStringOption(globalObject, options, Identifier::fromString(vm, "unit"_s), { }, { }, { });
RETURN_IF_EXCEPTION(scope, void());
std::optional<WellFormedUnit> wellFormedUnit;
if (!unit.isNull()) {
wellFormedUnit = wellFormedUnitIdentifier(unit);
if (!wellFormedUnit) {
throwRangeError(globalObject, scope, "unit is not a well-formed unit identifier"_s);
return;
}
m_unit = unit;
} else if (m_style == Style::Unit) {
throwTypeError(globalObject, scope, "unit must be a string"_s);
return;
}
m_unitDisplay = intlOption<UnitDisplay>(globalObject, options, Identifier::fromString(vm, "unitDisplay"_s), { { "short"_s, UnitDisplay::Short }, { "narrow"_s, UnitDisplay::Narrow }, { "long"_s, UnitDisplay::Long } }, "unitDisplay must be either \"short\", \"narrow\", or \"long\""_s, UnitDisplay::Short);
RETURN_IF_EXCEPTION(scope, void());
unsigned minimumFractionDigitsDefault = (m_style == Style::Currency) ? currencyDigits : 0;
unsigned maximumFractionDigitsDefault = (m_style == Style::Currency) ? currencyDigits : (m_style == Style::Percent) ? 0 : 3;
m_notation = intlOption<IntlNotation>(globalObject, options, Identifier::fromString(vm, "notation"_s), { { "standard"_s, IntlNotation::Standard }, { "scientific"_s, IntlNotation::Scientific }, { "engineering"_s, IntlNotation::Engineering }, { "compact"_s, IntlNotation::Compact } }, "notation must be either \"standard\", \"scientific\", \"engineering\", or \"compact\""_s, IntlNotation::Standard);
RETURN_IF_EXCEPTION(scope, void());
setNumberFormatDigitOptions(globalObject, this, options, minimumFractionDigitsDefault, maximumFractionDigitsDefault, m_notation);
RETURN_IF_EXCEPTION(scope, void());
m_compactDisplay = intlOption<CompactDisplay>(globalObject, options, Identifier::fromString(vm, "compactDisplay"_s), { { "short"_s, CompactDisplay::Short }, { "long"_s, CompactDisplay::Long } }, "compactDisplay must be either \"short\" or \"long\""_s, CompactDisplay::Short);
RETURN_IF_EXCEPTION(scope, void());
UseGrouping defaultUseGrouping = UseGrouping::Auto;
if (m_notation == IntlNotation::Compact)
defaultUseGrouping = UseGrouping::Min2;
m_useGrouping = intlStringOrBooleanOption<UseGrouping>(globalObject, options, Identifier::fromString(vm, "useGrouping"_s), UseGrouping::Always, UseGrouping::False, { { "min2"_s, UseGrouping::Min2 }, { "auto"_s, UseGrouping::Auto }, { "always"_s, UseGrouping::Always } }, "useGrouping must be either true, false, \"min2\", \"auto\", or \"always\""_s, defaultUseGrouping);
RETURN_IF_EXCEPTION(scope, void());
m_signDisplay = intlOption<SignDisplay>(globalObject, options, Identifier::fromString(vm, "signDisplay"_s), { { "auto"_s, SignDisplay::Auto }, { "never"_s, SignDisplay::Never }, { "always"_s, SignDisplay::Always }, { "exceptZero"_s, SignDisplay::ExceptZero }, { "negative"_s, SignDisplay::Negative } }, "signDisplay must be either \"auto\", \"never\", \"always\", \"exceptZero\", or \"negative\""_s, SignDisplay::Auto);
RETURN_IF_EXCEPTION(scope, void());
CString dataLocaleWithExtensions = makeString(resolved.dataLocale, "-u-nu-"_s, m_numberingSystem).utf8();
dataLogLnIf(IntlNumberFormatInternal::verbose, "dataLocaleWithExtensions:(", dataLocaleWithExtensions , ")");
// Options are obtained. Configure formatter here.
// Constructing ICU Number Skeletons to configure UNumberFormatter.
// https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md
StringBuilder skeletonBuilder;
switch (m_style) {
case Style::Decimal:
// No skeleton is needed.
break;
case Style::Percent:
skeletonBuilder.append(" percent scale/100"_s);
break;
case Style::Currency: {
skeletonBuilder.append(" currency/"_s, currency);
// https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#unit-width
switch (m_currencyDisplay) {
case CurrencyDisplay::Code:
skeletonBuilder.append(" unit-width-iso-code"_s);
break;
case CurrencyDisplay::Symbol:
// Default option. Do not specify unit-width.
break;
case CurrencyDisplay::FormalSymbol:
ASSERT(Options::useMoreCurrencyDisplayChoices());
skeletonBuilder.append(" unit-width-formal"_s);
break;
case CurrencyDisplay::NarrowSymbol:
skeletonBuilder.append(" unit-width-narrow"_s);
break;
case CurrencyDisplay::Name:
skeletonBuilder.append(" unit-width-full-name"_s);
break;
case CurrencyDisplay::Never:
ASSERT(Options::useMoreCurrencyDisplayChoices());
skeletonBuilder.append(" unit-width-hidden"_s);
break;
}
break;
}
case Style::Unit: {
// The measure-unit stem takes one required option: the unit identifier of the unit to be formatted.
// The full unit identifier is required: both the type and the subtype (for example, length-meter).
// https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#unit
skeletonBuilder.append(" measure-unit/"_s);
auto numeratorUnit = wellFormedUnit->numerator;
skeletonBuilder.append(numeratorUnit.type, '-', numeratorUnit.subType);
if (auto denominatorUnitValue = wellFormedUnit->denominator) {
auto denominatorUnit = denominatorUnitValue.value();
skeletonBuilder.append(" per-measure-unit/"_s, denominatorUnit.type, '-', denominatorUnit.subType);
}
// https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#unit-width
switch (m_unitDisplay) {
case UnitDisplay::Short:
skeletonBuilder.append(" unit-width-short"_s);
break;
case UnitDisplay::Narrow:
skeletonBuilder.append(" unit-width-narrow"_s);
break;
case UnitDisplay::Long:
skeletonBuilder.append(" unit-width-full-name"_s);
break;
}
break;
}
}
appendNumberFormatDigitOptionsToSkeleton(this, skeletonBuilder);
// https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#notation
switch (m_notation) {
case IntlNotation::Standard:
break;
case IntlNotation::Scientific:
skeletonBuilder.append(" scientific"_s);
break;
case IntlNotation::Engineering:
skeletonBuilder.append(" engineering"_s);
break;
case IntlNotation::Compact:
switch (m_compactDisplay) {
case CompactDisplay::Short:
skeletonBuilder.append(" compact-short"_s);
break;
case CompactDisplay::Long:
skeletonBuilder.append(" compact-long"_s);
break;
}
break;
}
// https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#sign-display
// CurrencySign's accounting is a part of SignDisplay in ICU.
bool useAccounting = (m_style == Style::Currency && m_currencySign == CurrencySign::Accounting);
switch (m_signDisplay) {
case SignDisplay::Auto:
if (useAccounting)
skeletonBuilder.append(" sign-accounting"_s);
else
skeletonBuilder.append(" sign-auto"_s);
break;
case SignDisplay::Never:
skeletonBuilder.append(" sign-never"_s);
break;
case SignDisplay::Always:
if (useAccounting)
skeletonBuilder.append(" sign-accounting-always"_s);
else
skeletonBuilder.append(" sign-always"_s);
break;
case SignDisplay::ExceptZero:
if (useAccounting)
skeletonBuilder.append(" sign-accounting-except-zero"_s);
else
skeletonBuilder.append(" sign-except-zero"_s);
break;
case SignDisplay::Negative:
if (useAccounting)
skeletonBuilder.append(" sign-accounting-negative"_s);
else
skeletonBuilder.append(" sign-negative"_s);
break;
}
// https://github.com/tc39/proposal-intl-numberformat-v3/issues/3
// https://github.com/unicode-org/icu/blob/main/docs/userguide/format_parse/numbers/skeletons.md#grouping
switch (m_useGrouping) {
case UseGrouping::False:
skeletonBuilder.append(" group-off"_s);
break;
case UseGrouping::Min2:
skeletonBuilder.append(" group-min2"_s);
break;
case UseGrouping::Auto:
skeletonBuilder.append(" group-auto"_s);
break;
case UseGrouping::Always:
skeletonBuilder.append(" group-on-aligned"_s);
break;
}
String skeleton = skeletonBuilder.toString();
dataLogLnIf(IntlNumberFormatInternal::verbose, skeleton);
StringView skeletonView(skeleton);
auto upconverted = skeletonView.upconvertedCharacters();
UErrorCode status = U_ZERO_ERROR;
m_numberFormatter = std::unique_ptr<UNumberFormatter, UNumberFormatterDeleter>(unumf_openForSkeletonAndLocale(upconverted.get(), skeletonView.length(), dataLocaleWithExtensions.data(), &status));
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to initialize NumberFormat"_s);
return;
}
m_numberRangeFormatter = std::unique_ptr<UNumberRangeFormatter, UNumberRangeFormatterDeleter>(unumrf_openForSkeletonWithCollapseAndIdentityFallback(upconverted.get(), skeletonView.length(), UNUM_RANGE_COLLAPSE_AUTO, UNUM_IDENTITY_FALLBACK_APPROXIMATELY, dataLocaleWithExtensions.data(), nullptr, &status));
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "failed to initialize NumberFormat"_s);
return;
}
}
// https://tc39.es/ecma402/#sec-formatnumber
JSValue IntlNumberFormat::format(JSGlobalObject* globalObject, double value) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
value = purifyNaN(value);
Vector<UChar, 32> buffer;
ASSERT(m_numberFormatter);
UErrorCode status = U_ZERO_ERROR;
auto formattedNumber = std::unique_ptr<UFormattedNumber, ICUDeleter<unumf_closeResult>>(unumf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
unumf_formatDouble(m_numberFormatter.get(), value, formattedNumber.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
status = callBufferProducingFunction(unumf_resultToString, formattedNumber.get(), buffer);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
return jsString(vm, String(WTFMove(buffer)));
}
// https://tc39.es/ecma402/#sec-formatnumber
JSValue IntlNumberFormat::format(JSGlobalObject* globalObject, IntlMathematicalValue&& value) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
value.ensureNonDouble();
const auto& string = value.getString();
Vector<UChar, 32> buffer;
ASSERT(m_numberFormatter);
UErrorCode status = U_ZERO_ERROR;
auto formattedNumber = std::unique_ptr<UFormattedNumber, ICUDeleter<unumf_closeResult>>(unumf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a BigInt."_s);
unumf_formatDecimal(m_numberFormatter.get(), string.data(), string.length(), formattedNumber.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a BigInt."_s);
status = callBufferProducingFunction(unumf_resultToString, formattedNumber.get(), buffer);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a BigInt."_s);
return jsString(vm, String(WTFMove(buffer)));
}
JSValue IntlNumberFormat::formatRange(JSGlobalObject* globalObject, double start, double end) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(m_numberRangeFormatter);
if (std::isnan(start) || std::isnan(end))
return throwRangeError(globalObject, scope, "Passed numbers are out of range"_s);
UErrorCode status = U_ZERO_ERROR;
auto range = std::unique_ptr<UFormattedNumberRange, ICUDeleter<unumrf_closeResult>>(unumrf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
unumrf_formatDoubleRange(m_numberRangeFormatter.get(), start, end, range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
auto* formattedValue = unumrf_resultAsValue(range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
int32_t length = 0;
const UChar* string = ufmtval_getString(formattedValue, &length, &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
return jsString(vm, String({ string, static_cast<size_t>(length) }));
}
JSValue IntlNumberFormat::formatRange(JSGlobalObject* globalObject, IntlMathematicalValue&& start, IntlMathematicalValue&& end) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(m_numberRangeFormatter);
if (start.numberType() == IntlMathematicalValue::NumberType::NaN || end.numberType() == IntlMathematicalValue::NumberType::NaN)
return throwRangeError(globalObject, scope, "Passed numbers are out of range"_s);
start.ensureNonDouble();
const auto& startString = start.getString();
end.ensureNonDouble();
const auto& endString = end.getString();
UErrorCode status = U_ZERO_ERROR;
auto range = std::unique_ptr<UFormattedNumberRange, ICUDeleter<unumrf_closeResult>>(unumrf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
unumrf_formatDecimalRange(m_numberRangeFormatter.get(), startString.data(), startString.length(), endString.data(), endString.length(), range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
auto* formattedValue = unumrf_resultAsValue(range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
int32_t length = 0;
const UChar* string = ufmtval_getString(formattedValue, &length, &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
return jsString(vm, String({ string, static_cast<size_t>(length) }));
}
static constexpr int32_t literalField = -1;
struct IntlNumberFormatField {
int32_t m_field;
WTF::Range<int32_t> m_range;
};
static Vector<IntlNumberFormatField> flattenFields(Vector<IntlNumberFormatField>&& fields, int32_t formattedStringLength)
{
// ICU generates sequence of nested fields, but ECMA402 requires non-overlapping sequence of parts.
// This function flattens nested fields into sequence of non-overlapping parts.
//
// Formatted string: "100,000–20,000,000"
// | | | | | | |
// | B | | E F |
// | | | |
// +--C--+ +---G----+
// +--A--+ +---D----+
//
// Ranges ICU generates:
// A: (0, 7) UFIELD_CATEGORY_NUMBER_RANGE_SPAN startRange
// B: (3, 4) UFIELD_CATEGORY_NUMBER group ","
// C: (0, 7) UFIELD_CATEGORY_NUMBER integer
// D: (8, 18) UFIELD_CATEGORY_NUMBER_RANGE_SPAN endRange
// E: (10, 11) UFIELD_CATEGORY_NUMBER group ","
// F: (14, 15) UFIELD_CATEGORY_NUMBER group ","
// G: (8, 18) UFIELD_CATEGORY_NUMBER integer
//
// Then, we need to generate:
// A: (0, 3) startRange integer
// B: (3, 4) startRange group ","
// C: (4, 7) startRange integer
// D: (7, 8) shared literal "-"
// E: (8, 10) endRange integer
// F: (10, 11) endRange group ","
// G: (11, 14) endRange integer
// H: (14, 15) endRange group ","
// I: (15, 18) endRange integer
std::sort(fields.begin(), fields.end(), [](auto& lhs, auto& rhs) {
if (lhs.m_range.begin() < rhs.m_range.begin())
return true;
if (lhs.m_range.begin() > rhs.m_range.begin())
return false;
if (lhs.m_range.end() < rhs.m_range.end())
return false;
if (lhs.m_range.end() > rhs.m_range.end())
return true;
return lhs.m_field < rhs.m_field;
});
Vector<IntlNumberFormatField> flatten;
Vector<IntlNumberFormatField> stack;
// Top-level field covers entire parts, which makes parts "literal".
stack.append(IntlNumberFormatField { literalField, { 0, formattedStringLength } });
unsigned cursor = 0;
int32_t begin = 0;
while (cursor < fields.size()) {
const auto& field = fields[cursor];
// If the new field is out of the current top-most field, roll up and insert a flatten field.
// Because the top-level field in the stack covers all index range, this condition always becomes false
// if stack size is 1.
while (stack.last().m_range.end() < field.m_range.begin()) {
if (begin < stack.last().m_range.end()) {
IntlNumberFormatField flattenField { stack.last().m_field, { begin, stack.last().m_range.end() } };
flatten.append(flattenField);
begin = flattenField.m_range.end();
}
stack.removeLast();
}
ASSERT(!stack.isEmpty()); // At least, top-level field exists.
// If the new field is starting with the same index, diving into the new field by adding it into stack.
if (begin == field.m_range.begin()) {
stack.append(field);
++cursor;
continue;
}
// If there is a room between the current top-most field and the new field, insert a flatten field.
if (begin < field.m_range.begin()) {
IntlNumberFormatField flattenField { stack.last().m_field, { begin, field.m_range.begin() } };
flatten.append(flattenField);
stack.append(field);
begin = field.m_range.begin();
++cursor;
continue;
}
}
// Roll up the nested field at the end of the formatted string sequence.
// For example,
//
// <------------A-------------->
// <--------B------------>
// <---C---->
//
// Then, after C finishes, we should insert remaining B and A.
while (!stack.isEmpty()) {
if (begin < stack.last().m_range.end()) {
IntlNumberFormatField flattenField { stack.last().m_field, { begin, stack.last().m_range.end() } };
flatten.append(flattenField);
begin = flattenField.m_range.end();
}
stack.removeLast();
}
return flatten;
}
static bool numberFieldsPracticallyEqual(const UFormattedValue* formattedValue, UErrorCode& status)
{
auto iterator = std::unique_ptr<UConstrainedFieldPosition, ICUDeleter<ucfpos_close>>(ucfpos_open(&status));
if (U_FAILURE(status))
return false;
// We only care about UFIELD_CATEGORY_NUMBER_RANGE_SPAN category.
ucfpos_constrainCategory(iterator.get(), UFIELD_CATEGORY_NUMBER_RANGE_SPAN, &status);
if (U_FAILURE(status))
return false;
bool hasSpan = ufmtval_nextPosition(formattedValue, iterator.get(), &status);
if (U_FAILURE(status))
return false;
return !hasSpan;
}
void IntlNumberFormat::formatRangeToPartsInternal(JSGlobalObject* globalObject, Style style, IntlMathematicalValue&& start, IntlMathematicalValue&& end, const UFormattedValue* formattedValue, JSArray* parts)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
UErrorCode status = U_ZERO_ERROR;
int32_t formattedStringLength = 0;
const UChar* formattedStringPointer = ufmtval_getString(formattedValue, &formattedStringLength, &status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number range"_s);
return;
}
StringView resultStringView(std::span(formattedStringPointer, formattedStringLength));
// We care multiple categories (UFIELD_CATEGORY_DATE and UFIELD_CATEGORY_DATE_INTERVAL_SPAN).
// So we do not constraint iterator.
auto iterator = std::unique_ptr<UConstrainedFieldPosition, ICUDeleter<ucfpos_close>>(ucfpos_open(&status));
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number range"_s);
return;
}
auto sharedString = jsNontrivialString(vm, "shared"_s);
auto startRangeString = jsNontrivialString(vm, "startRange"_s);
auto endRangeString = jsNontrivialString(vm, "endRange"_s);
auto literalString = jsNontrivialString(vm, "literal"_s);
WTF::Range<int32_t> startRange { -1, -1 };
WTF::Range<int32_t> endRange { -1, -1 };
Vector<IntlNumberFormatField> fields;
while (true) {
bool next = ufmtval_nextPosition(formattedValue, iterator.get(), &status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number range"_s);
return;
}
if (!next)
break;
int32_t category = ucfpos_getCategory(iterator.get(), &status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number range"_s);
return;
}
int32_t fieldType = ucfpos_getField(iterator.get(), &status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number range"_s);
return;
}
int32_t beginIndex = 0;
int32_t endIndex = 0;
ucfpos_getIndexes(iterator.get(), &beginIndex, &endIndex, &status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number interval"_s);
return;
}
dataLogLnIf(IntlNumberFormatInternal::verbose, category, " ", fieldType, " (", beginIndex, ", ", endIndex, ")");
if (category != UFIELD_CATEGORY_NUMBER && category != UFIELD_CATEGORY_NUMBER_RANGE_SPAN)
continue;
if (category == UFIELD_CATEGORY_NUMBER && fieldType < 0)
continue;
if (category == UFIELD_CATEGORY_NUMBER_RANGE_SPAN) {
// > The special field category UFIELD_CATEGORY_NUMBER_RANGE_SPAN is used to indicate which number
// > primitives came from which arguments: 0 means start, and 1 means end. The span category
// > will always occur before the corresponding fields in UFIELD_CATEGORY_NUMBER in the nextPosition() iterator.
// from ICU comment. So, field 0 is startRange, field 1 is endRange.
if (!fieldType)
startRange = WTF::Range<int32_t>(beginIndex, endIndex);
else {
ASSERT(fieldType == 1);
endRange = WTF::Range<int32_t>(beginIndex, endIndex);
}
continue;
}
ASSERT(category == UFIELD_CATEGORY_NUMBER);
fields.append(IntlNumberFormatField { fieldType, { beginIndex, endIndex } });
}
auto flatten = flattenFields(WTFMove(fields), formattedStringLength);
auto createPart = [&] (JSString* type, int32_t beginIndex, int32_t length) {
auto sourceType = [&](int32_t index) -> JSString* {
if (startRange.contains(index))
return startRangeString;
if (endRange.contains(index))
return endRangeString;
return sharedString;
};
auto value = jsString(vm, resultStringView.substring(beginIndex, length));
JSObject* part = constructEmptyObject(globalObject);
part->putDirect(vm, vm.propertyNames->type, type);
part->putDirect(vm, vm.propertyNames->value, value);
part->putDirect(vm, vm.propertyNames->source, sourceType(beginIndex));
return part;
};
for (auto& field : flatten) {
bool sign = false;
IntlMathematicalValue::NumberType numberType = start.numberType();
if (startRange.contains(field.m_range.begin())) {
numberType = start.numberType();
sign = start.sign();
} else {
numberType = end.numberType();
sign = end.sign();
}
auto fieldType = field.m_field;
auto partType = fieldType == literalField ? literalString : jsNontrivialString(vm, partTypeString(UNumberFormatFields(fieldType), style, sign, numberType));
JSObject* part = createPart(partType, field.m_range.begin(), field.m_range.distance());
parts->push(globalObject, part);
RETURN_IF_EXCEPTION(scope, void());
}
}
JSValue IntlNumberFormat::formatRangeToParts(JSGlobalObject* globalObject, double start, double end) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(m_numberRangeFormatter);
if (std::isnan(start) || std::isnan(end))
return throwRangeError(globalObject, scope, "Passed numbers are out of range"_s);
UErrorCode status = U_ZERO_ERROR;
auto range = std::unique_ptr<UFormattedNumberRange, ICUDeleter<unumrf_closeResult>>(unumrf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
unumrf_formatDoubleRange(m_numberRangeFormatter.get(), start, end, range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
auto* formattedValue = unumrf_resultAsValue(range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
// After ICU 71, approximatelySign is supported. We use the old path only for < 71.
if (WTF::ICU::majorVersion() < 71) {
bool equal = numberFieldsPracticallyEqual(formattedValue, status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number range"_s);
return { };
}
if (equal)
RELEASE_AND_RETURN(scope, formatToParts(globalObject, start, jsNontrivialString(vm, "shared"_s)));
}
JSArray* parts = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), 0);
if (!parts) {
throwOutOfMemoryError(globalObject, scope);
return { };
}
formatRangeToPartsInternal(globalObject, m_style, IntlMathematicalValue(start), IntlMathematicalValue(end), formattedValue, parts);
RETURN_IF_EXCEPTION(scope, { });
return parts;
}
JSValue IntlNumberFormat::formatRangeToParts(JSGlobalObject* globalObject, IntlMathematicalValue&& start, IntlMathematicalValue&& end) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(m_numberRangeFormatter);
if (start.numberType() == IntlMathematicalValue::NumberType::NaN || end.numberType() == IntlMathematicalValue::NumberType::NaN)
return throwRangeError(globalObject, scope, "Passed numbers are out of range"_s);
start.ensureNonDouble();
const auto& startString = start.getString();
end.ensureNonDouble();
const auto& endString = end.getString();
UErrorCode status = U_ZERO_ERROR;
auto range = std::unique_ptr<UFormattedNumberRange, ICUDeleter<unumrf_closeResult>>(unumrf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
unumrf_formatDecimalRange(m_numberRangeFormatter.get(), startString.data(), startString.length(), endString.data(), endString.length(), range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
auto* formattedValue = unumrf_resultAsValue(range.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to format a range"_s);
// After ICU 71, approximatelySign is supported. We use the old path only for < 71.
if (WTF::ICU::majorVersion() < 71) {
bool equal = numberFieldsPracticallyEqual(formattedValue, status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to format number range"_s);
return { };
}
if (equal)
RELEASE_AND_RETURN(scope, formatToParts(globalObject, WTFMove(start), jsNontrivialString(vm, "shared"_s)));
}
JSArray* parts = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), 0);
if (!parts) {
throwOutOfMemoryError(globalObject, scope);
return { };
}
formatRangeToPartsInternal(globalObject, m_style, WTFMove(start), WTFMove(end), formattedValue, parts);
RETURN_IF_EXCEPTION(scope, { });
return parts;
}
ASCIILiteral IntlNumberFormat::styleString(Style style)
{
switch (style) {
case Style::Decimal:
return "decimal"_s;
case Style::Percent:
return "percent"_s;
case Style::Currency:
return "currency"_s;
case Style::Unit:
return "unit"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::currencyDisplayString(CurrencyDisplay currencyDisplay)
{
switch (currencyDisplay) {
case CurrencyDisplay::Code:
return "code"_s;
case CurrencyDisplay::Symbol:
return "symbol"_s;
case CurrencyDisplay::NarrowSymbol:
return "narrowSymbol"_s;
case CurrencyDisplay::Name:
return "name"_s;
case CurrencyDisplay::FormalSymbol:
ASSERT(Options::useMoreCurrencyDisplayChoices());
return "formalSymbol"_s;
case CurrencyDisplay::Never:
ASSERT(Options::useMoreCurrencyDisplayChoices());
return "never"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::notationString(IntlNotation notation)
{
switch (notation) {
case IntlNotation::Standard:
return "standard"_s;
case IntlNotation::Scientific:
return "scientific"_s;
case IntlNotation::Engineering:
return "engineering"_s;
case IntlNotation::Compact:
return "compact"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::currencySignString(CurrencySign currencySign)
{
switch (currencySign) {
case CurrencySign::Standard:
return "standard"_s;
case CurrencySign::Accounting:
return "accounting"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::unitDisplayString(UnitDisplay unitDisplay)
{
switch (unitDisplay) {
case UnitDisplay::Short:
return "short"_s;
case UnitDisplay::Narrow:
return "narrow"_s;
case UnitDisplay::Long:
return "long"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::compactDisplayString(CompactDisplay compactDisplay)
{
switch (compactDisplay) {
case CompactDisplay::Short:
return "short"_s;
case CompactDisplay::Long:
return "long"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::signDisplayString(SignDisplay signDisplay)
{
switch (signDisplay) {
case SignDisplay::Auto:
return "auto"_s;
case SignDisplay::Never:
return "never"_s;
case SignDisplay::Always:
return "always"_s;
case SignDisplay::ExceptZero:
return "exceptZero"_s;
case SignDisplay::Negative:
return "negative"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::roundingModeString(RoundingMode roundingMode)
{
switch (roundingMode) {
case RoundingMode::Ceil:
return "ceil"_s;
case RoundingMode::Floor:
return "floor"_s;
case RoundingMode::Expand:
return "expand"_s;
case RoundingMode::Trunc:
return "trunc"_s;
case RoundingMode::HalfCeil:
return "halfCeil"_s;
case RoundingMode::HalfFloor:
return "halfFloor"_s;
case RoundingMode::HalfExpand:
return "halfExpand"_s;
case RoundingMode::HalfTrunc:
return "halfTrunc"_s;
case RoundingMode::HalfEven:
return "halfEven"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::trailingZeroDisplayString(IntlTrailingZeroDisplay trailingZeroDisplay)
{
switch (trailingZeroDisplay) {
case IntlTrailingZeroDisplay::Auto:
return "auto"_s;
case IntlTrailingZeroDisplay::StripIfInteger:
return "stripIfInteger"_s;
}
ASSERT_NOT_REACHED();
return { };
}
ASCIILiteral IntlNumberFormat::roundingPriorityString(IntlRoundingType roundingType)
{
switch (roundingType) {
case IntlRoundingType::FractionDigits:
case IntlRoundingType::SignificantDigits:
return "auto"_s;
case IntlRoundingType::MorePrecision:
return "morePrecision"_s;
case IntlRoundingType::LessPrecision:
return "lessPrecision"_s;
}
ASSERT_NOT_REACHED();
return { };
}
JSValue IntlNumberFormat::useGroupingValue(VM& vm, UseGrouping useGrouping)
{
switch (useGrouping) {
case UseGrouping::False:
return jsBoolean(false);
case UseGrouping::Min2:
return jsNontrivialString(vm, "min2"_s);
case UseGrouping::Auto:
return jsNontrivialString(vm, "auto"_s);
case UseGrouping::Always:
return jsNontrivialString(vm, "always"_s);
}
return jsUndefined();
}
// https://tc39.es/ecma402/#sec-intl.numberformat.prototype.resolvedoptions
JSObject* IntlNumberFormat::resolvedOptions(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
JSObject* options = constructEmptyObject(globalObject);
options->putDirect(vm, vm.propertyNames->locale, jsString(vm, m_locale));
options->putDirect(vm, vm.propertyNames->numberingSystem, jsString(vm, m_numberingSystem));
options->putDirect(vm, vm.propertyNames->style, jsNontrivialString(vm, styleString(m_style)));
switch (m_style) {
case Style::Decimal:
case Style::Percent:
break;
case Style::Currency:
options->putDirect(vm, Identifier::fromString(vm, "currency"_s), jsNontrivialString(vm, m_currency));
options->putDirect(vm, Identifier::fromString(vm, "currencyDisplay"_s), jsNontrivialString(vm, currencyDisplayString(m_currencyDisplay)));
options->putDirect(vm, Identifier::fromString(vm, "currencySign"_s), jsNontrivialString(vm, currencySignString(m_currencySign)));
break;
case Style::Unit:
options->putDirect(vm, Identifier::fromString(vm, "unit"_s), jsNontrivialString(vm, m_unit));
options->putDirect(vm, Identifier::fromString(vm, "unitDisplay"_s), jsNontrivialString(vm, unitDisplayString(m_unitDisplay)));
break;
}
options->putDirect(vm, vm.propertyNames->minimumIntegerDigits, jsNumber(m_minimumIntegerDigits));
switch (m_roundingType) {
case IntlRoundingType::FractionDigits:
options->putDirect(vm, vm.propertyNames->minimumFractionDigits, jsNumber(m_minimumFractionDigits));
options->putDirect(vm, vm.propertyNames->maximumFractionDigits, jsNumber(m_maximumFractionDigits));
break;
case IntlRoundingType::SignificantDigits:
options->putDirect(vm, vm.propertyNames->minimumSignificantDigits, jsNumber(m_minimumSignificantDigits));
options->putDirect(vm, vm.propertyNames->maximumSignificantDigits, jsNumber(m_maximumSignificantDigits));
break;
case IntlRoundingType::MorePrecision:
case IntlRoundingType::LessPrecision:
options->putDirect(vm, vm.propertyNames->minimumFractionDigits, jsNumber(m_minimumFractionDigits));
options->putDirect(vm, vm.propertyNames->maximumFractionDigits, jsNumber(m_maximumFractionDigits));
options->putDirect(vm, vm.propertyNames->minimumSignificantDigits, jsNumber(m_minimumSignificantDigits));
options->putDirect(vm, vm.propertyNames->maximumSignificantDigits, jsNumber(m_maximumSignificantDigits));
break;
}
options->putDirect(vm, Identifier::fromString(vm, "useGrouping"_s), useGroupingValue(vm, m_useGrouping));
options->putDirect(vm, Identifier::fromString(vm, "notation"_s), jsNontrivialString(vm, notationString(m_notation)));
if (m_notation == IntlNotation::Compact)
options->putDirect(vm, Identifier::fromString(vm, "compactDisplay"_s), jsNontrivialString(vm, compactDisplayString(m_compactDisplay)));
options->putDirect(vm, Identifier::fromString(vm, "signDisplay"_s), jsNontrivialString(vm, signDisplayString(m_signDisplay)));
options->putDirect(vm, vm.propertyNames->roundingIncrement, jsNumber(m_roundingIncrement));
options->putDirect(vm, vm.propertyNames->roundingMode, jsNontrivialString(vm, roundingModeString(m_roundingMode)));
options->putDirect(vm, vm.propertyNames->roundingPriority, jsNontrivialString(vm, roundingPriorityString(m_roundingType)));
options->putDirect(vm, vm.propertyNames->trailingZeroDisplay, jsNontrivialString(vm, trailingZeroDisplayString(m_trailingZeroDisplay)));
return options;
}
void IntlNumberFormat::setBoundFormat(VM& vm, JSBoundFunction* format)
{
m_boundFormat.set(vm, this, format);
}
void IntlNumberFormat::formatToPartsInternal(JSGlobalObject* globalObject, Style style, bool sign, IntlMathematicalValue::NumberType numberType, const String& formatted, IntlFieldIterator& iterator, JSArray* parts, JSString* sourceType, JSString* unit)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto stringLength = formatted.length();
Vector<IntlNumberFormatField> fields;
while (true) {
int32_t beginIndex = 0;
int32_t endIndex = 0;
UErrorCode status = U_ZERO_ERROR;
int32_t fieldType = iterator.next(beginIndex, endIndex, status);
if (U_FAILURE(status)) {
throwTypeError(globalObject, scope, "Failed to iterate field position iterator"_s);
return;
}
if (fieldType < 0)
break;
fields.append(IntlNumberFormatField { fieldType, { beginIndex, endIndex } });
}
auto flatten = flattenFields(WTFMove(fields), stringLength);
auto literalString = jsNontrivialString(vm, "literal"_s);
Identifier unitName;
if (unit)
unitName = Identifier::fromString(vm, "unit"_s);
for (auto& field : flatten) {
auto fieldType = field.m_field;
auto partType = fieldType == literalField ? literalString : jsNontrivialString(vm, partTypeString(UNumberFormatFields(fieldType), style, sign, numberType));
auto partValue = jsSubstring(vm, formatted, field.m_range.begin(), field.m_range.distance());
JSObject* part = constructEmptyObject(globalObject);
part->putDirect(vm, vm.propertyNames->type, partType);
part->putDirect(vm, vm.propertyNames->value, partValue);
if (unit)
part->putDirect(vm, unitName, unit);
if (sourceType)
part->putDirect(vm, vm.propertyNames->source, sourceType);
parts->push(globalObject, part);
RETURN_IF_EXCEPTION(scope, void());
}
}
// https://tc39.github.io/ecma402/#sec-formatnumbertoparts
JSValue IntlNumberFormat::formatToParts(JSGlobalObject* globalObject, double value, JSString* sourceType) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
value = purifyNaN(value);
UErrorCode status = U_ZERO_ERROR;
auto fieldItr = std::unique_ptr<UFieldPositionIterator, UFieldPositionIteratorDeleter>(ufieldpositer_open(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to open field position iterator"_s);
Vector<UChar, 32> result;
ASSERT(m_numberFormatter);
auto formattedNumber = std::unique_ptr<UFormattedNumber, ICUDeleter<unumf_closeResult>>(unumf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
unumf_formatDouble(m_numberFormatter.get(), value, formattedNumber.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
status = callBufferProducingFunction(unumf_resultToString, formattedNumber.get(), result);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
unumf_resultGetAllFieldPositions(formattedNumber.get(), fieldItr.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
IntlFieldIterator iterator(*fieldItr.get());
auto resultString = String(WTFMove(result));
JSArray* parts = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), 0);
if (!parts)
return throwOutOfMemoryError(globalObject, scope);
formatToPartsInternal(globalObject, m_style, std::signbit(value), IntlMathematicalValue::numberTypeFromDouble(value), resultString, iterator, parts, sourceType, nullptr);
RETURN_IF_EXCEPTION(scope, { });
return parts;
}
JSValue IntlNumberFormat::formatToParts(JSGlobalObject* globalObject, IntlMathematicalValue&& value, JSString* sourceType) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
value.ensureNonDouble();
const auto& string = value.getString();
UErrorCode status = U_ZERO_ERROR;
auto fieldItr = std::unique_ptr<UFieldPositionIterator, UFieldPositionIteratorDeleter>(ufieldpositer_open(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "failed to open field position iterator"_s);
Vector<UChar, 32> result;
ASSERT(m_numberFormatter);
auto formattedNumber = std::unique_ptr<UFormattedNumber, ICUDeleter<unumf_closeResult>>(unumf_openResult(&status));
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
unumf_formatDecimal(m_numberFormatter.get(), string.data(), string.length(), formattedNumber.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
status = callBufferProducingFunction(unumf_resultToString, formattedNumber.get(), result);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
unumf_resultGetAllFieldPositions(formattedNumber.get(), fieldItr.get(), &status);
if (U_FAILURE(status))
return throwTypeError(globalObject, scope, "Failed to format a number."_s);
IntlFieldIterator iterator(*fieldItr.get());
auto resultString = String(WTFMove(result));
JSArray* parts = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), 0);
if (!parts)
return throwOutOfMemoryError(globalObject, scope);
formatToPartsInternal(globalObject, m_style, value.sign(), value.numberType(), resultString, iterator, parts, sourceType, nullptr);
RETURN_IF_EXCEPTION(scope, { });
return parts;
}
IntlMathematicalValue IntlMathematicalValue::parseString(JSGlobalObject* globalObject, StringView view)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto trimmed = view.trim([](auto character) {
return isStrWhiteSpace(character);
});
if (!trimmed.length())
return IntlMathematicalValue { 0.0 };
if (trimmed.length() > 2 && trimmed[0] == '0') {
auto character = trimmed[1];
auto remaining = trimmed.substring(2);
int32_t radix = 0;
if (character == 'b' || character == 'B') {
radix = 2;
if (!remaining.containsOnly<isASCIIBinaryDigit>())
return IntlMathematicalValue { PNaN };
} else if (character == 'o' || character == 'O') {
radix = 8;
if (!remaining.containsOnly<isASCIIOctalDigit>())
return IntlMathematicalValue { PNaN };
} else if (character == 'x' || character == 'X') {
radix = 16;
if (!remaining.containsOnly<isASCIIHexDigit>())
return IntlMathematicalValue { PNaN };
}
if (radix) {
double result = parseInt(remaining, radix);
if (result <= maxSafeInteger())
return IntlMathematicalValue { result };
JSValue bigInt = JSBigInt::parseInt(globalObject, vm, remaining, radix, JSBigInt::ErrorParseMode::IgnoreExceptions, JSBigInt::ParseIntSign::Unsigned);
if (!bigInt)
return IntlMathematicalValue { PNaN };
#if USE(BIGINT32)
if (bigInt.isBigInt32())
return IntlMathematicalValue { value.bigInt32AsInt32() };
#endif
auto* heapBigInt = bigInt.asHeapBigInt();
auto string = heapBigInt->toString(globalObject, 10);
RETURN_IF_EXCEPTION(scope, { });
return IntlMathematicalValue {
IntlMathematicalValue::NumberType::Integer,
false,
string.ascii(),
};
}
}
if (trimmed == "Infinity"_s || trimmed == "+Infinity"_s)
return IntlMathematicalValue { std::numeric_limits<double>::infinity() };
if (trimmed == "-Infinity"_s)
return IntlMathematicalValue { -std::numeric_limits<double>::infinity() };
size_t parsedLength = 0;
double result = parseDouble(trimmed, parsedLength);
if (parsedLength != trimmed.length())
return IntlMathematicalValue { PNaN };
if (!std::isfinite(result))
return IntlMathematicalValue { result };
return IntlMathematicalValue { IntlMathematicalValue::NumberType::Integer, trimmed[0] == '-', trimmed.utf8() };
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|