Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unicode.split with seperators examples #17176

Merged
merged 9 commits into from
Feb 26, 2021
83 changes: 25 additions & 58 deletions lib/pure/unicode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -955,43 +955,25 @@ iterator split*(s: string, seps: openArray[Rune] = unicodeSpaces,
## Splits the unicode string ``s`` into substrings using a group of separators.
##
## Substrings are separated by a substring containing only ``seps``.
##
## .. code-block:: nim
## for word in split("this\lis an\texample"):
## writeLine(stdout, word)
##
## ...generates this output:
##
## .. code-block::
## "this"
## "is"
## "an"
## "example"
##
## And the following code:
##
## .. code-block:: nim
## for word in split("this:is;an$example", {';', ':', '$'}):
## writeLine(stdout, word)
##
## ...produces the same output as the first example. The code:
##
## .. code-block:: nim
## let date = "2012-11-20T22:08:08.398990"
## let separators = {' ', '-', ':', 'T'}
## for number in split(date, separators):
## writeLine(stdout, number)
##
## ...results in:
##
## .. code-block::
## "2012"
## "11"
## "20"
## "22"
## "08"
## "08.398990"
##
runnableExamples:
import std/sequtils

assert toSeq("hÃllo\lthis\lis an\texample\l是".split) ==
@["hÃllo", "this", "is", "an", "example", "是"]

# And the following code splits the same string using a sequence of Runes.
assert toSeq(split("añyóng:hÃllo;是$example", ";:$".toRunes)) ==
@["añyóng", "hÃllo", "是", "example"]

timotheecour marked this conversation as resolved.
Show resolved Hide resolved
# example with a `Rune` separator and unused one `;`:
assert toSeq(split("ab是de:f:", ";:是".toRunes)) == @["ab", "de", "f", ""]

# Another example that splits a string containing a date.
let date = "2012-11-20T22:08:08.398990"

assert toSeq(split(date, " -:T".toRunes)) ==
@["2012", "11", "20", "22", "08", "08.398990"]

splitCommon(s, seps, maxsplit)

iterator splitWhitespace*(s: string): string =
Expand All @@ -1010,28 +992,13 @@ proc splitWhitespace*(s: string): seq[string] {.noSideEffect,

iterator split*(s: string, sep: Rune, maxsplit: int = -1): string =
## Splits the unicode string ``s`` into substrings using a single separator.
##
## Substrings are separated by the rune ``sep``.
## The code:
##
## .. code-block:: nim
## for word in split(";;this;is;an;;example;;;", ';'):
## writeLine(stdout, word)
##
## Results in:
##
## .. code-block::
## ""
## ""
## "this"
## "is"
## "an"
## ""
## "example"
## ""
## ""
## ""
##
runnableExamples:
import std/sequtils

assert toSeq(split(";;hÃllo;this;is;an;;example;;;是", ";".runeAt(0))) ==
@["", "", "hÃllo", "this", "is", "an", "", "example", "", "", "是"]

splitCommon(s, sep, maxsplit)

proc split*(s: string, seps: openArray[Rune] = unicodeSpaces, maxsplit: int = -1):
Expand Down