[swift-dev] Quick pitch: Change Linux’s string comparison to match Darwin’s
milseman at apple.com
Tue Jul 25 16:01:38 CDT 2017
On Darwin, known-ASCII strings are sorted according to the lexicographical ordering of their code units. All non-known-ASCII strings are otherwise ordered based on the UCA. On Linux, however, even known-ASCII strings are ordered based on UCA. I propose to unify these by changing Linux’s string sort order to match Darwin’s in Swift 4.0.
Swift’s default ordering for strings is appropriate for machine consumption (e.g. implementing sorted collections). It obeys Unicode canonical equivalence, that is strings compare the same modulo normalization. However, it is not meant to be sufficient for presenting a meaningful ordering to human consumers, as that requires incorporating reader-specific information (e.g. ).
Known-ASCII strings are a trivial case for the described sort order semantics: pure ASCII is unaffected by normalization. Thus, lexicographical ordering of code units is a valid machine ordering for ASCII strings. On Darwin, this is used to order known-ASCII strings while Linux uses UCA even for known-ASCII strings.
Long term, the plan is to switch String’s sort order to be the lexicographical ordering of normalized code units (or perhaps scalar values), as mentioned in the String Manifesto. This is a more efficient ordering than that provided by UCA. However, this will not make it in time for Swift 4.0.
I propose to change Linux’s sort order for known-ASCII strings to be the same as it is on Darwin. This will be accomplished by dropping the relevant #if guards in StringCompare.swift. An example implementation can be found at .
In addition to unifying sort order semantics across platforms, this will also deliver significant performance boosts to pure ASCII strings on Linux.
 UTS #10: Unicode Collation Algorithm <http://unicode.org/reports/tr10/>
 Canonical Equivalence in Applications <http://unicode.org/notes/tn5/>
 UCA: Contextual Sensitivity <http://unicode.org/reports/tr10/#Contextual_Sensitivity>
 String Manifesto: Comparing and Hashing Strings <https://github.com/apple/swift/blob/master/docs/StringManifesto.md#comparing-and-hashing-strings>
 Unifying Linux/Darwin ASCII sort order semantics - github <https://github.com/milseman/swift/commit/5560e13198d5cc284f46bf190f59a2edf7ed747b>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-dev