Skip to content

Commit 9a875b2

Browse files
authored
Merge pull request swiftlang#1360 from ikesyo/nsrange-init
[gardening] Use `NSRange.init` over `NSMakeRange`
2 parents 0765871 + d8250b2 commit 9a875b2

28 files changed

+305
-305
lines changed

Foundation/CharacterSet.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
private func _utfRangeToNSRange(_ inRange : Range<UnicodeScalar>) -> NSRange {
14-
return NSMakeRange(Int(inRange.lowerBound.value), Int(inRange.upperBound.value - inRange.lowerBound.value))
14+
return NSRange(location: Int(inRange.lowerBound.value), length: Int(inRange.upperBound.value - inRange.lowerBound.value))
1515
}
1616

1717
private func _utfRangeToNSRange(_ inRange : ClosedRange<UnicodeScalar>) -> NSRange {
18-
return NSMakeRange(Int(inRange.lowerBound.value), Int(inRange.upperBound.value - inRange.lowerBound.value + 1))
18+
return NSRange(location: Int(inRange.lowerBound.value), length: Int(inRange.upperBound.value - inRange.lowerBound.value + 1))
1919
}
2020

2121
internal final class _SwiftNSCharacterSet : NSCharacterSet, _SwiftNativeFoundationType {
@@ -379,7 +379,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
379379
/// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`.
380380
@discardableResult
381381
public mutating func insert(_ character: UnicodeScalar) -> (inserted: Bool, memberAfterInsert: UnicodeScalar) {
382-
let nsRange = NSMakeRange(Int(character.value), 1)
382+
let nsRange = NSRange(location: Int(character.value), length: 1)
383383
_applyUnmanagedMutation {
384384
$0.addCharacters(in: nsRange)
385385
}
@@ -392,7 +392,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
392392
/// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`.
393393
@discardableResult
394394
public mutating func update(with character: UnicodeScalar) -> UnicodeScalar? {
395-
let nsRange = NSMakeRange(Int(character.value), 1)
395+
let nsRange = NSRange(location: Int(character.value), length: 1)
396396
_applyUnmanagedMutation {
397397
$0.addCharacters(in: nsRange)
398398
}
@@ -408,7 +408,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
408408
public mutating func remove(_ character: UnicodeScalar) -> UnicodeScalar? {
409409
// TODO: Add method to NSCharacterSet to do this in one call
410410
let result : UnicodeScalar? = contains(character) ? character : nil
411-
let r = NSMakeRange(Int(character.value), 1)
411+
let r = NSRange(location: Int(character.value), length: 1)
412412
_applyUnmanagedMutation {
413413
$0.removeCharacters(in: r)
414414
}

Foundation/Data.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
13811381

13821382
guard !copyRange.isEmpty else { return 0 }
13831383

1384-
let nsRange = NSMakeRange(copyRange.lowerBound, copyRange.upperBound - copyRange.lowerBound)
1384+
let nsRange = NSRange(location: copyRange.lowerBound, length: copyRange.upperBound - copyRange.lowerBound)
13851385
_copyBytesHelper(to: buffer.baseAddress!, from: nsRange)
13861386
return copyRange.count
13871387
}
@@ -1437,9 +1437,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
14371437
let nsRange : NSRange
14381438
if let r = range {
14391439
_validateRange(r)
1440-
nsRange = NSMakeRange(r.lowerBound, r.upperBound - r.lowerBound)
1440+
nsRange = NSRange(location: r.lowerBound, length: r.upperBound - r.lowerBound)
14411441
} else {
1442-
nsRange = NSMakeRange(0, _backing.length)
1442+
nsRange = NSRange(location: 0, length: _backing.length)
14431443
}
14441444
let result = _backing.withInteriorPointerReference(_sliceRange) {
14451445
$0.range(of: dataToFind, options: options, in: nsRange)
@@ -1521,7 +1521,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
15211521
// it is worth noting that the range here may be out of bounds of the Data itself (which triggers a growth)
15221522
precondition(range.lowerBound >= 0, "Ranges must not be negative bounds")
15231523
precondition(range.upperBound >= 0, "Ranges must not be negative bounds")
1524-
let range = NSMakeRange(range.lowerBound, range.upperBound - range.lowerBound)
1524+
let range = NSRange(location: range.lowerBound, length: range.upperBound - range.lowerBound)
15251525
if !isKnownUniquelyReferenced(&_backing) {
15261526
_backing = _backing.mutableCopy(_sliceRange)
15271527
}
@@ -1590,7 +1590,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
15901590
@inline(__always)
15911591
public mutating func replaceSubrange(_ subrange: Range<Index>, with bytes: UnsafeRawPointer, count cnt: Int) {
15921592
_validateRange(subrange)
1593-
let nsRange = NSMakeRange(subrange.lowerBound, subrange.upperBound - subrange.lowerBound)
1593+
let nsRange = NSRange(location: subrange.lowerBound, length: subrange.upperBound - subrange.lowerBound)
15941594
if !isKnownUniquelyReferenced(&_backing) {
15951595
_backing = _backing.mutableCopy(_sliceRange)
15961596
}

Foundation/IndexSet.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ extension IndexSet {
827827
}
828828

829829
private func _toNSRange(_ r: Range<IndexSet.Element>) -> NSRange {
830-
return NSMakeRange(r.lowerBound, r.upperBound - r.lowerBound)
830+
return NSRange(location: r.lowerBound, length: r.upperBound - r.lowerBound)
831831
}
832832

833833
#if DEPLOYMENT_RUNTIME_SWIFT

Foundation/NSArray.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,11 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
537537
}
538538

539539
open func sortedArray(comparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
540-
return sortedArray(from: NSMakeRange(0, count), options: [], usingComparator: cmptr)
540+
return sortedArray(from: NSRange(location: 0, length: count), options: [], usingComparator: cmptr)
541541
}
542542

543543
open func sortedArray(options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
544-
return sortedArray(from: NSMakeRange(0, count), options: opts, usingComparator: cmptr)
544+
return sortedArray(from: NSRange(location: 0, length: count), options: opts, usingComparator: cmptr)
545545
}
546546

547547
open func index(of obj: Any, inSortedRange r: NSRange, options opts: NSBinarySearchingOptions = [], usingComparator cmp: (Any, Any) -> ComparisonResult) -> Int {
@@ -884,20 +884,20 @@ open class NSMutableArray : NSArray {
884884
if type(of: self) === NSMutableArray.self {
885885
_storage = otherArray.map { _SwiftValue.store($0) }
886886
} else {
887-
replaceObjects(in: NSMakeRange(0, count), withObjectsFrom: otherArray)
887+
replaceObjects(in: NSRange(location: 0, length: count), withObjectsFrom: otherArray)
888888
}
889889
}
890890

891891
open func removeObjects(at indexes: IndexSet) {
892892
for range in indexes.rangeView.reversed() {
893-
self.removeObjects(in: NSMakeRange(range.lowerBound, range.upperBound - range.lowerBound))
893+
self.removeObjects(in: NSRange(location: range.lowerBound, length: range.upperBound - range.lowerBound))
894894
}
895895
}
896896

897897
open func replaceObjects(at indexes: IndexSet, with objects: [Any]) {
898898
var objectIndex = 0
899899
for countedRange in indexes.rangeView {
900-
let range = NSMakeRange(countedRange.lowerBound, countedRange.upperBound - countedRange.lowerBound)
900+
let range = NSRange(location: countedRange.lowerBound, length: countedRange.upperBound - countedRange.lowerBound)
901901
let subObjects = objects[objectIndex..<objectIndex + range.length]
902902
self.replaceObjects(in: range, withObjectsFrom: Array(subObjects))
903903
objectIndex += range.length

Foundation/NSCFArray.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ internal func _CFSwiftArrayRemoveAllValues(_ array: AnyObject) {
126126

127127
internal func _CFSwiftArrayReplaceValues(_ array: AnyObject, _ range: CFRange, _ newValues: UnsafeMutablePointer<Unmanaged<AnyObject>>, _ newCount: CFIndex) {
128128
NSUnimplemented()
129-
// (array as! NSMutableArray).replaceObjectsInRange(NSMakeRange(range.location, range.length), withObjectsFrom: newValues.array(newCount))
129+
// (array as! NSMutableArray).replaceObjectsInRange(NSRange(location: range.location, length: range.length), withObjectsFrom: newValues.array(newCount))
130130
}

Foundation/NSCFString.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ internal func _CFSwiftStringGetCharacterAtIndex(_ str: AnyObject, index: CFIndex
144144
}
145145

146146
internal func _CFSwiftStringGetCharacters(_ str: AnyObject, range: CFRange, buffer: UnsafeMutablePointer<UniChar>) {
147-
(str as! NSString).getCharacters(buffer, range: NSMakeRange(range.location, range.length))
147+
(str as! NSString).getCharacters(buffer, range: NSRange(location: range.location, length: range.length))
148148
}
149149

150150
internal func _CFSwiftStringGetBytes(_ str: AnyObject, encoding: CFStringEncoding, range: CFRange, buffer: UnsafeMutablePointer<UInt8>?, maxBufLen: CFIndex, usedBufLen: UnsafeMutablePointer<CFIndex>?) -> CFIndex {
@@ -191,7 +191,7 @@ internal func _CFSwiftStringGetBytes(_ str: AnyObject, encoding: CFStringEncodin
191191
}
192192

193193
internal func _CFSwiftStringCreateWithSubstring(_ str: AnyObject, range: CFRange) -> Unmanaged<AnyObject> {
194-
return Unmanaged<AnyObject>.passRetained((str as! NSString).substring(with: NSMakeRange(range.location, range.length))._nsObject)
194+
return Unmanaged<AnyObject>.passRetained((str as! NSString).substring(with: NSRange(location: range.location, length: range.length))._nsObject)
195195
}
196196

197197

@@ -224,11 +224,11 @@ internal func _CFSwiftStringInsert(_ str: AnyObject, index: CFIndex, inserted: A
224224
}
225225

226226
internal func _CFSwiftStringDelete(_ str: AnyObject, range: CFRange) {
227-
(str as! NSMutableString).deleteCharacters(in: NSMakeRange(range.location, range.length))
227+
(str as! NSMutableString).deleteCharacters(in: NSRange(location: range.location, length: range.length))
228228
}
229229

230230
internal func _CFSwiftStringReplace(_ str: AnyObject, range: CFRange, replacement: AnyObject) {
231-
(str as! NSMutableString).replaceCharacters(in: NSMakeRange(range.location, range.length), with: (replacement as! NSString)._swiftObject)
231+
(str as! NSMutableString).replaceCharacters(in: NSRange(location: range.location, length: range.length), with: (replacement as! NSString)._swiftObject)
232232
}
233233

234234
internal func _CFSwiftStringReplaceAll(_ str: AnyObject, replacement: AnyObject) {

Foundation/NSCalendar.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -386,25 +386,25 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
386386
open func minimumRange(of unit: Unit) -> NSRange {
387387
let r = CFCalendarGetMinimumRangeOfUnit(self._cfObject, unit._cfValue)
388388
if (r.location == kCFNotFound) {
389-
return NSMakeRange(NSNotFound, NSNotFound)
389+
return NSRange(location: NSNotFound, length: NSNotFound)
390390
}
391-
return NSMakeRange(r.location, r.length)
391+
return NSRange(location: r.location, length: r.length)
392392
}
393393

394394
open func maximumRange(of unit: Unit) -> NSRange {
395395
let r = CFCalendarGetMaximumRangeOfUnit(_cfObject, unit._cfValue)
396396
if r.location == kCFNotFound {
397-
return NSMakeRange(NSNotFound, NSNotFound)
397+
return NSRange(location: NSNotFound, length: NSNotFound)
398398
}
399-
return NSMakeRange(r.location, r.length)
399+
return NSRange(location: r.location, length: r.length)
400400
}
401401

402402
open func range(of smaller: Unit, in larger: Unit, for date: Date) -> NSRange {
403403
let r = CFCalendarGetRangeOfUnit(_cfObject, smaller._cfValue, larger._cfValue, date.timeIntervalSinceReferenceDate)
404404
if r.location == kCFNotFound {
405-
return NSMakeRange(NSNotFound, NSNotFound)
405+
return NSRange(location: NSNotFound, length: NSNotFound)
406406
}
407-
return NSMakeRange(r.location, r.length)
407+
return NSRange(location: r.location, length: r.length)
408408
}
409409

410410
open func ordinality(of smaller: Unit, in larger: Unit, for date: Date) -> Int {

Foundation/NSData.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
702702
if (stopPointer.pointee) {
703703
return
704704
}
705-
block(bytes, NSMakeRange(0, length), stopPointer)
705+
block(bytes, NSRange(location: 0, length: length), stopPointer)
706706
}
707707
}
708708

@@ -1046,7 +1046,7 @@ open class NSMutableData : NSData {
10461046
open func setData(_ data: Data) {
10471047
length = data.count
10481048
data.withUnsafeBytes {
1049-
replaceBytes(in: NSMakeRange(0, length), withBytes: $0)
1049+
replaceBytes(in: NSRange(location: 0, length: length), withBytes: $0)
10501050
}
10511051
}
10521052

Foundation/NSIndexSet.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
107107
}
108108

109109
public convenience init(index value: Int) {
110-
self.init(indexesIn: NSMakeRange(value, 1))
110+
self.init(indexesIn: NSRange(location: value, length: 1))
111111
}
112112

113113
open func isEqual(to indexSet: IndexSet) -> Bool {
@@ -448,7 +448,7 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
448448
enumerate(options: [], using: block)
449449
}
450450
open func enumerate(options opts: NSEnumerationOptions = [], using block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
451-
let _ = _enumerateWithOptions(opts, range: NSMakeRange(0, Int.max), paramType: Int.self, returnType: Void.self, block: block)
451+
let _ = _enumerateWithOptions(opts, range: NSRange(location: 0, length: Int.max), paramType: Int.self, returnType: Void.self, block: block)
452452
}
453453
open func enumerate(in range: NSRange, options opts: NSEnumerationOptions = [], using block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
454454
let _ = _enumerateWithOptions(opts, range: range, paramType: Int.self, returnType: Void.self, block: block)
@@ -458,17 +458,17 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
458458
return index(options: [], passingTest: predicate)
459459
}
460460
open func index(options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
461-
return _enumerateWithOptions(opts, range: NSMakeRange(0, Int.max), paramType: Int.self, returnType: Bool.self, block: predicate) ?? NSNotFound
461+
return _enumerateWithOptions(opts, range: NSRange(location: 0, length: Int.max), paramType: Int.self, returnType: Bool.self, block: predicate) ?? NSNotFound
462462
}
463463
open func index(in range: NSRange, options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
464464
return _enumerateWithOptions(opts, range: range, paramType: Int.self, returnType: Bool.self, block: predicate) ?? NSNotFound
465465
}
466466

467467
open func indexes(passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
468-
return indexes(in: NSMakeRange(0, Int.max), options: [], passingTest: predicate)
468+
return indexes(in: NSRange(location: 0, length: Int.max), options: [], passingTest: predicate)
469469
}
470470
open func indexes(options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
471-
return indexes(in: NSMakeRange(0, Int.max), options: opts, passingTest: predicate)
471+
return indexes(in: NSRange(location: 0, length: Int.max), options: opts, passingTest: predicate)
472472
}
473473
open func indexes(in range: NSRange, options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
474474
var result = IndexSet()
@@ -489,7 +489,7 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
489489
enumerateRanges(options: [], using: block)
490490
}
491491
open func enumerateRanges(options opts: NSEnumerationOptions = [], using block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
492-
let _ = _enumerateWithOptions(opts, range: NSMakeRange(0, Int.max), paramType: NSRange.self, returnType: Void.self, block: block)
492+
let _ = _enumerateWithOptions(opts, range: NSRange(location: 0, length: Int.max), paramType: NSRange.self, returnType: Void.self, block: block)
493493
}
494494
open func enumerateRanges(in range: NSRange, options opts: NSEnumerationOptions = [], using block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
495495
let _ = _enumerateWithOptions(opts, range: range, paramType: NSRange.self, returnType: Void.self, block: block)
@@ -553,11 +553,11 @@ open class NSMutableIndexSet : NSIndexSet {
553553
}
554554

555555
open func add(_ value: Int) {
556-
add(in: NSMakeRange(value, 1))
556+
add(in: NSRange(location: value, length: 1))
557557
}
558558

559559
open func remove(_ value: Int) {
560-
remove(in: NSMakeRange(value, 1))
560+
remove(in: NSRange(location: value, length: 1))
561561
}
562562

563563
internal func _insertRange(_ range: NSRange, atIndex index: Int) {
@@ -672,16 +672,16 @@ open class NSMutableIndexSet : NSIndexSet {
672672
// Don't increment rangeIndex
673673
continue
674674
} else {
675-
self._replaceRangeAtIndex(rangeIndex, withRange: NSMakeRange(removeEnd, curEnd - removeEnd))
675+
self._replaceRangeAtIndex(rangeIndex, withRange: NSRange(location: removeEnd, length: curEnd - removeEnd))
676676
return
677677
}
678678
} else if range.location > curRange.location && removeEnd < curEnd {
679-
let firstPiece = NSMakeRange(curRange.location, range.location - curRange.location)
680-
let secondPiece = NSMakeRange(removeEnd, curEnd - removeEnd)
679+
let firstPiece = NSRange(location: curRange.location, length: range.location - curRange.location)
680+
let secondPiece = NSRange(location: removeEnd, length: curEnd - removeEnd)
681681
_replaceRangeAtIndex(rangeIndex, withRange: secondPiece)
682682
_insertRange(firstPiece, atIndex: rangeIndex)
683683
} else if range.location > curRange.location && range.location < curEnd && removeEnd >= curEnd {
684-
_replaceRangeAtIndex(rangeIndex, withRange: NSMakeRange(curRange.location, range.location - curRange.location))
684+
_replaceRangeAtIndex(rangeIndex, withRange: NSRange(location: curRange.location, length: range.location - curRange.location))
685685
}
686686
rangeIndex += 1
687687
}

Foundation/NSOrderedSet.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ extension NSOrderedSet {
287287
}
288288

289289
public convenience init(orderedSet set: NSOrderedSet, copyItems flag: Bool) {
290-
self.init(orderedSet: set, range: NSMakeRange(0, set.count), copyItems: flag)
290+
self.init(orderedSet: set, range: NSRange(location: 0, length: set.count), copyItems: flag)
291291
}
292292

293293
public convenience init(orderedSet set: NSOrderedSet, range: NSRange, copyItems flag: Bool) {
@@ -306,7 +306,7 @@ extension NSOrderedSet {
306306
}
307307

308308
public convenience init(array set: [Any], copyItems flag: Bool) {
309-
self.init(array: set, range: NSMakeRange(0, set.count), copyItems: flag)
309+
self.init(array: set, range: NSRange(location: 0, length: set.count), copyItems: flag)
310310
}
311311

312312
public convenience init(array set: [Any], range: NSRange, copyItems flag: Bool) {
@@ -540,11 +540,11 @@ extension NSMutableOrderedSet {
540540
}
541541

542542
open func sort(comparator cmptr: (Any, Any) -> ComparisonResult) {
543-
sortRange(NSMakeRange(0, count), options: [], usingComparator: cmptr)
543+
sortRange(NSRange(location: 0, length: count), options: [], usingComparator: cmptr)
544544
}
545545

546546
open func sort(options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) {
547-
sortRange(NSMakeRange(0, count), options: opts, usingComparator: cmptr)
547+
sortRange(NSRange(location: 0, length: count), options: opts, usingComparator: cmptr)
548548
}
549549

550550
open func sortRange(_ range: NSRange, options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) {

0 commit comments

Comments
 (0)