Skip to content

Commit 465e75b

Browse files
authoredJan 11, 2018
Merge pull request #682 from zig-lang/fix-endian
Fix endian swap parameters
2 parents 891c93c + 899e364 commit 465e75b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed
 

‎std/endian.zig

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ const mem = @import("mem.zig");
22
const builtin = @import("builtin");
33

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

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

1212
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

1616
pub fn swap(comptime T: type, x: T) -> T {
1717
var buf: [@sizeOf(T)]u8 = undefined;
18-
mem.writeInt(buf[0..], x, false);
18+
mem.writeInt(buf[0..], x, builtin.Endian.Little);
1919
return mem.readInt(buf, T, builtin.Endian.Big);
2020
}
21+
22+
test "swap" {
23+
const debug = @import("debug/index.zig");
24+
debug.assert(swap(u32, 0xDEADBEEF) == 0xEFBEADDE);
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.