Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breaking][debatable] change treatment of slices in strutils.delete & sequtils.delete #19127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ func delete*[T](s: var seq[T]; slice: Slice[int]) =
a.delete(1..<1) # empty slice
assert a == @[10, 13]
when compileOption("boundChecks"):
if not (slice.a < s.len and slice.a >= 0 and slice.b < s.len):
if not (slice.a <= s.len and slice.a >= 0 and slice.b < s.len and
slice.b - slice.a >= -1):
raise newException(IndexDefect, $(slice: slice, len: s.len))
if slice.b >= slice.a:
template defaultImpl =
Expand Down
5 changes: 3 additions & 2 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,8 @@ func delete*(s: var string, slice: Slice[int]) =
a.delete(1..<1) # empty slice
assert a == "ad"
when compileOption("boundChecks"):
if not (slice.a < s.len and slice.a >= 0 and slice.b < s.len):
if not (slice.a <= s.len and slice.a >= 0 and slice.b < s.len and
slice.b - slice.a >= -1):
raise newException(IndexDefect, $(slice: slice, len: s.len))
if slice.b >= slice.a:
var i = slice.a
Expand Down Expand Up @@ -2455,7 +2456,7 @@ func trimZeros*(x: var string; decimalSep = '.') =
var pos = last
while pos >= 0 and x[pos] == '0': dec(pos)
if pos > sPos: inc(pos)
x.delete(pos, last)
x.delete(pos .. last)

type
BinaryPrefixMode* = enum ## The different names for binary prefixes.
Expand Down
5 changes: 3 additions & 2 deletions tests/stdlib/tsequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ template main =
doAssertRaises(IndexDefect): a.delete(-1 .. -1)
doAssertRaises(IndexDefect): a.delete(5..5)
doAssertRaises(IndexDefect): a.delete(5..3)
doAssertRaises(IndexDefect): a.delete(5..<5) # edge case
a.delete(5..<5) # edge case
doAssert a == @[10, 11, 12, 13, 14]
a.delete(4..4)
doAssert a == @[10, 11, 12, 13]
Expand All @@ -518,7 +518,8 @@ template main =
a.delete(0..0)
doAssert a == @[]
doAssertRaises(IndexDefect): a.delete(0..0)
doAssertRaises(IndexDefect): a.delete(0..<0) # edge case
a.delete(0..<0)
doAssert a == @[] # edge case
block:
type A = object
a0: int
Expand Down
3 changes: 2 additions & 1 deletion tests/stdlib/tstrutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ template main() =
s = "abc"
doAssertRaises(IndexDefect): delete(s, -1 .. -2)
doAssertRaises(IndexDefect): delete(s, 2..3)
doAssertRaises(IndexDefect): delete(s, 3..2)
delete(s, 3..2)
doAssert s == "abc"
delete(s, 2..2)
doAssert s == "ab"
delete(s, 1..0)
Expand Down