Skip to content

Commit 8157a38

Browse files
committed
Add a RegexRangesCollection based on RegexMatches
1 parent fc72a72 commit 8157a38

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,38 @@ extension BidirectionalCollection where Element: Comparable {
226226
// }
227227
}
228228

229+
@available(SwiftStdlib 5.7, *)
230+
struct RegexRangesCollection<Output> {
231+
let base: RegexMatchesCollection<Output>
232+
233+
init(string: Substring, regex: Regex<Output>) {
234+
self.base = RegexMatchesCollection(base: string, regex: regex)
235+
}
236+
}
237+
238+
@available(SwiftStdlib 5.7, *)
239+
extension RegexRangesCollection: Collection {
240+
typealias Index = RegexMatchesCollection<Output>.Index
241+
242+
var startIndex: Index { base.startIndex }
243+
var endIndex: Index { base.endIndex }
244+
func index(after i: Index) -> Index { base.index(after: i) }
245+
subscript(position: Index) -> Range<String.Index> { base[position].range }
246+
}
247+
229248
// MARK: Regex algorithms
230249

231-
extension BidirectionalCollection where SubSequence == Substring {
250+
extension Collection where SubSequence == Substring {
232251
@available(SwiftStdlib 5.7, *)
233252
@_disfavoredOverload
234253
func _ranges<R: RegexComponent>(
235254
of regex: R
236-
) -> RangesCollection<RegexConsumer<R, Self>> {
237-
_ranges(of: RegexConsumer(regex))
255+
) -> RegexRangesCollection<R.RegexOutput> {
256+
RegexRangesCollection(string: self[...], regex: regex.regex)
238257
}
258+
}
239259

260+
extension BidirectionalCollection where SubSequence == Substring {
240261
@available(SwiftStdlib 5.7, *)
241262
func _rangesFromBack<R: RegexComponent>(
242263
of regex: R

0 commit comments

Comments
 (0)