Created
September 25, 2017 15:17
-
-
Save ole/1065640c31d675d651964d502b1365db to your computer and use it in GitHub Desktop.
Code for Stack Overflow question https://stackoverflow.com/q/46408487/116862
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "π¨πΎβπ" | |
print(str.unicodeScalars.map { "0x\(String($0.value, radix: 16))" }) | |
// β ["0x1f468", "0x1f3fe", "0x200d", "0x1f692"] | |
print(str.utf16.map { "0x\(String($0, radix: 16))" }) | |
// β ["0xd83d", "0xdc68", "0xd83c", "0xdffe", "0x200d", "0xd83d", "0xde92"] | |
print(str.utf16.count) | |
// β 7 | |
let utf16Offset = 2 | |
let utf16Index = String.Index(encodedOffset: utf16Offset) | |
let char = str[utf16Index] | |
print(char) | |
// β πΎβπ | |
print(char.unicodeScalars.map { "0x\(String($0.value, radix: 16))" }) | |
// β ["0x1f3fe", "0x200d", "0x1f692"] | |
let trappingIndex = String.Index(encodedOffset: 1) | |
//str[trappingIndex] | |
// fatal error: Can't form a Character from a String containing more than one extended grapheme cluster | |
extension String.Index { | |
func isOnCharacterBoundary(in str: String) -> Bool { | |
return String.Index(self, within: str) != nil | |
} | |
} | |
trappingIndex.isOnCharacterBoundary(in: str) | |
// β false (as expected) | |
utf16Index.isOnCharacterBoundary(in: str) | |
// β true (WTF!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment