Class: BinStruct::CString
- Inherits:
-
Object
- Object
- BinStruct::CString
- Extended by:
- Forwardable
- Includes:
- Structable
- Defined in:
- lib/bin_struct/cstring.rb
Overview
This class handles null-terminated strings (aka C strings). # @example
cstr = BinStruct::CString.new(value: 'abcd')
cstr.to_s #=> "abcd\x00".b
Instance Attribute Summary collapse
-
#static_length ⇒ Integer?
readonly
Static length, if any.
-
#string ⇒ ::String
readonly
Underlying Ruby String.
Instance Method Summary collapse
-
#<<(str) ⇒ self
Append the given string to CString.
-
#== ⇒ Boolean
Check equality with underlying Ruby String.
- #[](index) ⇒ ::String?
-
#empty? ⇒ Boolean
Return
true
is string is empty. - #encode(encoding, **options) ⇒ ::String
- #encoding ⇒ Encoding
- #force_encoding ⇒ Object
-
#from_human(str) ⇒ self
Populate CString from a human readable string.
-
#index(substring, offset = 0) ⇒ Integer?
Returns the Integer index of the first occurrence of the given substring, or
nil
if none found. -
#initialize(options = {}) ⇒ CString
constructor
A new instance of CString.
-
#length ⇒ Integer
Return string length (without null-terminator).
-
#read(str) ⇒ self
Populate self from binary string.
-
#size ⇒ Integer
Return string length (without null-terminator).
-
#slice(*args) ⇒ String?
Returns the substring of
self
specified by the arguments. -
#slice!(*args) ⇒ String?
Removes the substring of
self
specified by the arguments; returns the removed substring. -
#static_length? ⇒ Boolean
Say if a static length is defined.
-
#sz ⇒ Integer
Get C String size in bytes.
-
#to_human ⇒ ::String
Get human-readable string.
-
#to_s ⇒ ::String
Get null-terminated string.
-
#unpack ⇒ ::Array
Apply unpack on underlying Ruby String.
Methods included from Structable
Constructor Details
#initialize(options = {}) ⇒ CString
Returns a new instance of CString.
83 84 85 86 |
# File 'lib/bin_struct/cstring.rb', line 83 def initialize( = {}) register_internal_string([:value] || +'') @static_length = [:static_length] end |
Instance Attribute Details
#static_length ⇒ Integer? (readonly)
Static length, if any
78 79 80 |
# File 'lib/bin_struct/cstring.rb', line 78 def static_length @static_length end |
#string ⇒ ::String (readonly)
Underlying Ruby String
75 76 77 |
# File 'lib/bin_struct/cstring.rb', line 75 def string @string end |
Instance Method Details
#<<(str) ⇒ self
Append the given string to CString
114 115 116 117 118 |
# File 'lib/bin_struct/cstring.rb', line 114 def <<(str) @string << str.to_s remove_null_character self end |
#== ⇒ Boolean
Check equality with underlying Ruby String
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#[](index) ⇒ ::String? #[](start, length) ⇒ ::String?
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#empty? ⇒ Boolean
Return true
is string is empty.
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#encode(encoding, **options) ⇒ ::String
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#encoding ⇒ Encoding
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#force_encoding ⇒ Object
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#from_human(str) ⇒ self
Populate CString from a human readable string
139 140 141 |
# File 'lib/bin_struct/cstring.rb', line 139 def from_human(str) read(str) end |
#index(substring, offset = 0) ⇒ Integer?
Returns the Integer index of the first occurrence of the given substring, or nil
if none found.
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#length ⇒ Integer
Return string length (without null-terminator)
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#read(str) ⇒ self
Populate self from binary string
91 92 93 94 95 96 97 |
# File 'lib/bin_struct/cstring.rb', line 91 def read(str) s = str.to_s s = s[0, static_length] if static_length? register_internal_string s remove_null_character self end |
#size ⇒ Integer
Return string length (without null-terminator)
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#slice(*args) ⇒ String?
Returns the substring of self
specified by the arguments.
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#slice!(*args) ⇒ String?
Removes the substring of self
specified by the arguments; returns the removed substring.
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#static_length? ⇒ Boolean
Say if a static length is defined
132 133 134 |
# File 'lib/bin_struct/cstring.rb', line 132 def static_length? !static_length.nil? end |
#sz ⇒ Integer
Get C String size in bytes
122 123 124 125 126 127 128 |
# File 'lib/bin_struct/cstring.rb', line 122 def sz if static_length? static_length else to_s.size end end |
#to_human ⇒ ::String
Get human-readable string
145 146 147 |
# File 'lib/bin_struct/cstring.rb', line 145 def to_human string end |
#to_s ⇒ ::String
Get null-terminated string
101 102 103 104 105 106 107 108 109 |
# File 'lib/bin_struct/cstring.rb', line 101 def to_s if static_length? s = string[0, static_length - 1] s << ("\x00" * (static_length - s.length)) else s = "#{string}\x00" end s.b end |
#unpack ⇒ ::Array
Apply unpack on underlying Ruby String
69 70 71 |
# File 'lib/bin_struct/cstring.rb', line 69 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |