class BinData::String
- not be trimmed when writing.
from the end of the string. The value will
return the value with all pad_bytes trimmed:trim_padding
- Boolean, default false. If set, #value will
is false.
of the string rather than the end. Default:pad_front
- Signifies that the padding occurs at the front
Strings of length 1. “0” is the default.
set length. Valid values are Integers and:pad_byte
- The byte to use when padding a string to a
string is set, it will be padded to this length.:length
- The fixed length of the string. If a shorter
:read_length
-
The length in bytes to use when reading a value.
does, as well as the following:
String objects accept all the params that BinData::BasePrimitive
== Parameters
obj.to_binary_s #=> “abcdAA”
obj #=> “abcdAA”
obj.assign(“abcd”)
obj = BinData::String.new(:length => 6, :pad_byte => ‘A’)
obj.to_binary_s #=> “abcd000000”
obj #=> “abcd”
obj.assign(“abcd”)
obj = BinData::String.new(:length => 6, :trim_padding => true)
obj #=> “abcd000000”
obj.assign(“abcd”)
obj #=> “abcdef”
obj.assign(“abcdefghij”)
obj #=> “abcdef”
obj.read(data)
obj = BinData::String.new(:length => 6)
obj #=> “abcde”
obj.read(data)
obj = BinData::String.new(:read_length => 5)
data = “abcdefghij”
require ‘bindata’
The issue of character encoding is ignored by this class.
A String is a sequence of bytes. This is the same as strings in Ruby 1.8.
- The fixed length of the string. If a shorter
- The byte to use when padding a string to a
- Signifies that the padding occurs at the front
- Boolean, default false. If set, #value will
def assign(val)
def assign(val) super(binary_string(val)) end
def clamp_to_length(str)
def clamp_to_length(str) str = binary_string(str) len = eval_parameter(:length) || str.length if str.length == len str elsif str.length > len str.slice(0, len) else padding = (eval_parameter(:pad_byte) * (len - str.length)) if get_parameter(:pad_front) padding + str else str + padding end end end
def read_and_return_value(io)
def read_and_return_value(io) len = eval_parameter(:read_length) || eval_parameter(:length) || 0 io.readbytes(len) end
def sanitize_parameters!(params) #:nodoc:
def sanitize_parameters!(params) #:nodoc: params.warn_replacement_parameter(:initial_length, :read_length) params.warn_renamed_parameter(:pad_char, :pad_byte) # Remove this line in the future if params.has_parameter?(:pad_left) params[:pad_front] = params.delete(:pad_left) end if params.has_parameter?(:pad_byte) byte = params[:pad_byte] params[:pad_byte] = sanitized_pad_byte(byte) end end
def sanitized_pad_byte(byte)
def sanitized_pad_byte(byte) result = byte.is_a?(Integer) ? byte.chr : byte.to_s len = result.respond_to?(:bytesize) ? result.bytesize : result.length if len > 1 raise ArgumentError, ":pad_byte must not contain more than 1 byte" end result end
def sensible_default
def sensible_default "" end
def snapshot
def snapshot # override to trim padding result = super result = clamp_to_length(result) if get_parameter(:trim_padding) result = trim_padding(result) end result end
def trim_padding(str)
def trim_padding(str) if get_parameter(:pad_front) str.sub(/\A#{eval_parameter(:pad_byte)}*/, "") else str.sub(/#{eval_parameter(:pad_byte)}*\z/, "") end end
def value_to_binary_string(val)
def value_to_binary_string(val) clamp_to_length(val) end