authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-07 16:33:54 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-07 16:33:54 -07:00
log86e312abea8012e39f868f1f2fbc72d6e28a466b
tree27d006cf3ff21c7132383ebd7dc2db52b3459c38
parent0f9f785b9b5bc9276ffe04e4c50900c1c6612d02

add sliceToInt


1 files changed, 13 insertions(+), 0 deletions(-)

src/lib.zig+13
...@@ -87,3 +87,16 @@ pub fn doesFileExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {...@@ -87,3 +87,16 @@ pub fn doesFileExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {
87 defer file.close();87 defer file.close();
88 return true;88 return true;
89}89}
90
91pub fn sliceToInt(comptime T: type, comptime E: type, slice: []const E) !T {
92 const a = @typeInfo(T).Int.bits;
93 const b = @typeInfo(E).Int.bits;
94 if (a < b * slice.len) return error.Overflow;
95
96 var n: T = 0;
97 for (slice) |item, i| {
98 const shift = @intCast(std.math.Log2Int(T), b * (slice.len - 1 - i));
99 n = n | (@as(T, item) << shift);
100 }
101 return n;
102}