Merge pull request #2627 from msmith491/devel

Add arbitrary char support to the strutils  proc.  Issue #2626
This commit is contained in:
Varriount 2015-05-07 13:03:19 -04:00
commit ec0e60830a

View file

@ -169,14 +169,12 @@ proc cmpIgnoreStyle*(a, b: string): int {.noSideEffect,
{.pop.} {.pop.}
proc strip*(s: string, leading = true, trailing = true): string {.noSideEffect, proc strip*(s: string, leading = true, trailing = true, chars: set[char] = Whitespace): string
rtl, extern: "nsuStrip".} = {.noSideEffect, rtl, extern: "nsuStrip".} =
## Strips whitespace from `s` and returns the resulting string. ## Strips `chars` from `s` and returns the resulting string.
## ##
## If `leading` is true, leading whitespace is stripped. ## If `leading` is true, leading `chars` are stripped.
## If `trailing` is true, trailing whitespace is stripped. ## If `trailing` is true, trailing `chars` are stripped.
const
chars: set[char] = Whitespace
var var
first = 0 first = 0
last = len(s)-1 last = len(s)-1
@ -1433,3 +1431,11 @@ when isMainModule:
doAssert count("foofoofoo", "foofoo", overlapping = true) == 2 doAssert count("foofoofoo", "foofoo", overlapping = true) == 2
doAssert count("foofoofoo", 'f') == 3 doAssert count("foofoofoo", 'f') == 3
doAssert count("foofoofoobar", {'f','b'}) == 4 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"