diff options
| author | Matthew Borkowski <matthew.h.borkowski@gmail.com> | 2021-05-22 14:03:06 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-05-28 19:43:58 -0400 |
| commit | 54f774f7966e48a8419dbe2d3b37ae974ec03a83 (patch) | |
| tree | 789cf68edbb55760b79886739d6cf9d335de9327 /lib | |
| parent | 97a2f4e7ae9c52c595841347bb0b26572b180dcf (diff) | |
| download | zig-54f774f7966e48a8419dbe2d3b37ae974ec03a83.tar.gz zig-54f774f7966e48a8419dbe2d3b37ae974ec03a83.zip | |
make writeIntSlice functions work for signed integers
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/mem.zig | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 91d53c9f4b..f365ffe2e4 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1444,7 +1444,7 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void { // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); - var bits = @truncate(uint, value); + var bits = @bitCast(uint, value); for (buffer) |*b| { b.* = @truncate(u8, bits); bits >>= 8; @@ -1464,7 +1464,7 @@ pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void { // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); - var bits = @truncate(uint, value); + var bits = @bitCast(uint, value); var index: usize = buffer.len; while (index != 0) { index -= 1; @@ -2028,6 +2028,30 @@ fn testWriteIntImpl() !void { 0x00, 0x00, })); + + writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Little); + try testing.expect(eql(u8, &bytes, &[_]u8{ + 0xCD, + 0xAB, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + })); + + writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Big); + try testing.expect(eql(u8, &bytes, &[_]u8{ + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0xAB, + 0xCD, + })); } /// Returns the smallest number in a slice. O(n). |
