Class: PacketGen::Types::String

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Fieldable, LengthFrom
Defined in:
lib/packetgen/types/string.rb

Overview

This class mimics regular String, but it is Fieldable.

Author:

  • Sylvain Daubert

Since:

  • 3.1.6 no more a subclass or regular String

Constant Summary

Constants included from LengthFrom

LengthFrom::MAX_SZ_TO_READ

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LengthFrom

#initialize_length_from, #read_with_length_from

Methods included from Fieldable

#to_s, #type_name

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

Since:

  • 3.1.6 no more a subclass or regular String



35
36
37
38
39
# File 'lib/packetgen/types/string.rb', line 35

def initialize(options={})
  register_internal_string(+'')
  initialize_length_from(options)
  @static_length = options[:static_length]
end

Instance Attribute Details

#static_lengthInteger (readonly)

Returns:

  • (Integer)

Since:

  • 3.1.6 no more a subclass or regular String



29
30
31
# File 'lib/packetgen/types/string.rb', line 29

def static_length
  @static_length
end

#string::String (readonly)

Returns:

  • (::String)

Since:

  • 3.1.6 no more a subclass or regular String



27
28
29
# File 'lib/packetgen/types/string.rb', line 27

def string
  @string
end

Instance Method Details

#<<(str) ⇒ self

Append the given string to String

Parameters:

Returns:

  • (self)

Since:

  • 3.1.6 no more a subclass or regular String



80
81
82
83
# File 'lib/packetgen/types/string.rb', line 80

def <<(str)
  @string << str.to_s
  self
end

#format_inspectObject

Since:

  • 3.1.6 no more a subclass or regular String



73
74
75
# File 'lib/packetgen/types/string.rb', line 73

def format_inspect
  inspect
end

#initialize_copy(_orig) ⇒ Object

Since:

  • 3.1.6 no more a subclass or regular String



41
42
43
# File 'lib/packetgen/types/string.rb', line 41

def initialize_copy(_orig)
  @string = @string.dup
end

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

Returns self.

Parameters:

  • str (::String)

Returns:

Since:

  • 3.1.6 no more a subclass or regular String



47
48
49
50
51
# File 'lib/packetgen/types/string.rb', line 47

def read(str)
  s = read_with_length_from(str)
  register_internal_string s
  self
end

#static_length?Boolean

Say if a static length is defined

Returns:

  • (Boolean)

Since:

  • 3.1.6



69
70
71
# File 'lib/packetgen/types/string.rb', line 69

def static_length?
  !static_length.nil?
end

#sz_to_readInteger

Size to read. Computed from static_length or length_from, if defined.

Returns:

  • (Integer)

Since:

  • 3.1.6



60
61
62
63
64
# File 'lib/packetgen/types/string.rb', line 60

def sz_to_read
  return static_length if static_length?

  old_sz_to_read
end