Skip to content

Commit bf7a1a8

Browse files
committed
Simplify format_integer_with_underscore_sep
Only ever needs to handle decimal reprs
1 parent 356f2d0 commit bf7a1a8

File tree

3 files changed

+23
-63
lines changed

3 files changed

+23
-63
lines changed

src/librustdoc/clean/utils.rs

Lines changed: 16 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::{iter, mem};
55

66
use rustc_ast::tokenstream::TokenTree;
77
use rustc_hir::def::{DefKind, Res};
@@ -391,30 +391,13 @@ 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+
iter::chain(
397+
is_negative.then_some('-'),
398+
num.as_bytes().rchunks(3).rev().intersperse(b"_").flatten().copied().map(char::from),
399+
)
400+
.collect()
418401
}
419402

420403
fn print_const_with_custom_print_scalar<'tcx>(
@@ -428,7 +411,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
428411
match (ct, ct.ty().kind()) {
429412
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
430413
let mut output = if with_underscores {
431-
format_integer_with_underscore_sep(&int.to_string())
414+
format_integer_with_underscore_sep(
415+
int.assert_scalar_int().to_bits_unchecked(),
416+
false,
417+
)
432418
} else {
433419
int.to_string()
434420
};
@@ -445,7 +431,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
445431
.size;
446432
let sign_extended_data = int.assert_scalar_int().to_int(size);
447433
let mut output = if with_underscores {
448-
format_integer_with_underscore_sep(&sign_extended_data.to_string())
434+
format_integer_with_underscore_sep(
435+
sign_extended_data.unsigned_abs(),
436+
sign_extended_data.is_negative(),
437+
)
449438
} else {
450439
sign_extended_data.to_string()
451440
};

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(format_args_nl)]
1111
#![feature(if_let_guard)]
1212
#![feature(impl_trait_in_assoc_type)]
13+
#![feature(iter_chain)]
1314
#![feature(iter_intersperse)]
1415
#![feature(never_type)]
1516
#![feature(round_char_boundary)]

0 commit comments

Comments
 (0)