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 › mem › readVarInt Public Function readVarInt Doc Source Public Function std.mem.readVarInt
pub fn readVarInt (comptime ReturnType : type , bytes : []const u8 , endian : Endian ) ReturnType Params ReturnType : type bytes : []const u8 endian : Endian File mem.zig: 1792
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812 pub fn readVarInt (comptime ReturnType : type , bytes : []const u8 , endian : Endian ) ReturnType {
assert (@typeInfo (ReturnType ).int .bits >= bytes .len * 8 );
const bits = @typeInfo (ReturnType ).int .bits ;
const signedness = @typeInfo (ReturnType ).int .signedness ;
const WorkType = @Int (signedness , @max (16 , bits ));
var result : WorkType = 0 ;
switch (endian ) {
.big => {
for (bytes ) |b | {
result = (result << 8 ) | b ;
}
},
.little => {
const ShiftType = math .Log2Int (WorkType );
for (bytes , 0 ..) |b , index | {
result = result | (@as (WorkType , b ) << @as (ShiftType , @intCast (index * 8 )));
}
},
}
return @truncate (result );
}
Reads an integer from memory with size equal to bytes.len. ReturnType specifies the return type, which must be large enough to store the result.