| 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 "unicode/messageformat2_formattable.h" |
| 13 | #include "unicode/smpdtfmt.h" |
| 14 | #include "messageformat2_macros.h" |
| 15 | |
| 16 | #include "limits.h" |
| 17 | |
| 18 | U_NAMESPACE_BEGIN |
| 19 | |
| 20 | namespace message2 { |
| 21 | |
| 22 | // Fallback values are enclosed in curly braces; |
| 23 | // see https://github.com/unicode-org/message-format-wg/blob/main/spec/formatting.md#formatting-fallback-values |
| 24 | |
| 25 | static UnicodeString fallbackToString(const UnicodeString& s) { |
| 26 | UnicodeString result; |
| 27 | result += LEFT_CURLY_BRACE; |
| 28 | result += s; |
| 29 | result += RIGHT_CURLY_BRACE; |
| 30 | return result; |
| 31 | } |
| 32 | |
| 33 | Formattable& Formattable::operator=(Formattable other) noexcept { |
| 34 | swap(*this, other); |
| 35 | return *this; |
| 36 | } |
| 37 | |
| 38 | Formattable::Formattable(const Formattable& other) { |
| 39 | contents = other.contents; |
| 40 | holdsDate = other.holdsDate; |
| 41 | } |
| 42 | |
| 43 | Formattable Formattable::forDecimal(std::string_view number, UErrorCode &status) { |
| 44 | Formattable f; |
| 45 | // The relevant overload of the StringPiece constructor |
| 46 | // casts the string length to int32_t, so we have to check |
| 47 | // that the length makes sense |
| 48 | if (number.size() > INT_MAX) { |
| 49 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 50 | } else { |
| 51 | f.contents = icu::Formattable(StringPiece(number), status); |
| 52 | } |
| 53 | return f; |
| 54 | } |
| 55 | |
| 56 | UFormattableType Formattable::getType() const { |
| 57 | if (std::holds_alternative<double>(contents)) { |
| 58 | return holdsDate ? UFMT_DATE : UFMT_DOUBLE; |
| 59 | } |
| 60 | if (std::holds_alternative<int64_t>(contents)) { |
| 61 | return UFMT_INT64; |
| 62 | } |
| 63 | if (std::holds_alternative<UnicodeString>(contents)) { |
| 64 | return UFMT_STRING; |
| 65 | } |
| 66 | if (isDecimal()) { |
| 67 | switch (std::get_if<icu::Formattable>(&contents)->getType()) { |
| 68 | case icu::Formattable::Type::kLong: { |
| 69 | return UFMT_LONG; |
| 70 | } |
| 71 | case icu::Formattable::Type::kDouble: { |
| 72 | return UFMT_DOUBLE; |
| 73 | } |
| 74 | default: { |
| 75 | return UFMT_INT64; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | if (std::holds_alternative<const FormattableObject*>(contents)) { |
| 80 | return UFMT_OBJECT; |
| 81 | } |
| 82 | return UFMT_ARRAY; |
| 83 | } |
| 84 | |
| 85 | const Formattable* Formattable::getArray(int32_t& len, UErrorCode& status) const { |
| 86 | NULL_ON_ERROR(status); |
| 87 | |
| 88 | if (getType() != UFMT_ARRAY) { |
| 89 | len = 0; |
| 90 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 91 | return nullptr; |
| 92 | } |
| 93 | const std::pair<const Formattable*, int32_t>& p = *std::get_if<std::pair<const Formattable*, int32_t>>(&contents); |
| 94 | U_ASSERT(p.first != nullptr); |
| 95 | len = p.second; |
| 96 | return p.first; |
| 97 | } |
| 98 | |
| 99 | int64_t Formattable::getInt64(UErrorCode& status) const { |
| 100 | if (isDecimal() && isNumeric()) { |
| 101 | return std::get_if<icu::Formattable>(&contents)->getInt64(status); |
| 102 | } |
| 103 | |
| 104 | switch (getType()) { |
| 105 | case UFMT_LONG: |
| 106 | case UFMT_INT64: { |
| 107 | return *std::get_if<int64_t>(&contents); |
| 108 | } |
| 109 | case UFMT_DOUBLE: { |
| 110 | return icu::Formattable(*std::get_if<double>(&contents)).getInt64(status); |
| 111 | } |
| 112 | default: { |
| 113 | status = U_INVALID_FORMAT_ERROR; |
| 114 | return 0; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | icu::Formattable Formattable::asICUFormattable(UErrorCode& status) const { |
| 120 | if (U_FAILURE(status)) { |
| 121 | return {}; |
| 122 | } |
| 123 | // Type must not be UFMT_ARRAY or UFMT_OBJECT |
| 124 | if (getType() == UFMT_ARRAY || getType() == UFMT_OBJECT) { |
| 125 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 126 | return {}; |
| 127 | } |
| 128 | |
| 129 | if (isDecimal()) { |
| 130 | return *std::get_if<icu::Formattable>(&contents); |
| 131 | } |
| 132 | |
| 133 | switch (getType()) { |
| 134 | case UFMT_DATE: { |
| 135 | return icu::Formattable(*std::get_if<double>(&contents), icu::Formattable::kIsDate); |
| 136 | } |
| 137 | case UFMT_DOUBLE: { |
| 138 | return icu::Formattable(*std::get_if<double>(&contents)); |
| 139 | } |
| 140 | case UFMT_LONG: { |
| 141 | return icu::Formattable(static_cast<int32_t>(*std::get_if<double>(&contents))); |
| 142 | } |
| 143 | case UFMT_INT64: { |
| 144 | return icu::Formattable(*std::get_if<int64_t>(&contents)); |
| 145 | } |
| 146 | case UFMT_STRING: { |
| 147 | return icu::Formattable(*std::get_if<UnicodeString>(&contents)); |
| 148 | } |
| 149 | default: { |
| 150 | // Already checked for UFMT_ARRAY and UFMT_OBJECT |
| 151 | return icu::Formattable(); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | Formattable::~Formattable() {} |
| 157 | |
| 158 | FormattableObject::~FormattableObject() {} |
| 159 | |
| 160 | FormattedMessage::~FormattedMessage() {} |
| 161 | |
| 162 | FormattedValue::FormattedValue(const UnicodeString& s) { |
| 163 | type = kString; |
| 164 | stringOutput = std::move(s); |
| 165 | } |
| 166 | |
| 167 | FormattedValue::FormattedValue(number::FormattedNumber&& n) { |
| 168 | type = kNumber; |
| 169 | numberOutput = std::move(n); |
| 170 | } |
| 171 | |
| 172 | FormattedValue& FormattedValue::operator=(FormattedValue&& other) noexcept { |
| 173 | type = other.type; |
| 174 | if (type == kString) { |
| 175 | stringOutput = std::move(other.stringOutput); |
| 176 | } else { |
| 177 | numberOutput = std::move(other.numberOutput); |
| 178 | } |
| 179 | return *this; |
| 180 | } |
| 181 | |
| 182 | FormattedValue::~FormattedValue() {} |
| 183 | |
| 184 | FormattedPlaceholder& FormattedPlaceholder::operator=(FormattedPlaceholder&& other) noexcept { |
| 185 | type = other.type; |
| 186 | source = other.source; |
| 187 | if (type == kEvaluated) { |
| 188 | formatted = std::move(other.formatted); |
| 189 | previousOptions = std::move(other.previousOptions); |
| 190 | } |
| 191 | fallback = other.fallback; |
| 192 | return *this; |
| 193 | } |
| 194 | |
| 195 | const Formattable& FormattedPlaceholder::asFormattable() const { |
| 196 | return source; |
| 197 | } |
| 198 | |
| 199 | // Default formatters |
| 200 | // ------------------ |
| 201 | |
| 202 | number::FormattedNumber formatNumberWithDefaults(const Locale& locale, double toFormat, UErrorCode& errorCode) { |
| 203 | return number::NumberFormatter::withLocale(locale).formatDouble(toFormat, errorCode); |
| 204 | } |
| 205 | |
| 206 | number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int32_t toFormat, UErrorCode& errorCode) { |
| 207 | return number::NumberFormatter::withLocale(locale).formatInt(toFormat, errorCode); |
| 208 | } |
| 209 | |
| 210 | number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int64_t toFormat, UErrorCode& errorCode) { |
| 211 | return number::NumberFormatter::withLocale(locale).formatInt(toFormat, errorCode); |
| 212 | } |
| 213 | |
| 214 | number::FormattedNumber formatNumberWithDefaults(const Locale& locale, StringPiece toFormat, UErrorCode& errorCode) { |
| 215 | return number::NumberFormatter::withLocale(locale).formatDecimal(toFormat, errorCode); |
| 216 | } |
| 217 | |
| 218 | DateFormat* defaultDateTimeInstance(const Locale& locale, UErrorCode& errorCode) { |
| 219 | NULL_ON_ERROR(errorCode); |
| 220 | LocalPointer<DateFormat> df(DateFormat::createDateTimeInstance(DateFormat::SHORT, DateFormat::SHORT, locale)); |
| 221 | if (!df.isValid()) { |
| 222 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 223 | return nullptr; |
| 224 | } |
| 225 | return df.orphan(); |
| 226 | } |
| 227 | |
| 228 | void formatDateWithDefaults(const Locale& locale, UDate date, UnicodeString& result, UErrorCode& errorCode) { |
| 229 | CHECK_ERROR(errorCode); |
| 230 | |
| 231 | LocalPointer<DateFormat> df(defaultDateTimeInstance(locale, errorCode)); |
| 232 | CHECK_ERROR(errorCode); |
| 233 | df->format(date, result, 0, errorCode); |
| 234 | } |
| 235 | |
| 236 | // Called when output is required and the contents are an unevaluated `Formattable`; |
| 237 | // formats the source `Formattable` to a string with defaults, if it can be |
| 238 | // formatted with a default formatter |
| 239 | static FormattedPlaceholder formatWithDefaults(const Locale& locale, const FormattedPlaceholder& input, UErrorCode& status) { |
| 240 | if (U_FAILURE(status)) { |
| 241 | return {}; |
| 242 | } |
| 243 | |
| 244 | const Formattable& toFormat = input.asFormattable(); |
| 245 | // Try as decimal number first |
| 246 | if (toFormat.isNumeric()) { |
| 247 | // Note: the ICU Formattable has to be created here since the StringPiece |
| 248 | // refers to state inside the Formattable; so otherwise we'll have a reference |
| 249 | // to a temporary object |
| 250 | icu::Formattable icuFormattable = toFormat.asICUFormattable(status); |
| 251 | StringPiece asDecimal = icuFormattable.getDecimalNumber(status); |
| 252 | if (U_FAILURE(status)) { |
| 253 | return {}; |
| 254 | } |
| 255 | if (asDecimal != nullptr) { |
| 256 | return FormattedPlaceholder(input, FormattedValue(formatNumberWithDefaults(locale, asDecimal, status))); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | UFormattableType type = toFormat.getType(); |
| 261 | switch (type) { |
| 262 | case UFMT_DATE: { |
| 263 | UnicodeString result; |
| 264 | UDate d = toFormat.getDate(status); |
| 265 | U_ASSERT(U_SUCCESS(status)); |
| 266 | formatDateWithDefaults(locale, d, result, status); |
| 267 | return FormattedPlaceholder(input, FormattedValue(std::move(result))); |
| 268 | } |
| 269 | case UFMT_DOUBLE: { |
| 270 | double d = toFormat.getDouble(status); |
| 271 | U_ASSERT(U_SUCCESS(status)); |
| 272 | return FormattedPlaceholder(input, FormattedValue(formatNumberWithDefaults(locale, d, status))); |
| 273 | } |
| 274 | case UFMT_LONG: { |
| 275 | int32_t l = toFormat.getLong(status); |
| 276 | U_ASSERT(U_SUCCESS(status)); |
| 277 | return FormattedPlaceholder(input, FormattedValue(formatNumberWithDefaults(locale, l, status))); |
| 278 | } |
| 279 | case UFMT_INT64: { |
| 280 | int64_t i = toFormat.getInt64Value(status); |
| 281 | U_ASSERT(U_SUCCESS(status)); |
| 282 | return FormattedPlaceholder(input, FormattedValue(formatNumberWithDefaults(locale, i, status))); |
| 283 | } |
| 284 | case UFMT_STRING: { |
| 285 | const UnicodeString& s = toFormat.getString(status); |
| 286 | U_ASSERT(U_SUCCESS(status)); |
| 287 | return FormattedPlaceholder(input, FormattedValue(UnicodeString(s))); |
| 288 | } |
| 289 | default: { |
| 290 | // No default formatters for other types; use fallback |
| 291 | status = U_MF_FORMATTING_ERROR; |
| 292 | // Note: it would be better to set an internal formatting error so that a string |
| 293 | // (e.g. the type tag) can be provided. However, this method is called by the |
| 294 | // public method formatToString() and thus can't take a MessageContext |
| 295 | return FormattedPlaceholder(input.getFallback()); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Called when string output is required; forces output to be produced |
| 301 | // if none is present (including formatting number output as a string) |
| 302 | UnicodeString FormattedPlaceholder::formatToString(const Locale& locale, |
| 303 | UErrorCode& status) const { |
| 304 | if (U_FAILURE(status)) { |
| 305 | return {}; |
| 306 | } |
| 307 | if (isFallback() || isNullOperand()) { |
| 308 | return fallbackToString(fallback); |
| 309 | } |
| 310 | |
| 311 | // Evaluated value: either just return the string, or format the number |
| 312 | // as a string and return it |
| 313 | if (isEvaluated()) { |
| 314 | if (formatted.isString()) { |
| 315 | return formatted.getString(); |
| 316 | } else { |
| 317 | return formatted.getNumber().toString(status); |
| 318 | } |
| 319 | } |
| 320 | // Unevaluated value: first evaluate it fully, then format |
| 321 | UErrorCode savedStatus = status; |
| 322 | FormattedPlaceholder evaluated = formatWithDefaults(locale, *this, status); |
| 323 | if (status == U_MF_FORMATTING_ERROR) { |
| 324 | U_ASSERT(evaluated.isFallback()); |
| 325 | return evaluated.getFallback(); |
| 326 | } |
| 327 | // Ignore U_USING_DEFAULT_WARNING |
| 328 | if (status == U_USING_DEFAULT_WARNING) { |
| 329 | status = savedStatus; |
| 330 | } |
| 331 | return evaluated.formatToString(locale, status); |
| 332 | } |
| 333 | |
| 334 | } // namespace message2 |
| 335 | |
| 336 | U_NAMESPACE_END |
| 337 | |
| 338 | #endif /* #if !UCONFIG_NO_MF2 */ |
| 339 | |
| 340 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 341 | |
| 342 | #endif /* #if !UCONFIG_NO_NORMALIZATION */ |