R.distinctMatches

This commit is contained in:
2021-11-29 15:00:06 -07:00
parent cff42ea42d
commit 190b5ce1d2
2 changed files with 13 additions and 1 deletions

View File

@@ -5,3 +5,6 @@
match "a"
right "bc"))
(otherwise (throw "bad match result"))))
(let [matches (R.distinctMatches "a" "abababab")]
(assert (= 4 matches.length)))

View File

@@ -70,4 +70,13 @@
right
(r.matchedRight)
group
->idx (r.matched idx))))))
->idx (r.matched idx))))))
// Return an array of all non-overlapping matches in the given text, from left-to-right
(function :Array<RMatch> distinctMatches [:String regex :String textToSearch &opt :String flags]
(let [matches []
&mut nextMatch null]
(while (set nextMatch (match regex textToSearch flags))
(matches.push nextMatch)
(set textToSearch nextMatch.right))
matches))