Class: BERT::Encode
- Inherits:
-
Object
show all
- Includes:
- Types
- Defined in:
- lib/bert/encode.rb
Constant Summary
Constants included
from Types
Types::ATOM, Types::BIN, Types::FLOAT, Types::FUN, Types::INT, Types::LARGE_BIGNUM, Types::LARGE_TUPLE, Types::LIST, Types::MAGIC, Types::MAX_INT, Types::MIN_INT, Types::NEW_FUN, Types::NIL, Types::SMALL_BIGNUM, Types::SMALL_INT, Types::SMALL_TUPLE, Types::STRING
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(out) ⇒ Encode
Returns a new instance of Encode.
7
8
9
|
# File 'lib/bert/encode.rb', line 7
def initialize(out)
self.out = out
end
|
Instance Attribute Details
#out ⇒ Object
Returns the value of attribute out.
5
6
7
|
# File 'lib/bert/encode.rb', line 5
def out
@out
end
|
Class Method Details
.encode(data) ⇒ Object
11
12
13
14
15
16
|
# File 'lib/bert/encode.rb', line 11
def self.encode(data)
io = StringIO.new
io.set_encoding('binary') if io.respond_to?(:set_encoding)
self.new(io).write_any(data)
io.string
end
|
Instance Method Details
#write_1(byte) ⇒ Object
36
37
38
|
# File 'lib/bert/encode.rb', line 36
def write_1(byte)
out.write([byte].pack("C"))
end
|
#write_2(short) ⇒ Object
40
41
42
|
# File 'lib/bert/encode.rb', line 40
def write_2(short)
out.write([short].pack("n"))
end
|
#write_4(long) ⇒ Object
44
45
46
|
# File 'lib/bert/encode.rb', line 44
def write_4(long)
out.write([long].pack("N"))
end
|
#write_any(obj) ⇒ Object
18
19
20
21
|
# File 'lib/bert/encode.rb', line 18
def write_any obj
write_1 MAGIC
write_any_raw obj
end
|
#write_any_raw(obj) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/bert/encode.rb', line 23
def write_any_raw obj
case obj
when Symbol then write_symbol(obj)
when Fixnum, Bignum then write_fixnum(obj)
when Float then write_float(obj)
when Tuple then write_tuple(obj)
when Array then write_list(obj)
when String then write_binary(obj)
else
fail(obj)
end
end
|
#write_bignum(num) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/bert/encode.rb', line 81
def write_bignum(num)
n = (num.abs.to_s(2).size / 8.0).ceil
if n < 256
write_1 SMALL_BIGNUM
write_1 n
write_bignum_guts(num)
else
write_1 LARGE_BIGNUM
write_4 n
write_bignum_guts(num)
end
end
|
#write_bignum_guts(num) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/bert/encode.rb', line 94
def write_bignum_guts(num)
write_1 (num >= 0 ? 0 : 1)
num = num.abs
while num != 0
rem = num % 256
write_1 rem
num = num >> 8
end
end
|
#write_binary(data) ⇒ Object
127
128
129
130
131
|
# File 'lib/bert/encode.rb', line 127
def write_binary(data)
write_1 BIN
write_4 data.bytesize
write_string data
end
|
#write_boolean(bool) ⇒ Object
52
53
54
|
# File 'lib/bert/encode.rb', line 52
def write_boolean(bool)
write_symbol(bool.to_s.to_sym)
end
|
#write_fixnum(num) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/bert/encode.rb', line 64
def write_fixnum(num)
if num >= 0 && num < 256
write_1 SMALL_INT
write_1 num
elsif num <= MAX_INT && num >= MIN_INT
write_1 INT
write_4 num
else
write_bignum num
end
end
|
#write_float(float) ⇒ Object
76
77
78
79
|
# File 'lib/bert/encode.rb', line 76
def write_float(float)
write_1 FLOAT
write_string format("%15.15e", float).ljust(31, "\000")
end
|
#write_list(data) ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/bert/encode.rb', line 118
def write_list(data)
fail(data) unless data.is_a? Array
write_1 NIL and return if data.empty?
write_1 LIST
write_4 data.length
data.each{|e| write_any_raw e }
write_1 NIL
end
|
#write_string(string) ⇒ Object
48
49
50
|
# File 'lib/bert/encode.rb', line 48
def write_string(string)
out.write(string)
end
|
#write_symbol(sym) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/bert/encode.rb', line 56
def write_symbol(sym)
fail(sym) unless sym.is_a?(Symbol)
data = sym.to_s
write_1 ATOM
write_2 data.bytesize
write_string data
end
|
#write_tuple(data) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/bert/encode.rb', line 104
def write_tuple(data)
fail(data) unless data.is_a? Array
if data.length < 256
write_1 SMALL_TUPLE
write_1 data.length
else
write_1 LARGE_TUPLE
write_4 data.length
end
data.each { |e| write_any_raw e }
end
|