-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_count.go
83 lines (76 loc) · 2 KB
/
byte_count.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
*-----------------------------------------------------------------*/
package wipechromium
import (
"fmt"
)
/* ----------------------------------------------------------------
* F u n c t i o n s
*-----------------------------------------------------------------*/
// Given a total size return a string with that value formatted in
// either of the size formats (Standard, International, Binary)
func ReportByteCount(count int64, mode SizeMode) string {
var output string
switch mode {
case SizeModeSI:
output = ByteCountSI(count)
break
case SizeModeIEC:
output = ByteCountIEC(count)
break
case SizeModeStd:
fallthrough
default:
output = AddThousands(count, ',')
}
return output
}
// Byte count formatted using International System (1K = 1000)
func ByteCountSI(b int64) string {
const UNIT = 1000
if b < UNIT {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(UNIT), 0
for n := b / UNIT; n >= UNIT; n /= UNIT {
div *= UNIT
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}
// Byte count formatted using IEC (Binary) system (1K = 1024)
func ByteCountIEC(b int64) string {
const UNIT = 1024
if b < UNIT {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(UNIT), 0
for n := b / UNIT; n >= UNIT; n /= UNIT {
div *= UNIT
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
func AddThousands(nr int64, sep rune) string {
var result string = ""
nrS := Reverse(fmt.Sprintf("%d", nr))
for start := 0; start < len(nrS); start += 3 {
if start+3 < len(nrS) {
group := nrS[start : start+3]
if start != 0 {
result = result + string(sep) + group
} else {
result = group
}
} else {
group := nrS[start:]
result = result + string(sep) + group
break
}
}
return Reverse(result)
}