Skip to content

Commit e0ad608

Browse files
authored
Merge pull request #1130 from apple/swift-PR-11088
2 parents 1d52f65 + 8fb27f6 commit e0ad608

File tree

1 file changed

+1
-43
lines changed

1 file changed

+1
-43
lines changed

Foundation/NSString.swift

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -346,49 +346,7 @@ extension NSString {
346346
let start = _storage.utf16.startIndex
347347
let min = start.advanced(by: range.location)
348348
let max = start.advanced(by: range.location + range.length)
349-
if let substr = String(_storage.utf16[min..<max]) {
350-
return substr
351-
}
352-
//If we come here, then the range has created unpaired surrogates on either end.
353-
//An unpaired surrogate is replaced by OXFFFD - the Unicode Replacement Character.
354-
//The CRLF ("\r\n") sequence is also treated like a surrogate pair, but its constinuent
355-
//characters "\r" and "\n" can exist outside the pair!
356-
357-
let replacementCharacter = String(describing: UnicodeScalar(0xFFFD)!)
358-
let CR: UInt16 = 13 //carriage return
359-
let LF: UInt16 = 10 //new line
360-
361-
//make sure the range is of non-zero length
362-
guard range.length > 0 else { return "" }
363-
364-
//if the range is pointing to a single unpaired surrogate
365-
if range.length == 1 {
366-
switch _storage.utf16[min] {
367-
case CR: return "\r"
368-
case LF: return "\n"
369-
default: return replacementCharacter
370-
}
371-
}
372-
373-
//set the prefix and suffix characters
374-
let prefix = _storage.utf16[min] == LF ? "\n" : replacementCharacter
375-
let suffix = _storage.utf16[max.advanced(by: -1)] == CR ? "\r" : replacementCharacter
376-
377-
//if the range breaks a surrogate pair at the beginning of the string
378-
if let substrSuffix = String(_storage.utf16[min.advanced(by: 1)..<max]) {
379-
return prefix + substrSuffix
380-
}
381-
382-
//if the range breaks a surrogate pair at the end of the string
383-
if let substrPrefix = String(_storage.utf16[min..<max.advanced(by: -1)]) {
384-
return substrPrefix + suffix
385-
}
386-
387-
//the range probably breaks surrogate pairs at both the ends
388-
guard min.advanced(by: 1) <= max.advanced(by: -1) else { return prefix + suffix }
389-
390-
let substr = String(_storage.utf16[min.advanced(by: 1)..<max.advanced(by: -1)])!
391-
return prefix + substr + suffix
349+
return String(decoding: _storage.utf16[min..<max], as: UTF16.self)
392350
} else {
393351
let buff = UnsafeMutablePointer<unichar>.allocate(capacity: range.length)
394352
getCharacters(buff, range: range)

0 commit comments

Comments
 (0)