ArikSquad/zigenv
.env parser for Zig
A tiny, allocation-minimal .env parser with a single-pass in-place parsing over a mutable buffer.
KEY=VALUE
pairs that supports those optional quotes#
and blank lines\n
, \r
, \t
, \\
, \"
const std = @import("std");
const zigenv = @import("src/zigenv.zig");
pub fn main() !void {
var gpa = std.heap.page_allocator;
var file = try std.fs.cwd().openFile(".env", .{});
defer file.close();
const stat = try file.stat();
var buf = try gpa.alloc(u8, stat.size);
defer gpa.free(buf);
_ = try file.readAll(buf);
var table = std.StringHashMap([]const u8).init(gpa);
defer table.deinit();
try zigenv.parseInPlace(&table, buf, .{});
if (table.get("DATABASE_URL")) |v| {
std.debug.print("db={s}\n", .{v});
}
}
u32
length aren't supported. You'll need to adjust types if you need extremely large lines.