[swift-evolution] Pitch: String Index Overhaul

Dave Abrahams dabrahams at apple.com
Tue May 30 16:53:34 CDT 2017


on Tue May 30 2017, Jordan Rose <jordan_rose-AT-apple.com> wrote:

> My knee-jerk reaction is to say it's too late in Swift 4 for this kind
> of change, but with that out of the way, I'm most concerned about what
> it means to have, say, a UTF-8 index that's not on a UTF-16 boundary.
>
> let str = "言"
> let oneUnitIn = str.utf8.index(after: str.utf8.startIndex)
> let trailingBytes = str.utf8[oneUnitIn...]

This is not new; it exists today.  

> What can I do with 'oneUnitIn'? 

All the usual stuff; we're not proposing to change what you can do with
it.

> How do I test to see if it's on a Character boundary or a
> UnicodeScalar boundary?

as noted,

  Replacing the failable APIs listed [above](#motivation) that detect
  whether an index represents a valid position in a given view, and
  enhancement that explicitly round index positions to nearby boundaries
  in a given view, are left to a later proposal.  For now, we do not
  propose to remove the existing index conversion APIs.

That means you can use oneUnitIn.samePosition(in: str) or
oneUnitIn.samePosition(in: str.unicodeScalars) to find out if it's on ta
character or unicode scalar boundary.

-- 
-Dave


More information about the swift-evolution mailing list