Skip to content

Commit 1260c87

Browse files
catamorphismeemeliaphillips
authored
Clarifications to pattern selection (#385)
* Clarifications to pattern selection * State up front that the catch-all key `*` is never passed to the implementation-defined key-comparison method * Clarify the relationship between selector length and key length * Clarify the type signature for `SortVariants` * Fix typo * Remove reference to "the formatter" in description of catch-all matching Co-authored-by: Eemeli Aro <eemeli@gmail.com> * Change "decoded" to "resolved" for keys Change references to "the decoded value of a key" to "the resolved value of a key", and clarify that key resolution is the same as literal resolution * Clarify the relationship between key resolution and literal resolution Co-authored-by: Eemeli Aro <eemeli@gmail.com> * Clarify the relationship between key resolution and literal resolution in all instances * Also remove up-front text explaining key resolution * Normative language for required number of keys in variants Co-authored-by: Addison Phillips <addisonI18N@gmail.com> * Rewrite the call to `SortVariants` In the "Sort Variants" definition, re-specify the implementation-dependent `SortVariants` so as not to constraint whether it modifies the list in-place or returns a new list * Follow style for describing method return values Co-authored-by: Eemeli Aro <eemeli@gmail.com> * Add newline Co-authored-by: Eemeli Aro <eemeli@gmail.com> --------- Co-authored-by: Eemeli Aro <eemeli@gmail.com> Co-authored-by: Addison Phillips <addisonI18N@gmail.com>
1 parent 4e33d64 commit 1260c87

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

spec/formatting.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ the _pattern_ of one of the _variants_ must be selected for formatting.
3737
When a message has a single _selector_,
3838
an implementation-defined method compares each key to the _selector_
3939
and determines which of the keys match, and in what order of preference.
40-
A catch-all key will always match, but is always the least preferred choice.
41-
During selection, the _variant_ with the best-matching key is selected.
40+
The list of keys passed to this implementation-defined method
41+
does not include the catch-all key `*`.
42+
The catch-all key `*` is treated as a match for any _selector_,
43+
but with the lowest possible preference.
4244

4345
In a message with more than one _selector_,
44-
each _variant_ also has a corresponding number of keys.
46+
the number of keys in each _variant_ **_must_** equal the number of _selectors_.
4547
These correspond to _selectors_ by position.
4648
The same implementation-defined method as above is used to compare
4749
the corresponding key of each _variant_ to its _selector_,
@@ -84,7 +86,8 @@ Next, using `res`, resolve the preferential order for all message keys:
8486
1. For each _variant_ `var` of the message:
8587
1. Let `key` be the `var` key at position `i`.
8688
1. If `key` is not the catch-all key `'*'`:
87-
1. Let `ks` be the decoded value of `key`.
89+
1. Assert that `key` is a _literal_.
90+
1. Let `ks` be the resolved value of `key`.
8891
1. Append `ks` as the last element of the list `keys`.
8992
1. Let `rv` be the resolved value at index `i` of `res`.
9093
1. Let `matches` be the result of calling the method MatchSelectorKeys(`rv`, `keys`)
@@ -108,7 +111,8 @@ filter the list of _variants_ to the ones that match with some preference:
108111
1. Let `key` be the `var` key at position `i`.
109112
1. If `key` is the catch-all key `'*'`:
110113
1. Continue the inner loop on `pref`.
111-
1. Let `ks` be the decoded value of `key`.
114+
1. Assert that `key` is a _literal_.
115+
1. Let `ks` be the resolved value of `key`.
112116
1. Let `matches` be the list of strings at index `i` of `pref`.
113117
1. If `matches` includes `ks`:
114118
1. Continue the inner loop on `pref`.
@@ -133,21 +137,27 @@ Finally, sort the list of variants `vars` and select the _pattern_:
133137
1. Let `matchpref` be an integer with the value `minpref`.
134138
1. Let `key` be the `tuple` _variant_ key at position `i`.
135139
1. If `key` is not the catch-all key `'*'`:
136-
1. Let `ks` be the decoded value of `key`.
140+
1. Assert that `key` is a _literal_.
141+
1. Let `ks` be the resolved value of `key`.
137142
1. Let `matchpref` be the integer position of `ks` in `matches`.
138143
1. Set the `tuple` integer value as `matchpref`.
139-
1. Call the method SortVariants(`sortable`).
144+
1. Set `sortable` to be the result of calling the method `SortVariants(sortable)`.
140145
1. Set `i` to be `i` - 1.
141146
1. Let `var` be the _variant_ element of the first element of `sortable`.
142147
1. Select the _pattern_ of `var`.
143148

144-
The method SortVariants is determined by the implementation.
145-
It takes as an argument a `sortable` list of (integer, _variant_) tuples,
146-
which it modifies in place using some stable sorting algorithm.
147-
The method does not return anything.
148-
The list is sorted according to the tuple's first integer element,
149-
such that a lower number is sorted before a higher one,
150-
and entries that have the same number retain their order.
149+
`SortVariants` is a method whose single argument is
150+
a list of (integer, _variant_) tuples.
151+
It returns a list of (integer, _variant_) tuples.
152+
Any implementation of `SortVariants` is acceptable
153+
as long as it satisfies the following requirements:
154+
155+
1. Let `sortable` be an arbitrary list of (integer, _variant_) tuples.
156+
1. Let `sorted` be `SortVariants(sortable)`.
157+
1. `sorted` is the result of sorting `sortable` using the following comparator:
158+
1. `(i1, v1)` <= `(i2, v2)` if and only if `i1 <= i2`.
159+
1. The sort is stable (pairs of tuples from `sortable` that are equal
160+
in their first element have the same relative order in `sorted`).
151161

152162
### Examples
153163

@@ -231,7 +241,7 @@ when * * {Otherwise}
231241
Presuming a more powerful implementation which supports selection on numerical values,
232242
and provides a `:plural` function that matches keys by their exact value
233243
as well as their plural category (preferring the former, if possible),
234-
and an Enligh-language formatting context in which
244+
and an English-language formatting context in which
235245
the variable reference `$count` resolves to the number `1`,
236246
pattern selection proceeds as follows for this message:
237247

0 commit comments

Comments
 (0)