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

Simple class to represent a datastructure that is made up of a null terminted string followed by an arbitrary number of arbitrarily sized values, followed by a "rest" field.

Methods

decode   decode!   encode   new   to_s   to_str  

Attributes

rest  [RW]  everything else
types  [RW]  the array of types for this VT object
value  [RW]  the value for this VT object

Public Class methods

Create a new VT which consists of a null terminated string followed by some number of arbitrarily sized values, as specified by args

[Source]

# File lib/racket/misc/vt.rb, line 44
  def initialize(*args)
    @lengths = args
    @types = []
    @value = ""
  end

Public Instance methods

Given data, return the value and an array of the types as dictated by this instance

[Source]

# File lib/racket/misc/vt.rb, line 52
  def decode(data)
    null = data.index(0x00)
    value = data.unpack("a#{null}")[0]
    data = data.slice(null+1, data.length)
  
    n = 0
    types = []
    @lengths.each do |l|
      types[n] = data.unpack("#{punpack_string(l)}")[0]
      data = data.slice(l, data.length)
      n += 1
    end

    [value, types, data]
  end

Given data, set the value and types array accordingly

[Source]

# File lib/racket/misc/vt.rb, line 70
  def decode!(data)
    @value, @types, @rest = self.decode(data)
  end

Return a string suitable for use elsewhere

[Source]

# File lib/racket/misc/vt.rb, line 75
  def encode
    s = "#{@value}\000"

    n = 0
    @lengths.each do |l|
      s << [@types[n]].pack("#{punpack_string(l)}")
      n += 1
    end
    s
  end

[Source]

# File lib/racket/misc/vt.rb, line 86
  def to_s
    encode
  end

[Source]

# File lib/racket/misc/vt.rb, line 90
  def to_str
    encode
  end

[Validate]