Skip to content

[DO NOT MERGE][stdlib] Collection: switch to using '<' instead of '==' in some loop termination conditions #41331

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 3 additions & 5 deletions stdlib/public/core/BidirectionalCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ extension BidirectionalCollection {
}
var i = i
for _ in stride(from: 0, to: distance, by: -1) {
if i == limit {
return nil
}
guard i > limit else { return nil }
formIndex(before: &i)
}
return i
Expand All @@ -278,13 +276,13 @@ extension BidirectionalCollection {
var count = 0

if start < end {
while start != end {
while start < end {
count += 1
formIndex(after: &start)
}
}
else if start > end {
while start != end {
while start > end {
count -= 1
formIndex(before: &start)
}
Expand Down
8 changes: 3 additions & 5 deletions stdlib/public/core/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extension IndexingIterator: IteratorProtocol, Sequence {
@inlinable
@inline(__always)
public mutating func next() -> Elements.Element? {
if _position == _elements.endIndex { return nil }
guard _position < _elements.endIndex else { return nil }
let element = _elements[_position]
_elements.formIndex(after: &_position)
return element
Expand Down Expand Up @@ -899,7 +899,7 @@ extension Collection {

var start = start
var count = 0
while start != end {
while start < end {
count = count + 1
formIndex(after: &start)
}
Expand Down Expand Up @@ -989,9 +989,7 @@ extension Collection {

var i = i
for _ in stride(from: 0, to: n, by: 1) {
if i == limit {
return nil
}
guard i < limit else { return nil }
formIndex(after: &i)
}
return i
Expand Down