@@ -179,12 +179,15 @@ impl ByteSize {
179179pub fn to_string ( bytes : u64 , si_prefix : bool ) -> String {
180180 let unit = if si_prefix { KB } else { KIB } ;
181181 let unit_base = if si_prefix { LN_KB } else { LN_KIB } ;
182- let unit_prefix = if si_prefix {
183- UNITS_SI . as_bytes ( )
184- } else {
185- UNITS . as_bytes ( )
182+
183+ let unit_prefix = match si_prefix {
184+ true => UNITS_SI . as_bytes ( ) ,
185+ false => UNITS . as_bytes ( ) ,
186+ } ;
187+ let unit_suffix = match si_prefix {
188+ true => "B" ,
189+ false => "iB" ,
186190 } ;
187- let unit_suffix = if si_prefix { "B" } else { "iB" } ;
188191
189192 if bytes < unit {
190193 format ! ( "{} B" , bytes)
@@ -206,7 +209,7 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String {
206209
207210impl Display for ByteSize {
208211 fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
209- f. pad ( & to_string ( self . 0 , true ) )
212+ f. pad ( & to_string ( self . 0 , false ) )
210213 }
211214}
212215
@@ -440,10 +443,9 @@ mod tests {
440443 assert_to_string ( "293.9 KiB" , ByteSize :: kb ( 301 ) , false ) ;
441444 assert_to_string ( "301.0 kB" , ByteSize :: kb ( 301 ) , true ) ;
442445
443- assert_to_string ( "1 .0 MiB " , ByteSize :: mib ( 1 ) , false ) ;
444- assert_to_string ( "1048.6 kB " , ByteSize :: mib ( 1 ) , true ) ;
446+ assert_to_string ( "1024 .0 KiB " , ByteSize :: mib ( 1 ) , false ) ;
447+ assert_to_string ( "1.0 MB " , ByteSize :: mib ( 1 ) , true ) ;
445448
446- // a bug case: https://github.com/flang-project/bytesize/issues/8
447449 assert_to_string ( "1.9 GiB" , ByteSize :: mib ( 1907 ) , false ) ;
448450 assert_to_string ( "2.0 GB" , ByteSize :: mib ( 1908 ) , true ) ;
449451
0 commit comments