Skip to content

Commit

Permalink
Remove unneeded error code argument
Browse files Browse the repository at this point in the history
  • Loading branch information
catamorphism committed Jun 12, 2024
1 parent a87ac59 commit 64d142a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
38 changes: 18 additions & 20 deletions icu4c/source/i18n/messageformat2_formattable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionOptions>(std::move(o), status));
if (U_FAILURE(status)) {
Expand Down Expand Up @@ -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());
Expand All @@ -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));
Expand Down Expand Up @@ -300,9 +305,7 @@ namespace message2 {
const UnicodeString& fb,
Type t) {
fallback = fb;
if (t != kNull && t != kFallback) {
source = input;
}
source = input;
type = t;
}

Expand Down Expand Up @@ -367,8 +370,7 @@ namespace message2 {
if (asDecimal != nullptr) {
return input.withOutput(FormattedValue(formatNumberWithDefaults(locale,
asDecimal,
status)),
status);
status)));
}
}

Expand All @@ -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
Expand Down
22 changes: 13 additions & 9 deletions icu4c/source/i18n/messageformat2_function_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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: {
Expand All @@ -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)),
Expand Down
9 changes: 4 additions & 5 deletions icu4c/source/i18n/unicode/messageformat2_formattable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionOptions> options;

public:
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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`.
Expand Down
8 changes: 4 additions & 4 deletions icu4c/source/test/intltest/messageformat2test_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 64d142a

Please sign in to comment.