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

strip minor improvement #16444

Merged
merged 6 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2813,7 +2813,7 @@ func strip*(s: string, leading = true, trailing = true,
if leading:
while first <= last and s[first] in chars: inc(first)
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)

func stripLineEnd*(s: var string) =
Expand Down
13 changes: 13 additions & 0 deletions tests/stdlib/tstrutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ template main() =
doAssert strip("sfoofoofoos", leading = false, chars = {'s'}) == "sfoofoofoo"
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
var ret: seq[string] # or use `toSeq` or `collect`
for p in split("/home/a1:xyz:/usr/bin", {':'}): ret.add p
Expand Down