NumberFormat.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <AK/String.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Vector.h>
  11. #include <LibUnicode/Forward.h>
  12. namespace Unicode {
  13. struct NumberGroupings {
  14. u8 minimum_grouping_digits { 0 };
  15. u8 primary_grouping_size { 0 };
  16. u8 secondary_grouping_size { 0 };
  17. };
  18. enum class StandardNumberFormatType : u8 {
  19. Decimal,
  20. Currency,
  21. Accounting,
  22. Percent,
  23. Scientific,
  24. };
  25. enum class CompactNumberFormatType : u8 {
  26. DecimalLong,
  27. DecimalShort,
  28. CurrencyUnit,
  29. CurrencyShort,
  30. };
  31. struct NumberFormat {
  32. enum class Plurality : u8 {
  33. Other,
  34. Zero,
  35. Single,
  36. One,
  37. Two,
  38. Few,
  39. Many,
  40. };
  41. u8 magnitude { 0 };
  42. u8 exponent { 0 };
  43. Plurality plurality { Plurality::Other };
  44. StringView zero_format {};
  45. StringView positive_format {};
  46. StringView negative_format {};
  47. Vector<StringView> identifiers {};
  48. };
  49. enum class NumericSymbol : u8 {
  50. Decimal,
  51. Exponential,
  52. Group,
  53. Infinity,
  54. MinusSign,
  55. NaN,
  56. PercentSign,
  57. PlusSign,
  58. TimeSeparator,
  59. };
  60. Optional<StringView> get_default_number_system(StringView locale);
  61. Optional<StringView> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol);
  62. Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system);
  63. Optional<Span<u32 const>> get_digits_for_number_system(StringView system);
  64. String replace_digits_for_number_system(StringView system, StringView number);
  65. Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type);
  66. Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
  67. Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style);
  68. Optional<String> augment_currency_format_pattern(StringView currency_display, StringView base_pattern);
  69. template<typename FormatType>
  70. Optional<FormatType> select_pattern_with_plurality(Vector<FormatType> const& formats, double number)
  71. {
  72. // FIXME: This is a rather naive and locale-unaware implementation Unicode's TR-35 pluralization
  73. // rules: https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
  74. // Once those rules are implemented for LibJS, we better use them instead.
  75. auto find_plurality = [&](auto plurality) -> Optional<FormatType> {
  76. if (auto it = formats.find_if([&](auto& patterns) { return patterns.plurality == plurality; }); it != formats.end())
  77. return *it;
  78. return {};
  79. };
  80. if (number == 0) {
  81. if (auto patterns = find_plurality(FormatType::Plurality::Zero); patterns.has_value())
  82. return patterns;
  83. } else if (number == 1) {
  84. if (auto patterns = find_plurality(FormatType::Plurality::One); patterns.has_value())
  85. return patterns;
  86. } else if (number == 2) {
  87. if (auto patterns = find_plurality(FormatType::Plurality::Two); patterns.has_value())
  88. return patterns;
  89. } else if (number > 2) {
  90. if (auto patterns = find_plurality(FormatType::Plurality::Many); patterns.has_value())
  91. return patterns;
  92. }
  93. return find_plurality(FormatType::Plurality::Other);
  94. }
  95. }