Skip to content

Commit 58933d8

Browse files
committed
[stdlib] Rename index(...) methods to firstIndex(...)
A la SE-204.
1 parent 67852ed commit 58933d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+200
-175
lines changed

benchmark/single-source/CSVParsing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func parseQuotedField(_ remainder: inout Substring) throws -> Substring? {
3131
var result: Substring = "" // we accumulate the result
3232

3333
while !remainder.isEmpty {
34-
guard let nextQuoteIndex = remainder.index(of: "\"") else {
34+
guard let nextQuoteIndex = remainder.firstIndex(of: "\"") else {
3535
throw ParseError(message: "Expected a closing \"")
3636
}
3737

benchmark/single-source/RemoveWhere.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension RangeReplaceableCollection {
4646

4747
extension RangeReplaceableCollection where Self: MutableCollection {
4848
mutating func removeWhere_move(where match: (Element) throws -> Bool) rethrows {
49-
guard var i = try index(where: match) else { return }
49+
guard var i = try firstIndex(where: match) else { return }
5050

5151
var j = index(after: i)
5252
while j != endIndex {
@@ -62,7 +62,7 @@ extension RangeReplaceableCollection where Self: MutableCollection {
6262
}
6363

6464
mutating func removeWhere_swap(where match: (Element) throws -> Bool) rethrows {
65-
guard var i = try index(where: match) else { return }
65+
guard var i = try firstIndex(where: match) else { return }
6666

6767
var j = index(after: i)
6868
while j != endIndex {

stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -814,13 +814,13 @@ extension TestSuite {
814814
}
815815

816816
//===------------------------------------------------------------------===//
817-
// index(of:)/index(where:)
817+
// firstIndex(of:)/firstIndex(where:)
818818
//===------------------------------------------------------------------===//
819819

820-
self.test("\(testNamePrefix).index(of:)/semantics") {
820+
self.test("\(testNamePrefix).firstIndex(of:)/semantics") {
821821
for test in findTests {
822822
let c = makeWrappedCollectionWithEquatableElement(test.sequence)
823-
var result = c.index(of: wrapValueIntoEquatable(test.element))
823+
var result = c.firstIndex(of: wrapValueIntoEquatable(test.element))
824824
expectType(
825825
Optional<CollectionWithEquatableElement.Index>.self,
826826
&result)
@@ -834,12 +834,12 @@ extension TestSuite {
834834
}
835835
}
836836

837-
self.test("\(testNamePrefix).index(where:)/semantics") {
837+
self.test("\(testNamePrefix).firstIndex(where:)/semantics") {
838838
for test in findTests {
839839
let closureLifetimeTracker = LifetimeTracked(0)
840840
expectEqual(1, LifetimeTracked.instances)
841841
let c = makeWrappedCollectionWithEquatableElement(test.sequence)
842-
let result = c.index {
842+
let result = c.firstIndex {
843843
(candidate) in
844844
_blackHole(closureLifetimeTracker)
845845
return

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,17 +1935,17 @@ self.test("\(testNamePrefix).forEach/semantics") {
19351935
}
19361936

19371937
//===----------------------------------------------------------------------===//
1938-
// first()
1938+
// first(where:)
19391939
//===----------------------------------------------------------------------===//
19401940

1941-
self.test("\(testNamePrefix).first/semantics") {
1941+
self.test("\(testNamePrefix).first(where:)/semantics") {
19421942
for test in findTests {
19431943
let s = makeWrappedSequenceWithEquatableElement(test.sequence)
19441944
let closureLifetimeTracker = LifetimeTracked(0)
1945-
let found = s.first {
1945+
let found = s.first(where: {
19461946
_blackHole(closureLifetimeTracker)
19471947
return $0 == wrapValueIntoEquatable(test.element)
1948-
}
1948+
})
19491949
expectEqual(
19501950
test.expected == nil ? nil : wrapValueIntoEquatable(test.element),
19511951
found,

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ internal struct _CollectionStateTransition {
333333
transitions = Box<[_CollectionStateTransition]>([])
334334
_CollectionStateTransition._allTransitions[previousState] = transitions
335335
}
336-
if let i = transitions!.value.index(where: { $0._operation == operation }) {
336+
if let i = transitions!.value.firstIndex(where: { $0._operation == operation }) {
337337
self = transitions!.value[i]
338338
return
339339
}

stdlib/public/core/Arrays.swift.gyb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,22 @@ if True:
137137
/// tasked with finding the first two days with absences in the session. To
138138
/// find the indices of the two days in question, follow these steps:
139139
///
140-
/// 1) Call `index(where:)` to find the index of the first element in the
140+
/// 1) Call `firstIndex(where:)` to find the index of the first element in the
141141
/// `absences` array that is greater than zero.
142142
/// 2) Create a slice of the `absences` array starting after the index found in
143143
/// step 1.
144-
/// 3) Call `index(where:)` again, this time on the slice created in step 2.
145-
/// Where in some languages you might pass a starting index into an
144+
/// 3) Call `firstIndex(where:)` again, this time on the slice created in step
145+
/// 2. Where in some languages you might pass a starting index into an
146146
/// `indexOf` method to find the second day, in Swift you perform the same
147147
/// operation on a slice of the original array.
148148
/// 4) Print the results using the indices found in steps 1 and 3 on the
149149
/// original `absences` array.
150150
///
151151
/// Here's an implementation of those steps:
152152
///
153-
/// if let i = absences.index(where: { $0 > 0 }) { // 1
153+
/// if let i = absences.firstIndex(where: { $0 > 0 }) { // 1
154154
/// let absencesAfterFirst = absences[(i + 1)...] // 2
155-
/// if let j = absencesAfterFirst.index(where: { $0 > 0 }) { // 3
155+
/// if let j = absencesAfterFirst.firstIndex(where: { $0 > 0 }) { // 3
156156
/// print("The first day with absences had \(absences[i]).") // 4
157157
/// print("The second day with absences had \(absences[j]).")
158158
/// }
@@ -293,7 +293,7 @@ if True:
293293
/// You can replace an existing element with a new value by assigning the new
294294
/// value to the subscript.
295295
///
296-
/// if let i = students.index(of: "Maxime") {
296+
/// if let i = students.firstIndex(of: "Maxime") {
297297
/// students[i] = "Max"
298298
/// }
299299
/// // ["Ivy", "Jordell", "Liam", "Max", "Shakia"]
@@ -533,7 +533,7 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
533533
/// safe to use with `endIndex`. For example:
534534
///
535535
/// let numbers = [10, 20, 30, 40, 50]
536-
/// if let i = numbers.index(of: 30) {
536+
/// if let i = numbers.firstIndex(of: 30) {
537537
/// print(numbers[i ..< numbers.endIndex])
538538
/// }
539539
/// // Prints "[30, 40, 50]"
@@ -784,7 +784,7 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
784784
/// print(streetsSlice)
785785
/// // Prints "["Channing", "Douglas", "Evarts"]"
786786
///
787-
/// let i = streetsSlice.index(of: "Evarts") // 4
787+
/// let i = streetsSlice.firstIndex(of: "Evarts") // 4
788788
/// print(streets[i!])
789789
/// // Prints "Evarts"
790790
///

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
119119
/// print(streetsSlice)
120120
/// // Prints "["Channing", "Douglas", "Evarts"]"
121121
///
122-
/// let index = streetsSlice.index(of: "Evarts") // 4
122+
/// let index = streetsSlice.firstIndex(of: "Evarts") // 4
123123
/// print(streets[index!])
124124
/// // Prints "Evarts"
125125
///

stdlib/public/core/Collection.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ extension IndexingIterator: IteratorProtocol, Sequence {
161161
/// that position.
162162
///
163163
/// let text = "Buffalo buffalo buffalo buffalo."
164-
/// if let firstSpace = text.index(of: " ") {
164+
/// if let firstSpace = text.firstIndex(of: " ") {
165165
/// print(text[..<firstSpace])
166166
/// }
167167
/// // Prints "Buffalo"
@@ -226,7 +226,7 @@ extension IndexingIterator: IteratorProtocol, Sequence {
226226
/// You can retrieve the same slice using the string's ranged subscript, which
227227
/// takes a range expression.
228228
///
229-
/// if let firstSpace = text.index(of: " ") {
229+
/// if let firstSpace = text.firstIndex(of: " ") {
230230
/// print(text[..<firstSpace]
231231
/// // Prints "Buffalo"
232232
/// }
@@ -373,7 +373,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
373373
/// safe to use with `endIndex`. For example:
374374
///
375375
/// let numbers = [10, 20, 30, 40, 50]
376-
/// if let index = numbers.index(of: 30) {
376+
/// if let index = numbers.firstIndex(of: 30) {
377377
/// print(numbers[index ..< numbers.endIndex])
378378
/// }
379379
/// // Prints "[30, 40, 50]"
@@ -439,7 +439,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
439439
/// original collection. This example searches `streetsSlice` for one of the
440440
/// strings in the slice, and then uses that index in the original array.
441441
///
442-
/// let index = streetsSlice.index(of: "Evarts")! // 4
442+
/// let index = streetsSlice.firstIndex(of: "Evarts")! // 4
443443
/// print(streets[index])
444444
/// // "Evarts"
445445
///
@@ -497,7 +497,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
497497
/// but not including, that index:
498498
///
499499
/// let numbers = [10, 20, 30, 40, 50, 60]
500-
/// if let i = numbers.index(of: 40) {
500+
/// if let i = numbers.firstIndex(of: 40) {
501501
/// print(numbers.prefix(upTo: i))
502502
/// }
503503
/// // Prints "[10, 20, 30]"
@@ -512,7 +512,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
512512
/// half-open range as the collection's subscript. The subscript notation is
513513
/// preferred over `prefix(upTo:)`.
514514
///
515-
/// if let i = numbers.index(of: 40) {
515+
/// if let i = numbers.firstIndex(of: 40) {
516516
/// print(numbers[..<i])
517517
/// }
518518
/// // Prints "[10, 20, 30]"
@@ -532,7 +532,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
532532
/// that index:
533533
///
534534
/// let numbers = [10, 20, 30, 40, 50, 60]
535-
/// if let i = numbers.index(of: 40) {
535+
/// if let i = numbers.firstIndex(of: 40) {
536536
/// print(numbers.suffix(from: i))
537537
/// }
538538
/// // Prints "[40, 50, 60]"
@@ -547,7 +547,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
547547
/// from the index as the collection's subscript. The subscript notation is
548548
/// preferred over `suffix(from:)`.
549549
///
550-
/// if let i = numbers.index(of: 40) {
550+
/// if let i = numbers.firstIndex(of: 40) {
551551
/// print(numbers[i...])
552552
/// }
553553
/// // Prints "[40, 50, 60]"
@@ -568,7 +568,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
568568
/// including, that index:
569569
///
570570
/// let numbers = [10, 20, 30, 40, 50, 60]
571-
/// if let i = numbers.index(of: 40) {
571+
/// if let i = numbers.firstIndex(of: 40) {
572572
/// print(numbers.prefix(through: i))
573573
/// }
574574
/// // Prints "[10, 20, 30, 40]"
@@ -577,7 +577,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
577577
/// closed range as the collection's subscript. The subscript notation is
578578
/// preferred over `prefix(through:)`.
579579
///
580-
/// if let i = numbers.index(of: 40) {
580+
/// if let i = numbers.firstIndex(of: 40) {
581581
/// print(numbers[...i])
582582
/// }
583583
/// // Prints "[10, 20, 30, 40]"
@@ -621,8 +621,9 @@ public protocol Collection: Sequence where SubSequence: Collection {
621621
/// of the collection.
622622
var count: Int { get }
623623

624-
// The following requirement enables dispatching for index(of:) when
624+
// The following requirements enable dispatching for firstIndex(of:) when
625625
// the element type is Equatable.
626+
626627
/// Returns `Optional(Optional(index))` if an element was found
627628
/// or `Optional(nil)` if an element was determined to be missing;
628629
/// otherwise, `nil`.
@@ -1067,7 +1068,7 @@ extension Collection where SubSequence == Slice<Self> {
10671068
/// print(streetsSlice)
10681069
/// // Prints "["Channing", "Douglas", "Evarts"]"
10691070
///
1070-
/// let index = streetsSlice.index(of: "Evarts") // 4
1071+
/// let index = streetsSlice.firstIndex(of: "Evarts") // 4
10711072
/// print(streets[index!])
10721073
/// // Prints "Evarts"
10731074
///
@@ -1180,7 +1181,7 @@ extension Collection {
11801181
}
11811182

11821183
// TODO: swift-3-indexing-model - rename the following to _customIndexOfEquatable(element)?
1183-
/// Customization point for `Collection.index(of:)`.
1184+
/// Customization point for `Collection.firstIndex(of:)`.
11841185
///
11851186
/// Define this method if the collection can find an element in less than
11861187
/// O(*n*) by exploiting collection-specific knowledge.
@@ -1189,7 +1190,7 @@ extension Collection {
11891190
/// `Optional(nil)` if the element was not found, or
11901191
/// `Optional(Optional(index))` if an element was found.
11911192
///
1192-
/// - Complexity: O(`count`).
1193+
/// - Complexity: Hopefully less than O(`count`).
11931194
@inlinable
11941195
public // dispatching
11951196
func _customIndexOfEquatableElement(_: Iterator.Element) -> Index?? {
@@ -1402,7 +1403,7 @@ extension Collection {
14021403
/// but not including, that index:
14031404
///
14041405
/// let numbers = [10, 20, 30, 40, 50, 60]
1405-
/// if let i = numbers.index(of: 40) {
1406+
/// if let i = numbers.firstIndex(of: 40) {
14061407
/// print(numbers.prefix(upTo: i))
14071408
/// }
14081409
/// // Prints "[10, 20, 30]"
@@ -1417,7 +1418,7 @@ extension Collection {
14171418
/// half-open range as the collection's subscript. The subscript notation is
14181419
/// preferred over `prefix(upTo:)`.
14191420
///
1420-
/// if let i = numbers.index(of: 40) {
1421+
/// if let i = numbers.firstIndex(of: 40) {
14211422
/// print(numbers[..<i])
14221423
/// }
14231424
/// // Prints "[10, 20, 30]"
@@ -1440,7 +1441,7 @@ extension Collection {
14401441
/// that index:
14411442
///
14421443
/// let numbers = [10, 20, 30, 40, 50, 60]
1443-
/// if let i = numbers.index(of: 40) {
1444+
/// if let i = numbers.firstIndex(of: 40) {
14441445
/// print(numbers.suffix(from: i))
14451446
/// }
14461447
/// // Prints "[40, 50, 60]"
@@ -1455,7 +1456,7 @@ extension Collection {
14551456
/// from the index as the collection's subscript. The subscript notation is
14561457
/// preferred over `suffix(from:)`.
14571458
///
1458-
/// if let i = numbers.index(of: 40) {
1459+
/// if let i = numbers.firstIndex(of: 40) {
14591460
/// print(numbers[i...])
14601461
/// }
14611462
/// // Prints "[40, 50, 60]"
@@ -1479,7 +1480,7 @@ extension Collection {
14791480
/// including, that index:
14801481
///
14811482
/// let numbers = [10, 20, 30, 40, 50, 60]
1482-
/// if let i = numbers.index(of: 40) {
1483+
/// if let i = numbers.firstIndex(of: 40) {
14831484
/// print(numbers.prefix(through: i))
14841485
/// }
14851486
/// // Prints "[10, 20, 30, 40]"
@@ -1488,7 +1489,7 @@ extension Collection {
14881489
/// closed range as the collection's subscript. The subscript notation is
14891490
/// preferred over `prefix(through:)`.
14901491
///
1491-
/// if let i = numbers.index(of: 40) {
1492+
/// if let i = numbers.firstIndex(of: 40) {
14921493
/// print(numbers[...i])
14931494
/// }
14941495
/// // Prints "[10, 20, 30, 40]"

0 commit comments

Comments
 (0)