Module: VarintPB

Defined in:
lib/varint_pb/version.rb,
ext/varint_pb/varint_pb.c

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#decode(io) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/varint_pb/varint_pb.c', line 28

static
VALUE varint_pb_decode(VALUE module, VALUE io)
{
    unsigned long long int_val = 0;
    unsigned shift = 0;
    unsigned char byte;

    while (1)
    {
        if (shift >= 64)
        {
            rb_raise(rb_eArgError, "too many bytes when decoding varint");
        }
        byte = (unsigned char)FIX2INT(rb_funcall(io, getbyte, 0));
        int_val |= ((unsigned long long)(byte & 0x7f)) << shift;
        shift += 7;
        if ((byte & 0x80) == 0)
        {
            /* return ULL2NUM(int_val); */
            return LL2NUM((long long)int_val);
        }
    }
}

#encode(io, int_valV) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'ext/varint_pb/varint_pb.c', line 6

static
VALUE varint_pb_encode(VALUE module, VALUE io, VALUE int_valV)
{
    /* unsigned for the bit shifting ops */
    unsigned long long int_val = (unsigned long long)NUM2LL(int_valV);
    unsigned char byte;
    while (1)
    {
        byte = int_val & 0x7f;
        int_val >>= 7;
        if (int_val == 0)
        {
            rb_funcall(io, putbyte, 1, INT2FIX(byte));
            return Qnil;
        }
        else
        {
            rb_funcall(io, putbyte, 1, INT2FIX(byte | 0x80));
        }
    }
}