Fastest way to advance by char count and get that distance in utf16?

These are not stable across Unicode versions, so if they are saved or transmitted separately from the string itself, they risk pointing at something different after they are reloaded.

If you disallow undefined scalars, then you can pick a normalization form, apply it after loading, and save the ranges as scalar distances in that normalization form. Otherwise, to be completely stable, you need to save raw bytes (not strings) and save the ranges as byte distances.

Unless the context guarantees the string is long enough, you probably want to use index(_:offsetBy:limitedBy:) instead.

3 Likes