decode

Converts a ubyte string to LEB128 number

  1. DecodeLEB128!T decode(const(ubyte[]) data)
  2. DecodeLEB128!T decode(const(ubyte[]) data)
    @safe @nogc pure nothrow
    DecodeLEB128!T
    decode
    (
    T = long
    )
    (
    const(ubyte[]) data
    )
    if (
    isSigned!T
    )

Return Value

Type: DecodeLEB128!T

The value and the size In case of an error this size is set to zero

Examples

1 import std.algorithm.comparison : equal;
2 
3 void ok(T)(T x, const(ubyte[]) expected) {
4     const encoded = encode(x);
5     assert(equal(encoded, expected));
6     assert(calc_size(x) == expected.length);
7     assert(calc_size(expected) == expected.length);
8     const decoded = decode!T(expected);
9     assert(decoded.size == expected.length);
10     assert(decoded.value == x);
11 }
12 
13 {
14     ok!int(int.max, [255, 255, 255, 255, 7]);
15     ok!ulong(27, [27]);
16     ok!ulong(2727, [167, 21]);
17     ok!ulong(272727, [215, 210, 16]);
18     ok!ulong(27272727, [151, 204, 128, 13]);
19     ok!ulong(1427449141, [181, 202, 212, 168, 5]);
20     ok!ulong(ulong.max, [255, 255, 255, 255, 255, 255, 255, 255, 255, 1]);
21 }
22 
23 {
24     ok!int(-1, [127]);
25     ok!int(int.max, [255, 255, 255, 255, 7]);
26     ok!int(int.min, [128, 128, 128, 128, 120]);
27     ok!int(int.max, [255, 255, 255, 255, 7]);
28     ok!long(int.min, [128, 128, 128, 128, 120]);
29     ok!long(int.max, [255, 255, 255, 255, 7]);
30 
31     ok!long(27, [27]);
32     ok!long(2727, [167, 21]);
33     ok!long(272727, [215, 210, 16]);
34     ok!long(27272727, [151, 204, 128, 13]);
35     ok!long(1427449141, [181, 202, 212, 168, 5]);
36 
37     ok!int(-123456, [192, 187, 120]);
38     ok!long(-27, [101]);
39     ok!long(-2727, [217, 106]);
40     ok!long(-272727, [169, 173, 111]);
41     ok!long(-27272727, [233, 179, 255, 114]);
42     ok!long(-1427449141L, [203, 181, 171, 215, 122]);
43 
44     ok!long(-1L, [127]);
45     ok!long(long.max - 1, [254, 255, 255, 255, 255, 255, 255, 255, 255, 0]);
46     ok!long(long.max, [255, 255, 255, 255, 255, 255, 255, 255, 255, 0]);
47     ok!long(long.min + 1, [129, 128, 128, 128, 128, 128, 128, 128, 128, 127]);
48     ok!long(long.min, [128, 128, 128, 128, 128, 128, 128, 128, 128, 127]);
49 }
50 
51 {
52     assert(decode!int([127]).value == -1);
53 }
54 
55 { // Bug fix
56     assert(calc_size(-77) == 2);
57     ok!int(-77, [179, 127]);
58 }