class PacketGen::Types::IntString
Provides a class for creating strings preceeded by their length as a {Int}. By default, a null string will have one byte length (length byte set to 0). @author Sylvain Daubert
Attributes
string[R]
internal string @return [String]
Public Class Methods
new(len_type=Int8, string: '')
click to toggle source
@param [Class] len_type should be a {Int} subclass @param [::String] string
# File lib/packetgen/types/int_string.rb, line 23 def initialize(len_type=Int8, string: '') @string = Types::String.new.read(string) @length = len_type.new calc_length end
Public Instance Methods
calc_length()
click to toggle source
Set length from internal string length @return [Integer]
# File lib/packetgen/types/int_string.rb, line 84 def calc_length @length.read @string.length end
empty?()
click to toggle source
Say if IntString
is empty @return [Boolean]
# File lib/packetgen/types/int_string.rb, line 96 def empty? length.zero? end
from_human(str)
click to toggle source
Set from a human readable string @param [String] str @return [self]
# File lib/packetgen/types/int_string.rb, line 69 def from_human(str) @string.read str calc_length self end
length()
click to toggle source
@return [Integer]
# File lib/packetgen/types/int_string.rb, line 49 def length @length.to_i end
length=(len)
click to toggle source
@param [Integer] len @return [Integer]
# File lib/packetgen/types/int_string.rb, line 43 def length=(len) @length.read len len end
read(str)
click to toggle source
@param [::String] str @return [IntString] self
# File lib/packetgen/types/int_string.rb, line 31 def read(str) unless str[0, @length.width].size == @length.width raise ParseError, "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
string=(str)
click to toggle source
@param [#to_s] str @return [String]
# File lib/packetgen/types/int_string.rb, line 55 def string=(str) @length.value = str.to_s.size @string = str.to_s end
sz()
click to toggle source
Give binary string length (including length
field) @return [Integer]
# File lib/packetgen/types/int_string.rb, line 90 def sz to_s.size end
to_human()
click to toggle source
Get human readable string @return [::String] @since 2.2.0
# File lib/packetgen/types/int_string.rb, line 78 def to_human @string end
to_s()
click to toggle source
Get binary string @return [::String]
# File lib/packetgen/types/int_string.rb, line 62 def to_s @length.to_s << @string.to_s end