diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-04-06 21:45:37 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-04-06 21:45:37 +0200 |
| commit | 9a2b6c01aa2853a87ac5cff2f9981ffce39a2293 (patch) | |
| tree | d160dc4b29bfecd3b33aeac81000ce3895ca51e3 /core/text/regex | |
| parent | 66077add3375d89c15d986e58a845c9f0b70b3df (diff) | |
Return loop index in regex iterator.
Diffstat (limited to 'core/text/regex')
| -rw-r--r-- | core/text/regex/regex.odin | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/core/text/regex/regex.odin b/core/text/regex/regex.odin index 1ad582a4f..a887d5967 100644 --- a/core/text/regex/regex.odin +++ b/core/text/regex/regex.odin @@ -73,6 +73,7 @@ Match_Iterator :: struct { offset: int, regex: Regular_Expression, capture: Capture, + idx: int, temp: runtime.Allocator, } @@ -442,7 +443,7 @@ Returns: - result: `Capture` for this iteration. - ok: A bool indicating if there was a match, stopping the iteration on `false`. */ -match_iterator :: proc(it: ^Match_Iterator) -> (result: Capture, ok: bool) { +match_iterator :: proc(it: ^Match_Iterator) -> (result: Capture, index: int, ok: bool) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() num_groups: int num_groups, ok = match_with_preallocated_capture( @@ -452,6 +453,10 @@ match_iterator :: proc(it: ^Match_Iterator) -> (result: Capture, ok: bool) { it.temp, ) + defer if ok { + it.idx += 1 + } + if num_groups > 0 { for i in 0..<num_groups { it.capture.pos[i] += it.offset @@ -459,7 +464,7 @@ match_iterator :: proc(it: ^Match_Iterator) -> (result: Capture, ok: bool) { it.offset = it.capture.pos[0][1] result = {it.capture.pos[:num_groups], it.capture.groups[:num_groups]} } - return + return result, it.idx, ok } match :: proc { @@ -468,6 +473,11 @@ match :: proc { match_iterator, } +reset :: proc(it: ^Match_Iterator) { + it.offset = 0 + it.idx = 0 +} + /* Allocate a `Capture` in advance for use with `match`. This can save some time if you plan on performing several matches at once and only need the results |