authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2018-12-16 11:14:53-05:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2018-12-16 11:59:29-05:00
log7533d1b14cb105f604cac3bd1e9dd99cab7a0c2b
tree7e49043fab596e8f911b66b815a094aac2594847
parent624c74af6f61da55e2ba5dcb6b8605b6aec2cea2

mem foreign functions call the native ones

this reduces the amount of implementation to change for #1835

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

std/mem.zig+3-7
...@@ -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.
445pub fn readIntForeign(comptime T: type, bytes: *const [@sizeOf(T)]u8) T {445pub 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}
449448
450pub const readIntLittle = switch (builtin.endian) {449pub 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.
478pub fn readIntSliceForeign(comptime T: type, bytes: []const u8) T {477pub 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}
484480
485pub const readIntSliceLittle = switch (builtin.endian) {481pub 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.
550pub fn writeIntForeign(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void {546pub 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}
553549
554pub const writeIntLittle = switch (builtin.endian) {550pub const writeIntLittle = switch (builtin.endian) {