Skip to content

Commit 29059b5

Browse files
committed
Consolidates index checking in NSOrderedSet
Consolidates several index checks into object(at:) and adds a few index checks where it makes sense.
1 parent f70c4ec commit 29059b5

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

Foundation/NSOrderedSet.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
4141
guard aCoder.allowsKeyedCoding else {
4242
preconditionFailure("Unkeyed coding is unsupported.")
4343
}
44-
for idx in 0..<self.count {
44+
for idx in _indices {
4545
aCoder.encode(_SwiftValue.store(self.object(at: idx)), forKey:"NS.object.\(idx)")
4646
}
4747
}
@@ -67,6 +67,7 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
6767
}
6868

6969
open func object(at idx: Int) -> Any {
70+
_validateSubscript(idx)
7071
return _SwiftValue.fetch(nonOptional: _orderedStorage[idx])
7172
}
7273

@@ -120,11 +121,26 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
120121
if type(of: self) === NSOrderedSet.self || type(of: self) === NSMutableOrderedSet.self {
121122
return _orderedStorage.map { _SwiftValue.fetch(nonOptional: $0) }
122123
} else {
123-
return (0..<count).map { idx in
124+
return _indices.map { idx in
124125
return self[idx]
125126
}
126127
}
127128
}
129+
130+
/// The range of indices that are valid for subscripting the ordered set.
131+
internal var _indices: Range<Int> {
132+
return 0..<count
133+
}
134+
135+
/// Checks that an index is valid: 0 ≤ `index` ≤ `count`.
136+
internal func _validateIndex(_ index: Int, file: StaticString = #file, line: UInt = #line) {
137+
precondition(index <= count && index >= 0, "\(self): Index out of bounds", file: file, line: line)
138+
}
139+
140+
/// Checks that an index is valid for subscripting: 0 ≤ `index` < `count`.
141+
internal func _validateSubscript(_ index: Int, file: StaticString = #file, line: UInt = #line) {
142+
precondition(_indices.contains(index), "\(self): Index out of bounds", file: file, line: line)
143+
}
128144
}
129145

130146
extension NSOrderedSet : Sequence {
@@ -148,9 +164,6 @@ extension NSOrderedSet {
148164
open func objects(at indexes: IndexSet) -> [Any] {
149165
var entries = [Any]()
150166
for idx in indexes {
151-
guard idx < count && idx >= 0 else {
152-
fatalError("\(self): Index out of bounds")
153-
}
154167
entries.append(object(at: idx))
155168
}
156169
return entries
@@ -177,7 +190,7 @@ extension NSOrderedSet {
177190
return false
178191
}
179192

180-
for idx in 0..<count {
193+
for idx in _indices {
181194
if let value1 = object(at: idx) as? AnyHashable,
182195
let value2 = other.object(at: idx) as? AnyHashable {
183196
if value1 != value2 {
@@ -338,9 +351,7 @@ extension NSOrderedSet {
338351
open class NSMutableOrderedSet : NSOrderedSet {
339352

340353
open func insert(_ object: Any, at idx: Int) {
341-
guard idx <= count && idx >= 0 else {
342-
fatalError("\(self): Index out of bounds")
343-
}
354+
_validateIndex(idx)
344355

345356
let value = _SwiftValue.store(object)
346357

@@ -353,15 +364,12 @@ open class NSMutableOrderedSet : NSOrderedSet {
353364
}
354365

355366
open func removeObject(at idx: Int) {
367+
_validateSubscript(idx)
356368
_storage.remove(_orderedStorage[idx])
357369
_orderedStorage.remove(at: idx)
358370
}
359371

360372
open func replaceObject(at idx: Int, with obj: Any) {
361-
guard idx < count && idx >= 0 else {
362-
fatalError("\(self): Index out of bounds")
363-
}
364-
365373
let value = _SwiftValue.store(obj)
366374
let objectToReplace = _SwiftValue.store(object(at: idx))
367375
_orderedStorage[idx] = value
@@ -420,10 +428,6 @@ extension NSMutableOrderedSet {
420428
}
421429

422430
open func exchangeObject(at idx1: Int, withObjectAt idx2: Int) {
423-
guard idx1 < count && idx1 >= 0 && idx2 < count && idx2 >= 0 else {
424-
fatalError("\(self): Index out of bounds")
425-
}
426-
427431
let object1 = self.object(at: idx1)
428432
let object2 = self.object(at: idx2)
429433
_orderedStorage[idx1] = _SwiftValue.store(object2)
@@ -451,6 +455,7 @@ extension NSMutableOrderedSet {
451455
}
452456

453457
open func setObject(_ obj: Any, at idx: Int) {
458+
_validateIndex(idx)
454459
let object = _SwiftValue.store(obj)
455460
_storage.insert(object)
456461
if idx == _orderedStorage.count {

0 commit comments

Comments
 (0)