class VirtFS::ByteRange
ByteRange
utility class, encapsulate a range of bytes as given by their first / last offsets
Attributes
first[RW]
last[RW]
Public Class Methods
new(first = nil, last = nil)
click to toggle source
# File lib/virtfs/byte_range.rb, line 7 def initialize(first = nil, last = nil) set(first, last) end
Public Instance Methods
adjacent?(*args)
click to toggle source
# 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()
click to toggle source
# File lib/virtfs/byte_range.rb, line 49 def clear set(nil, nil) end
contiguous?(*args)
click to toggle source
# File lib/virtfs/byte_range.rb, line 38 def contiguous?(*args) nrange = range_arg(args) adjacent?(nrange) || overlap?(nrange) end
empty?()
click to toggle source
# File lib/virtfs/byte_range.rb, line 11 def empty? @first.nil? || @last.nil? end
expand(*args)
click to toggle source
# File lib/virtfs/byte_range.rb, line 43 def expand(*args) nrange = range_arg(args) @first = nrange.first if empty? || nrange.first < @first @last = nrange.last if empty? || nrange.last > @last end
include?(obj)
click to toggle source
# 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()
click to toggle source
# File lib/virtfs/byte_range.rb, line 15 def length return 0 if empty? @last - @first + 1 end
overlap?(*args)
click to toggle source
# 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)
click to toggle source
# File lib/virtfs/byte_range.rb, line 53 def set(first, last) @first = first @last = last end
Private Instance Methods
range_arg(args)
click to toggle source
# File lib/virtfs/byte_range.rb, line 60 def range_arg(args) case args.length when 1 return args[0] when 2 return self.class.new(args[0], args[1]) else raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)" end end