Class: BinStruct::String
- Inherits:
-
Object
- Object
- BinStruct::String
- Extended by:
- Forwardable
- Includes:
- LengthFrom, Structable
- Defined in:
- lib/bin_struct/string.rb
Overview
This class mimics regular String, but it is Structable.
It may take its length from another field (LengthFrom capacity). It may also has a static length (i.e. string has always the same length, whatever its content is).
Constant Summary
Constants included from LengthFrom
Instance Attribute Summary collapse
-
#static_length ⇒ Integer
readonly
String static length, if set.
-
#string ⇒ ::String
readonly
Underlying Ruby String.
Instance Method Summary collapse
-
#<<(str) ⇒ self
Append the given string to String.
-
#format_inspect ⇒ ::String
Format String when inspecting from a Struct.
-
#initialize(options = {}) ⇒ String
constructor
A new instance of String.
-
#initialize_copy(_orig) ⇒ void
Initialize object on copying: * duplicate underlying Ruby String.
-
#read(str) ⇒ self
(also: #from_human)
Populate String from a binary String.
-
#static_length? ⇒ Boolean
Say if a static length is defined.
-
#sz_to_read ⇒ Integer
Size to read.
-
#to_s ⇒ ::String
(also: #to_human)
Generate “binary” string.
Methods included from LengthFrom
#initialize_length_from, #read_with_length_from
Methods included from Structable
Constructor Details
#initialize(options = {}) ⇒ String
Returns a new instance of String.
63 64 65 66 67 |
# File 'lib/bin_struct/string.rb', line 63 def initialize( = {}) register_internal_string([:value] || +'') initialize_length_from() @static_length = [:static_length] end |
Instance Attribute Details
#static_length ⇒ Integer (readonly)
String static length, if set
56 57 58 |
# File 'lib/bin_struct/string.rb', line 56 def static_length @static_length end |
#string ⇒ ::String (readonly)
Underlying Ruby String
53 54 55 |
# File 'lib/bin_struct/string.rb', line 53 def string @string end |
Instance Method Details
#<<(str) ⇒ self
Append the given string to String
112 113 114 115 |
# File 'lib/bin_struct/string.rb', line 112 def <<(str) @string << str.to_s.b self end |
#format_inspect ⇒ ::String
Format String when inspecting from a BinStruct::Struct
105 106 107 |
# File 'lib/bin_struct/string.rb', line 105 def format_inspect inspect end |
#initialize_copy(_orig) ⇒ void
This method returns an undefined value.
Initialize object on copying:
-
duplicate underlying Ruby String
72 73 74 |
# File 'lib/bin_struct/string.rb', line 72 def initialize_copy(_orig) @string = @string.dup end |
#read(str) ⇒ self Also known as: from_human
Populate String from a binary String. Limit length using LengthFrom or #static_length, if one is set.
79 80 81 82 83 |
# File 'lib/bin_struct/string.rb', line 79 def read(str) s = read_with_length_from(str) register_internal_string(s) self end |
#static_length? ⇒ Boolean
Say if a static length is defined
99 100 101 |
# File 'lib/bin_struct/string.rb', line 99 def static_length? !static_length.nil? end |
#sz_to_read ⇒ Integer
Size to read. Computed from #static_length or length_from
, if one defined.
91 92 93 94 95 |
# File 'lib/bin_struct/string.rb', line 91 def sz_to_read return static_length if static_length? old_sz_to_read end |
#to_s ⇒ ::String Also known as: to_human
Generate “binary” string
119 120 121 122 123 124 125 126 127 |
# File 'lib/bin_struct/string.rb', line 119 def to_s if static_length? s = @string[0, static_length] s << ("\x00" * (static_length - s.length)) s.b else @string.b end end |