| ... | @@ -443,8 +443,7 @@ pub fn readIntNative(comptime T: type, bytes: *const [@sizeOf(T)]u8) T { | ... | @@ -443,8 +443,7 @@ pub fn readIntNative(comptime T: type, bytes: *const [@sizeOf(T)]u8) T { |
| 443 | /// This function cannot fail and cannot cause undefined behavior. | 443 | /// This function cannot fail and cannot cause undefined behavior. |
| 444 | /// Assumes the endianness of memory is foreign, so it must byte-swap. | 444 | /// Assumes the endianness of memory is foreign, so it must byte-swap. |
| 445 | pub fn readIntForeign(comptime T: type, bytes: *const [@sizeOf(T)]u8) T { | 445 | pub fn readIntForeign(comptime T: type, bytes: *const [@sizeOf(T)]u8) T { |
| 446 | comptime assert(T.bit_count % 8 == 0); | 446 | return @bswap(T, readIntNative(T, bytes)); |
| 447 | return @bswap(T, @ptrCast(*align(1) const T, bytes).*); | | |
| 448 | } | 447 | } |
| 449 | | 448 | |
| 450 | pub const readIntLittle = switch (builtin.endian) { | 449 | pub const readIntLittle = switch (builtin.endian) { |
| ... | @@ -476,10 +475,7 @@ pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T { | ... | @@ -476,10 +475,7 @@ pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T { |
| 476 | /// The bit count of T must be evenly divisible by 8. | 475 | /// The bit count of T must be evenly divisible by 8. |
| 477 | /// Assumes the endianness of memory is foreign, so it must byte-swap. | 476 | /// Assumes the endianness of memory is foreign, so it must byte-swap. |
| 478 | pub fn readIntSliceForeign(comptime T: type, bytes: []const u8) T { | 477 | pub fn readIntSliceForeign(comptime T: type, bytes: []const u8) T { |
| 479 | assert(@sizeOf(u24) == 3); | 478 | return @bswap(T, readIntSliceNative(T, bytes)); |
| 480 | assert(bytes.len >= @sizeOf(T)); | | |
| 481 | // TODO https://github.com/ziglang/zig/issues/863 | | |
| 482 | return readIntForeign(T, @ptrCast(*const [@sizeOf(T)]u8, bytes.ptr)); | | |
| 483 | } | 479 | } |
| 484 | | 480 | |
| 485 | pub const readIntSliceLittle = switch (builtin.endian) { | 481 | pub const readIntSliceLittle = switch (builtin.endian) { |
| ... | @@ -548,7 +544,7 @@ pub fn writeIntNative(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void { | ... | @@ -548,7 +544,7 @@ pub fn writeIntNative(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void { |
| 548 | /// the integer bit width must be divisible by 8. | 544 | /// the integer bit width must be divisible by 8. |
| 549 | /// This function stores in foreign endian, which means it does a @bswap first. | 545 | /// This function stores in foreign endian, which means it does a @bswap first. |
| 550 | pub fn writeIntForeign(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void { | 546 | pub fn writeIntForeign(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void { |
| 551 | @ptrCast(*align(1) T, buf).* = @bswap(T, value); | 547 | writeIntNative(T, buf, @bswap(T, value)); |
| 552 | } | 548 | } |
| 553 | | 549 | |
| 554 | pub const writeIntLittle = switch (builtin.endian) { | 550 | pub const writeIntLittle = switch (builtin.endian) { |