module PacketGen::Types::LengthFrom
This module is a mixin adding length_from
capacity to a type. length_from
capacity is the capacity, for a type, to gets its length from another object. @author Sylvain Daubert @since 3.0.0
Constants
- MAX_SZ_TO_READ
Max value returned by {#sz_to_read}.
Public Instance Methods
initialize_length_from(options)
click to toggle source
Initialize +length from+ capacity. Should be call by extensed object's initialize. @param [Hash] options @option options [Types::Int,Proc] :length_from object or proc from which
takes length when reading
@return [void]
# File lib/packetgen/types/length_from.rb, line 26 def initialize_length_from(options) @length_from = options[:length_from] end
read_with_length_from(str)
click to toggle source
Return a substring from str
of length given in another object. @param [#to_s] str @return [String]
# File lib/packetgen/types/length_from.rb, line 33 def read_with_length_from(str) s = PacketGen.force_binary(str.to_s) s[0, sz_to_read] end
sz_to_read()
click to toggle source
Size to read, from length_from @return [Integer]
# File lib/packetgen/types/length_from.rb, line 40 def sz_to_read len = case @length_from when Types::Int @length_from.to_i when Proc @length_from.call else MAX_SZ_TO_READ end [0, len].max end