NumberFormat.cpp 5.1 KB

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