Class: CTypes::Pad

Inherits:
Object
  • Object
show all
Includes:
Type
Defined in:
lib/ctypes/pad.rb

Overview

Generic type to represent a gap in the data structure. Unpacking this type will consume the pad size and return nil. Packing this type will return a string of null bytes of the appropriate size.

Examples:

t = Pad.new(4)
t.unpack_one("hello_world)  # => [nil, "o_world"]
t.pack("blahblahblah")      # => "\0\0\0\0"

Instance Attribute Summary collapse

Attributes included from Type

#dry_type, #endian

Instance Method Summary collapse

Methods included from Type

#default_endian, #default_value, #fixed_size?, #pread, #read, #unpack, #unpack_all, #with_endian, #without_endian

Constructor Details

#initialize(size) ⇒ Pad

Returns a new instance of Pad.



19
20
21
22
# File 'lib/ctypes/pad.rb', line 19

def initialize(size)
  @size = size
  @dry_type = Dry::Types::Any.default(nil)
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



23
24
25
# File 'lib/ctypes/pad.rb', line 23

def size
  @size
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
# File 'lib/ctypes/pad.rb', line 52

def ==(other)
  other.is_a?(self.class) && other.size == size
end

#export_type(q) ⇒ Object



48
49
50
# File 'lib/ctypes/pad.rb', line 48

def export_type(q)
  q << ".pad(%d)" % [@size]
end

#greedy?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/ctypes/pad.rb', line 35

def greedy?
  false
end

#pack(value, endian: default_endian, validate: true) ⇒ Object



25
26
27
# File 'lib/ctypes/pad.rb', line 25

def pack(value, endian: default_endian, validate: true)
  "\0" * @size
end

#pretty_print(q) ⇒ Object



43
44
45
# File 'lib/ctypes/pad.rb', line 43

def pretty_print(q)
  q.text("pad(%d)" % @size)
end

#to_sObject



39
40
41
# File 'lib/ctypes/pad.rb', line 39

def to_s
  "pad(%d)" % [@size]
end

#unpack_one(buf, endian: default_endian) ⇒ Object



29
30
31
32
33
# File 'lib/ctypes/pad.rb', line 29

def unpack_one(buf, endian: default_endian)
  raise missing_bytes_error(input: buf, need: @size) if
    @size && buf.size < @size
  [nil, buf.byteslice(@size..)]
end