fix: allow empty range subscripts [0..-1] and [0...0] on empty lists

Add special case in list_subscript native to return a new empty list when
the source list is empty and the requested range is [0..-1] (inclusive) or
[0...0] (exclusive). This enables using list[0..-1] as a universal copy
idiom that works even for empty lists. Update the List.+ operator to use
this idiom instead of manual iteration. Add test cases for both range
forms on empty lists.
This commit is contained in:
Bob Nystrom
2014-02-15 04:10:41 +00:00
parent 5f16a3a1cc
commit 08618abc5d
3 changed files with 18 additions and 8 deletions
+4
View File
@@ -29,3 +29,7 @@ IO.print(list[1..-2]) // expect: [b, c, d]
IO.print(list[2...-1]) // expect: [c, d]
IO.print(list[4..-5]) // expect: [e, d, c, b, a]
IO.print(list[3...-6]) // expect: [d, c, b, a]
// An empty range at zero is allowed on an empty list.
IO.print([][0...0]) // expect: []
IO.print([][0..-1]) // expect: []