From 6904f323aa61b931061c49ab5e6dd8f03c6f4410 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 29 May 2019 18:11:03 +0800 Subject: [PATCH] Fixed #9762 (#11296) --- lib/system.nim | 55 +++++++++++++-------------- tests/manyloc/standalone/barebone.nim | 3 ++ 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 37229fa0c..69ef35256 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -3834,36 +3834,35 @@ template `^^`(s, i: untyped): untyped = template `[]`*(s: string; i: int): char = arrGet(s, i) 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.} = - ## Slice operation for strings. - ## Returns the inclusive range `[s[x.a], s[x.b]]`: - ## - ## .. code-block:: Nim - ## var s = "abcdef" - ## assert s[1..3] == "bcd" - let a = s ^^ x.a - let L = (s ^^ x.b) - a + 1 - result = newString(L) - for i in 0 ..< L: result[i] = s[i + a] +proc `[]`*[T, U](s: string, x: HSlice[T, U]): string {.inline.} = + ## Slice operation for strings. + ## Returns the inclusive range `[s[x.a], s[x.b]]`: + ## + ## .. code-block:: Nim + ## var s = "abcdef" + ## assert s[1..3] == "bcd" + let a = s ^^ x.a + let L = (s ^^ x.b) - a + 1 + result = newString(L) + for i in 0 ..< L: result[i] = s[i + a] - proc `[]=`*[T, U](s: var string, x: HSlice[T, U], b: string) = - ## Slice assignment for strings. - ## - ## If ``b.len`` is not exactly the number of elements that are referred to - ## by `x`, a `splice`:idx: is performed: - ## - runnableExamples: - var s = "abcdefgh" - s[1 .. ^2] = "xyz" - assert s == "axyzh" +proc `[]=`*[T, U](s: var string, x: HSlice[T, U], b: string) = + ## Slice assignment for strings. + ## + ## If ``b.len`` is not exactly the number of elements that are referred to + ## by `x`, a `splice`:idx: is performed: + ## + runnableExamples: + var s = "abcdefgh" + s[1 .. ^2] = "xyz" + assert s == "axyzh" - var a = s ^^ x.a - var L = (s ^^ x.b) - a + 1 - if L == b.len: - for i in 0..", cdecl.} var x = 0 inc x printf("hi %ld\n", x+4777) + +proc substr(a: string): string = a[0 .. 3] # This should compile. See #9762 +const a = substr("foobar")