scoop:scoop.regex:0.1.0
src/iteration.scoop
Selected production source · src/iteration.scoop
1
package scoop.regex3
import scoop.sequences.Sequence5
internal class RegexMatches internal constructor(6
private val regex: Regex,7
private val text: String,8
private val start: Long9
) : Sequence<MatchResult> {10
public override operator fun iterator(): Iterator<MatchResult> = RegexIterator(regex, text, start)11
}13
internal class RegexIterator internal constructor(14
private val regex: Regex,15
text: String,16
start: Long17
) : Iterator<MatchResult> {18
private val input: RegexInput = RegexInput(text)19
private var position: Long = start20
private var flags: UInt = 0u21
private var finished: Boolean = false23
public override fun next(): MatchResult? {24
if (finished) return None25
when (val step = regex.search(input, position, flags)) {26
case None -> {27
finished = true28
return None29
}30
case Some(val found) -> {31
position = found.start32
flags = found.flags33
finished = !found.hasNext34
return Some(found.result)35
}36
}37
}38
}