NumberFormat.cpp 4.3 KB

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