summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-06-01 19:34:02 +0200
committerAndreas Rumpf <rumpf_a@web.de>2016-06-01 19:34:02 +0200
commitf125f2e4ceca08de862d5856d6d14c86f9ff943f (patch)
tree04f3128a4c0692cc0231b8326600723af8290be0 /lib
parent7f09d6bf1f0eafe2ce4336483035f76ae0ef3539 (diff)
downloadNim-f125f2e4ceca08de862d5856d6d14c86f9ff943f.tar.gz
make split with strings as separator faster
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/strutils.nim7
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index f59313450..85e67f508 100644
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -421,6 +421,11 @@ iterator split*(s: string, sep: char, maxsplit: int = -1): string =
       dec(splits)
       inc(last)
 
+proc substrEq(s: string, a, L: int, x: string): bool =
+  var i = 0
+  while i < L and s[a+i] == x[i]: inc i
+  result = i == L
+
 iterator split*(s: string, sep: string, maxsplit: int = -1): string =
   ## Splits the string `s` into substrings using a string separator.
   ##
@@ -430,7 +435,7 @@ iterator split*(s: string, sep: string, maxsplit: int = -1): string =
   if len(s) > 0:
     while last <= len(s):
       var first = last
-      while last < len(s) and s.substr(last, last + <sep.len) != sep:
+      while last < len(s) and not s.substrEq(last, sep.len, sep):
         inc(last)
       if splits == 0: last = len(s)
       yield substr(s, first, last-1)