Class: BinData::String

Inherits:
BasePrimitive show all
Defined in:
lib/bindata/string.rb,
lib/bindata/deprecated.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.

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_value => 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

#==, accepted_parameters, #assign, #clear, #clear?, #debug_name, #debug_name_of, default_parameters, #eval_parameter, #get_parameter, #has_parameter?, #initialize, #inspect, mandatory_parameters, mutually_exclusive_parameters, #num_bytes, #offset, #offset_of, optional_parameters, #read, read, #single_value?, #snapshot, #to_binary_s, #to_s, #write

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

.deprecate!(params, old_key, new_key) ⇒ Object

:nodoc:



82
83
84
85
86
87
# File 'lib/bindata/deprecated.rb', line 82

def deprecate!(params, old_key, new_key) #:nodoc:
  if params.has_parameter?(old_key)
    warn ":#{old_key} is deprecated. Replacing with :#{new_key}"
    params[new_key] = params.delete(old_key)
  end
end

.orig_sanitize_parameters!Object



89
90
91
92
93
94
95
96
# File 'lib/bindata/deprecated.rb', line 89

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

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

.sanitize_parameters!(params, sanitizer) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
97
# File 'lib/bindata/deprecated.rb', line 90

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

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