-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
55 lines (51 loc) · 1.33 KB
/
utils.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
package logger
// Functions and structs that are unrelated to Logger
// but are used for formatting values for logging.
import (
"strconv"
"time"
)
// Escape escapes multi-line NMEA sentences for debug logging.
// It replaces CR, LF and NUL with \r, \n and \0,
// and is only slightly longer than string().
func Escape(b []byte) string {
s := make([]byte, 0, len(b))
for _, c := range b {
switch c {
case byte('\r'):
s = append(s, "\\r"...)
case byte('\n'):
s = append(s, "\\n"...)
case 0:
s = append(s, "\\0"...)
default:
s = append(s, c)
}
}
return string(s)
}
// SiMultiple rounds n down to the nearest Kilo, Mega, Giga, ..., or Yotta, and append the letter.
// `multipleOf` can be 1000 or 1024 (or anything >=256 (=(2^64)^(1/8))).
// `maxUnit` prevents losing too much precission by using too big units.
func SiMultiple(n, multipleOf uint64, maxUnit byte) string {
var steps, rem uint64
units := " KMGTPEZY"
for n >= multipleOf && units[steps] != maxUnit {
rem = n % multipleOf
n /= multipleOf
steps++
}
if rem%multipleOf >= multipleOf/2 {
n++ // round the last
}
s := strconv.FormatUint(n, 10)
if steps > 0 {
s += units[steps : steps+1]
}
return s
}
// RoundDuration removes excessive precission for printing.
func RoundDuration(d, to time.Duration) string {
d = d - (d % to)
return d.String()
}