summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xlib/pure/strutils.nim12
-rwxr-xr-xrod/semfold.nim4
-rwxr-xr-xtests/accept/run/tstrutil.nim11
3 files changed, 19 insertions, 8 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 301f82c1f..6854999e3 100755
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -655,13 +655,13 @@ proc delete*(s: var string, first, last: int) =
   ## Deletes in `s` the characters at position `first`..`last`. This modifies
   ## `s` itself, it does not return a copy.
   var i = first
-  # example: "abc___uvwxyz\0"  (___ is to be deleted)
-  # --> first == 3, last == 5
-  # s[first..] = s[last+1..]
-  while last+i+1 < len(s):
-    s[i] = s[last+i+1]
+  var j = last+1
+  var newLen = len(s)-j+i
+  while i < newLen:
+    s[i] = s[j]
     inc(i)
-  setlen(s, len(s)-(last-first+1))
+    inc(j)
+  setlen(s, newLen)
 
 proc replaceStr(s, sub, by: string): string = return replace(s, sub, by)
 proc replaceStr(s: string, sub, by: char): string = return replace(s, sub, by)
diff --git a/rod/semfold.nim b/rod/semfold.nim
index 1a55141c7..7aa6da1a6 100755
--- a/rod/semfold.nim
+++ b/rod/semfold.nim
@@ -258,9 +258,9 @@ proc leValueConv(a, b: PNode): bool =
   else: InternalError(a.info, "leValueConv")
   
 proc magicCall(m: PSym, n: PNode): PNode =
-  var s = n.sons[0].sym
   if sonsLen(n) <= 1: return
-  
+
+  var s = n.sons[0].sym
   var a = getConstExpr(m, n.sons[1])
   var b, c: PNode
   if a == nil: return 
diff --git a/tests/accept/run/tstrutil.nim b/tests/accept/run/tstrutil.nim
index affe6e58d..0488d1dc7 100755
--- a/tests/accept/run/tstrutil.nim
+++ b/tests/accept/run/tstrutil.nim
@@ -10,6 +10,17 @@ proc main() =
   testStrip()

   for p in split("/home/a1:xyz:/usr/bin", {':'}):

     write(stdout, p)

+
+proc testDelete = 
+  var s = "0123456789ABCDEFGH"
+  delete(s, 4, 5)
+  assert s == "01236789ABCDEFGH"
+  delete(s, s.len-1, s.len-1)
+  assert s == "01236789ABCDEFG"
+  delete(s, 0, 0)
+  assert s == "1236789ABCDEFG"
+
+testDelete()  
     

 assert(insertSep($1000_000) == "1_000_000")
 assert(insertSep($232) == "232")