| 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 |
#if !UCONFIG_NO_NORMALIZATION |
| 7 |
|
| 8 |
#if !UCONFIG_NO_FORMATTING |
| 9 |
|
| 10 |
#if !UCONFIG_NO_MF2 |
| 11 |
|
| 12 |
#include <math.h> |
| 13 |
|
| 14 |
#include "unicode/dtptngen.h" |
| 15 |
#include "unicode/messageformat2_data_model_names.h" |
| 16 |
#include "unicode/messageformat2_function_registry.h" |
| 17 |
#include "unicode/smpdtfmt.h" |
| 18 |
#include "charstr.h" |
| 19 |
#include "double-conversion.h" |
| 20 |
#include "messageformat2_allocation.h" |
| 21 |
#include "messageformat2_function_registry_internal.h" |
| 22 |
#include "messageformat2_macros.h" |
| 23 |
#include "hash.h" |
| 24 |
#include "number_types.h" |
| 25 |
#include "uvector.h" // U_ASSERT |
| 26 |
|
| 27 |
// The C99 standard suggested that C++ implementations not define PRId64 etc. constants |
| 28 |
// unless this macro is defined. |
| 29 |
// See the Notes at https://en.cppreference.com/w/cpp/types/integer . |
| 30 |
// Similar to defining __STDC_LIMIT_MACROS in unicode/ptypes.h . |
| 31 |
#ifndef __STDC_FORMAT_MACROS |
| 32 |
# define __STDC_FORMAT_MACROS |
| 33 |
#endif |
| 34 |
#include <inttypes.h> |
| 35 |
#include <math.h> |
| 36 |
|
| 37 |
U_NAMESPACE_BEGIN |
| 38 |
|
| 39 |
namespace message2 { |
| 40 |
|
| 41 |
// Function registry implementation |
| 42 |
|
| 43 |
Formatter::~Formatter() {} |
| 44 |
Selector::~Selector() {} |
| 45 |
FormatterFactory::~FormatterFactory() {} |
| 46 |
SelectorFactory::~SelectorFactory() {} |
| 47 |
|
| 48 |
MFFunctionRegistry MFFunctionRegistry::Builder::build() { |
| 49 |
U_ASSERT(formatters != nullptr && selectors != nullptr && formattersByType != nullptr); |
| 50 |
MFFunctionRegistry result = MFFunctionRegistry(formatters, selectors, formattersByType); |
| 51 |
formatters = nullptr; |
| 52 |
selectors = nullptr; |
| 53 |
formattersByType = nullptr; |
| 54 |
return result; |
| 55 |
} |
| 56 |
|
| 57 |
MFFunctionRegistry::Builder& MFFunctionRegistry::Builder::adoptSelector(const FunctionName& selectorName, SelectorFactory* selectorFactory, UErrorCode& errorCode) { |
| 58 |
if (U_SUCCESS(errorCode)) { |
| 59 |
U_ASSERT(selectors != nullptr); |
| 60 |
selectors->put(selectorName, selectorFactory, errorCode); |
| 61 |
} |
| 62 |
return *this; |
| 63 |
} |
| 64 |
|
| 65 |
MFFunctionRegistry::Builder& MFFunctionRegistry::Builder::adoptFormatter(const FunctionName& formatterName, FormatterFactory* formatterFactory, UErrorCode& errorCode) { |
| 66 |
if (U_SUCCESS(errorCode)) { |
| 67 |
U_ASSERT(formatters != nullptr); |
| 68 |
formatters->put(formatterName, formatterFactory, errorCode); |
| 69 |
} |
| 70 |
return *this; |
| 71 |
} |
| 72 |
|
| 73 |
MFFunctionRegistry::Builder& MFFunctionRegistry::Builder::setDefaultFormatterNameByType(const UnicodeString& type, const FunctionName& functionName, UErrorCode& errorCode) { |
| 74 |
if (U_SUCCESS(errorCode)) { |
| 75 |
U_ASSERT(formattersByType != nullptr); |
| 76 |
FunctionName* f = create<FunctionName>(FunctionName(functionName), errorCode); |
| 77 |
formattersByType->put(type, f, errorCode); |
| 78 |
} |
| 79 |
return *this; |
| 80 |
} |
| 81 |
|
| 82 |
MFFunctionRegistry::Builder::Builder(UErrorCode& errorCode) { |
| 83 |
CHECK_ERROR(errorCode); |
| 84 |
|
| 85 |
formatters = new Hashtable(); |
| 86 |
selectors = new Hashtable(); |
| 87 |
formattersByType = new Hashtable(); |
| 88 |
if (!(formatters != nullptr && selectors != nullptr && formattersByType != nullptr)) { |
| 89 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 90 |
} else { |
| 91 |
formatters->setValueDeleter(uprv_deleteUObject); |
| 92 |
selectors->setValueDeleter(uprv_deleteUObject); |
| 93 |
formattersByType->setValueDeleter(uprv_deleteUObject); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
MFFunctionRegistry::Builder::~Builder() { |
| 98 |
if (formatters != nullptr) { |
| 99 |
delete formatters; |
| 100 |
} |
| 101 |
if (selectors != nullptr) { |
| 102 |
delete selectors; |
| 103 |
} |
| 104 |
if (formattersByType != nullptr) { |
| 105 |
delete formattersByType; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Returns non-owned pointer. Returns pointer rather than reference because it can fail. |
| 110 |
// Returns non-const because FormatterFactory is mutable. |
| 111 |
// TODO: This is unsafe because of the cached-formatters map |
| 112 |
// (the caller could delete the resulting pointer) |
| 113 |
FormatterFactory* MFFunctionRegistry::getFormatter(const FunctionName& formatterName) const { |
| 114 |
U_ASSERT(formatters != nullptr); |
| 115 |
return static_cast<FormatterFactory*>(formatters->get(formatterName)); |
| 116 |
} |
| 117 |
|
| 118 |
UBool MFFunctionRegistry::getDefaultFormatterNameByType(const UnicodeString& type, FunctionName& name) const { |
| 119 |
U_ASSERT(formatters != nullptr); |
| 120 |
const FunctionName* f = static_cast<FunctionName*>(formattersByType->get(type)); |
| 121 |
if (f != nullptr) { |
| 122 |
name = *f; |
| 123 |
return true; |
| 124 |
} |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
const SelectorFactory* MFFunctionRegistry::getSelector(const FunctionName& selectorName) const { |
| 129 |
U_ASSERT(selectors != nullptr); |
| 130 |
return static_cast<const SelectorFactory*>(selectors->get(selectorName)); |
| 131 |
} |
| 132 |
|
| 133 |
bool MFFunctionRegistry::hasFormatter(const FunctionName& f) const { |
| 134 |
return getFormatter(f) != nullptr; |
| 135 |
} |
| 136 |
|
| 137 |
bool MFFunctionRegistry::hasSelector(const FunctionName& s) const { |
| 138 |
return getSelector(s) != nullptr; |
| 139 |
} |
| 140 |
|
| 141 |
void MFFunctionRegistry::checkFormatter(const char* s) const { |
| 142 |
#if U_DEBUG |
| 143 |
U_ASSERT(hasFormatter(FunctionName(UnicodeString(s)))); |
| 144 |
#else |
| 145 |
(void) s; |
| 146 |
#endif |
| 147 |
} |
| 148 |
|
| 149 |
void MFFunctionRegistry::checkSelector(const char* s) const { |
| 150 |
#if U_DEBUG |
| 151 |
U_ASSERT(hasSelector(FunctionName(UnicodeString(s)))); |
| 152 |
#else |
| 153 |
(void) s; |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
// Debugging |
| 158 |
void MFFunctionRegistry::checkStandard() const { |
| 159 |
checkFormatter("datetime"); |
| 160 |
checkFormatter("date"); |
| 161 |
checkFormatter("time"); |
| 162 |
checkFormatter("number"); |
| 163 |
checkFormatter("integer"); |
| 164 |
checkFormatter("test:function"); |
| 165 |
checkFormatter("test:format"); |
| 166 |
checkSelector("number"); |
| 167 |
checkSelector("integer"); |
| 168 |
checkSelector("string"); |
| 169 |
checkSelector("test:function"); |
| 170 |
checkSelector("test:select"); |
| 171 |
} |
| 172 |
|
| 173 |
// Formatter/selector helpers |
| 174 |
|
| 175 |
// Converts `s` to a double, indicating failure via `errorCode` |
| 176 |
static void strToDouble(const UnicodeString& s, double& result, UErrorCode& errorCode) { |
| 177 |
CHECK_ERROR(errorCode); |
| 178 |
|
| 179 |
// Using en-US locale because it happens to correspond to the spec: |
| 180 |
// https://github.com/unicode-org/message-format-wg/blob/main/spec/registry.md#number-operands |
| 181 |
// Ideally, this should re-use the code for parsing number literals (Parser::parseUnquotedLiteral()) |
| 182 |
// It's hard to reuse the same code because of how parse errors work. |
| 183 |
// TODO: Refactor |
| 184 |
LocalPointer<NumberFormat> numberFormat(NumberFormat::createInstance(Locale("en-US"), errorCode)); |
| 185 |
CHECK_ERROR(errorCode); |
| 186 |
icu::Formattable asNumber; |
| 187 |
numberFormat->parse(s, asNumber, errorCode); |
| 188 |
CHECK_ERROR(errorCode); |
| 189 |
result = asNumber.getDouble(errorCode); |
| 190 |
} |
| 191 |
|
| 192 |
static double tryStringAsNumber(const Locale& locale, const Formattable& val, UErrorCode& errorCode) { |
| 193 |
// Check for a string option, try to parse it as a number if present |
| 194 |
UnicodeString tempString = val.getString(errorCode); |
| 195 |
LocalPointer<NumberFormat> numberFormat(NumberFormat::createInstance(locale, errorCode)); |
| 196 |
if (U_SUCCESS(errorCode)) { |
| 197 |
icu::Formattable asNumber; |
| 198 |
numberFormat->parse(tempString, asNumber, errorCode); |
| 199 |
if (U_SUCCESS(errorCode)) { |
| 200 |
return asNumber.getDouble(errorCode); |
| 201 |
} |
| 202 |
} |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static int64_t getInt64Value(const Locale& locale, const Formattable& value, UErrorCode& errorCode) { |
| 207 |
if (U_SUCCESS(errorCode)) { |
| 208 |
if (!value.isNumeric()) { |
| 209 |
double doubleResult = tryStringAsNumber(locale, value, errorCode); |
| 210 |
if (U_SUCCESS(errorCode)) { |
| 211 |
return static_cast<int64_t>(doubleResult); |
| 212 |
} |
| 213 |
} |
| 214 |
else { |
| 215 |
int64_t result = value.getInt64(errorCode); |
| 216 |
if (U_SUCCESS(errorCode)) { |
| 217 |
return result; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
// Option was numeric but couldn't be converted to int64_t -- could be overflow |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
// Adopts its arguments |
| 226 |
MFFunctionRegistry::MFFunctionRegistry(FormatterMap* f, SelectorMap* s, Hashtable* byType) : formatters(f), selectors(s), formattersByType(byType) { |
| 227 |
U_ASSERT(f != nullptr && s != nullptr && byType != nullptr); |
| 228 |
} |
| 229 |
|
| 230 |
MFFunctionRegistry& MFFunctionRegistry::operator=(MFFunctionRegistry&& other) noexcept { |
| 231 |
cleanup(); |
| 232 |
|
| 233 |
formatters = other.formatters; |
| 234 |
selectors = other.selectors; |
| 235 |
formattersByType = other.formattersByType; |
| 236 |
other.formatters = nullptr; |
| 237 |
other.selectors = nullptr; |
| 238 |
other.formattersByType = nullptr; |
| 239 |
|
| 240 |
return *this; |
| 241 |
} |
| 242 |
|
| 243 |
void MFFunctionRegistry::cleanup() noexcept { |
| 244 |
if (formatters != nullptr) { |
| 245 |
delete formatters; |
| 246 |
} |
| 247 |
if (selectors != nullptr) { |
| 248 |
delete selectors; |
| 249 |
} |
| 250 |
if (formattersByType != nullptr) { |
| 251 |
delete formattersByType; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
MFFunctionRegistry::~MFFunctionRegistry() { |
| 257 |
cleanup(); |
| 258 |
} |
| 259 |
|
| 260 |
// Specific formatter implementations |
| 261 |
|
| 262 |
// --------- Number |
| 263 |
|
| 264 |
/* static */ number::LocalizedNumberFormatter StandardFunctions::formatterForOptions(const Number& number, |
| 265 |
const FunctionOptions& opts, |
| 266 |
UErrorCode& status) { |
| 267 |
number::UnlocalizedNumberFormatter nf; |
| 268 |
|
| 269 |
using namespace number; |
| 270 |
|
| 271 |
if (U_SUCCESS(status)) { |
| 272 |
Formattable opt; |
| 273 |
nf = NumberFormatter::with(); |
| 274 |
bool isInteger = number.isInteger; |
| 275 |
|
| 276 |
if (isInteger) { |
| 277 |
nf = nf.precision(Precision::integer()); |
| 278 |
} |
| 279 |
|
| 280 |
// Notation options |
| 281 |
if (!isInteger) { |
| 282 |
// These options only apply to `:number` |
| 283 |
|
| 284 |
// Default notation is simple |
| 285 |
Notation notation = Notation::simple(); |
| 286 |
UnicodeString notationOpt = opts.getStringFunctionOption(UnicodeString("notation")); |
| 287 |
if (notationOpt == UnicodeString("scientific")) { |
| 288 |
notation = Notation::scientific(); |
| 289 |
} else if (notationOpt == UnicodeString("engineering")) { |
| 290 |
notation = Notation::engineering(); |
| 291 |
} else if (notationOpt == UnicodeString("compact")) { |
| 292 |
UnicodeString displayOpt = opts.getStringFunctionOption(UnicodeString("compactDisplay")); |
| 293 |
if (displayOpt == UnicodeString("long")) { |
| 294 |
notation = Notation::compactLong(); |
| 295 |
} else { |
| 296 |
// Default is short |
| 297 |
notation = Notation::compactShort(); |
| 298 |
} |
| 299 |
} else { |
| 300 |
// Already set to default |
| 301 |
} |
| 302 |
nf = nf.notation(notation); |
| 303 |
} |
| 304 |
|
| 305 |
// Style options -- specific to `:number` |
| 306 |
if (!isInteger) { |
| 307 |
if (number.usePercent(opts)) { |
| 308 |
nf = nf.unit(NoUnit::percent()).scale(Scale::powerOfTen(2)); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
int32_t maxSignificantDigits = number.maximumSignificantDigits(opts); |
| 313 |
if (!isInteger) { |
| 314 |
int32_t minFractionDigits = number.minimumFractionDigits(opts); |
| 315 |
int32_t maxFractionDigits = number.maximumFractionDigits(opts); |
| 316 |
int32_t minSignificantDigits = number.minimumSignificantDigits(opts); |
| 317 |
Precision p = Precision::unlimited(); |
| 318 |
bool precisionOptions = false; |
| 319 |
|
| 320 |
// Returning -1 means the option wasn't provided |
| 321 |
if (maxFractionDigits != -1 && minFractionDigits != -1) { |
| 322 |
precisionOptions = true; |
| 323 |
p = Precision::minMaxFraction(minFractionDigits, maxFractionDigits); |
| 324 |
} else if (minFractionDigits != -1) { |
| 325 |
precisionOptions = true; |
| 326 |
p = Precision::minFraction(minFractionDigits); |
| 327 |
} else if (maxFractionDigits != -1) { |
| 328 |
precisionOptions = true; |
| 329 |
p = Precision::maxFraction(maxFractionDigits); |
| 330 |
} |
| 331 |
|
| 332 |
if (minSignificantDigits != -1) { |
| 333 |
precisionOptions = true; |
| 334 |
p = p.minSignificantDigits(minSignificantDigits); |
| 335 |
} |
| 336 |
if (maxSignificantDigits != -1) { |
| 337 |
precisionOptions = true; |
| 338 |
p = p.maxSignificantDigits(maxSignificantDigits); |
| 339 |
} |
| 340 |
if (precisionOptions) { |
| 341 |
nf = nf.precision(p); |
| 342 |
} |
| 343 |
} else { |
| 344 |
// maxSignificantDigits applies to `:integer`, but the other precision options don't |
| 345 |
Precision p = Precision::integer(); |
| 346 |
if (maxSignificantDigits != -1) { |
| 347 |
p = p.maxSignificantDigits(maxSignificantDigits); |
| 348 |
} |
| 349 |
nf = nf.precision(p); |
| 350 |
} |
| 351 |
|
| 352 |
// All other options apply to both `:number` and `:integer` |
| 353 |
int32_t minIntegerDigits = number.minimumIntegerDigits(opts); |
| 354 |
nf = nf.integerWidth(IntegerWidth::zeroFillTo(minIntegerDigits)); |
| 355 |
|
| 356 |
// signDisplay |
| 357 |
UnicodeString sd = opts.getStringFunctionOption(UnicodeString("signDisplay")); |
| 358 |
UNumberSignDisplay signDisplay; |
| 359 |
if (sd == UnicodeString("always")) { |
| 360 |
signDisplay = UNumberSignDisplay::UNUM_SIGN_ALWAYS; |
| 361 |
} else if (sd == UnicodeString("exceptZero")) { |
| 362 |
signDisplay = UNumberSignDisplay::UNUM_SIGN_EXCEPT_ZERO; |
| 363 |
} else if (sd == UnicodeString("negative")) { |
| 364 |
signDisplay = UNumberSignDisplay::UNUM_SIGN_NEGATIVE; |
| 365 |
} else if (sd == UnicodeString("never")) { |
| 366 |
signDisplay = UNumberSignDisplay::UNUM_SIGN_NEVER; |
| 367 |
} else { |
| 368 |
signDisplay = UNumberSignDisplay::UNUM_SIGN_AUTO; |
| 369 |
} |
| 370 |
nf = nf.sign(signDisplay); |
| 371 |
|
| 372 |
// useGrouping |
| 373 |
UnicodeString ug = opts.getStringFunctionOption(UnicodeString("useGrouping")); |
| 374 |
UNumberGroupingStrategy grp; |
| 375 |
if (ug == UnicodeString("always")) { |
| 376 |
grp = UNumberGroupingStrategy::UNUM_GROUPING_ON_ALIGNED; |
| 377 |
} else if (ug == UnicodeString("never")) { |
| 378 |
grp = UNumberGroupingStrategy::UNUM_GROUPING_OFF; |
| 379 |
} else if (ug == UnicodeString("min2")) { |
| 380 |
grp = UNumberGroupingStrategy::UNUM_GROUPING_MIN2; |
| 381 |
} else { |
| 382 |
// Default is "auto" |
| 383 |
grp = UNumberGroupingStrategy::UNUM_GROUPING_AUTO; |
| 384 |
} |
| 385 |
nf = nf.grouping(grp); |
| 386 |
|
| 387 |
// numberingSystem |
| 388 |
UnicodeString ns = opts.getStringFunctionOption(UnicodeString("numberingSystem")); |
| 389 |
if (ns.length() > 0) { |
| 390 |
ns = ns.toLower(Locale("en-US")); |
| 391 |
CharString buffer; |
| 392 |
// Ignore bad option values, so use a local status |
| 393 |
UErrorCode localStatus = U_ZERO_ERROR; |
| 394 |
// Copied from number_skeletons.cpp (helpers::parseNumberingSystemOption) |
| 395 |
buffer.appendInvariantChars({false, ns.getBuffer(), ns.length()}, localStatus); |
| 396 |
if (U_SUCCESS(localStatus)) { |
| 397 |
LocalPointer<NumberingSystem> symbols |
| 398 |
(NumberingSystem::createInstanceByName(buffer.data(), localStatus)); |
| 399 |
if (U_SUCCESS(localStatus)) { |
| 400 |
nf = nf.adoptSymbols(symbols.orphan()); |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
return nf.locale(number.locale); |
| 406 |
} |
| 407 |
|
| 408 |
Formatter* StandardFunctions::NumberFactory::createFormatter(const Locale& locale, UErrorCode& errorCode) { |
| 409 |
NULL_ON_ERROR(errorCode); |
| 410 |
|
| 411 |
Formatter* result = new Number(locale); |
| 412 |
if (result == nullptr) { |
| 413 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 414 |
} |
| 415 |
return result; |
| 416 |
} |
| 417 |
|
| 418 |
Formatter* StandardFunctions::IntegerFactory::createFormatter(const Locale& locale, UErrorCode& errorCode) { |
| 419 |
NULL_ON_ERROR(errorCode); |
| 420 |
|
| 421 |
Formatter* result = new Number(Number::integer(locale)); |
| 422 |
if (result == nullptr) { |
| 423 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 424 |
} |
| 425 |
return result; |
| 426 |
} |
| 427 |
|
| 428 |
StandardFunctions::IntegerFactory::~IntegerFactory() {} |
| 429 |
|
| 430 |
static FormattedPlaceholder notANumber(const FormattedPlaceholder& input) { |
| 431 |
return FormattedPlaceholder(input, FormattedValue(UnicodeString("NaN"))); |
| 432 |
} |
| 433 |
|
| 434 |
static double parseNumberLiteral(const Formattable& input, UErrorCode& errorCode) { |
| 435 |
if (U_FAILURE(errorCode)) { |
| 436 |
return {}; |
| 437 |
} |
| 438 |
|
| 439 |
// Copying string to avoid GCC dangling-reference warning |
| 440 |
// (although the reference is safe) |
| 441 |
UnicodeString inputStr = input.getString(errorCode); |
| 442 |
// Precondition: `input`'s source Formattable has type string |
| 443 |
if (U_FAILURE(errorCode)) { |
| 444 |
return {}; |
| 445 |
} |
| 446 |
|
| 447 |
// Hack: Check for cases that are forbidden by the MF2 grammar |
| 448 |
// but allowed by StringToDouble |
| 449 |
int32_t len = inputStr.length(); |
| 450 |
|
| 451 |
if (len > 0 && ((inputStr[0] == '+') |
| 452 |
|| (inputStr[0] == '0' && len > 1 && inputStr[1] != '.') |
| 453 |
|| (inputStr[len - 1] == '.') |
| 454 |
|| (inputStr[0] == '.'))) { |
| 455 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
// Otherwise, convert to double using double_conversion::StringToDoubleConverter |
| 460 |
using namespace double_conversion; |
| 461 |
int processedCharactersCount = 0; |
| 462 |
StringToDoubleConverter converter(0, 0, 0, "", ""); |
| 463 |
double result = |
| 464 |
converter.StringToDouble(reinterpret_cast<const uint16_t*>(inputStr.getBuffer()), |
| 465 |
len, |
| 466 |
&processedCharactersCount); |
| 467 |
if (processedCharactersCount != len) { |
| 468 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 469 |
} |
| 470 |
return result; |
| 471 |
} |
| 472 |
|
| 473 |
static UChar32 digitToChar(int32_t val, UErrorCode errorCode) { |
| 474 |
if (U_FAILURE(errorCode)) { |
| 475 |
return '0'; |
| 476 |
} |
| 477 |
if (val < 0 || val > 9) { |
| 478 |
errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 479 |
} |
| 480 |
switch(val) { |
| 481 |
case 0: |
| 482 |
return '0'; |
| 483 |
case 1: |
| 484 |
return '1'; |
| 485 |
case 2: |
| 486 |
return '2'; |
| 487 |
case 3: |
| 488 |
return '3'; |
| 489 |
case 4: |
| 490 |
return '4'; |
| 491 |
case 5: |
| 492 |
return '5'; |
| 493 |
case 6: |
| 494 |
return '6'; |
| 495 |
case 7: |
| 496 |
return '7'; |
| 497 |
case 8: |
| 498 |
return '8'; |
| 499 |
case 9: |
| 500 |
return '9'; |
| 501 |
default: |
| 502 |
errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 503 |
return '0'; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
static FormattedPlaceholder tryParsingNumberLiteral(const number::LocalizedNumberFormatter& nf, const FormattedPlaceholder& input, UErrorCode& errorCode) { |
| 508 |
double numberValue = parseNumberLiteral(input.asFormattable(), errorCode); |
| 509 |
if (U_FAILURE(errorCode)) { |
| 510 |
return notANumber(input); |
| 511 |
} |
| 512 |
|
| 513 |
UErrorCode savedStatus = errorCode; |
| 514 |
number::FormattedNumber result = nf.formatDouble(numberValue, errorCode); |
| 515 |
// Ignore U_USING_DEFAULT_WARNING |
| 516 |
if (errorCode == U_USING_DEFAULT_WARNING) { |
| 517 |
errorCode = savedStatus; |
| 518 |
} |
| 519 |
return FormattedPlaceholder(input, FormattedValue(std::move(result))); |
| 520 |
} |
| 521 |
|
| 522 |
int32_t StandardFunctions::Number::maximumFractionDigits(const FunctionOptions& opts) const { |
| 523 |
Formattable opt; |
| 524 |
|
| 525 |
if (isInteger) { |
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
if (opts.getFunctionOption(UnicodeString("maximumFractionDigits"), opt)) { |
| 530 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 531 |
int64_t val = getInt64Value(locale, opt, localErrorCode); |
| 532 |
if (U_SUCCESS(localErrorCode)) { |
| 533 |
return static_cast<int32_t>(val); |
| 534 |
} |
| 535 |
} |
| 536 |
// Returning -1 indicates that the option wasn't provided or was a non-integer. |
| 537 |
// The caller needs to check for that case, since passing -1 to Precision::maxFraction() |
| 538 |
// is an error. |
| 539 |
return -1; |
| 540 |
} |
| 541 |
|
| 542 |
int32_t StandardFunctions::Number::minimumFractionDigits(const FunctionOptions& opts) const { |
| 543 |
Formattable opt; |
| 544 |
|
| 545 |
if (!isInteger) { |
| 546 |
if (opts.getFunctionOption(UnicodeString("minimumFractionDigits"), opt)) { |
| 547 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 548 |
int64_t val = getInt64Value(locale, opt, localErrorCode); |
| 549 |
if (U_SUCCESS(localErrorCode)) { |
| 550 |
return static_cast<int32_t>(val); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
// Returning -1 indicates that the option wasn't provided or was a non-integer. |
| 555 |
// The caller needs to check for that case, since passing -1 to Precision::minFraction() |
| 556 |
// is an error. |
| 557 |
return -1; |
| 558 |
} |
| 559 |
|
| 560 |
int32_t StandardFunctions::Number::minimumIntegerDigits(const FunctionOptions& opts) const { |
| 561 |
Formattable opt; |
| 562 |
|
| 563 |
if (opts.getFunctionOption(UnicodeString("minimumIntegerDigits"), opt)) { |
| 564 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 565 |
int64_t val = getInt64Value(locale, opt, localErrorCode); |
| 566 |
if (U_SUCCESS(localErrorCode)) { |
| 567 |
return static_cast<int32_t>(val); |
| 568 |
} |
| 569 |
} |
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
int32_t StandardFunctions::Number::minimumSignificantDigits(const FunctionOptions& opts) const { |
| 574 |
Formattable opt; |
| 575 |
|
| 576 |
if (!isInteger) { |
| 577 |
if (opts.getFunctionOption(UnicodeString("minimumSignificantDigits"), opt)) { |
| 578 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 579 |
int64_t val = getInt64Value(locale, opt, localErrorCode); |
| 580 |
if (U_SUCCESS(localErrorCode)) { |
| 581 |
return static_cast<int32_t>(val); |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
// Returning -1 indicates that the option wasn't provided or was a non-integer. |
| 586 |
// The caller needs to check for that case, since passing -1 to Precision::minSignificantDigits() |
| 587 |
// is an error. |
| 588 |
return -1; |
| 589 |
} |
| 590 |
|
| 591 |
int32_t StandardFunctions::Number::maximumSignificantDigits(const FunctionOptions& opts) const { |
| 592 |
Formattable opt; |
| 593 |
|
| 594 |
if (opts.getFunctionOption(UnicodeString("maximumSignificantDigits"), opt)) { |
| 595 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 596 |
int64_t val = getInt64Value(locale, opt, localErrorCode); |
| 597 |
if (U_SUCCESS(localErrorCode)) { |
| 598 |
return static_cast<int32_t>(val); |
| 599 |
} |
| 600 |
} |
| 601 |
// Returning -1 indicates that the option wasn't provided or was a non-integer. |
| 602 |
// The caller needs to check for that case, since passing -1 to Precision::maxSignificantDigits() |
| 603 |
// is an error. |
| 604 |
return -1; // Not a valid value for Precision; has to be checked |
| 605 |
} |
| 606 |
|
| 607 |
bool StandardFunctions::Number::usePercent(const FunctionOptions& opts) const { |
| 608 |
Formattable opt; |
| 609 |
if (isInteger |
| 610 |
|| !opts.getFunctionOption(UnicodeString("style"), opt) |
| 611 |
|| opt.getType() != UFMT_STRING) { |
| 612 |
return false; |
| 613 |
} |
| 614 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 615 |
const UnicodeString& style = opt.getString(localErrorCode); |
| 616 |
U_ASSERT(U_SUCCESS(localErrorCode)); |
| 617 |
return (style == UnicodeString("percent")); |
| 618 |
} |
| 619 |
|
| 620 |
/* static */ StandardFunctions::Number StandardFunctions::Number::integer(const Locale& loc) { |
| 621 |
return StandardFunctions::Number(loc, true); |
| 622 |
} |
| 623 |
|
| 624 |
FormattedPlaceholder StandardFunctions::Number::format(FormattedPlaceholder&& arg, FunctionOptions&& opts, UErrorCode& errorCode) const { |
| 625 |
if (U_FAILURE(errorCode)) { |
| 626 |
return {}; |
| 627 |
} |
| 628 |
|
| 629 |
// No argument => return "NaN" |
| 630 |
if (!arg.canFormat()) { |
| 631 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 632 |
return notANumber(arg); |
| 633 |
} |
| 634 |
|
| 635 |
number::LocalizedNumberFormatter realFormatter; |
| 636 |
realFormatter = formatterForOptions(*this, opts, errorCode); |
| 637 |
|
| 638 |
number::FormattedNumber numberResult; |
| 639 |
if (U_SUCCESS(errorCode)) { |
| 640 |
// Already checked that contents can be formatted |
| 641 |
const Formattable& toFormat = arg.asFormattable(); |
| 642 |
switch (toFormat.getType()) { |
| 643 |
case UFMT_DOUBLE: { |
| 644 |
double d = toFormat.getDouble(errorCode); |
| 645 |
U_ASSERT(U_SUCCESS(errorCode)); |
| 646 |
numberResult = realFormatter.formatDouble(d, errorCode); |
| 647 |
break; |
| 648 |
} |
| 649 |
case UFMT_LONG: { |
| 650 |
int32_t l = toFormat.getLong(errorCode); |
| 651 |
U_ASSERT(U_SUCCESS(errorCode)); |
| 652 |
numberResult = realFormatter.formatInt(l, errorCode); |
| 653 |
break; |
| 654 |
} |
| 655 |
case UFMT_INT64: { |
| 656 |
int64_t i = toFormat.getInt64(errorCode); |
| 657 |
U_ASSERT(U_SUCCESS(errorCode)); |
| 658 |
numberResult = realFormatter.formatInt(i, errorCode); |
| 659 |
break; |
| 660 |
} |
| 661 |
case UFMT_STRING: { |
| 662 |
// Try to parse the string as a number |
| 663 |
return tryParsingNumberLiteral(realFormatter, arg, errorCode); |
| 664 |
} |
| 665 |
default: { |
| 666 |
// Other types can't be parsed as a number |
| 667 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 668 |
return notANumber(arg); |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
return FormattedPlaceholder(arg, FormattedValue(std::move(numberResult))); |
| 674 |
} |
| 675 |
|
| 676 |
StandardFunctions::Number::~Number() {} |
| 677 |
StandardFunctions::NumberFactory::~NumberFactory() {} |
| 678 |
|
| 679 |
// --------- PluralFactory |
| 680 |
|
| 681 |
|
| 682 |
StandardFunctions::Plural::PluralType StandardFunctions::Plural::pluralType(const FunctionOptions& opts) const { |
| 683 |
Formattable opt; |
| 684 |
|
| 685 |
if (opts.getFunctionOption(UnicodeString("select"), opt)) { |
| 686 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 687 |
UnicodeString val = opt.getString(localErrorCode); |
| 688 |
if (U_SUCCESS(localErrorCode)) { |
| 689 |
if (val == UnicodeString("ordinal")) { |
| 690 |
return PluralType::PLURAL_ORDINAL; |
| 691 |
} |
| 692 |
if (val == UnicodeString("exact")) { |
| 693 |
return PluralType::PLURAL_EXACT; |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
return PluralType::PLURAL_CARDINAL; |
| 698 |
} |
| 699 |
|
| 700 |
Selector* StandardFunctions::PluralFactory::createSelector(const Locale& locale, UErrorCode& errorCode) const { |
| 701 |
NULL_ON_ERROR(errorCode); |
| 702 |
|
| 703 |
Selector* result; |
| 704 |
if (isInteger) { |
| 705 |
result = new Plural(Plural::integer(locale, errorCode)); |
| 706 |
} else { |
| 707 |
result = new Plural(locale, errorCode); |
| 708 |
} |
| 709 |
NULL_ON_ERROR(errorCode); |
| 710 |
if (result == nullptr) { |
| 711 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 712 |
} |
| 713 |
return result; |
| 714 |
} |
| 715 |
|
| 716 |
void StandardFunctions::Plural::selectKey(FormattedPlaceholder&& toFormat, |
| 717 |
FunctionOptions&& opts, |
| 718 |
const UnicodeString* keys, |
| 719 |
int32_t keysLen, |
| 720 |
UnicodeString* prefs, |
| 721 |
int32_t& prefsLen, |
| 722 |
UErrorCode& errorCode) const { |
| 723 |
CHECK_ERROR(errorCode); |
| 724 |
|
| 725 |
// No argument => return "NaN" |
| 726 |
if (!toFormat.canFormat()) { |
| 727 |
errorCode = U_MF_SELECTOR_ERROR; |
| 728 |
return; |
| 729 |
} |
| 730 |
|
| 731 |
// Handle any formatting options |
| 732 |
PluralType type = pluralType(opts); |
| 733 |
FormattedPlaceholder resolvedSelector = numberFormatter->format(std::move(toFormat), |
| 734 |
std::move(opts), |
| 735 |
errorCode); |
| 736 |
CHECK_ERROR(errorCode); |
| 737 |
|
| 738 |
U_ASSERT(resolvedSelector.isEvaluated() && resolvedSelector.output().isNumber()); |
| 739 |
|
| 740 |
// See https://github.com/unicode-org/message-format-wg/blob/main/spec/registry.md#number-selection |
| 741 |
// 1. Let exact be the JSON string representation of the numeric value of resolvedSelector |
| 742 |
const number::FormattedNumber& formattedNumber = resolvedSelector.output().getNumber(); |
| 743 |
UnicodeString exact = formattedNumber.toString(errorCode); |
| 744 |
|
| 745 |
if (U_FAILURE(errorCode)) { |
| 746 |
// Non-number => selector error |
| 747 |
errorCode = U_MF_SELECTOR_ERROR; |
| 748 |
return; |
| 749 |
} |
| 750 |
|
| 751 |
// Step 2. Let keyword be a string which is the result of rule selection on resolvedSelector. |
| 752 |
// If the option select is set to exact, rule-based selection is not used. Return the empty string. |
| 753 |
UnicodeString keyword; |
| 754 |
if (type != PluralType::PLURAL_EXACT) { |
| 755 |
UPluralType t = type == PluralType::PLURAL_ORDINAL ? UPLURAL_TYPE_ORDINAL : UPLURAL_TYPE_CARDINAL; |
| 756 |
// Look up plural rules by locale and type |
| 757 |
LocalPointer<PluralRules> rules(PluralRules::forLocale(locale, t, errorCode)); |
| 758 |
CHECK_ERROR(errorCode); |
| 759 |
|
| 760 |
keyword = rules->select(formattedNumber, errorCode); |
| 761 |
} |
| 762 |
|
| 763 |
// Steps 3-4 elided: |
| 764 |
// 3. Let resultExact be a new empty list of strings. |
| 765 |
// 4. Let resultKeyword be a new empty list of strings. |
| 766 |
// Instead, we use `prefs` the concatenation of `resultExact` |
| 767 |
// and `resultKeyword`. |
| 768 |
|
| 769 |
prefsLen = 0; |
| 770 |
|
| 771 |
// 5. For each string key in keys: |
| 772 |
double keyAsDouble = 0; |
| 773 |
for (int32_t i = 0; i < keysLen; i++) { |
| 774 |
// Try parsing the key as a double |
| 775 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 776 |
strToDouble(keys[i], keyAsDouble, localErrorCode); |
| 777 |
// 5i. If the value of key matches the production number-literal, then |
| 778 |
if (U_SUCCESS(localErrorCode)) { |
| 779 |
// 5i(a). If key and exact consist of the same sequence of Unicode code points, then |
| 780 |
if (exact == keys[i]) { |
| 781 |
// 5i(a)(a) Append key as the last element of the list resultExact. |
| 782 |
prefs[prefsLen] = keys[i]; |
| 783 |
prefsLen++; |
| 784 |
break; |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
// Return immediately if exact matching was requested |
| 790 |
if (prefsLen == keysLen || type == PluralType::PLURAL_EXACT) { |
| 791 |
return; |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
for (int32_t i = 0; i < keysLen; i ++) { |
| 796 |
if (prefsLen >= keysLen) { |
| 797 |
break; |
| 798 |
} |
| 799 |
// 5ii. Else if key is one of the keywords zero, one, two, few, many, or other, then |
| 800 |
// 5ii(a). If key and keyword consist of the same sequence of Unicode code points, then |
| 801 |
if (keyword == keys[i]) { |
| 802 |
// 5ii(a)(a) Append key as the last element of the list resultKeyword. |
| 803 |
prefs[prefsLen] = keys[i]; |
| 804 |
prefsLen++; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
// Note: Step 5(iii) "Else, emit a Selection Error" is omitted in both loops |
| 809 |
|
| 810 |
// 6. Return a new list whose elements are the concatenation of the elements |
| 811 |
// (in order) of resultExact followed by the elements (in order) of resultKeyword. |
| 812 |
// (Implicit, since `prefs` is an out-parameter) |
| 813 |
} |
| 814 |
|
| 815 |
StandardFunctions::Plural::Plural(const Locale& loc, UErrorCode& status) : locale(loc) { |
| 816 |
CHECK_ERROR(status); |
| 817 |
|
| 818 |
numberFormatter.adoptInstead(new StandardFunctions::Number(loc)); |
| 819 |
if (!numberFormatter.isValid()) { |
| 820 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
StandardFunctions::Plural::Plural(const Locale& loc, bool isInt, UErrorCode& status) : locale(loc), isInteger(isInt) { |
| 825 |
CHECK_ERROR(status); |
| 826 |
|
| 827 |
if (isInteger) { |
| 828 |
numberFormatter.adoptInstead(new StandardFunctions::Number(loc, true)); |
| 829 |
} else { |
| 830 |
numberFormatter.adoptInstead(new StandardFunctions::Number(loc)); |
| 831 |
} |
| 832 |
|
| 833 |
if (!numberFormatter.isValid()) { |
| 834 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
StandardFunctions::Plural::~Plural() {} |
| 839 |
|
| 840 |
StandardFunctions::PluralFactory::~PluralFactory() {} |
| 841 |
|
| 842 |
// --------- DateTimeFactory |
| 843 |
|
| 844 |
/* static */ UnicodeString StandardFunctions::getStringOption(const FunctionOptions& opts, |
| 845 |
const UnicodeString& optionName, |
| 846 |
UErrorCode& errorCode) { |
| 847 |
if (U_SUCCESS(errorCode)) { |
| 848 |
Formattable opt; |
| 849 |
if (opts.getFunctionOption(optionName, opt)) { |
| 850 |
return opt.getString(errorCode); // In case it's not a string, error code will be set |
| 851 |
} else { |
| 852 |
errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 853 |
} |
| 854 |
} |
| 855 |
// Default is empty string |
| 856 |
return {}; |
| 857 |
} |
| 858 |
|
| 859 |
// Date/time options only |
| 860 |
static UnicodeString defaultForOption(const UnicodeString& optionName) { |
| 861 |
if (optionName == UnicodeString("dateStyle") |
| 862 |
|| optionName == UnicodeString("timeStyle") |
| 863 |
|| optionName == UnicodeString("style")) { |
| 864 |
return UnicodeString("short"); |
| 865 |
} |
| 866 |
return {}; // Empty string is default |
| 867 |
} |
| 868 |
|
| 869 |
// TODO |
| 870 |
// Only DateTime currently uses the function options stored in the placeholder. |
| 871 |
// It also doesn't use them very consistently (it looks at the previous set of options, |
| 872 |
// and others aren't preserved). This needs to be generalized, |
| 873 |
// but that depends on https://github.com/unicode-org/message-format-wg/issues/515 |
| 874 |
// Finally, the option value is assumed to be a string, |
| 875 |
// which works for datetime options but not necessarily in general. |
| 876 |
UnicodeString StandardFunctions::DateTime::getFunctionOption(const FormattedPlaceholder& toFormat, |
| 877 |
const FunctionOptions& opts, |
| 878 |
const UnicodeString& optionName) const { |
| 879 |
// Options passed to the current function invocation take priority |
| 880 |
Formattable opt; |
| 881 |
UnicodeString s; |
| 882 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 883 |
s = getStringOption(opts, optionName, localErrorCode); |
| 884 |
if (U_SUCCESS(localErrorCode)) { |
| 885 |
return s; |
| 886 |
} |
| 887 |
// Next try the set of options used to construct `toFormat` |
| 888 |
localErrorCode = U_ZERO_ERROR; |
| 889 |
s = getStringOption(toFormat.options(), optionName, localErrorCode); |
| 890 |
if (U_SUCCESS(localErrorCode)) { |
| 891 |
return s; |
| 892 |
} |
| 893 |
// Finally, use default |
| 894 |
return defaultForOption(optionName); |
| 895 |
} |
| 896 |
|
| 897 |
// Used for options that don't have defaults |
| 898 |
UnicodeString StandardFunctions::DateTime::getFunctionOption(const FormattedPlaceholder& toFormat, |
| 899 |
const FunctionOptions& opts, |
| 900 |
const UnicodeString& optionName, |
| 901 |
UErrorCode& errorCode) const { |
| 902 |
if (U_SUCCESS(errorCode)) { |
| 903 |
// Options passed to the current function invocation take priority |
| 904 |
Formattable opt; |
| 905 |
UnicodeString s; |
| 906 |
UErrorCode localErrorCode = U_ZERO_ERROR; |
| 907 |
s = getStringOption(opts, optionName, localErrorCode); |
| 908 |
if (U_SUCCESS(localErrorCode)) { |
| 909 |
return s; |
| 910 |
} |
| 911 |
// Next try the set of options used to construct `toFormat` |
| 912 |
localErrorCode = U_ZERO_ERROR; |
| 913 |
s = getStringOption(toFormat.options(), optionName, localErrorCode); |
| 914 |
if (U_SUCCESS(localErrorCode)) { |
| 915 |
return s; |
| 916 |
} |
| 917 |
errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 918 |
} |
| 919 |
return {}; |
| 920 |
} |
| 921 |
|
| 922 |
static DateFormat::EStyle stringToStyle(UnicodeString option, UErrorCode& errorCode) { |
| 923 |
if (U_SUCCESS(errorCode)) { |
| 924 |
UnicodeString upper = option.toUpper(); |
| 925 |
if (upper == UnicodeString("FULL")) { |
| 926 |
return DateFormat::EStyle::kFull; |
| 927 |
} |
| 928 |
if (upper == UnicodeString("LONG")) { |
| 929 |
return DateFormat::EStyle::kLong; |
| 930 |
} |
| 931 |
if (upper == UnicodeString("MEDIUM")) { |
| 932 |
return DateFormat::EStyle::kMedium; |
| 933 |
} |
| 934 |
if (upper == UnicodeString("SHORT")) { |
| 935 |
return DateFormat::EStyle::kShort; |
| 936 |
} |
| 937 |
if (upper.isEmpty() || upper == UnicodeString("DEFAULT")) { |
| 938 |
return DateFormat::EStyle::kDefault; |
| 939 |
} |
| 940 |
errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 941 |
} |
| 942 |
return DateFormat::EStyle::kNone; |
| 943 |
} |
| 944 |
|
| 945 |
/* static */ StandardFunctions::DateTimeFactory* StandardFunctions::DateTimeFactory::dateTime(UErrorCode& errorCode) { |
| 946 |
NULL_ON_ERROR(errorCode); |
| 947 |
|
| 948 |
DateTimeFactory* result = new StandardFunctions::DateTimeFactory(DateTimeType::DateTime); |
| 949 |
if (result == nullptr) { |
| 950 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 951 |
} |
| 952 |
return result; |
| 953 |
} |
| 954 |
|
| 955 |
/* static */ StandardFunctions::DateTimeFactory* StandardFunctions::DateTimeFactory::date(UErrorCode& errorCode) { |
| 956 |
NULL_ON_ERROR(errorCode); |
| 957 |
|
| 958 |
DateTimeFactory* result = new DateTimeFactory(DateTimeType::Date); |
| 959 |
if (result == nullptr) { |
| 960 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 961 |
} |
| 962 |
return result; |
| 963 |
} |
| 964 |
|
| 965 |
/* static */ StandardFunctions::DateTimeFactory* StandardFunctions::DateTimeFactory::time(UErrorCode& errorCode) { |
| 966 |
NULL_ON_ERROR(errorCode); |
| 967 |
|
| 968 |
DateTimeFactory* result = new DateTimeFactory(DateTimeType::Time); |
| 969 |
if (result == nullptr) { |
| 970 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 971 |
} |
| 972 |
return result; |
| 973 |
} |
| 974 |
|
| 975 |
Formatter* StandardFunctions::DateTimeFactory::createFormatter(const Locale& locale, UErrorCode& errorCode) { |
| 976 |
NULL_ON_ERROR(errorCode); |
| 977 |
|
| 978 |
Formatter* result = new StandardFunctions::DateTime(locale, type); |
| 979 |
if (result == nullptr) { |
| 980 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 981 |
} |
| 982 |
return result; |
| 983 |
} |
| 984 |
|
| 985 |
FormattedPlaceholder StandardFunctions::DateTime::format(FormattedPlaceholder&& toFormat, |
| 986 |
FunctionOptions&& opts, |
| 987 |
UErrorCode& errorCode) const { |
| 988 |
if (U_FAILURE(errorCode)) { |
| 989 |
return {}; |
| 990 |
} |
| 991 |
|
| 992 |
// Argument must be present |
| 993 |
if (!toFormat.canFormat()) { |
| 994 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 995 |
return std::move(toFormat); |
| 996 |
} |
| 997 |
|
| 998 |
LocalPointer<DateFormat> df; |
| 999 |
Formattable opt; |
| 1000 |
|
| 1001 |
DateFormat::EStyle dateStyle = DateFormat::kShort; |
| 1002 |
DateFormat::EStyle timeStyle = DateFormat::kShort; |
| 1003 |
|
| 1004 |
UnicodeString dateStyleName("dateStyle"); |
| 1005 |
UnicodeString timeStyleName("timeStyle"); |
| 1006 |
UnicodeString styleName("style"); |
| 1007 |
|
| 1008 |
bool hasDateStyleOption = opts.getFunctionOption(dateStyleName, opt); |
| 1009 |
bool hasTimeStyleOption = opts.getFunctionOption(timeStyleName, opt); |
| 1010 |
bool noOptions = opts.optionsCount() == 0; |
| 1011 |
|
| 1012 |
bool useStyle = (type == DateTimeFactory::DateTimeType::DateTime |
| 1013 |
&& (hasDateStyleOption || hasTimeStyleOption |
| 1014 |
|| noOptions)) |
| 1015 |
|| (type != DateTimeFactory::DateTimeType::DateTime); |
| 1016 |
|
| 1017 |
bool useDate = type == DateTimeFactory::DateTimeType::Date |
| 1018 |
|| (type == DateTimeFactory::DateTimeType::DateTime |
| 1019 |
&& hasDateStyleOption); |
| 1020 |
bool useTime = type == DateTimeFactory::DateTimeType::Time |
| 1021 |
|| (type == DateTimeFactory::DateTimeType::DateTime |
| 1022 |
&& hasTimeStyleOption); |
| 1023 |
|
| 1024 |
if (useStyle) { |
| 1025 |
// Extract style options |
| 1026 |
if (type == DateTimeFactory::DateTimeType::DateTime) { |
| 1027 |
// Note that the options-getting has to be repeated across the three cases, |
| 1028 |
// since `:datetime` uses "dateStyle"/"timeStyle" and `:date` and `:time` |
| 1029 |
// use "style" |
| 1030 |
dateStyle = stringToStyle(getFunctionOption(toFormat, opts, dateStyleName), errorCode); |
| 1031 |
timeStyle = stringToStyle(getFunctionOption(toFormat, opts, timeStyleName), errorCode); |
| 1032 |
|
| 1033 |
if (useDate && !useTime) { |
| 1034 |
df.adoptInstead(DateFormat::createDateInstance(dateStyle, locale)); |
| 1035 |
} else if (useTime && !useDate) { |
| 1036 |
df.adoptInstead(DateFormat::createTimeInstance(timeStyle, locale)); |
| 1037 |
} else { |
| 1038 |
df.adoptInstead(DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale)); |
| 1039 |
} |
| 1040 |
} else if (type == DateTimeFactory::DateTimeType::Date) { |
| 1041 |
dateStyle = stringToStyle(getFunctionOption(toFormat, opts, styleName), errorCode); |
| 1042 |
df.adoptInstead(DateFormat::createDateInstance(dateStyle, locale)); |
| 1043 |
} else { |
| 1044 |
// :time |
| 1045 |
timeStyle = stringToStyle(getFunctionOption(toFormat, opts, styleName), errorCode); |
| 1046 |
df.adoptInstead(DateFormat::createTimeInstance(timeStyle, locale)); |
| 1047 |
} |
| 1048 |
} else { |
| 1049 |
// Build up a skeleton based on the field options, then use that to |
| 1050 |
// create the date formatter |
| 1051 |
|
| 1052 |
UnicodeString skeleton; |
| 1053 |
#define ADD_PATTERN(s) skeleton += UnicodeString(s) |
| 1054 |
if (U_SUCCESS(errorCode)) { |
| 1055 |
// Year |
| 1056 |
UnicodeString year = getFunctionOption(toFormat, opts, UnicodeString("year"), errorCode); |
| 1057 |
if (U_FAILURE(errorCode)) { |
| 1058 |
errorCode = U_ZERO_ERROR; |
| 1059 |
} else { |
| 1060 |
useDate = true; |
| 1061 |
if (year == UnicodeString("2-digit")) { |
| 1062 |
ADD_PATTERN("YY"); |
| 1063 |
} else if (year == UnicodeString("numeric")) { |
| 1064 |
ADD_PATTERN("YYYY"); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
// Month |
| 1068 |
UnicodeString month = getFunctionOption(toFormat, opts, UnicodeString("month"), errorCode); |
| 1069 |
if (U_FAILURE(errorCode)) { |
| 1070 |
errorCode = U_ZERO_ERROR; |
| 1071 |
} else { |
| 1072 |
useDate = true; |
| 1073 |
/* numeric, 2-digit, long, short, narrow */ |
| 1074 |
if (month == UnicodeString("long")) { |
| 1075 |
ADD_PATTERN("MMMM"); |
| 1076 |
} else if (month == UnicodeString("short")) { |
| 1077 |
ADD_PATTERN("MMM"); |
| 1078 |
} else if (month == UnicodeString("narrow")) { |
| 1079 |
ADD_PATTERN("MMMMM"); |
| 1080 |
} else if (month == UnicodeString("numeric")) { |
| 1081 |
ADD_PATTERN("M"); |
| 1082 |
} else if (month == UnicodeString("2-digit")) { |
| 1083 |
ADD_PATTERN("MM"); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
// Weekday |
| 1087 |
UnicodeString weekday = getFunctionOption(toFormat, opts, UnicodeString("weekday"), errorCode); |
| 1088 |
if (U_FAILURE(errorCode)) { |
| 1089 |
errorCode = U_ZERO_ERROR; |
| 1090 |
} else { |
| 1091 |
useDate = true; |
| 1092 |
if (weekday == UnicodeString("long")) { |
| 1093 |
ADD_PATTERN("EEEE"); |
| 1094 |
} else if (weekday == UnicodeString("short")) { |
| 1095 |
ADD_PATTERN("EEEEE"); |
| 1096 |
} else if (weekday == UnicodeString("narrow")) { |
| 1097 |
ADD_PATTERN("EEEEE"); |
| 1098 |
} |
| 1099 |
} |
| 1100 |
// Day |
| 1101 |
UnicodeString day = getFunctionOption(toFormat, opts, UnicodeString("day"), errorCode); |
| 1102 |
if (U_FAILURE(errorCode)) { |
| 1103 |
errorCode = U_ZERO_ERROR; |
| 1104 |
} else { |
| 1105 |
useDate = true; |
| 1106 |
if (day == UnicodeString("numeric")) { |
| 1107 |
ADD_PATTERN("d"); |
| 1108 |
} else if (day == UnicodeString("2-digit")) { |
| 1109 |
ADD_PATTERN("dd"); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
// Hour |
| 1113 |
UnicodeString hour = getFunctionOption(toFormat, opts, UnicodeString("hour"), errorCode); |
| 1114 |
if (U_FAILURE(errorCode)) { |
| 1115 |
errorCode = U_ZERO_ERROR; |
| 1116 |
} else { |
| 1117 |
useTime = true; |
| 1118 |
if (hour == UnicodeString("numeric")) { |
| 1119 |
ADD_PATTERN("h"); |
| 1120 |
} else if (hour == UnicodeString("2-digit")) { |
| 1121 |
ADD_PATTERN("hh"); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
// Minute |
| 1125 |
UnicodeString minute = getFunctionOption(toFormat, opts, UnicodeString("minute"), errorCode); |
| 1126 |
if (U_FAILURE(errorCode)) { |
| 1127 |
errorCode = U_ZERO_ERROR; |
| 1128 |
} else { |
| 1129 |
useTime = true; |
| 1130 |
if (minute == UnicodeString("numeric")) { |
| 1131 |
ADD_PATTERN("m"); |
| 1132 |
} else if (minute == UnicodeString("2-digit")) { |
| 1133 |
ADD_PATTERN("mm"); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
// Second |
| 1137 |
UnicodeString second = getFunctionOption(toFormat, opts, UnicodeString("second"), errorCode); |
| 1138 |
if (U_FAILURE(errorCode)) { |
| 1139 |
errorCode = U_ZERO_ERROR; |
| 1140 |
} else { |
| 1141 |
useTime = true; |
| 1142 |
if (second == UnicodeString("numeric")) { |
| 1143 |
ADD_PATTERN("s"); |
| 1144 |
} else if (second == UnicodeString("2-digit")) { |
| 1145 |
ADD_PATTERN("ss"); |
| 1146 |
} |
| 1147 |
} |
| 1148 |
} |
| 1149 |
/* |
| 1150 |
TODO |
| 1151 |
fractionalSecondDigits |
| 1152 |
hourCycle |
| 1153 |
timeZoneName |
| 1154 |
era |
| 1155 |
*/ |
| 1156 |
df.adoptInstead(DateFormat::createInstanceForSkeleton(skeleton, errorCode)); |
| 1157 |
} |
| 1158 |
|
| 1159 |
if (U_FAILURE(errorCode)) { |
| 1160 |
return {}; |
| 1161 |
} |
| 1162 |
if (!df.isValid()) { |
| 1163 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1164 |
return {}; |
| 1165 |
} |
| 1166 |
|
| 1167 |
UnicodeString result; |
| 1168 |
const Formattable& source = toFormat.asFormattable(); |
| 1169 |
switch (source.getType()) { |
| 1170 |
case UFMT_STRING: { |
| 1171 |
const UnicodeString& sourceStr = source.getString(errorCode); |
| 1172 |
U_ASSERT(U_SUCCESS(errorCode)); |
| 1173 |
// Pattern for ISO 8601 format - datetime |
| 1174 |
UnicodeString pattern("YYYY-MM-dd'T'HH:mm:ss"); |
| 1175 |
LocalPointer<DateFormat> dateParser(new SimpleDateFormat(pattern, errorCode)); |
| 1176 |
if (U_FAILURE(errorCode)) { |
| 1177 |
errorCode = U_MF_FORMATTING_ERROR; |
| 1178 |
} else { |
| 1179 |
// Parse the date |
| 1180 |
UDate d = dateParser->parse(sourceStr, errorCode); |
| 1181 |
if (U_FAILURE(errorCode)) { |
| 1182 |
// Pattern for ISO 8601 format - date |
| 1183 |
UnicodeString pattern("YYYY-MM-dd"); |
| 1184 |
errorCode = U_ZERO_ERROR; |
| 1185 |
dateParser.adoptInstead(new SimpleDateFormat(pattern, errorCode)); |
| 1186 |
if (U_FAILURE(errorCode)) { |
| 1187 |
errorCode = U_MF_FORMATTING_ERROR; |
| 1188 |
} else { |
| 1189 |
d = dateParser->parse(sourceStr, errorCode); |
| 1190 |
if (U_FAILURE(errorCode)) { |
| 1191 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
} |
| 1195 |
// Use the parsed date as the source value |
| 1196 |
// in the returned FormattedPlaceholder; this is necessary |
| 1197 |
// so the date can be re-formatted |
| 1198 |
toFormat = FormattedPlaceholder(message2::Formattable::forDate(d), |
| 1199 |
toFormat.getFallback()); |
| 1200 |
df->format(d, result, 0, errorCode); |
| 1201 |
} |
| 1202 |
break; |
| 1203 |
} |
| 1204 |
case UFMT_DATE: { |
| 1205 |
df->format(source.asICUFormattable(errorCode), result, 0, errorCode); |
| 1206 |
if (U_FAILURE(errorCode)) { |
| 1207 |
if (errorCode == U_ILLEGAL_ARGUMENT_ERROR) { |
| 1208 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
break; |
| 1212 |
} |
| 1213 |
// Any other cases are an error |
| 1214 |
default: { |
| 1215 |
errorCode = U_MF_OPERAND_MISMATCH_ERROR; |
| 1216 |
break; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
if (U_FAILURE(errorCode)) { |
| 1220 |
return {}; |
| 1221 |
} |
| 1222 |
return FormattedPlaceholder(toFormat, std::move(opts), FormattedValue(std::move(result))); |
| 1223 |
} |
| 1224 |
|
| 1225 |
StandardFunctions::DateTimeFactory::~DateTimeFactory() {} |
| 1226 |
StandardFunctions::DateTime::~DateTime() {} |
| 1227 |
|
| 1228 |
// --------- TextFactory |
| 1229 |
|
| 1230 |
Selector* StandardFunctions::TextFactory::createSelector(const Locale& locale, UErrorCode& errorCode) const { |
| 1231 |
Selector* result = new TextSelector(locale); |
| 1232 |
if (result == nullptr) { |
| 1233 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1234 |
return nullptr; |
| 1235 |
} |
| 1236 |
return result; |
| 1237 |
} |
| 1238 |
|
| 1239 |
void StandardFunctions::TextSelector::selectKey(FormattedPlaceholder&& toFormat, |
| 1240 |
FunctionOptions&& opts, |
| 1241 |
const UnicodeString* keys, |
| 1242 |
int32_t keysLen, |
| 1243 |
UnicodeString* prefs, |
| 1244 |
int32_t& prefsLen, |
| 1245 |
UErrorCode& errorCode) const { |
| 1246 |
// No options |
| 1247 |
(void) opts; |
| 1248 |
|
| 1249 |
CHECK_ERROR(errorCode); |
| 1250 |
|
| 1251 |
// Just compares the key and value as strings |
| 1252 |
|
| 1253 |
// Argument must be present |
| 1254 |
if (!toFormat.canFormat()) { |
| 1255 |
errorCode = U_MF_SELECTOR_ERROR; |
| 1256 |
return; |
| 1257 |
} |
| 1258 |
|
| 1259 |
prefsLen = 0; |
| 1260 |
|
| 1261 |
// Convert to string |
| 1262 |
const UnicodeString& formattedValue = toFormat.formatToString(locale, errorCode); |
| 1263 |
if (U_FAILURE(errorCode)) { |
| 1264 |
return; |
| 1265 |
} |
| 1266 |
|
| 1267 |
for (int32_t i = 0; i < keysLen; i++) { |
| 1268 |
if (keys[i] == formattedValue) { |
| 1269 |
prefs[0] = keys[i]; |
| 1270 |
prefsLen = 1; |
| 1271 |
break; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
StandardFunctions::TextFactory::~TextFactory() {} |
| 1277 |
StandardFunctions::TextSelector::~TextSelector() {} |
| 1278 |
|
| 1279 |
// ------------ TestFormatFactory |
| 1280 |
|
| 1281 |
Formatter* StandardFunctions::TestFormatFactory::createFormatter(const Locale& locale, UErrorCode& errorCode) { |
| 1282 |
NULL_ON_ERROR(errorCode); |
| 1283 |
|
| 1284 |
// Results are not locale-dependent |
| 1285 |
(void) locale; |
| 1286 |
|
| 1287 |
Formatter* result = new TestFormat(); |
| 1288 |
if (result == nullptr) { |
| 1289 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1290 |
} |
| 1291 |
return result; |
| 1292 |
} |
| 1293 |
|
| 1294 |
StandardFunctions::TestFormatFactory::~TestFormatFactory() {} |
| 1295 |
StandardFunctions::TestFormat::~TestFormat() {} |
| 1296 |
|
| 1297 |
// Extract numeric value from a Formattable or, if it's a string, |
| 1298 |
// parse it as a number according to the MF2 `number-literal` grammar production |
| 1299 |
double formattableToNumber(const Formattable& arg, UErrorCode& status) { |
| 1300 |
if (U_FAILURE(status)) { |
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
|
| 1304 |
double result = 0; |
| 1305 |
|
| 1306 |
switch (arg.getType()) { |
| 1307 |
case UFMT_DOUBLE: { |
| 1308 |
result = arg.getDouble(status); |
| 1309 |
U_ASSERT(U_SUCCESS(status)); |
| 1310 |
break; |
| 1311 |
} |
| 1312 |
case UFMT_LONG: { |
| 1313 |
result = (double) arg.getLong(status); |
| 1314 |
U_ASSERT(U_SUCCESS(status)); |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
case UFMT_INT64: { |
| 1318 |
result = (double) arg.getInt64(status); |
| 1319 |
U_ASSERT(U_SUCCESS(status)); |
| 1320 |
break; |
| 1321 |
} |
| 1322 |
case UFMT_STRING: { |
| 1323 |
// Try to parse the string as a number |
| 1324 |
result = parseNumberLiteral(arg, status); |
| 1325 |
if (U_FAILURE(status)) { |
| 1326 |
status = U_MF_OPERAND_MISMATCH_ERROR; |
| 1327 |
} |
| 1328 |
break; |
| 1329 |
} |
| 1330 |
default: { |
| 1331 |
// Other types can't be parsed as a number |
| 1332 |
status = U_MF_OPERAND_MISMATCH_ERROR; |
| 1333 |
break; |
| 1334 |
} |
| 1335 |
} |
| 1336 |
return result; |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
/* static */ void StandardFunctions::TestFormat::testFunctionParameters(const FormattedPlaceholder& arg, |
| 1341 |
const FunctionOptions& options, |
| 1342 |
int32_t& decimalPlaces, |
| 1343 |
bool& failsFormat, |
| 1344 |
bool& failsSelect, |
| 1345 |
double& input, |
| 1346 |
UErrorCode& status) { |
| 1347 |
CHECK_ERROR(status); |
| 1348 |
|
| 1349 |
// 1. Let DecimalPlaces be 0. |
| 1350 |
decimalPlaces = 0; |
| 1351 |
|
| 1352 |
// 2. Let FailsFormat be false. |
| 1353 |
failsFormat = false; |
| 1354 |
|
| 1355 |
// 3. Let FailsSelect be false. |
| 1356 |
failsSelect = false; |
| 1357 |
|
| 1358 |
// 4. Let arg be the resolved value of the expression operand. |
| 1359 |
// (already true) |
| 1360 |
|
| 1361 |
// Step 5 omitted because composition isn't fully implemented yet |
| 1362 |
// 6. Else if arg is a numerical value or a string matching the number-literal production, then |
| 1363 |
input = formattableToNumber(arg.asFormattable(), status); |
| 1364 |
if (U_FAILURE(status)) { |
| 1365 |
// 7. Else, |
| 1366 |
// 7i. Emit "bad-input" Resolution Error. |
| 1367 |
status = U_MF_OPERAND_MISMATCH_ERROR; |
| 1368 |
// 7ii. Use a fallback value as the resolved value of the expression. |
| 1369 |
// Further steps of this algorithm are not followed. |
| 1370 |
} |
| 1371 |
// 8. If the decimalPlaces option is set, then |
| 1372 |
Formattable opt; |
| 1373 |
if (options.getFunctionOption(UnicodeString("decimalPlaces"), opt)) { |
| 1374 |
// 8i. If its value resolves to a numerical integer value 0 or 1 |
| 1375 |
// or their corresponding string representations '0' or '1', then |
| 1376 |
double decimalPlacesInput = formattableToNumber(opt, status); |
| 1377 |
if (U_SUCCESS(status)) { |
| 1378 |
if (decimalPlacesInput == 0 || decimalPlacesInput == 1) { |
| 1379 |
// 8ia. Set DecimalPlaces to be the numerical value of the option. |
| 1380 |
decimalPlaces = decimalPlacesInput; |
| 1381 |
} |
| 1382 |
} |
| 1383 |
// 8ii. Else if its value is not an unresolved value set by option resolution, |
| 1384 |
else { |
| 1385 |
// 8iia. Emit "bad-option" Resolution Error. |
| 1386 |
status = U_MF_BAD_OPTION; |
| 1387 |
// 8iib. Use a fallback value as the resolved value of the expression. |
| 1388 |
} |
| 1389 |
} |
| 1390 |
// 9. If the fails option is set, then |
| 1391 |
Formattable failsOpt; |
| 1392 |
if (options.getFunctionOption(UnicodeString("fails"), failsOpt)) { |
| 1393 |
UnicodeString failsString = failsOpt.getString(status); |
| 1394 |
if (U_SUCCESS(status)) { |
| 1395 |
// 9i. If its value resolves to the string 'always', then |
| 1396 |
if (failsString == u"always") { |
| 1397 |
// 9ia. Set FailsFormat to be true |
| 1398 |
failsFormat = true; |
| 1399 |
// 9ib. Set FailsSelect to be true. |
| 1400 |
failsSelect = true; |
| 1401 |
} |
| 1402 |
// 9ii. Else if its value resolves to the string "format", then |
| 1403 |
else if (failsString == u"format") { |
| 1404 |
// 9ia. Set FailsFormat to be true |
| 1405 |
failsFormat = true; |
| 1406 |
} |
| 1407 |
// 9iii. Else if its value resolves to the string "select", then |
| 1408 |
else if (failsString == u"select") { |
| 1409 |
// 9iiia. Set FailsSelect to be true. |
| 1410 |
failsSelect = true; |
| 1411 |
} |
| 1412 |
// 9iv. Else if its value does not resolve to the string "never", then |
| 1413 |
else if (failsString != u"never") { |
| 1414 |
// 9iv(a). Emit "bad-option" Resolution Error. |
| 1415 |
status = U_MF_BAD_OPTION; |
| 1416 |
} |
| 1417 |
} else { |
| 1418 |
// 9iv. again |
| 1419 |
status = U_MF_BAD_OPTION; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
FormattedPlaceholder StandardFunctions::TestFormat::format(FormattedPlaceholder&& arg, |
| 1425 |
FunctionOptions&& options, |
| 1426 |
UErrorCode& status) const{ |
| 1427 |
|
| 1428 |
int32_t decimalPlaces; |
| 1429 |
bool failsFormat; |
| 1430 |
bool failsSelect; |
| 1431 |
double input; |
| 1432 |
|
| 1433 |
testFunctionParameters(arg, options, decimalPlaces, |
| 1434 |
failsFormat, failsSelect, input, status); |
| 1435 |
if (U_FAILURE(status)) { |
| 1436 |
return FormattedPlaceholder(arg.getFallback()); |
| 1437 |
} |
| 1438 |
|
| 1439 |
// If FailsFormat is true, attempting to format the placeholder to any |
| 1440 |
// formatting target will fail. |
| 1441 |
if (failsFormat) { |
| 1442 |
status = U_MF_FORMATTING_ERROR; |
| 1443 |
return FormattedPlaceholder(arg.getFallback()); |
| 1444 |
} |
| 1445 |
UnicodeString result; |
| 1446 |
// When :test:function is used as a formatter, a placeholder resolving to a value |
| 1447 |
// with a :test:function expression is formatted as a concatenation of the following parts: |
| 1448 |
// 1. If Input is less than 0, the character - U+002D Hyphen-Minus. |
| 1449 |
if (input < 0) { |
| 1450 |
result += HYPHEN; |
| 1451 |
} |
| 1452 |
// 2. The truncated absolute integer value of Input, i.e. floor(abs(Input)), formatted as a |
| 1453 |
// sequence of decimal digit characters (U+0030...U+0039). |
| 1454 |
char buffer[256]; |
| 1455 |
bool ignore; |
| 1456 |
int ignoreLen; |
| 1457 |
int ignorePoint; |
| 1458 |
double_conversion::DoubleToStringConverter::DoubleToAscii(floor(abs(input)), |
| 1459 |
double_conversion::DoubleToStringConverter::DtoaMode::SHORTEST, |
| 1460 |
0, |
| 1461 |
buffer, |
| 1462 |
256, |
| 1463 |
&ignore, |
| 1464 |
&ignoreLen, |
| 1465 |
&ignorePoint); |
| 1466 |
result += UnicodeString(buffer); |
| 1467 |
// 3. If DecimalPlaces is 1, then |
| 1468 |
if (decimalPlaces == 1) { |
| 1469 |
// 3i. The character . U+002E Full Stop. |
| 1470 |
result += u"."; |
| 1471 |
// 3ii. The single decimal digit character representing the value |
| 1472 |
// floor((abs(Input) - floor(abs(Input))) * 10) |
| 1473 |
int32_t val = floor((abs(input) - floor(abs(input)) * 10)); |
| 1474 |
result += digitToChar(val, status); |
| 1475 |
U_ASSERT(U_SUCCESS(status)); |
| 1476 |
} |
| 1477 |
return FormattedPlaceholder(result); |
| 1478 |
} |
| 1479 |
|
| 1480 |
// ------------ TestSelectFactory |
| 1481 |
|
| 1482 |
StandardFunctions::TestSelectFactory::~TestSelectFactory() {} |
| 1483 |
StandardFunctions::TestSelect::~TestSelect() {} |
| 1484 |
|
| 1485 |
Selector* StandardFunctions::TestSelectFactory::createSelector(const Locale& locale, |
| 1486 |
UErrorCode& errorCode) const { |
| 1487 |
NULL_ON_ERROR(errorCode); |
| 1488 |
|
| 1489 |
// Results are not locale-dependent |
| 1490 |
(void) locale; |
| 1491 |
|
| 1492 |
Selector* result = new TestSelect(); |
| 1493 |
if (result == nullptr) { |
| 1494 |
errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1495 |
} |
| 1496 |
return result; |
| 1497 |
} |
| 1498 |
|
| 1499 |
void StandardFunctions::TestSelect::selectKey(FormattedPlaceholder&& val, |
| 1500 |
FunctionOptions&& options, |
| 1501 |
const UnicodeString* keys, |
| 1502 |
int32_t keysLen, |
| 1503 |
UnicodeString* prefs, |
| 1504 |
int32_t& prefsLen, |
| 1505 |
UErrorCode& status) const { |
| 1506 |
int32_t decimalPlaces; |
| 1507 |
bool failsFormat; |
| 1508 |
bool failsSelect; |
| 1509 |
double input; |
| 1510 |
|
| 1511 |
TestFormat::testFunctionParameters(val, options, decimalPlaces, |
| 1512 |
failsFormat, failsSelect, input, status); |
| 1513 |
|
| 1514 |
if (U_FAILURE(status)) { |
| 1515 |
return; |
| 1516 |
} |
| 1517 |
|
| 1518 |
if (failsSelect) { |
| 1519 |
status = U_MF_SELECTOR_ERROR; |
| 1520 |
return; |
| 1521 |
} |
| 1522 |
|
| 1523 |
// If the Input is 1 and DecimalPlaces is 1, the method will return some slice |
| 1524 |
// of the list « '1.0', '1' », depending on whether those values are included in keys. |
| 1525 |
bool include1point0 = false; |
| 1526 |
bool include1 = false; |
| 1527 |
if (input == 1 && decimalPlaces == 1) { |
| 1528 |
include1point0 = true; |
| 1529 |
include1 = true; |
| 1530 |
} else if (input == 1 && decimalPlaces == 0) { |
| 1531 |
include1 = true; |
| 1532 |
} |
| 1533 |
|
| 1534 |
// If the Input is 1 and DecimalPlaces is 0, the method will return the list « '1' » if |
| 1535 |
// keys includes '1', or an empty list otherwise. |
| 1536 |
// If the Input is any other value, the method will return an empty list. |
| 1537 |
for (int32_t i = 0; i < keysLen; i++) { |
| 1538 |
if ((keys[i] == u"1" && include1) |
| 1539 |
|| (keys[i] == u"1.0" && include1point0)) { |
| 1540 |
prefs[prefsLen] = keys[i]; |
| 1541 |
prefsLen++; |
| 1542 |
} |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
} // namespace message2 |
| 1547 |
U_NAMESPACE_END |
| 1548 |
|
| 1549 |
#endif /* #if !UCONFIG_NO_MF2 */ |
| 1550 |
|
| 1551 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |
| 1552 |
|
| 1553 |
#endif /* #if !UCONFIG_NO_NORMALIZATION */ |