Annotate

util/human/units.lua @ 12714:82bca7191f13

util.crypto: Use stack space buffers Removes assumption that LUAL_BUFFERSIZE is known at pre-processing time, which it is not in Lua 5.3 and 5.4, where it is a computed macro based on sizeof. Allocation of stack space is safer and faster, no need to worry about luaL_prepbuffer failing to allocate memory and skipping free()
author Kim Alvefur <zash@zash.se>
date Mon, 11 Jul 2022 17:01:55 +0200
parent 12589:39ae08180c81
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10889
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
1 local math_abs = math.abs;
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
2 local math_ceil = math.ceil;
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
3 local math_floor = math.floor;
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
4 local math_log = math.log;
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
5 local math_max = math.max;
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
6 local math_min = math.min;
12589
39ae08180c81 compat: Remove handling of Lua 5.1 location of 'unpack' function
Kim Alvefur <zash@zash.se>
parents: 12573
diff changeset
7 local unpack = table.unpack;
10888
42a0d9089de9 util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 10886
diff changeset
8
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local large = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 "k", 1000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 "M", 1000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 "G", 1000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 "T", 1000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 "P", 1000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 "E", 1000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 "Z", 1000000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 "Y", 1000000000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local small = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 "m", 0.001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 "μ", 0.000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 "n", 0.000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 "p", 0.000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 "f", 0.000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 "a", 0.000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 "z", 0.000000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 "y", 0.000000000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local binary = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 "Ki", 2^10,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 "Mi", 2^20,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 "Gi", 2^30,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 "Ti", 2^40,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 "Pi", 2^50,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 "Ei", 2^60,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 "Zi", 2^70,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 "Yi", 2^80,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
10903
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
41 local function adjusted_unit(n, b)
10889
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
42 local round = math_floor;
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 local prefixes = large;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local logbase = 1000;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 if b == 'b' then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 prefixes = binary;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 logbase = 1024;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 elseif n < 1 then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 prefixes = small;
10889
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
50 round = math_ceil;
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end
10889
25e0ec11b4e4 util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents: 10888
diff changeset
52 local m = math_max(0, math_min(8, round(math_abs(math_log(math_abs(n), logbase)))));
10888
42a0d9089de9 util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 10886
diff changeset
53 local prefix, multiplier = unpack(prefixes, m * 2-1, m*2);
10903
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
54 return multiplier or 1, prefix;
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
55 end
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
56
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
57 -- n: number, the number to format
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
58 -- unit: string, the base unit
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
59 -- b: optional enum 'b', thousands base
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
60 local function format(n, unit, b) --> string
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
61 local fmt = "%.3g %s%s";
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
62 if n == 0 then
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
63 return fmt:format(n, "", unit);
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
64 end
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
65 local multiplier, prefix = adjusted_unit(n, b);
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
66 return fmt:format(n / multiplier, prefix or "", unit);
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 return {
10903
c5f26f9adb31 util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents: 10890
diff changeset
70 adjust = adjusted_unit;
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 format = format;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 };