Skip to content

Commit 6af6432

Browse files
committed
wip
1 parent 86cdb98 commit 6af6432

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

src/lib.rs

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,42 +170,66 @@ impl ByteSize {
170170
}
171171

172172
#[inline(always)]
173-
pub fn to_string_as(&self, si_unit: bool) -> String {
174-
to_string(self.0, si_unit)
173+
pub fn to_string_as(&self, si_unit: bool, simple: bool) -> String {
174+
to_string(self.0, si_unit, simple)
175175
}
176176
}
177177

178-
pub fn to_string(bytes: u64, si_prefix: bool) -> String {
178+
pub fn to_string(bytes: u64, si_prefix: bool, simple: bool) -> String {
179179
let unit = if si_prefix { KIB } else { KB };
180180
let unit_base = if si_prefix { LN_KIB } else { LN_KB };
181181
let unit_prefix = if si_prefix {
182182
UNITS_SI.as_bytes()
183183
} else {
184184
UNITS.as_bytes()
185185
};
186-
let unit_suffix = if si_prefix { "iB" } else { "B" };
186+
187+
let unit_suffix = if simple {
188+
""
189+
} else if si_prefix {
190+
"iB"
191+
} else {
192+
"B"
193+
};
187194

188195
if bytes < unit {
189-
format!("{} B", bytes)
196+
if simple {
197+
format!("{}", bytes)
198+
} else {
199+
format!("{} B", bytes)
200+
}
190201
} else {
191202
let size = bytes as f64;
192203
let exp = match (size.ln() / unit_base) as usize {
193204
e if e == 0 => 1,
194205
e => e,
195206
};
196207

208+
println!("exp: {}", exp);
209+
println!("value: {}", (size / unit.pow(exp as u32) as f64));
210+
211+
let prefix = if simple {
212+
format!("{}", unit_prefix[exp - 1] as char).to_uppercase()
213+
} else {
214+
format!(" {}", unit_prefix[exp - 1] as char)
215+
};
216+
217+
println!("unit_prefix: {}", unit_prefix[exp - 1] as char);
218+
println!("prefix: {}", prefix);
219+
println!("suffix: {}", unit_suffix);
220+
197221
format!(
198-
"{:.1} {}{}",
222+
"{:.1}{}{}",
199223
(size / unit.pow(exp as u32) as f64),
200-
unit_prefix[exp - 1] as char,
224+
prefix,
201225
unit_suffix
202226
)
203227
}
204228
}
205229

206230
impl Display for ByteSize {
207231
fn fmt(&self, f: &mut Formatter) -> Result {
208-
write!(f, "{}", to_string(self.0, false))
232+
write!(f, "{}", to_string(self.0, false, false))
209233
}
210234
}
211235

@@ -319,7 +343,7 @@ mod tests {
319343
}
320344

321345
fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
322-
assert_eq!(expected.to_string(), b.to_string_as(si));
346+
assert_eq!(expected.to_string(), b.to_string_as(si, false));
323347
}
324348

325349
#[test]
@@ -362,4 +386,39 @@ mod tests {
362386
fn test_to_string() {
363387
assert_to_string("609.0 PB", ByteSize::pb(609), false);
364388
}
389+
390+
fn assert_simple(expected: &str, b: ByteSize, si: bool) {
391+
assert_eq!(expected.to_string(), b.to_string_as(si, true));
392+
}
393+
394+
#[test]
395+
fn test_simple() {
396+
assert_simple("215", ByteSize::b(215), true);
397+
assert_simple("215", ByteSize::b(215), false);
398+
399+
assert_simple("1.0K", ByteSize::kib(1), true);
400+
assert_simple("1.0K", ByteSize::kib(1), false);
401+
402+
assert_simple("293.9K", ByteSize::kb(301), true);
403+
assert_simple("301.0K", ByteSize::kb(301), false);
404+
405+
assert_simple("1.0M", ByteSize::mib(1), true);
406+
assert_simple("1048.6K", ByteSize::mib(1), false);
407+
408+
// a bug case: https://github.com/flang-project/bytesize/issues/8
409+
assert_simple("1.9G", ByteSize::mib(1907), true);
410+
assert_simple("2.0G", ByteSize::mib(1908), false);
411+
412+
assert_simple("399.6M", ByteSize::mb(419), true);
413+
assert_simple("419.0M", ByteSize::mb(419), false);
414+
415+
assert_simple("482.4G", ByteSize::gb(518), true);
416+
assert_simple("518.0G", ByteSize::gb(518), false);
417+
418+
assert_simple("741.2T", ByteSize::tb(815), true);
419+
assert_simple("815.0T", ByteSize::tb(815), false);
420+
421+
assert_simple("540.9P", ByteSize::pb(609), true);
422+
assert_simple("609.0P", ByteSize::pb(609), false);
423+
}
365424
}

0 commit comments

Comments
 (0)