added strutils.splitWhitespace

This commit is contained in:
Andreas Rumpf 2016-07-06 11:59:20 +02:00
commit 6d9177c6f1

View file

@ -513,21 +513,19 @@ template splitCommon(s, sep, maxsplit, sepLen) =
dec(splits) dec(splits)
inc(last, sepLen) inc(last, sepLen)
when defined(nimOldSplit): template oldSplit(s, seps, maxsplit) =
template oldSplit(s, seps, maxsplit) = var last = 0
## Deprecated split[char] for transition period var splits = maxsplit
var last = 0 assert(not ('\0' in seps))
var splits = maxsplit while last < len(s):
assert(not ('\0' in seps)) while s[last] in seps: inc(last)
while last < len(s): var first = last
while s[last] in seps: inc(last) while last < len(s) and s[last] notin seps: inc(last)
var first = last if first <= last-1:
while last < len(s) and s[last] notin seps: inc(last) # BUGFIX! if splits == 0: last = len(s)
if first <= last-1: yield substr(s, first, last-1)
if splits == 0: last = len(s) if splits == 0: break
yield substr(s, first, last-1) dec(splits)
if splits == 0: break
dec(splits)
iterator split*(s: string, seps: set[char] = Whitespace, iterator split*(s: string, seps: set[char] = Whitespace,
maxsplit: int = -1): string = maxsplit: int = -1): string =
@ -576,6 +574,16 @@ iterator split*(s: string, seps: set[char] = Whitespace,
else: else:
splitCommon(s, seps, maxsplit, 1) splitCommon(s, seps, maxsplit, 1)
iterator splitWhitespace*(s: string): string =
## Splits at whitespace.
oldSplit(s, Whitespace, -1)
proc splitWhitespace*(s: string): seq[string] {.noSideEffect,
rtl, extern: "nsuSplitWhitespace".} =
## The same as the `splitWhitespace <#splitWhitespace.i,string>`_
## iterator, but is a proc that returns a sequence of substrings.
accumulateResult(splitWhitespace(s))
iterator split*(s: string, sep: char, maxsplit: int = -1): string = iterator split*(s: string, sep: char, maxsplit: int = -1): string =
## Splits the string `s` into substrings using a single separator. ## Splits the string `s` into substrings using a single separator.
## ##