Skip to content

Commit e9a0c0b

Browse files
authored
Merge pull request #11531 from T0mstone/binary-prefixes
og_image: Use binary prefixes for `format_bytes`
2 parents ede6512 + 6a41d5d commit e9a0c0b

7 files changed

+32
-32
lines changed

crates/crates_io_og_image/src/formatting.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use serde::Serializer;
88
/// Formats a byte size value into a human-readable string.
99
///
1010
/// The function follows these rules:
11-
/// - Uses units: B, kB and MB
12-
/// - Switches from B to kB at 1500 bytes
13-
/// - Switches from kB to MB at 1500 * 1024 bytes
11+
/// - Uses units: B, KiB and MiB
12+
/// - Switches from B to KiB at 1500 bytes
13+
/// - Switches from KiB to MiB at 1500 * 1024 bytes
1414
/// - Limits the number to a maximum of 4 characters by adjusting decimal places
1515
///
1616
/// # Arguments
@@ -22,7 +22,7 @@ use serde::Serializer;
2222
/// A formatted string representing the size with appropriate units
2323
pub fn format_bytes(bytes: u32) -> String {
2424
const THRESHOLD: f64 = 1500.;
25-
const UNITS: &[&str] = &["B", "kB", "MB"];
25+
const UNITS: &[&str] = &["B", "KiB", "MiB"];
2626

2727
let mut value = bytes as f64;
2828
let mut unit_index = 0;
@@ -40,15 +40,15 @@ pub fn format_bytes(bytes: u32) -> String {
4040
return format!("{bytes} {unit}");
4141
}
4242

43-
// For kB and MB, format with appropriate decimal places
43+
// For KiB and MiB, format with appropriate decimal places
4444

4545
// Determine number of decimal places to keep number under 4 chars
4646
if value < 10.0 {
47-
format!("{value:.2} {unit}") // e.g., 1.50 kB, 9.99 MB
47+
format!("{value:.2} {unit}") // e.g., 1.50 KiB, 9.99 MiB
4848
} else if value < 100.0 {
49-
format!("{value:.1} {unit}") // e.g., 10.5 kB, 99.9 MB
49+
format!("{value:.1} {unit}") // e.g., 10.5 KiB, 99.9 MiB
5050
} else {
51-
format!("{value:.0} {unit}") // e.g., 100 kB, 999 MB
51+
format!("{value:.0} {unit}") // e.g., 100 KiB, 999 MiB
5252
}
5353
}
5454

@@ -73,7 +73,7 @@ pub fn serialize_bytes<S: Serializer>(bytes: &u32, serializer: S) -> Result<S::O
7373
/// A formatted string representing the number with appropriate suffixes
7474
pub fn format_number(number: u32) -> String {
7575
const THRESHOLD: f64 = 1500.;
76-
const UNITS: &[&str] = &["", "k", "M"];
76+
const UNITS: &[&str] = &["", "K", "M"];
7777

7878
let mut value = number as f64;
7979
let mut unit_index = 0;
@@ -128,23 +128,23 @@ mod tests {
128128
assert_eq!(format_bytes(1499), "1499 B");
129129

130130
// Test kilobytes format (1500 bytes to 1500 * 1024 bytes)
131-
assert_eq!(format_bytes(1500), "1.46 kB");
132-
assert_eq!(format_bytes(2048), "2.00 kB");
133-
assert_eq!(format_bytes(5120), "5.00 kB");
134-
assert_eq!(format_bytes(10240), "10.0 kB");
135-
assert_eq!(format_bytes(51200), "50.0 kB");
136-
assert_eq!(format_bytes(102400), "100 kB");
137-
assert_eq!(format_bytes(512000), "500 kB");
138-
assert_eq!(format_bytes(1048575), "1024 kB");
131+
assert_eq!(format_bytes(1500), "1.46 KiB");
132+
assert_eq!(format_bytes(2048), "2.00 KiB");
133+
assert_eq!(format_bytes(5120), "5.00 KiB");
134+
assert_eq!(format_bytes(10240), "10.0 KiB");
135+
assert_eq!(format_bytes(51200), "50.0 KiB");
136+
assert_eq!(format_bytes(102400), "100 KiB");
137+
assert_eq!(format_bytes(512000), "500 KiB");
138+
assert_eq!(format_bytes(1048575), "1024 KiB");
139139

140140
// Test megabytes format (above 1500 * 1024 bytes)
141-
assert_eq!(format_bytes(1536000), "1.46 MB");
142-
assert_eq!(format_bytes(2097152), "2.00 MB");
143-
assert_eq!(format_bytes(5242880), "5.00 MB");
144-
assert_eq!(format_bytes(10485760), "10.0 MB");
145-
assert_eq!(format_bytes(52428800), "50.0 MB");
146-
assert_eq!(format_bytes(104857600), "100 MB");
147-
assert_eq!(format_bytes(1073741824), "1024 MB");
141+
assert_eq!(format_bytes(1536000), "1.46 MiB");
142+
assert_eq!(format_bytes(2097152), "2.00 MiB");
143+
assert_eq!(format_bytes(5242880), "5.00 MiB");
144+
assert_eq!(format_bytes(10485760), "10.0 MiB");
145+
assert_eq!(format_bytes(52428800), "50.0 MiB");
146+
assert_eq!(format_bytes(104857600), "100 MiB");
147+
assert_eq!(format_bytes(1073741824), "1024 MiB");
148148
}
149149

150150
#[test]
@@ -156,14 +156,14 @@ mod tests {
156156
assert_eq!(format_number(1499), "1499");
157157

158158
// Test numbers with k suffix (1500 to 1500 * 1000)
159-
assert_eq!(format_number(1500), "1.5k");
160-
assert_eq!(format_number(2000), "2.0k");
161-
assert_eq!(format_number(5000), "5.0k");
162-
assert_eq!(format_number(10000), "10k");
163-
assert_eq!(format_number(50000), "50k");
164-
assert_eq!(format_number(100000), "100k");
165-
assert_eq!(format_number(500000), "500k");
166-
assert_eq!(format_number(999999), "1000k");
159+
assert_eq!(format_number(1500), "1.5K");
160+
assert_eq!(format_number(2000), "2.0K");
161+
assert_eq!(format_number(5000), "5.0K");
162+
assert_eq!(format_number(10000), "10K");
163+
assert_eq!(format_number(50000), "50K");
164+
assert_eq!(format_number(100000), "100K");
165+
assert_eq!(format_number(500000), "500K");
166+
assert_eq!(format_number(999999), "1000K");
167167

168168
// Test numbers with M suffix (above 1500 * 1000)
169169
assert_eq!(format_number(1500000), "1.5M");
Loading
Loading
Loading

0 commit comments

Comments
 (0)