Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created January 20, 2014 04:49
Show Gist options
  • Save Varriount/8514991 to your computer and use it in GitHub Desktop.
Save Varriount/8514991 to your computer and use it in GitHub Desktop.
# Implementation
iterator split*(s, sub: string): string =
## Replaces `sub` in `s` by the string `by`.
var a {.noinit.}: TSkipTable
preprocessSub(sub, a)
var i, j = 0
let z = s.len()
while true:
j = findAux(s, sub, i, a)
if j < 0: break
yield substr(s, i, j - 1)
i = j + len(sub)
# copy the rest:
if i != z:
yield substr(s, i, j-1)
# Test
import strutils
when isMainModule:
var y = 0
for x in split("Hello, world!<br>Int, main. Void?<br>Terrible...<br>!","<br>"):
echo(x)
if y == 2:
break
else:
inc(y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment