Class: ArrayBuffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/arraybuffer/arraybuffer.c

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'ext/arraybuffer/arraybuffer.c', line 94

static VALUE
t_bb_initialize(VALUE self, VALUE size) {
  DECLAREBB(self);
  unsigned int s = NUM2UINT(size);
  bb->size = s;
  if (bb->backing_str)
    rb_gc_mark(bb->backing_str);

  bb->backing_str = rb_str_buf_new(s);

  t_bb_reassign_ptr(bb);
  memset(bb->ptr, 0, (size_t)s);
  return self;
}

Instance Method Details

#[](index) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'ext/arraybuffer/arraybuffer.c', line 109

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



119
120
121
122
123
124
125
126
127
128
129
# File 'ext/arraybuffer/arraybuffer.c', line 119

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;
}

#bytesString

Returns a ASCII-8BIT string with the contents of the buffer

The returned string is the backing string of the buffer. It’s encoding is always ASCII-8BIT. If the buffer has size zero, an empty string is returned.

Returns:

  • (String)


176
177
178
179
180
# File 'ext/arraybuffer/arraybuffer.c', line 176

static VALUE
t_bb_bytes(VALUE self) {
  DECLAREBB(self);
  return bb->backing_str;
}

#eachObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'ext/arraybuffer/arraybuffer.c', line 137

static VALUE
t_bb_each(VALUE self) {
  DECLAREBB(self);

  if (rb_block_given_p()) {
    for (unsigned 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



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/arraybuffer/arraybuffer.c', line 153

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;

  rb_str_resize(bb->backing_str, new_size);
  bb->size = new_size;
  t_bb_reassign_ptr(bb);

  return self;
}

#sizeObject Also known as: length



131
132
133
134
135
# File 'ext/arraybuffer/arraybuffer.c', line 131

static VALUE
t_bb_size(VALUE self) {
  DECLAREBB(self);
  return UINT2NUM(bb->size);
}

#to_sString

Returns a ASCII-8BIT string with the contents of the buffer

The returned string is the backing string of the buffer. It’s encoding is always ASCII-8BIT. If the buffer has size zero, an empty string is returned.

Returns:

  • (String)


176
177
178
179
180
# File 'ext/arraybuffer/arraybuffer.c', line 176

static VALUE
t_bb_bytes(VALUE self) {
  DECLAREBB(self);
  return bb->backing_str;
}