ciathefed/sizeify
Format byte sizes into human-readable strings in Zig. Supports SI, IEC, and Windows-style units with no dependencies.
Format byte sizes into human-readable strings using multiple scale types. Supports decimal (SI), binary (IEC), and Windows-style units.
Inspired by the humansize crate from the Rust ecosystem.
Written in Zig. No dependencies.
kB, MB, GB, etc. (base 1000)KiB, MiB, GiB, etc. (base 1024)KB, MB, GB, etc. (base 1024, but uses SI labels)Run this command in the root of your Zig project:
zig fetch --save "git+https://github.com/ciathefed/sizeify"
Add this snippet to your build.zig:
const sizeify = b.dependency("sizeify", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("sizeify", sizeify.module("sizeify"));
const sizeify = @import("sizeify");
std.log.debug("{f}", .{sizeify.fmt(1536, .binary_short)});
// debug: 1.50 KiB
Also available:
formatBuf(size, scale, buffer)formatAlloc(size, scale, allocator)formatWriter(size, scale, writer)| Bytes | Scale | Output |
|---|---|---|
| 0 | decimal_short | 0.00 B |
| 1500 | decimal_short | 1.50 kB |
| 1024 | binary_long | 1.00 Kibibytes |
| 1024 | windows_short | 1.00 KB |
| max usize | decimal_short | 18.45 EB (on 64-bit) |
Run with:
zig build test
This project is licensed under the MIT License