diff options
| author | Veikka Tuominen <git@vexu.eu> | 2020-09-05 13:58:02 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-05 13:58:02 +0300 |
| commit | 41bbadbb9a27107da539e27e175f5bdb52656bd5 (patch) | |
| tree | 48a89b9ef19eedded7d5b8f3ab9e5e40efacf528 /lib/std/debug/leb128.zig | |
| parent | cff14dc2c67d9a35ae2c3e07bd6d2c5594d8a0a1 (diff) | |
| parent | 09c861b829480be525a787e54117c108705256e6 (diff) | |
| download | zig-41bbadbb9a27107da539e27e175f5bdb52656bd5.tar.gz zig-41bbadbb9a27107da539e27e175f5bdb52656bd5.zip | |
Merge pull request #6246 from Vexu/field
Remove deprecated fields on `type`
Diffstat (limited to 'lib/std/debug/leb128.zig')
| -rw-r--r-- | lib/std/debug/leb128.zig | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/lib/std/debug/leb128.zig b/lib/std/debug/leb128.zig index eca777c1cf..2b96d39131 100644 --- a/lib/std/debug/leb128.zig +++ b/lib/std/debug/leb128.zig @@ -9,10 +9,10 @@ const testing = std.testing; /// Read a single unsigned LEB128 value from the given reader as type T, /// or error.Overflow if the value cannot fit. pub fn readULEB128(comptime T: type, reader: anytype) !T { - const U = if (T.bit_count < 8) u8 else T; + const U = if (@typeInfo(T).Int.bits < 8) u8 else T; const ShiftT = std.math.Log2Int(U); - const max_group = (U.bit_count + 6) / 7; + const max_group = (@typeInfo(U).Int.bits + 6) / 7; var value = @as(U, 0); var group = @as(ShiftT, 0); @@ -40,7 +40,7 @@ pub fn readULEB128(comptime T: type, reader: anytype) !T { /// Write a single unsigned integer as unsigned LEB128 to the given writer. pub fn writeULEB128(writer: anytype, uint_value: anytype) !void { const T = @TypeOf(uint_value); - const U = if (T.bit_count < 8) u8 else T; + const U = if (@typeInfo(T).Int.bits < 8) u8 else T; var value = @intCast(U, uint_value); while (true) { @@ -68,7 +68,7 @@ pub fn readULEB128Mem(comptime T: type, ptr: *[]const u8) !T { /// returning the number of bytes written. pub fn writeULEB128Mem(ptr: []u8, uint_value: anytype) !usize { const T = @TypeOf(uint_value); - const max_group = (T.bit_count + 6) / 7; + const max_group = (@typeInfo(T).Int.bits + 6) / 7; var buf = std.io.fixedBufferStream(ptr); try writeULEB128(buf.writer(), uint_value); return buf.pos; @@ -77,11 +77,11 @@ pub fn writeULEB128Mem(ptr: []u8, uint_value: anytype) !usize { /// Read a single signed LEB128 value from the given reader as type T, /// or error.Overflow if the value cannot fit. pub fn readILEB128(comptime T: type, reader: anytype) !T { - const S = if (T.bit_count < 8) i8 else T; - const U = std.meta.Int(false, S.bit_count); + const S = if (@typeInfo(T).Int.bits < 8) i8 else T; + const U = std.meta.Int(false, @typeInfo(S).Int.bits); const ShiftU = std.math.Log2Int(U); - const max_group = (U.bit_count + 6) / 7; + const max_group = (@typeInfo(U).Int.bits + 6) / 7; var value = @as(U, 0); var group = @as(ShiftU, 0); @@ -97,7 +97,7 @@ pub fn readILEB128(comptime T: type, reader: anytype) !T { if (@bitCast(S, temp) >= 0) return error.Overflow; // and all the overflowed bits are 1 - const remaining_shift = @intCast(u3, U.bit_count - @as(u16, shift)); + const remaining_shift = @intCast(u3, @typeInfo(U).Int.bits - @as(u16, shift)); const remaining_bits = @bitCast(i8, byte | 0x80) >> remaining_shift; if (remaining_bits != -1) return error.Overflow; } @@ -127,8 +127,8 @@ pub fn readILEB128(comptime T: type, reader: anytype) !T { /// Write a single signed integer as signed LEB128 to the given writer. pub fn writeILEB128(writer: anytype, int_value: anytype) !void { const T = @TypeOf(int_value); - const S = if (T.bit_count < 8) i8 else T; - const U = std.meta.Int(false, S.bit_count); + const S = if (@typeInfo(T).Int.bits < 8) i8 else T; + const U = std.meta.Int(false, @typeInfo(S).Int.bits); var value = @intCast(S, int_value); @@ -173,7 +173,7 @@ pub fn writeILEB128Mem(ptr: []u8, int_value: anytype) !usize { /// different value without shifting all the following code. pub fn writeUnsignedFixed(comptime l: usize, ptr: *[l]u8, int: std.meta.Int(false, l * 7)) void { const T = @TypeOf(int); - const U = if (T.bit_count < 8) u8 else T; + const U = if (@typeInfo(T).Int.bits < 8) u8 else T; var value = @intCast(U, int); comptime var i = 0; @@ -346,28 +346,29 @@ test "deserialize unsigned LEB128" { fn test_write_leb128(value: anytype) !void { const T = @TypeOf(value); + const t_signed = @typeInfo(T).Int.is_signed; - const writeStream = if (T.is_signed) writeILEB128 else writeULEB128; - const writeMem = if (T.is_signed) writeILEB128Mem else writeULEB128Mem; - const readStream = if (T.is_signed) readILEB128 else readULEB128; - const readMem = if (T.is_signed) readILEB128Mem else readULEB128Mem; + const writeStream = if (t_signed) writeILEB128 else writeULEB128; + const writeMem = if (t_signed) writeILEB128Mem else writeULEB128Mem; + const readStream = if (t_signed) readILEB128 else readULEB128; + const readMem = if (t_signed) readILEB128Mem else readULEB128Mem; // decode to a larger bit size too, to ensure sign extension // is working as expected - const larger_type_bits = ((T.bit_count + 8) / 8) * 8; - const B = std.meta.Int(T.is_signed, larger_type_bits); + const larger_type_bits = ((@typeInfo(T).Int.bits + 8) / 8) * 8; + const B = std.meta.Int(t_signed, larger_type_bits); const bytes_needed = bn: { - const S = std.meta.Int(T.is_signed, @sizeOf(T) * 8); - if (T.bit_count <= 7) break :bn @as(u16, 1); + const S = std.meta.Int(t_signed, @sizeOf(T) * 8); + if (@typeInfo(T).Int.bits <= 7) break :bn @as(u16, 1); const unused_bits = if (value < 0) @clz(T, ~value) else @clz(T, value); - const used_bits: u16 = (T.bit_count - unused_bits) + @boolToInt(T.is_signed); + const used_bits: u16 = (@typeInfo(T).Int.bits - unused_bits) + @boolToInt(t_signed); if (used_bits <= 7) break :bn @as(u16, 1); break :bn ((used_bits + 6) / 7); }; - const max_groups = if (T.bit_count == 0) 1 else (T.bit_count + 6) / 7; + const max_groups = if (@typeInfo(T).Int.bits == 0) 1 else (@typeInfo(T).Int.bits + 6) / 7; var buf: [max_groups]u8 = undefined; var fbs = std.io.fixedBufferStream(&buf); @@ -414,7 +415,7 @@ test "serialize unsigned LEB128" { const T = std.meta.Int(false, t); const min = std.math.minInt(T); const max = std.math.maxInt(T); - var i = @as(std.meta.Int(false, T.bit_count + 1), min); + var i = @as(std.meta.Int(false, @typeInfo(T).Int.bits + 1), min); while (i <= max) : (i += 1) try test_write_leb128(@intCast(T, i)); } @@ -432,7 +433,7 @@ test "serialize signed LEB128" { const T = std.meta.Int(true, t); const min = std.math.minInt(T); const max = std.math.maxInt(T); - var i = @as(std.meta.Int(true, T.bit_count + 1), min); + var i = @as(std.meta.Int(true, @typeInfo(T).Int.bits + 1), min); while (i <= max) : (i += 1) try test_write_leb128(@intCast(T, i)); } |
