summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhlaaftana <10591326+hlaaftana@users.noreply.github.com>2020-11-12 13:44:21 +0300
committerGitHub <noreply@github.com>2020-11-12 11:44:21 +0100
commit527e792827497002ded742ad815c567a4a056471 (patch)
tree6ff7161e1f7cc8fce24d70407cfef14c62030069
parentef2677f5f3f58ad5bd514f82a0efd68d4d17e7f9 (diff)
downloadNim-527e792827497002ded742ad815c567a4a056471.tar.gz
make var string return var char w/ BackwardsIndex (#15461)
* make var string return var char w/ BackwardsIndex

fixes #14497

* work around VM bug
* properly workaround again
-rw-r--r--lib/pure/pathnorm.nim6
-rw-r--r--lib/system.nim1
-rw-r--r--tests/stdlib/tstring.nim7
3 files changed, 11 insertions, 3 deletions
diff --git a/lib/pure/pathnorm.nim b/lib/pure/pathnorm.nim
index 5bc66f0b7..7834f8d95 100644
--- a/lib/pure/pathnorm.nim
+++ b/lib/pure/pathnorm.nim
@@ -69,7 +69,7 @@ proc addNormalizePath*(x: string; result: var string; state: var int;
   while hasNext(it, x):
     let b = next(it, x)
     if (state shr 1 == 0) and isSlash(x, b):
-      if result.len == 0 or result[^1] notin {DirSep, AltSep}:
+      if result.len == 0 or result[result.len - 1] notin {DirSep, AltSep}:
         result.add dirSep
       state = state or 1
     elif isDotDot(x, b):
@@ -87,13 +87,13 @@ proc addNormalizePath*(x: string; result: var string; state: var int;
           setLen(result, d-1)
           dec state, 2
       else:
-        if result.len > 0 and result[^1] notin {DirSep, AltSep}:
+        if result.len > 0 and result[result.len - 1] notin {DirSep, AltSep}:
           result.add dirSep
         result.add substr(x, b[0], b[1])
     elif isDot(x, b):
       discard "discard the dot"
     elif b[1] >= b[0]:
-      if result.len > 0 and result[^1] notin {DirSep, AltSep}:
+      if result.len > 0 and result[result.len - 1] notin {DirSep, AltSep}:
         result.add dirSep
       result.add substr(x, b[0], b[1])
       inc state, 2
diff --git a/lib/system.nim b/lib/system.nim
index fd5eb2dac..1104cb17f 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2579,6 +2579,7 @@ proc `[]`*[T](s: var openArray[T]; i: BackwardsIndex): var T {.inline.} =
   system.`[]`(s, s.len - int(i))
 proc `[]`*[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T {.inline.} =
   a[Idx(a.len - int(i) + int low(a))]
+proc `[]`*(s: var string; i: BackwardsIndex): var char {.inline.} = s[s.len - int(i)]
 
 proc `[]=`*[T](s: var openArray[T]; i: BackwardsIndex; x: T) {.inline.} =
   system.`[]=`(s, s.len - int(i), x)
diff --git a/tests/stdlib/tstring.nim b/tests/stdlib/tstring.nim
index 0a7bf0511..ff3d41b49 100644
--- a/tests/stdlib/tstring.nim
+++ b/tests/stdlib/tstring.nim
@@ -91,3 +91,10 @@ proc tester[T](x: T) =
 
 tester(1)
 
+# #14497 
+func reverse*(a: string): string =
+  result = a
+  for i in 0 ..< a.len div 2:
+    swap(result[i], result[^(i + 1)])
+
+doAssert reverse("hello") == "olleh"