Skip to content

Instantly share code, notes, and snippets.

@Varriount
Last active January 3, 2016 22:19
Show Gist options
  • Save Varriount/8527495 to your computer and use it in GitHub Desktop.
Save Varriount/8527495 to your computer and use it in GitHub Desktop.
import strutils
type charContainer = generic c
c.contains(char) is bool
iterator split*(s: string, seps: charContainer = Whitespace): string =
## Splits the string `s` into substrings using a group of separators.
##
## Substrings are separated by a substring containing only `seps`. Note
## that whole sequences of characters found in ``seps`` will be counted as
## a single split point and leading/trailing separators will be ignored.
var last = 0
assert(not ('\0' in seps))
while last < len(s):
while s[last] in seps: inc(last)
var first = last
while last < len(s) and s[last] notin seps: inc(last) # BUGFIX!
if first <= last-1:
yield substr(s, first, last-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment