2121import SwiftShims
2222
2323extension 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-
3124 public typealias SubSequence = Substring
32-
3325 public typealias Element = Character
3426
3527 /// The position of the first character in a nonempty string.
@@ -95,22 +87,22 @@ extension String: BidirectionalCollection {
9587 /// print(s[i])
9688 /// // Prints "t"
9789 ///
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.
10092 ///
10193 /// - Parameters:
10294 /// - 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 `.
110102 @inlinable @inline ( __always)
111- public func index( _ i: Index , offsetBy n : IndexDistance ) -> Index {
103+ public func index( _ i: Index , offsetBy distance : Int ) -> Index {
112104 // TODO: known-ASCII and single-scalar-grapheme fast path, etc.
113- return _index ( i, offsetBy: n )
105+ return _index ( i, offsetBy: distance )
114106 }
115107
116108 /// Returns an index that is the specified distance from the given index,
@@ -135,27 +127,28 @@ extension String: BidirectionalCollection {
135127 /// print(j)
136128 /// // Prints "nil"
137129 ///
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
140132 /// beyond those bounds.
141133 ///
142134 /// - Parameters:
143135 /// - 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`.
153146 @inlinable @inline ( __always)
154147 public func index(
155- _ i: Index , offsetBy n : IndexDistance , limitedBy limit: Index
148+ _ i: Index , offsetBy distance : Int , limitedBy limit: Index
156149 ) -> Index ? {
157150 // 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)
159152 }
160153
161154 /// Returns the distance between two indices.
@@ -168,7 +161,7 @@ extension String: BidirectionalCollection {
168161 ///
169162 /// - Complexity: O(*n*), where *n* is the resulting distance.
170163 @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 {
172165 // TODO: known-ASCII and single-scalar-grapheme fast path, etc.
173166 return _distance ( from: _guts. scalarAlign ( start) , to: _guts. scalarAlign ( end) )
174167 }
0 commit comments