diff options
author | Varriount <Varriount@users.noreply.github.com> | 2015-05-07 13:03:19 -0400 |
---|---|---|
committer | Varriount <Varriount@users.noreply.github.com> | 2015-05-07 13:03:19 -0400 |
commit | ec0e60830a2acbbe701b6b206bc93d56baea3fd4 (patch) | |
tree | 152b0c5a43677327f73789a9a175847b640ed198 | |
parent | d3f69e14f7e1b69d30ed37aa3dcfd2623b658b08 (diff) | |
parent | 50f54bf60b55af731e4d6aff098226003b643bca (diff) | |
download | Nim-ec0e60830a2acbbe701b6b206bc93d56baea3fd4.tar.gz |
Merge pull request #2627 from msmith491/devel
Add arbitrary char support to the strutils proc. Issue #2626
-rw-r--r-- | lib/pure/strutils.nim | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 59cebf7fa..eb4be719a 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -169,14 +169,12 @@ proc cmpIgnoreStyle*(a, b: string): int {.noSideEffect, {.pop.} -proc strip*(s: string, leading = true, trailing = true): string {.noSideEffect, - rtl, extern: "nsuStrip".} = - ## Strips whitespace from `s` and returns the resulting string. +proc strip*(s: string, leading = true, trailing = true, chars: set[char] = Whitespace): string + {.noSideEffect, rtl, extern: "nsuStrip".} = + ## Strips `chars` from `s` and returns the resulting string. ## - ## If `leading` is true, leading whitespace is stripped. - ## If `trailing` is true, trailing whitespace is stripped. - const - chars: set[char] = Whitespace + ## If `leading` is true, leading `chars` are stripped. + ## If `trailing` is true, trailing `chars` are stripped. var first = 0 last = len(s)-1 @@ -1433,3 +1431,11 @@ when isMainModule: doAssert count("foofoofoo", "foofoo", overlapping = true) == 2 doAssert count("foofoofoo", 'f') == 3 doAssert count("foofoofoobar", {'f','b'}) == 4 + + doAssert strip(" foofoofoo ") == "foofoofoo" + doAssert strip("sfoofoofoos", chars = {'s'}) == "foofoofoo" + doAssert strip("barfoofoofoobar", chars = {'b', 'a', 'r'}) == "foofoofoo" + doAssert strip("stripme but don't strip this stripme", + chars = {'s', 't', 'r', 'i', 'p', 'm', 'e'}) == " but don't strip this " + doAssert strip("sfoofoofoos", leading = false, chars = {'s'}) == "sfoofoofoo" + doAssert strip("sfoofoofoos", trailing = false, chars = {'s'}) == "foofoofoos" |