Skip to content

Commit f62fc64

Browse files
committed
Use an array of indices for prefs in selectKeys()
1 parent c199c1c commit f62fc64

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
@@ -420,12 +420,12 @@ void MessageFormatter::matchSelectorKeys(const UVector& keys,
420420
LocalArray<UnicodeString> adoptedKeys(keysArr);
421421

422422
// Create an array to hold the output
423-
UnicodeString* prefsArr = new UnicodeString[keysLen];
423+
int32_t* prefsArr = new int32_t[keysLen];
424424
if (prefsArr == nullptr) {
425425
status = U_MEMORY_ALLOCATION_ERROR;
426426
return;
427427
}
428-
LocalArray<UnicodeString> adoptedPrefs(prefsArr);
428+
LocalArray<int32_t> adoptedPrefs(prefsArr);
429429
int32_t prefsLen = 0;
430430

431431
// Call the selector
@@ -450,7 +450,8 @@ void MessageFormatter::matchSelectorKeys(const UVector& keys,
450450
// Copy the resulting keys (if there was no error)
451451
keysOut.removeAllElements();
452452
for (int32_t i = 0; i < prefsLen; i++) {
453-
UnicodeString* k = message2::create<UnicodeString>(std::move(prefsArr[i]), status);
453+
UnicodeString* k =
454+
message2::create<UnicodeString>(std::move(keysArr[prefsArr[i]]), status);
454455
if (k == nullptr) {
455456
status = U_MEMORY_ALLOCATION_ERROR;
456457
return;

icu4c/source/i18n/messageformat2_function_registry.cpp

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

609609
void StandardFunctions::NumberValue::selectKeys(const UnicodeString* keys,
610610
int32_t keysLen,
611-
UnicodeString* prefs,
611+
int32_t* prefs,
612612
int32_t& prefsLen,
613613
UErrorCode& errorCode) {
614614
CHECK_ERROR(errorCode);
@@ -658,7 +658,7 @@ void StandardFunctions::NumberValue::selectKeys(const UnicodeString* keys,
658658
// 5i(a). If key and exact consist of the same sequence of Unicode code points, then
659659
if (exact == keys[i]) {
660660
// 5i(a)(a) Append key as the last element of the list resultExact.
661-
prefs[prefsLen] = keys[i];
661+
prefs[prefsLen] = i;
662662
prefsLen++;
663663
break;
664664
}
@@ -679,7 +679,7 @@ void StandardFunctions::NumberValue::selectKeys(const UnicodeString* keys,
679679
// 5ii(a). If key and keyword consist of the same sequence of Unicode code points, then
680680
if (keyword == keys[i]) {
681681
// 5ii(a)(a) Append key as the last element of the list resultKeyword.
682-
prefs[prefsLen] = keys[i];
682+
prefs[prefsLen] = i;
683683
prefsLen++;
684684
}
685685
}
@@ -1078,7 +1078,7 @@ StandardFunctions::StringValue::StringValue(const FunctionContext& context,
10781078

10791079
void StandardFunctions::StringValue::selectKeys(const UnicodeString* keys,
10801080
int32_t keysLen,
1081-
UnicodeString* prefs,
1081+
int32_t* prefs,
10821082
int32_t& prefsLen,
10831083
UErrorCode& errorCode) {
10841084
CHECK_ERROR(errorCode);
@@ -1093,7 +1093,7 @@ void StandardFunctions::StringValue::selectKeys(const UnicodeString* keys,
10931093

10941094
for (int32_t i = 0; i < keysLen; i++) {
10951095
if (keys[i] == formattedString) {
1096-
prefs[0] = keys[i];
1096+
prefs[0] = i;
10971097
prefsLen = 1;
10981098
break;
10991099
}

icu4c/source/i18n/messageformat2_function_registry_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace message2 {
114114
UnicodeString formatToString(UErrorCode&) const override;
115115
void selectKeys(const UnicodeString* keys,
116116
int32_t keysLen,
117-
UnicodeString* prefs,
117+
int32_t* prefs,
118118
int32_t& prefsLen,
119119
UErrorCode& status) override;
120120
UBool isSelectable() const override { return true; }
@@ -165,7 +165,7 @@ namespace message2 {
165165
UnicodeString formatToString(UErrorCode&) const override;
166166
void selectKeys(const UnicodeString* keys,
167167
int32_t keysLen,
168-
UnicodeString* prefs,
168+
int32_t* prefs,
169169
int32_t& prefsLen,
170170
UErrorCode& status) override;
171171
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
@@ -406,9 +406,11 @@ namespace message2 {
406406
*
407407
* @param keys An array of strings to compare to the input.
408408
* @param keysLen The length of `keys`.
409-
* @param prefs An array of strings with length `keysLen`. The contents of
409+
* @param prefs An array of indices into `keys`.
410+
* The initial contents of
410411
* the array is undefined. `selectKey()` should set the contents
411-
* of `prefs` to a subset of `keys`, with the best match placed at the lowest index.
412+
* of `prefs` to a subset of the indices in `keys`,
413+
* with the best match placed at the lowest index in `prefs`.
412414
* @param prefsLen A reference that `selectKey()` should set to the length of `prefs`,
413415
* which must be less than or equal to `keysLen`.
414416
* @param status Input/output error code.
@@ -418,7 +420,7 @@ namespace message2 {
418420
*/
419421
virtual void selectKeys(const UnicodeString* keys,
420422
int32_t keysLen,
421-
UnicodeString* prefs,
423+
int32_t* prefs,
422424
int32_t& prefsLen,
423425
UErrorCode& status) {
424426
(void) keys;

0 commit comments

Comments
 (0)