@@ -41,7 +41,7 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
41
41
guard aCoder. allowsKeyedCoding else {
42
42
preconditionFailure ( " Unkeyed coding is unsupported. " )
43
43
}
44
- for idx in 0 ..< self . count {
44
+ for idx in _indices {
45
45
aCoder. encode ( _SwiftValue. store ( self . object ( at: idx) ) , forKey: " NS.object. \( idx) " )
46
46
}
47
47
}
@@ -67,6 +67,7 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
67
67
}
68
68
69
69
open func object( at idx: Int ) -> Any {
70
+ _validateSubscript ( idx)
70
71
return _SwiftValue. fetch ( nonOptional: _orderedStorage [ idx] )
71
72
}
72
73
@@ -120,11 +121,26 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
120
121
if type ( of: self ) === NSOrderedSet . self || type ( of: self ) === NSMutableOrderedSet . self {
121
122
return _orderedStorage. map { _SwiftValue. fetch ( nonOptional: $0) }
122
123
} else {
123
- return ( 0 ..< count ) . map { idx in
124
+ return _indices . map { idx in
124
125
return self [ idx]
125
126
}
126
127
}
127
128
}
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
+ }
128
144
}
129
145
130
146
extension NSOrderedSet : Sequence {
@@ -148,9 +164,6 @@ extension NSOrderedSet {
148
164
open func objects( at indexes: IndexSet ) -> [ Any ] {
149
165
var entries = [ Any] ( )
150
166
for idx in indexes {
151
- guard idx < count && idx >= 0 else {
152
- fatalError ( " \( self ) : Index out of bounds " )
153
- }
154
167
entries. append ( object ( at: idx) )
155
168
}
156
169
return entries
@@ -177,7 +190,7 @@ extension NSOrderedSet {
177
190
return false
178
191
}
179
192
180
- for idx in 0 ..< count {
193
+ for idx in _indices {
181
194
if let value1 = object ( at: idx) as? AnyHashable ,
182
195
let value2 = other. object ( at: idx) as? AnyHashable {
183
196
if value1 != value2 {
@@ -338,9 +351,7 @@ extension NSOrderedSet {
338
351
open class NSMutableOrderedSet : NSOrderedSet {
339
352
340
353
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)
344
355
345
356
let value = _SwiftValue. store ( object)
346
357
@@ -353,15 +364,12 @@ open class NSMutableOrderedSet : NSOrderedSet {
353
364
}
354
365
355
366
open func removeObject( at idx: Int ) {
367
+ _validateSubscript ( idx)
356
368
_storage. remove ( _orderedStorage [ idx] )
357
369
_orderedStorage. remove ( at: idx)
358
370
}
359
371
360
372
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
-
365
373
let value = _SwiftValue. store ( obj)
366
374
let objectToReplace = _SwiftValue. store ( object ( at: idx) )
367
375
_orderedStorage [ idx] = value
@@ -420,10 +428,6 @@ extension NSMutableOrderedSet {
420
428
}
421
429
422
430
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
-
427
431
let object1 = self . object ( at: idx1)
428
432
let object2 = self . object ( at: idx2)
429
433
_orderedStorage [ idx1] = _SwiftValue. store ( object2)
@@ -451,6 +455,7 @@ extension NSMutableOrderedSet {
451
455
}
452
456
453
457
open func setObject( _ obj: Any , at idx: Int ) {
458
+ _validateIndex ( idx)
454
459
let object = _SwiftValue. store ( obj)
455
460
_storage. insert ( object)
456
461
if idx == _orderedStorage. count {
0 commit comments