NumberFormat.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/Utf8View.h>
  8. #include <LibLocale/Locale.h>
  9. #include <LibLocale/NumberFormat.h>
  10. #include <LibUnicode/CharacterTypes.h>
  11. #if ENABLE_UNICODE_DATA
  12. # include <LibUnicode/UnicodeData.h>
  13. #endif
  14. namespace Locale {
  15. Optional<StringView> __attribute__((weak)) get_number_system_symbol(StringView, StringView, NumericSymbol) { return {}; }
  16. Optional<NumberGroupings> __attribute__((weak)) get_number_system_groupings(StringView, StringView) { return {}; }
  17. Optional<NumberFormat> __attribute__((weak)) get_standard_number_system_format(StringView, StringView, StandardNumberFormatType) { return {}; }
  18. Vector<NumberFormat> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return {}; }
  19. Vector<NumberFormat> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return {}; }
  20. Optional<ReadonlySpan<u32>> __attribute__((weak)) get_digits_for_number_system(StringView)
  21. {
  22. // Fall back to "latn" digits when Unicode data generation is disabled.
  23. constexpr Array<u32, 10> digits { { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 } };
  24. return digits.span();
  25. }
  26. String replace_digits_for_number_system(StringView system, StringView number)
  27. {
  28. auto digits = get_digits_for_number_system(system);
  29. if (!digits.has_value())
  30. digits = get_digits_for_number_system("latn"sv);
  31. VERIFY(digits.has_value());
  32. StringBuilder builder;
  33. for (auto ch : number) {
  34. if (is_ascii_digit(ch)) {
  35. u32 digit = digits->at(parse_ascii_digit(ch));
  36. builder.append_code_point(digit);
  37. } else {
  38. builder.append(ch);
  39. }
  40. }
  41. return MUST(builder.to_string());
  42. }
  43. #if ENABLE_UNICODE_DATA
  44. static u32 last_code_point(StringView string)
  45. {
  46. Utf8View utf8_string { string };
  47. u32 code_point = 0;
  48. for (auto it = utf8_string.begin(); it != utf8_string.end(); ++it)
  49. code_point = *it;
  50. return code_point;
  51. }
  52. #endif
  53. // https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
  54. Optional<String> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
  55. {
  56. #if ENABLE_UNICODE_DATA
  57. constexpr auto number_key = "{number}"sv;
  58. constexpr auto currency_key = "{currency}"sv;
  59. constexpr auto spacing = "\u00A0"sv; // No-Break Space (NBSP)
  60. auto number_index = base_pattern.find(number_key);
  61. VERIFY(number_index.has_value());
  62. auto currency_index = base_pattern.find(currency_key);
  63. VERIFY(currency_index.has_value());
  64. Utf8View utf8_currency_display { currency_display };
  65. Optional<String> currency_key_with_spacing;
  66. if (*number_index < *currency_index) {
  67. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *currency_index));
  68. if (!Unicode::code_point_has_general_category(last_pattern_code_point, Unicode::GeneralCategory::Separator)) {
  69. u32 first_currency_code_point = *utf8_currency_display.begin();
  70. if (!Unicode::code_point_has_general_category(first_currency_code_point, Unicode::GeneralCategory::Symbol))
  71. currency_key_with_spacing = MUST(String::formatted("{}{}", spacing, currency_key));
  72. }
  73. } else {
  74. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index));
  75. if (!Unicode::code_point_has_general_category(last_pattern_code_point, Unicode::GeneralCategory::Separator)) {
  76. u32 last_currency_code_point = last_code_point(currency_display);
  77. if (!Unicode::code_point_has_general_category(last_currency_code_point, Unicode::GeneralCategory::Symbol))
  78. currency_key_with_spacing = MUST(String::formatted("{}{}", currency_key, spacing));
  79. }
  80. }
  81. if (currency_key_with_spacing.has_value())
  82. return MUST(MUST(String::from_utf8(base_pattern)).replace(currency_key, *currency_key_with_spacing, ReplaceMode::FirstOnly));
  83. #endif
  84. return {};
  85. }
  86. // https://unicode.org/reports/tr35/tr35-numbers.html#83-range-pattern-processing
  87. Optional<String> augment_range_pattern([[maybe_unused]] StringView range_separator, [[maybe_unused]] StringView lower, [[maybe_unused]] StringView upper)
  88. {
  89. #if ENABLE_UNICODE_DATA
  90. auto range_pattern_with_spacing = [&]() {
  91. return MUST(String::formatted(" {} ", range_separator));
  92. };
  93. Utf8View utf8_range_separator { range_separator };
  94. Utf8View utf8_upper { upper };
  95. // NOTE: Our implementation does the prescribed checks backwards for simplicity.
  96. // To determine whether to add spacing, the currently recommended heuristic is:
  97. // 2. If the range pattern does not contain a character having the White_Space binary Unicode property after the {0} or before the {1} placeholders.
  98. for (auto it = utf8_range_separator.begin(); it != utf8_range_separator.end(); ++it) {
  99. if (Unicode::code_point_has_property(*it, Unicode::Property::White_Space))
  100. return {};
  101. }
  102. // 1. If the lower string ends with a character other than a digit, or if the upper string begins with a character other than a digit.
  103. if (auto it = utf8_upper.begin(); it != utf8_upper.end()) {
  104. if (!Unicode::code_point_has_general_category(*it, Unicode::GeneralCategory::Decimal_Number))
  105. return range_pattern_with_spacing();
  106. }
  107. if (!Unicode::code_point_has_general_category(last_code_point(lower), Unicode::GeneralCategory::Decimal_Number))
  108. return range_pattern_with_spacing();
  109. #endif
  110. return {};
  111. }
  112. }