Skip to content

Instantly share code, notes, and snippets.

@nitely
Created August 25, 2018 18:00
Show Gist options
  • Save nitely/633d3a6412c6776098d5a1ec3af073c3 to your computer and use it in GitHub Desktop.
Save nitely/633d3a6412c6776098d5a1ec3af073c3 to your computer and use it in GitHub Desktop.
nim-regex find based split
proc split*(s: string, sep: Regex): seq[string] =
result = @[]
var
m = RegexMatch()
ds = initDataSets(sep.states.len, true)
first = 0
last = 0
n = 0
while last <= s.len:
first = last
if findImpl(ds, s, sep, m, last) and
m.boundaries.a <= m.boundaries.b:
n = m.boundaries.b+1
last = m.boundaries.a
else:
n = s.len+1
last = s.len+1
result.add(substr(s, first, last-1))
if n > 0:
last = n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment