authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-26 19:55:37 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-26 19:55:37 -07:00
logfcbd3949caa7ca4cf4ac8301a106ab760d0fe053
treee79882793b96ee142e10cf03fdb85bbbfa1a0043
parent1525bbb1a7c33baebc5a2291ce52f3738fd10578

add initial code


4 files changed, 34 insertions(+), 0 deletions(-)

.gitignore created+3
......@@ -0,0 +1,3 @@
1zig-*
2.zigmod
3deps.zig
src/lib.zig created+24
......@@ -0,0 +1,24 @@
1const std = @import("std");
2const string = []const u8;
3
4pub fn fmtByteCountIEC(alloc: *std.mem.Allocator, b: u64) !string {
5 return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZY");
6}
7
8pub fn reduceNumber(alloc: *std.mem.Allocator, input: u64, comptime unit: u64, comptime base: string, comptime prefixes: string) !string {
9 if (input < unit) {
10 return std.fmt.allocPrint(alloc, "{d} {s}", .{ input, base });
11 }
12 var div = unit;
13 var exp: usize = 0;
14 var n = input / unit;
15 while (n >= unit) : (n /= unit) {
16 div *= unit;
17 exp += 1;
18 }
19 return try std.fmt.allocPrint(alloc, "{d:.3} {s}{s}", .{ intToFloat(input) / intToFloat(div), prefixes[exp .. exp + 1], base });
20}
21
22pub fn intToFloat(n: u64) f64 {
23 return @intToFloat(f64, n);
24}
zig.mod created+6
......@@ -0,0 +1,6 @@
1id: f7dubzb7cyqeqqpzcphw4stuec3b0q0lapyf9e5clzsszlwl
2name: extras
3main: src/lib.zig
4license: MIT
5description: An assortment of random utility functions that aren't in std and don't deserve their own pacakge.
6dependencies:
zigmod.lock created+1
......@@ -0,0 +1 @@
12