@@ -62,6 +62,116 @@ where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
62
62
/// `startIndex`.
63
63
func formIndex( before i: inout Index )
64
64
65
+ /// Returns the position immediately after the given index.
66
+ ///
67
+ /// The successor of an index must be well defined. For an index `i` into a
68
+ /// collection `c`, calling `c.index(after: i)` returns the same index every
69
+ /// time.
70
+ ///
71
+ /// - Parameter i: A valid index of the collection. `i` must be less than
72
+ /// `endIndex`.
73
+ /// - Returns: The index value immediately after `i`.
74
+ func index( after i: Index ) -> Index
75
+
76
+ /// Replaces the given index with its successor.
77
+ ///
78
+ /// - Parameter i: A valid index of the collection. `i` must be less than
79
+ /// `endIndex`.
80
+ func formIndex( after i: inout Index )
81
+
82
+ /// Returns an index that is the specified distance from the given index.
83
+ ///
84
+ /// The following example obtains an index advanced four positions from a
85
+ /// string's starting index and then prints the character at that position.
86
+ ///
87
+ /// let s = "Swift"
88
+ /// let i = s.index(s.startIndex, offsetBy: 4)
89
+ /// print(s[i])
90
+ /// // Prints "t"
91
+ ///
92
+ /// The value passed as `distance` must not offset `i` beyond the bounds of
93
+ /// the collection.
94
+ ///
95
+ /// - Parameters:
96
+ /// - i: A valid index of the collection.
97
+ /// - distance: The distance to offset `i`. `distance` must not be negative
98
+ /// unless the collection conforms to the `BidirectionalCollection`
99
+ /// protocol.
100
+ /// - Returns: An index offset by `distance` from the index `i`. If
101
+ /// `distance` is positive, this is the same value as the result of
102
+ /// `distance` calls to `index(after:)`. If `distance` is negative, this
103
+ /// is the same value as the result of `abs(distance)` calls to
104
+ /// `index(before:)`.
105
+ ///
106
+ /// - Complexity: O(1) if the collection conforms to
107
+ /// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the absolute
108
+ /// value of `distance`.
109
+ func index( _ i: Index , offsetBy distance: Int ) -> Index
110
+
111
+ /// Returns an index that is the specified distance from the given index,
112
+ /// unless that distance is beyond a given limiting index.
113
+ ///
114
+ /// The following example obtains an index advanced four positions from a
115
+ /// string's starting index and then prints the character at that position.
116
+ /// The operation doesn't require going beyond the limiting `s.endIndex`
117
+ /// value, so it succeeds.
118
+ ///
119
+ /// let s = "Swift"
120
+ /// if let i = s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex) {
121
+ /// print(s[i])
122
+ /// }
123
+ /// // Prints "t"
124
+ ///
125
+ /// The next example attempts to retrieve an index six positions from
126
+ /// `s.startIndex` but fails, because that distance is beyond the index
127
+ /// passed as `limit`.
128
+ ///
129
+ /// let j = s.index(s.startIndex, offsetBy: 6, limitedBy: s.endIndex)
130
+ /// print(j)
131
+ /// // Prints "nil"
132
+ ///
133
+ /// The value passed as `distance` must not offset `i` beyond the bounds of
134
+ /// the collection, unless the index passed as `limit` prevents offsetting
135
+ /// beyond those bounds.
136
+ ///
137
+ /// - Parameters:
138
+ /// - i: A valid index of the collection.
139
+ /// - distance: The distance to offset `i`. `distance` must not be negative
140
+ /// unless the collection conforms to the `BidirectionalCollection`
141
+ /// protocol.
142
+ /// - limit: A valid index of the collection to use as a limit. If
143
+ /// `distance > 0`, a limit that is less than `i` has no effect.
144
+ /// Likewise, if `distance < 0`, a limit that is greater than `i` has no
145
+ /// effect.
146
+ /// - Returns: An index offset by `distance` from the index `i`, unless that
147
+ /// index would be beyond `limit` in the direction of movement. In that
148
+ /// case, the method returns `nil`.
149
+ ///
150
+ /// - Complexity: O(1) if the collection conforms to
151
+ /// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the absolute
152
+ /// value of `distance`.
153
+ func index(
154
+ _ i: Index , offsetBy distance: Int , limitedBy limit: Index
155
+ ) -> Index ?
156
+
157
+ /// Returns the distance between two indices.
158
+ ///
159
+ /// Unless the collection conforms to the `BidirectionalCollection` protocol,
160
+ /// `start` must be less than or equal to `end`.
161
+ ///
162
+ /// - Parameters:
163
+ /// - start: A valid index of the collection.
164
+ /// - end: Another valid index of the collection. If `end` is equal to
165
+ /// `start`, the result is zero.
166
+ /// - Returns: The distance between `start` and `end`. The result can be
167
+ /// negative only if the collection conforms to the
168
+ /// `BidirectionalCollection` protocol.
169
+ ///
170
+ /// - Complexity: O(1) if the collection conforms to
171
+ /// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the
172
+ /// resulting distance.
173
+ func distance( from start: Index , to end: Index ) -> Int
174
+
65
175
/// The indices that are valid for subscripting the collection, in ascending
66
176
/// order.
67
177
///
@@ -257,9 +367,7 @@ extension BidirectionalCollection where SubSequence == Self {
257
367
_precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
258
368
_precondition ( count >= k,
259
369
" Can't remove more items from a collection than it contains " )
260
- // FIXME: using non-_'d `index` incorrectly calls the Collection one for
261
- // conditional conformances to BidirectionalCollections.
262
- self = self [ startIndex..< _index ( endIndex, offsetBy: - k) ]
370
+ self = self [ startIndex..< index ( endIndex, offsetBy: - k) ]
263
371
}
264
372
}
265
373
@@ -287,9 +395,7 @@ extension BidirectionalCollection {
287
395
public func dropLast( _ k: Int ) -> SubSequence {
288
396
_precondition (
289
397
k >= 0 , " Can't drop a negative number of elements from a collection " )
290
- // FIXME: using non-_'d `index` incorrectly calls the Collection one for
291
- // conditional conformances to BidirectionalCollections.
292
- let end = _index (
398
+ let end = index (
293
399
endIndex,
294
400
offsetBy: - k,
295
401
limitedBy: startIndex) ?? startIndex
@@ -321,9 +427,7 @@ extension BidirectionalCollection {
321
427
_precondition (
322
428
maxLength >= 0 ,
323
429
" Can't take a suffix of negative length from a collection " )
324
- // FIXME: using non-_'d `index` incorrectly calls the Collection one for
325
- // conditional conformances to BidirectionalCollections.
326
- let start = _index (
430
+ let start = index (
327
431
endIndex,
328
432
offsetBy: - maxLength,
329
433
limitedBy: startIndex) ?? startIndex
0 commit comments