Class: ArrayBuffer
- Inherits:
-
Object
- Object
- ArrayBuffer
- Includes:
- Enumerable
- Defined in:
- ext/arraybuffer/arraybuffer.c
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
-
#bytes ⇒ String
Returns a ASCII-8BIT string with the contents of the buffer.
- #each ⇒ Object
- #initialize(size) ⇒ Object constructor
- #realloc(_new_size) ⇒ Object
- #size ⇒ Object (also: #length)
Constructor Details
#initialize(size) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'ext/arraybuffer/arraybuffer.c', line 36
static VALUE
t_bb_initialize(VALUE self, VALUE size) {
DECLAREBB(self);
unsigned int s = NUM2UINT(size);
bb->size = s;
if (bb->ptr)
xfree(bb->ptr);
bb->ptr = xmalloc(s);
memset(bb->ptr, 0, (size_t)s);
return self;
}
|
Instance Method Details
#[](index) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'ext/arraybuffer/arraybuffer.c', line 48
static VALUE
t_bb_getbyte(VALUE self, VALUE index) {
DECLAREBB(self);
int idx = NUM2INT(index);
if (idx < 0)
idx += (int)bb->size;
CHECKBOUNDS(bb, idx);
return UINT2NUM((unsigned int)bb->ptr[idx]);
}
|
#[]=(index, value) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'ext/arraybuffer/arraybuffer.c', line 58
static VALUE
t_bb_setbyte(VALUE self, VALUE index, VALUE value) {
DECLAREBB(self);
int idx = NUM2INT(index);
unsigned int val = NUM2UINT(value);
if (idx < 0)
idx += (int)bb->size;
CHECKBOUNDS(bb, idx);
bb->ptr[idx] = (unsigned char)val;
return self;
}
|
#bytes ⇒ String
Returns a ASCII-8BIT string with the contents of the buffer
The returned string is a copy of the buffer. It’s encoding is always ASCII-8BIT. If the buffer has size zero, an empty string is returned.
137 138 139 140 141 142 143 |
# File 'ext/arraybuffer/arraybuffer.c', line 137
static VALUE
t_bb_bytes(VALUE self) {
DECLAREBB(self);
return rb_tainted_str_new(
(const char*)bb->ptr,
bb->size);
}
|
#each ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'ext/arraybuffer/arraybuffer.c', line 76
static VALUE
t_bb_each(VALUE self) {
DECLAREBB(self);
if (rb_block_given_p()) {
for (int i = 0; i < bb->size; i++) {
unsigned int val = (unsigned int)bb->ptr[i];
rb_yield(UINT2NUM(val));
}
} else {
rb_raise(rb_eArgError, "no block given");
}
return self;
}
|
#realloc(_new_size) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'ext/arraybuffer/arraybuffer.c', line 92
static VALUE
t_bb_realloc(VALUE self, VALUE _new_size) {
DECLAREBB(self);
unsigned int new_size = NUM2UINT(_new_size);
if (new_size == bb->size)
return self;
void *old_ptr = (void*)bb->ptr;
if (new_size > 0) {
char *new_ptr = (void*)xmalloc(new_size);
if (old_ptr) {
if (new_size > bb->size) {
size_t diff = (size_t)(new_size - bb->size);
memcpy(new_ptr, old_ptr, (size_t)bb->size);
xfree(old_ptr);
old_ptr = NULL;
memset((char*)new_ptr + (size_t)bb->size, 0, diff);
} else {
memcpy(new_ptr, old_ptr, (size_t)new_size);
}
} else {
memset(new_ptr, 0, new_size);
}
bb->size = new_size;
bb->ptr = (unsigned char*)new_ptr;
} else {
bb->size = 0;
bb->ptr = NULL;
}
if (old_ptr)
xfree(old_ptr);
return self;
}
|
#size ⇒ Object Also known as: length
70 71 72 73 74 |
# File 'ext/arraybuffer/arraybuffer.c', line 70
static VALUE
t_bb_size(VALUE self) {
DECLAREBB(self);
return UINT2NUM(bb->size);
}
|