Debian Codegraph @

intl/components/src/NumberFormatterSkeleton.h

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #ifndef intl_components_NumberFormatterSkeleton_h_
5 #define intl_components_NumberFormatterSkeleton_h_
6 #include <string_view>
7 #include "mozilla/intl/NumberFormat.h"
8 #include "mozilla/intl/NumberRangeFormat.h"
9 #include "mozilla/Vector.h"
10 #include "unicode/unumberformatter.h"
11 #include "unicode/utypes.h"
12
13 struct UNumberRangeFormatter;
14
15 namespace mozilla::intl {
16
17 /**
18 * Class to create a number formatter skeleton.
19 *
20 * The skeleton syntax is documented at:
21 * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md
22 */
23 class MOZ_STACK_CLASS NumberFormatterSkeleton final {
24 public:
25 explicit NumberFormatterSkeleton(const NumberFormatOptions& options);
26
27 /**
28 * Return a new UNumberFormatter based on this skeleton.
29 */
30 UNumberFormatter* toFormatter(std::string_view locale);
31
32 /**
33 * Return a new UNumberRangeFormatter based on this skeleton.
34 */
35 UNumberRangeFormatter* toRangeFormatter(
36 std::string_view locale, NumberRangeFormatOptions::RangeCollapse collapse,
37 NumberRangeFormatOptions::RangeIdentityFallback identity);
38
39 private:
40 static constexpr size_t DefaultVectorSize = 128;
41
42 mozilla::Vector<char16_t, DefaultVectorSize> mVector;
43 bool mValidSkeleton = false;
44
45 [[nodiscard]] bool append(char16_t c) { return mVector.append(c); }
46
47 [[nodiscard]] bool appendN(char16_t c, size_t times) {
48 return mVector.appendN(c, times);
49 }
50
51 template <size_t N>
52 [[nodiscard]] bool append(const char16_t (&chars)[N]) {
53 static_assert(N > 0,
54 "should only be used with string literals or properly "
55 "null-terminated arrays");
56 MOZ_ASSERT(chars[N - 1] == '\0',
57 "should only be used with string literals or properly "
58 "null-terminated arrays");
59 // Without trailing \0.
60 return mVector.append(chars, N - 1);
61 }
62
63 template <size_t N>
64 [[nodiscard]] bool appendToken(const char16_t (&token)[N]) {
65 return append(token) && append(' ');
66 }
67
68 [[nodiscard]] bool append(const char* chars, size_t length) {
69 return mVector.append(chars, length);
70 }
71
72 [[nodiscard]] bool currency(std::string_view currency);
73
74 [[nodiscard]] bool currencyDisplay(
75 NumberFormatOptions::CurrencyDisplay display);
76
77 [[nodiscard]] bool unit(std::string_view unit);
78
79 [[nodiscard]] bool unitDisplay(NumberFormatOptions::UnitDisplay display);
80
81 [[nodiscard]] bool percent();
82
83 [[nodiscard]] bool fractionDigits(uint32_t min, uint32_t max,
84 bool stripTrailingZero);
85
86 [[nodiscard]] bool fractionWithSignificantDigits(uint32_t mnfd, uint32_t mxfd,
87 uint32_t mnsd, uint32_t mxsd,
88 bool relaxed,
89 bool stripTrailingZero);
90
91 [[nodiscard]] bool minIntegerDigits(uint32_t min);
92
93 [[nodiscard]] bool significantDigits(uint32_t min, uint32_t max,
94 bool stripTrailingZero);
95
96 [[nodiscard]] bool grouping(NumberFormatOptions::Grouping grouping);
97
98 [[nodiscard]] bool notation(NumberFormatOptions::Notation style);
99
100 [[nodiscard]] bool signDisplay(NumberFormatOptions::SignDisplay display);
101
102 [[nodiscard]] bool roundingIncrement(uint32_t increment, uint32_t mnfd,
103 uint32_t mxfd, bool stripTrailingZero);
104
105 [[nodiscard]] bool roundingMode(NumberFormatOptions::RoundingMode rounding);
106 };
107
108 } // namespace mozilla::intl
109
110 #endif