mirror of
https://github.com/apple/swift-foundation.git
synced 2025-05-22 05:19:57 +08:00
`AttributedString` goes out of its way to emulate the stdlib’s behavior for `String` slicing. Unfortunately, it went a little overboard: ```swift import Foundation let str1 = "F\u{301}a\u{308}n\u{303}c\u{327}y\u{30a}" // "F́äñçẙ" let str2 = AttributedString(str1) let substr1 = str1[...str1.startIndex] // “F\u{301}” let substr2 = str2[...str2.startIndex] // “F” print(Array(substr1.unicodeScalars)) print(Array(substr2.unicodeScalars)) ``` We expect slicing on range expressions to always produce consistent results between `String` and `AttributedString`. When slicing character-based string views on `…str.startIndex`, we naturally expect the range expression to cover the entire first `Character`, and that is precisely what `String` does. `AttributedString`’s default view is not as obviously `Character`-based as `String` is, but emulating the stdlib’s behavior is much preferable than to allow such simple, character-aligned range expressions to slice the attributed string at random sub-character positions.