summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-08-30 23:01:15 +0200
committerGitHub <noreply@github.com>2018-08-30 23:01:15 +0200
commitdf4d5b77a14b3b84f3ff37b3741dd039c9e21ce4 (patch)
tree79693afbc7fc9ba2385c8807bff2c955dbf35926 /lib
parentdfdf8e58c711cadaeb9f830f128eef28aeb6f1ab (diff)
downloadNim-df4d5b77a14b3b84f3ff37b3741dd039c9e21ce4.tar.gz
introduce precise string '[]', '[]=' accessors; fixes #8049 (#8817)
Diffstat (limited to 'lib')
-rw-r--r--lib/system.nim9
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 2c88fe60f..92f51fe7e 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -313,6 +313,12 @@ when defined(nimArrIdx):
   proc `[]=`*[I: Ordinal;T,S](a: T; i: I;
     x: S) {.noSideEffect, magic: "ArrPut".}
   proc `=`*[T](dest: var T; src: T) {.noSideEffect, magic: "Asgn".}
+
+  proc arrGet[I: Ordinal;T](a: T; i: I): T {.
+    noSideEffect, magic: "ArrGet".}
+  proc arrPut[I: Ordinal;T,S](a: T; i: I;
+    x: S) {.noSideEffect, magic: "ArrPut".}
+
   when defined(nimNewRuntime):
     proc `=destroy`*[T](x: var T) {.inline, magic: "Asgn".} =
       ## generic `destructor`:idx: implementation that can be overriden.
@@ -3526,6 +3532,9 @@ template spliceImpl(s, a, L, b: untyped): untyped =
 template `^^`(s, i: untyped): untyped =
   (when i is BackwardsIndex: s.len - int(i) else: int(i))
 
+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.