mirror of
https://github.com/ziglang/zig.git
synced 2026-02-21 16:54:52 +00:00
mem: delete readIntSlice and writeIntSlice
After deleting all potentially incorrect usages, there were no more remaining.
This commit is contained in:
parent
d890e81761
commit
149200aac5
171
lib/std/mem.zig
171
lib/std/mem.zig
@ -1612,17 +1612,6 @@ test readInt {
|
||||
try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .Little) == -4);
|
||||
}
|
||||
|
||||
/// Asserts that buffer.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0
|
||||
/// and ignores extra bytes.
|
||||
/// The bit count of T must be evenly divisible by 8.
|
||||
pub inline fn readIntSlice(comptime T: type, buffer: []const u8, endian: Endian) T {
|
||||
const byte_len = @divExact(@typeInfo(T).Int.bits, 8);
|
||||
return switch (endian) {
|
||||
.Little => readInt(T, buffer[0..byte_len], endian),
|
||||
.Big => readInt(T, buffer[buffer.len - byte_len ..], endian),
|
||||
};
|
||||
}
|
||||
|
||||
fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T {
|
||||
const uN = std.meta.Int(.unsigned, @bitSizeOf(T));
|
||||
const Log2N = std.math.Log2Int(T);
|
||||
@ -1760,166 +1749,6 @@ test writeInt {
|
||||
try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xfc, 0xff }));
|
||||
}
|
||||
|
||||
/// Writes a twos-complement integer to memory, with the specified endianness.
|
||||
/// Asserts that buf.len >= @typeInfo(T).Int.bits / 8.
|
||||
/// The bit count of T must be evenly divisible by 8.
|
||||
/// Any extra bytes in buffer not part of the integer are set to zero, with
|
||||
/// respect to endianness. To avoid the branch to check for extra buffer bytes,
|
||||
/// use writeInt instead.
|
||||
pub inline fn writeIntSlice(comptime T: type, buffer: []u8, value: T, endian: Endian) void {
|
||||
const byte_len = @divExact(@typeInfo(T).Int.bits, 8);
|
||||
switch (endian) {
|
||||
.Little => {
|
||||
writeInt(T, buffer[0..byte_len], value, endian);
|
||||
@memset(buffer[byte_len..], 0);
|
||||
},
|
||||
.Big => {
|
||||
@memset(buffer[0 .. buffer.len - byte_len], 0);
|
||||
writeInt(T, buffer[buffer.len - byte_len ..][0..byte_len], value, endian);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test writeIntSlice {
|
||||
try testWriteIntImpl();
|
||||
try comptime testWriteIntImpl();
|
||||
}
|
||||
fn testWriteIntImpl() !void {
|
||||
var bytes: [8]u8 = undefined;
|
||||
|
||||
writeIntSlice(u0, bytes[0..], 0, .Big);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
}));
|
||||
|
||||
writeIntSlice(u0, bytes[0..], 0, .Little);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
}));
|
||||
|
||||
writeIntSlice(u64, bytes[0..], 0x12345678CAFEBABE, .Big);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x12,
|
||||
0x34,
|
||||
0x56,
|
||||
0x78,
|
||||
0xCA,
|
||||
0xFE,
|
||||
0xBA,
|
||||
0xBE,
|
||||
}));
|
||||
|
||||
writeIntSlice(u64, bytes[0..], 0xBEBAFECA78563412, .Little);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x12,
|
||||
0x34,
|
||||
0x56,
|
||||
0x78,
|
||||
0xCA,
|
||||
0xFE,
|
||||
0xBA,
|
||||
0xBE,
|
||||
}));
|
||||
|
||||
writeIntSlice(u32, bytes[0..], 0x12345678, .Big);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x12,
|
||||
0x34,
|
||||
0x56,
|
||||
0x78,
|
||||
}));
|
||||
|
||||
writeIntSlice(u32, bytes[0..], 0x78563412, .Little);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x12,
|
||||
0x34,
|
||||
0x56,
|
||||
0x78,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
}));
|
||||
|
||||
writeIntSlice(u16, bytes[0..], 0x1234, .Big);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x12,
|
||||
0x34,
|
||||
}));
|
||||
|
||||
writeIntSlice(u16, bytes[0..], 0x1234, .Little);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x34,
|
||||
0x12,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
}));
|
||||
|
||||
writeIntSlice(i16, bytes[0..], @as(i16, -21555), .Little);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0xCD,
|
||||
0xAB,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
}));
|
||||
|
||||
writeIntSlice(i16, bytes[0..], @as(i16, -21555), .Big);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0xAB,
|
||||
0xCD,
|
||||
}));
|
||||
|
||||
writeIntSlice(u8, bytes[0..], 0x12, .Big);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x12,
|
||||
}));
|
||||
|
||||
writeIntSlice(u8, bytes[0..], 0x12, .Little);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x12, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
}));
|
||||
|
||||
writeIntSlice(i8, bytes[0..], -1, .Big);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0xff,
|
||||
}));
|
||||
|
||||
writeIntSlice(i8, bytes[0..], -1, .Little);
|
||||
try testing.expect(eql(u8, &bytes, &[_]u8{
|
||||
0xff, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
}));
|
||||
}
|
||||
|
||||
fn writePackedIntLittle(comptime T: type, bytes: []u8, bit_offset: usize, value: T) void {
|
||||
const uN = std.meta.Int(.unsigned, @bitSizeOf(T));
|
||||
const Log2N = std.math.Log2Int(T);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user