Skip to content

Instantly share code, notes, and snippets.

@ole
Created September 25, 2017 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/1065640c31d675d651964d502b1365db to your computer and use it in GitHub Desktop.
Save ole/1065640c31d675d651964d502b1365db to your computer and use it in GitHub Desktop.
Code for Stack Overflow question https://stackoverflow.com/q/46408487/116862
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