| ... | @@ -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 | |
| | 91 | pub 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 | } |