Class: ZeroFormatter::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_formatter/utils.rb

Class Method Summary collapse

Class Method Details

.read_d8(bytes, index) ⇒ Object



41
42
43
# File 'lib/zero_formatter/utils.rb', line 41

def read_d8(bytes, index)
  bytes.byteslice(index, 8).unpack('E')[0]
end

.read_f4(bytes, index) ⇒ Object



37
38
39
# File 'lib/zero_formatter/utils.rb', line 37

def read_f4(bytes, index)
  bytes.byteslice(index, 4).unpack('e')[0]
end

.read_s1(bytes, index) ⇒ Object



4
5
6
# File 'lib/zero_formatter/utils.rb', line 4

def read_s1(bytes, index)
  to_signed(bytes.getbyte(index), 1 << (8 -1))
end

.read_s2(bytes, index) ⇒ Object



16
17
18
# File 'lib/zero_formatter/utils.rb', line 16

def read_s2(bytes, index)
  to_signed(read_u2(bytes, index), 1 << (16 - 1))
end

.read_s4(bytes, index) ⇒ Object



24
25
26
# File 'lib/zero_formatter/utils.rb', line 24

def read_s4(bytes, index)
  to_signed(read_u4(bytes, index), 1 << (32 - 1))
end

.read_s8(bytes, index) ⇒ Object



33
34
35
# File 'lib/zero_formatter/utils.rb', line 33

def read_s8(bytes, index)
  to_signed(read_u8(bytes, index), 1 << (64 - 1))
end

.read_u1(bytes, index) ⇒ Object



8
9
10
# File 'lib/zero_formatter/utils.rb', line 8

def read_u1(bytes, index)
  bytes.getbyte(index)
end

.read_u2(bytes, index) ⇒ Object



12
13
14
# File 'lib/zero_formatter/utils.rb', line 12

def read_u2(bytes, index)
  bytes.byteslice(index, 2).unpack('v')[0]
end

.read_u4(bytes, index) ⇒ Object



20
21
22
# File 'lib/zero_formatter/utils.rb', line 20

def read_u4(bytes, index)
  bytes.byteslice(index, 4).unpack('V')[0]
end

.read_u8(bytes, index) ⇒ Object



28
29
30
31
# File 'lib/zero_formatter/utils.rb', line 28

def read_u8(bytes, index)
  l, h = bytes.byteslice(index, 8).unpack('VV')
  (h << 32) + l
end

.to_signed(v, mask) ⇒ Object



45
46
47
# File 'lib/zero_formatter/utils.rb', line 45

def to_signed(v, mask)
  (v & ~mask) - (v & mask)
end

.write_d8(value) ⇒ Object



98
99
100
101
# File 'lib/zero_formatter/utils.rb', line 98

def write_d8(value)
  value ||= 0.0
  [value].pack("E")
end

.write_f4(value) ⇒ Object



93
94
95
96
# File 'lib/zero_formatter/utils.rb', line 93

def write_f4(value)
  value ||= 0.0
  [value].pack("e")
end

.write_s1(value) ⇒ Object



57
58
59
60
# File 'lib/zero_formatter/utils.rb', line 57

def write_s1(value)
  value ||= 0
  [value].pack("c")
end

.write_s2(value) ⇒ Object



67
68
69
70
# File 'lib/zero_formatter/utils.rb', line 67

def write_s2(value)
  value ||= 0
  [value].pack('v')
end

.write_s4(value) ⇒ Object



77
78
79
80
# File 'lib/zero_formatter/utils.rb', line 77

def write_s4(value)
  value ||= 0
  [value].pack('V')
end

.write_s8(value) ⇒ Object



87
88
89
90
91
# File 'lib/zero_formatter/utils.rb', line 87

def write_s8(value)
  value ||= 0
  value += (2 ** 64) if value < 0
  [value].pack('q!<')
end

.write_u1(value) ⇒ Object



52
53
54
55
# File 'lib/zero_formatter/utils.rb', line 52

def write_u1(value)
  value ||= 0
  [value].pack("C")
end

.write_u2(value) ⇒ Object



62
63
64
65
# File 'lib/zero_formatter/utils.rb', line 62

def write_u2(value)
  value ||= 0
  [value].pack('v')
end

.write_u4(value) ⇒ Object



72
73
74
75
# File 'lib/zero_formatter/utils.rb', line 72

def write_u4(value)
  value ||= 0
  [value].pack('V')
end

.write_u8(value) ⇒ Object



82
83
84
85
# File 'lib/zero_formatter/utils.rb', line 82

def write_u8(value)
  value ||= 0
  [value].pack('Q!<')
end