forked from anbinh/dna
/
int.go
294 lines (256 loc) · 6.29 KB
/
int.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package dna
import (
"database/sql/driver"
"errors"
"fmt"
"math"
"math/rand"
"strconv"
"strings"
"time"
"unicode"
)
// Redefine new Int Type
type Int int
// ToString returns String from Int
func (i Int) ToString() String {
return String(fmt.Sprint(i))
}
// ToFormattedString returns a new string from Int with given width.
//
// If width is greater than the length of all digits of the number,
// it will fill with either zero or whitespace.
// If width is positive, the padding is filled from left to right.
// Otherwise, it's filled from right to left.
//
// Special case: when zero padding enable, it's only filled to the left
func (i Int) ToFormattedString(width Int, hasZeroPadding Bool) String {
if hasZeroPadding {
return String(fmt.Sprintf("%0[2]*[1]d", i, int(width)))
} else {
return String(fmt.Sprintf("%[2]*[1]d", i, int(width)))
}
}
// Value returns primitive int type
func (i Int) ToPrimitiveType() int {
return int(i)
}
// Value returns primitive int type
func (i Int) ToPrimitiveValue() int {
return int(i)
}
// Value implements the Valuer interface in database/sql/driver package.
func (i Int) Value() (driver.Value, error) {
return driver.Value(int64(i.ToPrimitiveValue())), nil
}
// Scan implements the Scanner interface in database/sql package.
// Default value for nil is 0
func (i *Int) Scan(src interface{}) error {
var source Int
switch src.(type) {
case int64:
source = Int(int(src.(int64)))
case nil:
source = 0
default:
return errors.New("Incompatible type for dna.Int type")
}
*i = source
return nil
}
// ToFloat returns Float from Int
func (i Int) ToFloat() Float {
return Float(float64(int(i)))
}
// ToHex returns hex string from Int
func (i Int) ToHex() String {
return String(fmt.Sprintf("%x", i))
}
// ToTimeFormat returns int as seconds from 1970-01-01 to format "2013-05-14 11:00:00"
// It is used to work with postgresql
func (i Int) ToTimeFormat() String {
tm := time.Unix(int64(i), 0)
year := fmt.Sprint(tm.Year())
month := fmt.Sprintf("%d", tm.Month())
day := fmt.Sprint(tm.Day())
hour := fmt.Sprint(tm.Hour())
min := fmt.Sprint(tm.Minute())
sec := fmt.Sprint(tm.Second())
ret := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
return String(ret)
}
func (i Int) ToTime() time.Time {
return time.Unix(int64(i), 0)
}
// ToBin returns binary string from Int
func (i Int) ToBin() String {
return String(fmt.Sprintf("%b", i))
}
// Rand returns random number from [0,n)
func (i Int) Rand() Int {
rand.Seed(time.Now().Unix())
return Int(rand.Int31n(int32(i)))
}
// IsDivisibleBy returns true if the integer visible by x
func (i Int) IsDivisibleBy(x Int) Bool {
if i%x == 0 {
return true
} else {
return false
}
}
// IsEven returns true if a number is even
func (i Int) IsEven() Bool {
return i.IsDivisibleBy(2)
}
// IsNegative returns true if a number is negative
func (i Int) IsNegative() Bool {
if i < 0 {
return true
} else {
return false
}
}
// IsOdd returns true if a number is odd
func (i Int) IsOdd() Bool {
return !i.IsDivisibleBy(2)
}
// IsPositive returns true if a number is positive
func (i Int) IsPositive() Bool {
return !i.IsNegative()
}
// CONVERTS INTEGER TO HUMAN READABLE STRING. CREDIT GOES TO http://godoc.org/github.com/dustin/go-humanize
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
func humanateBytes(s uint64, base float64, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%dB", s)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := float64(s) / math.Pow(base, math.Floor(e))
f := "%.0f"
if val < 10 {
f = "%.1f"
}
return fmt.Sprintf(f+"%s", val, suffix)
}
// ToBytesFormat produces a human readable representation of an SI size.
// Bytes(67343643) -> 67MB
func (i Int) ToBytesFormat() String {
if i.IsPositive() {
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
return String(humanateBytes(uint64(i), 1000, sizes))
} else {
return "0B"
}
}
// IBytes produces a human readable representation of an IEC size.
// IBytes(82854982) -> 79MiB
func (i Int) ToIBytesFormat() String {
if i.IsPositive() {
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
return String(humanateBytes(uint64(i), 1024, sizes))
} else {
return "0B"
}
}
// Comma produces a string form of the given number in base 10 with
// commas after every three orders of magnitude.
// e.g. Comma(834142) -> 834,142
func (i Int) ToCommaFormat() String {
v := int64(i)
sign := ""
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 String(sign + strings.Join(parts[j:len(parts)], ","))
}
var bytesSizeTable = map[string]uint64{
"b": bytez,
"kib": kiByte,
"kb": kByte,
"mib": miByte,
"mb": mByte,
"gib": giByte,
"gb": gByte,
"tib": tiByte,
"tb": tByte,
"pib": piByte,
"pb": pByte,
"eib": eiByte,
"eb": eByte,
// Without suffix
"": bytez,
"ki": kiByte,
"k": kByte,
"mi": miByte,
"m": mByte,
"gi": giByte,
"g": gByte,
"ti": tiByte,
"t": tByte,
"pi": piByte,
"p": pByte,
"ei": eiByte,
"e": eByte,
}
const (
bytez = 1
kiByte = bytez * 1024
miByte = kiByte * 1024
giByte = miByte * 1024
tiByte = giByte * 1024
piByte = tiByte * 1024
eiByte = piByte * 1024
)
// SI Sizes.
const (
ibyte = 1
kByte = ibyte * 1000
mByte = kByte * 1000
gByte = mByte * 1000
tByte = gByte * 1000
pByte = tByte * 1000
eByte = pByte * 1000
)
// ParseBytesFormat parses a string representation of bytes into the number
// of bytes it represents.
// ParseBytesFormat("42MB") -> 42000000
// ParseBytesFormat("42mib") -> 44040192
func (s String) ParseBytesFormat() Int {
lastDigit := 0
for _, r := range s {
if !(unicode.IsDigit(r) || r == '.') {
break
}
lastDigit++
}
f, err := strconv.ParseFloat(string(s)[:lastDigit], 64)
if err != nil {
fmt.Printf("Error occurs at ParseBytesFormat(). %v", err)
return 0
}
extra := strings.ToLower(strings.TrimSpace(string(s)[lastDigit:]))
if m, ok := bytesSizeTable[extra]; ok {
return Int(uint64(f * float64(m)))
}
fmt.Printf("Unhandled size name: %v. Error occurs at ParseBytesFormat()", extra)
return 0
}