Method: CTypes::String.terminated

Defined in:
lib/ctypes/string.rb

.terminated(terminator = "\0") ⇒ Object

Return a CTypes::String type that is terminated by the supplied sequence

Examples:

null-terminated string

t = CTypes::String.terminated
t.unpack("hello world\0bye")    # => "hello world"

string terminated string

t = CTypes::String.terminated("STOP")
t.unpack("test 1STOPtest 2STOP")  # => "test 1"

Parameters:

  • terminator (::String) (defaults to: "\0")

    byte sequence to terminate the string



54
55
56
57
58
59
# File 'lib/ctypes/string.rb', line 54

def self.terminated(terminator = "\0")
  size = terminator.size
  Terminated.new(type: new,
    locate: proc { |b, _| [b.index(terminator), size] },
    terminate: terminator)
end