-
Notifications
You must be signed in to change notification settings - Fork 1
/
bits.go
470 lines (391 loc) · 10.7 KB
/
bits.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package cunits
import "fmt"
// Bits represent a size in bits
type Bits uint64
// String allows direct reprensetation of Bit by calling GetHumanSizeRepresentation()
func (size Bits) String() string {
return size.GetHumanSizeRepresentation()
}
// GetHumanSizeAndSuffix returns the size in a human readable binary prefix of bytes format and the suffix
func (size Bits) GetHumanSizeAndSuffix() (float64, string) {
if size >= EiB {
return size.EiB(), "EiB"
}
if size >= PiB {
return size.PiB(), "PiB"
}
if size >= TiB {
return size.TiB(), "TiB"
}
if size >= GiB {
return size.GiB(), "GiB"
}
if size >= MiB {
return size.MiB(), "MiB"
}
if size >= KiB {
return size.KiB(), "KiB"
}
return size.Byte(), "B"
}
// GetHumanSizeRepresentation returns the size in a human readable binary prefix of bytes format
func (size Bits) GetHumanSizeRepresentation() string {
// if size >= YiB {
// return size.YiBString()
// }
// if size >= ZiB {
// return size.ZiBString()
// }
if size >= EiB {
return size.EiBString()
}
if size >= PiB {
return size.PiBString()
}
if size >= TiB {
return size.TiBString()
}
if size >= GiB {
return size.GiBString()
}
if size >= MiB {
return size.MiBString()
}
if size >= KiB {
return size.KiBString()
}
return size.ByteString()
}
// GetHumanSizeAndSuffix returns the size in a human readable binary prefix of bytes format and the suffix
func (size Bits) GetHumanSpeedAndSuffix() (float64, string) {
if size >= EiB {
return size.Ebit(), "Eb"
}
if size >= PiB {
return size.Pbit(), "Pb"
}
if size >= TiB {
return size.Tbit(), "Tb"
}
if size >= GiB {
return size.Gbit(), "Gb"
}
if size >= MiB {
return size.Mbit(), "Mb"
}
if size >= KiB {
return size.Kbit(), "Kb"
}
return float64(size), "b"
}
// GetHumanSpeedRepresentation returns the size in a human readable decimal prefix of bits format
func (size Bits) GetHumanSpeedRepresentation() string {
// if size >= Ybit {
// return size.YbitString()
// }
// if size >= Zbit {
// return size.ZbitString()
// }
if size >= Ebit {
return size.EbitString()
}
if size >= Pbit {
return size.PbitString()
}
if size >= Tbit {
return size.TbitString()
}
if size >= Gbit {
return size.GbitString()
}
if size >= Mbit {
return size.MbitString()
}
if size >= Kbit {
return size.KbitString()
}
return size.BitString()
}
/*
Base forms
*/
// BitString returns the size in bit with unit suffix
func (size Bits) BitString() string {
return fmt.Sprintf("%d b", size)
}
// Byte returns the size in byte
func (size Bits) Byte() float64 {
return float64(size) / 8
}
// ByteString returns the size in byte with unit suffix
func (size Bits) ByteString() string {
return fmt.Sprintf("%.2f B", size.Byte())
}
/*
Decimal prefix of Bit
*/
// Kbit returns the size in kilobit
func (size Bits) Kbit() float64 {
return float64(size) / Kbit
}
// KbitString returns the size in kilobit with unit suffix
func (size Bits) KbitString() string {
return fmt.Sprintf("%.2f Kb", size.Kbit())
}
// Mbit returns the size in megabit
func (size Bits) Mbit() float64 {
return float64(size) / Mbit
}
// MbitString returns the size in megabit with unit suffix
func (size Bits) MbitString() string {
return fmt.Sprintf("%.2f Mb", size.Mbit())
}
// Gbit returns the size in gigabit
func (size Bits) Gbit() float64 {
return float64(size) / Gbit
}
// GbitString returns the size in gigabit with unit suffix
func (size Bits) GbitString() string {
return fmt.Sprintf("%.2f Gb", size.Gbit())
}
// Tbit returns the size in terabit
func (size Bits) Tbit() float64 {
return float64(size) / Tbit
}
// TbitString returns the size in terabit with unit suffix
func (size Bits) TbitString() string {
return fmt.Sprintf("%.2f Tb", size.Tbit())
}
// Pbit returns the size in petabit
func (size Bits) Pbit() float64 {
return float64(size) / Pbit
}
// PbitString returns the size in petabit with unit suffix
func (size Bits) PbitString() string {
return fmt.Sprintf("%.2f Pb", size.Pbit())
}
// Ebit returns the size in exabit
func (size Bits) Ebit() float64 {
return float64(size) / Ebit
}
// EbitString returns the size in exabit with unit suffix
func (size Bits) EbitString() string {
return fmt.Sprintf("%.2f Eb", size.Ebit())
}
// Zbit returns the size in zettabit
func (size Bits) Zbit() float64 {
return float64(size) / Zbit
}
// ZbitString returns the size in zettabit with unit suffix (carefull with sub zeros !)
func (size Bits) ZbitString() string {
return fmt.Sprintf("%f Zb", size.Zbit())
}
// Ybit returns the size in yottabit
func (size Bits) Ybit() float64 {
return float64(size) / Ybit
}
// YbitString returns the size in yottabit with unit suffix (carefull with sub zeros !)
func (size Bits) YbitString() string {
return fmt.Sprintf("%f Yb", size.Ybit())
}
/*
Binary prefix of Bit
*/
// Kibit returns the size in kibibit
func (size Bits) Kibit() float64 {
return float64(size) / Kibit
}
// KibitString returns the size in kibibit with unit suffix
func (size Bits) KibitString() string {
return fmt.Sprintf("%.2f Kib", size.Kibit())
}
// Mibit returns the size in mebibit
func (size Bits) Mibit() float64 {
return float64(size) / Mibit
}
// MibitString returns the size in mebibit with unit suffix
func (size Bits) MibitString() string {
return fmt.Sprintf("%.2f Mib", size.Mibit())
}
// Gibit returns the size in gibibit
func (size Bits) Gibit() float64 {
return float64(size) / Gibit
}
// GibitString returns the size in gibibit with unit suffix
func (size Bits) GibitString() string {
return fmt.Sprintf("%.2f Gib", size.Gibit())
}
// Tibit returns the size in tebibit
func (size Bits) Tibit() float64 {
return float64(size) / Tibit
}
// TibitString returns the size in tebibit with unit suffix
func (size Bits) TibitString() string {
return fmt.Sprintf("%.2f Tib", size.Tibit())
}
// Pibit returns the size in pebibit
func (size Bits) Pibit() float64 {
return float64(size) / Pibit
}
// PibitString returns the size in pebibit with unit suffix
func (size Bits) PibitString() string {
return fmt.Sprintf("%.2f Pib", size.Pibit())
}
// Eibit returns the size in exbibit
func (size Bits) Eibit() float64 {
return float64(size) / Eibit
}
// EibitString returns the size in exbibit with unit suffix
func (size Bits) EibitString() string {
return fmt.Sprintf("%.2f Eib", size.Eibit())
}
// Zibit returns the size in zebibit
func (size Bits) Zibit() float64 {
return float64(size) / Zibit
}
// ZibitString returns the size in zebibit with unit suffix (carefull with sub zeros !)
func (size Bits) ZibitString() string {
return fmt.Sprintf("%f Zib", size.Zibit())
}
// Yibit returns the size in yobibit
func (size Bits) Yibit() float64 {
return float64(size) / Yibit
}
// YibitString returns the size in yobibit with unit suffix (carefull with sub zeros !)
func (size Bits) YibitString() string {
return fmt.Sprintf("%f Yib", size.Yibit())
}
/*
Decimal prefix of bytes
*/
// KB returns the size in kilobyte
func (size Bits) KB() float64 {
return float64(size) / KB
}
// KBString returns the size in kilobyte with unit suffix
func (size Bits) KBString() string {
return fmt.Sprintf("%.2f KB", size.KB())
}
// MB returns the size in megabyte
func (size Bits) MB() float64 {
return float64(size) / MB
}
// MBString returns the size in megabyte with unit suffix
func (size Bits) MBString() string {
return fmt.Sprintf("%.2f MB", size.MB())
}
// GB returns the size in gigabyte
func (size Bits) GB() float64 {
return float64(size) / GB
}
// GBString returns the size in gigabyte with unit suffix
func (size Bits) GBString() string {
return fmt.Sprintf("%.2f GB", size.GB())
}
// TB returns the size in terabyte
func (size Bits) TB() float64 {
return float64(size) / TB
}
// TBString returns the size in terabyte with unit suffix
func (size Bits) TBString() string {
return fmt.Sprintf("%.2f TB", size.TB())
}
// PB returns the size in petabyte
func (size Bits) PB() float64 {
return float64(size) / PB
}
// PBString returns the size in petabyte with unit suffix
func (size Bits) PBString() string {
return fmt.Sprintf("%.2f PB", size.PB())
}
// EB returns the size in exabyte
func (size Bits) EB() float64 {
return float64(size) / EB
}
// EBString returns the size in exabyte with unit suffix
func (size Bits) EBString() string {
return fmt.Sprintf("%.2f EB", size.EB())
}
// ZB returns the size in zettabyte
func (size Bits) ZB() float64 {
return float64(size) / ZB
}
// ZBString returns the size in zettabyte with unit suffix (carefull with sub zeros !)
func (size Bits) ZBString() string {
return fmt.Sprintf("%f ZB", size.ZB())
}
// YB returns the size in yottabyte
func (size Bits) YB() float64 {
return float64(size) / YB
}
// YBString returns the size in yottabyte with unit suffix (carefull with sub zeros !)
func (size Bits) YBString() string {
return fmt.Sprintf("%f YB", size.YB())
}
/*
Binary prefix of bytes
*/
// KiB returns the size in kibibyte
func (size Bits) KiB() float64 {
return float64(size) / KiB
}
// KiBString returns the size in kibibyte with unit suffix
func (size Bits) KiBString() string {
return fmt.Sprintf("%.2f KiB", size.KiB())
}
// MiB returns the size in mebibyte
func (size Bits) MiB() float64 {
return float64(size) / MiB
}
// MiBString returns the size in mebibyte with unit suffix
func (size Bits) MiBString() string {
return fmt.Sprintf("%.2f MiB", size.MiB())
}
// GiB returns the size in gibibyte
func (size Bits) GiB() float64 {
return float64(size) / GiB
}
// GiBString returns the size in gibibyte with unit suffix
func (size Bits) GiBString() string {
return fmt.Sprintf("%.2f GiB", size.GiB())
}
// TiB returns the size in tebibyte
func (size Bits) TiB() float64 {
return float64(size) / TiB
}
// TiBString returns the size in tebibyte wit unit suffix
func (size Bits) TiBString() string {
return fmt.Sprintf("%.2f TiB", size.TiB())
}
// PiB returns the size in pebibyte
func (size Bits) PiB() float64 {
return float64(size) / PiB
}
// PiBString returns the size in pebibyte with unit suffix
func (size Bits) PiBString() string {
return fmt.Sprintf("%.2f PiB", size.PiB())
}
// EiB returns the size in exbibyte
func (size Bits) EiB() float64 {
return float64(size) / EiB
}
// EiBString returns the size in exbibyte with unit suffix (carefull with sub zeros !)
func (size Bits) EiBString() string {
return fmt.Sprintf("%f EiB", size.EiB())
}
// ZiB returns the size in zebibyte
func (size Bits) ZiB() float64 {
return float64(size) / ZiB
}
// ZiBString returns the size in zebibyte with unit suffix (carefull with sub zeros !)
func (size Bits) ZiBString() string {
return fmt.Sprintf("%f ZiB", size.ZiB())
}
// YiB returns the size in yobibyte
func (size Bits) YiB() float64 {
return float64(size) / YiB
}
// YiBString returns the size in yobibyte with unit suffix (carefull with sub zeros !)
func (size Bits) YiBString() string {
return fmt.Sprintf("%f YiB", size.YiB())
}