Class: ArrayBuffer

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

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'ext/arraybuffer/arraybuffer.c', line 84

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



99
100
101
102
103
104
105
106
107
# File 'ext/arraybuffer/arraybuffer.c', line 99

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



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

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)


166
167
168
169
170
# File 'ext/arraybuffer/arraybuffer.c', line 166

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

#eachObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/arraybuffer/arraybuffer.c', line 127

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



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/arraybuffer/arraybuffer.c', line 143

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



121
122
123
124
125
# File 'ext/arraybuffer/arraybuffer.c', line 121

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)


166
167
168
169
170
# File 'ext/arraybuffer/arraybuffer.c', line 166

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