Class: ArrayBuffer
- Inherits:
-
Object
- Object
- ArrayBuffer
- Defined in:
- ext/type_array/array_buffer.c
Class Method Summary collapse
-
.new(obj) ⇒ Object
Creates a new ArrayBuffer instance.
Instance Method Summary collapse
-
#byte_length ⇒ Fixnum
Returns the size of the buffer.
-
#slice(1) ⇒ ArrayBuffer
Returns a new ArrayBuffer instance with a slice (copy) of a range of memory managed by the source buffer.
-
#to_s ⇒ String
Returns a String (binary) representation of the buffer.
Class Method Details
.new(8) ⇒ ArrayBuffer .new("buffer") ⇒ ArrayBuffer
Creates a new ArrayBuffer instance. Both length and data (String) constructors are supported.
Examples
buf = ArrayBuffer.new(8) => ArrayBuffer
buf.byte_length => 8
buf = ArrayBuffer.new("buffer") => ArrayBuffer
buf.byte_length => 6
buf.to_s => "buffer"
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'ext/type_array/array_buffer.c', line 87
static VALUE rb_array_buffer_s_new(VALUE klass, VALUE obj)
{
VALUE buffer;
if (FIXNUM_P(obj)) {
buffer = rb_alloc_array_buffer(FIX2ULONG(obj), NULL);
} else if (rb_type(obj) == T_STRING) {
ArrayBufferEncode(obj);
buffer = rb_alloc_array_buffer((unsigned long)RSTRING_LEN(obj), (void *)RSTRING_PTR(obj));
} else {
rb_raise(rb_eTypeError, "ArrayBuffer constructor %s not supported.", RSTRING_PTR(rb_obj_as_string(obj)));
}
return buffer;
}
|
Instance Method Details
#byte_length ⇒ Fixnum
Returns the size of the buffer.
Examples
buf = ArrayBuffer.new(8) => ArrayBuffer
buf.byte_length => 8
112 113 114 115 116 |
# File 'ext/type_array/array_buffer.c', line 112
static VALUE rb_array_buffer_byte_length(VALUE obj)
{
GetArrayBuffer(obj);
return ULONG2NUM(buf->length);
}
|
#slice(1) ⇒ ArrayBuffer
Returns a new ArrayBuffer instance with a slice (copy) of a range of memory managed by the source buffer.
Examples
buf = ArrayBuffer.new(8) => ArrayBuffer
buf.slice(2) => ArrayBuffer
buf.slice(4,6) => ArrayBuffer
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'ext/type_array/array_buffer.c', line 130
static VALUE rb_array_buffer_slice(int argc, VALUE *argv, VALUE obj)
{
VALUE buffer, begin, end;
long b = 0;
long e = 0;
GetArrayBuffer(obj);
rb_scan_args(argc, argv, "11", &begin, &end);
Check_Type(begin, T_FIXNUM);
b = FIX2LONG(begin);
if (NIL_P(end)) {
e = buf->length;
} else {
Check_Type(end, T_FIXNUM);
e = FIX2LONG(end);
}
if (b < 0) b = buf->length - abs(b);
if (e < 0) e = buf->length - abs(e);
if (e > buf->length) rb_raise(rb_eRangeError, "Offset out of range.");
buffer = rb_copy_array_buffer(buf, b, e);
return buffer;
}
|
#to_s ⇒ String
Returns a String (binary) representation of the buffer.
Examples
buf = ArrayBuffer.new("buffer") => ArrayBuffer
buf.byte_length => 6
buf.to_s => "buffer"
165 166 167 168 169 170 171 172 |
# File 'ext/type_array/array_buffer.c', line 165
VALUE rb_array_buffer_to_s(VALUE obj)
{
VALUE str;
GetArrayBuffer(obj);
str = rb_str_new((const char*)buf->buf, buf->length);
ArrayBufferEncode(str);
return rb_obj_freeze(str);
}
|