PluralRules.cpp 6.8 KB

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