Class: BinData::Stringz

Inherits:
BasePrimitive show all
Defined in:
lib/bindata/stringz.rb

Overview

A BinData::Stringz object is a container for a zero (“0”) terminated string.

For convenience, the zero terminator is not necessary when setting the value. Likewise, the returned value will not be zero terminated.

require 'bindata'

data = "abcd\x00efgh"

obj = BinData::Stringz.new
obj.read(data)
obj.snapshot #=> "abcd"
obj.num_bytes #=> 5
obj.to_binary_s #=> "abcd\000"

Parameters

Stringz objects accept all the params that BinData::BasePrimitive does, as well as the following:

:max_length

The maximum length of the string including the zero byte.

Instance Attribute Summary

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from BasePrimitive

#<=>, bit_aligned, #clear, #clear?, #do_num_bytes, #do_read, #do_read_with_check_value, #do_write, #eql?, #hash, #initialize_instance, #initialize_shared_instance, #method_missing, #respond_to?, turn_off_tracing, turn_on_tracing, #value

Methods inherited from Base

#==, #_assign, #_do_num_bytes, #_do_read, #_do_write, #_snapshot, arg_extractor, bindata_name, #clear, #clear?, #debug_name, #debug_name_of, #eval_parameter, #get_parameter, #has_parameter?, #initialize_instance, #initialize_with_deprecation, #inspect, #new, #num_bytes, #offset, #offset_of, #pretty_print, #read, read, register, register_self, register_subclasses, #rel_offset, #to_binary_s, #to_s, unregister_self, #write

Methods included from CheckOrAdjustOffsetMixin

#do_read_with_adjust_offset, #do_read_with_check_offset, included

Methods included from AcceptedParametersMixin

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BinData::BasePrimitive

Instance Method Details

#assign(val) ⇒ Object



31
32
33
34
# File 'lib/bindata/stringz.rb', line 31

def assign(val)
  val = val.dup.force_encoding(Encoding::BINARY) if RUBY_VERSION >= "1.9"
  super(val)
end

#snapshotObject



36
37
38
39
40
# File 'lib/bindata/stringz.rb', line 36

def snapshot
  # override to always remove trailing zero bytes
  result = super
  trim_and_zero_terminate(result).chomp("\0")
end