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.

It may take its length from another field (LengthFrom capacity). It may also has a static length (i.e. string has always the same length, whatever its content is).

Examples:

Basic example

str = BinStruct::String.new
str.read("abc")
str.to_s #=> "abc".b

LengthFrom example

class StrLen < BinStruct::Struct
  define_attr :length, BinStruct::Int8
  define_attr :str, BinStruct::String, builder: ->(h, t) { t.new(length_from: h[:length]) }
end

# Length is 3, but rest of data is 4 byte long. Only 3 bytes will be read.
s = StrLen.new.read("\x03abcd")
s.length #=> 3
s.str.to_s #=> "abc".b
s.to_s # => "\x03abc".b

static length example

s = BinStruct::String.new(static_length: 10)
s.sz #=> 10
s.to_s #=> "\0\0\0\0\0\0\0\0\0\0".b
s.read("01234567890123456789")
s.to_s #=> "0123456789".b

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

#sz, #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 “”)



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

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)


56
57
58
# File 'lib/bin_struct/string.rb', line 56

def static_length
  @static_length
end

#string::String (readonly)

Underlying Ruby String

Returns:

  • (::String)


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

def string
  @string
end

Instance Method Details

#<<(str) ⇒ self

Append the given string to String

Parameters:

Returns:

  • (self)


112
113
114
115
# File 'lib/bin_struct/string.rb', line 112

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

#format_inspect::String

Format String when inspecting from a BinStruct::Struct

Returns:

  • (::String)


105
106
107
# File 'lib/bin_struct/string.rb', line 105

def format_inspect
  inspect
end

#initialize_copy(_orig) ⇒ void

This method returns an undefined value.

Initialize object on copying:

  • duplicate underlying Ruby String



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

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, nil)

Returns:

  • (self)


79
80
81
82
83
# File 'lib/bin_struct/string.rb', line 79

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)


99
100
101
# File 'lib/bin_struct/string.rb', line 99

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)


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

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)


119
120
121
122
123
124
125
126
127
# File 'lib/bin_struct/string.rb', line 119

def to_s
  if static_length?
    s = @string[0, static_length]
    s << ("\x00" * (static_length - s.length))
    s.b
  else
    @string.b
  end
end