Skip to content

Commit bdfe1b9

Browse files
committed
Overhaul the thousands module.
It currently only inserts separators into `usize`s, because that's all that has been needed so far. `-Zmacro-stats` will need `isize` and `f64` handling, so this commit adds that.
1 parent fb73893 commit bdfe1b9

File tree

4 files changed

+86
-27
lines changed

4 files changed

+86
-27
lines changed
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
//! This is an extremely bare-bones alternative to the `thousands` crate on
2-
//! crates.io, for printing large numbers in a readable fashion.
1+
//! This is a bare-bones alternative to the `thousands` crate on crates.io, for
2+
//! printing large numbers in a readable fashion.
33
44
#[cfg(test)]
55
mod tests;
66

7-
// Converts the number to a string, with underscores as the thousands separator.
8-
pub fn format_with_underscores(n: usize) -> String {
9-
let mut s = n.to_string();
10-
let mut i = s.len();
11-
while i > 3 {
7+
fn format_with_underscores(mut s: String) -> String {
8+
// Ignore a leading '-'.
9+
let start = if s.starts_with('-') { 1 } else { 0 };
10+
11+
// Stop after the first non-digit, e.g. '.' or 'e' for floats.
12+
let non_digit = s[start..].find(|c: char| !c.is_digit(10));
13+
let end = if let Some(non_digit) = non_digit { start + non_digit } else { s.len() };
14+
15+
// Insert underscores within `start..end`.
16+
let mut i = end;
17+
while i > start + 3 {
1218
i -= 3;
1319
s.insert(i, '_');
1420
}
1521
s
1622
}
23+
24+
/// Print a `usize` with underscore separators.
25+
pub fn usize_with_underscores(n: usize) -> String {
26+
format_with_underscores(format!("{n}"))
27+
}
28+
29+
/// Print an `isize` with underscore separators.
30+
pub fn isize_with_underscores(n: isize) -> String {
31+
format_with_underscores(format!("{n}"))
32+
}
33+
34+
/// Print an `f64` with precision 1 (one decimal place) and underscore separators.
35+
pub fn f64p1_with_underscores(n: f64) -> String {
36+
format_with_underscores(format!("{n:.1}"))
37+
}

compiler/rustc_data_structures/src/thousands/tests.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,51 @@ use super::*;
22

33
#[test]
44
fn test_format_with_underscores() {
5-
assert_eq!("0", format_with_underscores(0));
6-
assert_eq!("1", format_with_underscores(1));
7-
assert_eq!("99", format_with_underscores(99));
8-
assert_eq!("345", format_with_underscores(345));
9-
assert_eq!("1_000", format_with_underscores(1_000));
10-
assert_eq!("12_001", format_with_underscores(12_001));
11-
assert_eq!("999_999", format_with_underscores(999_999));
12-
assert_eq!("1_000_000", format_with_underscores(1_000_000));
13-
assert_eq!("12_345_678", format_with_underscores(12_345_678));
5+
assert_eq!("", format_with_underscores("".to_string()));
6+
assert_eq!("0", format_with_underscores("0".to_string()));
7+
assert_eq!("12_345.67e14", format_with_underscores("12345.67e14".to_string()));
8+
assert_eq!("-1_234.5678e10", format_with_underscores("-1234.5678e10".to_string()));
9+
assert_eq!("------", format_with_underscores("------".to_string()));
10+
assert_eq!("abcdefgh", format_with_underscores("abcdefgh".to_string()));
11+
assert_eq!("-1b", format_with_underscores("-1b".to_string()));
12+
assert_eq!("-3_456xyz", format_with_underscores("-3456xyz".to_string()));
13+
}
14+
15+
#[test]
16+
fn test_usize_with_underscores() {
17+
assert_eq!("0", usize_with_underscores(0));
18+
assert_eq!("1", usize_with_underscores(1));
19+
assert_eq!("99", usize_with_underscores(99));
20+
assert_eq!("345", usize_with_underscores(345));
21+
assert_eq!("1_000", usize_with_underscores(1_000));
22+
assert_eq!("12_001", usize_with_underscores(12_001));
23+
assert_eq!("999_999", usize_with_underscores(999_999));
24+
assert_eq!("1_000_000", usize_with_underscores(1_000_000));
25+
assert_eq!("12_345_678", usize_with_underscores(12_345_678));
26+
}
27+
28+
#[test]
29+
fn test_isize_with_underscores() {
30+
assert_eq!("0", isize_with_underscores(0));
31+
assert_eq!("-1", isize_with_underscores(-1));
32+
assert_eq!("99", isize_with_underscores(99));
33+
assert_eq!("345", isize_with_underscores(345));
34+
assert_eq!("-1_000", isize_with_underscores(-1_000));
35+
assert_eq!("12_001", isize_with_underscores(12_001));
36+
assert_eq!("-999_999", isize_with_underscores(-999_999));
37+
assert_eq!("1_000_000", isize_with_underscores(1_000_000));
38+
assert_eq!("-12_345_678", isize_with_underscores(-12_345_678));
39+
}
40+
41+
#[test]
42+
fn test_f64p1_with_underscores() {
43+
assert_eq!("0.0", f64p1_with_underscores(0f64));
44+
assert_eq!("0.0", f64p1_with_underscores(0.00000001));
45+
assert_eq!("-0.0", f64p1_with_underscores(-0.00000001));
46+
assert_eq!("1.0", f64p1_with_underscores(0.9999999));
47+
assert_eq!("-1.0", f64p1_with_underscores(-0.9999999));
48+
assert_eq!("345.5", f64p1_with_underscores(345.4999999));
49+
assert_eq!("-100_000.0", f64p1_with_underscores(-100_000f64));
50+
assert_eq!("123_456_789.1", f64p1_with_underscores(123456789.123456789));
51+
assert_eq!("-123_456_789.1", f64p1_with_underscores(-123456789.123456789));
1452
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
1010
use rustc_data_structures::memmap::{Mmap, MmapMut};
1111
use rustc_data_structures::sync::{join, par_for_each_in};
1212
use rustc_data_structures::temp_dir::MaybeTempDir;
13-
use rustc_data_structures::thousands::format_with_underscores;
13+
use rustc_data_structures::thousands::usize_with_underscores;
1414
use rustc_feature::Features;
1515
use rustc_hir as hir;
1616
use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE, LocalDefId, LocalDefIdSet};
@@ -789,7 +789,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
789789
"{} {:<23}{:>10} ({:4.1}%)",
790790
prefix,
791791
label,
792-
format_with_underscores(size),
792+
usize_with_underscores(size),
793793
perc(size)
794794
);
795795
}
@@ -798,7 +798,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
798798
"{} {:<23}{:>10} (of which {:.1}% are zero bytes)",
799799
prefix,
800800
"Total",
801-
format_with_underscores(total_bytes),
801+
usize_with_underscores(total_bytes),
802802
perc(zero_bytes)
803803
);
804804
eprintln!("{prefix}");

compiler/rustc_passes/src/input_stats.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use rustc_ast::visit::BoundKind;
66
use rustc_ast::{self as ast, NodeId, visit as ast_visit};
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8-
use rustc_data_structures::thousands::format_with_underscores;
8+
use rustc_data_structures::thousands::usize_with_underscores;
99
use rustc_hir::{self as hir, AmbigArg, HirId, intravisit as hir_visit};
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_span::Span;
@@ -140,10 +140,10 @@ impl<'k> StatCollector<'k> {
140140
"{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
141141
prefix,
142142
label,
143-
format_with_underscores(size),
143+
usize_with_underscores(size),
144144
percent(size, total_size),
145-
format_with_underscores(node.stats.count),
146-
format_with_underscores(node.stats.size)
145+
usize_with_underscores(node.stats.count),
146+
usize_with_underscores(node.stats.size)
147147
);
148148
if !node.subnodes.is_empty() {
149149
// We will soon sort, so the initial order does not matter.
@@ -159,9 +159,9 @@ impl<'k> StatCollector<'k> {
159159
"{} - {:<18}{:>10} ({:4.1}%){:>14}",
160160
prefix,
161161
label,
162-
format_with_underscores(size),
162+
usize_with_underscores(size),
163163
percent(size, total_size),
164-
format_with_underscores(subnode.count),
164+
usize_with_underscores(subnode.count),
165165
);
166166
}
167167
}
@@ -171,8 +171,8 @@ impl<'k> StatCollector<'k> {
171171
"{} {:<18}{:>10} {:>14}",
172172
prefix,
173173
"Total",
174-
format_with_underscores(total_size),
175-
format_with_underscores(total_count),
174+
usize_with_underscores(total_size),
175+
usize_with_underscores(total_count),
176176
);
177177
eprintln!("{prefix}");
178178
}

0 commit comments

Comments
 (0)