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



35
36
37
38
39
# File 'lib/bin_struct/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)

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)


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

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

#format_inspect::String

Format String when inspecting from a BinStruct::Struct

Returns:

  • (::String)


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

def format_inspect
  inspect
end

#initialize_copy(_orig) ⇒ void

This method returns an undefined value.

Initialize object on copying:

  • duplicate underlying Ruby String



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

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)


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

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)


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

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)


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

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)


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

def to_s
  @string
end