parent
13505c3be2
commit
6904f323aa
2 changed files with 30 additions and 28 deletions
|
|
@ -3834,36 +3834,35 @@ template `^^`(s, i: untyped): untyped =
|
||||||
template `[]`*(s: string; i: int): char = arrGet(s, i)
|
template `[]`*(s: string; i: int): char = arrGet(s, i)
|
||||||
template `[]=`*(s: string; i: int; val: char) = arrPut(s, i, val)
|
template `[]=`*(s: string; i: int; val: char) = arrPut(s, i, val)
|
||||||
|
|
||||||
when hasAlloc or defined(nimscript):
|
proc `[]`*[T, U](s: string, x: HSlice[T, U]): string {.inline.} =
|
||||||
proc `[]`*[T, U](s: string, x: HSlice[T, U]): string {.inline.} =
|
## Slice operation for strings.
|
||||||
## Slice operation for strings.
|
## Returns the inclusive range `[s[x.a], s[x.b]]`:
|
||||||
## Returns the inclusive range `[s[x.a], s[x.b]]`:
|
##
|
||||||
##
|
## .. code-block:: Nim
|
||||||
## .. code-block:: Nim
|
## var s = "abcdef"
|
||||||
## var s = "abcdef"
|
## assert s[1..3] == "bcd"
|
||||||
## assert s[1..3] == "bcd"
|
let a = s ^^ x.a
|
||||||
let a = s ^^ x.a
|
let L = (s ^^ x.b) - a + 1
|
||||||
let L = (s ^^ x.b) - a + 1
|
result = newString(L)
|
||||||
result = newString(L)
|
for i in 0 ..< L: result[i] = s[i + a]
|
||||||
for i in 0 ..< L: result[i] = s[i + a]
|
|
||||||
|
|
||||||
proc `[]=`*[T, U](s: var string, x: HSlice[T, U], b: string) =
|
proc `[]=`*[T, U](s: var string, x: HSlice[T, U], b: string) =
|
||||||
## Slice assignment for strings.
|
## Slice assignment for strings.
|
||||||
##
|
##
|
||||||
## If ``b.len`` is not exactly the number of elements that are referred to
|
## If ``b.len`` is not exactly the number of elements that are referred to
|
||||||
## by `x`, a `splice`:idx: is performed:
|
## by `x`, a `splice`:idx: is performed:
|
||||||
##
|
##
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
var s = "abcdefgh"
|
var s = "abcdefgh"
|
||||||
s[1 .. ^2] = "xyz"
|
s[1 .. ^2] = "xyz"
|
||||||
assert s == "axyzh"
|
assert s == "axyzh"
|
||||||
|
|
||||||
var a = s ^^ x.a
|
var a = s ^^ x.a
|
||||||
var L = (s ^^ x.b) - a + 1
|
var L = (s ^^ x.b) - a + 1
|
||||||
if L == b.len:
|
if L == b.len:
|
||||||
for i in 0..<L: s[i+a] = b[i]
|
for i in 0..<L: s[i+a] = b[i]
|
||||||
else:
|
else:
|
||||||
spliceImpl(s, a, L, b)
|
spliceImpl(s, a, L, b)
|
||||||
|
|
||||||
proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: HSlice[U, V]): seq[T] =
|
proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: HSlice[U, V]): seq[T] =
|
||||||
## Slice operation for arrays.
|
## Slice operation for arrays.
|
||||||
|
|
|
||||||
|
|
@ -11,3 +11,6 @@ proc printf(frmt: cstring) {.varargs, header: "<stdio.h>", cdecl.}
|
||||||
var x = 0
|
var x = 0
|
||||||
inc x
|
inc x
|
||||||
printf("hi %ld\n", x+4777)
|
printf("hi %ld\n", x+4777)
|
||||||
|
|
||||||
|
proc substr(a: string): string = a[0 .. 3] # This should compile. See #9762
|
||||||
|
const a = substr("foobar")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue