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 commit is contained in:
Smitty
2021-04-08 17:18:25 -04:00
parent 603a42ec54
commit f84b4c51ce
2 changed files with 62 additions and 2 deletions

View File

@@ -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()
}