Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good follow-up would be to remove num_chars altogether - num is always ascii so you should be able to just use bytes directly. Doesn't need to be in this PR though :)

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

Expand Down
41 changes: 41 additions & 0 deletions src/librustdoc/clean/utils/tests.rs
Original file line number Diff line number Diff line change
@@ -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");
}