Skip to content

Commit fcaed33

Browse files
committed
Trap when going past the end
1 parent b79ef3e commit fcaed33

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Sources/Algorithms/Chunked.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@ extension LazyChunked: LazyCollectionProtocol {
7373
}
7474
}
7575

76-
/// Returns the index in the base collection for the first element that
77-
/// doesn't match the current chunk.
76+
/// Returns the index in the base collection of the end of the chunk starting
77+
/// at the given index.
7878
@usableFromInline
79-
internal func endOfChunk(from i: Base.Index) -> Base.Index {
80-
guard i != base.endIndex else { return base.endIndex }
81-
let subject = projection(base[i])
82-
return base[base.index(after: i)...]
79+
internal func endOfChunk(startingAt start: Base.Index) -> Base.Index {
80+
let subject = projection(base[start])
81+
return base[base.index(after: start)...]
8382
.firstIndex(where: { !belongInSameGroup(subject, projection($0)) })
8483
?? base.endIndex
8584
}
@@ -91,20 +90,21 @@ extension LazyChunked: LazyCollectionProtocol {
9190

9291
@inlinable
9392
public var endIndex: Index {
94-
Index(lowerBound: base.endIndex, upperBound: base.endIndex)
93+
Index(lowerBound: base.endIndex)
9594
}
9695

9796
@inlinable
9897
public func index(after i: Index) -> Index {
99-
let upperBound = i.upperBound ?? endOfChunk(from: i.lowerBound)
100-
let end = endOfChunk(from: upperBound)
98+
let upperBound = i.upperBound ?? endOfChunk(startingAt: i.lowerBound)
99+
guard upperBound != base.endIndex else { return endIndex }
100+
let end = endOfChunk(startingAt: upperBound)
101101
return Index(lowerBound: upperBound, upperBound: end)
102102
}
103103

104104
@inlinable
105105
public subscript(position: Index) -> Base.SubSequence {
106106
let upperBound = position.upperBound
107-
?? endOfChunk(from: position.lowerBound)
107+
?? endOfChunk(startingAt: position.lowerBound)
108108
return base[position.lowerBound..<upperBound]
109109
}
110110
}
@@ -114,8 +114,8 @@ extension LazyChunked.Index: Hashable where Base.Index: Hashable {}
114114
extension LazyChunked: BidirectionalCollection
115115
where Base: BidirectionalCollection
116116
{
117-
/// Returns the index in the base collection for the element that starts
118-
/// the chunk ending at the given index.
117+
/// Returns the index in the base collection of the start of the chunk ending
118+
/// at the given index.
119119
@usableFromInline
120120
internal func startOfChunk(endingAt end: Base.Index) -> Base.Index {
121121
let indexBeforeEnd = base.index(before: end)

0 commit comments

Comments
 (0)