stdlib/NSString APIs on String: add more tests and fix a crash in

_countFormatSpecifiers() that was triggered by non-BMP characters in the format
string


Swift SVN r21014
This commit is contained in:
Dmitri Hrybenko
2014-08-04 15:37:58 +00:00
parent ef6748571a
commit a75c7a427a
5 changed files with 42 additions and 22 deletions

View File

@@ -35,21 +35,27 @@ func _toNSRange(r: Range<String.Index>) -> NSRange {
}
func _countFormatSpecifiers(a: String) -> Int {
var lastChar = _asUTF16CodeUnit(".") // anything other than % would work here
// The implementation takes advantage of the fact that internal
// representation of String is UTF-16. Because we only care about the ASCII
// percent character, we don't need to decode UTF-16.
let percentUTF16 = UTF16.CodeUnit(("%" as UnicodeScalar).value)
let notPercentUTF16: UTF16.CodeUnit = 0
var lastChar = notPercentUTF16 // anything other than % would work here
var count = 0
for c in a.unicodeScalars {
if lastChar == _asUTF16CodeUnit("%") {
if c == "%" {
for c in a.utf16 {
if lastChar == percentUTF16 {
if c == percentUTF16 {
// a "%" following this one should not be taken as literal
lastChar = _asUTF16CodeUnit(".")
lastChar = notPercentUTF16
}
else {
++count
lastChar = _asUTF16CodeUnit(c)
lastChar = c
}
} else {
lastChar = _asUTF16CodeUnit(c)
lastChar = c
}
}
return count