@@ -285,12 +285,17 @@ public protocol RangeReplaceableCollection: Collection
285
285
/// Customization point for `removeLast()`. Implement this function if you
286
286
/// want to replace the default implementation.
287
287
///
288
+ /// The collection must not be empty.
289
+ ///
288
290
/// - Returns: A non-nil value if the operation was performed.
289
291
mutating func _customRemoveLast( ) -> Element ?
290
292
291
293
/// Customization point for `removeLast(_:)`. Implement this function if you
292
294
/// want to replace the default implementation.
293
295
///
296
+ /// - Parameter n: The number of elements to remove from the collection.
297
+ /// `n` must be greater than or equal to zero and must not exceed the
298
+ /// number of elements in the collection.
294
299
/// - Returns: `true` if the operation was performed.
295
300
mutating func _customRemoveLast( _ n: Int ) -> Bool
296
301
@@ -591,9 +596,10 @@ extension RangeReplaceableCollection {
591
596
public mutating func removeFirst( _ k: Int ) {
592
597
if k == 0 { return }
593
598
_precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
594
- _precondition ( count >= k,
595
- " Can't remove more items from a collection than it has " )
596
- let end = index ( startIndex, offsetBy: k)
599
+ guard let end = index ( startIndex, offsetBy: k, limitedBy: endIndex) else {
600
+ _preconditionFailure (
601
+ " Can't remove more items from a collection than it has " )
602
+ }
597
603
removeSubrange ( startIndex..< end)
598
604
}
599
605
@@ -699,9 +705,11 @@ extension RangeReplaceableCollection where SubSequence == Self {
699
705
public mutating func removeFirst( _ k: Int ) {
700
706
if k == 0 { return }
701
707
_precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
702
- _precondition ( count >= k,
703
- " Can't remove more items from a collection than it contains " )
704
- self = self [ index ( startIndex, offsetBy: k) ..< endIndex]
708
+ guard let idx = index ( startIndex, offsetBy: k, limitedBy: endIndex) else {
709
+ _preconditionFailure (
710
+ " Can't remove more items from a collection than it contains " )
711
+ }
712
+ self = self [ idx..< endIndex]
705
713
}
706
714
}
707
715
@@ -800,7 +808,12 @@ extension RangeReplaceableCollection
800
808
801
809
@inlinable
802
810
public mutating func _customRemoveLast( _ n: Int ) -> Bool {
803
- self = self [ startIndex..< index ( endIndex, offsetBy: numericCast ( - n) ) ]
811
+ guard let end = index ( endIndex, offsetBy: - n, limitedBy: startIndex)
812
+ else {
813
+ _preconditionFailure (
814
+ " Can't remove more items from a collection than it contains " )
815
+ }
816
+ self = self [ startIndex..< end]
804
817
return true
805
818
}
806
819
}
@@ -864,13 +877,17 @@ extension RangeReplaceableCollection where Self: BidirectionalCollection {
864
877
public mutating func removeLast( _ k: Int ) {
865
878
if k == 0 { return }
866
879
_precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
867
- _precondition ( count >= k,
868
- " Can't remove more items from a collection than it contains " )
869
880
if _customRemoveLast ( k) {
870
881
return
871
882
}
872
883
let end = endIndex
873
- removeSubrange ( index ( end, offsetBy: - k) ..< end)
884
+ guard let start = index ( end, offsetBy: - k, limitedBy: startIndex)
885
+ else {
886
+ _preconditionFailure (
887
+ " Can't remove more items from a collection than it contains " )
888
+ }
889
+
890
+ removeSubrange ( start..< end)
874
891
}
875
892
}
876
893
@@ -934,13 +951,16 @@ where Self: BidirectionalCollection, SubSequence == Self {
934
951
public mutating func removeLast( _ k: Int ) {
935
952
if k == 0 { return }
936
953
_precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
937
- _precondition ( count >= k,
938
- " Can't remove more items from a collection than it contains " )
939
954
if _customRemoveLast ( k) {
940
955
return
941
956
}
942
957
let end = endIndex
943
- removeSubrange ( index ( end, offsetBy: - k) ..< end)
958
+ guard let start = index ( end, offsetBy: - k, limitedBy: startIndex)
959
+ else {
960
+ _preconditionFailure (
961
+ " Can't remove more items from a collection than it contains " )
962
+ }
963
+ removeSubrange ( start..< end)
944
964
}
945
965
}
946
966
0 commit comments