From 22115a2c6a43709aba02054b77ce2e641f280be1 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 28 Oct 2011 17:57:58 +0200 Subject: [PATCH] splicing tested and documented --- lib/system.nim | 43 ++++++++++++++++++------------------ tests/accept/run/tslices.nim | 16 ++++++++++++++ tests/rodfiles/bmethods.nim | 3 +-- tests/rodfiles/bmethods2.nim | 3 +-- tests/rodfiles/deada.nim | 3 +-- tests/rodfiles/deada2.nim | 3 +-- todo.txt | 31 +++++++++++++++++++++++++- 7 files changed, 72 insertions(+), 30 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 8854495370..8657efe2a8 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -1930,36 +1930,37 @@ proc `[]`*(s: string, x: TSlice[int]): string {.inline.} = ## slice operation for strings. Negative indexes are supported. result = s.substr(x.a-|s, x.b-|s) -template spliceImpl(x, start, endp, spliced: expr): stmt = - var - count = endp - start + 1 - shift = spliced.len - count - newLen = x.len + shift - totalShifted = x.len - (start + count) - firstShifted = newLen - totalShifted - +template spliceImpl(s, a, L, b: expr): stmt = + # make room for additional elements or cut: + var slen = s.len + var shift = b.len - L + var newLen = slen + shift if shift > 0: - setLen(x, newLen) - - for i in countdown(newLen - 1, firstShifted): - shallowCopy(x[i], x[i-shift]) - - for c in countup(0, spliced.len - 1): - x[start + c] = spliced[c] - - if shift < 0: - setLen(x, newLen) + # enlarge: + setLen(s, newLen) + for i in countdown(newLen-1, a+shift+1): shallowCopy(s[i], s[i-shift]) + else: + for i in countup(a+b.len, s.len-1+shift): shallowCopy(s[i], s[i-shift]) + # cut down: + setLen(s, newLen) + # fill the hole: + for i in 0 ..