mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Auto merge of #84017 - Smittyvb:int-literal-underscores, r=jyn514
Valid underscores in hex/octal/binary literal docs Currently hex/octal/binary literals with computed values are displayed like `0_xff_fff_fffu32`, which is invalid since underscores can't be in the middle of integer prefixes. This properly formats prefixed integers. This causes [`std::u32::MAX`](https://doc.rust-lang.org/std/u32/constant.MAX.html) to be displayed as ```rust pub const MAX: u32 = u32::MAX; // 0_xff_fff_fffu32 ``` This PR changes it to be displayed as: ```rust pub const MAX: u32 = u32::MAX; // 0xffff_ffffu32 ```
This commit is contained in:
commit
a5f164faad
@ -16,6 +16,9 @@ use rustc_middle::ty::{self, DefIdTree, TyCtxt};
|
||||
use rustc_span::symbol::{kw, sym, Symbol};
|
||||
use std::mem;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
crate fn krate(cx: &mut DocContext<'_>) -> Crate {
|
||||
use crate::visit_lib::LibEmbargoVisitor;
|
||||
|
||||
@ -335,11 +338,27 @@ crate fn print_evaluated_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String>
|
||||
|
||||
fn format_integer_with_underscore_sep(num: &str) -> String {
|
||||
let num_chars: Vec<_> = num.chars().collect();
|
||||
let num_start_index = if num_chars.get(0) == Some(&'-') { 1 } else { 0 };
|
||||
let mut num_start_index = if num_chars.get(0) == Some(&'-') { 1 } else { 0 };
|
||||
let chunk_size = match num[num_start_index..].as_bytes() {
|
||||
[b'0', b'b' | b'x', ..] => {
|
||||
num_start_index += 2;
|
||||
4
|
||||
}
|
||||
[b'0', b'o', ..] => {
|
||||
num_start_index += 2;
|
||||
let remaining_chars = num_chars.len() - num_start_index;
|
||||
if remaining_chars <= 6 {
|
||||
// don't add underscores to Unix permissions like 0755 or 100755
|
||||
return num.to_string();
|
||||
}
|
||||
3
|
||||
}
|
||||
_ => 3,
|
||||
};
|
||||
|
||||
num_chars[..num_start_index]
|
||||
.iter()
|
||||
.chain(num_chars[num_start_index..].rchunks(3).rev().intersperse(&['_']).flatten())
|
||||
.chain(num_chars[num_start_index..].rchunks(chunk_size).rev().intersperse(&['_']).flatten())
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
41
src/librustdoc/clean/utils/tests.rs
Normal file
41
src/librustdoc/clean/utils/tests.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn int_format_decimal() {
|
||||
assert_eq!(format_integer_with_underscore_sep("12345678"), "12_345_678");
|
||||
assert_eq!(format_integer_with_underscore_sep("123"), "123");
|
||||
assert_eq!(format_integer_with_underscore_sep("123459"), "123_459");
|
||||
assert_eq!(format_integer_with_underscore_sep("-12345678"), "-12_345_678");
|
||||
assert_eq!(format_integer_with_underscore_sep("-123"), "-123");
|
||||
assert_eq!(format_integer_with_underscore_sep("-123459"), "-123_459");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn int_format_hex() {
|
||||
assert_eq!(format_integer_with_underscore_sep("0xab3"), "0xab3");
|
||||
assert_eq!(format_integer_with_underscore_sep("0xa2345b"), "0xa2_345b");
|
||||
assert_eq!(format_integer_with_underscore_sep("0xa2e6345b"), "0xa2e6_345b");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0xab3"), "-0xab3");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0xa2345b"), "-0xa2_345b");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0xa2e6345b"), "-0xa2e6_345b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn int_format_binary() {
|
||||
assert_eq!(format_integer_with_underscore_sep("0o12345671"), "0o12_345_671");
|
||||
assert_eq!(format_integer_with_underscore_sep("0o123"), "0o123");
|
||||
assert_eq!(format_integer_with_underscore_sep("0o123451"), "0o123451");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0o12345671"), "-0o12_345_671");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0o123"), "-0o123");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0o123451"), "-0o123451");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn int_format_octal() {
|
||||
assert_eq!(format_integer_with_underscore_sep("0b101"), "0b101");
|
||||
assert_eq!(format_integer_with_underscore_sep("0b101101011"), "0b1_0110_1011");
|
||||
assert_eq!(format_integer_with_underscore_sep("0b01101011"), "0b0110_1011");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0b101"), "-0b101");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0b101101011"), "-0b1_0110_1011");
|
||||
assert_eq!(format_integer_with_underscore_sep("-0b01101011"), "-0b0110_1011");
|
||||
}
|
Loading…
Reference in New Issue
Block a user