Class: Hyperll::Varint

Inherits:
Object
  • Object
show all
Defined in:
ext/hyperll/varint.c

Class Method Summary collapse

Class Method Details

.read_unsigned_var_int(rbytes) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/hyperll/varint.c', line 43

static VALUE rb_varint_read_unsigned_var_int(VALUE klass, VALUE rbytes) {
  int rlen = RARRAY_LEN(rbytes);
  int maxlen = (rlen > 5) ? 5 : rlen;

  uint8_t bytes[5];
  for (int i = 0; i < maxlen; i++) {
    bytes[i] = (uint8_t)NUM2INT(rb_ary_entry(rbytes, i));
  }

  int len = 0;
  uint32_t value = varint_read_unsigned(bytes, maxlen, &len);

  if (len == -1) {
    rb_raise(rb_eRuntimeError, "Variable length quantity is too long");
    return Qnil;
  }

  // Discard elements that were used to retrieve the value
  for (int i = 0; i < len; i++) {
    rb_ary_shift(rbytes);
  }
  return ULONG2NUM(value);
}

.write_unsigned_var_int(value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'ext/hyperll/varint.c', line 67

static VALUE rb_varint_write_unsigned_var_int(VALUE klass, VALUE value) {
  VALUE rbytes = rb_ary_new2(5);

  uint8_t bytes[5];
  int len = varint_write_unsigned(NUM2ULONG(value), bytes);
  for (int i = 0; i < len; i++) {
    rb_ary_push(rbytes, INT2NUM(bytes[i]));
  }

  return rbytes;
}