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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include "unicode/utypes.h"
#ifndef U_HIDE_DEPRECATED_API
#ifndef MESSAGEFORMAT2_FUNCTION_REGISTRY_INTERNAL_H
#define MESSAGEFORMAT2_FUNCTION_REGISTRY_INTERNAL_H
#if U_SHOW_CPLUSPLUS_API
#if !UCONFIG_NO_NORMALIZATION
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_MF2
#include "unicode/datefmt.h"
#include "unicode/messageformat2_function_registry.h"
U_NAMESPACE_BEGIN
namespace message2 {
// Constants for option names
namespace options {
static constexpr std::u16string_view ALWAYS = u"always";
static constexpr std::u16string_view COMPACT = u"compact";
static constexpr std::u16string_view COMPACT_DISPLAY = u"compactDisplay";
static constexpr std::u16string_view DATE_STYLE = u"dateStyle";
static constexpr std::u16string_view DAY = u"day";
static constexpr std::u16string_view DECIMAL_PLACES = u"decimalPlaces";
static constexpr std::u16string_view DEFAULT_UPPER = u"DEFAULT";
static constexpr std::u16string_view ENGINEERING = u"engineering";
static constexpr std::u16string_view EXACT = u"exact";
static constexpr std::u16string_view EXCEPT_ZERO = u"exceptZero";
static constexpr std::u16string_view FAILS = u"fails";
static constexpr std::u16string_view FULL_UPPER = u"FULL";
static constexpr std::u16string_view HOUR = u"hour";
static constexpr std::u16string_view LONG = u"long";
static constexpr std::u16string_view LONG_UPPER = u"LONG";
static constexpr std::u16string_view MAXIMUM_FRACTION_DIGITS = u"maximumFractionDigits";
static constexpr std::u16string_view MAXIMUM_SIGNIFICANT_DIGITS = u"maximumSignificantDigits";
static constexpr std::u16string_view MEDIUM_UPPER = u"MEDIUM";
static constexpr std::u16string_view MIN2 = u"min2";
static constexpr std::u16string_view MINIMUM_FRACTION_DIGITS = u"minimumFractionDigits";
static constexpr std::u16string_view MINIMUM_INTEGER_DIGITS = u"minimumIntegerDigits";
static constexpr std::u16string_view MINIMUM_SIGNIFICANT_DIGITS = u"minimumSignificantDigits";
static constexpr std::u16string_view MINUTE = u"minute";
static constexpr std::u16string_view MONTH = u"month";
static constexpr std::u16string_view NARROW = u"narrow";
static constexpr std::u16string_view NEGATIVE = u"negative";
static constexpr std::u16string_view NEVER = u"never";
static constexpr std::u16string_view NOTATION = u"notation";
static constexpr std::u16string_view NUMBERING_SYSTEM = u"numberingSystem";
static constexpr std::u16string_view NUMERIC = u"numeric";
static constexpr std::u16string_view ORDINAL = u"ordinal";
static constexpr std::u16string_view PERCENT_STRING = u"percent";
static constexpr std::u16string_view SCIENTIFIC = u"scientific";
static constexpr std::u16string_view SECOND = u"second";
static constexpr std::u16string_view SELECT = u"select";
static constexpr std::u16string_view SHORT = u"short";
static constexpr std::u16string_view SHORT_UPPER = u"SHORT";
static constexpr std::u16string_view SIGN_DISPLAY = u"signDisplay";
static constexpr std::u16string_view STYLE = u"style";
static constexpr std::u16string_view TIME_STYLE = u"timeStyle";
static constexpr std::u16string_view TWO_DIGIT = u"2-digit";
static constexpr std::u16string_view USE_GROUPING = u"useGrouping";
static constexpr std::u16string_view WEEKDAY = u"weekday";
static constexpr std::u16string_view YEAR = u"year";
} // namespace options
// Built-in functions
/*
The standard functions are :datetime, :date, :time,
:number, :integer, and :string,
per https://github.com/unicode-org/message-format-wg/blob/main/spec/registry.md
as of https://github.com/unicode-org/message-format-wg/releases/tag/LDML45-alpha
*/
class StandardFunctions {
friend class MessageFormatter;
public:
// Used for normalizing variable names and keys for comparison
static UnicodeString normalizeNFC(const UnicodeString&);
private:
static void validateDigitSizeOptions(const FunctionOptions&, UErrorCode&);
static void checkSelectOption(const FunctionOptions&, UErrorCode&);
static UnicodeString getStringOption(const FunctionOptions& opts,
std::u16string_view optionName,
UErrorCode& errorCode);
class DateTime;
class DateTimeFactory : public FormatterFactory {
public:
Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
static DateTimeFactory* date(UErrorCode&);
static DateTimeFactory* time(UErrorCode&);
static DateTimeFactory* dateTime(UErrorCode&);
DateTimeFactory() = delete;
virtual ~DateTimeFactory();
private:
friend class DateTime;
typedef enum DateTimeType {
Date,
Time,
DateTime
} DateTimeType;
DateTimeType type;
DateTimeFactory(DateTimeType t) : type(t) {}
};
class DateTime : public Formatter {
public:
FormattedPlaceholder format(FormattedPlaceholder&& toFormat, FunctionOptions&& options, UErrorCode& status) const override;
virtual ~DateTime();
private:
const Locale& locale;
const DateTimeFactory::DateTimeType type;
friend class DateTimeFactory;
DateTime(const Locale& l, DateTimeFactory::DateTimeType t)
: locale(l), type(t) {}
const LocalPointer<icu::DateFormat> icuFormatter;
// Methods for parsing date literals
UDate tryPatterns(const UnicodeString&, UErrorCode&) const;
UDate tryTimeZonePatterns(const UnicodeString&, UErrorCode&) const;
DateInfo createDateInfoFromString(const UnicodeString&, UErrorCode&) const;
/*
Looks up an option by name, first checking `opts`, then the cached options
in `toFormat` if applicable, and finally using a default
Ignores any options with non-string values
*/
UnicodeString getFunctionOption(const FormattedPlaceholder& toFormat,
const FunctionOptions& opts,
std::u16string_view optionName) const;
// Version for options that don't have defaults; sets the error
// code instead of returning a default value
UnicodeString getFunctionOption(const FormattedPlaceholder& toFormat,
const FunctionOptions& opts,
std::u16string_view optionName,
UErrorCode& errorCode) const;
};
// Note: IntegerFactory doesn't implement SelectorFactory;
// instead, an instance of PluralFactory is registered to the integer
// selector
// TODO
class IntegerFactory : public FormatterFactory {
public:
Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
virtual ~IntegerFactory();
};
class NumberFactory : public FormatterFactory {
public:
Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
virtual ~NumberFactory();
private:
friend class IntegerFactory;
static NumberFactory integer(const Locale& locale, UErrorCode& status);
};
class Number : public Formatter {
public:
FormattedPlaceholder format(FormattedPlaceholder&& toFormat, FunctionOptions&& options, UErrorCode& status) const override;
virtual ~Number();
private:
friend class NumberFactory;
friend class StandardFunctions;
Number(const Locale& loc) : locale(loc), icuFormatter(number::NumberFormatter::withLocale(loc)) {}
Number(const Locale& loc, bool isInt) : locale(loc), isInteger(isInt), icuFormatter(number::NumberFormatter::withLocale(loc)) {}
static Number integer(const Locale& loc);
// These options have their own accessor methods, since they have different default values.
int32_t maximumFractionDigits(const FunctionOptions& options) const;
int32_t minimumFractionDigits(const FunctionOptions& options) const;
int32_t minimumSignificantDigits(const FunctionOptions& options) const;
int32_t maximumSignificantDigits(const FunctionOptions& options) const;
int32_t minimumIntegerDigits(const FunctionOptions& options) const;
bool usePercent(const FunctionOptions& options) const;
const Locale& locale;
const bool isInteger = false;
const number::LocalizedNumberFormatter icuFormatter;
};
static number::LocalizedNumberFormatter formatterForOptions(const Number& number,
const FunctionOptions& opts,
UErrorCode& status);
class PluralFactory : public SelectorFactory {
public:
Selector* createSelector(const Locale& locale, UErrorCode& status) const override;
virtual ~PluralFactory();
private:
friend class IntegerFactory;
friend class MessageFormatter;
PluralFactory() {}
PluralFactory(bool isInt) : isInteger(isInt) {}
static PluralFactory integer() { return PluralFactory(true);}
const bool isInteger = false;
};
class Plural : public Selector {
public:
void selectKey(FormattedPlaceholder&& val,
FunctionOptions&& options,
const UnicodeString* keys,
int32_t keysLen,
UnicodeString* prefs,
int32_t& prefsLen,
UErrorCode& status) const override;
virtual ~Plural();
private:
friend class IntegerFactory;
friend class PluralFactory;
// Can't use UPluralType for this since we want to include
// exact matching as an option
typedef enum PluralType {
PLURAL_ORDINAL,
PLURAL_CARDINAL,
PLURAL_EXACT
} PluralType;
Plural(const Locale& loc, UErrorCode& errorCode);
Plural(const Locale& loc, bool isInt, UErrorCode& errorCode);
static Plural integer(const Locale& loc, UErrorCode& errorCode) { return Plural(loc, true, errorCode); }
PluralType pluralType(const FunctionOptions& opts) const;
const Locale& locale;
const bool isInteger = false;
LocalPointer<StandardFunctions::Number> numberFormatter;
};
class TextFactory : public SelectorFactory {
public:
Selector* createSelector(const Locale& locale, UErrorCode& status) const override;
virtual ~TextFactory();
};
class TextSelector : public Selector {
public:
void selectKey(FormattedPlaceholder&& val,
FunctionOptions&& options,
const UnicodeString* keys,
int32_t keysLen,
UnicodeString* prefs,
int32_t& prefsLen,
UErrorCode& status) const override;
virtual ~TextSelector();
private:
friend class TextFactory;
// Formatting `value` to a string might require the locale
const Locale& locale;
TextSelector(const Locale& l) : locale(l) {}
};
// See https://github.com/unicode-org/message-format-wg/blob/main/test/README.md
class TestFormatFactory : public FormatterFactory {
public:
Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
TestFormatFactory() {}
virtual ~TestFormatFactory();
};
class TestSelect;
class TestFormat : public Formatter {
public:
FormattedPlaceholder format(FormattedPlaceholder&& toFormat, FunctionOptions&& options, UErrorCode& status) const override;
virtual ~TestFormat();
private:
friend class TestFormatFactory;
friend class TestSelect;
TestFormat() {}
static void testFunctionParameters(const FormattedPlaceholder& arg,
const FunctionOptions& options,
int32_t& decimalPlaces,
bool& failsFormat,
bool& failsSelect,
double& input,
UErrorCode& status);
};
// See https://github.com/unicode-org/message-format-wg/blob/main/test/README.md
class TestSelectFactory : public SelectorFactory {
public:
Selector* createSelector(const Locale& locale, UErrorCode& status) const override;
TestSelectFactory() {}
virtual ~TestSelectFactory();
};
class TestSelect : public Selector {
public:
void selectKey(FormattedPlaceholder&& val,
FunctionOptions&& options,
const UnicodeString* keys,
int32_t keysLen,
UnicodeString* prefs,
int32_t& prefsLen,
UErrorCode& status) const override;
virtual ~TestSelect();
private:
friend class TestSelectFactory;
TestSelect() {}
};
};
extern void formatDateWithDefaults(const Locale& locale, const DateInfo& date, UnicodeString&, UErrorCode& errorCode);
extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, double toFormat, UErrorCode& errorCode);
extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int32_t toFormat, UErrorCode& errorCode);
extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int64_t toFormat, UErrorCode& errorCode);
extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, StringPiece toFormat, UErrorCode& errorCode);
extern DateFormat* defaultDateTimeInstance(const Locale&, UErrorCode&);
} // namespace message2
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_MF2 */
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* #if !UCONFIG_NO_NORMALIZATION */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // MESSAGEFORMAT2_FUNCTION_REGISTRY_INTERNAL_H
#endif // U_HIDE_DEPRECATED_API
// eof
|