Skip to content

Commit be097f0

Browse files
committed
wip
1 parent bf3eed8 commit be097f0

File tree

6 files changed

+56
-125
lines changed

6 files changed

+56
-125
lines changed

icu4c/source/i18n/messageformat2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ FunctionContext MessageFormatter::makeFunctionContext(const FunctionOptions& opt
353353

354354
const Operand& rand = expr.getOperand();
355355
// Evaluate the operand (evalOperand handles the case of a null operand)
356-
InternalValue& randVal = evalOperand(globalEnv, rand, context, status);
356+
InternalValue& randVal = evalOperand(fallback, globalEnv, rand, context, status);
357357

358358
// If there's no function, we check for an implicit formatter
359359
if (!expr.isFunctionCall()) {

icu4c/source/i18n/messageformat2_evaluation.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -469,46 +469,6 @@ PrioritizedVariant::~PrioritizedVariant() {}
469469
// InternalValue
470470
// -------------
471471

472-
bool InternalValue::isFallback() const {
473-
return std::holds_alternative<FormattedPlaceholder>(argument)
474-
&& std::get_if<FormattedPlaceholder>(&argument)->isFallback();
475-
}
476-
477-
bool InternalValue::hasNullOperand() const {
478-
return std::holds_alternative<FormattedPlaceholder>(argument)
479-
&& std::get_if<FormattedPlaceholder>(&argument)->isNullOperand();
480-
}
481-
482-
FormattedPlaceholder InternalValue::takeArgument(UErrorCode& errorCode) {
483-
if (U_FAILURE(errorCode)) {
484-
return {};
485-
}
486-
487-
if (std::holds_alternative<FormattedPlaceholder>(argument)) {
488-
return std::move(*std::get_if<FormattedPlaceholder>(&argument));
489-
}
490-
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
491-
return {};
492-
}
493-
494-
const UnicodeString& InternalValue::getFallback() const {
495-
if (std::holds_alternative<FormattedPlaceholder>(argument)) {
496-
return std::get_if<FormattedPlaceholder>(&argument)->getFallback();
497-
}
498-
return (*std::get_if<InternalValue*>(&argument))->getFallback();
499-
}
500-
501-
const Selector* InternalValue::getSelector(UErrorCode& errorCode) const {
502-
if (U_FAILURE(errorCode)) {
503-
return nullptr;
504-
}
505-
506-
if (selector == nullptr) {
507-
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
508-
}
509-
return selector;
510-
}
511-
512472
InternalValue::InternalValue(FormattedPlaceholder&& arg) {
513473
argument = std::move(arg);
514474
selector = nullptr;

icu4c/source/i18n/messageformat2_evaluation.h

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -311,44 +311,6 @@ namespace message2 {
311311

312312
}; // class MessageContext
313313

314-
// InternalValue
315-
// ----------------
316-
317-
class InternalValue : public UObject {
318-
public:
319-
const FunctionName& getFunctionName() const { return name; }
320-
bool canSelect() const { return selector != nullptr; }
321-
const Selector* getSelector(UErrorCode&) const;
322-
FormattedPlaceholder forceFormatting(DynamicErrors& errs,
323-
UErrorCode& errorCode);
324-
void forceSelection(DynamicErrors& errs,
325-
const UnicodeString* keys,
326-
int32_t keysLen,
327-
UnicodeString* prefs,
328-
int32_t& prefsLen,
329-
UErrorCode& errorCode);
330-
// Needs to be deep-copyable and movable
331-
virtual ~InternalValue();
332-
InternalValue(FormattedPlaceholder&&);
333-
// Formatter and selector may be null
334-
InternalValue(InternalValue*, FunctionOptions&&, const FunctionName&, const Formatter*,
335-
const Selector*);
336-
const UnicodeString& getFallback() const;
337-
bool isFallback() const;
338-
bool hasNullOperand() const;
339-
// Can't be used anymore after calling this
340-
FormattedPlaceholder takeArgument(UErrorCode& errorCode);
341-
InternalValue(InternalValue&& other) { *this = std::move(other); }
342-
InternalValue& operator=(InternalValue&& other) noexcept;
343-
private:
344-
// InternalValue is owned (if present)
345-
std::variant<InternalValue*, FormattedPlaceholder> argument;
346-
FunctionOptions options;
347-
FunctionName name;
348-
const Selector* selector; // May be null
349-
const Formatter* formatter; // May be null, but one or the other should be non-null unless argument is a FormattedPlaceholder
350-
}; // class InternalValue
351-
352314
} // namespace message2
353315

354316
U_NAMESPACE_END

icu4c/source/i18n/messageformat2_formatter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ namespace message2 {
144144
.adoptFunction(FunctionName(UnicodeString("integer")),
145145
integer.orphan(), success)
146146
.adoptFunction(FunctionName(UnicodeString("string")),
147-
string.orphan(), success);
147+
string.orphan(), success)
148148
.adoptFunction(FunctionName(UnicodeString("test:function")),
149149
testFunction.orphan(), success)
150150
.adoptFunction(FunctionName(UnicodeString("test:format")),

icu4c/source/i18n/messageformat2_function_registry.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ MFFunctionRegistry::Builder::Builder(UErrorCode& errorCode) {
8585
formattersByType = new Hashtable();
8686
if (functions == nullptr || formattersByType == nullptr) {
8787
errorCode = U_MEMORY_ALLOCATION_ERROR;
88-
} else {
89-
formatters->setValueDeleter(uprv_deleteUObject);
90-
selectors->setValueDeleter(uprv_deleteUObject);
91-
formattersByType->setValueDeleter(uprv_deleteUObject);
9288
}
9389

9490
functions->setValueDeleter(uprv_deleteUObject);
@@ -507,7 +503,7 @@ static UChar32 digitToChar(int32_t val, UErrorCode errorCode) {
507503
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
508504
return '0';
509505
}
510-
return result;
506+
return '0';
511507
}
512508

513509
int32_t StandardFunctions::Number::digitSizeOption(const FunctionOptions& opts,
@@ -623,9 +619,11 @@ StandardFunctions::NumberValue::NumberValue(const Number& parent,
623619
}
624620
case UFMT_STRING: {
625621
// Try to parse the string as a number
626-
double d = parseNumberLiteral(toFormat, errorCode);
622+
const UnicodeString& s = operand.getString(errorCode);
623+
U_ASSERT(U_SUCCESS(errorCode));
624+
double d = parseNumberLiteral(s, errorCode);
627625
if (U_FAILURE(errorCode))
628-
return {};
626+
return;
629627
formattedNumber = realFormatter.formatDouble(d, errorCode);
630628
integerValue = static_cast<int64_t>(std::round(d));
631629
break;

icu4c/source/i18n/messageformat2_function_registry_internal.h

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -172,56 +172,67 @@ namespace message2 {
172172
};
173173

174174
// See https://github.com/unicode-org/message-format-wg/blob/main/test/README.md
175-
class TestFormatFactory : public FormatterFactory {
175+
class TestFormat : public Function {
176176
public:
177-
Formatter* createFormatter(const Locale& locale, UErrorCode& status) override;
178-
TestFormatFactory() {}
179-
virtual ~TestFormatFactory();
177+
LocalPointer<FunctionValue> call(const FunctionContext& context,
178+
const FunctionValue& operand,
179+
const FunctionOptions& options,
180+
UErrorCode& errorCode) override;
181+
virtual ~TestFormat();
180182
};
181183

182-
class TestSelect;
183-
184-
class TestFormat : public Formatter {
184+
class TestFormatValue : public FunctionValue {
185185
public:
186-
FormattedPlaceholder format(FormattedPlaceholder&& toFormat, FunctionOptions&& options, UErrorCode& status) const override;
187-
virtual ~TestFormat();
188-
189-
private:
190-
friend class TestFormatFactory;
191-
friend class TestSelect;
192-
TestFormat() {}
193-
static void testFunctionParameters(const FormattedPlaceholder& arg,
194-
const FunctionOptions& options,
195-
int32_t& decimalPlaces,
196-
bool& failsFormat,
197-
bool& failsSelect,
198-
double& input,
199-
UErrorCode& status);
186+
UnicodeString formatToString(UErrorCode&) const override;
187+
TestFormatValue();
188+
virtual ~TestFormatValue();
189+
};
200190

191+
class TestSelect : public Function {
192+
public:
193+
LocalPointer<FunctionValue> call(const FunctionContext& context,
194+
const FunctionValue& operand,
195+
const FunctionOptions& options,
196+
UErrorCode& errorCode) override;
197+
virtual ~TestSelect();
201198
};
202199

203-
// See https://github.com/unicode-org/message-format-wg/blob/main/test/README.md
204-
class TestSelectFactory : public SelectorFactory {
200+
class TestSelectValue : public FunctionValue {
205201
public:
206-
Selector* createSelector(const Locale& locale, UErrorCode& status) const override;
207-
TestSelectFactory() {}
208-
virtual ~TestSelectFactory();
202+
void selectKeys(const UnicodeString* keys,
203+
int32_t keysLen,
204+
int32_t* prefs,
205+
int32_t& prefsLen,
206+
UErrorCode& status) const override;
207+
UBool isSelectable() const override { return true; }
208+
TestSelectValue();
209+
virtual ~TestSelectValue();
209210
};
210211

211-
class TestSelect : public Selector {
212+
class TestFunction : public Function {
212213
public:
213-
void selectKey(FormattedPlaceholder&& val,
214-
FunctionOptions&& options,
215-
const UnicodeString* keys,
216-
int32_t keysLen,
217-
UnicodeString* prefs,
218-
int32_t& prefsLen,
219-
UErrorCode& status) const override;
220-
virtual ~TestSelect();
214+
static TestFunction* testFunction(UErrorCode&);
215+
static TestFunction* testFormat(UErrorCode&);
216+
static TestFunction* testSelect(UErrorCode&);
221217

222-
private:
223-
friend class TestSelectFactory;
224-
TestSelect() {}
218+
LocalPointer<FunctionValue> call(const FunctionContext& context,
219+
const FunctionValue& operand,
220+
const FunctionOptions& options,
221+
UErrorCode& errorCode) override;
222+
virtual ~TestFunction();
223+
};
224+
225+
class TestFunctionValue : public FunctionValue {
226+
public:
227+
UnicodeString formatToString(UErrorCode&) const override;
228+
void selectKeys(const UnicodeString* keys,
229+
int32_t keysLen,
230+
int32_t* prefs,
231+
int32_t& prefsLen,
232+
UErrorCode& status) const override;
233+
UBool isSelectable() const override { return true; }
234+
TestFunctionValue();
235+
virtual ~TestFunctionValue();
225236
};
226237

227238
class StringValue : public FunctionValue {

0 commit comments

Comments
 (0)