From 64d142a5d67ff078457e0b8521601dac13d58f75 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Wed, 12 Jun 2024 15:49:42 -0700 Subject: [PATCH] Remove unneeded error code argument --- .../i18n/messageformat2_formattable.cpp | 38 +++++++++---------- .../i18n/messageformat2_function_registry.cpp | 22 ++++++----- .../i18n/unicode/messageformat2_formattable.h | 9 ++--- .../intltest/messageformat2test_custom.cpp | 8 ++-- 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/icu4c/source/i18n/messageformat2_formattable.cpp b/icu4c/source/i18n/messageformat2_formattable.cpp index aa44f2b21cab..825a17d6a65a 100644 --- a/icu4c/source/i18n/messageformat2_formattable.cpp +++ b/icu4c/source/i18n/messageformat2_formattable.cpp @@ -163,6 +163,9 @@ namespace message2 { } FormattableWithOptions::FormattableWithOptions(const Formattable& f, FunctionOptions&& o, UErrorCode& status) { + if (U_FAILURE(status)) { + return; + } value = f; options.adoptInstead(create(std::move(o), status)); if (U_FAILURE(status)) { @@ -224,6 +227,13 @@ namespace message2 { return source; } + FormattableWithOptions::FormattableWithOptions() { + options.adoptInstead(new FunctionOptions()); + if (!options.isValid()) { + bogus = true; + } + } + FormattableWithOptions::FormattableWithOptions(const FormattableWithOptions& other) { U_ASSERT(!other.bogus); U_ASSERT(other.options.isValid()); @@ -242,12 +252,7 @@ namespace message2 { } } - FormattedPlaceholder FormattedPlaceholder::withOutput(FormattedValue&& output, - UErrorCode& status) const { - if (U_FAILURE(status)) { - return {}; - } - + FormattedPlaceholder FormattedPlaceholder::withOutput(FormattedValue&& output) const { return FormattedPlaceholder(source, fallback, std::move(output)); @@ -300,9 +305,7 @@ namespace message2 { const UnicodeString& fb, Type t) { fallback = fb; - if (t != kNull && t != kFallback) { - source = input; - } + source = input; type = t; } @@ -367,8 +370,7 @@ namespace message2 { if (asDecimal != nullptr) { return input.withOutput(FormattedValue(formatNumberWithDefaults(locale, asDecimal, - status)), - status); + status))); } } @@ -379,31 +381,27 @@ namespace message2 { UDate d = toFormat.getDate(status); U_ASSERT(U_SUCCESS(status)); formatDateWithDefaults(locale, d, result, status); - return input.withOutput(FormattedValue(std::move(result)), status); + return input.withOutput(FormattedValue(std::move(result))); } case UFMT_DOUBLE: { double d = toFormat.getDouble(status); U_ASSERT(U_SUCCESS(status)); - return input.withOutput(FormattedValue(formatNumberWithDefaults(locale, d, status)), - status); + return input.withOutput(FormattedValue(formatNumberWithDefaults(locale, d, status))); } case UFMT_LONG: { int32_t l = toFormat.getLong(status); U_ASSERT(U_SUCCESS(status)); - return input.withOutput(FormattedValue(formatNumberWithDefaults(locale, l, status)), - status); + return input.withOutput(FormattedValue(formatNumberWithDefaults(locale, l, status))); } case UFMT_INT64: { int64_t i = toFormat.getInt64Value(status); U_ASSERT(U_SUCCESS(status)); - return input.withOutput(FormattedValue(formatNumberWithDefaults(locale, i, status)), - status); + return input.withOutput(FormattedValue(formatNumberWithDefaults(locale, i, status))); } case UFMT_STRING: { const UnicodeString& s = toFormat.getString(status); U_ASSERT(U_SUCCESS(status)); - return input.withOutput(FormattedValue(UnicodeString(s)), - status); + return input.withOutput(FormattedValue(UnicodeString(s))); } default: { // No default formatters for other types; use fallback diff --git a/icu4c/source/i18n/messageformat2_function_registry.cpp b/icu4c/source/i18n/messageformat2_function_registry.cpp index e2e50e6e2b85..e684a0756ecf 100644 --- a/icu4c/source/i18n/messageformat2_function_registry.cpp +++ b/icu4c/source/i18n/messageformat2_function_registry.cpp @@ -377,8 +377,8 @@ Formatter* StandardFunctions::IntegerFactory::createFormatter(const Locale& loca StandardFunctions::IntegerFactory::~IntegerFactory() {} -static FormattedPlaceholder notANumber(const FormattedPlaceholder& input, UErrorCode& status) { - return input.withOutput(FormattedValue(UnicodeString("NaN")), status); +static FormattedPlaceholder notANumber(const FormattedPlaceholder& input) { + return input.withOutput(FormattedValue(UnicodeString("NaN"))); } static number::FormattedNumber stringAsNumber(const number::LocalizedNumberFormatter& nf, @@ -476,7 +476,7 @@ FormattedPlaceholder StandardFunctions::Number::format(FormattedPlaceholder&& ar // No argument => return "NaN" if (!arg.canFormat()) { errorCode = U_MF_OPERAND_MISMATCH_ERROR; - return notANumber(arg, errorCode); + return notANumber(arg); } // Already checked that contents can be formatted @@ -488,6 +488,8 @@ FormattedPlaceholder StandardFunctions::Number::format(FormattedPlaceholder&& ar realFormatter = formatterForOptions(*this, opts, errorCode); number::FormattedNumber numberResult; + bool badOperand = false; + if (U_SUCCESS(errorCode)) { switch (toFormat.getType()) { case UFMT_DOUBLE: { @@ -511,20 +513,22 @@ FormattedPlaceholder StandardFunctions::Number::format(FormattedPlaceholder&& ar case UFMT_STRING: { // Try to parse the string as a number numberResult = stringAsNumber(realFormatter, arg, errorCode); - if (U_FAILURE(errorCode)) { - errorCode = U_MF_OPERAND_MISMATCH_ERROR; - return notANumber(arg, errorCode); - } + badOperand = U_FAILURE(errorCode); break; } default: { // Other types can't be parsed as a number - errorCode = U_MF_OPERAND_MISMATCH_ERROR; - return notANumber(arg, errorCode); + badOperand = true; + break; } } } + if (badOperand) { + errorCode = U_MF_OPERAND_MISMATCH_ERROR; + return notANumber(arg); + } + // The result's options are the merged options FormattedPlaceholder result = arg.withOutputAndOptions(std::move(opts), FormattedValue(std::move(numberResult)), diff --git a/icu4c/source/i18n/unicode/messageformat2_formattable.h b/icu4c/source/i18n/unicode/messageformat2_formattable.h index 5f1ff03720ec..35dbfdacc84e 100644 --- a/icu4c/source/i18n/unicode/messageformat2_formattable.h +++ b/icu4c/source/i18n/unicode/messageformat2_formattable.h @@ -449,8 +449,7 @@ class U_I18N_API FormattableWithOptions : public UObject { Formattable value; // Has to be a pointer in order to break the loop between FormattableWithOptions // and FunctionOptions - // Options from previous evaluation. Non-null (map is empty if - // type != kEvaluated) + // Options from previous evaluation. bogus || options.isValid() LocalPointer options; public: @@ -468,6 +467,7 @@ class U_I18N_API FormattableWithOptions : public UObject { friend inline void swap(FormattableWithOptions& f1, FormattableWithOptions& f2) noexcept { using std::swap; + swap(f1.bogus, f2.bogus); swap(f1.value, f2.value); swap(f1.options, f2.options); } @@ -485,7 +485,7 @@ class U_I18N_API FormattableWithOptions : public UObject { * @deprecated This API is for technology preview only. */ FormattableWithOptions& operator=(FormattableWithOptions) noexcept; - FormattableWithOptions() {} + FormattableWithOptions(); FormattableWithOptions(const Formattable&, UErrorCode&); FormattableWithOptions(const Formattable& f, FunctionOptions&& o, UErrorCode& status); FormattableWithOptions withOptions(FunctionOptions&&, UErrorCode& status) const; @@ -685,12 +685,11 @@ class U_I18N_API ResolvedFunctionOption : public UObject { * * @param output A `FormattedValue` representing formatted output. * Passed by move. - * @param status Input/output error code * * @internal ICU 75 technology preview * @deprecated This API is for technology preview only. */ - FormattedPlaceholder withOutput(FormattedValue&& output, UErrorCode& status) const; + FormattedPlaceholder withOutput(FormattedValue&& output) const; /** * Constructor for fully formatted placeholders with options. Uses the source * and fallback string from `this`. diff --git a/icu4c/source/test/intltest/messageformat2test_custom.cpp b/icu4c/source/test/intltest/messageformat2test_custom.cpp index 23b51626a7af..acc7c073dac9 100644 --- a/icu4c/source/test/intltest/messageformat2test_custom.cpp +++ b/icu4c/source/test/intltest/messageformat2test_custom.cpp @@ -331,7 +331,7 @@ message2::FormattedPlaceholder PersonNameFormatter::format(FormattedPlaceholder& result += firstName; } - return arg.withOutput(FormattedValue(std::move(result)), errorCode); + return arg.withOutput(FormattedValue(std::move(result))); } FormattableProperties::~FormattableProperties() {} @@ -419,7 +419,7 @@ message2::FormattedPlaceholder GrammarCasesFormatter::format(FormattedPlaceholde } } - return arg.withOutput(FormattedValue(std::move(result)), errorCode); + return arg.withOutput(FormattedValue(std::move(result))); } void TestMessageFormat2::testGrammarCasesFormatter(IcuTestErrorCode& errorCode) { @@ -557,7 +557,7 @@ message2::FormattedPlaceholder message2::ListFormatter::format(FormattedPlacehol } } - return arg.withOutput(FormattedValue(std::move(result)), errorCode); + return arg.withOutput(FormattedValue(std::move(result))); } void TestMessageFormat2::testListFormatter(IcuTestErrorCode& errorCode) { @@ -715,7 +715,7 @@ message2::FormattedPlaceholder ResourceManager::format(FormattedPlaceholder&& ar if (U_FAILURE(errorCode)) { errorCode = savedStatus; } - return arg.withOutput(FormattedValue(std::move(result)), errorCode); + return arg.withOutput(FormattedValue(std::move(result))); } else { // Properties must be provided errorCode = U_MF_FORMATTING_ERROR;