Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › Io › Reader › takeLeb128 Public Function takeLeb128 Doc Source Public Function std.Io.Reader.takeLeb128
pub fn takeLeb128 (r : *Reader , comptime T : type ) TakeLeb128Error !T Params File Io/Reader.zig: 1295
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403 pub fn takeLeb128 (r : *Reader , comptime T : type ) TakeLeb128Error !T {
const info = switch (@typeInfo (T )) {
.int => |info | info ,
else => @compileError (@typeName (T ) ++ " not supported" ),
};
const Byte = packed struct { bits : u7 , more : bool };
if (info .bits <= 7 ) {
var byte : Byte = undefined ;
const Bits = @Int (info .signedness , 7 );
byte = @bitCast (try r .takeByte ());
const val = std .math .cast (T , @as (Bits , @bitCast (byte .bits ))) orelse error .Overflow ;
const allowed_bits : u7 = switch (info .signedness ) {
.unsigned => 0 ,
.signed => @bitCast (@as (i7 , @bitCast (byte .bits )) >> 6 ),
};
var fits = true ;
while (byte .more ) {
byte = @bitCast (try r .takeByte ());
if (byte .bits != allowed_bits ) fits = false ;
}
return if (fits ) blk : {
@branchHint (.likely );
break :blk val ;
} else error .Overflow ;
}
const Unsigned = @Int (.unsigned , info .bits );
const UInt = std .math .ByteAlignedInt (Unsigned );
const Int = std .math .ByteAlignedInt (T );
const uint_bits = @typeInfo (UInt ).int .bits ;
var byte : Byte = undefined ;
var val : UInt = 0 ;
const max_bytes = @divFloor (info .bits - 1 , 7 ) + 1 ;
inline for (0 ..max_bytes ) |iteration | {
const shift = iteration * 7 ;
byte = @bitCast (try r .takeByte ());
const extended : UInt = byte .bits ;
val |= extended << shift ;
const bits_written = shift + 7 ;
if (bits_written >= info .bits ) {
const bits_overflowed = bits_written - info .bits ;
const bits_remaining = @mod (info .bits , 7 );
const allowed_bits : u7 , var fits : bool = switch (info .signedness ) {
.unsigned => blk : {
const fits = bits_remaining == 0 or byte .bits >> bits_remaining == 0 ;
break :blk .{ 0 , fits };
},
.signed => blk : {
const bits : i7 = @bitCast (byte .bits );
// Move the sign bit into the MSB
const shifted_bits : i7 = bits << bits_overflowed ;
const value_sign : i7 = shifted_bits >> 6 ; // sign extends
const bits_sign : i7 = bits >> bits_remaining ; // sign extends
const fits = bits_remaining == 0 or bits_sign == value_sign ;
if (uint_bits != info .bits and value_sign != 0 ) {
const sign_extend_mask = @as (UInt , std .math .maxInt (UInt )) << info .bits ;
val |= sign_extend_mask ;
}
break :blk .{ @bitCast (value_sign ), fits };
},
};
switch (info .signedness ) {
.signed => assert (allowed_bits == 0 or allowed_bits == 0x7F ),
.unsigned => comptime assert (allowed_bits == 0 ),
}
while (byte .more ) {
byte = @bitCast (try r .takeByte ());
if (byte .bits != allowed_bits ) fits = false ;
}
return if (fits ) blk : {
@branchHint (.likely );
break :blk std .math .cast (T , @as (Int , @bitCast (val ))) orelse error .Overflow ;
} else error .Overflow ;
}
comptime assert (bits_written < info .bits );
if (!byte .more ) {
if (info .signedness == .signed and // can be negative
byte .bits & 0x40 != 0 ) // is negative
{
const sign_extend_mask = @as (UInt , std .math .maxInt (UInt )) << bits_written ;
val |= sign_extend_mask ;
}
return std .math .cast (T , @as (Int , @bitCast (val ))) orelse error .Overflow ;
}
}
}
Read a single LEB128 value as type T, or
error.Overflowif the value cannot fit.