-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
246 lines (219 loc) · 5.36 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"fmt"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// strings
type caseSensitivity int
const (
caseSensitive caseSensitivity = 0
caseInsensitive caseSensitivity = 1
)
func strIndexOf(str1, str2 string, from int, cs caseSensitivity) int {
if from >= len(str1) {
return -1
}
src := str1
if from < 0 {
from = 0
} else if from > 0 {
src = src[from:]
}
i := -1
if cs == caseSensitive {
i = strings.Index(src, str2)
} else {
i = strings.Index(strings.ToLower(src), strings.ToLower(str2))
}
if i >= 0 {
i += from
}
return i
}
func strStartsWith(str, prefix string, cs caseSensitivity) bool {
if cs == caseSensitive {
return strings.HasPrefix(str, prefix)
}
return strings.HasPrefix(strings.ToLower(str), strings.ToLower(prefix))
}
func strEndsWith(str, postfix string, cs caseSensitivity) bool {
if cs == caseSensitive {
return strings.HasSuffix(str, postfix)
}
return strings.HasSuffix(strings.ToLower(str), strings.ToLower(postfix))
}
func strContains(str, part string, cs caseSensitivity) bool {
if cs == caseSensitive {
return strings.Contains(str, part)
}
return strings.Contains(strings.ToLower(str), strings.ToLower(part))
}
func strContainsAny(str string, parts []string, cs caseSensitivity) bool {
for _, part := range parts {
if strContains(str, part, cs) {
return true
}
}
return false
}
func substring(str string, i int, iEnd int) string {
strLen := len(str)
if i < 0 {
i = 0
}
if i >= strLen {
return str
} else if iEnd < 0 || iEnd >= strLen {
return str[i:]
}
return str[i:iEnd]
}
func substringBefore(str, sep string, includeSeparator bool, cs caseSensitivity) string {
i := strIndexOf(str, sep, -1, cs)
if i < 0 {
return str
}
if includeSeparator {
i += len(sep)
}
return substring(str, 0, i)
}
// files
func cleanPath(path string) string {
if len(path) == 0 {
return path
}
path, err := filepath.Abs(path)
logFatalError(err)
return filepath.ToSlash(filepath.Clean(path))
}
func fileExists(path string) bool {
if len(path) == 0 {
return false
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func fileBasename(path string) string {
p := cleanPath(path)
if p[len(p)-1] == '/' {
p = p[0 : len(p)-1]
}
if i := strings.LastIndex(p, "/"); i != -1 {
p = p[i+1:]
}
if i := strings.LastIndex(p, "."); i != -1 {
p = p[0:i]
}
return p
}
// The returned ext is always lower-cased and contains a prefix "." dot (e.g. ".png")
func fileExtension(path string) string {
// Strip query from URLs http(s)://domain.com/path/to/my.jpg?id=123312
if strStartsWith(path, "http", caseInsensitive) && strContains(path, "?", caseSensitive) {
path = substringBefore(path, "?", false, caseSensitive)
}
return strings.ToLower(filepath.Ext(path))
}
func fileSize(path string) int64 {
if len(path) == 0 {
return 0
}
if fi, err := os.Stat(path); os.IsNotExist(err) {
return 0
} else {
return fi.Size()
}
}
// formatting
func formatInt(num int) string {
str := intToString(num)
for i := len(str) - 1; i > 2; i -= 3 {
if str[0:i-2] == "-" {
break
}
str = str[0:i-2] + " " + str[i-2:]
}
return str
}
func formatUInt(num uint) string {
str := uintToString(num)
for i := len(str) - 1; i > 2; i -= 3 {
str = str[0:i-2] + " " + str[i-2:]
}
return str
}
func intToString(num int) string {
return strconv.Itoa(num)
}
func uintToString(num uint) string {
return strconv.FormatUint(uint64(num), 10)
}
func formatFloat32(f float32, decimals int) string {
return strconv.FormatFloat(float64(f), 'f', decimals, 32)
}
func formatFloat64(f float64, decimals int) string {
return strconv.FormatFloat(f, 'f', decimals, 64)
}
func formatBytes(numBytes int64) string {
prefix := ""
numAbs := numBytes
if numBytes < 0 {
prefix = "-"
numAbs = -numBytes
}
if numAbs >= 1024 {
if numAbs >= 1024*1024 {
if numAbs >= 1024*1024*1024 {
return fmt.Sprintf("%s%.*f GB", prefix, 2, (float32(numAbs)/1024.0)/1024.0/1024.0)
}
return fmt.Sprintf("%s%.*f MB", prefix, 2, (float32(numAbs)/1024.0)/1024.0)
}
return fmt.Sprintf("%s%.*f kB", prefix, 2, float32(numAbs)/1024.0)
}
return fmt.Sprintf("%s%d B", prefix, numAbs)
}
func formatDurationSince(t time.Time) string {
return formatDuration(time.Since(t))
}
func formatDuration(d time.Duration) (duration string) {
if d.Minutes() < 1.0 {
// sec
duration = fmt.Sprintf("%ss", strconv.FormatFloat(d.Seconds(), 'f', 2, 64))
} else if d.Minutes() < 60.0 {
// min sec
s := math.Mod(d.Seconds(), 60.0)
duration = fmt.Sprintf("%dm %ss", int(math.Floor(d.Minutes())),
strconv.FormatFloat(s, 'f', 2, 64))
} else {
s := math.Mod(d.Seconds(), 60.0)
m := math.Mod(d.Minutes(), 60.0)
if d.Hours() < 24.0 {
// hour min sec
duration = fmt.Sprintf("%dh %dm %ss", int(math.Floor(d.Hours())),
int(math.Floor(m)), strconv.FormatFloat(s, 'f', 2, 64))
} else {
h := math.Mod(d.Hours(), 24.0)
days := d.Hours() / 24.0
if days < 7.0 {
// day hour min sec
duration = fmt.Sprintf("%dd %dh %dm %ss", int(math.Floor(days)), int(math.Floor(h)),
int(math.Floor(m)), strconv.FormatFloat(s, 'f', 2, 64))
} else {
// week day hour min sec
w := math.Floor(days / 7.0)
days := math.Mod(days, 7.0)
duration = fmt.Sprintf("%dw %dd %dh %dm %ss", int(w), int(math.Floor(days)),
int(math.Floor(h)), int(math.Floor(m)), strconv.FormatFloat(s, 'f', 2, 64))
}
}
}
return
}