Skip to content

Commit

Permalink
Merge pull request #682 from zig-lang/fix-endian
Browse files Browse the repository at this point in the history
Fix endian swap parameters
  • Loading branch information
andrewrk committed Jan 11, 2018
2 parents 891c93c + 899e364 commit 465e75b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions std/endian.zig
Expand Up @@ -2,11 +2,11 @@ const mem = @import("mem.zig");
const builtin = @import("builtin");

pub fn swapIfLe(comptime T: type, x: T) -> T {
return swapIf(false, T, x);
return swapIf(builtin.Endian.Little, T, x);
}

pub fn swapIfBe(comptime T: type, x: T) -> T {
return swapIf(true, T, x);
return swapIf(builtin.Endian.Big, T, x);
}

pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {
Expand All @@ -15,6 +15,11 @@ pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {

pub fn swap(comptime T: type, x: T) -> T {
var buf: [@sizeOf(T)]u8 = undefined;
mem.writeInt(buf[0..], x, false);
mem.writeInt(buf[0..], x, builtin.Endian.Little);
return mem.readInt(buf, T, builtin.Endian.Big);
}

test "swap" {
const debug = @import("debug/index.zig");
debug.assert(swap(u32, 0xDEADBEEF) == 0xEFBEADDE);
}

0 comments on commit 465e75b

Please sign in to comment.