added strutils.splitWhitespace
This commit is contained in:
parent
5f9da6b2ae
commit
6d9177c6f1
1 changed files with 23 additions and 15 deletions
|
|
@ -513,16 +513,14 @@ 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) =
|
|
||||||
## Deprecated split[char] for transition period
|
|
||||||
var last = 0
|
var last = 0
|
||||||
var splits = maxsplit
|
var splits = maxsplit
|
||||||
assert(not ('\0' in seps))
|
assert(not ('\0' in seps))
|
||||||
while last < len(s):
|
while last < len(s):
|
||||||
while s[last] in seps: inc(last)
|
while s[last] in seps: inc(last)
|
||||||
var first = last
|
var first = last
|
||||||
while last < len(s) and s[last] notin seps: inc(last) # BUGFIX!
|
while last < len(s) and s[last] notin seps: inc(last)
|
||||||
if first <= last-1:
|
if first <= last-1:
|
||||||
if splits == 0: last = len(s)
|
if splits == 0: last = len(s)
|
||||||
yield substr(s, first, last-1)
|
yield substr(s, first, last-1)
|
||||||
|
|
@ -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.
|
||||||
##
|
##
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue