Debian Codegraph @

intl/icu/source/i18n/messageformat2_function_registry_internal.h

raw
1 // © 2024 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #ifndef U_HIDE_DEPRECATED_API
7
8 #ifndef MESSAGEFORMAT2_FUNCTION_REGISTRY_INTERNAL_H
9 #define MESSAGEFORMAT2_FUNCTION_REGISTRY_INTERNAL_H
10
11 #if U_SHOW_CPLUSPLUS_API
12
13 #if !UCONFIG_NO_NORMALIZATION
14
15 #if !UCONFIG_NO_FORMATTING
16
17 #if !UCONFIG_NO_MF2
18
19 #include "unicode/datefmt.h"
20 #include "unicode/messageformat2_function_registry.h"
21
22 U_NAMESPACE_BEGIN
23
24 namespace message2 {
25
26 // Built-in functions
27 /*
28 The standard functions are :datetime, :date, :time,
29 :number, :integer, and :string,
30 per https://github.com/unicode-org/message-format-wg/blob/main/spec/registry.md
31 as of https://github.com/unicode-org/message-format-wg/releases/tag/LDML45-alpha
32 */
33 class StandardFunctions {
34 friend class MessageFormatter;
35
36 static UnicodeString getStringOption(const FunctionOptions& opts,
37 const UnicodeString& optionName,
38 UErrorCode& errorCode);
39
40 class DateTime;
41
42 class DateTimeFactory : public FormatterFactory {
43 public:
44 Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
45 static DateTimeFactory* date(UErrorCode&);
46 static DateTimeFactory* time(UErrorCode&);
47 static DateTimeFactory* dateTime(UErrorCode&);
48 DateTimeFactory() = delete;
49 virtual ~DateTimeFactory();
50
51 private:
52 friend class DateTime;
53
54 typedef enum DateTimeType {
55 Date,
56 Time,
57 DateTime
58 } DateTimeType;
59
60 DateTimeType type;
61 DateTimeFactory(DateTimeType t) : type(t) {}
62 };
63
64 class DateTime : public Formatter {
65 public:
66 FormattedPlaceholder format(FormattedPlaceholder&& toFormat, FunctionOptions&& options, UErrorCode& status) const override;
67 virtual ~DateTime();
68
69 private:
70 const Locale& locale;
71 const DateTimeFactory::DateTimeType type;
72 friend class DateTimeFactory;
73 DateTime(const Locale& l, DateTimeFactory::DateTimeType t) : locale(l), type(t) {}
74 const LocalPointer<icu::DateFormat> icuFormatter;
75
76 /*
77 Looks up an option by name, first checking `opts`, then the cached options
78 in `toFormat` if applicable, and finally using a default
79
80 Ignores any options with non-string values
81 */
82 UnicodeString getFunctionOption(const FormattedPlaceholder& toFormat,
83 const FunctionOptions& opts,
84 const UnicodeString& optionName) const;
85 // Version for options that don't have defaults; sets the error
86 // code instead of returning a default value
87 UnicodeString getFunctionOption(const FormattedPlaceholder& toFormat,
88 const FunctionOptions& opts,
89 const UnicodeString& optionName,
90 UErrorCode& errorCode) const;
91
92 };
93
94 // Note: IntegerFactory doesn't implement SelectorFactory;
95 // instead, an instance of PluralFactory is registered to the integer
96 // selector
97 // TODO
98 class IntegerFactory : public FormatterFactory {
99 public:
100 Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
101 virtual ~IntegerFactory();
102 };
103
104 class NumberFactory : public FormatterFactory {
105 public:
106 Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
107 virtual ~NumberFactory();
108 private:
109 friend class IntegerFactory;
110 static NumberFactory integer(const Locale& locale, UErrorCode& status);
111 };
112
113 class Number : public Formatter {
114 public:
115 FormattedPlaceholder format(FormattedPlaceholder&& toFormat, FunctionOptions&& options, UErrorCode& status) const override;
116 virtual ~Number();
117
118 private:
119 friend class NumberFactory;
120 friend class StandardFunctions;
121
122 Number(const Locale& loc) : locale(loc), icuFormatter(number::NumberFormatter::withLocale(loc)) {}
123 Number(const Locale& loc, bool isInt) : locale(loc), isInteger(isInt), icuFormatter(number::NumberFormatter::withLocale(loc)) {}
124 static Number integer(const Locale& loc);
125
126 // These options have their own accessor methods, since they have different default values.
127 int32_t maximumFractionDigits(const FunctionOptions& options) const;
128 int32_t minimumFractionDigits(const FunctionOptions& options) const;
129 int32_t minimumSignificantDigits(const FunctionOptions& options) const;
130 int32_t maximumSignificantDigits(const FunctionOptions& options) const;
131 int32_t minimumIntegerDigits(const FunctionOptions& options) const;
132
133 bool usePercent(const FunctionOptions& options) const;
134 const Locale& locale;
135 const bool isInteger = false;
136 const number::LocalizedNumberFormatter icuFormatter;
137 };
138
139 static number::LocalizedNumberFormatter formatterForOptions(const Number& number,
140 const FunctionOptions& opts,
141 UErrorCode& status);
142
143 class PluralFactory : public SelectorFactory {
144 public:
145 Selector* createSelector(const Locale& locale, UErrorCode& status) const override;
146 virtual ~PluralFactory();
147
148 private:
149 friend class IntegerFactory;
150 friend class MessageFormatter;
151
152 PluralFactory() {}
153 PluralFactory(bool isInt) : isInteger(isInt) {}
154 static PluralFactory integer() { return PluralFactory(true);}
155 const bool isInteger = false;
156 };
157
158 class Plural : public Selector {
159 public:
160 void selectKey(FormattedPlaceholder&& val,
161 FunctionOptions&& options,
162 const UnicodeString* keys,
163 int32_t keysLen,
164 UnicodeString* prefs,
165 int32_t& prefsLen,
166 UErrorCode& status) const override;
167 virtual ~Plural();
168
169 private:
170 friend class IntegerFactory;
171 friend class PluralFactory;
172
173 // Can't use UPluralType for this since we want to include
174 // exact matching as an option
175 typedef enum PluralType {
176 PLURAL_ORDINAL,
177 PLURAL_CARDINAL,
178 PLURAL_EXACT
179 } PluralType;
180 Plural(const Locale& loc, UErrorCode& errorCode);
181 Plural(const Locale& loc, bool isInt, UErrorCode& errorCode);
182 static Plural integer(const Locale& loc, UErrorCode& errorCode) { return Plural(loc, true, errorCode); }
183 PluralType pluralType(const FunctionOptions& opts) const;
184 const Locale& locale;
185 const bool isInteger = false;
186 LocalPointer<StandardFunctions::Number> numberFormatter;
187 };
188
189 class TextFactory : public SelectorFactory {
190 public:
191 Selector* createSelector(const Locale& locale, UErrorCode& status) const override;
192 virtual ~TextFactory();
193 };
194
195 class TextSelector : public Selector {
196 public:
197 void selectKey(FormattedPlaceholder&& val,
198 FunctionOptions&& options,
199 const UnicodeString* keys,
200 int32_t keysLen,
201 UnicodeString* prefs,
202 int32_t& prefsLen,
203 UErrorCode& status) const override;
204 virtual ~TextSelector();
205
206 private:
207 friend class TextFactory;
208
209 // Formatting `value` to a string might require the locale
210 const Locale& locale;
211
212 TextSelector(const Locale& l) : locale(l) {}
213 };
214
215 // See https://github.com/unicode-org/message-format-wg/blob/main/test/README.md
216 class TestFormatFactory : public FormatterFactory {
217 public:
218 Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
219 TestFormatFactory() {}
220 virtual ~TestFormatFactory();
221 };
222
223 class TestSelect;
224
225 class TestFormat : public Formatter {
226 public:
227 FormattedPlaceholder format(FormattedPlaceholder&& toFormat, FunctionOptions&& options, UErrorCode& status) const override;
228 virtual ~TestFormat();
229
230 private:
231 friend class TestFormatFactory;
232 friend class TestSelect;
233 TestFormat() {}
234 static void testFunctionParameters(const FormattedPlaceholder& arg,
235 const FunctionOptions& options,
236 int32_t& decimalPlaces,
237 bool& failsFormat,
238 bool& failsSelect,
239 double& input,
240 UErrorCode& status);
241
242 };
243
244 // See https://github.com/unicode-org/message-format-wg/blob/main/test/README.md
245 class TestSelectFactory : public SelectorFactory {
246 public:
247 Selector* createSelector(const Locale& locale, UErrorCode& status) const override;
248 TestSelectFactory() {}
249 virtual ~TestSelectFactory();
250 };
251
252 class TestSelect : public Selector {
253 public:
254 void selectKey(FormattedPlaceholder&& val,
255 FunctionOptions&& options,
256 const UnicodeString* keys,
257 int32_t keysLen,
258 UnicodeString* prefs,
259 int32_t& prefsLen,
260 UErrorCode& status) const override;
261 virtual ~TestSelect();
262
263 private:
264 friend class TestSelectFactory;
265 TestSelect() {}
266 };
267
268 };
269
270 extern void formatDateWithDefaults(const Locale& locale, UDate date, UnicodeString&, UErrorCode& errorCode);
271 extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, double toFormat, UErrorCode& errorCode);
272 extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int32_t toFormat, UErrorCode& errorCode);
273 extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int64_t toFormat, UErrorCode& errorCode);
274 extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, StringPiece toFormat, UErrorCode& errorCode);
275 extern DateFormat* defaultDateTimeInstance(const Locale&, UErrorCode&);
276
277 } // namespace message2
278
279 U_NAMESPACE_END
280
281 #endif /* #if !UCONFIG_NO_MF2 */
282
283 #endif /* #if !UCONFIG_NO_FORMATTING */
284
285 #endif /* #if !UCONFIG_NO_NORMALIZATION */
286
287 #endif /* U_SHOW_CPLUSPLUS_API */
288
289 #endif // MESSAGEFORMAT2_FUNCTION_REGISTRY_INTERNAL_H
290
291 #endif // U_HIDE_DEPRECATED_API
292 // eof