Source file
src/tawesoft.co.uk/go/humanizex/humanizer.go
Documentation:
src/tawesoft.co.uk/go/humanizex/humanizer.go
1 package humanizex
2
3 import (
4 "math"
5 "time"
6
7 "golang.org/x/text/language"
8 "golang.org/x/text/message"
9 "tawesoft.co.uk/go/lxstrconv"
10 )
11
12
13 type String struct {
14
15 Utf8 string
16
17
18
19
20 Ascii string
21 }
22
23
24
25 type Unit String
26
27
28 func (u Unit) Cat(v Unit) Unit {
29 return Unit{
30 u.Utf8 + v.Utf8,
31 u.Ascii + v.Ascii,
32 }
33 }
34
35
36 type Part struct {
37 Magnitude float64
38 Unit Unit
39 }
40
41 func partEqual(a Part, b Part, epsilon float64) bool {
42 if a.Unit != b.Unit { return false }
43 return math.Abs(a.Magnitude - b.Magnitude) < epsilon
44 }
45
46 func partsEqual(a []Part, b []Part, epsilon float64) bool {
47 if len(a) != len(b) { return false }
48
49 for i := 0; i < len(a); i++ {
50 if !partEqual(a[i], b[i], epsilon) { return false }
51 }
52
53 return true
54 }
55
56
57
58 type Humanizer interface {
59
60
61
62 Format(value float64, unit Unit, factors Factors) String
63
64 FormatNumber(number float64) String
65 FormatDistance(meters float64) String
66 FormatDuration(duration time.Duration) string
67 FormatSeconds(seconds float64) string
68 FormatBytesJEDEC(bytes int64) string
69 FormatBytesIEC(bytes int64) string
70 FormatBytesSI(bytes int64) string
71
72
73
74
75
76
77
78 Accept(str string, unit Unit, factors Factors) (float64, int, error)
79
80
81
82
83 Parse(str string, unit Unit, factors Factors) (float64, error)
84
85 ParseDuration(str string) (time.Duration, error)
86 ParseBytesJEDEC(str string) (int64, error)
87 ParseBytesIEC(str string) (int64, error)
88 ParseBytesSI(str string) (int64, error)
89 }
90
91 type humanizer struct {
92 Tag language.Tag
93 NF lxstrconv.NumberFormat
94 Printer *message.Printer
95 }
96
97 func (h *humanizer) FormatDistance(meters float64) String {
98 return h.Format(meters, CommonUnits.Meter, CommonFactors.Distance)
99 }
100
101 func (h *humanizer) FormatBytesJEDEC(bytes int64) string {
102 return h.Format(float64(bytes), CommonUnits.Byte, CommonFactors.JEDEC).Utf8
103 }
104
105 func (h *humanizer) FormatBytesIEC(bytes int64) string {
106 return h.Format(float64(bytes), CommonUnits.Byte, CommonFactors.IEC).Utf8
107 }
108
109 func (h *humanizer) FormatBytesSI(bytes int64) string {
110 return h.Format(float64(bytes), CommonUnits.Byte, CommonFactors.SI).Utf8
111 }
112
113 func (h *humanizer) FormatDuration(duration time.Duration) string {
114 return h.Format(duration.Seconds(), CommonUnits.Second, CommonFactors.Time).Utf8
115 }
116
117 func (h *humanizer) FormatSeconds(seconds float64) string {
118 return h.Format(seconds, CommonUnits.Second, CommonFactors.Time).Utf8
119 }
120
121 func (h *humanizer) FormatNumber(number float64) String {
122 return h.Format(number, CommonUnits.None, CommonFactors.SI)
123 }
124
125 func (h *humanizer) ParseDuration(str string) (time.Duration, error) {
126 v, err := h.Parse(str, CommonUnits.Second, CommonFactors.Time)
127 return time.Second * time.Duration(v), err
128 }
129
130 func (h *humanizer) ParseBytesJEDEC(str string) (int64, error) {
131 v, err := h.Parse(str, CommonUnits.Byte, CommonFactors.JEDEC)
132 return int64(v), err
133 }
134
135 func (h *humanizer) ParseBytesIEC(str string) (int64, error) {
136 v, err := h.Parse(str, CommonUnits.Byte, CommonFactors.IEC)
137 return int64(v), err
138 }
139
140 func (h *humanizer) ParseBytesSI(str string) (int64, error) {
141 v, err := h.Parse(str, CommonUnits.Byte, CommonFactors.SI)
142 return int64(v), err
143 }
144
145
146
147
148
149
150
151 func NewHumanizer(tag language.Tag, options ... interface{}) Humanizer {
152 return &humanizer{
153 Tag: tag,
154 NF: lxstrconv.NewDecimalFormat(tag),
155 Printer: message.NewPrinter(tag),
156 }
157 }
158
View as plain text