[swift-evolution] Pitch: String Index Overhaul

Dave Abrahams dabrahams at apple.com
Fri Jun 2 19:06:32 CDT 2017


on Fri Jun 02 2017, Brent Royal-Gordon <swift-evolution at swift.org> wrote:

>> On May 27, 2017, at 10:40 AM, Dave Abrahams via swift-evolution
> <swift-evolution at swift.org> wrote:
>> 
>> A property and an intializer will be added to `String.Index`, exposing
>> the offset of the index in code units (currently only UTF-16) from the
>> beginning of the string:
>> 
>> ```swift
>> let n: Int = html.endIndex.encodedOffset
>> let end = String.Index(encodedOffset: n)
>> assert(end == String.endIndex)
>> ```
>
> Do you intend to allow users to serialize an `encodedOffset` and
> deserialize it later, perhaps in a later version of Swift, to
> represent the same position? If so, I'm not sure how you intend to
> maintain compatibility once the "currently only UTF-16" notation is no
> longer true.

Strings will offer access to their underlying code units, and can be
serialized and deserialized however they are represented.  A String
stored as UTF-8 will be serialized and deserialized as UTF-8, so
encodedOffsets will maintain their meanings.

Cheers,
-- 
-Dave



More information about the swift-evolution mailing list