Merge branch 'maxsplit' of https://github.com/mjoud/Nim into mjoud-maxsplit
This commit is contained in:
commit
4e83a24662
1 changed files with 29 additions and 10 deletions
|
|
@ -324,7 +324,7 @@ proc toOctal*(c: char): string {.noSideEffect, rtl, extern: "nsuToOctal".} =
|
||||||
result[i] = chr(val mod 8 + ord('0'))
|
result[i] = chr(val mod 8 + ord('0'))
|
||||||
val = val div 8
|
val = val div 8
|
||||||
|
|
||||||
iterator split*(s: string, seps: set[char] = Whitespace): string =
|
iterator split*(s: string, seps: set[char] = Whitespace, maxsplit: int = -1): string =
|
||||||
## Splits the string `s` into substrings using a group of separators.
|
## Splits the string `s` into substrings using a group of separators.
|
||||||
##
|
##
|
||||||
## Substrings are separated by a substring containing only `seps`. Note
|
## Substrings are separated by a substring containing only `seps`. Note
|
||||||
|
|
@ -369,15 +369,19 @@ iterator split*(s: string, seps: set[char] = Whitespace): string =
|
||||||
## "08.398990"
|
## "08.398990"
|
||||||
##
|
##
|
||||||
var last = 0
|
var last = 0
|
||||||
|
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) # BUGFIX!
|
||||||
if first <= last-1:
|
if first <= last-1:
|
||||||
|
if splits == 0: last = len(s)
|
||||||
yield substr(s, first, last-1)
|
yield substr(s, first, last-1)
|
||||||
|
if splits == 0: break
|
||||||
|
dec(splits)
|
||||||
|
|
||||||
iterator split*(s: string, sep: char): 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.
|
||||||
##
|
##
|
||||||
## Substrings are separated by the character `sep`.
|
## Substrings are separated by the character `sep`.
|
||||||
|
|
@ -404,26 +408,34 @@ iterator split*(s: string, sep: char): string =
|
||||||
## ""
|
## ""
|
||||||
##
|
##
|
||||||
var last = 0
|
var last = 0
|
||||||
|
var splits = maxsplit
|
||||||
assert('\0' != sep)
|
assert('\0' != sep)
|
||||||
if len(s) > 0:
|
if len(s) > 0:
|
||||||
# `<=` is correct here for the edge cases!
|
# `<=` is correct here for the edge cases!
|
||||||
while last <= len(s):
|
while last <= len(s):
|
||||||
var first = last
|
var first = last
|
||||||
while last < len(s) and s[last] != sep: inc(last)
|
while last < len(s) and s[last] != sep: inc(last)
|
||||||
|
if splits == 0: last = len(s)
|
||||||
yield substr(s, first, last-1)
|
yield substr(s, first, last-1)
|
||||||
|
if splits == 0: break
|
||||||
|
dec(splits)
|
||||||
inc(last)
|
inc(last)
|
||||||
|
|
||||||
iterator split*(s: string, sep: string): string =
|
iterator split*(s: string, sep: string, maxsplit: int = -1): string =
|
||||||
## Splits the string `s` into substrings using a string separator.
|
## Splits the string `s` into substrings using a string separator.
|
||||||
##
|
##
|
||||||
## Substrings are separated by the string `sep`.
|
## Substrings are separated by the string `sep`.
|
||||||
var last = 0
|
var last = 0
|
||||||
|
var splits = maxsplit
|
||||||
if len(s) > 0:
|
if len(s) > 0:
|
||||||
while last <= len(s):
|
while last <= len(s):
|
||||||
var first = last
|
var first = last
|
||||||
while last < len(s) and s.substr(last, last + <sep.len) != sep:
|
while last < len(s) and s.substr(last, last + <sep.len) != sep:
|
||||||
inc(last)
|
inc(last)
|
||||||
|
if splits == 0: last = len(s)
|
||||||
yield substr(s, first, last-1)
|
yield substr(s, first, last-1)
|
||||||
|
if splits == 0: break
|
||||||
|
dec(splits)
|
||||||
inc(last, sep.len)
|
inc(last, sep.len)
|
||||||
|
|
||||||
iterator splitLines*(s: string): string =
|
iterator splitLines*(s: string): string =
|
||||||
|
|
@ -493,25 +505,25 @@ proc countLines*(s: string): int {.noSideEffect,
|
||||||
else: discard
|
else: discard
|
||||||
inc i
|
inc i
|
||||||
|
|
||||||
proc split*(s: string, seps: set[char] = Whitespace): seq[string] {.
|
proc split*(s: string, seps: set[char] = Whitespace, maxsplit: int = -1): seq[string] {.
|
||||||
noSideEffect, rtl, extern: "nsuSplitCharSet".} =
|
noSideEffect, rtl, extern: "nsuSplitCharSet".} =
|
||||||
## The same as the `split iterator <#split.i,string,set[char]>`_, but is a
|
## The same as the `split iterator <#split.i,string,set[char]>`_, but is a
|
||||||
## proc that returns a sequence of substrings.
|
## proc that returns a sequence of substrings.
|
||||||
accumulateResult(split(s, seps))
|
accumulateResult(split(s, seps, maxsplit))
|
||||||
|
|
||||||
proc split*(s: string, sep: char): 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>`_, but is a proc
|
## The same as the `split iterator <#split.i,string,char>`_, but is a proc
|
||||||
## that returns a sequence of substrings.
|
## that returns a sequence of substrings.
|
||||||
accumulateResult(split(s, sep))
|
accumulateResult(split(s, sep, maxsplit))
|
||||||
|
|
||||||
proc split*(s: string, sep: string): seq[string] {.noSideEffect,
|
proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEffect,
|
||||||
rtl, extern: "nsuSplitString".} =
|
rtl, extern: "nsuSplitString".} =
|
||||||
## Splits the string `s` into substrings using a string separator.
|
## Splits the string `s` into substrings using a string separator.
|
||||||
##
|
##
|
||||||
## 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>`_.
|
## `split iterator <#split.i,string,string>`_.
|
||||||
accumulateResult(split(s, sep))
|
accumulateResult(split(s, sep, maxsplit))
|
||||||
|
|
||||||
proc toHex*(x: BiggestInt, len: Positive): string {.noSideEffect,
|
proc toHex*(x: BiggestInt, len: Positive): string {.noSideEffect,
|
||||||
rtl, extern: "nsuToHex".} =
|
rtl, extern: "nsuToHex".} =
|
||||||
|
|
@ -1743,6 +1755,7 @@ when isMainModule:
|
||||||
doAssert isUpper("ABC")
|
doAssert isUpper("ABC")
|
||||||
doAssert(not isUpper("AAcc"))
|
doAssert(not isUpper("AAcc"))
|
||||||
doAssert(not isUpper("A#$"))
|
doAssert(not isUpper("A#$"))
|
||||||
|
|
||||||
doAssert(unescape(r"\x013", "", "") == "\x013")
|
doAssert(unescape(r"\x013", "", "") == "\x013")
|
||||||
|
|
||||||
doAssert join(["foo", "bar", "baz"]) == "foobarbaz"
|
doAssert join(["foo", "bar", "baz"]) == "foobarbaz"
|
||||||
|
|
@ -1778,4 +1791,10 @@ bar
|
||||||
bar
|
bar
|
||||||
""".unindent() == "foo\nfoo\nbar\n"
|
""".unindent() == "foo\nfoo\nbar\n"
|
||||||
|
|
||||||
echo("strutils tests passed")
|
let s = " this is an example "
|
||||||
|
doAssert s.split() == @["this", "is", "an", "example"]
|
||||||
|
doAssert s.split(maxsplit=4) == @["this", "is", "an", "example"]
|
||||||
|
doAssert s.split(' ', maxsplit=4) == @["", "this", "", "", "is an example "]
|
||||||
|
doAssert s.split(" ", maxsplit=4) == @["", "this", "", "", "is an example "]
|
||||||
|
|
||||||
|
#echo("strutils tests passed")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue