Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created January 19, 2014 04:39
Show Gist options
  • Save Varriount/8500547 to your computer and use it in GitHub Desktop.
Save Varriount/8500547 to your computer and use it in GitHub Desktop.
A split procedure that accepts strings
import strutils
iterator split*(s, sep: string): string =
var remainder = s # What string contents is left
var pos = find(remainder, sep)
while pos != -1:
let
start = pos-1
middle = pos+sep.len
ending = remainder.len-1
yield remainder[0..start]
remainder = remainder[middle..ending]
pos = find(remainder, sep)
if remainder != "": # Added this in the gist, should work.
yield remainder
proc split*(s, sep: string): seq[string] =
## Below is a macro found in system.nim for iterator result accumulation
accumulateResult(split(s, sep))
when isMainModule:
echo(repr(
split(
"Hello, world!<br>Int, main. Void?<br>Terrible...<br>",
"<br>"
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment