forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
64 lines (55 loc) · 1.38 KB
/
format.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
package fmtutil
import (
"encoding/json"
"github.com/gookit/goutil/byteutil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil"
)
// data size
const (
OneKByte = 1024
OneMByte = 1024 * 1024
OneGByte = 1024 * 1024 * 1024
)
// DataSize format bytes number friendly.
//
// Usage:
//
// file, err := os.Open(path)
// fl, err := file.Stat()
// fmtSize := DataSize(fl.Size())
func DataSize(size uint64) string {
return mathutil.DataSize(size)
}
// SizeToString alias of the DataSize
func SizeToString(size uint64) string { return DataSize(size) }
// StringToByte alias of the ParseByte
func StringToByte(sizeStr string) uint64 { return ParseByte(sizeStr) }
// ParseByte converts size string like 1GB/1g or 12mb/12M into an unsigned integer number of bytes
func ParseByte(sizeStr string) uint64 {
val, _ := strutil.ToByteSize(sizeStr)
return val
}
// PrettyJSON get pretty Json string
func PrettyJSON(v any) (string, error) {
out, err := json.MarshalIndent(v, "", " ")
return string(out), err
}
// ArgsWithSpaces it like Println, will add spaces for each argument
func ArgsWithSpaces(vs []any) (message string) {
ln := len(vs)
if ln == 0 {
return ""
}
if ln == 1 {
return strutil.SafeString(vs[0])
}
bs := make([]byte, 0, ln*8)
for i := range vs {
if i > 0 { // add space
bs = append(bs, ' ')
}
bs = byteutil.AppendAny(bs, vs[i])
}
return string(bs)
}