summary refs log tree commit diff stats
diff options
context:
space:
mode:
authormsmith491 <matthew.smith491@gmail.com>2015-04-30 21:36:50 -0700
committerMatt Smith <matthew.smith491@gmail.com>2015-04-30 21:40:26 -0700
commit50f54bf60b55af731e4d6aff098226003b643bca (patch)
tree4edddf77b0042e0cba3c9660e4ee08ff1b570bcf
parentc82cc7c37c4a6c79a22c78d43627516074bdc0ac (diff)
downloadNim-50f54bf60b55af731e4d6aff098226003b643bca.tar.gz
Add arbitrary char support to the strutils proc. Issue #2626
-rw-r--r--lib/pure/strutils.nim20
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"