stdlib/Unicode: fix encoding U+20000 and higher to UTF-16

UTF16.{leadSurrogate,trailSurrogate} were converting intermediate
results to UInt16 too soon, causing a trap on U+20000 and higher.

rdar://19156359

Swift SVN r24678
This commit is contained in:
Dmitri Hrybenko
2015-01-23 04:47:02 +00:00
parent 842d8aee03
commit 7209392bb4
2 changed files with 74 additions and 6 deletions

View File

@@ -867,7 +867,7 @@ extension UTF16 {
/// Requires: `width(x) == 2`
public static func leadSurrogate(x: UnicodeScalar) -> UTF16.CodeUnit {
_precondition(width(x) == 2)
return (UTF16.CodeUnit(x.value - 0x1_0000) >> 10) + 0xD800
return UTF16.CodeUnit((x.value - 0x1_0000) >> 10) + 0xD800
}
/// Return the low surrogate code unit of a `surrogate pair
@@ -877,9 +877,8 @@ extension UTF16 {
/// Requires: `width(x) == 2`
public static func trailSurrogate(x: UnicodeScalar) -> UTF16.CodeUnit {
_precondition(width(x) == 2)
return (
UTF16.CodeUnit(x.value - 0x1_0000)
& (((1 as UTF16.CodeUnit) << 10) - 1)
return UTF16.CodeUnit(
(x.value - 0x1_0000) & (((1 as UInt32) << 10) - 1)
) + 0xDC00
}