Code Snippets Zig

Short texts Zig 🧑‍💻 Code snippets Zig ✍️ Reading lists Zig 👀

hello.zig

const std = @import(“std”); pub fn main() !void { const stdout = std.io.getStdOut().writer(); try stdout.print(“Hello, {s}!\n”, .{“world”}); }

print-timestamp.zig

const std = @import(“std”); pub fn main() anyerror!void { var stdout = std.io.getStdOut(); var writer = stdout.writer(); const timestamp = std.time.milliTimestamp(); writer.print(“{d}”, .{timestamp}) catch unreachable; }

helper program to cross check correctness of ffs with simpler algorithm

const std = @import(“std”); const maxInt = std.math.maxInt; const print = std.debug.print; test “print ffs” { //var in: u64 = 0; var in: u32 = 256; //var in: u64 = 256; //var in: u128 = 256; var x: @TypeOf(in) = 0; var out: @TypeOf(in) = 0; print(“\n”, .{}); //while (in > 1; index += 1; […]

A discontiguous list made of blocks of contiguous elements.

const std = @import(“std”); /// A discontiguous list made of blocks of contiguous elements. /// /// Each block takes a single allocation from the underlying allocator /// and is doubly-linked to the blocks next to it. /// /// When the current block is full, an attempt will be made to expand it /// in place […]

query_string_map.zig

const std = @import(“std”); const Api = @import(“./api/schema.zig”).Api; usingnamespace @import(“./global.zig”); /// QueryString array-backed hash table that does few allocations and preserves the original order pub const QueryStringMap = struct { allocator: *std.mem.Allocator, slice: string, buffer: []u8, list: Param.List, name_count: ?usize = null, threadlocal var _name_count: [8]string = undefined; pub fn getNameCount(this: *QueryStringMap) usize { if […]