Created
January 20, 2014 04:49
-
-
Save Varriount/8514991 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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