-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinteger.go
212 lines (187 loc) · 3.82 KB
/
integer.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
package utils
import (
"fmt"
"math"
"math/big"
"sort"
"strconv"
"strings"
)
// MaxInt 整数取大值
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
// MinInt 整数取小值
func MinInt(a, b int) int {
if a < b {
return a
}
return b
}
// SumInt 整数和
func SumInt(v ...int) int {
x := 0
for _, n := range v {
x += n
}
return x
}
// GetInt 获取 int 结果, 可选指定默认值(若给定了默认值,则返回正整数或 0)
func GetInt(v interface{}, defaultInt ...int) int {
i := MustInt(v)
if i <= 0 && len(defaultInt) > 0 {
return defaultInt[0]
}
return i
}
// SearchInt 搜索整数位置(左, 第一个)
func SearchInt(slice []int, n int) int {
for i, v := range slice {
if n == v {
return i
}
}
return -1
}
// InInts 检查整数是否存在于 slice
func InInts(slice []int, n int) bool {
return SearchInt(slice, n) != -1
}
// Commai 整数转千分位分隔字符串
func Commai(v int) string {
return Comma(int64(v))
}
// Comma 整数转千分位分隔字符串
// Ref: dustin/go-humanize
// e.g. Comma(834142) -> 834,142
func Comma(v int64) string {
sign := ""
// Min int64 can't be negated to a usable value, so it has to be special cased.
if v == math.MinInt64 {
return "-9,223,372,036,854,775,808"
}
if v < 0 {
sign = "-"
v = 0 - v
}
parts := []string{"", "", "", "", "", "", ""}
j := len(parts) - 1
for v > 999 {
parts[j] = strconv.FormatInt(v%1000, 10)
switch len(parts[j]) {
case 2:
parts[j] = "0" + parts[j]
case 1:
parts[j] = "00" + parts[j]
}
v = v / 1000
j--
}
parts[j] = strconv.Itoa(int(v))
return sign + strings.Join(parts[j:], ",")
}
// Commau 整数转千分位分隔字符串
// Ref: dustin/go-humanize
func Commau(v uint64) string {
sign := ""
parts := []string{"", "", "", "", "", "", ""}
j := len(parts) - 1
for v > 999 {
parts[j] = strconv.FormatUint(v%1000, 10)
switch len(parts[j]) {
case 2:
parts[j] = "0" + parts[j]
case 1:
parts[j] = "00" + parts[j]
}
v = v / 1000
j--
}
parts[j] = strconv.Itoa(int(v))
return sign + strings.Join(parts[j:], ",")
}
// BigComma big.Int 千分位分隔字符串
// Ref: dustin/go-humanize
func BigComma(b *big.Int) string {
sign := ""
if b.Sign() < 0 {
sign = "-"
b.Abs(b)
}
athousand := big.NewInt(1000)
c := (&big.Int{}).Set(b)
_, m := Bigoom(c, athousand)
parts := make([]string, m+1)
j := len(parts) - 1
mod := &big.Int{}
for b.Cmp(athousand) >= 0 {
b.DivMod(b, athousand, mod)
parts[j] = strconv.FormatInt(mod.Int64(), 10)
switch len(parts[j]) {
case 2:
parts[j] = "0" + parts[j]
case 1:
parts[j] = "00" + parts[j]
}
j--
}
parts[j] = strconv.Itoa(int(b.Int64()))
return sign + strings.Join(parts[j:], ",")
}
// Bigoom big.Int 总数量级
// Ref: dustin/go-humanize
func Bigoom(n, b *big.Int) (float64, int) {
mag := 0
m := &big.Int{}
for n.Cmp(b) >= 0 {
n.DivMod(n, b, m)
mag++
}
return float64(n.Int64()) + (float64(m.Int64()) / float64(b.Int64())), mag
}
// ParseInts 解析字符串, 得到去重并排序后的数字列表
// "5,0-3, 3" => [0,1,2,3,5]
func ParseInts(s string) ([]int, error) {
var (
r []int
e error
)
set := make(map[int]struct{})
blocks := strings.Split(s, ",")
for _, v := range blocks {
ss := strings.Split(v, "-")
if len(ss) == 1 {
i, err := strconv.Atoi(strings.TrimSpace(ss[0]))
if err != nil {
e = err
continue
}
set[i] = struct{}{}
} else if len(ss) == 2 {
start, err := strconv.Atoi(strings.TrimSpace(ss[0]))
if err != nil {
e = err
continue
}
end, err := strconv.Atoi(strings.TrimSpace(ss[1]))
if err != nil {
e = err
continue
}
for i := start; i <= end; i++ {
set[i] = struct{}{}
}
}
}
if len(set) == 0 {
return nil, fmt.Errorf("invalid integer range")
}
for n := range set {
r = append(r, n)
}
sort.Ints(r)
return r, e
}