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).
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.
79 80 81 82 |
# File 'lib/bin_struct/cstring.rb', line 79 def initialize( = {}) register_internal_string(+'') @static_length = [:static_length] end |
Instance Attribute Details
#static_length ⇒ Integer? (readonly)
Static length, if any
75 76 77 |
# File 'lib/bin_struct/cstring.rb', line 75 def static_length @static_length end |
#string ⇒ ::String (readonly)
Underlying Ruby String
72 73 74 |
# File 'lib/bin_struct/cstring.rb', line 72 def string @string end |
Instance Method Details
#<<(str) ⇒ self
Append the given string to CString
110 111 112 113 114 |
# File 'lib/bin_struct/cstring.rb', line 110 def <<(str) @string << str.to_s remove_null_character self end |
#== ⇒ Boolean
Check equality with underlying Ruby String
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#[](index) ⇒ ::String? #[](start, length) ⇒ ::String?
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#empty? ⇒ Boolean
Return true
is string is empty.
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#encode(encoding, **options) ⇒ ::String
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#encoding ⇒ Encoding
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#force_encoding ⇒ Object
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 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
135 136 137 |
# File 'lib/bin_struct/cstring.rb', line 135 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.
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#length ⇒ Integer
Return string length (without null-terminator)
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |
#read(str) ⇒ self
Populate self from binary string
87 88 89 90 91 92 93 |
# File 'lib/bin_struct/cstring.rb', line 87 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)
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 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.
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 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.
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 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
128 129 130 |
# File 'lib/bin_struct/cstring.rb', line 128 def static_length? !static_length.nil? end |
#sz ⇒ Integer
Get C String size in bytes
118 119 120 121 122 123 124 |
# File 'lib/bin_struct/cstring.rb', line 118 def sz if static_length? static_length else to_s.size end end |
#to_human ⇒ ::String
Get human-readable string
141 142 143 |
# File 'lib/bin_struct/cstring.rb', line 141 def to_human string end |
#to_s ⇒ ::String
Get null-terminated string
97 98 99 100 101 102 103 104 105 |
# File 'lib/bin_struct/cstring.rb', line 97 def to_s if static_length? s = string[0, static_length - 1] s << ("\x00" * (static_length - s.length)) else s = "#{string}\x00" end BinStruct.force_binary(s) end |
#unpack ⇒ ::Array
Apply unpack on underlying Ruby String
66 67 68 |
# File 'lib/bin_struct/cstring.rb', line 66 def_delegators :@string, :[], :length, :size, :inspect, :==, :unpack, :force_encoding, :encoding, :index, :empty?, :encode, :slice, :slice! |