[swift-evolution] Int indexing into UTF16View

David Hart david at hartbit.com
Thu Jun 8 18:38:28 CDT 2017



> On 8 Jun 2017, at 14:15, Vladimir.S <svabox at gmail.com> wrote:
> 
>> On 08.06.2017 20:32, David Hart via swift-evolution wrote:
>> Hello,
>> When working with Strings which are known to be ASCII, I tend to use the UTF16View for the performance of random access. 
> 
> About the performance. Do we have a guarantee that 'barcode' declared in code and/or containing only ASCII chars internally stored as UTF16 ? Otherwise, as I understand, you'll have a performance penalty calling utf16 when internal storage is in UTF8 for example, no?

I'm fairly sure the internal storage of String is always UTF16.

> 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?
>> David.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list