Skip to content

Commit 5b47d34

Browse files
committed
Simplify format_integer_with_underscore_sep
Only ever needs to handle decimal reprs
1 parent 52bf0cf commit 5b47d34

File tree

3 files changed

+23
-63
lines changed

3 files changed

+23
-63
lines changed

src/librustdoc/clean/utils.rs

Lines changed: 15 additions & 27 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};
@@ -391,30 +391,12 @@ pub(crate) fn print_evaluated_const(
391391
})
392392
}
393393

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()
394+
fn format_integer_with_underscore_sep(num: u128, is_negative: bool) -> String {
395+
let num = num.to_string();
396+
let chars = num.as_ascii().unwrap();
397+
let mut result = if is_negative { "-".to_string() } else { String::new() };
398+
result.extend(chars.rchunks(3).rev().intersperse(&[ascii::Char::LowLine]).flatten());
399+
result
418400
}
419401

420402
fn print_const_with_custom_print_scalar<'tcx>(
@@ -428,7 +410,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
428410
match (ct, ct.ty().kind()) {
429411
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
430412
let mut output = if with_underscores {
431-
format_integer_with_underscore_sep(&int.to_string())
413+
format_integer_with_underscore_sep(
414+
int.assert_scalar_int().to_bits_unchecked(),
415+
false,
416+
)
432417
} else {
433418
int.to_string()
434419
};
@@ -445,7 +430,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
445430
.size;
446431
let sign_extended_data = int.assert_scalar_int().to_int(size);
447432
let mut output = if with_underscores {
448-
format_integer_with_underscore_sep(&sign_extended_data.to_string())
433+
format_integer_with_underscore_sep(
434+
sign_extended_data.unsigned_abs(),
435+
sign_extended_data.is_negative(),
436+
)
449437
} else {
450438
sign_extended_data.to_string()
451439
};

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)