NumberFormat.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Span<StringView const> __attribute__((weak)) get_available_number_systems() { return {}; }
  16. Optional<NumberSystem> __attribute__((weak)) number_system_from_string(StringView) { return {}; }
  17. Optional<StringView> __attribute__((weak)) get_number_system_symbol(StringView, StringView, NumericSymbol) { return {}; }
  18. Optional<NumberGroupings> __attribute__((weak)) get_number_system_groupings(StringView, StringView) { return {}; }
  19. Optional<NumberFormat> __attribute__((weak)) get_standard_number_system_format(StringView, StringView, StandardNumberFormatType) { return {}; }
  20. Vector<NumberFormat> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return {}; }
  21. Vector<NumberFormat> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return {}; }
  22. Optional<StringView> get_default_number_system(StringView locale)
  23. {
  24. if (auto systems = get_locale_key_mapping(locale, "nu"sv); systems.has_value()) {
  25. auto index = systems->find(',');
  26. return index.has_value() ? systems->substring_view(0, *index) : *systems;
  27. }
  28. return {};
  29. }
  30. Optional<Span<u32 const>> __attribute__((weak)) get_digits_for_number_system(StringView)
  31. {
  32. // Fall back to "latn" digits when Unicode data generation is disabled.
  33. constexpr Array<u32, 10> digits { { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 } };
  34. return digits.span();
  35. }
  36. String replace_digits_for_number_system(StringView system, StringView number)
  37. {
  38. auto digits = get_digits_for_number_system(system);
  39. if (!digits.has_value())
  40. digits = get_digits_for_number_system("latn"sv);
  41. VERIFY(digits.has_value());
  42. StringBuilder builder;
  43. for (auto ch : number) {
  44. if (is_ascii_digit(ch)) {
  45. u32 digit = digits->at(parse_ascii_digit(ch));
  46. builder.append_code_point(digit);
  47. } else {
  48. builder.append(ch);
  49. }
  50. }
  51. return builder.build();
  52. }
  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. auto last_code_point = [](StringView string) {
  67. Utf8View utf8_string { string };
  68. u32 code_point = 0;
  69. for (auto it = utf8_string.begin(); it != utf8_string.end(); ++it)
  70. code_point = *it;
  71. return code_point;
  72. };
  73. if (*number_index < *currency_index) {
  74. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *currency_index));
  75. if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) {
  76. u32 first_currency_code_point = *utf8_currency_display.begin();
  77. if (!code_point_has_general_category(first_currency_code_point, GeneralCategory::Symbol))
  78. currency_key_with_spacing = String::formatted("{}{}", spacing, currency_key);
  79. }
  80. } else {
  81. u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index));
  82. if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) {
  83. u32 last_currency_code_point = last_code_point(currency_display);
  84. if (!code_point_has_general_category(last_currency_code_point, GeneralCategory::Symbol))
  85. currency_key_with_spacing = String::formatted("{}{}", currency_key, spacing);
  86. }
  87. }
  88. if (currency_key_with_spacing.has_value())
  89. return base_pattern.replace(currency_key, *currency_key_with_spacing);
  90. #endif
  91. return {};
  92. }
  93. }