Class: VirtFS::ByteRange
- Inherits:
-
Object
- Object
- VirtFS::ByteRange
- Defined in:
- lib/virtfs/byte_range.rb
Overview
ByteRange utility class, encapsulate a range of bytes as given by their first / last offsets
Instance Attribute Summary collapse
-
#first ⇒ Object
Returns the value of attribute first.
-
#last ⇒ Object
Returns the value of attribute last.
Instance Method Summary collapse
- #adjacent?(*args) ⇒ Boolean
- #clear ⇒ Object
- #contiguous?(*args) ⇒ Boolean
- #empty? ⇒ Boolean
- #expand(*args) ⇒ Object
- #include?(obj) ⇒ Boolean
-
#initialize(first = nil, last = nil) ⇒ ByteRange
constructor
A new instance of ByteRange.
- #length ⇒ Object
- #overlap?(*args) ⇒ Boolean
- #set(first, last) ⇒ Object
Constructor Details
#initialize(first = nil, last = nil) ⇒ ByteRange
Returns a new instance of ByteRange.
7 8 9 |
# File 'lib/virtfs/byte_range.rb', line 7 def initialize(first = nil, last = nil) set(first, last) end |
Instance Attribute Details
#first ⇒ Object
Returns the value of attribute first.
5 6 7 |
# File 'lib/virtfs/byte_range.rb', line 5 def first @first end |
#last ⇒ Object
Returns the value of attribute last.
5 6 7 |
# File 'lib/virtfs/byte_range.rb', line 5 def last @last end |
Instance Method Details
#adjacent?(*args) ⇒ Boolean
26 27 28 29 30 |
# File 'lib/virtfs/byte_range.rb', line 26 def adjacent?(*args) return false if empty? nrange = range_arg(args) nrange.first == @last + 1 || nrange.last == @first - 1 end |
#clear ⇒ Object
49 50 51 |
# File 'lib/virtfs/byte_range.rb', line 49 def clear set(nil, nil) end |
#contiguous?(*args) ⇒ Boolean
38 39 40 41 |
# File 'lib/virtfs/byte_range.rb', line 38 def contiguous?(*args) nrange = range_arg(args) adjacent?(nrange) || overlap?(nrange) end |
#empty? ⇒ Boolean
11 12 13 |
# File 'lib/virtfs/byte_range.rb', line 11 def empty? @first.nil? || @last.nil? end |
#expand(*args) ⇒ Object
43 44 45 46 47 |
# File 'lib/virtfs/byte_range.rb', line 43 def (*args) nrange = range_arg(args) @first = nrange.first if empty? || nrange.first < @first @last = nrange.last if empty? || nrange.last > @last end |
#include?(obj) ⇒ Boolean
20 21 22 23 24 |
# File 'lib/virtfs/byte_range.rb', line 20 def include?(obj) return false if empty? return (obj.first >= @first && obj.last <= @last) if obj.is_a?(self.class) obj >= @first && obj <= @last end |
#length ⇒ Object
15 16 17 18 |
# File 'lib/virtfs/byte_range.rb', line 15 def length return 0 if empty? @last - @first + 1 end |
#overlap?(*args) ⇒ Boolean
32 33 34 35 36 |
# File 'lib/virtfs/byte_range.rb', line 32 def overlap?(*args) return false if empty? nrange = range_arg(args) include?(nrange.first) || include?(nrange.last) || nrange.include?(@first) || nrange.include?(@last) end |
#set(first, last) ⇒ Object
53 54 55 56 |
# File 'lib/virtfs/byte_range.rb', line 53 def set(first, last) @first = first @last = last end |