Merge pull request #6817 from nim-lang/fixes-6223

Fixes #6223.
This commit is contained in:
Dominik Picheta
2017-11-29 00:32:07 +00:00
committed by GitHub
4 changed files with 17 additions and 7 deletions

View File

@@ -105,6 +105,9 @@ This now needs to be written as:
:test:
# shows how the 'if' statement works
if true: echo "yes"
- The ``[]`` proc for strings now raises an ``IndexError`` exception when
the specified slice is out of bounds. See issue
[#6223](https://github.com/nim-lang/Nim/issues/6223) for more details.
- ``strutils.split`` and ``strutils.rsplit`` with an empty string and a
separator now returns that empty string.
See issue [#4377](https://github.com/nim-lang/Nim/issues/4377).

View File

@@ -293,33 +293,33 @@ proc runeSubStr*(s: string, pos:int, len:int = int.high): string =
if pos < 0:
let (o, rl) = runeReverseOffset(s, -pos)
if len >= rl:
result = s[o.. s.len-1]
result = s.substr(o, s.len-1)
elif len < 0:
let e = rl + len
if e < 0:
result = ""
else:
result = s[o.. runeOffset(s, e-(rl+pos) , o)-1]
result = s.substr(o, runeOffset(s, e-(rl+pos) , o)-1)
else:
result = s[o.. runeOffset(s, len, o)-1]
result = s.substr(o, runeOffset(s, len, o)-1)
else:
let o = runeOffset(s, pos)
if o < 0:
result = ""
elif len == int.high:
result = s[o.. s.len-1]
result = s.substr(o, s.len-1)
elif len < 0:
let (e, rl) = runeReverseOffset(s, -len)
discard rl
if e <= 0:
result = ""
else:
result = s[o.. e-1]
result = s.substr(o, e-1)
else:
var e = runeOffset(s, len, o)
if e < 0:
e = s.len
result = s[o.. e-1]
result = s.substr(o, e-1)
const
alphaRanges = [

View File

@@ -3529,7 +3529,10 @@ when hasAlloc or defined(nimscript):
## .. code-block:: nim
## var s = "abcdef"
## assert s[1..3] == "bcd"
result = s.substr(s ^^ x.a, s ^^ x.b)
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

View File

@@ -50,6 +50,10 @@ proc test_string_slice() =
s[2..0] = numbers
doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
# bug #6223
doAssertRaises(IndexError):
discard s[0..999]
echo("OK")
test_string_slice()