diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..71969716e3e9ca87fdafa1839c25c6daf7da7560 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +zig-* +.zigmod +deps.zig diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000000000000000000000000000000000000..d4c903df3db38f9f70511e0ad0df00ad6c9baaa8 --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const string = []const u8; + +pub fn fmtByteCountIEC(alloc: *std.mem.Allocator, b: u64) !string { + return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZY"); +} + +pub fn reduceNumber(alloc: *std.mem.Allocator, input: u64, comptime unit: u64, comptime base: string, comptime prefixes: string) !string { + if (input < unit) { + return std.fmt.allocPrint(alloc, "{d} {s}", .{ input, base }); + } + var div = unit; + var exp: usize = 0; + var n = input / unit; + while (n >= unit) : (n /= unit) { + div *= unit; + exp += 1; + } + return try std.fmt.allocPrint(alloc, "{d:.3} {s}{s}", .{ intToFloat(input) / intToFloat(div), prefixes[exp .. exp + 1], base }); +} + +pub fn intToFloat(n: u64) f64 { + return @intToFloat(f64, n); +} diff --git a/zig.mod b/zig.mod new file mode 100644 index 0000000000000000000000000000000000000000..596d8af23f1811b4bb24160ae8ac993efeba5fce --- /dev/null +++ b/zig.mod @@ -0,0 +1,6 @@ +id: f7dubzb7cyqeqqpzcphw4stuec3b0q0lapyf9e5clzsszlwl +name: extras +main: src/lib.zig +license: MIT +description: An assortment of random utility functions that aren't in std and don't deserve their own pacakge. +dependencies: diff --git a/zigmod.lock b/zigmod.lock new file mode 100644 index 0000000000000000000000000000000000000000..0cfbf08886fca9a91cb753ec8734c84fcbe52c9f --- /dev/null +++ b/zigmod.lock @@ -0,0 +1 @@ +2