unit.go 3.31 KB
Newer Older
liming6's avatar
liming6 committed
1
2
3
4
5
6
7
8
9
10
11
package utils

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

type StorageCapacityUnit uint64

liming6's avatar
liming6 committed
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
func (u StorageCapacityUnit) String() string {
	switch u {
	case Byte:
		return "Byte"
	case KB:
		return "KB"
	case MB:
		return "MB"
	case GB:
		return "GB"
	case TB:
		return "TB"
	case PB:
		return "PB"
	case KiB:
		return "KiB"
	case MiB:
		return "MiB"
	case GiB:
		return "GiB"
	case TiB:
		return "TiB"
	case PiB:
		return "PiB"
	}
	return "unknow unit"
}

liming6's avatar
liming6 committed
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
const (
	Byte StorageCapacityUnit = 1
	KB   StorageCapacityUnit = Byte * 1000
	MB   StorageCapacityUnit = KB * 1000
	GB   StorageCapacityUnit = MB * 1000
	TB   StorageCapacityUnit = GB * 1000
	PB   StorageCapacityUnit = TB * 1000

	KiB StorageCapacityUnit = 1 << 10
	MiB StorageCapacityUnit = 1 << 20
	GiB StorageCapacityUnit = 1 << 30
	TiB StorageCapacityUnit = 1 << 40
	PiB StorageCapacityUnit = 1 << 50
)

var (
	ReStorageSize = regexp.MustCompile(`^([1-9][0-9]*)((?:[KMGTPkmgtp]|)i?[bB])$`)
	ReUnit        = regexp.MustCompile(`^([kmgtpKMGTP]|)(|i)([bB])$`)
)

// MemorySize 内存大小,Num代表数字,Unit代表单位
type MemorySize struct {
	Num  uint64
	Unit StorageCapacityUnit
}

66
func (s MemorySize) HumanReadStr(i int) string {
liming6's avatar
liming6 committed
67
68
69
70
71
72
73
74
75
76
77
78
	total := s.Num * uint64(s.Unit)
	units := []StorageCapacityUnit{Byte, KiB, MiB, GiB, TiB, PiB}
	var target StorageCapacityUnit
	for k, v := range units {
		if total/uint64(v) < 1 {
			target = units[k-1]
			break
		} else {
			target = v
		}
	}
	num := float64(total) / float64(target)
79
80
81
82
83
84
85
86
87
88
89
90
91
	switch i {
	case 0:
		return fmt.Sprintf("%d %s", int(num), target)
	case 1:
		return fmt.Sprintf("%.1f %s", num, target)
	case 2:
		return fmt.Sprintf("%.2f %s", num, target)
	case 3:
		return fmt.Sprintf("%.3f %s", num, target)
	default:
		return fmt.Sprintf("%.3f %s", num, target)
	}

liming6's avatar
liming6 committed
92
93
}

liming6's avatar
liming6 committed
94
95
96
97
98
99
100
101
102
103
104
func ParseUnit(s string) (StorageCapacityUnit, error) {
	s = strings.Trim(strings.TrimSpace(s), "\n")
	s = strings.ReplaceAll(s, " ", "")
	if !ReUnit.MatchString(s) {
		return 0, fmt.Errorf("invalid storage size unit: %s", s)
	}
	s = strings.ToLower(s)
	// [MiB M i B]
	// [KB K '' B]
	// [B '' '' B]
	matchs := ReUnit.FindStringSubmatch(s)
liming6's avatar
liming6 committed
105
	if len(matchs) < 4 {
liming6's avatar
liming6 committed
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
		return 0, fmt.Errorf("invalid storage size unit: %s", s)
	}
	isI := matchs[2] == "i"
	switch matchs[1] {
	case "":
		return Byte, nil
	case "k":
		if isI {
			return KiB, nil
		}
		return KB, nil
	case "m":
		if isI {
			return MiB, nil
		}
		return MB, nil
	case "g":
		if isI {
			return GiB, nil
		}
		return GB, nil
	case "t":
		if isI {
			return TiB, nil
		}
		return TB, nil
	case "p":
		if isI {
			return PiB, nil
		}
		return PB, nil
	default:
		return 0, fmt.Errorf("invalid storage size unit: %s", s)
	}
}

// ParseMemorySize 解析容量字符串,支持的格式有:123MiB 123MB "123 MiB" "123 MB"
func ParseMemorySize(s string) (*MemorySize, error) {
	s = strings.TrimSpace(strings.Trim(s, " \n"))
	s = strings.ReplaceAll(s, " ", "")
	if !ReStorageSize.MatchString(s) {
		return nil, fmt.Errorf("invalid memory size format: %s", s)
	}
	matchs := ReStorageSize.FindStringSubmatch(s)
liming6's avatar
liming6 committed
150
	if len(matchs) < 3 {
liming6's avatar
liming6 committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
		return nil, fmt.Errorf("invalid memory size format: %s", s)
	}

	num, err := strconv.ParseUint(matchs[1], 10, 64)
	if err != nil {
		return nil, err
	}
	result := MemorySize{}
	result.Num = num
	unit, err := ParseUnit(matchs[2])
	if err != nil {
		return nil, err
	}
	result.Unit = unit
	return &result, nil
}