blob: a38c0c88980135df0823bbe291e8e410a2297dae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
//! Size formatting
const int = @import("int.zig");
const KB: u64 = 1024;
const MB: u64 = 1024 * 1024;
const GB: u64 = 1024 * 1024 * 1024;
pub fn format(bytes: u64, buf: []u8) []u8 {
if (bytes < KB) {
const s = int.toStr(bytes, buf);
buf[s.len] = 'B';
return buf[0 .. s.len + 1];
} else if (bytes < MB) {
const kb = bytes / KB;
const s = int.toStr(kb, buf);
buf[s.len] = 'K';
buf[s.len + 1] = 'B';
return buf[0 .. s.len + 2];
} else if (bytes < GB) {
const mb = bytes / MB;
const s = int.toStr(mb, buf);
buf[s.len] = 'M';
buf[s.len + 1] = 'B';
return buf[0 .. s.len + 2];
} else {
const gb = bytes / GB;
const s = int.toStr(gb, buf);
buf[s.len] = 'G';
buf[s.len + 1] = 'B';
return buf[0 .. s.len + 2];
}
}
|