Class: BinData::String

Inherits:
Single show all
Defined in:
lib/bindata/string.rb

Overview

A String is a sequence of bytes. This is the same as strings in Ruby. The issue of character encoding is ignored by this class.

Parameters

String objects accept all the params that BinData::Single does, as well as the following:

:read_length

The length to use when reading a value.

:length

The fixed length of the string. If a shorter string is set, it will be padded to this length.

:pad_char

The character to use when padding a string to a set length. Valid values are Integers and Strings of length 1. “0” is the default.

:trim_value

Boolean, default false. If set, #value will return the value with all pad_chars trimmed from the end of the string. The value will not be trimmed when writing.

Instance Method Summary collapse

Methods inherited from Single

#_do_read, #_num_bytes, #_write, #clear, #clear?, #done_read, #field_names, inherited, #snapshot, #value=

Methods inherited from Base

#accepted_parameters, #clear, #do_read, #done_read, #field_names, #inspect, #klass_lookup, lookup, mandatory_parameters, #num_bytes, optional_parameters, parameters, read, #read, register, #single_value?, #snapshot, #write

Constructor Details

#initialize(params = {}, env = nil) ⇒ String

Returns a new instance of String.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bindata/string.rb', line 27

def initialize(params = {}, env = nil)
  super(cleaned_params(params), env)

  # the only valid param combinations of length and value are:
  #   :read_length and :value
  #   :read_length and :initial_value
  #   :length and :initial_value
  ensure_mutual_exclusion(:initial_value, :value)
  ensure_mutual_exclusion(:read_length, :length)
  ensure_mutual_exclusion(:length, :value)
end

Instance Method Details

#valueObject

Overrides value to return the value padded to the desired length or trimmed as required.



41
42
43
44
45
# File 'lib/bindata/string.rb', line 41

def value
  v = val_to_str(_value)
  v.sub!(/#{eval_param(:pad_char)}*$/, "") if param(:trim_value) == true
  v
end