Module: RLP::DecodeLazy
Constant Summary
Constants included from Utils
Constants included from Constant
Constant::BYTE_EMPTY, Constant::BYTE_ZERO, Constant::LIST_PREFIX_OFFSET, Constant::LONG_LENGTH_LIMIT, Constant::PRIMITIVE_PREFIX_OFFSET, Constant::SHORT_LENGTH_LIMIT
Instance Method Summary collapse
-
#consume_item_lazy(rlp, start) ⇒ Array
Read an item from an RLP string lazily.
-
#decode_lazy(rlp, sedes: nil, sedes_options: {}) ⇒ Object
Decode an RLP encoded object in a lazy fashion.
-
#peek(rlp, index, sedes: nil) ⇒ Object
Get a specific element from an rlp encoded nested list.
Methods included from Decode
Methods included from Utils
#big_endian_to_int, #bytes?, #bytes_to_str, #decode_hex, #encode_hex, #int_to_big_endian, #list?, make_immutable!, #primitive?, #str_to_bytes
Instance Method Details
#consume_item_lazy(rlp, start) ⇒ Array
Read an item from an RLP string lazily.
If the length prefix announces a string, the string is read; if it announces a list, a LazyList is created.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rlp/decode_lazy.rb', line 63 def consume_item_lazy(rlp, start) t, l, s = consume_length_prefix(rlp, start) if t == :str consume_payload(rlp, s, :str, l) elsif t == :list [LazyList.new(rlp, s, s+l), s+l] else raise "Invalid item type: #{t}" end end |
#decode_lazy(rlp, sedes: nil, sedes_options: {}) ⇒ Object
Decode an RLP encoded object in a lazy fashion.
If the encoded object is a byte string, this function acts similar to RLP::Decode#decode. If it is a list however, a LazyList is returned instead. This object will decode the string lazily, avoiding both horizontal and vertical traversing as much as possible.
The way ‘sedes` is applied depends on the decoded object: If it is a string `sedes` deserializes it as a whole; if it is a list, each element is deserialized individually. In both cases, `sedes_options` are passed on. Note that, if a deserializer is used, only “horizontal” but not “vertical lazyness” can be preserved.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rlp/decode_lazy.rb', line 31 def decode_lazy(rlp, sedes: nil, sedes_options: {}) item, next_start = consume_item_lazy(rlp, 0) raise DecodingError.new("RLP length prefix announced wrong length", rlp) if next_start != rlp.size if item.instance_of?(LazyList) item.sedes = sedes item. = item elsif sedes # FIXME: lazy man's kwargs .empty? ? sedes.deserialize(item) : sedes.deserialize(item, **) else item end end |
#peek(rlp, index, sedes: nil) ⇒ Object
Get a specific element from an rlp encoded nested list.
This method uses #decode_lazy and, thus, decodes only the necessary parts of the string.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rlp/decode_lazy.rb', line 93 def peek(rlp, index, sedes: nil) ll = decode_lazy(rlp) index = Array(index) index.each do |i| raise IndexError, "Too many indices given" if primitive?(ll) ll = ll.fetch(i) end sedes ? sedes.deserialize(ll) : ll end |