Class: DataView

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

Instance Method Summary collapse

Constructor Details

#initialize(buffer, offset, size, endianess: ) ⇒ Object

Constructs a new DataView that provides a view of the data in a ArrayBuffer.

Example:

buffer = ArrayBuffer.new(10)
buffer[1] = 20
buffer[2] = 55
view = DataView.new(buffer, 1, endianess: :little)
view.getU8() == ( 20 & (55 << 8) ) # true

Or:

view = DataView.new(buffer, 1) # default endianess is "big endian"
view.getU8() == ( (20 << 8) | 55 ) # true


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/arraybuffer/dataview.c', line 109

static VALUE
t_dv_initialize(int argc, VALUE *argv, VALUE self) {
  DECLAREDV(self);
  VALUE bb_obj;
  VALUE offset;
  VALUE size;
  VALUE kwargs;
  static ID keyword_ids[] = { 0 };

  rb_scan_args(argc, argv, "12:", &bb_obj, &offset, &size, &kwargs);

  DECLAREBB(bb_obj);

  int size_val = NIL_P(size) ? (int)bb->size : NUM2INT(size);
  if (size_val < 0)
    size_val += (int)bb->size;
  if (size_val < 0)
    rb_raise(rb_eArgError, "calculated size is negative: %d", size_val);

  int offset_val = NIL_P(offset) ? 0 : NUM2INT(offset);
  if (offset_val < 0)
    offset_val += (int)bb->size;
  if (offset_val < 0)
    rb_raise(rb_eArgError, "calculated offset is negative: %d", offset_val);

  dv->offset = (unsigned int)offset_val;
  dv->size = (unsigned int)size_val;
  dv->bb_obj = bb_obj;

  if (!keyword_ids[0]) {
    keyword_ids[0] = idEndianess;
  }

  if (!NIL_P(kwargs)) {
    VALUE endianess;
    rb_get_kwargs(kwargs, keyword_ids, 0, 1, &endianess);
    if (endianess != Qundef) {
      Check_Type(endianess, T_SYMBOL);
      ID id = SYM2ID(endianess);
      if (id == idLittle)
        dv->flags |= FLAG_LITTLE_ENDIAN;
      else if (id != idBig)
        rb_raise(rb_eArgError, "endianess must be either :big or :little");
    }
  }

  return self;
}

Instance Method Details

#eachObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/arraybuffer/dataview.c', line 178

static VALUE
t_dv_each(VALUE self) {
  DECLAREDV(self);
  DECLAREBB(dv->bb_obj);

  int size = dv->size;
  if (size + dv->offset >= bb->size)
    size = bb->size - dv->offset;

  if (rb_block_given_p()) {
    for (int i = 0; i < size; i++) {
      unsigned int val = (unsigned int)bb->ptr[i + dv->offset];
      rb_yield(UINT2NUM(val));
    }
  } else {
    rb_raise(rb_eArgError, "no block given");
  }
  return self;
}

#endianessObject



170
171
172
173
174
175
176
# File 'ext/arraybuffer/dataview.c', line 170

static VALUE
t_dv_endianess(VALUE self) {
  DECLAREDV(self);
  return CHECK_LITTLEENDIAN(dv) ?
    ID2SYM(idLittle) :
    ID2SYM(idBig);
}

#getBit(index) ⇒ Integer

Reads a bit at index.

This method differs from the other in that the index is given in bits, not in bytes. Thus it’s possible to read from index 0 to (size * 8) - 1



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'ext/arraybuffer/dataview.c', line 260

static VALUE
t_dv_getbit(VALUE self, VALUE index) {
  DECLAREDV(self); DECLAREBB(dv->bb_obj);
  int idx = NUM2INT(index);
  if (idx < 0)
    idx += (int)dv->size * 8;
  if (idx < 0 || idx >= (int)dv->size * 8)
    rb_raise(rb_eArgError, "index out of bounds: %d", idx);

  unsigned int bit_idx = ((unsigned int)idx) & 7;
  unsigned int byte_idx = (((unsigned int)idx) >> 3) + dv->offset;
  unsigned char bit_mask = 1 << bit_idx;

  CHECKBOUNDSBB(byte_idx);
  return (bb->ptr[byte_idx] & bit_mask) ? UINT2NUM(1) : UINT2NUM(0);
}

#getU16(index) ⇒ Integer

Reads two bytes starting at index as an unsigned short



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'ext/arraybuffer/dataview.c', line 295

static VALUE
t_dv_getu16(VALUE self, VALUE index) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  unsigned int idx1 = dv->offset + (unsigned int)idx + 1;
  CHECKBOUNDSBB(idx0);
  CHECKBOUNDSBB(idx1);
  unsigned short val = 0;
  if (CHECK_LITTLEENDIAN(dv)) {
    val |= bb->ptr[idx0];
    val |= bb->ptr[idx1] << 8;
  } else {
    val |= bb->ptr[idx0] << 8;
    val |= bb->ptr[idx1];
  }
  return UINT2NUM(val);
}

#getU24(index) ⇒ Integer

Reads three bytes starting at index as a 3 bytes long unsigned integer



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/arraybuffer/dataview.c', line 318

static VALUE
t_dv_getu24(VALUE self, VALUE index) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  unsigned int idx1 = dv->offset + (unsigned int)idx + 1;
  unsigned int idx2 = dv->offset + (unsigned int)idx + 2;
  CHECKBOUNDSBB(idx0);
  CHECKBOUNDSBB(idx2);
  unsigned int val = 0;
  if (CHECK_LITTLEENDIAN(dv)) {
    val |= bb->ptr[idx0];
    val |= bb->ptr[idx1] << 8;
    val |= bb->ptr[idx2] << 16;
  } else {
    val |= bb->ptr[idx0] << 16;
    val |= bb->ptr[idx1] << 8;
    val |= bb->ptr[idx2];
  }
  return UINT2NUM(val);
}

#getU32(index) ⇒ Integer

Reads four bytes starting at index as a 4 bytes long unsigned integer



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'ext/arraybuffer/dataview.c', line 344

static VALUE
t_dv_getu32(VALUE self, VALUE index) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  unsigned int idx1 = dv->offset + (unsigned int)idx + 1;
  unsigned int idx2 = dv->offset + (unsigned int)idx + 2;
  unsigned int idx3 = dv->offset + (unsigned int)idx + 3;
  CHECKBOUNDSBB(idx0);
  CHECKBOUNDSBB(idx3);
  unsigned int val = 0;
  if (CHECK_LITTLEENDIAN(dv)) {
    val |= bb->ptr[idx0];
    val |= bb->ptr[idx1] << 8;
    val |= bb->ptr[idx2] << 16;
    val |= bb->ptr[idx3] << 24;
  } else {
    val |= bb->ptr[idx0] << 24;
    val |= bb->ptr[idx1] << 16;
    val |= bb->ptr[idx2] << 8;
    val |= bb->ptr[idx3];
  }
  return UINT2NUM(val);
}

#getU8(index) ⇒ Integer

Reads the byte at index as an unsigned char



282
283
284
285
286
287
288
# File 'ext/arraybuffer/dataview.c', line 282

static VALUE
t_dv_getu8(VALUE self, VALUE index) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int real_idx = dv->offset + (unsigned int)idx;
  CHECKBOUNDSBB(real_idx);
  return UINT2NUM(bb->ptr[real_idx]);
}

#offsetObject



164
165
166
167
168
# File 'ext/arraybuffer/dataview.c', line 164

static VALUE
t_dv_offset(VALUE self) {
  DECLAREDV(self);
  return UINT2NUM(dv->offset);
}

#offset=(offset) ⇒ Object

Defined the new offset for the DataView.

Changing the offset does not affect the size. If passed a negative value, it will be summed with the underlying buffer size. The final value must not be lower than zero.



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/arraybuffer/dataview.c', line 207

static VALUE
t_dv_setoffset(VALUE self, VALUE offset) {
  DECLAREDV(self);
  DECLAREBB(dv->bb_obj);
  int offset_val = NIL_P(offset) ? 0 : NUM2INT(offset);
  if (offset_val < 0)
    offset_val += (int)bb->size;
  if (offset_val < 0)
    rb_raise(rb_eArgError, "calculated offset is negative: %d", offset_val);

  dv->offset = (unsigned int)offset_val;
  return self;
}

#setBytes(index, bytes) ⇒ Object



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'ext/arraybuffer/dataview.c', line 474

static VALUE
t_dv_setbytes(VALUE self, VALUE index, VALUE bytes) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  CHECKBOUNDSBB(idx0);

  if (RB_TYPE_P(bytes, T_ARRAY)) {
    const unsigned int length = (unsigned int)rb_array_len(bytes);
    const VALUE* items = rb_array_const_ptr(bytes);
    CHECKBOUNDSBB(idx0 + length);

    for (unsigned int i = 0; i < length; i++) {
      if (!RB_FIXNUM_P(items[i]))
        rb_raise(rb_eRuntimeError, "array contains non fixnum value at index %d", i);
      int num = NUM2INT(items[i]);
      ADJUSTBOUNDS(num, 0xFF);
      bb->ptr[idx0 + i] = (unsigned char)num;
    }
  } else if (RB_TYPE_P(bytes, T_STRING)) {
    const char *str_ptr = RSTRING_PTR(bytes);
    const unsigned int length = (unsigned int)RSTRING_LEN(bytes);
    CHECKBOUNDSBB(idx0 + length);

    for (unsigned int i = 0; i < length; i++) {
      bb->ptr[idx0 + i] = (unsigned char)str_ptr[i];
    }
  } else if (RB_TYPE_P(bytes, T_DATA) &&
    (CLASS_OF(bytes) == cArrayBuffer || CLASS_OF(bytes) == cDataView)) {
    unsigned int length;
    const char *src_bytes;
    if (CLASS_OF(bytes) == cArrayBuffer) {
      struct LLC_ArrayBuffer *src_bb = (struct LLC_ArrayBuffer*)rb_data_object_get(bytes);
      length = src_bb->size;
      src_bytes = (const char*)src_bb->ptr;
    } else {
      struct LLC_DataView *src_dv = (struct LLC_DataView*)rb_data_object_get(bytes);
      struct LLC_ArrayBuffer *src_bb = (struct LLC_ArrayBuffer*)rb_data_object_get(src_dv->bb_obj);
      length = src_dv->size;
      src_bytes = (const char*)(src_bb->ptr + (size_t)src_dv->offset);
      if (src_dv->offset >= src_bb->size)
        rb_raise(rb_eRuntimeError, "offset exceeds the underlying source buffer size");
      if (src_dv->offset + length >= src_bb->size)
        rb_raise(rb_eRuntimeError, "offset + size exceeds the underlying source buffer size");
    }

    CHECKBOUNDSBB(idx0 + length);
    memcpy((void*)(bb->ptr + (size_t)idx0), src_bytes, (size_t)length);
  } else {
    rb_raise(rb_eArgError, "Invalid type: %+"PRIsVALUE, CLASS_OF(bytes));
  }

  return self;
}

#setU16(index, value) ⇒ Object

Interprets two bytes starting at index and sets them an unsigned value

Values lower than zero will be set to 0 and values greater than 65535 will be capped.



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'ext/arraybuffer/dataview.c', line 393

static VALUE
t_dv_setu16(VALUE self, VALUE index, VALUE value) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  unsigned int idx1 = dv->offset + (unsigned int)idx + 1;
  CHECKBOUNDSBB(idx0);
  CHECKBOUNDSBB(idx1);
  int val = NUM2INT(value);
  ADJUSTBOUNDS(val, 0xFFFF);
  unsigned int uval = (unsigned int)val;
  if (CHECK_LITTLEENDIAN(dv)) {
    bb->ptr[idx0] = (unsigned char)(uval & 0xFF);
    bb->ptr[idx1] = (unsigned char)((uval >> 8) & 0xFF);
  } else {
    bb->ptr[idx0] = (unsigned char)((uval >> 8) & 0xFF);
    bb->ptr[idx1] = (unsigned char)(uval & 0xFF);
  }
  return self;
}

#setU24(index, value) ⇒ Object

Interprets three bytes starting at index and sets them an unsigned value

Values lower than zero will be set to 0 and values greater than 16777215 will be capped.



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'ext/arraybuffer/dataview.c', line 419

static VALUE
t_dv_setu24(VALUE self, VALUE index, VALUE value) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  unsigned int idx1 = dv->offset + (unsigned int)idx + 1;
  unsigned int idx2 = dv->offset + (unsigned int)idx + 2;
  CHECKBOUNDSBB(idx0);
  CHECKBOUNDSBB(idx2);
  int val = NUM2INT(value);
  ADJUSTBOUNDS(val, 0xFFFFFF);
  unsigned int uval = (unsigned int)val;
  if (CHECK_LITTLEENDIAN(dv)) {
    bb->ptr[idx0] = (unsigned char)(uval & 0xFF);
    bb->ptr[idx1] = (unsigned char)((uval >> 8) & 0xFF);
    bb->ptr[idx2] = (unsigned char)((uval >> 16) & 0xFF);
  } else {
    bb->ptr[idx0] = (unsigned char)((uval >> 16) & 0xFF);
    bb->ptr[idx1] = (unsigned char)((uval >> 8) & 0xFF);
    bb->ptr[idx2] = (unsigned char)(uval & 0xFF);
  }
  return self;
}

#setU32(index, value) ⇒ Object

Interprets four bytes starting at index and sets them an unsigned value

Values lower than zero will be set to 0 and values greater than 4294967295 will be capped.



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'ext/arraybuffer/dataview.c', line 448

static VALUE
t_dv_setu32(VALUE self, VALUE index, VALUE value) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  unsigned int idx1 = dv->offset + (unsigned int)idx + 1;
  unsigned int idx2 = dv->offset + (unsigned int)idx + 2;
  unsigned int idx3 = dv->offset + (unsigned int)idx + 3;
  CHECKBOUNDSBB(idx0);
  CHECKBOUNDSBB(idx3);
  long val = NUM2LONG(value);
  ADJUSTBOUNDS(val, 0xFFFFFFFF);
  unsigned long uval = (unsigned long)val;
  if (CHECK_LITTLEENDIAN(dv)) {
    bb->ptr[idx0] = (unsigned char)(uval & 0xFF);
    bb->ptr[idx1] = (unsigned char)((uval >> 8) & 0xFF);
    bb->ptr[idx2] = (unsigned char)((uval >> 16) & 0xFF);
    bb->ptr[idx3] = (unsigned char)((uval >> 24) & 0xFF);
  } else {
    bb->ptr[idx0] = (unsigned char)((uval >> 24) & 0xFF);
    bb->ptr[idx1] = (unsigned char)((uval >> 16) & 0xFF);
    bb->ptr[idx2] = (unsigned char)((uval >> 8) & 0xFF);
    bb->ptr[idx3] = (unsigned char)(uval & 0xFF);
  }
  return self;
}

#setU8(index, value) ⇒ Object

Interprets one byte at index and assigns it an unsigned value

Values lower than zero will be set to 0 and values greater than 255 will be capped.



376
377
378
379
380
381
382
383
384
385
# File 'ext/arraybuffer/dataview.c', line 376

static VALUE
t_dv_setu8(VALUE self, VALUE index, VALUE value) {
  DECLAREDV(self); DECLARENCHECKIDX(index); DECLAREBB(dv->bb_obj);
  unsigned int idx0 = dv->offset + (unsigned int)idx;
  CHECKBOUNDSBB(idx0);
  int val = NUM2INT(value);
  ADJUSTBOUNDS(val, 0xFF);
  bb->ptr[idx0] = (unsigned char)val;
  return self;
}

#sizeObject Also known as: length



158
159
160
161
162
# File 'ext/arraybuffer/dataview.c', line 158

static VALUE
t_dv_size(VALUE self) {
  DECLAREDV(self);
  return UINT2NUM(dv->size);
}

#size=(size) ⇒ Object Also known as: length=

Defined the new offset for the DataView.

Changing the size does not affect the offset. If passed a negative value, it will be summed with the underlying buffer size. The final value must not be lower than zero.



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'ext/arraybuffer/dataview.c', line 230

static VALUE
t_dv_setsize(VALUE self, VALUE size) {
  DECLAREDV(self);
  DECLAREBB(dv->bb_obj);
  int size_val = NIL_P(size) ? (int)bb->size : NUM2INT(size);
  if (size_val < 0)
    size_val += (int)bb->size;
  if (size_val < 0)
    rb_raise(rb_eArgError, "calculated size is negative: %d", size_val);

  dv->size = (unsigned int)size_val;
  return self;
}

#to_sObject



528
529
530
531
532
533
534
535
536
537
# File 'ext/arraybuffer/dataview.c', line 528

VALUE
t_dv_to_s(VALUE self) {
  DECLAREDV(self);
  DECLAREBB(dv->bb_obj);

  const char *ptr = (const char*)bb->ptr + (size_t)dv->offset;
  size_t len = (size_t)dv->size;

  return rb_str_new(ptr, len);
}