Last active
August 29, 2015 14:03
-
-
Save dankogai/f917c6de0db1da40b087 to your computer and use it in GitHub Desktop.
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
extension String { | |
static func fromUTF16Chars(utf16s:UInt16[]) -> String { | |
var str = "" | |
for var i = 0; i < utf16s.count; i++ { | |
let hi = Int(utf16s[i]) | |
switch hi { | |
case 0xD800...0xDBFF: | |
let lo = Int(utf16s[++i]) | |
let us = 0x10000 | |
+ (hi - 0xD800)*0x400 + (lo - 0xDC00) | |
str += Character(UnicodeScalar(us)) | |
default: | |
str += Character(UnicodeScalar(hi)) | |
} | |
} | |
return str | |
} | |
} | |
let str = "aαあ🐣aαあ🐣" | |
var utf16cs = UInt16[]() | |
for utf16c in str.utf16 { | |
utf16cs += utf16c | |
} | |
let str2 = String.fromUTF16Chars(utf16cs) | |
assert(str2 == str) | |
println(str2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment