Skip to content

[stdlib] Rename index(...) methods to firstIndex(...) #16089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/single-source/CSVParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func parseQuotedField(_ remainder: inout Substring) throws -> Substring? {
var result: Substring = "" // we accumulate the result

while !remainder.isEmpty {
guard let nextQuoteIndex = remainder.index(of: "\"") else {
guard let nextQuoteIndex = remainder.firstIndex(of: "\"") else {
throw ParseError(message: "Expected a closing \"")
}

Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/RemoveWhere.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension RangeReplaceableCollection {

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

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

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

var j = index(after: i)
while j != endIndex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,13 +814,13 @@ extension TestSuite {
}

//===------------------------------------------------------------------===//
// index(of:)/index(where:)
// firstIndex(of:)/firstIndex(where:)
//===------------------------------------------------------------------===//

self.test("\(testNamePrefix).index(of:)/semantics") {
self.test("\(testNamePrefix).firstIndex(of:)/semantics") {
for test in findTests {
let c = makeWrappedCollectionWithEquatableElement(test.sequence)
var result = c.index(of: wrapValueIntoEquatable(test.element))
var result = c.firstIndex(of: wrapValueIntoEquatable(test.element))
expectType(
Optional<CollectionWithEquatableElement.Index>.self,
&result)
Expand All @@ -834,12 +834,12 @@ extension TestSuite {
}
}

self.test("\(testNamePrefix).index(where:)/semantics") {
self.test("\(testNamePrefix).firstIndex(where:)/semantics") {
for test in findTests {
let closureLifetimeTracker = LifetimeTracked(0)
expectEqual(1, LifetimeTracked.instances)
let c = makeWrappedCollectionWithEquatableElement(test.sequence)
let result = c.index {
let result = c.firstIndex {
(candidate) in
_blackHole(closureLifetimeTracker)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1935,17 +1935,17 @@ self.test("\(testNamePrefix).forEach/semantics") {
}

//===----------------------------------------------------------------------===//
// first()
// first(where:)
//===----------------------------------------------------------------------===//

self.test("\(testNamePrefix).first/semantics") {
self.test("\(testNamePrefix).first(where:)/semantics") {
for test in findTests {
let s = makeWrappedSequenceWithEquatableElement(test.sequence)
let closureLifetimeTracker = LifetimeTracked(0)
let found = s.first {
let found = s.first(where: {
_blackHole(closureLifetimeTracker)
return $0 == wrapValueIntoEquatable(test.element)
}
})
expectEqual(
test.expected == nil ? nil : wrapValueIntoEquatable(test.element),
found,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ internal struct _CollectionStateTransition {
transitions = Box<[_CollectionStateTransition]>([])
_CollectionStateTransition._allTransitions[previousState] = transitions
}
if let i = transitions!.value.index(where: { $0._operation == operation }) {
if let i = transitions!.value.firstIndex(where: { $0._operation == operation }) {
self = transitions!.value[i]
return
}
Expand Down
16 changes: 8 additions & 8 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,22 @@ if True:
/// tasked with finding the first two days with absences in the session. To
/// find the indices of the two days in question, follow these steps:
///
/// 1) Call `index(where:)` to find the index of the first element in the
/// 1) Call `firstIndex(where:)` to find the index of the first element in the
/// `absences` array that is greater than zero.
/// 2) Create a slice of the `absences` array starting after the index found in
/// step 1.
/// 3) Call `index(where:)` again, this time on the slice created in step 2.
/// Where in some languages you might pass a starting index into an
/// 3) Call `firstIndex(where:)` again, this time on the slice created in step
/// 2. Where in some languages you might pass a starting index into an
/// `indexOf` method to find the second day, in Swift you perform the same
/// operation on a slice of the original array.
/// 4) Print the results using the indices found in steps 1 and 3 on the
/// original `absences` array.
///
/// Here's an implementation of those steps:
///
/// if let i = absences.index(where: { $0 > 0 }) { // 1
/// if let i = absences.firstIndex(where: { $0 > 0 }) { // 1
/// let absencesAfterFirst = absences[(i + 1)...] // 2
/// if let j = absencesAfterFirst.index(where: { $0 > 0 }) { // 3
/// if let j = absencesAfterFirst.firstIndex(where: { $0 > 0 }) { // 3
/// print("The first day with absences had \(absences[i]).") // 4
/// print("The second day with absences had \(absences[j]).")
/// }
Expand Down Expand Up @@ -293,7 +293,7 @@ if True:
/// You can replace an existing element with a new value by assigning the new
/// value to the subscript.
///
/// if let i = students.index(of: "Maxime") {
/// if let i = students.firstIndex(of: "Maxime") {
/// students[i] = "Max"
/// }
/// // ["Ivy", "Jordell", "Liam", "Max", "Shakia"]
Expand Down Expand Up @@ -533,7 +533,7 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
/// safe to use with `endIndex`. For example:
///
/// let numbers = [10, 20, 30, 40, 50]
/// if let i = numbers.index(of: 30) {
/// if let i = numbers.firstIndex(of: 30) {
/// print(numbers[i ..< numbers.endIndex])
/// }
/// // Prints "[30, 40, 50]"
Expand Down Expand Up @@ -784,7 +784,7 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
/// print(streetsSlice)
/// // Prints "["Channing", "Douglas", "Evarts"]"
///
/// let i = streetsSlice.index(of: "Evarts") // 4
/// let i = streetsSlice.firstIndex(of: "Evarts") // 4
/// print(streets[i!])
/// // Prints "Evarts"
///
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/BidirectionalCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
/// print(streetsSlice)
/// // Prints "["Channing", "Douglas", "Evarts"]"
///
/// let index = streetsSlice.index(of: "Evarts") // 4
/// let index = streetsSlice.firstIndex(of: "Evarts") // 4
/// print(streets[index!])
/// // Prints "Evarts"
///
Expand Down
41 changes: 21 additions & 20 deletions stdlib/public/core/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ extension IndexingIterator: IteratorProtocol, Sequence {
/// that position.
///
/// let text = "Buffalo buffalo buffalo buffalo."
/// if let firstSpace = text.index(of: " ") {
/// if let firstSpace = text.firstIndex(of: " ") {
/// print(text[..<firstSpace])
/// }
/// // Prints "Buffalo"
Expand Down Expand Up @@ -226,7 +226,7 @@ extension IndexingIterator: IteratorProtocol, Sequence {
/// You can retrieve the same slice using the string's ranged subscript, which
/// takes a range expression.
///
/// if let firstSpace = text.index(of: " ") {
/// if let firstSpace = text.firstIndex(of: " ") {
/// print(text[..<firstSpace]
/// // Prints "Buffalo"
/// }
Expand Down Expand Up @@ -373,7 +373,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// safe to use with `endIndex`. For example:
///
/// let numbers = [10, 20, 30, 40, 50]
/// if let index = numbers.index(of: 30) {
/// if let index = numbers.firstIndex(of: 30) {
/// print(numbers[index ..< numbers.endIndex])
/// }
/// // Prints "[30, 40, 50]"
Expand Down Expand Up @@ -439,7 +439,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// original collection. This example searches `streetsSlice` for one of the
/// strings in the slice, and then uses that index in the original array.
///
/// let index = streetsSlice.index(of: "Evarts")! // 4
/// let index = streetsSlice.firstIndex(of: "Evarts")! // 4
/// print(streets[index])
/// // "Evarts"
///
Expand Down Expand Up @@ -497,7 +497,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// but not including, that index:
///
/// let numbers = [10, 20, 30, 40, 50, 60]
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers.prefix(upTo: i))
/// }
/// // Prints "[10, 20, 30]"
Expand All @@ -512,7 +512,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// half-open range as the collection's subscript. The subscript notation is
/// preferred over `prefix(upTo:)`.
///
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers[..<i])
/// }
/// // Prints "[10, 20, 30]"
Expand All @@ -532,7 +532,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// that index:
///
/// let numbers = [10, 20, 30, 40, 50, 60]
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers.suffix(from: i))
/// }
/// // Prints "[40, 50, 60]"
Expand All @@ -547,7 +547,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// from the index as the collection's subscript. The subscript notation is
/// preferred over `suffix(from:)`.
///
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers[i...])
/// }
/// // Prints "[40, 50, 60]"
Expand All @@ -568,7 +568,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// including, that index:
///
/// let numbers = [10, 20, 30, 40, 50, 60]
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers.prefix(through: i))
/// }
/// // Prints "[10, 20, 30, 40]"
Expand All @@ -577,7 +577,7 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// closed range as the collection's subscript. The subscript notation is
/// preferred over `prefix(through:)`.
///
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers[...i])
/// }
/// // Prints "[10, 20, 30, 40]"
Expand Down Expand Up @@ -621,8 +621,9 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// of the collection.
var count: Int { get }

// The following requirement enables dispatching for index(of:) when
// The following requirements enable dispatching for firstIndex(of:) when
// the element type is Equatable.

/// Returns `Optional(Optional(index))` if an element was found
/// or `Optional(nil)` if an element was determined to be missing;
/// otherwise, `nil`.
Expand Down Expand Up @@ -1067,7 +1068,7 @@ extension Collection where SubSequence == Slice<Self> {
/// print(streetsSlice)
/// // Prints "["Channing", "Douglas", "Evarts"]"
///
/// let index = streetsSlice.index(of: "Evarts") // 4
/// let index = streetsSlice.firstIndex(of: "Evarts") // 4
/// print(streets[index!])
/// // Prints "Evarts"
///
Expand Down Expand Up @@ -1180,7 +1181,7 @@ extension Collection {
}

// TODO: swift-3-indexing-model - rename the following to _customIndexOfEquatable(element)?
/// Customization point for `Collection.index(of:)`.
/// Customization point for `Collection.firstIndex(of:)`.
///
/// Define this method if the collection can find an element in less than
/// O(*n*) by exploiting collection-specific knowledge.
Expand All @@ -1189,7 +1190,7 @@ extension Collection {
/// `Optional(nil)` if the element was not found, or
/// `Optional(Optional(index))` if an element was found.
///
/// - Complexity: O(`count`).
/// - Complexity: Hopefully less than O(`count`).
@inlinable
public // dispatching
func _customIndexOfEquatableElement(_: Iterator.Element) -> Index?? {
Expand Down Expand Up @@ -1402,7 +1403,7 @@ extension Collection {
/// but not including, that index:
///
/// let numbers = [10, 20, 30, 40, 50, 60]
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers.prefix(upTo: i))
/// }
/// // Prints "[10, 20, 30]"
Expand All @@ -1417,7 +1418,7 @@ extension Collection {
/// half-open range as the collection's subscript. The subscript notation is
/// preferred over `prefix(upTo:)`.
///
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers[..<i])
/// }
/// // Prints "[10, 20, 30]"
Expand All @@ -1440,7 +1441,7 @@ extension Collection {
/// that index:
///
/// let numbers = [10, 20, 30, 40, 50, 60]
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers.suffix(from: i))
/// }
/// // Prints "[40, 50, 60]"
Expand All @@ -1455,7 +1456,7 @@ extension Collection {
/// from the index as the collection's subscript. The subscript notation is
/// preferred over `suffix(from:)`.
///
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers[i...])
/// }
/// // Prints "[40, 50, 60]"
Expand All @@ -1479,7 +1480,7 @@ extension Collection {
/// including, that index:
///
/// let numbers = [10, 20, 30, 40, 50, 60]
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers.prefix(through: i))
/// }
/// // Prints "[10, 20, 30, 40]"
Expand All @@ -1488,7 +1489,7 @@ extension Collection {
/// closed range as the collection's subscript. The subscript notation is
/// preferred over `prefix(through:)`.
///
/// if let i = numbers.index(of: 40) {
/// if let i = numbers.firstIndex(of: 40) {
/// print(numbers[...i])
/// }
/// // Prints "[10, 20, 30, 40]"
Expand Down
Loading