Class: Racket::Misc::LV
- Inherits:
-
Object
- Object
- Racket::Misc::LV
- Defined in:
- lib/racket/misc/lv.rb
Overview
consist of an arbitrary number of length value pairs.
Instance Attribute Summary collapse
-
#lengths ⇒ Object
The lengths of the values parsed from this LV.
-
#rest ⇒ Object
everything else.
-
#values ⇒ Object
An array containing the values parsed from this LV.
Instance Method Summary collapse
- #decode(data) ⇒ Object
- #decode!(data) ⇒ Object
- #encode ⇒ Object
-
#initialize(*args) ⇒ LV
constructor
Create a new LV object whose L sizes are specified in
args
. - #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(*args) ⇒ LV
Create a new LV object whose L sizes are specified in args
41 42 43 44 45 |
# File 'lib/racket/misc/lv.rb', line 41 def initialize(*args) @sizes = args @values = [] @lengths = [] end |
Instance Attribute Details
#lengths ⇒ Object
The lengths of the values parsed from this LV
36 37 38 |
# File 'lib/racket/misc/lv.rb', line 36 def lengths @lengths end |
#rest ⇒ Object
everything else
38 39 40 |
# File 'lib/racket/misc/lv.rb', line 38 def rest @rest end |
#values ⇒ Object
An array containing the values parsed from this LV
34 35 36 |
# File 'lib/racket/misc/lv.rb', line 34 def values @values end |
Instance Method Details
#decode(data) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/racket/misc/lv.rb', line 48 def decode(data) n = 0 values = [] lengths = [] @sizes.each do |s| # XXX: raise an error here if there is not enough data to # unpack this next LV lengths[n] = data.unpack("#{punpack_string(s)}")[0] data = data.slice(s, data.length) values[n] = data.unpack("a#{lengths[n]}")[0] data = data.slice(lengths[n], data.length) n += 1 end # data now contains "rest" [lengths, values, data] end |
#decode!(data) ⇒ Object
66 67 68 |
# File 'lib/racket/misc/lv.rb', line 66 def decode!(data) @lengths, @values, @rest = self.decode(data) end |
#encode ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/racket/misc/lv.rb', line 70 def encode n = 0 s = "" @lengths.each do |l| s << [l].pack("#{punpack_string(@sizes[n])}") s << [@values[n]].pack("a#{l}") n += 1 end s end |
#to_s ⇒ Object
81 82 83 |
# File 'lib/racket/misc/lv.rb', line 81 def to_s encode end |
#to_str ⇒ Object
85 86 87 |
# File 'lib/racket/misc/lv.rb', line 85 def to_str encode end |