NumberFormat.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. };
  59. Optional<StringView> get_default_number_system(StringView locale);
  60. Optional<StringView> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol);
  61. Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system);
  62. Optional<Span<u32 const>> get_digits_for_number_system(StringView system);
  63. String replace_digits_for_number_system(StringView system, StringView number);
  64. Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type);
  65. Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
  66. Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style);
  67. Optional<String> augment_currency_format_pattern(StringView currency_display, StringView base_pattern);
  68. template<typename FormatType>
  69. Optional<FormatType> select_pattern_with_plurality(Vector<FormatType> const& formats, double number)
  70. {
  71. // FIXME: This is a rather naive and locale-unaware implementation Unicode's TR-35 pluralization
  72. // rules: https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
  73. // Once those rules are implemented for LibJS, we better use them instead.
  74. auto find_plurality = [&](auto plurality) -> Optional<FormatType> {
  75. if (auto it = formats.find_if([&](auto& patterns) { return patterns.plurality == plurality; }); it != formats.end())
  76. return *it;
  77. return {};
  78. };
  79. if (number == 0) {
  80. if (auto patterns = find_plurality(FormatType::Plurality::Zero); patterns.has_value())
  81. return patterns;
  82. } else if (number == 1) {
  83. if (auto patterns = find_plurality(FormatType::Plurality::One); patterns.has_value())
  84. return patterns;
  85. } else if (number == 2) {
  86. if (auto patterns = find_plurality(FormatType::Plurality::Two); patterns.has_value())
  87. return patterns;
  88. } else if (number > 2) {
  89. if (auto patterns = find_plurality(FormatType::Plurality::Many); patterns.has_value())
  90. return patterns;
  91. }
  92. return find_plurality(FormatType::Plurality::Other);
  93. }
  94. }