summaryrefslogtreecommitdiffstats
path: root/intl/components/src/NumberFormatterSkeleton.h
blob: 134e9e086095ed91773deff6c625ae26e1c32859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef intl_components_NumberFormatterSkeleton_h_
#define intl_components_NumberFormatterSkeleton_h_
#include <string_view>
#include "mozilla/intl/NumberFormat.h"
#include "mozilla/intl/NumberRangeFormat.h"
#include "mozilla/Vector.h"
#include "unicode/unumberformatter.h"
#include "unicode/utypes.h"

struct UNumberRangeFormatter;

namespace mozilla::intl {

/**
 * Class to create a number formatter skeleton.
 *
 * The skeleton syntax is documented at:
 * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md
 */
class MOZ_STACK_CLASS NumberFormatterSkeleton final {
 public:
  explicit NumberFormatterSkeleton(const NumberFormatOptions& options);

  /**
   * Return a new UNumberFormatter based on this skeleton.
   */
  UNumberFormatter* toFormatter(std::string_view locale);

  /**
   * Return a new UNumberRangeFormatter based on this skeleton.
   */
  UNumberRangeFormatter* toRangeFormatter(
      std::string_view locale, NumberRangeFormatOptions::RangeCollapse collapse,
      NumberRangeFormatOptions::RangeIdentityFallback identity);

 private:
  static constexpr size_t DefaultVectorSize = 128;

  mozilla::Vector<char16_t, DefaultVectorSize> mVector;
  bool mValidSkeleton = false;

  [[nodiscard]] bool append(char16_t c) { return mVector.append(c); }

  [[nodiscard]] bool appendN(char16_t c, size_t times) {
    return mVector.appendN(c, times);
  }

  template <size_t N>
  [[nodiscard]] bool append(const char16_t (&chars)[N]) {
    static_assert(N > 0,
                  "should only be used with string literals or properly "
                  "null-terminated arrays");
    MOZ_ASSERT(chars[N - 1] == '\0',
               "should only be used with string literals or properly "
               "null-terminated arrays");
    // Without trailing \0.
    return mVector.append(chars, N - 1);
  }

  template <size_t N>
  [[nodiscard]] bool appendToken(const char16_t (&token)[N]) {
    return append(token) && append(' ');
  }

  [[nodiscard]] bool append(const char* chars, size_t length) {
    return mVector.append(chars, length);
  }

  [[nodiscard]] bool currency(std::string_view currency);

  [[nodiscard]] bool currencyDisplay(
      NumberFormatOptions::CurrencyDisplay display);

  [[nodiscard]] bool unit(std::string_view unit);

  [[nodiscard]] bool unitDisplay(NumberFormatOptions::UnitDisplay display);

  [[nodiscard]] bool percent();

  [[nodiscard]] bool fractionDigits(uint32_t min, uint32_t max,
                                    bool stripTrailingZero);

  [[nodiscard]] bool fractionWithSignificantDigits(uint32_t mnfd, uint32_t mxfd,
                                                   uint32_t mnsd, uint32_t mxsd,
                                                   bool relaxed,
                                                   bool stripTrailingZero);

  [[nodiscard]] bool minIntegerDigits(uint32_t min);

  [[nodiscard]] bool significantDigits(uint32_t min, uint32_t max,
                                       bool stripTrailingZero);

  [[nodiscard]] bool grouping(NumberFormatOptions::Grouping grouping);

  [[nodiscard]] bool notation(NumberFormatOptions::Notation style);

  [[nodiscard]] bool signDisplay(NumberFormatOptions::SignDisplay display);

  [[nodiscard]] bool roundingIncrement(uint32_t increment, uint32_t mnfd,
                                       uint32_t mxfd, bool stripTrailingZero);

  [[nodiscard]] bool roundingMode(NumberFormatOptions::RoundingMode rounding);
};

}  // namespace mozilla::intl

#endif