Class Racket::Misc::LV
In: lib/racket/misc/lv.rb  (CVS)
Parent: Object

Simple class to represent data structures that consist of an arbitrary number of length value pairs.

Methods

decode   decode!   encode   new   to_s   to_str  

Attributes

lengths  [RW]  The lengths of the values parsed from this LV
rest  [RW]  everything else
values  [RW]  An array containing the values parsed from this LV

Public Class methods

Create a new LV object whose L sizes are specified in args

[Source]

# File lib/racket/misc/lv.rb, line 41
  def initialize(*args)
    @sizes = args
    @values = []
    @lengths = []
  end

Public Instance methods

[Source]

# 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

[Source]

# File lib/racket/misc/lv.rb, line 66
  def decode!(data)
    @lengths, @values, @rest = self.decode(data)
  end

[Source]

# 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

[Source]

# File lib/racket/misc/lv.rb, line 81
  def to_s
    encode
  end

[Source]

# File lib/racket/misc/lv.rb, line 85
  def to_str
    encode
  end

[Validate]