authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-11 02:51:17-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-01-11 02:51:17-05:00
log465e75bc5a41aa899b673c0a3ff59d6da871fbe6
tree9fc6ecf6ff587878e178c9413d1d06c53ad035bd
parent891c93c118a85ce16225ed2e97676cdae2abe657
parent899e36489d68fba560122d9222693593cd92aa24
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #682 from zig-lang/fix-endian

Fix endian swap parameters

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

std/endian.zig+8-3
...@@ -2,11 +2,11 @@ const mem = @import("mem.zig");...@@ -2,11 +2,11 @@ const mem = @import("mem.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn swapIfLe(comptime T: type, x: T) -> T {4pub fn swapIfLe(comptime T: type, x: T) -> T {
5 return swapIf(false, T, x);5 return swapIf(builtin.Endian.Little, T, x);
6}6}
77
8pub fn swapIfBe(comptime T: type, x: T) -> T {8pub fn swapIfBe(comptime T: type, x: T) -> T {
9 return swapIf(true, T, x);9 return swapIf(builtin.Endian.Big, T, x);
10}10}
1111
12pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {12pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {
...@@ -15,6 +15,11 @@ pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {...@@ -15,6 +15,11 @@ pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {
1515
16pub fn swap(comptime T: type, x: T) -> T {16pub fn swap(comptime T: type, x: T) -> T {
17 var buf: [@sizeOf(T)]u8 = undefined;17 var buf: [@sizeOf(T)]u8 = undefined;
18 mem.writeInt(buf[0..], x, false);18 mem.writeInt(buf[0..], x, builtin.Endian.Little);
19 return mem.readInt(buf, T, builtin.Endian.Big);19 return mem.readInt(buf, T, builtin.Endian.Big);
20}20}
21
22test "swap" {
23 const debug = @import("debug/index.zig");
24 debug.assert(swap(u32, 0xDEADBEEF) == 0xEFBEADDE);
25}