1package cunits
2
3import "fmt"
4
5// Bits represent a size in bits
6type Bits uint64
7
8// String allows direct reprensetation of Bit by calling GetHumanSizeRepresentation()
9func (size Bits) String() string {
10	return size.GetHumanSizeRepresentation()
11}
12
13// GetHumanSizeRepresentation returns the size in a human readable binary prefix of bytes format
14func (size Bits) GetHumanSizeRepresentation() string {
15	// if size >= YiB {
16	// 	return size.YiBString()
17	// }
18	// if size >= ZiB {
19	// 	return size.ZiBString()
20	// }
21	if size >= EiB {
22		return size.EiBString()
23	}
24	if size >= PiB {
25		return size.PiBString()
26	}
27	if size >= TiB {
28		return size.TiBString()
29	}
30	if size >= GiB {
31		return size.GiBString()
32	}
33	if size >= MiB {
34		return size.MiBString()
35	}
36	if size >= KiB {
37		return size.KiBString()
38	}
39	return size.ByteString()
40}
41
42// GetHumanSpeedRepresentation returns the size in a human readable decimal prefix of bits format
43func (size Bits) GetHumanSpeedRepresentation() string {
44	// if size >= Ybit {
45	// 	return size.YbitString()
46	// }
47	// if size >= Zbit {
48	// 	return size.ZbitString()
49	// }
50	if size >= Ebit {
51		return size.EbitString()
52	}
53	if size >= Pbit {
54		return size.PbitString()
55	}
56	if size >= Tbit {
57		return size.TbitString()
58	}
59	if size >= Gbit {
60		return size.GbitString()
61	}
62	if size >= Mbit {
63		return size.MbitString()
64	}
65	if size >= Kbit {
66		return size.KbitString()
67	}
68	return size.BitString()
69}
70
71/*
72	Base forms
73*/
74
75// BitString returns the size in bit with unit suffix
76func (size Bits) BitString() string {
77	return fmt.Sprintf("%d b", size)
78}
79
80// Byte returns the size in byte
81func (size Bits) Byte() float64 {
82	return float64(size) / 8
83}
84
85// ByteString returns the size in byte with unit suffix
86func (size Bits) ByteString() string {
87	return fmt.Sprintf("%.2f B", size.Byte())
88}
89
90/*
91	Decimal prefix of Bit
92*/
93
94// Kbit returns the size in kilobit
95func (size Bits) Kbit() float64 {
96	return float64(size) / Kbit
97}
98
99// KbitString returns the size in kilobit with unit suffix
100func (size Bits) KbitString() string {
101	return fmt.Sprintf("%.2f Kb", size.Kbit())
102}
103
104// Mbit returns the size in megabit
105func (size Bits) Mbit() float64 {
106	return float64(size) / Mbit
107}
108
109// MbitString returns the size in megabit with unit suffix
110func (size Bits) MbitString() string {
111	return fmt.Sprintf("%.2f Mb", size.Mbit())
112}
113
114// Gbit returns the size in gigabit
115func (size Bits) Gbit() float64 {
116	return float64(size) / Gbit
117}
118
119// GbitString returns the size in gigabit with unit suffix
120func (size Bits) GbitString() string {
121	return fmt.Sprintf("%.2f Gb", size.Gbit())
122}
123
124// Tbit returns the size in terabit
125func (size Bits) Tbit() float64 {
126	return float64(size) / Tbit
127}
128
129// TbitString returns the size in terabit with unit suffix
130func (size Bits) TbitString() string {
131	return fmt.Sprintf("%.2f Tb", size.Tbit())
132}
133
134// Pbit returns the size in petabit
135func (size Bits) Pbit() float64 {
136	return float64(size) / Pbit
137}
138
139// PbitString returns the size in petabit with unit suffix
140func (size Bits) PbitString() string {
141	return fmt.Sprintf("%.2f Pb", size.Pbit())
142}
143
144// Ebit returns the size in exabit
145func (size Bits) Ebit() float64 {
146	return float64(size) / Ebit
147}
148
149// EbitString returns the size in exabit with unit suffix
150func (size Bits) EbitString() string {
151	return fmt.Sprintf("%.2f Eb", size.Ebit())
152}
153
154// Zbit returns the size in zettabit
155func (size Bits) Zbit() float64 {
156	return float64(size) / Zbit
157}
158
159// ZbitString returns the size in zettabit with unit suffix (carefull with sub zeros !)
160func (size Bits) ZbitString() string {
161	return fmt.Sprintf("%f Zb", size.Zbit())
162}
163
164// Ybit returns the size in yottabit
165func (size Bits) Ybit() float64 {
166	return float64(size) / Ybit
167}
168
169// YbitString returns the size in yottabit with unit suffix (carefull with sub zeros !)
170func (size Bits) YbitString() string {
171	return fmt.Sprintf("%f Yb", size.Ybit())
172}
173
174/*
175	Binary prefix of Bit
176*/
177
178// Kibit returns the size in kibibit
179func (size Bits) Kibit() float64 {
180	return float64(size) / Kibit
181}
182
183// KibitString returns the size in kibibit with unit suffix
184func (size Bits) KibitString() string {
185	return fmt.Sprintf("%.2f Kib", size.Kibit())
186}
187
188// Mibit returns the size in mebibit
189func (size Bits) Mibit() float64 {
190	return float64(size) / Mibit
191}
192
193// MibitString returns the size in mebibit with unit suffix
194func (size Bits) MibitString() string {
195	return fmt.Sprintf("%.2f Mib", size.Mibit())
196}
197
198// Gibit returns the size in gibibit
199func (size Bits) Gibit() float64 {
200	return float64(size) / Gibit
201}
202
203// GibitString returns the size in gibibit with unit suffix
204func (size Bits) GibitString() string {
205	return fmt.Sprintf("%.2f Gib", size.Gibit())
206}
207
208// Tibit returns the size in tebibit
209func (size Bits) Tibit() float64 {
210	return float64(size) / Tibit
211}
212
213// TibitString returns the size in tebibit with unit suffix
214func (size Bits) TibitString() string {
215	return fmt.Sprintf("%.2f Tib", size.Tibit())
216}
217
218// Pibit returns the size in pebibit
219func (size Bits) Pibit() float64 {
220	return float64(size) / Pibit
221}
222
223// PibitString returns the size in pebibit with unit suffix
224func (size Bits) PibitString() string {
225	return fmt.Sprintf("%.2f Pib", size.Pibit())
226}
227
228// Eibit returns the size in exbibit
229func (size Bits) Eibit() float64 {
230	return float64(size) / Eibit
231}
232
233// EibitString returns the size in exbibit with unit suffix
234func (size Bits) EibitString() string {
235	return fmt.Sprintf("%.2f Eib", size.Eibit())
236}
237
238// Zibit returns the size in zebibit
239func (size Bits) Zibit() float64 {
240	return float64(size) / Zibit
241}
242
243// ZibitString returns the size in zebibit with unit suffix (carefull with sub zeros !)
244func (size Bits) ZibitString() string {
245	return fmt.Sprintf("%f Zib", size.Zibit())
246}
247
248// Yibit returns the size in yobibit
249func (size Bits) Yibit() float64 {
250	return float64(size) / Yibit
251}
252
253// YibitString returns the size in yobibit with unit suffix (carefull with sub zeros !)
254func (size Bits) YibitString() string {
255	return fmt.Sprintf("%f Yib", size.Yibit())
256}
257
258/*
259	Decimal prefix of bytes
260*/
261
262// KB returns the size in kilobyte
263func (size Bits) KB() float64 {
264	return float64(size) / KB
265}
266
267// KBString returns the size in kilobyte with unit suffix
268func (size Bits) KBString() string {
269	return fmt.Sprintf("%.2f KB", size.KB())
270}
271
272// MB returns the size in megabyte
273func (size Bits) MB() float64 {
274	return float64(size) / MB
275}
276
277// MBString returns the size in megabyte with unit suffix
278func (size Bits) MBString() string {
279	return fmt.Sprintf("%.2f MB", size.MB())
280}
281
282// GB returns the size in gigabyte
283func (size Bits) GB() float64 {
284	return float64(size) / GB
285}
286
287// GBString returns the size in gigabyte with unit suffix
288func (size Bits) GBString() string {
289	return fmt.Sprintf("%.2f GB", size.GB())
290}
291
292// TB returns the size in terabyte
293func (size Bits) TB() float64 {
294	return float64(size) / TB
295}
296
297// TBString returns the size in terabyte with unit suffix
298func (size Bits) TBString() string {
299	return fmt.Sprintf("%.2f TB", size.TB())
300}
301
302// PB returns the size in petabyte
303func (size Bits) PB() float64 {
304	return float64(size) / PB
305}
306
307// PBString returns the size in petabyte with unit suffix
308func (size Bits) PBString() string {
309	return fmt.Sprintf("%.2f PB", size.PB())
310}
311
312// EB returns the size in exabyte
313func (size Bits) EB() float64 {
314	return float64(size) / EB
315}
316
317// EBString returns the size in exabyte with unit suffix
318func (size Bits) EBString() string {
319	return fmt.Sprintf("%.2f EB", size.EB())
320}
321
322// ZB returns the size in zettabyte
323func (size Bits) ZB() float64 {
324	return float64(size) / ZB
325}
326
327// ZBString returns the size in zettabyte with unit suffix (carefull with sub zeros !)
328func (size Bits) ZBString() string {
329	return fmt.Sprintf("%f ZB", size.ZB())
330}
331
332// YB returns the size in yottabyte
333func (size Bits) YB() float64 {
334	return float64(size) / YB
335}
336
337// YBString returns the size in yottabyte with unit suffix (carefull with sub zeros !)
338func (size Bits) YBString() string {
339	return fmt.Sprintf("%f YB", size.YB())
340}
341
342/*
343	Binary prefix of bytes
344*/
345
346// KiB returns the size in kibibyte
347func (size Bits) KiB() float64 {
348	return float64(size) / KiB
349}
350
351// KiBString returns the size in kibibyte with unit suffix
352func (size Bits) KiBString() string {
353	return fmt.Sprintf("%.2f KiB", size.KiB())
354}
355
356// MiB returns the size in mebibyte
357func (size Bits) MiB() float64 {
358	return float64(size) / MiB
359}
360
361// MiBString returns the size in mebibyte with unit suffix
362func (size Bits) MiBString() string {
363	return fmt.Sprintf("%.2f MiB", size.MiB())
364}
365
366// GiB returns the size in gibibyte
367func (size Bits) GiB() float64 {
368	return float64(size) / GiB
369}
370
371// GiBString returns the size in gibibyte with unit suffix
372func (size Bits) GiBString() string {
373	return fmt.Sprintf("%.2f GiB", size.GiB())
374}
375
376// TiB returns the size in tebibyte
377func (size Bits) TiB() float64 {
378	return float64(size) / TiB
379}
380
381// TiBString returns the size in tebibyte wit unit suffix
382func (size Bits) TiBString() string {
383	return fmt.Sprintf("%.2f TiB", size.TiB())
384}
385
386// PiB returns the size in pebibyte
387func (size Bits) PiB() float64 {
388	return float64(size) / PiB
389}
390
391// PiBString returns the size in pebibyte with unit suffix
392func (size Bits) PiBString() string {
393	return fmt.Sprintf("%.2f PiB", size.PiB())
394}
395
396// EiB returns the size in exbibyte
397func (size Bits) EiB() float64 {
398	return float64(size) / EiB
399}
400
401// EiBString returns the size in exbibyte with unit suffix (carefull with sub zeros !)
402func (size Bits) EiBString() string {
403	return fmt.Sprintf("%f EiB", size.EiB())
404}
405
406// ZiB returns the size in zebibyte
407func (size Bits) ZiB() float64 {
408	return float64(size) / ZiB
409}
410
411// ZiBString returns the size in zebibyte with unit suffix (carefull with sub zeros !)
412func (size Bits) ZiBString() string {
413	return fmt.Sprintf("%f ZiB", size.ZiB())
414}
415
416// YiB returns the size in yobibyte
417func (size Bits) YiB() float64 {
418	return float64(size) / YiB
419}
420
421// YiBString returns the size in yobibyte with unit suffix (carefull with sub zeros !)
422func (size Bits) YiBString() string {
423	return fmt.Sprintf("%f YiB", size.YiB())
424}
425