Class: BinStruct::IntString
- Inherits:
-
Object
- Object
- BinStruct::IntString
- 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"
Instance Attribute Summary collapse
-
#string ⇒ ::String
internal string.
Instance Method Summary collapse
-
#calc_length ⇒ Integer
Set length from internal string length.
-
#empty? ⇒ Boolean
Say if IntString is empty.
-
#from_human(str) ⇒ self
Set from a human readable string.
-
#initialize(options = {}) ⇒ IntString
constructor
A new instance of IntString.
-
#length ⇒ Integer
Get length as registered in
IntLength. -
#length=(len) ⇒ Integer
Set length.
-
#read(str) ⇒ self
Populate IntString from a binary String.
-
#sz ⇒ Integer
Give binary string length (including
lengthattribute). -
#to_human ⇒ ::String
Get human readable string.
-
#to_s ⇒ ::String
Get binary string.
Methods included from Structable
Constructor Details
Instance Attribute Details
#string ⇒ ::String
internal string
26 27 28 |
# File 'lib/bin_struct/int_string.rb', line 26 def string @string end |
Instance Method Details
#calc_length ⇒ Integer
Set length from internal string length
98 99 100 |
# File 'lib/bin_struct/int_string.rb', line 98 def calc_length @length.from_human(@string.length) end |
#empty? ⇒ Boolean
Say if IntString is empty
110 111 112 |
# File 'lib/bin_struct/int_string.rb', line 110 def empty? length.zero? end |
#from_human(str) ⇒ self
Set from a human readable string
84 85 86 87 88 |
# File 'lib/bin_struct/int_string.rb', line 84 def from_human(str) @string.read(str) calc_length self end |
#length ⇒ Integer
Get length as registered in IntLength
63 64 65 |
# File 'lib/bin_struct/int_string.rb', line 63 def length @length.to_i end |
#length=(len) ⇒ Integer
Set length
56 57 58 59 |
# File 'lib/bin_struct/int_string.rb', line 56 def length=(len) @length.from_human(len) len # rubocop:disable Lint/Void end |
#read(str) ⇒ self
Populate IntString from a binary String
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bin_struct/int_string.rb', line 40 def read(str) return self if str.nil? str = str.b unless str[0, @length.width].size == @length.width raise Error, "String too short for #{@length.width}-byte length" end @length.read(str[0, @length.width]) @string.read(str[@length.width, @length.to_i]) self end |
#sz ⇒ Integer
Give binary string length (including length attribute)
104 105 106 |
# File 'lib/bin_struct/int_string.rb', line 104 def sz to_s.size end |
#to_human ⇒ ::String
Get human readable string
92 93 94 |
# File 'lib/bin_struct/int_string.rb', line 92 def to_human @string.to_s end |
#to_s ⇒ ::String
Get binary string
77 78 79 |
# File 'lib/bin_struct/int_string.rb', line 77 def to_s @length.to_s << @string.to_s end |