This commit is contained in:
Dominik Picheta 2017-11-27 17:51:23 +00:00 • committed by Andreas Rumpf
commit 45821ea2ab
3 changed files with 55 additions and 34 deletions

View file

@ -105,3 +105,6 @@ This now needs to be written as:
:test: :test:
# shows how the 'if' statement works # shows how the 'if' statement works
if true: echo "yes" if true: echo "yes"
- ``strutils.split`` and ``strutils.rsplit`` with an empty string and a
separator now returns that empty string.
See issue [#4377](https://github.com/nim-lang/Nim/issues/4377).

View file

@ -59,8 +59,8 @@ proc xmlCheckedTag*(e: NimNode, tag: string, optAttr = "", reqAttr = "",
# copy the attributes; when iterating over them these lists # copy the attributes; when iterating over them these lists
# will be modified, so that each attribute is only given one value # will be modified, so that each attribute is only given one value
var req = split(reqAttr) var req = splitWhitespace(reqAttr)
var opt = split(optAttr) var opt = splitWhitespace(optAttr)
result = newNimNode(nnkBracket, e) result = newNimNode(nnkBracket, e)
result.add(newStrLitNode("<")) result.add(newStrLitNode("<"))
result.add(newStrLitNode(tag)) result.add(newStrLitNode(tag))

View file

@ -502,7 +502,6 @@ template splitCommon(s, sep, maxsplit, sepLen) =
var last = 0 var last = 0
var splits = maxsplit var splits = maxsplit
if len(s) > 0:
while last <= len(s): while last <= len(s):
var first = last var first = last
while last < len(s) and not stringHasSep(s, last, sep): while last < len(s) and not stringHasSep(s, last, sep):
@ -669,7 +668,6 @@ template rsplitCommon(s, sep, maxsplit, sepLen) =
splits = maxsplit splits = maxsplit
startPos = 0 startPos = 0
if len(s) > 0:
# go to -1 in order to get separators at the beginning # go to -1 in order to get separators at the beginning
while first >= -1: while first >= -1:
while first >= 0 and not stringHasSep(s, first, sep): while first >= 0 and not stringHasSep(s, first, sep):
@ -820,12 +818,18 @@ proc split*(s: string, seps: set[char] = Whitespace, maxsplit: int = -1): seq[st
noSideEffect, rtl, extern: "nsuSplitCharSet".} = noSideEffect, rtl, extern: "nsuSplitCharSet".} =
## The same as the `split iterator <#split.i,string,set[char],int>`_, but is a ## The same as the `split iterator <#split.i,string,set[char],int>`_, but is a
## proc that returns a sequence of substrings. ## proc that returns a sequence of substrings.
runnableExamples:
doAssert "a,b;c".split({',', ';'}) == @["a", "b", "c"]
doAssert "".split({' '}) == @[""]
accumulateResult(split(s, seps, maxsplit)) accumulateResult(split(s, seps, maxsplit))
proc split*(s: string, sep: char, maxsplit: int = -1): seq[string] {.noSideEffect, proc split*(s: string, sep: char, maxsplit: int = -1): seq[string] {.noSideEffect,
rtl, extern: "nsuSplitChar".} = rtl, extern: "nsuSplitChar".} =
## The same as the `split iterator <#split.i,string,char,int>`_, but is a proc ## The same as the `split iterator <#split.i,string,char,int>`_, but is a proc
## that returns a sequence of substrings. ## that returns a sequence of substrings.
runnableExamples:
doAssert "a,b,c".split(',') == @["a", "b", "c"]
doAssert "".split(' ') == @[""]
accumulateResult(split(s, sep, maxsplit)) accumulateResult(split(s, sep, maxsplit))
proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEffect, proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEffect,
@ -834,6 +838,13 @@ proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEff
## ##
## Substrings are separated by the string `sep`. This is a wrapper around the ## Substrings are separated by the string `sep`. This is a wrapper around the
## `split iterator <#split.i,string,string,int>`_. ## `split iterator <#split.i,string,string,int>`_.
runnableExamples:
doAssert "a,b,c".split(",") == @["a", "b", "c"]
doAssert "a man a plan a canal panama".split("a ") == @["", "man ", "plan ", "canal panama"]
doAssert "".split("Elon Musk") == @[""]
doAssert "a largely spaced sentence".split(" ") == @["a", "", "largely", "", "", "", "spaced", "sentence"]
doAssert "a largely spaced sentence".split(" ", maxsplit=1) == @["a", " largely spaced sentence"]
doAssert(sep.len > 0) doAssert(sep.len > 0)
accumulateResult(split(s, sep, maxsplit)) accumulateResult(split(s, sep, maxsplit))
@ -902,6 +913,13 @@ proc rsplit*(s: string, sep: string, maxsplit: int = -1): seq[string]
## .. code-block:: nim ## .. code-block:: nim
## @["Root#Object#Method", "Index"] ## @["Root#Object#Method", "Index"]
## ##
runnableExamples:
doAssert "a largely spaced sentence".rsplit(" ", maxsplit=1) == @["a largely spaced", "sentence"]
doAssert "a,b,c".rsplit(",") == @["a", "b", "c"]
doAssert "a man a plan a canal panama".rsplit("a ") == @["", "man ", "plan ", "canal panama"]
doAssert "".rsplit("Elon Musk") == @[""]
doAssert "a largely spaced sentence".rsplit(" ") == @["a", "", "largely", "", "", "", "spaced", "sentence"]
accumulateResult(rsplit(s, sep, maxsplit)) accumulateResult(rsplit(s, sep, maxsplit))
result.reverse() result.reverse()