Skip to content

Commit 7bdf2e5

Browse files
committed
Add fallback to FunctionValue that can be used by function implementations
1 parent 6b1128c commit 7bdf2e5

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

icu4c/source/i18n/messageformat2.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static UnicodeString functionFallback(const InternalValue& operand,
6969
fallbackToUse += var;
7070
}
7171
// If it exists, create a BaseValue (FunctionValue) for it
72-
LocalPointer<BaseValue> result(BaseValue::create(locale, *val, errorCode));
72+
LocalPointer<BaseValue> result(BaseValue::create(locale, fallbackToUse, *val, errorCode));
7373
// Add fallback and return an InternalValue
7474
if (U_SUCCESS(errorCode)) {
7575
return InternalValue(result.orphan(), fallbackToUse);
@@ -110,6 +110,7 @@ static UnicodeString reserialize(const UnicodeString& s) {
110110

111111
// Create a BaseValue (FunctionValue) that wraps the literal
112112
LocalPointer<BaseValue> val(BaseValue::create(locale,
113+
fallbackToUse,
113114
Formattable(lit.unquoted()),
114115
errorCode));
115116
if (U_SUCCESS(errorCode)) {

icu4c/source/i18n/messageformat2_evaluation.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@ using namespace data_model;
2727
// BaseValue
2828
// ---------
2929

30-
BaseValue::BaseValue(const Locale& loc, const Formattable& source)
30+
BaseValue::BaseValue(const Locale& loc, const UnicodeString& fb, const Formattable& source)
3131
: locale(loc) {
3232
operand = source;
33+
fallback += LEFT_CURLY_BRACE;
34+
fallback += fb;
35+
fallback += RIGHT_CURLY_BRACE;
3336
}
3437

3538
/* static */ BaseValue* BaseValue::create(const Locale& locale,
39+
const UnicodeString& fallback,
3640
const Formattable& source,
3741
UErrorCode& errorCode) {
38-
return message2::create<BaseValue>(BaseValue(locale, source), errorCode);
42+
return message2::create<BaseValue>(BaseValue(locale, fallback, source), errorCode);
3943
}
4044

4145
extern UnicodeString formattableToString(const Locale&, const UBiDiDirection, const Formattable&, UErrorCode&);
@@ -51,6 +55,7 @@ BaseValue& BaseValue::operator=(BaseValue&& other) noexcept {
5155
operand = std::move(other.operand);
5256
opts = std::move(other.opts);
5357
locale = other.locale;
58+
fallback = other.fallback;
5459

5560
return *this;
5661
}

icu4c/source/i18n/messageformat2_evaluation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ namespace message2 {
132132
// in a context that expects a FunctionValue.
133133
class BaseValue : public FunctionValue {
134134
public:
135-
static BaseValue* create(const Locale&, const Formattable&, UErrorCode&);
135+
static BaseValue* create(const Locale&, const UnicodeString&, const Formattable&, UErrorCode&);
136136
// Apply default formatters to the argument value
137137
UnicodeString formatToString(UErrorCode&) const override;
138138
UBool isSelectable() const override { return true; }
@@ -142,7 +142,7 @@ namespace message2 {
142142
private:
143143
Locale locale;
144144

145-
BaseValue(const Locale&, const Formattable&);
145+
BaseValue(const Locale&, const UnicodeString&, const Formattable&);
146146
}; // class BaseValue
147147

148148
// A NullValue represents the absence of an argument.

icu4c/source/i18n/messageformat2_function_registry.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ StandardFunctions::TestFunctionValue::TestFunctionValue(const TestFunction& pare
18051805
// If FailsFormat is true, attempting to format the placeholder to any
18061806
// formatting target will fail.
18071807
if (failsFormat) {
1808-
status = U_MF_FORMATTING_ERROR;
1808+
formattedString = arg.getFallback();
18091809
return;
18101810
}
18111811

@@ -1848,6 +1848,8 @@ UnicodeString StandardFunctions::TestFunctionValue::formatToString(UErrorCode& s
18481848
}
18491849
if (!canFormat || failsFormat) {
18501850
status = U_MF_FORMATTING_ERROR;
1851+
}
1852+
if (!canFormat) {
18511853
return {};
18521854
}
18531855
return formattedString;

icu4c/source/i18n/unicode/messageformat2_function_registry.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,15 @@ namespace message2 {
438438
* @deprecated This API is for technology preview only.
439439
*/
440440
virtual const UnicodeString& getFunctionName() const { return functionName; }
441+
/* Returns a fallback string that can be used as output
442+
* if processing this function results in an error.
443+
* *
444+
* @returns A string determined by the creator of this FunctionValue.
445+
*
446+
* @internal ICU 78 technology preview
447+
* @deprecated This API is for technology preview only.
448+
*/
449+
virtual const UnicodeString& getFallback() const { return fallback; }
441450
/**
442451
* Destructor.
443452
*
@@ -462,12 +471,19 @@ namespace message2 {
462471
FunctionOptions opts;
463472
/**
464473
* The name of the function that constructed this FunctionValue.
474+
*
465475
* @internal ICU 78 technology preview
466476
* @deprecated This API is for technology preview only.
467477
*/
468478
UnicodeString functionName;
469-
private:
470-
479+
/**
480+
* Fallback string that can be used if a later function encounters
481+
* an error when processing this FunctionValue.
482+
*
483+
* @internal ICU 78 technology preview
484+
* @deprecated This API is for technology preview only.
485+
*/
486+
UnicodeString fallback;
471487
}; // class FunctionValue
472488

473489
} // namespace message2

0 commit comments

Comments
 (0)