@@ -171,41 +171,63 @@ impl ByteSize {
171171
172172 #[ inline( always) ]
173173 pub fn to_string_as ( & self , si_unit : bool ) -> String {
174- to_string ( self . 0 , si_unit)
174+ to_string ( self . 0 , si_unit, false )
175+ }
176+
177+ #[ inline( always) ]
178+ pub fn to_simple_string_as ( & self , si_unit : bool , simple : bool ) -> String {
179+ to_string ( self . 0 , si_unit, simple)
175180 }
176181}
177182
178- pub fn to_string ( bytes : u64 , si_prefix : bool ) -> String {
183+ pub fn to_string ( bytes : u64 , si_prefix : bool , simple : bool ) -> String {
179184 let unit = if si_prefix { KIB } else { KB } ;
180185 let unit_base = if si_prefix { LN_KIB } else { LN_KB } ;
181186 let unit_prefix = if si_prefix {
182187 UNITS_SI . as_bytes ( )
183188 } else {
184189 UNITS . as_bytes ( )
185190 } ;
186- let unit_suffix = if si_prefix { "iB" } else { "B" } ;
191+
192+ let unit_suffix = if simple {
193+ ""
194+ } else if si_prefix {
195+ "iB"
196+ } else {
197+ "B"
198+ } ;
187199
188200 if bytes < unit {
189- format ! ( "{} B" , bytes)
201+ if simple {
202+ format ! ( "{}" , bytes)
203+ } else {
204+ format ! ( "{} B" , bytes)
205+ }
190206 } else {
191207 let size = bytes as f64 ;
192208 let exp = match ( size. ln ( ) / unit_base) as usize {
193209 e if e == 0 => 1 ,
194210 e => e,
195211 } ;
196212
213+ let prefix = if simple {
214+ format ! ( "{}" , unit_prefix[ exp - 1 ] as char ) . to_uppercase ( )
215+ } else {
216+ format ! ( " {}" , unit_prefix[ exp - 1 ] as char )
217+ } ;
218+
197219 format ! (
198- "{:.1} {}{}" ,
220+ "{:.1}{}{}" ,
199221 ( size / unit. pow( exp as u32 ) as f64 ) ,
200- unit_prefix [ exp - 1 ] as char ,
222+ prefix ,
201223 unit_suffix
202224 )
203225 }
204226}
205227
206228impl Display for ByteSize {
207229 fn fmt ( & self , f : & mut Formatter ) -> Result {
208- write ! ( f, "{}" , to_string( self . 0 , false ) )
230+ write ! ( f, "{}" , to_string( self . 0 , false , false ) )
209231 }
210232}
211233
@@ -362,4 +384,39 @@ mod tests {
362384 fn test_to_string ( ) {
363385 assert_to_string ( "609.0 PB" , ByteSize :: pb ( 609 ) , false ) ;
364386 }
387+
388+ fn assert_simple ( expected : & str , b : ByteSize , si : bool ) {
389+ assert_eq ! ( expected. to_string( ) , b. to_simple_string_as( si, true ) ) ;
390+ }
391+
392+ #[ test]
393+ fn test_simple ( ) {
394+ assert_simple ( "215" , ByteSize :: b ( 215 ) , true ) ;
395+ assert_simple ( "215" , ByteSize :: b ( 215 ) , false ) ;
396+
397+ assert_simple ( "1.0K" , ByteSize :: kib ( 1 ) , true ) ;
398+ assert_simple ( "1.0K" , ByteSize :: kib ( 1 ) , false ) ;
399+
400+ assert_simple ( "293.9K" , ByteSize :: kb ( 301 ) , true ) ;
401+ assert_simple ( "301.0K" , ByteSize :: kb ( 301 ) , false ) ;
402+
403+ assert_simple ( "1.0M" , ByteSize :: mib ( 1 ) , true ) ;
404+ assert_simple ( "1048.6K" , ByteSize :: mib ( 1 ) , false ) ;
405+
406+ // a bug case: https://github.com/flang-project/bytesize/issues/8
407+ assert_simple ( "1.9G" , ByteSize :: mib ( 1907 ) , true ) ;
408+ assert_simple ( "2.0G" , ByteSize :: mib ( 1908 ) , false ) ;
409+
410+ assert_simple ( "399.6M" , ByteSize :: mb ( 419 ) , true ) ;
411+ assert_simple ( "419.0M" , ByteSize :: mb ( 419 ) , false ) ;
412+
413+ assert_simple ( "482.4G" , ByteSize :: gb ( 518 ) , true ) ;
414+ assert_simple ( "518.0G" , ByteSize :: gb ( 518 ) , false ) ;
415+
416+ assert_simple ( "741.2T" , ByteSize :: tb ( 815 ) , true ) ;
417+ assert_simple ( "815.0T" , ByteSize :: tb ( 815 ) , false ) ;
418+
419+ assert_simple ( "540.9P" , ByteSize :: pb ( 609 ) , true ) ;
420+ assert_simple ( "609.0P" , ByteSize :: pb ( 609 ) , false ) ;
421+ }
365422}
0 commit comments