fix: allow empty ranges at the end of a sequence for lists and strings

Previously, empty ranges were only permitted at index zero on an empty sequence.
This change extends the `calculateRange` function to allow empty ranges at the
end of a non-empty sequence, enabling `list[3...3]` and `list[3..-1]` to return
an empty slice. Updated the range validation logic to check `range->from == *length`
instead of requiring a zero-length sequence, and added test cases for both lists
and strings to verify the new behavior.
This commit is contained in:
Bob Nystrom
2015-12-06 18:37:58 +00:00
parent 320c804ecf
commit 9d2b4af508
3 changed files with 14 additions and 5 deletions
+4
View File
@@ -33,6 +33,10 @@ System.print(string[3...-6]) // expect: dcba
System.print(""[0...0] == "") // expect: true
System.print(""[0..-1] == "") // expect: true
// An empty range at the end is allowed on a string.
System.print("abc"[3...3] == "") // expect: true
System.print("abc"[3..-1] == "") // expect: true
// Indexes by byte, not code point.
//
// Bytes: 11111