Method: FFI::AbstractMemory#put_bytes

Defined in:
ext/ffi_c/AbstractMemory.c

#put_bytes(offset, str, index = 0, length = nil) ⇒ self

Put a string in memory.

Parameters:

  • offset (Integer)

    point in buffer to start from

  • str (String)

    string to put to memory

  • index (Integer)
  • length (Integer)

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

Returns:

  • (self)

Raises:

  • (IndexError)

    if length is too great

  • (NullPointerError)

    if memory not initialized

  • (RangeError)

    if index is negative, or if index+length is greater than size of string

  • (SecurityError)

    when writing unsafe string to memory



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'ext/ffi_c/AbstractMemory.c', line 572

static VALUE
memory_put_bytes(int argc, VALUE* argv, VALUE self)
{
    AbstractMemory* ptr = MEMORY(self);
    VALUE offset = Qnil, str = Qnil, rbIndex = Qnil, rbLength = Qnil;
    long off, len, idx;
    int nargs = rb_scan_args(argc, argv, "22", &offset, &str, &rbIndex, &rbLength);

    Check_Type(str, T_STRING);

    off = NUM2LONG(offset);
    idx = nargs > 2 ? NUM2LONG(rbIndex) : 0;
    if (idx < 0) {
        rb_raise(rb_eRangeError, "index cannot be less than zero");
        return Qnil;
    }
    len = nargs > 3 ? NUM2LONG(rbLength) : (RSTRING_LEN(str) - idx);
    if ((idx + len) > RSTRING_LEN(str)) {
        rb_raise(rb_eRangeError, "index+length is greater than size of string");
        return Qnil;
    }

    checkWrite(ptr);
    checkBounds(ptr, off, len);

    memcpy(ptr->address + off, RSTRING_PTR(str) + idx, len);

    return self;
}