[swift-evolution] Int indexing into UTF16View

Dave Abrahams dabrahams at apple.com
Fri Jun 9 12:29:17 CDT 2017

on Thu Jun 08 2017, David Hart <swift-evolution at swift.org> wrote:

> Hello,
> When working with Strings which are known to be ASCII, I tend to use
> the UTF16View for the performance of random access. I would also like
> to have the convenience of indexing with Int:
> let barcode = "M1XXXXXXXXX/CLEMENT   EELT9QBQGVAAMSEZY1353 244 21D 531  10A1311446838”
> let name = barcode.utf16[2..<22]
> let pnrCode = barcode.utf16[23..<30]
> let seatNo = barcode.utf16[47..<51]
> let fromCity = barcode.utf16[30..<33]
> let toCity = barcode.utf16[33..<36]
> let carrier = barcode.utf16[36..<39]
> let flightNumber = barcode.utf16[39..<44]
> let day = barcode.utf16[44..<47]
> I define my own subscript in an extension to UTF16View but I think this should go in the Standard
> Library.
> Any thoughts?

The standard library has been headed in the direction of supporting
underlying String encodings that are not UTF-16 (or UTF-16 subsets, like
ASCII and Latin-1), e.g. UTF-8, which would make it impossible to
support such an API performantly.  So, such an addition would require a
change in our long-term strategy for String, which was laid out in

That's not to say it's impossible, but it would be a major course change.


More information about the swift-evolution mailing list