21
21
import SwiftShims
22
22
23
23
extension String : BidirectionalCollection {
24
- /// A type that represents the number of steps between two `String.Index`
25
- /// values, where one value is reachable from the other.
26
- ///
27
- /// In Swift, *reachability* refers to the ability to produce one value from
28
- /// the other through zero or more applications of `index(after:)`.
29
- public typealias IndexDistance = Int
30
-
31
24
public typealias SubSequence = Substring
32
-
33
25
public typealias Element = Character
34
26
35
27
/// The position of the first character in a nonempty string.
@@ -95,22 +87,22 @@ extension String: BidirectionalCollection {
95
87
/// print(s[i])
96
88
/// // Prints "t"
97
89
///
98
- /// The value passed as `n ` must not offset `i` beyond the bounds of the
99
- /// collection.
90
+ /// The value passed as `distance ` must not offset `i` beyond the bounds of
91
+ /// the collection.
100
92
///
101
93
/// - Parameters:
102
94
/// - i: A valid index of the collection.
103
- /// - n : The distance to offset `i`.
104
- /// - Returns: An index offset by `n ` from the index `i`. If `n` is positive,
105
- /// this is the same value as the result of `n` calls to `index(after:)`.
106
- /// If `n ` is negative, this is the same value as the result of `-n` calls
107
- /// to `index(before:)`.
108
- ///
109
- /// - Complexity: O(*n*), where *n* is the absolute value of `n `.
95
+ /// - distance : The distance to offset `i`.
96
+ /// - Returns: An index offset by `distance ` from the index `i`. If
97
+ /// `distance` is positive, this is the same value as the result of
98
+ /// `distance` calls to `index(after:)`. If `distance ` is negative, this
99
+ /// is the same value as the result of `abs(distance)` calls to
100
+ /// `index(before:)`.
101
+ /// - Complexity: O(*n*), where *n* is the absolute value of `distance `.
110
102
@inlinable @inline ( __always)
111
- public func index( _ i: Index , offsetBy n : IndexDistance ) -> Index {
103
+ public func index( _ i: Index , offsetBy distance : Int ) -> Index {
112
104
// TODO: known-ASCII and single-scalar-grapheme fast path, etc.
113
- return _index ( i, offsetBy: n )
105
+ return _index ( i, offsetBy: distance )
114
106
}
115
107
116
108
/// Returns an index that is the specified distance from the given index,
@@ -135,27 +127,28 @@ extension String: BidirectionalCollection {
135
127
/// print(j)
136
128
/// // Prints "nil"
137
129
///
138
- /// The value passed as `n ` must not offset `i` beyond the bounds of the
139
- /// collection, unless the index passed as `limit` prevents offsetting
130
+ /// The value passed as `distance ` must not offset `i` beyond the bounds of
131
+ /// the collection, unless the index passed as `limit` prevents offsetting
140
132
/// beyond those bounds.
141
133
///
142
134
/// - Parameters:
143
135
/// - i: A valid index of the collection.
144
- /// - n: The distance to offset `i`.
145
- /// - limit: A valid index of the collection to use as a limit. If `n > 0`,
146
- /// a limit that is less than `i` has no effect. Likewise, if `n < 0`, a
147
- /// limit that is greater than `i` has no effect.
148
- /// - Returns: An index offset by `n` from the index `i`, unless that index
149
- /// would be beyond `limit` in the direction of movement. In that case,
150
- /// the method returns `nil`.
151
- ///
152
- /// - Complexity: O(*n*), where *n* is the absolute value of `n`.
136
+ /// - distance: The distance to offset `i`.
137
+ /// - limit: A valid index of the collection to use as a limit. If
138
+ /// `distance > 0`, a limit that is less than `i` has no effect.
139
+ /// Likewise, if `distance < 0`, a limit that is greater than `i` has no
140
+ /// effect.
141
+ /// - Returns: An index offset by `distance` from the index `i`, unless that
142
+ /// index would be beyond `limit` in the direction of movement. In that
143
+ /// case, the method returns `nil`.
144
+ ///
145
+ /// - Complexity: O(*n*), where *n* is the absolute value of `distance`.
153
146
@inlinable @inline ( __always)
154
147
public func index(
155
- _ i: Index , offsetBy n : IndexDistance , limitedBy limit: Index
148
+ _ i: Index , offsetBy distance : Int , limitedBy limit: Index
156
149
) -> Index ? {
157
150
// TODO: known-ASCII and single-scalar-grapheme fast path, etc.
158
- return _index ( i, offsetBy: n , limitedBy: limit)
151
+ return _index ( i, offsetBy: distance , limitedBy: limit)
159
152
}
160
153
161
154
/// Returns the distance between two indices.
@@ -168,7 +161,7 @@ extension String: BidirectionalCollection {
168
161
///
169
162
/// - Complexity: O(*n*), where *n* is the resulting distance.
170
163
@inlinable @inline ( __always)
171
- public func distance( from start: Index , to end: Index ) -> IndexDistance {
164
+ public func distance( from start: Index , to end: Index ) -> Int {
172
165
// TODO: known-ASCII and single-scalar-grapheme fast path, etc.
173
166
return _distance ( from: _guts. scalarAlign ( start) , to: _guts. scalarAlign ( end) )
174
167
}
0 commit comments