Class: Racket::Misc::VT

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

Overview

arbitrarily sized values, followed by a “rest” field.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ VT

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



44
45
46
47
48
# File 'lib/racket/misc/vt.rb', line 44

def initialize(*args)
  @lengths = args
  @types = []
  @value = ""
end

Instance Attribute Details

#restObject

everything else



39
40
41
# File 'lib/racket/misc/vt.rb', line 39

def rest
  @rest
end

#typesObject

the array of types for this VT object



37
38
39
# File 'lib/racket/misc/vt.rb', line 37

def types
  @types
end

#valueObject

the value for this VT object



35
36
37
# File 'lib/racket/misc/vt.rb', line 35

def value
  @value
end

Instance Method Details

#decode(data) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#decode!(data) ⇒ Object

Given data, set the value and types array accordingly



70
71
72
# File 'lib/racket/misc/vt.rb', line 70

def decode!(data)
  @value, @types, @rest = self.decode(data)
end

#encodeObject

Return a string suitable for use elsewhere



75
76
77
78
79
80
81
82
83
84
# 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

#to_sObject



86
87
88
# File 'lib/racket/misc/vt.rb', line 86

def to_s
  encode
end

#to_strObject



90
91
92
# File 'lib/racket/misc/vt.rb', line 90

def to_str
  encode
end