aboutsummaryrefslogtreecommitdiff
path: root/std/endian.zig
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-02-12 17:22:35 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-02-12 17:35:51 -0500
commit6dba1f1c8eee5e2f037c7ef216bc64423aef8e00 (patch)
treef39a29e98b7e3404b114aaa08cd26d767a1666c1 /std/endian.zig
parentca180d3f02914d282505752a1d2fe08e175f9d99 (diff)
downloadzig-6dba1f1c8eee5e2f037c7ef216bc64423aef8e00.tar.gz
zig-6dba1f1c8eee5e2f037c7ef216bc64423aef8e00.zip
slice and array re-work plus some misc. changes
* `@truncate` builtin allows casting to the same size integer. It also performs two's complement casting between signed and unsigned integers. * The idiomatic way to convert between bytes and numbers is now `mem.readInt` and `mem.writeInt` instead of an unsafe cast. It works at compile time, is safer, and looks cleaner. * Implicitly casting an array to a slice is allowed only if the slice is const. * Constant pointer values know if their memory is from a compile- time constant value or a compile-time variable. * Cast from [N]u8 to []T no longer allowed, but [N]u8 to []const T still allowed. * Fix inability to pass a mutable pointer to comptime variable at compile-time to a function and have the function modify the memory pointed to by the pointer. * Add the `comptime T: type` parameter back to mem.eql. Prevents accidentally creating instantiations for arrays.
Diffstat (limited to 'std/endian.zig')
-rw-r--r--std/endian.zig18
1 files changed, 8 insertions, 10 deletions
diff --git a/std/endian.zig b/std/endian.zig
index 0dcc587e4a..1d67e414f8 100644
--- a/std/endian.zig
+++ b/std/endian.zig
@@ -1,21 +1,19 @@
-pub inline fn swapIfLe(comptime T: type, x: T) -> T {
+const mem = @import("mem.zig");
+
+pub fn swapIfLe(comptime T: type, x: T) -> T {
swapIf(false, T, x)
}
-pub inline fn swapIfBe(comptime T: type, x: T) -> T {
+pub fn swapIfBe(comptime T: type, x: T) -> T {
swapIf(true, T, x)
}
-pub inline fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
+pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
if (@compileVar("is_big_endian") == is_be) swap(T, x) else x
}
pub fn swap(comptime T: type, x: T) -> T {
- const x_slice = ([]u8)((&const x)[0...1]);
- var result: T = undefined;
- const result_slice = ([]u8)((&result)[0...1]);
- for (result_slice) |*b, i| {
- *b = x_slice[@sizeOf(T) - i - 1];
- }
- return result;
+ var buf: [@sizeOf(T)]u8 = undefined;
+ mem.writeInt(buf[0...], x, false);
+ return mem.readInt(buf, T, true);
}