Skip to content

Commit c7dd144

Browse files
committed
Add a custom iterator for RegexRangesCollection
1 parent b200e9a commit c7dd144

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,22 @@ struct RegexRangesCollection<Output> {
234234
self.base = RegexMatchesCollection(base: string, regex: regex)
235235
}
236236
}
237+
238+
@available(SwiftStdlib 5.7, *)
239+
extension RegexRangesCollection: Sequence {
240+
struct Iterator: IteratorProtocol {
241+
var matchesBase: RegexMatchesCollection<Output>.Iterator
242+
243+
mutating func next() -> Range<String.Index>? {
244+
matchesBase.next().map(\.range)
245+
}
246+
}
237247

248+
func makeIterator() -> Iterator {
249+
Iterator(matchesBase: base.makeIterator())
250+
}
251+
}
252+
238253
@available(SwiftStdlib 5.7, *)
239254
extension RegexRangesCollection: Collection {
240255
typealias Index = RegexMatchesCollection<Output>.Index

0 commit comments

Comments
 (0)