Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const bigToNative = std.mem.bigToNative; pub const Reader = struct { bytes: []const u8, index: usize, pub fn init(bytes: []const u8) Reader { return .{ .bytes = bytes, .index = 0 }; } fn readInt(self: *Reader, comptime T: type) !T { const size = @sizeOf(T); if (self.index + size > self.bytes.len) return error.EndOfStream; const slice = self.bytes[self.index .. self.index + size]; const value = @ptrCast(*align(1) const T, slice).*; self.index += size; return bigToNative(T, value); } fn readFloat(self: *Reader) !f32 { const size = @sizeOf(f32); if (self.index + size > self.bytes.len) return error.EndOfStream; const slice = self.bytes[self.index .. self.index + size]; const value = @ptrCast(*align(1) const u32, slice).*; self.index += size; return @bitCast(f32, bigToNative(u32, value)); } fn readStruct(self: *Reader, comptime T: type) !T { const fields = std.meta.fields(T); var item: T = undefined; inline for (fields) |field| { @field(item, field.name) = try self.read(field.field_type); } return item; } pub fn read(self: *Reader, comptime T: type) !T { return switch (@typeInfo(T)) { .Int => try self.readInt(T), .Float => try self.readFloat(), .Array => |array| { var arr: [array.len]array.child = undefined; var index: usize = 0; while (index < array.len) : (index += 1) { arr[index] = try self.read(array.child); } return arr; }, .Struct => try self.readStruct(T), else => @compileError("unsupported type"), }; } }; const Point = struct { x: i32, y: i32 }; const Data = struct { pi: f32, points: [2]Point, pad: u32, num: u64, byte: u8, }; export fn example(bytes: [*]const u8, len: usize) i64 { var reader = Reader.init(bytes[0..len]); const parsed = reader.read(Data) catch unreachable; // do something with all the data so it's not optimized away! var sum: i64 = parsed.points[0].x + parsed.points[0].y + parsed.points[1].x + parsed.points[1].y; sum += @intCast(i64, parsed.byte) + @intCast(i64, parsed.num); sum += @floatToInt(u32, parsed.pi); return sum; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree