stdlib/String: fix two bugs in UnicodeScalarView

If underlying NSString contained isolated surrogates, then we were crashing in
following ways:

- subscripting by index could crash;

- index pointing to the second code unit sequence was not moved backwards
  correctly.  Instead of moving it to pointing to the beginning of the view it
  could be moved to point to the code unit before the beginning of the view.



Swift SVN r19230
This commit is contained in:
Dmitri Hrybenko
2014-06-26 13:47:00 +00:00
parent 990960850f
commit 4671211ce3
3 changed files with 206 additions and 98 deletions

View File

@@ -50,11 +50,11 @@ extension String {
@public func predecessor() -> IndexType {
var i = _position
let codeUnit = self._base[--i]
// FIXME: consider adding:
// assert(!(codeUnit >= 0xD800 && codeUnit <= 0xDBFF), "unpaired surrogates are ill-formed")
if codeUnit >= 0xDC00 && codeUnit <= 0xDFFF {
--i
let codeUnit = _base[--i]
if _slowPath((codeUnit >> 10) == 0b1101_11) {
if i != 0 && (_base[i - 1] >> 10) == 0b1101_10 {
--i
}
}
return IndexType(i, _base)
}
@@ -80,15 +80,10 @@ extension String {
case .EmptyInput:
_fatalError("can not subscript using an endIndex")
case .Error:
_fatalError("unpaired surrogates are ill-formed in UTF-16")
return UnicodeScalar(0xfffd)
}
}
func __slice__(start: IndexType, end: IndexType) -> UnicodeScalarView {
return UnicodeScalarView(_base[start._position..<end._position])
}
@public subscript(r: Range<IndexType>) -> UnicodeScalarView {
return UnicodeScalarView(
_base[r.startIndex._position..<r.endIndex._position])