Class: BinStruct::IntString

Inherits:
Object
  • Object
show all
Includes:
Structable
Defined in:
lib/bin_struct/int_string.rb

Overview

Provides a class for creating strings preceeded by their length as an Int. By default, a null string will have one byte length (length byte set to 0).

Examples

# IntString with 8-bit length
is8 = BinStruct::IntString.new(value: "abcd")
is8.to_s   # => "\x04abcd"
# IntString with 16-bit length
is16 = BinStruct::IntString.new(length_type: BinStruct::Int16le, value: "abcd")
is16.to_s  # => "\x04\x00abcd"

Author:

  • Sylvain Daubert (2016-2024)

  • LemonTree55

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Structable

#format_inspect, #type_name

Constructor Details

#initialize(options = {}) ⇒ IntString

Returns a new instance of IntString.

Parameters:

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

Options Hash (options):

  • :length_type (Class)

    should be a BinStruct::Int subclass. Default to BinStruct::Int8.

  • :value (::String)

    String value. Default to “”



31
32
33
34
35
# File 'lib/bin_struct/int_string.rb', line 31

def initialize(options = {})
  @string = BinStruct::String.new.read(options[:value] || +'')
  @length = (options[:length_type] || Int8).new
  calc_length
end

Instance Attribute Details

#string::String

internal string

Returns:

  • (::String)


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

def string
  @string
end

Instance Method Details

#calc_lengthInteger

Set length from internal string length

Returns:

  • (Integer)


97
98
99
# File 'lib/bin_struct/int_string.rb', line 97

def calc_length
  @length.from_human(@string.length)
end

#empty?Boolean

Say if IntString is empty

Returns:

  • (Boolean)


109
110
111
# File 'lib/bin_struct/int_string.rb', line 109

def empty?
  length.zero?
end

#from_human(str) ⇒ self

Set from a human readable string

Parameters:

  • str (::String)

Returns:

  • (self)


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

def from_human(str)
  @string.read(str)
  calc_length
  self
end

#lengthInteger

Get length as registered in IntLength

Returns:

  • (Integer)


62
63
64
# File 'lib/bin_struct/int_string.rb', line 62

def length
  @length.to_i
end

#length=(len) ⇒ Integer

Set length

Parameters:

  • len (Integer)

Returns:

  • (Integer)


53
54
55
56
57
58
# File 'lib/bin_struct/int_string.rb', line 53

def length=(len)
  @length.from_human(len)
  # rubocop:disable Lint/Void
  len
  # rubocop:enable Lint/Void
end

#read(str) ⇒ self

Populate IntString from a binary String

Parameters:

  • str (::String)

Returns:

  • (self)


40
41
42
43
44
45
46
47
48
# File 'lib/bin_struct/int_string.rb', line 40

def read(str)
  unless str[0, @length.width].size == @length.width
    raise Error,
          "String too short for type #{@length.class.to_s.gsub(/.*::/, '')}"
  end
  @length.read str[0, @length.width]
  @string.read str[@length.width, @length.to_i]
  self
end

#szInteger

Give binary string length (including length attribute)

Returns:

  • (Integer)


103
104
105
# File 'lib/bin_struct/int_string.rb', line 103

def sz
  to_s.size
end

#to_human::String

Get human readable string

Returns:

  • (::String)


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

def to_human
  @string.to_s
end

#to_s::String

Get binary string

Returns:

  • (::String)


76
77
78
# File 'lib/bin_struct/int_string.rb', line 76

def to_s
  @length.to_s << @string.to_s
end