NumberFormat.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf8View.h>
  7. #include <LibUnicode/CharacterTypes.h>
  8. #include <LibUnicode/Locale.h>
  9. #include <LibUnicode/NumberFormat.h>
  10. #include <LibUnicode/UnicodeSymbols.h>
  11. #if ENABLE_UNICODE_DATA
  12. # include <LibUnicode/UnicodeData.h>
  13. #endif
  14. namespace Unicode {
  15. Optional<StringView> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol)
  16. {
  17. static auto const& symbols = Detail::Symbols::ensure_loaded();
  18. return symbols.get_number_system_symbol(locale, system, symbol);
  19. }
  20. Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system)
  21. {
  22. static auto const& symbols = Detail::Symbols::ensure_loaded();
  23. return symbols.get_number_system_groupings(locale, system);
  24. }
  25. Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type)
  26. {
  27. static auto const& symbols = Detail::Symbols::ensure_loaded();
  28. return symbols.get_standard_number_system_format(locale, system, type);
  29. }
  30. Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type)
  31. {
  32. static auto const& symbols = Detail::Symbols::ensure_loaded();
  33. return symbols.get_compact_number_system_formats(locale, system, type);
  34. }
  35. Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style)
  36. {
  37. static auto const& symbols = Detail::Symbols::ensure_loaded();
  38. return symbols.get_unit_formats(locale, unit, style);
  39. }
  40. Optional<NumberFormat> select_pattern_with_plurality(Vector<NumberFormat> const& formats, double number)
  41. {
  42. // FIXME: This is a rather naive and locale-unaware implementation Unicode's TR-35 pluralization
  43. // rules: https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
  44. // Once those rules are implemented for LibJS, we better use them instead.
  45. auto find_plurality = [&](auto plurality) -> Optional<NumberFormat> {
  46. if (auto it = formats.find_if([&](auto& patterns) { return patterns.plurality == plurality; }); it != formats.end())
  47. return *it;
  48. return {};
  49. };
  50. if (number == 0) {
  51. if (auto patterns = find_plurality(NumberFormat::Plurality::Zero); patterns.has_value())
  52. return patterns;
  53. } else if (number == 1) {
  54. if (auto patterns = find_plurality(NumberFormat::Plurality::One); patterns.has_value())
  55. return patterns;
  56. } else if (number == 2) {
  57. if (auto patterns = find_plurality(NumberFormat::Plurality::Two); patterns.has_value())
  58. return patterns;
  59. } else if (number > 2) {
  60. if (auto patterns = find_plurality(NumberFormat::Plurality::Many); patterns.has_value())
  61. return patterns;
  62. }
  63. return find_plurality(NumberFormat::Plurality::Other);
  64. }
  65. // https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
  66. Optional<String> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
  67. {
  68. #if ENABLE_UNICODE_DATA
  69. constexpr auto number_key = "{number}"sv;
  70. constexpr auto currency_key = "{currency}"sv;
  71. constexpr auto spacing = "\u00A0"sv; // No-Break Space (NBSP)
  72. auto number_index = base_pattern.find(number_key);
  73. VERIFY(number_index.has_value());
  74. auto currency_index = base_pattern.find(currency_key);
  75. VERIFY(currency_index.has_value());
  76. Utf8View utf8_currency_display { currency_display };
  77. Optional<String> currency_key_with_spacing;
  78. auto last_code_point = [](StringView string) {
  79. Utf8View utf8_string { string };
  80. u32 code_point = 0;
  81. for (auto it = utf8_string.begin(); it != utf8_string.end(); ++it)
  82. code_point = *it;
  83. return code_point;
  84. };
  85. if (*number_index < *currency_index) {
  86. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *currency_index));
  87. if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) {
  88. u32 first_currency_code_point = *utf8_currency_display.begin();
  89. if (!code_point_has_general_category(first_currency_code_point, GeneralCategory::Symbol))
  90. currency_key_with_spacing = String::formatted("{}{}", spacing, currency_key);
  91. }
  92. } else {
  93. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index));
  94. if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) {
  95. u32 last_currency_code_point = last_code_point(currency_display);
  96. if (!code_point_has_general_category(last_currency_code_point, GeneralCategory::Symbol))
  97. currency_key_with_spacing = String::formatted("{}{}", currency_key, spacing);
  98. }
  99. }
  100. if (currency_key_with_spacing.has_value())
  101. return base_pattern.replace(currency_key, *currency_key_with_spacing);
  102. #endif
  103. return {};
  104. }
  105. }