Skip to content

Commit 62abc88

Browse files
committed
Untested speedup for NSString -getCharacters:range:
1 parent 9aabdda commit 62abc88

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Foundation/NSString.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,12 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
343343

344344
extension NSString {
345345
public func getCharacters(_ buffer: UnsafeMutablePointer<unichar>, range: NSRange) {
346-
for idx in 0..<range.length {
347-
buffer[idx] = character(at: idx + range.location)
346+
let utf16 = _storage.utf16
347+
let start = utf16.startIndex
348+
let min = utf16.index(start, offsetBy: range.location) //NSNotFound handling?
349+
let max = utf16.index(min, offsetBy: range.length)
350+
for char in utf16[min..<max] {
351+
buffer[idx] = char
348352
}
349353
}
350354

@@ -368,7 +372,7 @@ extension NSString {
368372
if type(of: self) == NSString.self || type(of: self) == NSMutableString.self {
369373
let start = _storage.utf16.startIndex
370374
let min = _storage.utf16.index(start, offsetBy: range.location)
371-
let max = _storage.utf16.index(start, offsetBy: range.location + range.length)
375+
let max = _storage.utf16.index(min, offsetBy: range.length)
372376
return String(decoding: _storage.utf16[min..<max], as: UTF16.self)
373377
} else {
374378
let buff = UnsafeMutablePointer<unichar>.allocate(capacity: range.length)

0 commit comments

Comments
 (0)