authorgravatar for matthew.h.borkowski@gmail.comMatthew Borkowski <matthew.h.borkowski@gmail.com> 2021-05-22 14:03:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-28 19:43:58-04:00
log54f774f7966e48a8419dbe2d3b37ae974ec03a83
tree789cf68edbb55760b79886739d6cf9d335de9327
parent97a2f4e7ae9c52c595841347bb0b26572b180dcf

make writeIntSlice functions work for signed integers


1 files changed, 26 insertions(+), 2 deletions(-)

lib/std/mem.zig+26-2
......@@ -1444,7 +1444,7 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {
14441444
14451445 // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough
14461446 const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits);
1447 var bits = @truncate(uint, value);
1447 var bits = @bitCast(uint, value);
14481448 for (buffer) |*b| {
14491449 b.* = @truncate(u8, bits);
14501450 bits >>= 8;
......@@ -1464,7 +1464,7 @@ pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void {
14641464
14651465 // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough
14661466 const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits);
1467 var bits = @truncate(uint, value);
1467 var bits = @bitCast(uint, value);
14681468 var index: usize = buffer.len;
14691469 while (index != 0) {
14701470 index -= 1;
......@@ -2028,6 +2028,30 @@ fn testWriteIntImpl() !void {
20282028 0x00,
20292029 0x00,
20302030 }));
2031
2032 writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Little);
2033 try testing.expect(eql(u8, &bytes, &[_]u8{
2034 0xCD,
2035 0xAB,
2036 0x00,
2037 0x00,
2038 0x00,
2039 0x00,
2040 0x00,
2041 0x00,
2042 }));
2043
2044 writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Big);
2045 try testing.expect(eql(u8, &bytes, &[_]u8{
2046 0x00,
2047 0x00,
2048 0x00,
2049 0x00,
2050 0x00,
2051 0x00,
2052 0xAB,
2053 0xCD,
2054 }));
20312055}
20322056
20332057/// Returns the smallest number in a slice. O(n).