parent
d90f3f59ac
commit
c57fcf42df
2 changed files with 60 additions and 6 deletions
|
|
@ -3252,19 +3252,18 @@ proc `/`*(x, y: int): float {.inline, noSideEffect.} =
|
||||||
|
|
||||||
template spliceImpl(s, a, L, b: untyped): untyped =
|
template spliceImpl(s, a, L, b: untyped): untyped =
|
||||||
# make room for additional elements or cut:
|
# make room for additional elements or cut:
|
||||||
var slen = s.len
|
var shift = b.len - max(0,L) # ignore negative slice size
|
||||||
var shift = b.len - L
|
var newLen = s.len + shift
|
||||||
var newLen = slen + shift
|
|
||||||
if shift > 0:
|
if shift > 0:
|
||||||
# enlarge:
|
# enlarge:
|
||||||
setLen(s, newLen)
|
setLen(s, newLen)
|
||||||
for i in countdown(newLen-1, a+shift+1): shallowCopy(s[i], s[i-shift])
|
for i in countdown(newLen-1, a+b.len): shallowCopy(s[i], s[i-shift])
|
||||||
else:
|
else:
|
||||||
for i in countup(a+b.len, s.len-1+shift): shallowCopy(s[i], s[i-shift])
|
for i in countup(a+b.len, newLen-1): shallowCopy(s[i], s[i-shift])
|
||||||
# cut down:
|
# cut down:
|
||||||
setLen(s, newLen)
|
setLen(s, newLen)
|
||||||
# fill the hole:
|
# fill the hole:
|
||||||
for i in 0 .. <b.len: s[i+a] = b[i]
|
for i in 0 .. <b.len: s[a+i] = b[i]
|
||||||
|
|
||||||
when hasAlloc or defined(nimscript):
|
when hasAlloc or defined(nimscript):
|
||||||
proc `[]`*(s: string, x: Slice[int]): string {.inline.} =
|
proc `[]`*(s: string, x: Slice[int]): string {.inline.} =
|
||||||
|
|
|
||||||
55
tests/stdlib/tstring.nim
Normal file
55
tests/stdlib/tstring.nim
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
discard """
|
||||||
|
file: "tstring.nim"
|
||||||
|
output: "OK"
|
||||||
|
"""
|
||||||
|
const characters = "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
const numbers = "1234567890"
|
||||||
|
|
||||||
|
var s: string
|
||||||
|
|
||||||
|
proc test_string_slice() =
|
||||||
|
# test "slice of length == len(characters)":
|
||||||
|
# replace characters completely by numbers
|
||||||
|
s = characters
|
||||||
|
s[0..^1] = numbers
|
||||||
|
doAssert s == numbers
|
||||||
|
|
||||||
|
# test "slice of length > len(numbers)":
|
||||||
|
# replace characters by slice of same length
|
||||||
|
s = characters
|
||||||
|
s[1..16] = numbers
|
||||||
|
doAssert s == "a1234567890rstuvwxyz"
|
||||||
|
|
||||||
|
# test "slice of length == len(numbers)":
|
||||||
|
# replace characters by slice of same length
|
||||||
|
s = characters
|
||||||
|
s[1..10] = numbers
|
||||||
|
doAssert s == "a1234567890lmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
# test "slice of length < len(numbers)":
|
||||||
|
# replace slice of length. and insert remaining chars
|
||||||
|
s = characters
|
||||||
|
s[1..4] = numbers
|
||||||
|
doAssert s == "a1234567890fghijklmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
# test "slice of length == 1":
|
||||||
|
# replace first character. and insert remaining 9 chars
|
||||||
|
s = characters
|
||||||
|
s[1..1] = numbers
|
||||||
|
doAssert s == "a1234567890cdefghijklmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
# test "slice of length == 0":
|
||||||
|
# insert chars at slice start index
|
||||||
|
s = characters
|
||||||
|
s[2..1] = numbers
|
||||||
|
doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
# test "slice of negative length":
|
||||||
|
# same as slice of zero length
|
||||||
|
s = characters
|
||||||
|
s[2..0] = numbers
|
||||||
|
doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
echo("OK")
|
||||||
|
|
||||||
|
test_string_slice()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue