Module: Twilic::Core::Wire
- Defined in:
- lib/twilic/core/wire.rb
Defined Under Namespace
Classes: Reader
Class Method Summary collapse
- .append_f64_le(out, v) ⇒ Object
- .append_u64_le(out, v) ⇒ Object
- .decode_zigzag(value) ⇒ Object
- .encode_bitmap(bits, out) ⇒ Object
- .encode_bytes(bytes, out) ⇒ Object
- .encode_string(value, out) ⇒ Object
- .encode_varuint(value, out) ⇒ Object
- .encode_zigzag(value) ⇒ Object
- .read_f64_le(reader) ⇒ Object
- .read_u64_le(reader) ⇒ Object
Class Method Details
.append_f64_le(out, v) ⇒ Object
153 154 155 |
# File 'lib/twilic/core/wire.rb', line 153 def append_f64_le(out, v) append_u64_le(out, [v].pack("E").unpack1("Q<")) end |
.append_u64_le(out, v) ⇒ Object
149 150 151 |
# File 'lib/twilic/core/wire.rb', line 149 def append_u64_le(out, v) out << [v].pack("Q<") end |
.decode_zigzag(value) ⇒ Object
28 29 30 31 32 |
# File 'lib/twilic/core/wire.rb', line 28 def decode_zigzag(value) ((value >> 1) ^ (-(value & 1))) & 0xFFFFFFFFFFFFFFFF v = (value >> 1) ^ (-(value & 1)) v >= 0x8000000000000000 ? v - 0x10000000000000000 : v end |
.encode_bitmap(bits, out) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/twilic/core/wire.rb', line 43 def encode_bitmap(bits, out) encode_varuint(bits.length, out) current = 0 bits.each_with_index do |bit, i| current |= (1 << (i % 8)) if bit if (i % 8) == 7 out << current.chr current = 0 end end out << current.chr unless bits.empty? || (bits.length % 8).zero? end |
.encode_bytes(bytes, out) ⇒ Object
34 35 36 37 |
# File 'lib/twilic/core/wire.rb', line 34 def encode_bytes(bytes, out) encode_varuint(bytes.bytesize, out) out << bytes end |
.encode_string(value, out) ⇒ Object
39 40 41 |
# File 'lib/twilic/core/wire.rb', line 39 def encode_string(value, out) encode_bytes(value.b, out) end |
.encode_varuint(value, out) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/twilic/core/wire.rb', line 10 def encode_varuint(value, out) if value < 0x80 out << value.chr return end loop do b = value & 0x7F value >>= 7 b |= 0x80 unless value.zero? out << b.chr break if value.zero? end end |
.encode_zigzag(value) ⇒ Object
24 25 26 |
# File 'lib/twilic/core/wire.rb', line 24 def encode_zigzag(value) ((value << 1) ^ (value >> 63)) & 0xFFFFFFFFFFFFFFFF end |
.read_f64_le(reader) ⇒ Object
145 146 147 |
# File 'lib/twilic/core/wire.rb', line 145 def read_f64_le(reader) reader.read_f64_le end |
.read_u64_le(reader) ⇒ Object
141 142 143 |
# File 'lib/twilic/core/wire.rb', line 141 def read_u64_le(reader) reader.read_u64_le end |