Skip to content

Commit b74a514

Browse files
committed
Fixes #6223.
1 parent 8fbe37b commit b74a514

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ This now needs to be written as:
105105
:test:
106106
# shows how the 'if' statement works
107107
if true: echo "yes"
108+
- The ``[]`` proc for strings now raises an ``IndexError`` exception when
109+
the specified slice is out of bounds. See issue
110+
[#6223](https://github.com/nim-lang/Nim/issues/6223) for more details.

lib/pure/unicode.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,33 +293,33 @@ proc runeSubStr*(s: string, pos:int, len:int = int.high): string =
293293
if pos < 0:
294294
let (o, rl) = runeReverseOffset(s, -pos)
295295
if len >= rl:
296-
result = s[o.. s.len-1]
296+
result = s.substr(o, s.len-1)
297297
elif len < 0:
298298
let e = rl + len
299299
if e < 0:
300300
result = ""
301301
else:
302-
result = s[o.. runeOffset(s, e-(rl+pos) , o)-1]
302+
result = s.substr(o, runeOffset(s, e-(rl+pos) , o)-1)
303303
else:
304-
result = s[o.. runeOffset(s, len, o)-1]
304+
result = s.substr(o, runeOffset(s, len, o)-1)
305305
else:
306306
let o = runeOffset(s, pos)
307307
if o < 0:
308308
result = ""
309309
elif len == int.high:
310-
result = s[o.. s.len-1]
310+
result = s.substr(o, s.len-1)
311311
elif len < 0:
312312
let (e, rl) = runeReverseOffset(s, -len)
313313
discard rl
314314
if e <= 0:
315315
result = ""
316316
else:
317-
result = s[o.. e-1]
317+
result = s.substr(o, e-1)
318318
else:
319319
var e = runeOffset(s, len, o)
320320
if e < 0:
321321
e = s.len
322-
result = s[o.. e-1]
322+
result = s.substr(o, e-1)
323323

324324
const
325325
alphaRanges = [

lib/system.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3529,7 +3529,10 @@ when hasAlloc or defined(nimscript):
35293529
## .. code-block:: nim
35303530
## var s = "abcdef"
35313531
## assert s[1..3] == "bcd"
3532-
result = s.substr(s ^^ x.a, s ^^ x.b)
3532+
let a = s ^^ x.a
3533+
let L = (s ^^ x.b) - a + 1
3534+
result = newString(L)
3535+
for i in 0 ..< L: result[i] = s[i + a]
35333536
35343537
proc `[]=`*[T, U](s: var string, x: HSlice[T, U], b: string) =
35353538
## slice assignment for strings. If

tests/stdlib/tstring.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ proc test_string_slice() =
5050
s[2..0] = numbers
5151
doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
5252

53+
# bug #6223
54+
doAssertRaises(IndexError):
55+
discard s[0..999]
56+
5357
echo("OK")
5458

5559
test_string_slice()

0 commit comments

Comments
 (0)