class BinData::String
- not be trimmed when writing.
from the end of the string. The value will
return the value with all pad_chars trimmed:trim_value
- Boolean, default false. If set, #value will
Strings of length 1. “0” is the default.
set length. Valid values are Integers and:pad_char
- The character 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 to use when reading a value.
does, as well as the following:
String objects accept all the params that BinData::Single
== Parameters
obj.to_s #=> “abcdAA”
obj.value #=> “abcdAA”
obj.value = “abcd”
obj = BinData::String.new(:length => 6, :pad_char => ‘A’)
obj.to_s #=> “abcd000000”
obj.value #=> “abcd”
obj.value = “abcd”
obj = BinData::String.new(:length => 6, :trim_value => true)
obj.value #=> “abcd000000”
obj.value = “abcd”
obj.value #=> “abcdef”
obj.value = “abcdefghij”
obj.value #=> “abcdef”
obj.read(data)
obj = BinData::String.new(:length => 6)
obj.value #=> “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.
- The fixed length of the string. If a shorter
- The character to use when padding a string to a
- Boolean, default false. If set, #value will
def read_val(io)
def read_val(io) len = eval_param(:read_length) || eval_param(:length) || 0 io.readbytes(len) end
def sanitize_parameters!(sanitizer, params)
def sanitize_parameters!(sanitizer, params) # warn about deprecated param - remove before releasing 1.0 if params[:initial_length] warn ":initial_length is deprecated. Replacing with :read_length" params[:read_length] = params.delete(:initial_length) end # set :pad_char to be a single length character string if params.has_key?(:pad_char) ch = params[:pad_char] ch = ch.respond_to?(:chr) ? ch.chr : ch.to_s if ch.length > 1 raise ArgumentError, ":pad_char must not contain more than 1 char" end params[:pad_char] = ch end super(sanitizer, params) end
def sensible_default
def sensible_default "" end
def val_to_str(val)
def val_to_str(val) # trim val if necessary len = eval_param(:length) || val.length str = val.slice(0, len) # then pad to length if str is short str << (eval_param(:pad_char) * (len - str.length)) end
def value
Overrides value to return the value padded to the desired length or
def value v = val_to_str(_value) if no_eval_param(:trim_value) == true v.sub!(/#{eval_param(:pad_char)}*$/, "") end v end