authorgravatar for dbaumgartner21@gmail.comTrioct <dbaumgartner21@gmail.com> 2021-09-12 05:52:15-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-21 16:47:39-05:00
logb644d493658d8fe4eddeeb3ec6d13593f89739bb
treec974fddf24105ed2ab2a146a44d46fbbf811119f
parent939f51aebba982259628f6255a0b0b0ec2d9c99a

Fix type error for u8 in writeIntSlice


1 files changed, 36 insertions(+), 3 deletions(-)

lib/std/mem.zig+36-3
...@@ -1480,9 +1480,13 @@ pub fn writeInt(comptime T: type, buffer: *[@divExact(@typeInfo(T).Int.bits, 8)]...@@ -1480,9 +1480,13 @@ pub fn writeInt(comptime T: type, buffer: *[@divExact(@typeInfo(T).Int.bits, 8)]
1480pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {1480pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {
1481 assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8));1481 assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8));
14821482
1483 if (@typeInfo(T).Int.bits == 0)1483 if (@typeInfo(T).Int.bits == 0) {
1484 return set(u8, buffer, 0);1484 return set(u8, buffer, 0);
14851485 } else if (@typeInfo(T).Int.bits == 8) {
1486 set(u8, buffer, 0);
1487 buffer[0] = @bitCast(u8, value);
1488 return;
1489 }
1486 // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough1490 // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough
1487 const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits);1491 const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits);
1488 var bits = @bitCast(uint, value);1492 var bits = @bitCast(uint, value);
...@@ -1500,8 +1504,13 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {...@@ -1500,8 +1504,13 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {
1500pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void {1504pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void {
1501 assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8));1505 assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8));
15021506
1503 if (@typeInfo(T).Int.bits == 0)1507 if (@typeInfo(T).Int.bits == 0) {
1504 return set(u8, buffer, 0);1508 return set(u8, buffer, 0);
1509 } else if (@typeInfo(T).Int.bits == 8) {
1510 set(u8, buffer, 0);
1511 buffer[buffer.len - 1] = @bitCast(u8, value);
1512 return;
1513 }
15051514
1506 // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough1515 // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough
1507 const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits);1516 const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits);
...@@ -2169,6 +2178,30 @@ fn testWriteIntImpl() !void {...@@ -2169,6 +2178,30 @@ fn testWriteIntImpl() !void {
2169 0xAB,2178 0xAB,
2170 0xCD,2179 0xCD,
2171 }));2180 }));
2181
2182 writeIntSlice(u8, bytes[0..], 0x12, Endian.Big);
2183 try testing.expect(eql(u8, &bytes, &[_]u8{
2184 0x00, 0x00, 0x00, 0x00,
2185 0x00, 0x00, 0x00, 0x12,
2186 }));
2187
2188 writeIntSlice(u8, bytes[0..], 0x12, Endian.Little);
2189 try testing.expect(eql(u8, &bytes, &[_]u8{
2190 0x12, 0x00, 0x00, 0x00,
2191 0x00, 0x00, 0x00, 0x00,
2192 }));
2193
2194 writeIntSlice(i8, bytes[0..], -1, Endian.Big);
2195 try testing.expect(eql(u8, &bytes, &[_]u8{
2196 0x00, 0x00, 0x00, 0x00,
2197 0x00, 0x00, 0x00, 0xff,
2198 }));
2199
2200 writeIntSlice(i8, bytes[0..], -1, Endian.Little);
2201 try testing.expect(eql(u8, &bytes, &[_]u8{
2202 0xff, 0x00, 0x00, 0x00,
2203 0x00, 0x00, 0x00, 0x00,
2204 }));
2172}2205}
21732206
2174/// Returns the smallest number in a slice. O(n).2207/// Returns the smallest number in a slice. O(n).