Skip to content

Commit 14c011e

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 59f259e commit 14c011e

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

lib/net/imap/sequence_set.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ def slice_range(range)
12491249
remain_frozen_empty
12501250
elsif (min = at(first))
12511251
max = at(last)
1252+
max = :* if max.nil?
12521253
if max == :* then self & (min..)
12531254
elsif min <= max then self & (min..max)
12541255
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
@@ -296,6 +296,9 @@ def obj.to_sequence_set; 192_168.001_255 end
296296
SequenceSet[((1..10_000) % 10).to_a][-5, 4]
297297
assert_nil SequenceSet[111..222, 888..999][2000, 4]
298298
assert_nil SequenceSet[111..222, 888..999][-2000, 4]
299+
# with length longer than the remaining members
300+
assert_equal SequenceSet[101...200],
301+
SequenceSet[1...200][100, 10000]
299302
end
300303

301304
test "#[range]" do
@@ -322,6 +325,8 @@ def obj.to_sequence_set; 192_168.001_255 end
322325
assert_nil SequenceSet.empty[2..4]
323326
assert_nil SequenceSet[101..200][1000..1060]
324327
assert_nil SequenceSet[101..200][-1000..-60]
328+
# with length longer than the remaining members
329+
assert_equal SequenceSet[101..1111], SequenceSet[1..1111][100..999_999]
325330
end
326331

327332
test "#find_index" do

0 commit comments

Comments
 (0)