about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-07-01 16:16:12 +0000
committerbors <bors@rust-lang.org>2021-07-01 16:16:12 +0000
commitecef52abeba37f24157005ce0e3dfb06a79bfd21 (patch)
treeb71735b4507ce63fd856ba010c9bcb75b5e16312
parent64de4979e8979836e7c029d69eb96e6f7da66185 (diff)
parent2b57fc40d8ee59f6e2a6340a5700270c422525da (diff)
downloadrust-ecef52abeba37f24157005ce0e3dfb06a79bfd21.tar.gz
rust-ecef52abeba37f24157005ce0e3dfb06a79bfd21.zip
Auto merge of #86304 - klensy:hex-length, r=jackh726
rustc_mir: calc hex number length without string allocation
-rw-r--r--compiler/rustc_mir/src/util/pretty.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/compiler/rustc_mir/src/util/pretty.rs b/compiler/rustc_mir/src/util/pretty.rs
index cb1203393b3..17401c24ddd 100644
--- a/compiler/rustc_mir/src/util/pretty.rs
+++ b/compiler/rustc_mir/src/util/pretty.rs
@@ -819,7 +819,7 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
 ) -> std::fmt::Result {
     let num_lines = alloc.size().bytes_usize().saturating_sub(BYTES_PER_LINE);
     // Number of chars needed to represent all line numbers.
-    let pos_width = format!("{:x}", alloc.size().bytes()).len();
+    let pos_width = hex_number_length(alloc.size().bytes());
 
     if num_lines > 0 {
         write!(w, "{}0x{:02$x} │ ", prefix, 0, pos_width)?;
@@ -1018,3 +1018,23 @@ pub fn dump_mir_def_ids(tcx: TyCtxt<'_>, single: Option<DefId>) -> Vec<DefId> {
         tcx.mir_keys(()).iter().map(|def_id| def_id.to_def_id()).collect()
     }
 }
+
+/// Calc converted u64 decimal into hex and return it's length in chars
+///
+/// ```ignore (cannot-test-private-function)
+/// assert_eq!(1, hex_number_length(0));
+/// assert_eq!(1, hex_number_length(1));
+/// assert_eq!(2, hex_number_length(16));
+/// ```
+fn hex_number_length(x: u64) -> usize {
+    if x == 0 {
+        return 1;
+    }
+    let mut length = 0;
+    let mut x_left = x;
+    while x_left > 0 {
+        x_left /= 16;
+        length += 1;
+    }
+    length
+}