class ByteRange < Struct
- Inherits from
Struct
Represents one byte-range-spec or suffix-byte-range-spec.
Definitions
def self.parse(value)
Parse one byte range.
Signature
-
parameter
valueString The byte range to parse.
-
returns
ByteRange The parsed byte range.
Implementation
def self.parse(value)
unless match = BYTE_RANGE.match(value)
raise ParseError, "Invalid byte range: #{value.inspect}"
end
if suffix = match[:suffix]
return self.new(nil, Integer(suffix))
else
first = Integer(match[:first])
last = match[:last]
last = last.empty? ? nil : Integer(last)
if last && last < first
raise ParseError, "Invalid byte range: #{value.inspect}"
end
return self.new(first, last)
end
end
def resolve(size)
Resolve this byte range against the selected representation size.
Signature
-
parameter
sizeInteger The size of the selected representation.
-
returns
::Range | Nil The resolved range, or
nilwhen it is unsatisfiable.
Implementation
def resolve(size)
if first
if first < size
return first..[last || size - 1, size - 1].min
end
elsif last > 0 && size > 0
return [0, size - last].max..size - 1
end
return nil
end
def to_s
Convert this byte range to its wire representation.
Signature
-
returns
String The serialized byte range.
Implementation
def to_s
if first
"#{first}-#{last}"
else
"-#{last}"
end
end