Class: BinStruct::String

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
LengthFrom, Structable
Defined in:
lib/bin_struct/string.rb

Overview

This class mimics regular String, but it is Structable.

Author:

  • Sylvain Daubert (2016-2024)

  • LemonTree55

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 Structable

#type_name

Constructor Details

#initialize(options = {}) ⇒ String

Returns a new instance of String.

Parameters:

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

Options Hash (options):

  • :length_from (Int, Proc)

    object or proc from which takes length when reading

  • :static_length (Integer)

    set a static length for this string

  • :value (::String)

    string value (default to )



36
37
38
39
40
# File 'lib/bin_struct/string.rb', line 36

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

Instance Attribute Details

#static_lengthInteger (readonly)

String static length, if set

Returns:

  • (Integer)


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

def static_length
  @static_length
end

#string::String (readonly)

Underlying Ruby String

Returns:

  • (::String)


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

def string
  @string
end

Instance Method Details

#<<(str) ⇒ self

Append the given string to String

Parameters:

Returns:

  • (self)


85
86
87
88
# File 'lib/bin_struct/string.rb', line 85

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

#format_inspect::String

Format String when inspecting from a BinStruct::Struct

Returns:

  • (::String)


78
79
80
# File 'lib/bin_struct/string.rb', line 78

def format_inspect
  inspect
end

#initialize_copy(_orig) ⇒ void

This method returns an undefined value.

Initialize object on copying:

  • duplicate underlying Ruby String



45
46
47
# File 'lib/bin_struct/string.rb', line 45

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

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

Populate String from a binary String. Limit length using LengthFrom or #static_length, if one is set.

Parameters:

  • str (::String)

Returns:

  • (self)


52
53
54
55
56
# File 'lib/bin_struct/string.rb', line 52

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)


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

def static_length?
  !static_length.nil?
end

#sz_to_readInteger

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

Returns:

  • (Integer)


64
65
66
67
68
# File 'lib/bin_struct/string.rb', line 64

def sz_to_read
  return static_length if static_length?

  old_sz_to_read
end

#to_s::String Also known as: to_human

Generate binary string

Returns:

  • (::String)


92
93
94
# File 'lib/bin_struct/string.rb', line 92

def to_s
  @string
end