PluralRules.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2022-2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Variant.h>
  7. #include <LibJS/Runtime/Intl/PluralRules.h>
  8. #include <math.h>
  9. #include <stdlib.h>
  10. namespace JS::Intl {
  11. // 16 PluralRules Objects, https://tc39.es/ecma402/#pluralrules-objects
  12. PluralRules::PluralRules(Object& prototype)
  13. : NumberFormatBase(prototype)
  14. {
  15. }
  16. // 16.5.1 GetOperands ( s ), https://tc39.es/ecma402/#sec-getoperands
  17. ::Locale::PluralOperands get_operands(StringView string)
  18. {
  19. // 1.Let n be ! ToNumber(s).
  20. auto number = string.to_double(AK::TrimWhitespace::Yes).release_value();
  21. // 2. Assert: n is finite.
  22. VERIFY(isfinite(number));
  23. // 3. Let dp be StringIndexOf(s, ".", 0).
  24. auto decimal_point = string.find('.');
  25. Variant<Empty, double, StringView> integer_part;
  26. StringView fraction_slice;
  27. // 4. If dp = -1, then
  28. if (!decimal_point.has_value()) {
  29. // a. Let intPart be n.
  30. integer_part = number;
  31. // b. Let fracSlice be "".
  32. }
  33. // 5. Else,
  34. else {
  35. // a. Let intPart be the substring of s from 0 to dp.
  36. integer_part = string.substring_view(0, *decimal_point);
  37. // b. Let fracSlice be the substring of s from dp + 1.
  38. fraction_slice = string.substring_view(*decimal_point + 1);
  39. }
  40. // 6. Let i be abs(! ToNumber(intPart)).
  41. auto integer = integer_part.visit(
  42. [](Empty) -> u64 { VERIFY_NOT_REACHED(); },
  43. [](double value) {
  44. return static_cast<u64>(fabs(value));
  45. },
  46. [](StringView value) {
  47. auto value_as_int = value.template to_int<i64>().value();
  48. return static_cast<u64>(value_as_int);
  49. });
  50. // 7. Let fracDigitCount be the length of fracSlice.
  51. auto fraction_digit_count = fraction_slice.length();
  52. // 8. Let f be ! ToNumber(fracSlice).
  53. auto fraction = fraction_slice.is_empty() ? 0u : fraction_slice.template to_uint<u64>().value();
  54. // 9. Let significantFracSlice be the value of fracSlice stripped of trailing "0".
  55. auto significant_fraction_slice = fraction_slice.trim("0"sv, TrimMode::Right);
  56. // 10. Let significantFracDigitCount be the length of significantFracSlice.
  57. auto significant_fraction_digit_count = significant_fraction_slice.length();
  58. // 11. Let significantFrac be ! ToNumber(significantFracSlice).
  59. auto significant_fraction = significant_fraction_slice.is_empty() ? 0u : significant_fraction_slice.template to_uint<u64>().value();
  60. // 12. Return a new Record { [[Number]]: abs(n), [[IntegerDigits]]: i, [[FractionDigits]]: f, [[NumberOfFractionDigits]]: fracDigitCount, [[FractionDigitsWithoutTrailing]]: significantFrac, [[NumberOfFractionDigitsWithoutTrailing]]: significantFracDigitCount }.
  61. return ::Locale::PluralOperands {
  62. .number = fabs(number),
  63. .integer_digits = integer,
  64. .fraction_digits = fraction,
  65. .number_of_fraction_digits = fraction_digit_count,
  66. .fraction_digits_without_trailing = significant_fraction,
  67. .number_of_fraction_digits_without_trailing = significant_fraction_digit_count,
  68. };
  69. }
  70. // 16.5.2 PluralRuleSelect ( locale, type, n, operands ), https://tc39.es/ecma402/#sec-pluralruleselect
  71. ::Locale::PluralCategory plural_rule_select(StringView locale, ::Locale::PluralForm type, Value, ::Locale::PluralOperands operands)
  72. {
  73. return ::Locale::determine_plural_category(locale, type, move(operands));
  74. }
  75. // 16.5.3 ResolvePlural ( pluralRules, n ), https://tc39.es/ecma402/#sec-resolveplural
  76. ThrowCompletionOr<ResolvedPlurality> resolve_plural(VM& vm, PluralRules const& plural_rules, Value number)
  77. {
  78. return resolve_plural(vm, plural_rules, plural_rules.type(), number);
  79. }
  80. // Non-standard overload of ResolvePlural to allow using the AO without an Intl.PluralRules object.
  81. ThrowCompletionOr<ResolvedPlurality> resolve_plural(VM& vm, NumberFormatBase const& number_format, ::Locale::PluralForm type, Value number)
  82. {
  83. // 1. Assert: Type(pluralRules) is Object.
  84. // 2. Assert: pluralRules has an [[InitializedPluralRules]] internal slot.
  85. // 3. Assert: Type(n) is Number.
  86. // 4. If n is not a finite Number, then
  87. if (!number.is_finite_number()) {
  88. // a. Return "other".
  89. return ResolvedPlurality { ::Locale::PluralCategory::Other, String {} };
  90. }
  91. // 5. Let locale be pluralRules.[[Locale]].
  92. auto const& locale = number_format.locale();
  93. // 6. Let type be pluralRules.[[Type]].
  94. // 7. Let res be ! FormatNumericToString(pluralRules, n).
  95. auto result = MUST_OR_THROW_OOM(format_numeric_to_string(vm, number_format, number));
  96. // 8. Let s be res.[[FormattedString]].
  97. auto string = move(result.formatted_string);
  98. // 9. Let operands be ! GetOperands(s).
  99. auto operands = get_operands(string);
  100. // 10. Let p be ! PluralRuleSelect(locale, type, n, operands).
  101. auto plural_category = plural_rule_select(locale, type, number, move(operands));
  102. // 11. Return the Record { [[PluralCategory]]: p, [[FormattedString]]: s }.
  103. return ResolvedPlurality { plural_category, move(string) };
  104. }
  105. // 16.5.4 PluralRuleSelectRange ( locale, type, xp, yp ), https://tc39.es/ecma402/#sec-resolveplural
  106. ::Locale::PluralCategory plural_rule_select_range(StringView locale, ::Locale::PluralForm, ::Locale::PluralCategory start, ::Locale::PluralCategory end)
  107. {
  108. return ::Locale::determine_plural_range(locale, start, end);
  109. }
  110. // 16.5.5 ResolvePluralRange ( pluralRules, x, y ), https://tc39.es/ecma402/#sec-resolveplural
  111. ThrowCompletionOr<::Locale::PluralCategory> resolve_plural_range(VM& vm, PluralRules const& plural_rules, Value start, Value end)
  112. {
  113. // 1. Assert: Type(pluralRules) is Object.
  114. // 2. Assert: pluralRules has an [[InitializedPluralRules]] internal slot.
  115. // 3. Assert: Type(x) is Number.
  116. // 4. Assert: Type(y) is Number.
  117. // 5. If x is NaN or y is NaN, throw a RangeError exception.
  118. if (start.is_nan())
  119. return vm.throw_completion<RangeError>(ErrorType::NumberIsNaN, "start"sv);
  120. if (end.is_nan())
  121. return vm.throw_completion<RangeError>(ErrorType::NumberIsNaN, "end"sv);
  122. // 6. Let xp be ! ResolvePlural(pluralRules, x).
  123. auto start_plurality = MUST_OR_THROW_OOM(resolve_plural(vm, plural_rules, start));
  124. // 7. Let yp be ! ResolvePlural(pluralRules, y).
  125. auto end_plurality = MUST_OR_THROW_OOM(resolve_plural(vm, plural_rules, end));
  126. // 8. If xp.[[FormattedString]] is yp.[[FormattedString]], then
  127. if (start_plurality.formatted_string == end_plurality.formatted_string) {
  128. // a. Return xp.[[PluralCategory]].
  129. return start_plurality.plural_category;
  130. }
  131. // 9. Let locale be pluralRules.[[Locale]].
  132. auto const& locale = plural_rules.locale();
  133. // 10. Let type be pluralRules.[[Type]].
  134. auto type = plural_rules.type();
  135. // 11. Return ! PluralRuleSelectRange(locale, type, xp.[[PluralCategory]], yp.[[PluralCategory]]).
  136. return plural_rule_select_range(locale, type, start_plurality.plural_category, end_plurality.plural_category);
  137. }
  138. }