Class: PacketGen::Types::String

Inherits:
String
  • Object
show all
Includes:
LengthFrom
Defined in:
lib/packetgen/types/string.rb

Overview

This class is just like regular String. It only adds #read and #sz methods to be compatible with others PacketGen::Types.

Author:

  • Sylvain Daubert

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LengthFrom

#initialize_length_from, #read_with_length_from

Constructor Details

#initialize(options = {}) ⇒ String

Returns a new instance of String.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :length_from (Types::Int, Proc)

    object or proc from which takes length when reading

  • :static_length (Integer)

    set a static length for this string



24
25
26
27
28
# File 'lib/packetgen/types/string.rb', line 24

def initialize(options={})
  super()
  initialize_length_from(options)
  @static_length = options[:static_length]
end

Instance Attribute Details

#static_lengthInteger (readonly)

Returns:

  • (Integer)


18
19
20
# File 'lib/packetgen/types/string.rb', line 18

def static_length
  @static_length
end

Instance Method Details

#read(str) ⇒ String Also known as: from_human

Returns self.

Parameters:

  • str (::String)

Returns:



32
33
34
35
36
37
# File 'lib/packetgen/types/string.rb', line 32

def read(str)
  s = read_with_length_from(str)
  s = s[0, static_length] if static_length
  self.replace(s)
  self
end