Skip to content

Commit 626582c

Browse files
committed
[stdlib] Mark String.IndexDistance as deprecated and stop mentioning it in method signatures
`IndexDistance` is an artifact of ancient history — it used to be an associated type of Collection in the Swift 3 era. The Collection requirement got replaced with a deprecated typealias of Int in Swift 4, the same release that re-introduced the Collection conformance for String. However, the typealias declaration for String.IndexDistance has fallen through the cracks. It was accidentally left un-deprecated and it’s still actively mentioned in String API declarations to this day. (These usages leak into the API docs, which can be a source of unnecessary confusion.) Let’s put an end to this and add the missing deprecation attribute, replacing `IndexDistance` usages with `Int`. While we’re here, also bring `String.index(_:offsetBy:)` and `String.index(_:offsetBy:limitedBy:)` in sync with their declarations in Collection by renaming the second argument from `n` to `distance`. (These method declaration changes do get emitted into .swiftinterface files, but they aren’t breaking any client code — the argument names are only relevant to the docs (and the method implementation). They do not affect callers of these functions.)
1 parent ac70bf7 commit 626582c

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

stdlib/public/core/MigrationSupport.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,3 +700,13 @@ public typealias _DefaultCustomPlaygroundQuickLookable = __DefaultCustomPlaygrou
700700
public protocol __DefaultCustomPlaygroundQuickLookable {
701701
var _defaultCustomPlaygroundQuickLook: _PlaygroundQuickLook { get }
702702
}
703+
704+
extension String {
705+
/// A type that represents the number of steps between two `String.Index`
706+
/// values, where one value is reachable from the other.
707+
///
708+
/// In Swift, *reachability* refers to the ability to produce one value from
709+
/// the other through zero or more applications of `index(after:)`.
710+
@available(*, deprecated, message: "All index distances are now of type Int")
711+
public typealias IndexDistance = Int
712+
}

stdlib/public/core/StringCharacterView.swift

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,7 @@
2121
import SwiftShims
2222

2323
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-
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

Comments
 (0)