strip minor improvement (#16444)
* strip minor improvement * add more tests * Update tests/stdlib/tstrutils.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
This commit is contained in:
parent
8d913a5921
commit
417c2509c4
2 changed files with 14 additions and 1 deletions
|
|
@ -2813,7 +2813,7 @@ func strip*(s: string, leading = true, trailing = true,
|
||||||
if leading:
|
if leading:
|
||||||
while first <= last and s[first] in chars: inc(first)
|
while first <= last and s[first] in chars: inc(first)
|
||||||
if trailing:
|
if trailing:
|
||||||
while last >= 0 and s[last] in chars: dec(last)
|
while last >= first and s[last] in chars: dec(last)
|
||||||
result = substr(s, first, last)
|
result = substr(s, first, last)
|
||||||
|
|
||||||
func stripLineEnd*(s: var string) =
|
func stripLineEnd*(s: var string) =
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,19 @@ template main() =
|
||||||
doAssert strip("sfoofoofoos", leading = false, chars = {'s'}) == "sfoofoofoo"
|
doAssert strip("sfoofoofoos", leading = false, chars = {'s'}) == "sfoofoofoo"
|
||||||
doAssert strip("sfoofoofoos", trailing = false, chars = {'s'}) == "foofoofoos"
|
doAssert strip("sfoofoofoos", trailing = false, chars = {'s'}) == "foofoofoos"
|
||||||
|
|
||||||
|
block:
|
||||||
|
let a = "xxxxxx"
|
||||||
|
doAssert a.strip(chars={'x'}).len == 0
|
||||||
|
|
||||||
|
doAssert "".strip(chars={'x'}).len == 0
|
||||||
|
doAssert " ".strip(chars={'x'}) == " "
|
||||||
|
doAssert "xxx xxx".strip(chars={'x'}) == " "
|
||||||
|
doAssert "xxx wind".strip(chars={'x'}) == " wind"
|
||||||
|
doAssert "xxx iii".strip(chars={'i'}) == "xxx "
|
||||||
|
doAssert "x".strip(leading = false, chars={'x'}).len == 0
|
||||||
|
doAssert "x".strip(trailing = false, chars={'x'}).len == 0
|
||||||
|
doAssert "x".strip(leading = false, trailing = false, chars={'x'}) == "x"
|
||||||
|
|
||||||
block: # split
|
block: # split
|
||||||
var ret: seq[string] # or use `toSeq` or `collect`
|
var ret: seq[string] # or use `toSeq` or `collect`
|
||||||
for p in split("/home/a1:xyz:/usr/bin", {':'}): ret.add p
|
for p in split("/home/a1:xyz:/usr/bin", {':'}): ret.add p
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue