Method: FFI::AbstractMemory#get_string

Defined in:
ext/ffi_c/AbstractMemory.c

#get_string(offset, length = nil) ⇒ String

Return string contained in memory.

Parameters:

  • offset (Integer)

    point in buffer to start from

  • length (Integer)

    string’s length in bytes. If nil, a (memory size - offset) length string is returned).

Returns:

  • (String)

Raises:

  • (IndexError)

    if length is too great

  • (NullPointerError)

    if memory not initialized



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'ext/ffi_c/AbstractMemory.c', line 416

static VALUE
memory_get_string(int argc, VALUE* argv, VALUE self)
{
    VALUE length = Qnil, offset = Qnil;
    AbstractMemory* ptr = MEMORY(self);
    long off, len;
    char* end;
    int nargs = rb_scan_args(argc, argv, "11", &offset, &length);

    off = NUM2LONG(offset);
    len = nargs > 1 && length != Qnil ? NUM2LONG(length) : (ptr->size - off);
    checkRead(ptr);
    checkBounds(ptr, off, len);

    end = memchr(ptr->address + off, 0, len);
    return rb_str_new((char *) ptr->address + off,
            (end != NULL ? end - ptr->address - off : len));
}