Skip to content

Commit a4836e9

Browse files
authored
Rollup merge of #141369 - yotamofek:pr/rustdoc/format_integer_with_underscore_sep, r=notriddle
Simplify `format_integer_with_underscore_sep` Noticed that this helper fn only ever gets called with decimal-base-formatted ints, so can be simplified a lot by not trying to handle hex and octal radixes. Second commit is completely unrelated, just simplified some code I wrote a while back 😁
2 parents 672ad6e + 5c735d1 commit a4836e9

File tree

3 files changed

+25
-72
lines changed

3 files changed

+25
-72
lines changed

src/librustdoc/clean/utils.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::assert_matches::debug_assert_matches;
22
use std::fmt::{self, Display, Write as _};
3-
use std::mem;
43
use std::sync::LazyLock as Lazy;
4+
use std::{ascii, mem};
55

66
use rustc_ast::tokenstream::TokenTree;
77
use rustc_hir::def::{DefKind, Res};
@@ -24,7 +24,7 @@ use crate::clean::{
2424
clean_middle_ty, inline,
2525
};
2626
use crate::core::DocContext;
27-
use crate::display::Joined as _;
27+
use crate::display::{Joined as _, MaybeDisplay as _};
2828

2929
#[cfg(test)]
3030
mod tests;
@@ -254,14 +254,7 @@ pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String {
254254
fmt::from_fn(|f| {
255255
segments
256256
.iter()
257-
.map(|seg| {
258-
fmt::from_fn(|f| {
259-
if seg.ident.name != kw::PathRoot {
260-
write!(f, "{}", seg.ident)?;
261-
}
262-
Ok(())
263-
})
264-
})
257+
.map(|seg| (seg.ident.name != kw::PathRoot).then_some(seg.ident).maybe_display())
265258
.joined("::", f)
266259
})
267260
.to_string()
@@ -391,30 +384,12 @@ pub(crate) fn print_evaluated_const(
391384
})
392385
}
393386

394-
fn format_integer_with_underscore_sep(num: &str) -> String {
395-
let num_chars: Vec<_> = num.chars().collect();
396-
let mut num_start_index = if num_chars.first() == Some(&'-') { 1 } else { 0 };
397-
let chunk_size = match &num.as_bytes()[num_start_index..] {
398-
[b'0', b'b' | b'x', ..] => {
399-
num_start_index += 2;
400-
4
401-
}
402-
[b'0', b'o', ..] => {
403-
num_start_index += 2;
404-
let remaining_chars = num_chars.len() - num_start_index;
405-
if remaining_chars <= 6 {
406-
// don't add underscores to Unix permissions like 0755 or 100755
407-
return num.to_string();
408-
}
409-
3
410-
}
411-
_ => 3,
412-
};
413-
414-
num_chars[..num_start_index]
415-
.iter()
416-
.chain(num_chars[num_start_index..].rchunks(chunk_size).rev().intersperse(&['_']).flatten())
417-
.collect()
387+
fn format_integer_with_underscore_sep(num: u128, is_negative: bool) -> String {
388+
let num = num.to_string();
389+
let chars = num.as_ascii().unwrap();
390+
let mut result = if is_negative { "-".to_string() } else { String::new() };
391+
result.extend(chars.rchunks(3).rev().intersperse(&[ascii::Char::LowLine]).flatten());
392+
result
418393
}
419394

420395
fn print_const_with_custom_print_scalar<'tcx>(
@@ -428,7 +403,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
428403
match (ct, ct.ty().kind()) {
429404
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
430405
let mut output = if with_underscores {
431-
format_integer_with_underscore_sep(&int.to_string())
406+
format_integer_with_underscore_sep(
407+
int.assert_scalar_int().to_bits_unchecked(),
408+
false,
409+
)
432410
} else {
433411
int.to_string()
434412
};
@@ -445,7 +423,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
445423
.size;
446424
let sign_extended_data = int.assert_scalar_int().to_int(size);
447425
let mut output = if with_underscores {
448-
format_integer_with_underscore_sep(&sign_extended_data.to_string())
426+
format_integer_with_underscore_sep(
427+
sign_extended_data.unsigned_abs(),
428+
sign_extended_data.is_negative(),
429+
)
449430
} else {
450431
sign_extended_data.to_string()
451432
};

src/librustdoc/clean/utils/tests.rs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,10 @@ use super::*;
22

33
#[test]
44
fn int_format_decimal() {
5-
assert_eq!(format_integer_with_underscore_sep("12345678"), "12_345_678");
6-
assert_eq!(format_integer_with_underscore_sep("123"), "123");
7-
assert_eq!(format_integer_with_underscore_sep("123459"), "123_459");
8-
assert_eq!(format_integer_with_underscore_sep("-12345678"), "-12_345_678");
9-
assert_eq!(format_integer_with_underscore_sep("-123"), "-123");
10-
assert_eq!(format_integer_with_underscore_sep("-123459"), "-123_459");
11-
}
12-
13-
#[test]
14-
fn int_format_hex() {
15-
assert_eq!(format_integer_with_underscore_sep("0xab3"), "0xab3");
16-
assert_eq!(format_integer_with_underscore_sep("0xa2345b"), "0xa2_345b");
17-
assert_eq!(format_integer_with_underscore_sep("0xa2e6345b"), "0xa2e6_345b");
18-
assert_eq!(format_integer_with_underscore_sep("-0xab3"), "-0xab3");
19-
assert_eq!(format_integer_with_underscore_sep("-0xa2345b"), "-0xa2_345b");
20-
assert_eq!(format_integer_with_underscore_sep("-0xa2e6345b"), "-0xa2e6_345b");
21-
}
22-
23-
#[test]
24-
fn int_format_binary() {
25-
assert_eq!(format_integer_with_underscore_sep("0o12345671"), "0o12_345_671");
26-
assert_eq!(format_integer_with_underscore_sep("0o123"), "0o123");
27-
assert_eq!(format_integer_with_underscore_sep("0o123451"), "0o123451");
28-
assert_eq!(format_integer_with_underscore_sep("-0o12345671"), "-0o12_345_671");
29-
assert_eq!(format_integer_with_underscore_sep("-0o123"), "-0o123");
30-
assert_eq!(format_integer_with_underscore_sep("-0o123451"), "-0o123451");
31-
}
32-
33-
#[test]
34-
fn int_format_octal() {
35-
assert_eq!(format_integer_with_underscore_sep("0b101"), "0b101");
36-
assert_eq!(format_integer_with_underscore_sep("0b101101011"), "0b1_0110_1011");
37-
assert_eq!(format_integer_with_underscore_sep("0b01101011"), "0b0110_1011");
38-
assert_eq!(format_integer_with_underscore_sep("-0b101"), "-0b101");
39-
assert_eq!(format_integer_with_underscore_sep("-0b101101011"), "-0b1_0110_1011");
40-
assert_eq!(format_integer_with_underscore_sep("-0b01101011"), "-0b0110_1011");
5+
assert_eq!(format_integer_with_underscore_sep(12345678, false), "12_345_678");
6+
assert_eq!(format_integer_with_underscore_sep(123, false), "123");
7+
assert_eq!(format_integer_with_underscore_sep(123459, false), "123_459");
8+
assert_eq!(format_integer_with_underscore_sep(12345678, true), "-12_345_678");
9+
assert_eq!(format_integer_with_underscore_sep(123, true), "-123");
10+
assert_eq!(format_integer_with_underscore_sep(123459, true), "-123_459");
4111
}

src/librustdoc/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
html_playground_url = "https://play.rust-lang.org/"
44
)]
55
#![feature(rustc_private)]
6+
#![feature(ascii_char)]
7+
#![feature(ascii_char_variants)]
68
#![feature(assert_matches)]
79
#![feature(box_patterns)]
810
#![feature(debug_closure_helpers)]

0 commit comments

Comments
 (0)