| ... | @@ -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)] |
| 1480 | pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void { | 1480 | pub 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)); |
| 1482 | | 1482 | |
| 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); |
| 1485 | | 1485 | } 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 enough | 1490 | // 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 { |
| 1500 | pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void { | 1504 | pub 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)); |
| 1502 | | 1506 | |
| 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 | } |
| 1505 | | 1514 | |
| 1506 | // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough | 1515 | // 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 | } |
| 2173 | | 2206 | |
| 2174 | /// Returns the smallest number in a slice. O(n). | 2207 | /// Returns the smallest number in a slice. O(n). |