Skip to content

Commit 3571c71

Browse files
committed
Use an array of indices for prefs in selectKeys()
1 parent 07b2cde commit 3571c71

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

icu4c/source/i18n/messageformat2.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,12 @@ void MessageFormatter::matchSelectorKeys(const UVector& keys,
466466
LocalArray<UnicodeString> adoptedKeys(keysArr);
467467

468468
// Create an array to hold the output
469-
UnicodeString* prefsArr = new UnicodeString[keysLen];
469+
int32_t* prefsArr = new int32_t[keysLen];
470470
if (prefsArr == nullptr) {
471471
status = U_MEMORY_ALLOCATION_ERROR;
472472
return;
473473
}
474-
LocalArray<UnicodeString> adoptedPrefs(prefsArr);
474+
LocalArray<int32_t> adoptedPrefs(prefsArr);
475475
int32_t prefsLen = 0;
476476

477477
// Call the selector
@@ -496,7 +496,8 @@ void MessageFormatter::matchSelectorKeys(const UVector& keys,
496496
// Copy the resulting keys (if there was no error)
497497
keysOut.removeAllElements();
498498
for (int32_t i = 0; i < prefsLen; i++) {
499-
UnicodeString* k = message2::create<UnicodeString>(std::move(prefsArr[i]), status);
499+
UnicodeString* k =
500+
message2::create<UnicodeString>(std::move(keysArr[prefsArr[i]]), status);
500501
if (k == nullptr) {
501502
status = U_MEMORY_ALLOCATION_ERROR;
502503
return;

icu4c/source/i18n/messageformat2_function_registry.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ StandardFunctions::Number::pluralType(const FunctionOptions& opts) {
672672

673673
void StandardFunctions::NumberValue::selectKeys(const UnicodeString* keys,
674674
int32_t keysLen,
675-
UnicodeString* prefs,
675+
int32_t* prefs,
676676
int32_t& prefsLen,
677677
UErrorCode& errorCode) {
678678
CHECK_ERROR(errorCode);
@@ -722,7 +722,7 @@ void StandardFunctions::NumberValue::selectKeys(const UnicodeString* keys,
722722
// 5i(a). If key and exact consist of the same sequence of Unicode code points, then
723723
if (exact == keys[i]) {
724724
// 5i(a)(a) Append key as the last element of the list resultExact.
725-
prefs[prefsLen] = keys[i];
725+
prefs[prefsLen] = i;
726726
prefsLen++;
727727
break;
728728
}
@@ -743,7 +743,7 @@ void StandardFunctions::NumberValue::selectKeys(const UnicodeString* keys,
743743
// 5ii(a). If key and keyword consist of the same sequence of Unicode code points, then
744744
if (keyword == keys[i]) {
745745
// 5ii(a)(a) Append key as the last element of the list resultKeyword.
746-
prefs[prefsLen] = keys[i];
746+
prefs[prefsLen] = i;
747747
prefsLen++;
748748
}
749749
}
@@ -1142,7 +1142,7 @@ StandardFunctions::StringValue::StringValue(const FunctionContext& context,
11421142

11431143
void StandardFunctions::StringValue::selectKeys(const UnicodeString* keys,
11441144
int32_t keysLen,
1145-
UnicodeString* prefs,
1145+
int32_t* prefs,
11461146
int32_t& prefsLen,
11471147
UErrorCode& errorCode) {
11481148
CHECK_ERROR(errorCode);
@@ -1159,7 +1159,7 @@ void StandardFunctions::StringValue::selectKeys(const UnicodeString* keys,
11591159

11601160
for (int32_t i = 0; i < keysLen; i++) {
11611161
if (keys[i] == normalized) {
1162-
prefs[0] = keys[i];
1162+
prefs[0] = i;
11631163
prefsLen = 1;
11641164
break;
11651165
}

icu4c/source/i18n/messageformat2_function_registry_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ namespace message2 {
125125
UnicodeString formatToString(UErrorCode&) const override;
126126
void selectKeys(const UnicodeString* keys,
127127
int32_t keysLen,
128-
UnicodeString* prefs,
128+
int32_t* prefs,
129129
int32_t& prefsLen,
130130
UErrorCode& status) override;
131131
UBool isSelectable() const override { return true; }
@@ -229,7 +229,7 @@ namespace message2 {
229229
UnicodeString formatToString(UErrorCode&) const override;
230230
void selectKeys(const UnicodeString* keys,
231231
int32_t keysLen,
232-
UnicodeString* prefs,
232+
int32_t* prefs,
233233
int32_t& prefsLen,
234234
UErrorCode& status) override;
235235
UBool isSelectable() const override { return true; }

icu4c/source/i18n/unicode/messageformat2_function_registry.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,11 @@ namespace message2 {
408408
*
409409
* @param keys An array of strings to compare to the input.
410410
* @param keysLen The length of `keys`.
411-
* @param prefs An array of strings with length `keysLen`. The contents of
411+
* @param prefs An array of indices into `keys`.
412+
* The initial contents of
412413
* the array is undefined. `selectKey()` should set the contents
413-
* of `prefs` to a subset of `keys`, with the best match placed at the lowest index.
414+
* of `prefs` to a subset of the indices in `keys`,
415+
* with the best match placed at the lowest index in `prefs`.
414416
* @param prefsLen A reference that `selectKey()` should set to the length of `prefs`,
415417
* which must be less than or equal to `keysLen`.
416418
* @param status Input/output error code.
@@ -420,7 +422,7 @@ namespace message2 {
420422
*/
421423
virtual void selectKeys(const UnicodeString* keys,
422424
int32_t keysLen,
423-
UnicodeString* prefs,
425+
int32_t* prefs,
424426
int32_t& prefsLen,
425427
UErrorCode& status) {
426428
(void) keys;

0 commit comments

Comments
 (0)