24
24
//! ```
25
25
//! use bytesize::ByteSize;
26
26
//!
27
- //! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true ));
28
- //! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false ));
27
+ //! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false ));
28
+ //! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true ));
29
29
//! ```
30
30
//!
31
31
//! Arithmetic operations are supported.
@@ -71,10 +71,28 @@ pub const TIB: u64 = 1_099_511_627_776;
71
71
/// bytes size for 1 pebibyte
72
72
pub const PIB : u64 = 1_125_899_906_842_624 ;
73
73
74
- static UNITS : & str = "KMGTPE" ;
75
- static UNITS_SI : & str = "KMGTPE" ;
76
- static LN_KB : f64 = 6.931471806 ; // ln 1024
77
- static LN_KIB : f64 = 6.907755279 ; // ln 1000
74
+ /// IEC (binary) units.
75
+ ///
76
+ /// See <https://en.wikipedia.org/wiki/Kilobyte>.
77
+ const UNITS_IEC : & str = "KMGTPE" ;
78
+
79
+ /// SI (decimal) units.
80
+ ///
81
+ /// See <https://en.wikipedia.org/wiki/Kilobyte>.
82
+ const UNITS_SI : & str = "kMGTPE" ;
83
+
84
+ /// `ln(1024) ~= 6.931`
85
+ const LN_KIB : f64 = 6.931_471_805_599_453 ;
86
+
87
+ /// `ln(1000) ~= 6.908`
88
+ const LN_KB : f64 = 6.907_755_278_982_137 ;
89
+
90
+ #[ derive( Debug , Clone , Default ) ]
91
+ pub enum Format {
92
+ #[ default]
93
+ IEC ,
94
+ SI ,
95
+ }
78
96
79
97
pub fn kb < V : Into < u64 > > ( size : V ) -> u64 {
80
98
size. into ( ) * KB
@@ -188,15 +206,28 @@ impl ByteSize {
188
206
}
189
207
}
190
208
191
- pub fn to_string ( bytes : u64 , si_prefix : bool ) -> String {
192
- let unit = if si_prefix { KIB } else { KB } ;
193
- let unit_base = if si_prefix { LN_KIB } else { LN_KB } ;
194
- let unit_prefix = if si_prefix {
195
- UNITS_SI . as_bytes ( )
196
- } else {
197
- UNITS . as_bytes ( )
209
+ pub fn to_string ( bytes : u64 , si_unit : bool ) -> String {
210
+ to_string_format ( bytes, if si_unit { Format :: SI } else { Format :: IEC } )
211
+ }
212
+
213
+ pub fn to_string_format ( bytes : u64 , format : Format ) -> String {
214
+ let unit = match format {
215
+ Format :: IEC => KIB ,
216
+ Format :: SI => KB ,
217
+ } ;
218
+ let unit_base = match format {
219
+ Format :: IEC => LN_KIB ,
220
+ Format :: SI => LN_KB ,
221
+ } ;
222
+
223
+ let unit_prefix = match format {
224
+ Format :: IEC => UNITS_IEC . as_bytes ( ) ,
225
+ Format :: SI => UNITS_SI . as_bytes ( ) ,
226
+ } ;
227
+ let unit_suffix = match format {
228
+ Format :: IEC => "iB" ,
229
+ Format :: SI => "B" ,
198
230
} ;
199
- let unit_suffix = if si_prefix { "iB" } else { "B" } ;
200
231
201
232
if bytes < unit {
202
233
format ! ( "{} B" , bytes)
@@ -218,13 +249,13 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String {
218
249
219
250
impl Display for ByteSize {
220
251
fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
221
- f. pad ( & to_string ( self . 0 , true ) )
252
+ f. pad ( & to_string_format ( self . 0 , Format :: IEC ) )
222
253
}
223
254
}
224
255
225
256
impl Debug for ByteSize {
226
257
fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
227
- write ! ( f , "{}" , self )
258
+ < Self as Display > :: fmt ( self , f )
228
259
}
229
260
}
230
261
@@ -408,6 +439,7 @@ mod tests {
408
439
assert ! ( ByteSize :: b( 0 ) < ByteSize :: tib( 1 ) ) ;
409
440
}
410
441
442
+ #[ track_caller]
411
443
fn assert_display ( expected : & str , b : ByteSize ) {
412
444
assert_eq ! ( expected, format!( "{}" , b) ) ;
413
445
}
@@ -435,39 +467,39 @@ mod tests {
435
467
assert_eq ! ( "|--357 B---|" , format!( "|{:-^10}|" , ByteSize ( 357 ) ) ) ;
436
468
}
437
469
438
- fn assert_to_string ( expected : & str , b : ByteSize , si : bool ) {
439
- assert_eq ! ( expected. to_string( ) , b. to_string_as( si) ) ;
470
+ #[ track_caller]
471
+ fn assert_to_string ( expected : & str , b : ByteSize , format : Format ) {
472
+ assert_eq ! ( expected. to_string( ) , to_string_format( b. 0 , format) ) ;
440
473
}
441
474
442
475
#[ test]
443
476
fn test_to_string_as ( ) {
444
- assert_to_string ( "215 B" , ByteSize :: b ( 215 ) , true ) ;
445
- assert_to_string ( "215 B" , ByteSize :: b ( 215 ) , false ) ;
477
+ assert_to_string ( "215 B" , ByteSize :: b ( 215 ) , Format :: IEC ) ;
478
+ assert_to_string ( "215 B" , ByteSize :: b ( 215 ) , Format :: SI ) ;
446
479
447
- assert_to_string ( "1.0 KiB" , ByteSize :: kib ( 1 ) , true ) ;
448
- assert_to_string ( "1.0 KB " , ByteSize :: kib ( 1 ) , false ) ;
480
+ assert_to_string ( "1.0 KiB" , ByteSize :: kib ( 1 ) , Format :: IEC ) ;
481
+ assert_to_string ( "1.0 kB " , ByteSize :: kib ( 1 ) , Format :: SI ) ;
449
482
450
- assert_to_string ( "293.9 KiB" , ByteSize :: kb ( 301 ) , true ) ;
451
- assert_to_string ( "301.0 KB " , ByteSize :: kb ( 301 ) , false ) ;
483
+ assert_to_string ( "293.9 KiB" , ByteSize :: kb ( 301 ) , Format :: IEC ) ;
484
+ assert_to_string ( "301.0 kB " , ByteSize :: kb ( 301 ) , Format :: SI ) ;
452
485
453
- assert_to_string ( "1.0 MiB" , ByteSize :: mib ( 1 ) , true ) ;
454
- assert_to_string ( "1048.6 KB " , ByteSize :: mib ( 1 ) , false ) ;
486
+ assert_to_string ( "1.0 MiB" , ByteSize :: mib ( 1 ) , Format :: IEC ) ;
487
+ assert_to_string ( "1.0 MB " , ByteSize :: mib ( 1 ) , Format :: SI ) ;
455
488
456
- // a bug case: https://github.com/flang-project/bytesize/issues/8
457
- assert_to_string ( "1.9 GiB" , ByteSize :: mib ( 1907 ) , true ) ;
458
- assert_to_string ( "2.0 GB" , ByteSize :: mib ( 1908 ) , false ) ;
489
+ assert_to_string ( "1.9 GiB" , ByteSize :: mib ( 1907 ) , Format :: IEC ) ;
490
+ assert_to_string ( "2.0 GB" , ByteSize :: mib ( 1908 ) , Format :: SI ) ;
459
491
460
- assert_to_string ( "399.6 MiB" , ByteSize :: mb ( 419 ) , true ) ;
461
- assert_to_string ( "419.0 MB" , ByteSize :: mb ( 419 ) , false ) ;
492
+ assert_to_string ( "399.6 MiB" , ByteSize :: mb ( 419 ) , Format :: IEC ) ;
493
+ assert_to_string ( "419.0 MB" , ByteSize :: mb ( 419 ) , Format :: SI ) ;
462
494
463
- assert_to_string ( "482.4 GiB" , ByteSize :: gb ( 518 ) , true ) ;
464
- assert_to_string ( "518.0 GB" , ByteSize :: gb ( 518 ) , false ) ;
495
+ assert_to_string ( "482.4 GiB" , ByteSize :: gb ( 518 ) , Format :: IEC ) ;
496
+ assert_to_string ( "518.0 GB" , ByteSize :: gb ( 518 ) , Format :: SI ) ;
465
497
466
- assert_to_string ( "741.2 TiB" , ByteSize :: tb ( 815 ) , true ) ;
467
- assert_to_string ( "815.0 TB" , ByteSize :: tb ( 815 ) , false ) ;
498
+ assert_to_string ( "741.2 TiB" , ByteSize :: tb ( 815 ) , Format :: IEC ) ;
499
+ assert_to_string ( "815.0 TB" , ByteSize :: tb ( 815 ) , Format :: SI ) ;
468
500
469
- assert_to_string ( "540.9 PiB" , ByteSize :: pb ( 609 ) , true ) ;
470
- assert_to_string ( "609.0 PB" , ByteSize :: pb ( 609 ) , false ) ;
501
+ assert_to_string ( "540.9 PiB" , ByteSize :: pb ( 609 ) , Format :: IEC ) ;
502
+ assert_to_string ( "609.0 PB" , ByteSize :: pb ( 609 ) , Format :: SI ) ;
471
503
}
472
504
473
505
#[ test]
@@ -477,6 +509,6 @@ mod tests {
477
509
478
510
#[ test]
479
511
fn test_to_string ( ) {
480
- assert_to_string ( "609.0 PB" , ByteSize :: pb ( 609 ) , false ) ;
512
+ assert_to_string ( "609.0 PB" , ByteSize :: pb ( 609 ) , Format :: SI ) ;
481
513
}
482
514
}
0 commit comments