authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-04-22 03:48:27+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-21 14:04:01-04:00
log211f0a2226db7c53cfa1581c5572a6100c3e17f6
treec3f321972a86a0cb0d9357d37a5c0572bfc95366
parentbb25f212b3630eeaac6a76575f21875d7d94e1a4

std: Add mem.nativeIntToBig and mem.nativeIntToLittle

To be used where `htons`, `htonl`, etc. would be used in C. It's useful to have a function that returns a number directly for use in initialisers.

1 files changed, 16 insertions(+), 0 deletions(-)

std/mem.zig+16
......@@ -754,6 +754,22 @@ test "writeIntBig and writeIntLittle" {
754754 testing.expect(eql_slice_u8(buf2[0..], []u8{ 0xfc, 0xff }));
755755}
756756
757pub fn nativeIntToBig(comptime T: type, x: T) T {
758 comptime assert(T.bit_count % 8 == 0);
759 switch (builtin.endian) {
760 builtin.Endian.Little => return @bswap(T, x),
761 builtin.Endian.Big => return x,
762 }
763}
764
765pub fn nativeIntToLittle(comptime T: type, x: T) T {
766 comptime assert(T.bit_count % 8 == 0);
767 switch (builtin.endian) {
768 builtin.Endian.Little => return x,
769 builtin.Endian.Big => return @bswap(T, x),
770 }
771}
772
757773pub fn hash_slice_u8(k: []const u8) u32 {
758774 // FNV 32-bit hash
759775 var h: u32 = 2166136261;