Last active
August 29, 2015 14:04
-
-
Save ilyannn/2cbad1c6ebf8f0b75e23 to your computer and use it in GitHub Desktop.
String elements in Swift
This file contains hidden or 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
// Demonstrates some rather elementary points about code points vs elements | |
import Foundation | |
import Swift | |
extension String { | |
func canonize() -> [Character] { | |
return Array(precomposedStringWithCanonicalMapping) | |
} | |
static func decanonize(chars: [Character]) -> String { | |
return "".join ( chars.map { String($0) }) | |
} | |
} | |
func test(s:String) -> String { | |
let c = s.canonize() | |
let ß = String.decanonize(c) | |
let equal = ß == s ? "equals" : "does not equal" | |
return "\(s) has \(s.utf16Count) UTF-16 code points, \(c.count) elements, \(ß.utf16Count) precomposed code points, \(equal)" | |
} | |
test("Åström") | |
// "Åström has 6 UTF-16 code points, 6 elements, 6 precomposed code points, equals" | |
test("\u{0041}\u{030A}str\u{006F}\u{0308}m") | |
// "Åström has 8 UTF-16 code points, 6 elements, 6 precomposed code points, does not equal" | |
let ö1 = "Åström".canonize()[4] | |
let ö2 = "\u{0041}\u{030A}str\u{006F}\u{0308}m".canonize()[4] | |
ö1 == ö2 // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment