NumberFormat.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2021, 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 <LibUnicode/CharacterTypes.h>
  9. #include <LibUnicode/Locale.h>
  10. #include <LibUnicode/NumberFormat.h>
  11. #if ENABLE_UNICODE_DATA
  12. # include <LibUnicode/UnicodeData.h>
  13. #endif
  14. namespace Unicode {
  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<StringView> get_default_number_system(StringView locale)
  21. {
  22. if (auto systems = get_keywords_for_locale(locale, "nu"sv); !systems.is_empty())
  23. return systems[0];
  24. return {};
  25. }
  26. Optional<Span<u32 const>> __attribute__((weak)) get_digits_for_number_system(StringView)
  27. {
  28. // Fall back to "latn" digits when Unicode data generation is disabled.
  29. constexpr Array<u32, 10> digits { { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 } };
  30. return digits.span();
  31. }
  32. String replace_digits_for_number_system(StringView system, StringView number)
  33. {
  34. auto digits = get_digits_for_number_system(system);
  35. if (!digits.has_value())
  36. digits = get_digits_for_number_system("latn"sv);
  37. VERIFY(digits.has_value());
  38. StringBuilder builder;
  39. for (auto ch : number) {
  40. if (is_ascii_digit(ch)) {
  41. u32 digit = digits->at(parse_ascii_digit(ch));
  42. builder.append_code_point(digit);
  43. } else {
  44. builder.append(ch);
  45. }
  46. }
  47. return builder.build();
  48. }
  49. // https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
  50. Optional<String> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
  51. {
  52. #if ENABLE_UNICODE_DATA
  53. constexpr auto number_key = "{number}"sv;
  54. constexpr auto currency_key = "{currency}"sv;
  55. constexpr auto spacing = "\u00A0"sv; // No-Break Space (NBSP)
  56. auto number_index = base_pattern.find(number_key);
  57. VERIFY(number_index.has_value());
  58. auto currency_index = base_pattern.find(currency_key);
  59. VERIFY(currency_index.has_value());
  60. Utf8View utf8_currency_display { currency_display };
  61. Optional<String> currency_key_with_spacing;
  62. auto last_code_point = [](StringView string) {
  63. Utf8View utf8_string { string };
  64. u32 code_point = 0;
  65. for (auto it = utf8_string.begin(); it != utf8_string.end(); ++it)
  66. code_point = *it;
  67. return code_point;
  68. };
  69. if (*number_index < *currency_index) {
  70. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *currency_index));
  71. if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) {
  72. u32 first_currency_code_point = *utf8_currency_display.begin();
  73. if (!code_point_has_general_category(first_currency_code_point, GeneralCategory::Symbol))
  74. currency_key_with_spacing = String::formatted("{}{}", spacing, currency_key);
  75. }
  76. } else {
  77. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index));
  78. if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) {
  79. u32 last_currency_code_point = last_code_point(currency_display);
  80. if (!code_point_has_general_category(last_currency_code_point, GeneralCategory::Symbol))
  81. currency_key_with_spacing = String::formatted("{}{}", currency_key, spacing);
  82. }
  83. }
  84. if (currency_key_with_spacing.has_value())
  85. return base_pattern.replace(currency_key, *currency_key_with_spacing, ReplaceMode::FirstOnly);
  86. #endif
  87. return {};
  88. }
  89. }