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


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

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


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

def static_length
  @static_length
end

#string::String (readonly)

Returns:

  • (::String)

Since:

  • 3.1.6 no more a subclass or regular String


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

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


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

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

#format_inspectObject

Since:

  • 3.1.6 no more a subclass or regular String


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

def format_inspect
  inspect
end

#initialize_copy(_orig) ⇒ Object

Since:

  • 3.1.6 no more a subclass or regular String


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

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


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

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


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

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


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

def sz_to_read
  return static_length if static_length?

  old_sz_to_read
end