Skip to content

Commit 69e6e19

Browse files
[stdlib] Minor refactor of StrideThroughGenerator.next()
1 parent ebc47e4 commit 69e6e19

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

stdlib/public/core/Stride.swift.gyb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public struct StrideThroughIterator<Element : Strideable> : IteratorProtocol {
227227
internal let _end: Element
228228
internal let _stride: Element.Stride
229229
internal var _current: (index: Int?, value: Element)
230-
internal var _done: Bool = false
230+
internal var _didReturnEnd: Bool = false
231231

232232
internal init(_start: Element, end: Element, stride: Element.Stride) {
233233
self._start = _start
@@ -241,13 +241,13 @@ public struct StrideThroughIterator<Element : Strideable> : IteratorProtocol {
241241
///
242242
/// Once `nil` has been returned, all subsequent calls return `nil`.
243243
public mutating func next() -> Element? {
244-
if _done {
245-
return nil
246-
}
247244
let result = _current.value
248245
if _stride > 0 ? result >= _end : result <= _end {
249-
if result == _end {
250-
_done = true
246+
// This check is needed because if we just changed the above operators
247+
// to > and <, respectively, we might advance current past the end
248+
// and throw it out of bounds (e.g. above Int.max) unnecessarily.
249+
if result == _end && !_didReturnEnd {
250+
_didReturnEnd = true
251251
return result
252252
}
253253
return nil

0 commit comments

Comments
 (0)