Class: BinData::String

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

Overview

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

require 'bindata'

data = "abcdefghij"

obj = BinData::String.new(:read_length => 5)
obj.read(data)
obj.value #=> "abcde"

obj = BinData::String.new(:length => 6)
obj.read(data)
obj.value #=> "abcdef"
obj.value = "abcdefghij"
obj.value #=> "abcdef"
obj.value = "abcd"
obj.value #=> "abcd\000\000"

obj = BinData::String.new(:length => 6, :trim_padding => true)
obj.value = "abcd"
obj.value #=> "abcd"
obj.to_binary_s #=> "abcd\000\000"

obj = BinData::String.new(:length => 6, :pad_char => 'A')
obj.value = "abcd"
obj.value #=> "abcdAA"
obj.to_binary_s #=> "abcdAA"

Parameters

String objects accept all the params that BinData::BasePrimitive 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_padding

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 Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Methods inherited from BasePrimitive

#clear, #clear?, #eql?, #hash, #initialize, #method_missing, #respond_to?, #value, #value=

Methods inherited from Base

#==, #assign, #clear, #clear?, #debug_name, #debug_name_of, #eval_parameter, #get_parameter, #has_parameter?, #initialize, #inspect, #num_bytes, #offset, #offset_of, #pretty_print, read, #read, register, register_class, register_self, register_subclasses, #rel_offset, #snapshot, #to_binary_s, #to_s, #write

Methods included from AcceptedParametersMixin

included

Constructor Details

This class inherits a constructor from BinData::BasePrimitive

Dynamic Method Handling

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

Class Method Details

.sanitize_parameters!(params, sanitizer) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
66
# File 'lib/bindata/string.rb', line 59

def sanitize_parameters!(params, sanitizer) #:nodoc:
  params.warn_replacement_parameter(:initial_length, :read_length)

  if params.has_parameter?(:pad_char)
    ch = params[:pad_char]
    params[:pad_char] = sanitized_pad_char(ch)
  end
end