diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-07-19 21:53:48 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-07-19 22:12:37 -0700 |
| commit | c40fb96ca358e2ef28aecc2b7ebc5ffab43ccac8 (patch) | |
| tree | 3aa9f1c66c4558472fbdd3c212a3b116385298e8 /lib/std/Io/Writer.zig | |
| parent | 741a66e03cd8644d01b38849f2bd8f70cae6beca (diff) | |
| download | zig-c40fb96ca358e2ef28aecc2b7ebc5ffab43ccac8.tar.gz zig-c40fb96ca358e2ef28aecc2b7ebc5ffab43ccac8.zip | |
std.Io.Writer: fix writeSliceSwap
tried to be too clever, wrote bad code
Diffstat (limited to 'lib/std/Io/Writer.zig')
| -rw-r--r-- | lib/std/Io/Writer.zig | 23 |
1 files changed, 7 insertions, 16 deletions
diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index 11bc05a00d..4d8f04b246 100644 --- a/lib/std/Io/Writer.zig +++ b/lib/std/Io/Writer.zig @@ -851,9 +851,6 @@ pub inline fn writeStruct(w: *Writer, value: anytype, endian: std.builtin.Endian } } -/// If, `endian` is not native, -/// * Asserts that the buffer storage capacity is at least enough to store `@sizeOf(Elem)` -/// * Asserts that the buffer is aligned enough for `@alignOf(Elem)`. pub inline fn writeSliceEndian( w: *Writer, Elem: type, @@ -867,18 +864,11 @@ pub inline fn writeSliceEndian( } } -/// Asserts that the buffer storage capacity is at least enough to store `@sizeOf(Elem)` -/// -/// Asserts that the buffer is aligned enough for `@alignOf(Elem)`. pub fn writeSliceSwap(w: *Writer, Elem: type, slice: []const Elem) Error!void { - var i: usize = 0; - while (i < slice.len) { - const dest_bytes = try w.writableSliceGreedy(@sizeOf(Elem)); - const dest: []Elem = @alignCast(@ptrCast(dest_bytes[0 .. dest_bytes.len - dest_bytes.len % @sizeOf(Elem)])); - const copy_len = @min(dest.len, slice.len - i); - @memcpy(dest[0..copy_len], slice[i..][0..copy_len]); - i += copy_len; - std.mem.byteSwapAllElements(Elem, dest); + for (slice) |elem| { + var tmp = elem; + std.mem.byteSwapAllFields(Elem, &tmp); + try w.writeAll(@ptrCast(&tmp)); } } @@ -2650,9 +2640,10 @@ test writeStruct { } test writeSliceEndian { - var buffer: [4]u8 align(2) = undefined; + var buffer: [5]u8 align(2) = undefined; var w: Writer = .fixed(&buffer); + try w.writeByte('x'); const array: [2]u16 = .{ 0x1234, 0x5678 }; try writeSliceEndian(&w, u16, &array, .big); - try testing.expectEqualSlices(u8, &.{ 0x12, 0x34, 0x56, 0x78 }, &buffer); + try testing.expectEqualSlices(u8, &.{ 'x', 0x12, 0x34, 0x56, 0x78 }, &buffer); } |
