11## ByteSize
2- [ ![ Build Status] ( https://travis-ci.org/hyunsik/bytesize.svg?branch=master )] ( https://travis-ci.org/hyunsik/bytesize )
2+
3+ [ ![ CI] ( https://github.com/hyunsik/bytesize/actions/workflows/rust.yml/badge.svg )] ( https://github.com/hyunsik/bytesize/actions/workflows/rust.yml )
34[ ![ Crates.io Version] ( https://img.shields.io/crates/v/bytesize.svg )] ( https://crates.io/crates/bytesize )
45
6+ Forked from https://github.com/hyunsik/bytesize .
57
68ByteSize is an utility for human-readable byte count representation.
79
810Features:
9- * Pre-defined constants for various size units (e.g., B, Kb, kib , Mb, Mib, Gb, Gib, ... PB)
11+ * Pre-defined constants for various size units (e.g., B, Kb, Kib , Mb, Mib, Gb, Gib, ... PB)
1012* ` ByteSize ` type which presents size units convertible to different size units.
1113* Artimetic operations for ` ByteSize `
1214* FromStr impl for ` ByteSize ` , allowing to parse from string size representations like 1.5KiB and 521TiB.
@@ -20,99 +22,79 @@ Add this to your Cargo.toml:
2022
2123``` toml
2224[dependencies ]
23- bytesize = {version = " 1.2.0" , features = [" serde" ]}
24- ```
25-
26- and this to your crate root:
27- ``` rust
28- extern crate bytesize;
25+ bytesize = { version = " 2" , features = [" serde" ]}
2926```
3027
3128## Example
29+
3230### Human readable representations (SI unit and Binary unit)
33- ``` rust
34- #[allow(dead_code)]
35- fn assert_display (expected : & str , b : ByteSize ) {
36- assert_eq! (expected , format! (" {}" , b ));
37- }
3831
39- #[test]
40- fn test_display () {
41- assert_display (" 215 B" , ByteSize (215 ));
42- assert_display (" 215 B" , ByteSize :: b (215 ));
43- assert_display (" 1.0 KB" , ByteSize :: kb (1 ));
44- assert_display (" 301.0 KB" , ByteSize :: kb (301 ));
45- assert_display (" 419.0 MB" , ByteSize :: mb (419 ));
46- assert_display (" 518.0 GB" , ByteSize :: gb (518 ));
47- assert_display (" 815.0 TB" , ByteSize :: tb (815 ));
48- assert_display (" 609.0 PB" , ByteSize :: pb (609 ));
49- }
50-
51- fn assert_to_string (expected : & str , b : ByteSize , si : bool ) {
52- assert_eq! (expected . to_string (), b . to_string_as (si ));
53- }
54-
55- #[test]
56- fn test_to_string () {
57- assert_to_string (" 215 B" , ByteSize (215 ), true );
58- assert_to_string (" 215 B" , ByteSize (215 ), false );
59-
60- assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
61- assert_to_string (" 215 B" , ByteSize :: b (215 ), false );
62-
63- assert_to_string (" 1.0 kiB" , ByteSize :: kib (1 ), true );
64- assert_to_string (" 1.0 KB" , ByteSize :: kib (1 ), false );
65-
66- assert_to_string (" 293.9 kiB" , ByteSize :: kb (301 ), true );
67- assert_to_string (" 301.0 KB" , ByteSize :: kb (301 ), false );
68-
69- assert_to_string (" 1.0 MiB" , ByteSize :: mib (1 ), true );
70- assert_to_string (" 1048.6 KB" , ByteSize :: mib (1 ), false );
71-
72- assert_to_string (" 399.6 MiB" , ByteSize :: mb (419 ), true );
73- assert_to_string (" 419.0 MB" , ByteSize :: mb (419 ), false );
74-
75- assert_to_string (" 482.4 GiB" , ByteSize :: gb (518 ), true );
76- assert_to_string (" 518.0 GB" , ByteSize :: gb (518 ), false );
77-
78- assert_to_string (" 741.2 TiB" , ByteSize :: tb (815 ), true );
79- assert_to_string (" 815.0 TB" , ByteSize :: tb (815 ), false );
80-
81- assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), true );
82- assert_to_string (" 609.0 PB" , ByteSize :: pb (609 ), false );
83- }
84-
85- #[test]
86- fn test_parsing_from_str () {
87- // shortcut for writing test cases
88- fn parse (s : & str ) -> u64 {
89- s . parse :: <ByteSize >(). unwrap (). 0
90- }
91-
92- assert_eq! (" 0" . parse :: <ByteSize >(). unwrap (). 0 , 0 );
93- assert_eq! (parse (" 0" ), 0 );
94- assert_eq! (parse (" 500" ), 500 );
95- assert_eq! (parse (" 1K" ), Unit :: KiloByte * 1 );
96- assert_eq! (parse (" 1Ki" ), Unit :: KibiByte * 1 );
97- assert_eq! (parse (" 1.5Ki" ), (1.5 * Unit :: KibiByte ) as u64 );
98- assert_eq! (parse (" 1KiB" ), 1 * Unit :: KibiByte );
99- assert_eq! (parse (" 1.5KiB" ), (1.5 * Unit :: KibiByte ) as u64 );
100- assert_eq! (parse (" 3 MB" ), Unit :: MegaByte * 3 );
101- assert_eq! (parse (" 4 MiB" ), Unit :: MebiByte * 4 );
102- assert_eq! (parse (" 6 GB" ), 6 * Unit :: GigaByte );
103- assert_eq! (parse (" 4 GiB" ), 4 * Unit :: GibiByte );
104- assert_eq! (parse (" 88TB" ), 88 * Unit :: TeraByte );
105- assert_eq! (parse (" 521TiB" ), 521 * Unit :: TebiByte );
106- assert_eq! (parse (" 8 PB" ), 8 * Unit :: PetaByte );
107- assert_eq! (parse (" 8P" ), 8 * Unit :: PetaByte );
108- assert_eq! (parse (" 12 PiB" ), 12 * Unit :: PebiByte );
109- }
32+ ``` rust
33+ fn assert_display (expected : & str , b : ByteSize ) {
34+ assert_eq! (expected , format! (" {}" , b ));
35+ }
36+
37+ #[test]
38+ fn test_display () {
39+ assert_display (" 215 B" , ByteSize :: b (215 ));
40+ assert_display (" 1.0 KiB" , ByteSize :: kib (1 ));
41+ assert_display (" 301.0 KiB" , ByteSize :: kib (301 ));
42+ assert_display (" 419.0 MiB" , ByteSize :: mib (419 ));
43+ assert_display (" 518.0 GiB" , ByteSize :: gib (518 ));
44+ assert_display (" 815.0 TiB" , ByteSize :: tib (815 ));
45+ assert_display (" 609.0 PiB" , ByteSize :: pib (609 ));
46+ }
47+
48+ #[test]
49+ fn test_display_alignment () {
50+ assert_eq! (" |357 B |" , format! (" |{:10}|" , ByteSize (357 )));
51+ assert_eq! (" | 357 B|" , format! (" |{:>10}|" , ByteSize (357 )));
52+ assert_eq! (" |357 B |" , format! (" |{:<10}|" , ByteSize (357 )));
53+ assert_eq! (" | 357 B |" , format! (" |{:^10}|" , ByteSize (357 )));
54+
55+ assert_eq! (" |-----357 B|" , format! (" |{:->10}|" , ByteSize (357 )));
56+ assert_eq! (" |357 B-----|" , format! (" |{:-<10}|" , ByteSize (357 )));
57+ assert_eq! (" |--357 B---|" , format! (" |{:-^10}|" , ByteSize (357 )));
58+ }
59+
60+ fn assert_to_string (expected : & str , b : ByteSize , si : bool ) {
61+ assert_eq! (expected . to_string (), b . to_string_as (si ));
62+ }
63+
64+ #[test]
65+ fn test_to_string_as () {
66+ assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
67+ assert_to_string (" 215 B" , ByteSize :: b (215 ), false );
68+
69+ assert_to_string (" 1.0 KiB" , ByteSize :: kib (1 ), true );
70+ assert_to_string (" 1.0 KB" , ByteSize :: kib (1 ), false );
71+
72+ assert_to_string (" 293.9 KiB" , ByteSize :: kb (301 ), true );
73+ assert_to_string (" 301.0 KB" , ByteSize :: kb (301 ), false );
74+
75+ assert_to_string (" 1.0 MiB" , ByteSize :: mib (1 ), true );
76+ assert_to_string (" 1048.6 KB" , ByteSize :: mib (1 ), false );
77+
78+ // a bug case: https://github.com/flang-project/bytesize/issues/8
79+ assert_to_string (" 1.9 GiB" , ByteSize :: mib (1907 ), true );
80+ assert_to_string (" 2.0 GB" , ByteSize :: mib (1908 ), false );
81+
82+ assert_to_string (" 399.6 MiB" , ByteSize :: mb (419 ), true );
83+ assert_to_string (" 419.0 MB" , ByteSize :: mb (419 ), false );
84+
85+ assert_to_string (" 482.4 GiB" , ByteSize :: gb (518 ), true );
86+ assert_to_string (" 518.0 GB" , ByteSize :: gb (518 ), false );
87+
88+ assert_to_string (" 741.2 TiB" , ByteSize :: tb (815 ), true );
89+ assert_to_string (" 815.0 TB" , ByteSize :: tb (815 ), false );
90+
91+ assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), true );
92+ assert_to_string (" 609.0 PB" , ByteSize :: pb (609 ), false );
93+ }
11094```
11195
11296### Arithmetic operations
11397``` rust
114- extern crate bytesize;
115-
11698use bytesize :: ByteSize ;
11799
118100fn byte_arithmetic_operator () {
0 commit comments