-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman.go
206 lines (170 loc) · 4 KB
/
human.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Package human provides types that support parsing and formatting
// human-friendly representations of values in various units.
//
// The package only exposes type names that are not that common to find in Go
// programs (in our experience). For that reason, it can be interesting to
// import the package as '.' (dot) to inject the symbols in the namespace of the
// importer, especially in the common case where it's being used in the main
// package of a program, for example:
//
// import (
// . "github.com/segmentio/cli/human"
// )
//
// This can help improve code readability by importing constants in the package
// namespace, allowing constructs like:
//
// type clientConfig{
// DialTimeout Duration
// BufferSize Bytes
// RateLimit Rate
// }
// ...
// config := clientConfig{
// DialTimeout: 10 * Second,
// BufferSize: 64 * KiB,
// RateLimit: 20 * PerSecond,
// }
package human
import (
"fmt"
"strconv"
"strings"
"unicode"
)
func isDot(r rune) bool {
return r == '.'
}
func isExp(r rune) bool {
return r == 'e' || r == 'E'
}
func isSign(r rune) bool {
return r == '-' || r == '+'
}
func isNumberPrefix(r rune) bool {
return isSign(r) || unicode.IsDigit(r)
}
func hasPrefixFunc(s string, f func(rune) bool) bool {
for _, r := range s {
return f(r)
}
return false
}
func countPrefixFunc(s string, f func(rune) bool) int {
var i int
var r rune
terminated := false
for i, r = range s {
if !f(r) {
terminated = true
break
}
}
if !terminated {
return i + 1
}
return i
}
func skipSpaces(s string) string {
return strings.TrimLeftFunc(s, unicode.IsSpace)
}
func trimSpaces(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}
func parseNextNumber(s string) (string, string) {
i := 0
// integer part
i += countPrefixFunc(s[i:], isSign) // - or +
i += countPrefixFunc(s[i:], unicode.IsDigit)
// Count all of the digits after the decimal (if one exists)
if hasPrefixFunc(s[i:], isDot) {
i++ // .
i += countPrefixFunc(s[i:], unicode.IsDigit)
}
// exponent part
if hasPrefixFunc(s[i:], isExp) {
i++ // e or E
i += countPrefixFunc(s[i:], isSign) // - or +
i += countPrefixFunc(s[i:], unicode.IsDigit)
}
return s[:i], skipSpaces(s[i:])
}
func parseNextToken(s string) (string, string) {
if hasPrefixFunc(s, isNumberPrefix) {
return parseNextNumber(s)
}
for i, r := range s {
if isNumberPrefix(r) || unicode.IsSpace(r) {
return s[:i], skipSpaces(s[i:])
}
}
return s, ""
}
// parseFloat tries to parse a number at the beginning of s, and returns the
// remainder as well as any error that occurs.
func parseFloat(s string) (float64, string, error) {
s, r := parseNextNumber(s)
f, err := strconv.ParseFloat(s, 64)
return f, r, err
}
func parseUnit(s string) (head, unit string) {
i := strings.LastIndexFunc(s, func(r rune) bool {
return !unicode.IsLetter(r)
})
if i < 0 {
head = s
return
}
head = trimSpaces(s[:i+1])
unit = s[i+1:]
return
}
func match(s, pattern string) bool {
return len(s) <= len(pattern) && strings.EqualFold(s, pattern[:len(s)])
}
type suffix byte
func (c suffix) trim(s string) string {
for len(s) > 0 && s[len(s)-1] == byte(c) {
s = s[:len(s)-1]
}
return s
}
func (c suffix) match(s string) bool {
return len(s) > 0 && s[len(s)-1] == byte(c)
}
func fabs(value float64) float64 {
if value < 0 {
return -value
}
return value
}
func ftoa(value, scale float64) string {
var format string
if value == 0 {
return "0"
}
if value < 0 {
return "-" + ftoa(-value, scale)
}
switch {
case (value / scale) >= 100:
format = "%.0f"
case (value / scale) >= 10:
format = "%.1f"
case scale > 1:
format = "%.2f"
default:
format = "%.3f"
}
s := fmt.Sprintf(format, value/scale)
if strings.Contains(s, ".") {
s = suffix('0').trim(s)
s = suffix('.').trim(s)
}
return s
}
func printError(verb rune, typ, val any) string {
return fmt.Sprintf("%%!%c(%T=%v)", verb, typ, val)
}
type formatter func(fmt.State, rune)
func (f formatter) Format(w fmt.State, v rune) { f(w, v) }