Scoop API

scoop:scoop.regex:0.1.0

src/iteration.scoop

Selected production source · src/iteration.scoop

1package scoop.regex
3import scoop.sequences.Sequence
5internal class RegexMatches internal constructor(
6 private val regex: Regex,
7 private val text: String,
8 private val start: Long
9) : Sequence<MatchResult> {
10 public override operator fun iterator(): Iterator<MatchResult> = RegexIterator(regex, text, start)
13internal class RegexIterator internal constructor(
14 private val regex: Regex,
15 text: String,
16 start: Long
17) : Iterator<MatchResult> {
18 private val input: RegexInput = RegexInput(text)
19 private var position: Long = start
20 private var flags: UInt = 0u
21 private var finished: Boolean = false
23 public override fun next(): MatchResult? {
24 if (finished) return None
25 when (val step = regex.search(input, position, flags)) {
26 case None -> {
27 finished = true
28 return None
29 }
30 case Some(val found) -> {
31 position = found.start
32 flags = found.flags
33 finished = !found.hasNext
34 return Some(found.result)
35 }
36 }
37 }