IntlNumberFormat.cpp
1 /* 2 * Copyright (C) 2015 Andy VanWagoner (andy@vanwagoner.family) 3 * Copyright (C) 2016 Sukolsak Sakshuwong (sukolsak@gmail.com) 4 * Copyright (C) 2016-2020 Apple Inc. All rights reserved. 5 * Copyright (C) 2020 Sony Interactive Entertainment Inc. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26 * THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "config.h" 30 #include "IntlNumberFormat.h" 31 32 #include "Error.h" 33 #include "IntlNumberFormatInlines.h" 34 #include "IntlObjectInlines.h" 35 #include "JSBoundFunction.h" 36 #include "JSCInlines.h" 37 #include "ObjectConstructor.h" 38 #include <wtf/unicode/icu/ICUHelpers.h> 39 40 namespace JSC { 41 42 const ClassInfo IntlNumberFormat::s_info = { "Object", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(IntlNumberFormat) }; 43 44 namespace IntlNumberFormatInternal { 45 static constexpr bool verbose = false; 46 } 47 48 struct IntlNumberFormatField { 49 int32_t type; 50 size_t size; 51 }; 52 53 IntlNumberFormat* IntlNumberFormat::create(VM& vm, Structure* structure) 54 { 55 IntlNumberFormat* format = new (NotNull, allocateCell<IntlNumberFormat>(vm.heap)) IntlNumberFormat(vm, structure); 56 format->finishCreation(vm); 57 return format; 58 } 59 60 Structure* IntlNumberFormat::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) 61 { 62 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); 63 } 64 65 IntlNumberFormat::IntlNumberFormat(VM& vm, Structure* structure) 66 : Base(vm, structure) 67 { 68 } 69 70 void IntlNumberFormat::finishCreation(VM& vm) 71 { 72 Base::finishCreation(vm); 73 ASSERT(inherits(vm, info())); 74 } 75 76 void IntlNumberFormat::visitChildren(JSCell* cell, SlotVisitor& visitor) 77 { 78 IntlNumberFormat* thisObject = jsCast<IntlNumberFormat*>(cell); 79 ASSERT_GC_OBJECT_INHERITS(thisObject, info()); 80 81 Base::visitChildren(thisObject, visitor); 82 83 visitor.append(thisObject->m_boundFormat); 84 } 85 86 Vector<String> IntlNumberFormat::localeData(const String& locale, RelevantExtensionKey key) 87 { 88 // 9.1 Internal slots of Service Constructors & 11.2.3 Internal slots (ECMA-402 2.0) 89 ASSERT_UNUSED(key, key == RelevantExtensionKey::Nu); 90 return numberingSystemsForLocale(locale); 91 } 92 93 static inline unsigned computeCurrencySortKey(const String& currency) 94 { 95 ASSERT(currency.length() == 3); 96 ASSERT(currency.isAllSpecialCharacters<isASCIIUpper>()); 97 return (currency[0] << 16) + (currency[1] << 8) + currency[2]; 98 } 99 100 static inline unsigned computeCurrencySortKey(const char* currency) 101 { 102 ASSERT(strlen(currency) == 3); 103 ASSERT(isAllSpecialCharacters<isASCIIUpper>(currency, 3)); 104 return (currency[0] << 16) + (currency[1] << 8) + currency[2]; 105 } 106 107 static unsigned extractCurrencySortKey(std::pair<const char*, unsigned>* currencyMinorUnit) 108 { 109 return computeCurrencySortKey(currencyMinorUnit->first); 110 } 111 112 static unsigned computeCurrencyDigits(const String& currency) 113 { 114 // 11.1.1 The abstract operation CurrencyDigits (currency) 115 // "If the ISO 4217 currency and funds code list contains currency as an alphabetic code, 116 // then return the minor unit value corresponding to the currency from the list; else return 2. 117 static constexpr std::pair<const char*, unsigned> currencyMinorUnits[] = { 118 { "BHD", 3 }, 119 { "BIF", 0 }, 120 { "BYR", 0 }, 121 { "CLF", 4 }, 122 { "CLP", 0 }, 123 { "DJF", 0 }, 124 { "GNF", 0 }, 125 { "IQD", 3 }, 126 { "ISK", 0 }, 127 { "JOD", 3 }, 128 { "JPY", 0 }, 129 { "KMF", 0 }, 130 { "KRW", 0 }, 131 { "KWD", 3 }, 132 { "LYD", 3 }, 133 { "OMR", 3 }, 134 { "PYG", 0 }, 135 { "RWF", 0 }, 136 { "TND", 3 }, 137 { "UGX", 0 }, 138 { "UYI", 0 }, 139 { "VND", 0 }, 140 { "VUV", 0 }, 141 { "XAF", 0 }, 142 { "XOF", 0 }, 143 { "XPF", 0 } 144 }; 145 auto* currencyMinorUnit = tryBinarySearch<std::pair<const char*, unsigned>>(currencyMinorUnits, WTF_ARRAY_LENGTH(currencyMinorUnits), computeCurrencySortKey(currency), extractCurrencySortKey); 146 if (currencyMinorUnit) 147 return currencyMinorUnit->second; 148 return 2; 149 } 150 151 // Create MeasureUnit like ICU4J. 152 struct MeasureUnit { 153 ASCIILiteral type; 154 ASCIILiteral subType; 155 }; 156 157 static Optional<MeasureUnit> sanctionedSimpleUnitIdentifier(StringView unitIdentifier) 158 { 159 static constexpr MeasureUnit simpleUnits[] = { 160 { "area"_s, "acre"_s }, 161 { "digital"_s, "bit"_s }, 162 { "digital"_s, "byte"_s }, 163 { "temperature"_s, "celsius"_s }, 164 { "length"_s, "centimeter"_s }, 165 { "duration"_s, "day"_s }, 166 { "angle"_s, "degree"_s }, 167 { "temperature"_s, "fahrenheit"_s }, 168 { "volume"_s, "fluid-ounce"_s }, 169 { "length"_s, "foot"_s }, 170 { "volume"_s, "gallon"_s }, 171 { "digital"_s, "gigabit"_s }, 172 { "digital"_s, "gigabyte"_s }, 173 { "mass"_s, "gram"_s }, 174 { "area"_s, "hectare"_s }, 175 { "duration"_s, "hour"_s }, 176 { "length"_s, "inch"_s }, 177 { "digital"_s, "kilobit"_s }, 178 { "digital"_s, "kilobyte"_s }, 179 { "mass"_s, "kilogram"_s }, 180 { "length"_s, "kilometer"_s }, 181 { "volume"_s, "liter"_s }, 182 { "digital"_s, "megabit"_s }, 183 { "digital"_s, "megabyte"_s }, 184 { "length"_s, "meter"_s }, 185 { "length"_s, "mile"_s }, 186 { "length"_s, "mile-scandinavian"_s }, 187 { "volume"_s, "milliliter"_s }, 188 { "length"_s, "millimeter"_s }, 189 { "duration"_s, "millisecond"_s }, 190 { "duration"_s, "minute"_s }, 191 { "duration"_s, "month"_s }, 192 { "mass"_s, "ounce"_s }, 193 { "concentr"_s, "percent"_s }, 194 { "digital"_s, "petabyte"_s }, 195 { "mass"_s, "pound"_s }, 196 { "duration"_s, "second"_s }, 197 { "mass"_s, "stone"_s }, 198 { "digital"_s, "terabit"_s }, 199 { "digital"_s, "terabyte"_s }, 200 { "duration"_s, "week"_s }, 201 { "length"_s, "yard"_s }, 202 { "duration"_s, "year"_s }, 203 }; 204 ASSERT( 205 std::is_sorted(std::begin(simpleUnits), std::end(simpleUnits), 206 [](const MeasureUnit& a, const MeasureUnit& b) { 207 return WTF::codePointCompare(StringView(a.subType), StringView(b.subType)) < 0; 208 })); 209 210 auto iterator = std::lower_bound(std::begin(simpleUnits), std::end(simpleUnits), unitIdentifier, 211 [](const MeasureUnit& unit, StringView unitIdentifier) { 212 return WTF::codePointCompare(StringView(unit.subType), unitIdentifier) < 0; 213 }); 214 if (iterator != std::end(simpleUnits) && iterator->subType == unitIdentifier) 215 return *iterator; 216 return WTF::nullopt; 217 } 218 219 struct WellFormedUnit { 220 public: 221 explicit WellFormedUnit(MeasureUnit numerator) 222 : numerator(numerator) 223 { 224 } 225 226 WellFormedUnit(MeasureUnit numerator, MeasureUnit denominator) 227 : numerator(numerator) 228 , denominator(denominator) 229 { 230 } 231 232 MeasureUnit numerator; 233 Optional<MeasureUnit> denominator; 234 }; 235 236 static Optional<WellFormedUnit> wellFormedUnitIdentifier(StringView unitIdentifier) 237 { 238 // https://tc39.es/ecma402/#sec-iswellformedunitidentifier 239 if (auto unit = sanctionedSimpleUnitIdentifier(unitIdentifier)) 240 return WellFormedUnit(unit.value()); 241 242 // If the substring "-per-" does not occur exactly once in unitIdentifier, then return false. 243 auto per = StringView("-per-"_s); 244 auto position = unitIdentifier.find(per); 245 if (position == WTF::notFound) 246 return WTF::nullopt; 247 if (unitIdentifier.find(per, position + per.length()) != WTF::notFound) 248 return WTF::nullopt; 249 250 // If the result of IsSanctionedSimpleUnitIdentifier(numerator) is false, then return false. 251 auto numerator = unitIdentifier.substring(0, position); 252 auto numeratorUnit = sanctionedSimpleUnitIdentifier(numerator); 253 if (!numeratorUnit) 254 return WTF::nullopt; 255 256 // If the result of IsSanctionedSimpleUnitIdentifier(denominator) is false, then return false. 257 auto denominator = unitIdentifier.substring(position + per.length()); 258 auto denominatorUnit = sanctionedSimpleUnitIdentifier(denominator); 259 if (!denominatorUnit) 260 return WTF::nullopt; 261 262 return WellFormedUnit(numeratorUnit.value(), denominatorUnit.value()); 263 } 264 265 // https://tc39.github.io/ecma402/#sec-initializenumberformat 266 void IntlNumberFormat::initializeNumberFormat(JSGlobalObject* globalObject, JSValue locales, JSValue optionsValue) 267 { 268 VM& vm = globalObject->vm(); 269 auto scope = DECLARE_THROW_SCOPE(vm); 270 271 auto requestedLocales = canonicalizeLocaleList(globalObject, locales); 272 RETURN_IF_EXCEPTION(scope, void()); 273 274 JSObject* options; 275 if (optionsValue.isUndefined()) 276 options = constructEmptyObject(vm, globalObject->nullPrototypeObjectStructure()); 277 else { 278 options = optionsValue.toObject(globalObject); 279 RETURN_IF_EXCEPTION(scope, void()); 280 } 281 282 ResolveLocaleOptions localeOptions; 283 284 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); 285 RETURN_IF_EXCEPTION(scope, void()); 286 287 String numberingSystem = intlStringOption(globalObject, options, vm.propertyNames->numberingSystem, { }, nullptr, nullptr); 288 RETURN_IF_EXCEPTION(scope, void()); 289 if (!numberingSystem.isNull()) { 290 if (!isUnicodeLocaleIdentifierType(numberingSystem)) { 291 throwRangeError(globalObject, scope, "numberingSystem is not a well-formed numbering system value"_s); 292 return; 293 } 294 localeOptions[static_cast<unsigned>(RelevantExtensionKey::Nu)] = numberingSystem; 295 } 296 297 auto& availableLocales = intlNumberFormatAvailableLocales(); 298 auto resolved = resolveLocale(globalObject, availableLocales, requestedLocales, localeMatcher, localeOptions, { RelevantExtensionKey::Nu }, localeData); 299 300 m_locale = resolved.locale; 301 if (m_locale.isEmpty()) { 302 throwTypeError(globalObject, scope, "failed to initialize NumberFormat due to invalid locale"_s); 303 return; 304 } 305 306 m_numberingSystem = resolved.extensions[static_cast<unsigned>(RelevantExtensionKey::Nu)]; 307 308 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); 309 RETURN_IF_EXCEPTION(scope, void()); 310 311 String currency = intlStringOption(globalObject, options, Identifier::fromString(vm, "currency"), { }, nullptr, nullptr); 312 RETURN_IF_EXCEPTION(scope, void()); 313 if (!currency.isNull()) { 314 if (!isWellFormedCurrencyCode(currency)) { 315 throwException(globalObject, scope, createRangeError(globalObject, "currency is not a well-formed currency code"_s)); 316 return; 317 } 318 } 319 320 unsigned currencyDigits = 0; 321 if (m_style == Style::Currency) { 322 if (currency.isNull()) { 323 throwTypeError(globalObject, scope, "currency must be a string"_s); 324 return; 325 } 326 327 currency = currency.convertToASCIIUppercase(); 328 m_currency = currency; 329 currencyDigits = computeCurrencyDigits(currency); 330 } 331 332 m_currencyDisplay = intlOption<CurrencyDisplay>(globalObject, options, Identifier::fromString(vm, "currencyDisplay"), { { "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); 333 RETURN_IF_EXCEPTION(scope, void()); 334 335 m_currencySign = intlOption<CurrencySign>(globalObject, options, Identifier::fromString(vm, "currencySign"), { { "standard"_s, CurrencySign::Standard }, { "accounting"_s, CurrencySign::Accounting } }, "currencySign must be either \"standard\" or \"accounting\""_s, CurrencySign::Standard); 336 RETURN_IF_EXCEPTION(scope, void()); 337 338 String unit = intlStringOption(globalObject, options, Identifier::fromString(vm, "unit"), { }, nullptr, nullptr); 339 RETURN_IF_EXCEPTION(scope, void()); 340 Optional<WellFormedUnit> wellFormedUnit; 341 if (!unit.isNull()) { 342 wellFormedUnit = wellFormedUnitIdentifier(unit); 343 if (!wellFormedUnit) { 344 throwRangeError(globalObject, scope, "unit is not a well-formed unit identifier"_s); 345 return; 346 } 347 m_unit = unit; 348 } else if (m_style == Style::Unit) { 349 throwTypeError(globalObject, scope, "unit must be a string"_s); 350 return; 351 } 352 353 m_unitDisplay = intlOption<UnitDisplay>(globalObject, options, Identifier::fromString(vm, "unitDisplay"), { { "short"_s, UnitDisplay::Short }, { "narrow"_s, UnitDisplay::Narrow }, { "long"_s, UnitDisplay::Long } }, "unitDisplay must be either \"short\", \"narrow\", or \"long\""_s, UnitDisplay::Short); 354 RETURN_IF_EXCEPTION(scope, void()); 355 356 unsigned minimumFractionDigitsDefault = (m_style == Style::Currency) ? currencyDigits : 0; 357 unsigned maximumFractionDigitsDefault = (m_style == Style::Currency) ? currencyDigits : (m_style == Style::Percent) ? 0 : 3; 358 359 m_notation = intlOption<IntlNotation>(globalObject, options, Identifier::fromString(vm, "notation"), { { "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); 360 RETURN_IF_EXCEPTION(scope, void()); 361 362 setNumberFormatDigitOptions(globalObject, this, options, minimumFractionDigitsDefault, maximumFractionDigitsDefault, m_notation); 363 RETURN_IF_EXCEPTION(scope, void()); 364 365 m_compactDisplay = intlOption<CompactDisplay>(globalObject, options, Identifier::fromString(vm, "compactDisplay"), { { "short"_s, CompactDisplay::Short }, { "long"_s, CompactDisplay::Long } }, "compactDisplay must be either \"short\" or \"long\""_s, CompactDisplay::Short); 366 RETURN_IF_EXCEPTION(scope, void()); 367 368 TriState useGrouping = intlBooleanOption(globalObject, options, Identifier::fromString(vm, "useGrouping")); 369 RETURN_IF_EXCEPTION(scope, void()); 370 m_useGrouping = useGrouping != TriState::False; 371 372 m_signDisplay = intlOption<SignDisplay>(globalObject, options, Identifier::fromString(vm, "signDisplay"), { { "auto"_s, SignDisplay::Auto }, { "never"_s, SignDisplay::Never }, { "always"_s, SignDisplay::Always }, { "exceptZero"_s, SignDisplay::ExceptZero } }, "signDisplay must be either \"auto\", \"never\", \"always\", or \"exceptZero\""_s, SignDisplay::Auto); 373 RETURN_IF_EXCEPTION(scope, void()); 374 375 CString dataLocaleWithExtensions = makeString(resolved.dataLocale, "-u-nu-", m_numberingSystem).utf8(); 376 dataLogLnIf(IntlNumberFormatInternal::verbose, "dataLocaleWithExtensions:(", dataLocaleWithExtensions , ")"); 377 378 // Options are obtained. Configure formatter here. 379 380 #if HAVE(ICU_U_NUMBER_FORMATTER) 381 // Constructing ICU Number Skeletons to configure UNumberFormatter. 382 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md 383 384 StringBuilder skeletonBuilder; 385 skeletonBuilder.appendLiteral("rounding-mode-half-up"); 386 387 switch (m_style) { 388 case Style::Decimal: 389 // No skeleton is needed. 390 break; 391 case Style::Percent: 392 skeletonBuilder.appendLiteral(" percent scale/100"); 393 break; 394 case Style::Currency: { 395 skeletonBuilder.appendLiteral(" currency/"); 396 skeletonBuilder.append(currency); 397 398 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#unit-width 399 switch (m_currencyDisplay) { 400 case CurrencyDisplay::Code: 401 skeletonBuilder.appendLiteral(" unit-width-iso-code"); 402 break; 403 case CurrencyDisplay::Symbol: 404 // Default option. Do not specify unit-width. 405 break; 406 case CurrencyDisplay::NarrowSymbol: 407 skeletonBuilder.appendLiteral(" unit-width-narrow"); 408 break; 409 case CurrencyDisplay::Name: 410 skeletonBuilder.appendLiteral(" unit-width-full-name"); 411 break; 412 } 413 break; 414 } 415 case Style::Unit: { 416 // The measure-unit stem takes one required option: the unit identifier of the unit to be formatted. 417 // The full unit identifier is required: both the type and the subtype (for example, length-meter). 418 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#unit 419 skeletonBuilder.appendLiteral(" measure-unit/"); 420 auto numeratorUnit = wellFormedUnit->numerator; 421 skeletonBuilder.append(numeratorUnit.type, '-', numeratorUnit.subType); 422 if (auto denominatorUnitValue = wellFormedUnit->denominator) { 423 auto denominatorUnit = denominatorUnitValue.value(); 424 skeletonBuilder.appendLiteral(" per-measure-unit/"); 425 skeletonBuilder.append(denominatorUnit.type, '-', denominatorUnit.subType); 426 } 427 428 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#unit-width 429 switch (m_unitDisplay) { 430 case UnitDisplay::Short: 431 skeletonBuilder.appendLiteral(" unit-width-short"); 432 break; 433 case UnitDisplay::Narrow: 434 skeletonBuilder.appendLiteral(" unit-width-narrow"); 435 break; 436 case UnitDisplay::Long: 437 skeletonBuilder.appendLiteral(" unit-width-full-name"); 438 break; 439 } 440 break; 441 } 442 } 443 444 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#integer-width 445 skeletonBuilder.appendLiteral(" integer-width/"); 446 skeletonBuilder.append((WTF::ICU::majorVersion() >= 67) ? '*' : '+'); // Prior to ICU 67, use the symbol + instead of *. 447 for (unsigned i = 0; i < m_minimumIntegerDigits; ++i) 448 skeletonBuilder.append('0'); 449 450 switch (m_roundingType) { 451 case IntlRoundingType::FractionDigits: { 452 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#fraction-precision 453 skeletonBuilder.append(" ."); 454 for (unsigned i = 0; i < m_minimumFractionDigits; ++i) 455 skeletonBuilder.append('0'); 456 for (unsigned i = 0; i < m_maximumFractionDigits - m_minimumFractionDigits; ++i) 457 skeletonBuilder.append('#'); 458 break; 459 } 460 case IntlRoundingType::SignificantDigits: { 461 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#significant-digits-precision 462 skeletonBuilder.append(' '); 463 for (unsigned i = 0; i < m_minimumSignificantDigits; ++i) 464 skeletonBuilder.append('@'); 465 for (unsigned i = 0; i < m_maximumSignificantDigits - m_minimumSignificantDigits; ++i) 466 skeletonBuilder.append('#'); 467 break; 468 } 469 case IntlRoundingType::CompactRounding: 470 // Do not set anything. 471 break; 472 } 473 474 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#notation 475 switch (m_notation) { 476 case IntlNotation::Standard: 477 break; 478 case IntlNotation::Scientific: 479 skeletonBuilder.appendLiteral(" scientific"); 480 break; 481 case IntlNotation::Engineering: 482 skeletonBuilder.appendLiteral(" engineering"); 483 break; 484 case IntlNotation::Compact: 485 switch (m_compactDisplay) { 486 case CompactDisplay::Short: 487 skeletonBuilder.appendLiteral(" compact-short"); 488 break; 489 case CompactDisplay::Long: 490 skeletonBuilder.appendLiteral(" compact-long"); 491 break; 492 } 493 break; 494 } 495 496 // https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#sign-display 497 // CurrencySign's accounting is a part of SignDisplay in ICU. 498 bool useAccounting = (m_style == Style::Currency && m_currencySign == CurrencySign::Accounting); 499 switch (m_signDisplay) { 500 case SignDisplay::Auto: 501 if (useAccounting) 502 skeletonBuilder.appendLiteral(" sign-accounting"); 503 else 504 skeletonBuilder.appendLiteral(" sign-auto"); 505 break; 506 case SignDisplay::Never: 507 skeletonBuilder.appendLiteral(" sign-never"); 508 break; 509 case SignDisplay::Always: 510 if (useAccounting) 511 skeletonBuilder.appendLiteral(" sign-accounting-always"); 512 else 513 skeletonBuilder.appendLiteral(" sign-always"); 514 break; 515 case SignDisplay::ExceptZero: 516 if (useAccounting) 517 skeletonBuilder.appendLiteral(" sign-accounting-except-zero"); 518 else 519 skeletonBuilder.appendLiteral(" sign-except-zero"); 520 break; 521 } 522 523 if (!m_useGrouping) 524 skeletonBuilder.appendLiteral(" group-off"); 525 526 String skeleton = skeletonBuilder.toString(); 527 dataLogLnIf(IntlNumberFormatInternal::verbose, skeleton); 528 StringView skeletonView(skeleton); 529 UErrorCode status = U_ZERO_ERROR; 530 m_numberFormatter = std::unique_ptr<UNumberFormatter, UNumberFormatterDeleter>(unumf_openForSkeletonAndLocale(skeletonView.upconvertedCharacters().get(), skeletonView.length(), dataLocaleWithExtensions.data(), &status)); 531 if (U_FAILURE(status)) { 532 throwTypeError(globalObject, scope, "Failed to initialize NumberFormat"_s); 533 return; 534 } 535 #else 536 UNumberFormatStyle style = UNUM_DEFAULT; 537 switch (m_style) { 538 case Style::Decimal: 539 style = UNUM_DECIMAL; 540 break; 541 case Style::Percent: 542 style = UNUM_PERCENT; 543 break; 544 case Style::Currency: 545 switch (m_currencyDisplay) { 546 case CurrencyDisplay::Code: 547 style = UNUM_CURRENCY_ISO; 548 break; 549 case CurrencyDisplay::Symbol: 550 style = UNUM_CURRENCY; 551 break; 552 case CurrencyDisplay::NarrowSymbol: 553 throwTypeError(globalObject, scope, "Failed to initialize NumberFormat since used feature is not supported in the linked ICU version"_s); 554 return; 555 case CurrencyDisplay::Name: 556 style = UNUM_CURRENCY_PLURAL; 557 break; 558 } 559 switch (m_currencySign) { 560 case CurrencySign::Standard: 561 break; 562 case CurrencySign::Accounting: 563 throwTypeError(globalObject, scope, "Failed to initialize NumberFormat since used feature is not supported in the linked ICU version"_s); 564 return; 565 } 566 break; 567 case Style::Unit: 568 throwTypeError(globalObject, scope, "Failed to initialize NumberFormat since used feature is not supported in the linked ICU version"_s); 569 return; 570 } 571 572 switch (m_notation) { 573 case IntlNotation::Standard: 574 break; 575 case IntlNotation::Scientific: 576 case IntlNotation::Engineering: 577 case IntlNotation::Compact: 578 throwTypeError(globalObject, scope, "Failed to initialize NumberFormat since used feature is not supported in the linked ICU version"_s); 579 return; 580 } 581 582 switch (m_signDisplay) { 583 case SignDisplay::Auto: 584 break; 585 case SignDisplay::Never: 586 case SignDisplay::Always: 587 case SignDisplay::ExceptZero: 588 throwTypeError(globalObject, scope, "Failed to initialize NumberFormat since used feature is not supported in the linked ICU version"_s); 589 return; 590 } 591 592 UErrorCode status = U_ZERO_ERROR; 593 m_numberFormat = std::unique_ptr<UNumberFormat, UNumberFormatDeleter>(unum_open(style, nullptr, 0, dataLocaleWithExtensions.data(), nullptr, &status)); 594 if (U_FAILURE(status)) { 595 throwTypeError(globalObject, scope, "failed to initialize NumberFormat"_s); 596 return; 597 } 598 599 if (m_style == Style::Currency) { 600 unum_setTextAttribute(m_numberFormat.get(), UNUM_CURRENCY_CODE, StringView(m_currency).upconvertedCharacters(), m_currency.length(), &status); 601 if (U_FAILURE(status)) { 602 throwTypeError(globalObject, scope, "failed to initialize NumberFormat"_s); 603 return; 604 } 605 } 606 607 switch (m_roundingType) { 608 case IntlRoundingType::FractionDigits: 609 unum_setAttribute(m_numberFormat.get(), UNUM_MIN_INTEGER_DIGITS, m_minimumIntegerDigits); 610 unum_setAttribute(m_numberFormat.get(), UNUM_MIN_FRACTION_DIGITS, m_minimumFractionDigits); 611 unum_setAttribute(m_numberFormat.get(), UNUM_MAX_FRACTION_DIGITS, m_maximumFractionDigits); 612 break; 613 case IntlRoundingType::SignificantDigits: 614 unum_setAttribute(m_numberFormat.get(), UNUM_SIGNIFICANT_DIGITS_USED, true); 615 unum_setAttribute(m_numberFormat.get(), UNUM_MIN_SIGNIFICANT_DIGITS, m_minimumSignificantDigits); 616 unum_setAttribute(m_numberFormat.get(), UNUM_MAX_SIGNIFICANT_DIGITS, m_maximumSignificantDigits); 617 break; 618 case IntlRoundingType::CompactRounding: 619 throwTypeError(globalObject, scope, "Failed to initialize NumberFormat since used feature is not supported in the linked ICU version"_s); 620 return; 621 } 622 unum_setAttribute(m_numberFormat.get(), UNUM_GROUPING_USED, m_useGrouping); 623 unum_setAttribute(m_numberFormat.get(), UNUM_ROUNDING_MODE, UNUM_ROUND_HALFUP); 624 #endif 625 } 626 627 // https://tc39.es/ecma402/#sec-formatnumber 628 JSValue IntlNumberFormat::format(JSGlobalObject* globalObject, double value) const 629 { 630 VM& vm = globalObject->vm(); 631 auto scope = DECLARE_THROW_SCOPE(vm); 632 633 Vector<UChar, 32> buffer; 634 #if HAVE(ICU_U_NUMBER_FORMATTER) 635 ASSERT(m_numberFormatter); 636 UErrorCode status = U_ZERO_ERROR; 637 auto formattedNumber = std::unique_ptr<UFormattedNumber, UFormattedNumberDeleter>(unumf_openResult(&status)); 638 if (U_FAILURE(status)) 639 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 640 unumf_formatDouble(m_numberFormatter.get(), value, formattedNumber.get(), &status); 641 if (U_FAILURE(status)) 642 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 643 status = callBufferProducingFunction(unumf_resultToString, formattedNumber.get(), buffer); 644 if (U_FAILURE(status)) 645 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 646 #else 647 ASSERT(m_numberFormat); 648 auto status = callBufferProducingFunction(unum_formatDouble, m_numberFormat.get(), value, buffer, nullptr); 649 if (U_FAILURE(status)) 650 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 651 #endif 652 return jsString(vm, String(buffer)); 653 } 654 655 // https://tc39.es/ecma402/#sec-formatnumber 656 JSValue IntlNumberFormat::format(JSGlobalObject* globalObject, JSBigInt* value) const 657 { 658 VM& vm = globalObject->vm(); 659 auto scope = DECLARE_THROW_SCOPE(vm); 660 661 auto string = value->toString(globalObject, 10); 662 RETURN_IF_EXCEPTION(scope, { }); 663 664 ASSERT(string.is8Bit() && string.isAllASCII()); 665 auto* rawString = reinterpret_cast<const char*>(string.characters8()); 666 667 Vector<UChar, 32> buffer; 668 #if HAVE(ICU_U_NUMBER_FORMATTER) 669 ASSERT(m_numberFormatter); 670 UErrorCode status = U_ZERO_ERROR; 671 auto formattedNumber = std::unique_ptr<UFormattedNumber, UFormattedNumberDeleter>(unumf_openResult(&status)); 672 if (U_FAILURE(status)) 673 return throwTypeError(globalObject, scope, "Failed to format a BigInt."_s); 674 unumf_formatDecimal(m_numberFormatter.get(), rawString, string.length(), formattedNumber.get(), &status); 675 if (U_FAILURE(status)) 676 return throwTypeError(globalObject, scope, "Failed to format a BigInt."_s); 677 status = callBufferProducingFunction(unumf_resultToString, formattedNumber.get(), buffer); 678 if (U_FAILURE(status)) 679 return throwTypeError(globalObject, scope, "Failed to format a BigInt."_s); 680 #else 681 ASSERT(m_numberFormat); 682 auto status = callBufferProducingFunction(unum_formatDecimal, m_numberFormat.get(), rawString, string.length(), buffer, nullptr); 683 if (U_FAILURE(status)) 684 return throwTypeError(globalObject, scope, "Failed to format a BigInt."_s); 685 #endif 686 return jsString(vm, String(buffer)); 687 } 688 689 ASCIILiteral IntlNumberFormat::styleString(Style style) 690 { 691 switch (style) { 692 case Style::Decimal: 693 return "decimal"_s; 694 case Style::Percent: 695 return "percent"_s; 696 case Style::Currency: 697 return "currency"_s; 698 case Style::Unit: 699 return "unit"_s; 700 } 701 ASSERT_NOT_REACHED(); 702 return ASCIILiteral::null(); 703 } 704 705 ASCIILiteral IntlNumberFormat::currencyDisplayString(CurrencyDisplay currencyDisplay) 706 { 707 switch (currencyDisplay) { 708 case CurrencyDisplay::Code: 709 return "code"_s; 710 case CurrencyDisplay::Symbol: 711 return "symbol"_s; 712 case CurrencyDisplay::NarrowSymbol: 713 return "narrowSymbol"_s; 714 case CurrencyDisplay::Name: 715 return "name"_s; 716 } 717 ASSERT_NOT_REACHED(); 718 return ASCIILiteral::null(); 719 } 720 721 ASCIILiteral IntlNumberFormat::notationString(IntlNotation notation) 722 { 723 switch (notation) { 724 case IntlNotation::Standard: 725 return "standard"_s; 726 case IntlNotation::Scientific: 727 return "scientific"_s; 728 case IntlNotation::Engineering: 729 return "engineering"_s; 730 case IntlNotation::Compact: 731 return "compact"_s; 732 } 733 ASSERT_NOT_REACHED(); 734 return ASCIILiteral::null(); 735 } 736 737 ASCIILiteral IntlNumberFormat::currencySignString(CurrencySign currencySign) 738 { 739 switch (currencySign) { 740 case CurrencySign::Standard: 741 return "standard"_s; 742 case CurrencySign::Accounting: 743 return "accounting"_s; 744 } 745 ASSERT_NOT_REACHED(); 746 return ASCIILiteral::null(); 747 } 748 749 ASCIILiteral IntlNumberFormat::unitDisplayString(UnitDisplay unitDisplay) 750 { 751 switch (unitDisplay) { 752 case UnitDisplay::Short: 753 return "short"_s; 754 case UnitDisplay::Narrow: 755 return "narrow"_s; 756 case UnitDisplay::Long: 757 return "long"_s; 758 } 759 ASSERT_NOT_REACHED(); 760 return ASCIILiteral::null(); 761 } 762 763 ASCIILiteral IntlNumberFormat::compactDisplayString(CompactDisplay compactDisplay) 764 { 765 switch (compactDisplay) { 766 case CompactDisplay::Short: 767 return "short"_s; 768 case CompactDisplay::Long: 769 return "long"_s; 770 } 771 ASSERT_NOT_REACHED(); 772 return ASCIILiteral::null(); 773 } 774 775 ASCIILiteral IntlNumberFormat::signDisplayString(SignDisplay signDisplay) 776 { 777 switch (signDisplay) { 778 case SignDisplay::Auto: 779 return "auto"_s; 780 case SignDisplay::Never: 781 return "never"_s; 782 case SignDisplay::Always: 783 return "always"_s; 784 case SignDisplay::ExceptZero: 785 return "exceptZero"_s; 786 } 787 ASSERT_NOT_REACHED(); 788 return ASCIILiteral::null(); 789 } 790 791 // https://tc39.es/ecma402/#sec-intl.numberformat.prototype.resolvedoptions 792 JSObject* IntlNumberFormat::resolvedOptions(JSGlobalObject* globalObject) const 793 { 794 VM& vm = globalObject->vm(); 795 JSObject* options = constructEmptyObject(globalObject); 796 options->putDirect(vm, vm.propertyNames->locale, jsString(vm, m_locale)); 797 options->putDirect(vm, vm.propertyNames->numberingSystem, jsString(vm, m_numberingSystem)); 798 options->putDirect(vm, vm.propertyNames->style, jsNontrivialString(vm, styleString(m_style))); 799 switch (m_style) { 800 case Style::Decimal: 801 case Style::Percent: 802 break; 803 case Style::Currency: 804 options->putDirect(vm, Identifier::fromString(vm, "currency"), jsNontrivialString(vm, m_currency)); 805 options->putDirect(vm, Identifier::fromString(vm, "currencyDisplay"), jsNontrivialString(vm, currencyDisplayString(m_currencyDisplay))); 806 options->putDirect(vm, Identifier::fromString(vm, "currencySign"), jsNontrivialString(vm, currencySignString(m_currencySign))); 807 break; 808 case Style::Unit: 809 options->putDirect(vm, Identifier::fromString(vm, "unit"), jsNontrivialString(vm, m_unit)); 810 options->putDirect(vm, Identifier::fromString(vm, "unitDisplay"), jsNontrivialString(vm, unitDisplayString(m_unitDisplay))); 811 break; 812 } 813 options->putDirect(vm, vm.propertyNames->minimumIntegerDigits, jsNumber(m_minimumIntegerDigits)); 814 switch (m_roundingType) { 815 case IntlRoundingType::FractionDigits: 816 options->putDirect(vm, vm.propertyNames->minimumFractionDigits, jsNumber(m_minimumFractionDigits)); 817 options->putDirect(vm, vm.propertyNames->maximumFractionDigits, jsNumber(m_maximumFractionDigits)); 818 break; 819 case IntlRoundingType::SignificantDigits: 820 options->putDirect(vm, vm.propertyNames->minimumSignificantDigits, jsNumber(m_minimumSignificantDigits)); 821 options->putDirect(vm, vm.propertyNames->maximumSignificantDigits, jsNumber(m_maximumSignificantDigits)); 822 break; 823 case IntlRoundingType::CompactRounding: 824 break; 825 } 826 options->putDirect(vm, Identifier::fromString(vm, "useGrouping"), jsBoolean(m_useGrouping)); 827 options->putDirect(vm, Identifier::fromString(vm, "notation"), jsNontrivialString(vm, notationString(m_notation))); 828 if (m_notation == IntlNotation::Compact) 829 options->putDirect(vm, Identifier::fromString(vm, "compactDisplay"), jsNontrivialString(vm, compactDisplayString(m_compactDisplay))); 830 options->putDirect(vm, Identifier::fromString(vm, "signDisplay"), jsNontrivialString(vm, signDisplayString(m_signDisplay))); 831 return options; 832 } 833 834 void IntlNumberFormat::setBoundFormat(VM& vm, JSBoundFunction* format) 835 { 836 m_boundFormat.set(vm, this, format); 837 } 838 839 static ASCIILiteral partTypeString(UNumberFormatFields field, IntlNumberFormat::Style style, double value) 840 { 841 switch (field) { 842 case UNUM_INTEGER_FIELD: 843 if (std::isnan(value)) 844 return "nan"_s; 845 if (!std::isfinite(value)) 846 return "infinity"_s; 847 return "integer"_s; 848 case UNUM_FRACTION_FIELD: 849 return "fraction"_s; 850 case UNUM_DECIMAL_SEPARATOR_FIELD: 851 return "decimal"_s; 852 case UNUM_EXPONENT_SYMBOL_FIELD: 853 return "exponentSeparator"_s; 854 case UNUM_EXPONENT_SIGN_FIELD: 855 return "exponentMinusSign"_s; 856 case UNUM_EXPONENT_FIELD: 857 return "exponentInteger"_s; 858 case UNUM_GROUPING_SEPARATOR_FIELD: 859 return "group"_s; 860 case UNUM_CURRENCY_FIELD: 861 return "currency"_s; 862 case UNUM_PERCENT_FIELD: 863 // If the style is "unit", we should report as unit. 864 // JSTests/test262/test/intl402/NumberFormat/prototype/formatToParts/percent-en-US.js 865 return (style == IntlNumberFormat::Style::Unit) ? "unit"_s : "percentSign"_s; 866 case UNUM_SIGN_FIELD: 867 return std::signbit(value) ? "minusSign"_s : "plusSign"_s; 868 #if HAVE(ICU_U_NUMBER_FORMATTER) 869 case UNUM_MEASURE_UNIT_FIELD: 870 return "unit"_s; 871 case UNUM_COMPACT_FIELD: 872 return "compact"_s; 873 #endif 874 // These should not show up because there is no way to specify them in NumberFormat options. 875 // If they do, they don't fit well into any of known part types, so consider it an "unknown". 876 case UNUM_PERMILL_FIELD: 877 // Any newer additions to the UNumberFormatFields enum should just be considered an "unknown" part. 878 default: 879 return "unknown"_s; 880 } 881 return "unknown"_s; 882 } 883 884 void IntlNumberFormat::formatToPartsInternal(JSGlobalObject* globalObject, Style style, double value, const String& formatted, IntlFieldIterator& iterator, JSArray* parts, JSString* unit) 885 { 886 VM& vm = globalObject->vm(); 887 auto scope = DECLARE_THROW_SCOPE(vm); 888 889 auto stringLength = formatted.length(); 890 891 int32_t literalFieldType = -1; 892 IntlNumberFormatField literalField { literalFieldType, stringLength }; 893 Vector<IntlNumberFormatField, 32> fields(stringLength, literalField); 894 int32_t beginIndex = 0; 895 int32_t endIndex = 0; 896 UErrorCode status = U_ZERO_ERROR; 897 auto fieldType = iterator.next(beginIndex, endIndex, status); 898 if (U_FAILURE(status)) { 899 throwTypeError(globalObject, scope, "Failed to iterate field position iterator"_s); 900 return; 901 } 902 while (fieldType >= 0) { 903 size_t size = endIndex - beginIndex; 904 for (auto i = beginIndex; i < endIndex; ++i) { 905 // Only override previous value if new value is more specific. 906 if (fields[i].size >= size) 907 fields[i] = IntlNumberFormatField { fieldType, size }; 908 } 909 fieldType = iterator.next(beginIndex, endIndex, status); 910 if (U_FAILURE(status)) { 911 throwTypeError(globalObject, scope, "Failed to iterate field position iterator"_s); 912 return; 913 } 914 } 915 916 auto literalString = jsNontrivialString(vm, "literal"_s); 917 Identifier unitName; 918 if (unit) 919 unitName = Identifier::fromString(vm, "unit"); 920 921 size_t currentIndex = 0; 922 while (currentIndex < stringLength) { 923 auto startIndex = currentIndex; 924 auto fieldType = fields[currentIndex].type; 925 while (currentIndex < stringLength && fields[currentIndex].type == fieldType) 926 ++currentIndex; 927 auto partType = fieldType == literalFieldType ? literalString : jsString(vm, partTypeString(UNumberFormatFields(fieldType), style, value)); 928 auto partValue = jsSubstring(vm, formatted, startIndex, currentIndex - startIndex); 929 JSObject* part = constructEmptyObject(globalObject); 930 part->putDirect(vm, vm.propertyNames->type, partType); 931 part->putDirect(vm, vm.propertyNames->value, partValue); 932 if (unit) 933 part->putDirect(vm, unitName, unit); 934 parts->push(globalObject, part); 935 RETURN_IF_EXCEPTION(scope, void()); 936 } 937 } 938 939 // https://tc39.github.io/ecma402/#sec-formatnumbertoparts 940 JSValue IntlNumberFormat::formatToParts(JSGlobalObject* globalObject, double value) const 941 { 942 VM& vm = globalObject->vm(); 943 auto scope = DECLARE_THROW_SCOPE(vm); 944 945 UErrorCode status = U_ZERO_ERROR; 946 auto fieldItr = std::unique_ptr<UFieldPositionIterator, UFieldPositionIteratorDeleter>(ufieldpositer_open(&status)); 947 if (U_FAILURE(status)) 948 return throwTypeError(globalObject, scope, "failed to open field position iterator"_s); 949 950 Vector<UChar, 32> result; 951 #if HAVE(ICU_U_NUMBER_FORMATTER) 952 ASSERT(m_numberFormatter); 953 auto formattedNumber = std::unique_ptr<UFormattedNumber, UFormattedNumberDeleter>(unumf_openResult(&status)); 954 if (U_FAILURE(status)) 955 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 956 unumf_formatDouble(m_numberFormatter.get(), value, formattedNumber.get(), &status); 957 if (U_FAILURE(status)) 958 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 959 status = callBufferProducingFunction(unumf_resultToString, formattedNumber.get(), result); 960 if (U_FAILURE(status)) 961 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 962 unumf_resultGetAllFieldPositions(formattedNumber.get(), fieldItr.get(), &status); 963 if (U_FAILURE(status)) 964 return throwTypeError(globalObject, scope, "Failed to format a number."_s); 965 IntlFieldIterator iterator(*fieldItr.get()); 966 #else 967 ASSERT(m_numberFormat); 968 status = callBufferProducingFunction(unum_formatDoubleForFields, m_numberFormat.get(), value, result, fieldItr.get()); 969 if (U_FAILURE(status)) 970 return throwTypeError(globalObject, scope, "failed to format a number."_s); 971 IntlFieldIterator iterator(*fieldItr.get()); 972 #endif 973 974 auto resultString = String(result); 975 976 JSArray* parts = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), 0); 977 if (!parts) 978 return throwOutOfMemoryError(globalObject, scope); 979 980 formatToPartsInternal(globalObject, m_style, value, resultString, iterator, parts); 981 RETURN_IF_EXCEPTION(scope, { }); 982 983 return parts; 984 } 985 986 } // namespace JSC