Class: Racket::Misc::LV

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/misc/lv.rb

Overview

consist of an arbitrary number of length value pairs.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#lengthsObject

The lengths of the values parsed from this LV



36
37
38
# File 'lib/racket/misc/lv.rb', line 36

def lengths
  @lengths
end

#restObject

everything else



38
39
40
# File 'lib/racket/misc/lv.rb', line 38

def rest
  @rest
end

#valuesObject

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

#encodeObject



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_sObject



81
82
83
# File 'lib/racket/misc/lv.rb', line 81

def to_s
  encode
end

#to_strObject



85
86
87
# File 'lib/racket/misc/lv.rb', line 85

def to_str
  encode
end