diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-07-06 11:59:20 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2016-07-06 11:59:20 +0200 |
commit | 6d9177c6f1c668a517eaaa1e6946a4885ef531de (patch) | |
tree | 37514f1074dcb6e1113b4b1e3a9a590c4a9daf3d | |
parent | 5f9da6b2ae137feb762c149ed5a7e0eb0f23a17d (diff) | |
download | Nim-6d9177c6f1c668a517eaaa1e6946a4885ef531de.tar.gz |
added strutils.splitWhitespace
-rw-r--r-- | lib/pure/strutils.nim | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 757268cd1..9b6cf45c5 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -513,21 +513,19 @@ template splitCommon(s, sep, maxsplit, sepLen) = dec(splits) inc(last, sepLen) -when defined(nimOldSplit): - template oldSplit(s, seps, maxsplit) = - ## Deprecated split[char] for transition period - var last = 0 - var splits = maxsplit - 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: - if splits == 0: last = len(s) - yield substr(s, first, last-1) - if splits == 0: break - dec(splits) +template oldSplit(s, seps, maxsplit) = + var last = 0 + var splits = maxsplit + 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) + if first <= last-1: + if splits == 0: last = len(s) + yield substr(s, first, last-1) + if splits == 0: break + dec(splits) iterator split*(s: string, seps: set[char] = Whitespace, maxsplit: int = -1): string = @@ -576,6 +574,16 @@ iterator split*(s: string, seps: set[char] = Whitespace, else: 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 = ## Splits the string `s` into substrings using a single separator. ## |