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?, #do_num_bytes, #do_read, #do_read_with_hook, #do_write, #eql?, #hash, #initialize_instance, #initialize_shared_instance, #method_missing, #respond_to_missing?, #value, #value=

Methods included from TraceHook

#turn_off_tracing, #turn_on_tracing

Methods inherited from Base

#==, #=~, #abs_offset, arg_processor, auto_call_delayed_io, bindata_name, #clear, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_instance, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #pretty_print, #read, read, register_subclasses, #rel_offset, #safe_respond_to?, #to_binary_s, #to_hex, #to_s, unregister_self, #write

Methods included from AcceptedParametersPlugin

#accepted_parameters, #default_parameters, #mandatory_parameters, #mutually_exclusive_parameters, #optional_parameters

Methods included from RegisterNamePlugin

included, #initialize_shared_instance

Methods included from Framework

#bit_aligned?, #clear?, #debug_name_of, #offset_of

Dynamic Method Handling

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

Instance Method Details

#assign(val) ⇒ Object



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

def assign(val)
  super(binary_string(val))
end

#snapshotObject



34
35
36
37
38
# File 'lib/bindata/stringz.rb', line 34

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