1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! # Humanize Bytes
//!  
//! Format a number of bytes as a human-readable string.
//! 
//! See for more info: <https://en.wikipedia.org/wiki/Binary_prefix>
//! 
//! 1 KB = 1000 B
//! 
//! 1 KiB = 1024 B
//! 
//! ```rust
//! use humanize_bytes::{humanize_bytes_decimal, humanize_bytes_binary, humanize_quantity};
//!  
//! assert_eq!(humanize_bytes_binary!(0), "0 B");
//! assert_eq!(humanize_bytes_binary!(512), "512 B");
//! assert_eq!(humanize_bytes_binary!(1023), "1023 B");
//! assert_eq!(humanize_bytes_binary!(1024), "1 KiB");
//! assert_eq!(humanize_bytes_binary!(1024 + 99), "1 KiB");
//! assert_eq!(humanize_bytes_binary!(1024 + 103), "1.1 KiB");
//! assert_eq!(humanize_bytes_binary!(1024 * 1024 - 1), "1023.9 KiB");
//! assert_eq!(humanize_bytes_binary!(1024 * 1024), "1 MiB");
//! assert_eq!(humanize_bytes_binary!(1024 * 1024 * 1024), "1 GiB");
//!
//! assert_eq!(humanize_bytes_decimal!(0), "0 B");
//! assert_eq!(humanize_bytes_decimal!(512), "512 B");
//! assert_eq!(humanize_bytes_decimal!(999), "999 B");
//! assert_eq!(humanize_bytes_decimal!(1000), "1 kB");
//! assert_eq!(humanize_bytes_decimal!(1000 + 99), "1 kB");
//! assert_eq!(humanize_bytes_decimal!(1000 + 100), "1.1 kB");
//! assert_eq!(humanize_bytes_decimal!(1000 * 1000 - 1), "999.9 kB");
//! assert_eq!(humanize_bytes_decimal!(1000 * 1000), "1 MB");
//! assert_eq!(humanize_bytes_decimal!(1000 * 1000 * 1000), "1 GB");
//!
//! assert_eq!(humanize_quantity!(0), "0");
//! assert_eq!(humanize_quantity!(512), "512");
//! assert_eq!(humanize_quantity!(999), "999");
//! assert_eq!(humanize_quantity!(1000), "1 k");
//! assert_eq!(humanize_quantity!(1000 + 99), "1 k");
//! assert_eq!(humanize_quantity!(1000 + 100), "1.1 k");
//! assert_eq!(humanize_quantity!(1000 * 1000 - 1), "999.9 k");
//! assert_eq!(humanize_quantity!(1000 * 1000), "1 M");
//! ```
//!

pub use smartstring;

mod binary {

    ///
    /// Format a number of bytes as a human-readable string, using the IEC binary suffixes.
    ///
    /// 1024 (B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB, RiB, QiB)
    /// 
    #[macro_export]
    macro_rules! humanize_bytes_binary {
        ($value:expr) => {
            {
                use ::humanize_bytes::smartstring::{SmartString, LazyCompact};
                use ::core::fmt::Write;
                let num_bytes = { $value } as f64;
                if num_bytes <= 0.0 {
                    "0 B".into()
                } else if num_bytes < 1024.0 {
                    let mut result = SmartString::<LazyCompact>::new();
                    write!(result, "{} B", num_bytes as u16).unwrap();
                    result
                } else {
                    const SUFFIX: [&str; 11] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "RiB", "QiB"];
                    const UNIT: f64 = 1024.0;
                    let base = num_bytes.log2() as usize / 10;
                    let curr_base = UNIT.powi(base as i32) as f64;
                    let units = num_bytes / curr_base;
                    let units = (units * 100.0).floor() / 100.0;
                    let mut once = true;
                    let mut extra = SmartString::<LazyCompact>::new();
                    write!(extra, "{:.2}", units).unwrap();
                    let trimmed = extra 
                        .trim_end_matches(|_| if once { once = false; true } else { false })
                        .trim_end_matches("0")
                        .trim_end_matches(".");
                    let mut result: SmartString<LazyCompact> = trimmed.into();
                    result.push_str(" ");
                    result.push_str(SUFFIX[base as usize]);
                    result
                }
            }
        }
    }

    pub use humanize_bytes_binary;
}

mod decimal {

    ///
    /// Format a number of bytes as a human-readable string, using the SI decimal suffixes.
    ///
    /// 1000 (B, kB, MB, GB, TB, PB, EB, ZB, YB) 
    #[macro_export]
    macro_rules! humanize_bytes_decimal {
        ($value:expr) => {
            {
                use ::humanize_bytes::smartstring::{SmartString, LazyCompact};
                use ::core::fmt::Write;
                let num_bytes = { $value } as f64;
                if num_bytes <= 0.0 {
                    "0 B".into()
                } else if num_bytes < 1000.0 {
                    let mut result = SmartString::<LazyCompact>::new();
                    write!(result, "{} B", num_bytes as u16).unwrap();
                    result
                } else {
                    const SUFFIX: [&str; 11] = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "RB", "QB"];
                    const UNIT: f64 = 1000.0;
                    let base = num_bytes.log10() as usize / 3;
                    let curr_base = UNIT.powi(base as i32) as f64;
                    let units = num_bytes / curr_base;
                    let units = (units * 100.0).floor() / 100.0;
                    let mut once = true;
                    let mut extra = SmartString::<LazyCompact>::new();
                    write!(extra, "{:.2}", units).unwrap();
                    let trimmed = extra 
                        .trim_end_matches(|_| if once { once = false; true } else { false })
                        .trim_end_matches("0")
                        .trim_end_matches(".");
                    let mut result: SmartString<LazyCompact> = trimmed.into();
                    result.push_str(" ");
                    result.push_str(SUFFIX[base as usize]);
                    result                
                }
            }        
        }
    }

    pub use humanize_bytes_decimal;


    ///
    /// Format a number of bytes as a human-readable string, using the SI decimal suffixes.
    ///
    /// Factors of 1000 (, k, M, G, T, P, E, Z, Y, R, Q) 
    /// kilo, mega, giga, tera, peta, exa, zetta, yotta, ronna, quetta
    /// 
    #[macro_export]
    macro_rules! humanize_quantity {
        ($value:expr) => {
            {
                use ::humanize_bytes::smartstring::{SmartString, LazyCompact};
                use ::core::fmt::Write;
                let num_bytes = { $value } as f64;
                if num_bytes <= 0.0 {
                    "0".into()
                } else if num_bytes < 1000.0 {
                    let mut result = SmartString::<LazyCompact>::new();
                    write!(result, "{}", num_bytes as u16).unwrap();
                    result
                } else {
                    const SUFFIX: [&str; 11] = ["", "k", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"];
                    const UNIT: f64 = 1000.0;
                    let base = num_bytes.log10() as usize / 3;
                    let curr_base = UNIT.powi(base as i32) as f64;
                    let units = num_bytes / curr_base;
                    let units = (units * 100.0).floor() / 100.0;
                    let mut once = true;
                    let mut extra = SmartString::<LazyCompact>::new();
                    write!(extra, "{:.2}", units).unwrap();
                    let trimmed = extra 
                        .trim_end_matches(|_| if once { once = false; true } else { false })
                        .trim_end_matches("0")
                        .trim_end_matches(".");
                    let mut result: SmartString<LazyCompact> = trimmed.into();
                    result.push_str(" ");
                    result.push_str(SUFFIX[base as usize]);
                    result                
                }
            }        
        }
    }

    pub use humanize_quantity;
}