From 110af768bbd80c602ad3314091d6a392ee2fd341 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 6 May 2025 20:25:55 -0700 Subject: [PATCH] remove std.io.BitWriter --- lib/std/io.zig | 4 - lib/std/io/BufferedReader.zig | 8 ++ lib/std/io/bit_writer.zig | 179 ---------------------------------- lib/std/io/test.zig | 13 --- 4 files changed, 8 insertions(+), 196 deletions(-) delete mode 100644 lib/std/io/bit_writer.zig diff --git a/lib/std/io.zig b/lib/std/io.zig index c7b45fefa0..b5ffd06de5 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -19,9 +19,6 @@ pub const AllocatingWriter = @import("io/AllocatingWriter.zig"); pub const MultiWriter = @import("io/multi_writer.zig").MultiWriter; pub const multiWriter = @import("io/multi_writer.zig").multiWriter; -pub const BitWriter = @import("io/bit_writer.zig").BitWriter; -pub const bitWriter = @import("io/bit_writer.zig").bitWriter; - pub const ChangeDetectionStream = @import("io/change_detection_stream.zig").ChangeDetectionStream; pub const changeDetectionStream = @import("io/change_detection_stream.zig").changeDetectionStream; @@ -434,7 +431,6 @@ pub fn PollFiles(comptime StreamEnum: type) type { test { _ = AllocatingWriter; - _ = BitWriter; _ = BufferedReader; _ = BufferedWriter; _ = Reader; diff --git a/lib/std/io/BufferedReader.zig b/lib/std/io/BufferedReader.zig index 1369f69c1a..4437bd931b 100644 --- a/lib/std/io/BufferedReader.zig +++ b/lib/std/io/BufferedReader.zig @@ -1160,3 +1160,11 @@ test readSliceShort { test readVec { return error.Unimplemented; } + +test "expected error.EndOfStream" { + // Unit test inspired by https://github.com/ziglang/zig/issues/17733 + var br: std.io.BufferedReader = undefined; + br.initFixed(""); + try std.testing.expectError(error.EndOfStream, br.readEnum(enum(u8) { a, b }, .little)); + try std.testing.expectError(error.EndOfStream, br.isBytes("foo")); +} diff --git a/lib/std/io/bit_writer.zig b/lib/std/io/bit_writer.zig deleted file mode 100644 index eef0ece81b..0000000000 --- a/lib/std/io/bit_writer.zig +++ /dev/null @@ -1,179 +0,0 @@ -const std = @import("../std.zig"); - -//General note on endianess: -//Big endian is packed starting in the most significant part of the byte and subsequent -// bytes contain less significant bits. Thus we write out bits from the high end -// of our input first. -//Little endian is packed starting in the least significant part of the byte and -// subsequent bytes contain more significant bits. Thus we write out bits from -// the low end of our input first. -//Regardless of endianess, within any given byte the bits are always in most -// to least significant order. -//Also regardless of endianess, the buffer always aligns bits to the low end -// of the byte. - -/// Creates a bit writer which allows for writing bits to an underlying standard writer -pub fn BitWriter(comptime endian: std.builtin.Endian, comptime Writer: type) type { - return struct { - writer: Writer, - bits: u8 = 0, - count: u4 = 0, - - const low_bit_mask = [9]u8{ - 0b00000000, - 0b00000001, - 0b00000011, - 0b00000111, - 0b00001111, - 0b00011111, - 0b00111111, - 0b01111111, - 0b11111111, - }; - - /// Write the specified number of bits to the writer from the least significant bits of - /// the specified value. Bits will only be written to the writer when there - /// are enough to fill a byte. - pub fn writeBits(self: *@This(), value: anytype, num: u16) !void { - const T = @TypeOf(value); - const UT = std.meta.Int(.unsigned, @bitSizeOf(T)); - const U = if (@bitSizeOf(T) < 8) u8 else UT; // 0) { - //if we can't fill the buffer, add what we have - const bits_free = 8 - self.count; - if (num < bits_free) { - self.addBits(@truncate(in), @intCast(num)); - return; - } - - //finish filling the buffer and flush it - if (num == bits_free) { - self.addBits(@truncate(in), @intCast(num)); - return self.flushBits(); - } - - switch (endian) { - .big => { - const bits = in >> @intCast(in_count - bits_free); - self.addBits(@truncate(bits), bits_free); - }, - .little => { - self.addBits(@truncate(in), bits_free); - in >>= @intCast(bits_free); - }, - } - in_count -= bits_free; - try self.flushBits(); - } - - //write full bytes while we can - const full_bytes_left = in_count / 8; - for (0..full_bytes_left) |_| { - switch (endian) { - .big => { - const bits = in >> @intCast(in_count - 8); - try self.writer.writeByte(@truncate(bits)); - }, - .little => { - try self.writer.writeByte(@truncate(in)); - if (U == u8) in = 0 else in >>= 8; - }, - } - in_count -= 8; - } - - //save the remaining bits in the buffer - self.addBits(@truncate(in), @intCast(in_count)); - } - - //convenience funciton for adding bits to the buffer - //in the appropriate position based on endianess - fn addBits(self: *@This(), bits: u8, num: u4) void { - if (num == 8) self.bits = bits else switch (endian) { - .big => { - self.bits <<= @intCast(num); - self.bits |= bits & low_bit_mask[num]; - }, - .little => { - const pos = bits << @intCast(self.count); - self.bits |= pos; - }, - } - self.count += num; - } - - /// Flush any remaining bits to the writer, filling - /// unused bits with 0s. - pub fn flushBits(self: *@This()) !void { - if (self.count == 0) return; - if (endian == .big) self.bits <<= @intCast(8 - self.count); - try self.writer.writeByte(self.bits); - self.bits = 0; - self.count = 0; - } - }; -} - -pub fn bitWriter(comptime endian: std.builtin.Endian, writer: anytype) BitWriter(endian, @TypeOf(writer)) { - return .{ .writer = writer }; -} - -/////////////////////////////// - -test "api coverage" { - var mem_be = [_]u8{0} ** 2; - var mem_le = [_]u8{0} ** 2; - - var mem_out_be = std.io.fixedBufferStream(&mem_be); - var bit_stream_be = bitWriter(.big, mem_out_be.writer()); - - const testing = std.testing; - - try bit_stream_be.writeBits(@as(u2, 1), 1); - try bit_stream_be.writeBits(@as(u5, 2), 2); - try bit_stream_be.writeBits(@as(u128, 3), 3); - try bit_stream_be.writeBits(@as(u8, 4), 4); - try bit_stream_be.writeBits(@as(u9, 5), 5); - try bit_stream_be.writeBits(@as(u1, 1), 1); - - try testing.expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001011); - - mem_out_be.pos = 0; - - try bit_stream_be.writeBits(@as(u15, 0b110011010000101), 15); - try bit_stream_be.flushBits(); - try testing.expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001010); - - mem_out_be.pos = 0; - try bit_stream_be.writeBits(@as(u32, 0b110011010000101), 16); - try testing.expect(mem_be[0] == 0b01100110 and mem_be[1] == 0b10000101); - - try bit_stream_be.writeBits(@as(u0, 0), 0); - - var mem_out_le = std.io.fixedBufferStream(&mem_le); - var bit_stream_le = bitWriter(.little, mem_out_le.writer()); - - try bit_stream_le.writeBits(@as(u2, 1), 1); - try bit_stream_le.writeBits(@as(u5, 2), 2); - try bit_stream_le.writeBits(@as(u128, 3), 3); - try bit_stream_le.writeBits(@as(u8, 4), 4); - try bit_stream_le.writeBits(@as(u9, 5), 5); - try bit_stream_le.writeBits(@as(u1, 1), 1); - - try testing.expect(mem_le[0] == 0b00011101 and mem_le[1] == 0b10010101); - - mem_out_le.pos = 0; - try bit_stream_le.writeBits(@as(u15, 0b110011010000101), 15); - try bit_stream_le.flushBits(); - try testing.expect(mem_le[0] == 0b10000101 and mem_le[1] == 0b01100110); - - mem_out_le.pos = 0; - try bit_stream_le.writeBits(@as(u32, 0b1100110100001011), 16); - try testing.expect(mem_le[0] == 0b00001011 and mem_le[1] == 0b11001101); - - try bit_stream_le.writeBits(@as(u0, 0), 0); -} diff --git a/lib/std/io/test.zig b/lib/std/io/test.zig index 523b25c9c8..4cc9847e86 100644 --- a/lib/std/io/test.zig +++ b/lib/std/io/test.zig @@ -167,16 +167,3 @@ test "updateTimes" { try expect(stat_new.atime < stat_old.atime); try expect(stat_new.mtime < stat_old.mtime); } - -test "GenericReader methods can return error.EndOfStream" { - // https://github.com/ziglang/zig/issues/17733 - var fbs = std.io.fixedBufferStream(""); - try std.testing.expectError( - error.EndOfStream, - fbs.reader().readEnum(enum(u8) { a, b }, .little), - ); - try std.testing.expectError( - error.EndOfStream, - fbs.reader().isBytes("foo"), - ); -}