Class: CTypes::Terminated Private
- Inherits:
-
Object
- Object
- CTypes::Terminated
- Includes:
- Type
- Defined in:
- lib/ctypes/terminated.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Wrap another CTypes::Type to provide terminated implementations of that type. Used by Array and String to truncate the buffer passed to the real type to terminate greedy types.
During #unpack, this class will locate the terminator in the input buffer, then pass a truncated input to the underlying greedy type for unpacking.
During #pack, this class will call the underlying greedy type #pack method, then append the terminator.
Instance Attribute Summary
Attributes included from Type
Instance Method Summary collapse
- #dry_type ⇒ Object private
- #greedy? ⇒ Boolean private
-
#initialize(type:, locate:, terminate:) ⇒ Terminated
constructor
private
A new instance of Terminated.
- #pack(value, endian: default_endian, validate: true) ⇒ Object private
- #size ⇒ Object private
- #terminate(buf, endian:) ⇒ Object private
- #unpack_one(buf, endian: default_endian) ⇒ Object private
Methods included from Type
#default_endian, #default_value, #fixed_size?, #pread, #read, #unpack, #unpack_all, #with_endian, #without_endian
Constructor Details
#initialize(type:, locate:, terminate:) ⇒ Terminated
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Terminated.
22 23 24 25 26 |
# File 'lib/ctypes/terminated.rb', line 22 def initialize(type:, locate:, terminate:) @type = type @locate = locate @term = terminate end |
Instance Method Details
#dry_type ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
28 29 30 |
# File 'lib/ctypes/terminated.rb', line 28 def dry_type @type.dry_type end |
#greedy? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/ctypes/terminated.rb', line 32 def greedy? false end |
#pack(value, endian: default_endian, validate: true) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
41 42 43 44 |
# File 'lib/ctypes/terminated.rb', line 41 def pack(value, endian: default_endian, validate: true) buf = @type.pack(value, endian:, validate:) terminate(buf, endian:) end |
#size ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
36 37 38 39 |
# File 'lib/ctypes/terminated.rb', line 36 def size @term_size ||= terminate(@type.default_value.dup, endian: default_endian).size end |
#terminate(buf, endian:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
56 57 58 59 60 61 62 63 |
# File 'lib/ctypes/terminated.rb', line 56 def terminate(buf, endian:) buf << case @term when Proc @term.call(buf, endian) else @term.to_s end end |
#unpack_one(buf, endian: default_endian) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
46 47 48 49 50 51 52 53 54 |
# File 'lib/ctypes/terminated.rb', line 46 def unpack_one(buf, endian: default_endian) value_size, term_size = @locate.call(buf, endian:) if value_size.nil? raise TerminatorNotFoundError, "terminator not found in: %p" % buf end value = @type.unpack(buf[0, value_size], endian:) [value, buf.byteslice((value_size + term_size)..)] end |