|
| 1 | +# Selection and Formatting on String Values |
| 2 | + |
| 3 | +Status: **Proposed** |
| 4 | + |
| 5 | +<details> |
| 6 | + <summary>Metadata</summary> |
| 7 | + <dl> |
| 8 | + <dt>Contributors</dt> |
| 9 | + <dd>@aphillips</dd> |
| 10 | + <dt>First proposed</dt> |
| 11 | + <dd>2024-02-16</dd> |
| 12 | + <dt>Pull Request</dt> |
| 13 | + <dd><a href="https://github.com/unicode-org/message-format-wg/pull/660">#660</a></dd> |
| 14 | + </dl> |
| 15 | +</details> |
| 16 | + |
| 17 | +## Objective |
| 18 | + |
| 19 | +Define how selection and formatting of string values takes place. |
| 20 | + |
| 21 | +## Background |
| 22 | + |
| 23 | +This design doc covers the text needed in the registry for selection and formatting of |
| 24 | +string values. |
| 25 | +This includes functionality to match or replace that found in MF1's `SelectFormat`. |
| 26 | + |
| 27 | +## Use-Cases |
| 28 | + |
| 29 | +As a user, I wish to select the pattern to use based on an enumerated list of values. |
| 30 | + |
| 31 | +As a user, I wish to insert strings into a pattern. |
| 32 | + |
| 33 | +As a user, I wish to insert data values that do not have a custom or built-in formatting |
| 34 | +function into a pattern, relying on the data value or object's ability to stringify itself. |
| 35 | + |
| 36 | + |
| 37 | +## Requirements |
| 38 | + |
| 39 | +- Enable `SelectFormat` |
| 40 | + |
| 41 | +## Constraints |
| 42 | + |
| 43 | +ICU MF1 messages using `select` should be representable in MF2. |
| 44 | + |
| 45 | +## Proposed Design |
| 46 | + |
| 47 | +## String Selection and Formatting |
| 48 | + |
| 49 | +### Functions |
| 50 | + |
| 51 | +The following functions are provided: |
| 52 | + |
| 53 | +The function `:string` provides string selection and formatting. |
| 54 | + |
| 55 | +### Operands |
| 56 | + |
| 57 | +The _operand_ is any literal or an implementation-defined set of string or |
| 58 | +character-sequence types. |
| 59 | + |
| 60 | +> [!NOTE] |
| 61 | +> This should probably include individual character types, such as `char`. |
| 62 | +
|
| 63 | +In addition, implementations MAY perform formatting and selection on |
| 64 | +`operand` values that do not otherwise have a formatting function registered. |
| 65 | + |
| 66 | +### Options |
| 67 | + |
| 68 | +The function `:string` has no options. |
| 69 | + |
| 70 | +> [!NOTE] |
| 71 | +> Proposals for string transformation options or implementation |
| 72 | +> experience with user requirements is desired during the Tech Preview. |
| 73 | +
|
| 74 | +### Selection |
| 75 | + |
| 76 | +When implementing [`MatchSelectorKeys`](spec/formatting.md#resolve-preferences), |
| 77 | +the `:string` selector performs as described below. |
| 78 | + |
| 79 | +- Let `return_value` be a new empty list of strings. |
| 80 | +- Let `operand` be the resolved value of the _operand_. |
| 81 | + If the `operand` is not a string literal, convert the value to a string literal, |
| 82 | + or, optionally: emit a _Selection Error_ and return `return_value`. |
| 83 | +- Let `keys` be a list of strings containing keys to match. |
| 84 | + (Hint: this list is an argument to `MatchSelectorKeys`) |
| 85 | +- For each string `key` in `keys`: |
| 86 | + - If the value of `key` is equal to the string value of `operand` |
| 87 | + then `key` matches the selector. |
| 88 | + A `key` and an `operand` are equal if they consist of the same |
| 89 | + sequence of Unicode code points. |
| 90 | + Add `key` to the front of the `return_value` list. |
| 91 | +- Return `return_value` |
| 92 | + |
| 93 | +> [!NOTE] |
| 94 | +> Matching of `key` and `operand` values is sensitive to the sequence of code points |
| 95 | +> in each string. |
| 96 | +> As a result, variations in how text can be encoded can affect the performance of matching. |
| 97 | +> The function `:string` does not perform case folding or Unicode Normalization of string values. |
| 98 | +
|
| 99 | +> [!NOTE] |
| 100 | +> Unquoted string literals in a _variant_ do not include spaces. |
| 101 | +> If users wish to match strings that include whitespace |
| 102 | +> (including U+3000 `IDEOGRAPHIC SPACE`) |
| 103 | +> to a key, the `key` needs to be quoted. |
| 104 | +> |
| 105 | +> For example: |
| 106 | +>``` |
| 107 | +> .match {$string :string} |
| 108 | +> | space key | {{Matches the string " space key "}} |
| 109 | +> * {{Matches the string "space key"}} |
| 110 | +>``` |
| 111 | +
|
| 112 | +### Formatting |
| 113 | +
|
| 114 | +The `:string` function returns the string value of the resolved value of the _operand_. |
0 commit comments