Skip to content

Commit da1390c

Browse files
committed
🐛 Fix SequenceSet#slice when length > result size
The goal is for `#[]` (aliased as `#slice`) to behave similarly to `Array#[]`/`Array#slice`. When `Array#slice` has a length or range that extends beyond the end of the array, they simply return everything up to the end.
1 parent 33367bb commit da1390c

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

lib/net/imap/sequence_set.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,7 @@ def slice_range(range)
12531253
remain_frozen_empty
12541254
elsif (min = at(first))
12551255
max = at(last)
1256+
max = :* if max.nil?
12561257
if max == :* then self & (min..)
12571258
elsif min <= max then self & (min..max)
12581259
else remain_frozen_empty

test/net/imap/test_sequence_set.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ def obj.to_sequence_set; 192_168.001_255 end
293293
SequenceSet[((1..10_000) % 10).to_a][-5, 4]
294294
assert_nil SequenceSet[111..222, 888..999][2000, 4]
295295
assert_nil SequenceSet[111..222, 888..999][-2000, 4]
296+
# with length longer than the remaining members
297+
assert_equal SequenceSet[101...200],
298+
SequenceSet[1...200][100, 10000]
296299
end
297300

298301
test "#[range]" do
@@ -319,6 +322,8 @@ def obj.to_sequence_set; 192_168.001_255 end
319322
assert_nil SequenceSet.empty[2..4]
320323
assert_nil SequenceSet[101..200][1000..1060]
321324
assert_nil SequenceSet[101..200][-1000..-60]
325+
# with length longer than the remaining members
326+
assert_equal SequenceSet[101..1111], SequenceSet[1..1111][100..999_999]
322327
end
323328

324329
test "#find_index" do

0 commit comments

Comments
 (0)