diff options
author | Dominik Picheta <dominikpicheta@gmail.com> | 2017-11-26 19:31:59 +0000 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@gmail.com> | 2017-11-28 14:33:53 +0000 |
commit | b74a5148a9e7bf646ee6a13cad0ce046d7b9d8b4 (patch) | |
tree | 793bf81cee0e457d3d4de8bd476476c3dcc81e15 | |
parent | 8fbe37b2d813f00ca934cee2fe17bc2e24f82f88 (diff) | |
download | Nim-b74a5148a9e7bf646ee6a13cad0ce046d7b9d8b4.tar.gz |
Fixes #6223.
-rw-r--r-- | changelog.md | 3 | ||||
-rw-r--r-- | lib/pure/unicode.nim | 12 | ||||
-rw-r--r-- | lib/system.nim | 5 | ||||
-rw-r--r-- | tests/stdlib/tstring.nim | 4 |
4 files changed, 17 insertions, 7 deletions
diff --git a/changelog.md b/changelog.md index aaee99cfb..bd40d7e99 100644 --- a/changelog.md +++ b/changelog.md @@ -105,3 +105,6 @@ 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. \ No newline at end of file diff --git a/lib/pure/unicode.nim b/lib/pure/unicode.nim index 7d9c3108b..257c620f7 100644 --- a/lib/pure/unicode.nim +++ b/lib/pure/unicode.nim @@ -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 = [ diff --git a/lib/system.nim b/lib/system.nim index 387973f4b..b9f01c306 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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 diff --git a/tests/stdlib/tstring.nim b/tests/stdlib/tstring.nim index ddf533a17..904bc462a 100644 --- a/tests/stdlib/tstring.nim +++ b/tests/stdlib/tstring.nim @@ -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() |