about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2016-04-28 15:57:49 +0200
committerFelix S. Klock II <pnkfelix@pnkfx.org>2016-05-16 09:13:42 +0200
commitbfe789c04412fd8bfb33d1d66f7f8c233a349345 (patch)
tree0e7c669ea7b49b0c735268342db5c40cac1b2b42
parentcd81b60a4aa45f0eb72c5170673f24dc1a1c3592 (diff)
fixes to `librustc_borrowck::bitslice::bits_to_string`, used for graphviz printing.
-rw-r--r--src/librustc_borrowck/bitslice.rs24
1 files changed, 14 insertions, 10 deletions
diff --git a/src/librustc_borrowck/bitslice.rs b/src/librustc_borrowck/bitslice.rs
index 0454b0f3e7c..ca672e80884 100644
--- a/src/librustc_borrowck/bitslice.rs
+++ b/src/librustc_borrowck/bitslice.rs
@@ -78,25 +78,29 @@ fn bit_str(bit: usize) -> String {
     format!("[{}:{}-{:02x}]", bit, byte, lobits)
 }
 
-pub fn bits_to_string(words: &[usize], bytes: usize) -> String {
+pub fn bits_to_string(words: &[usize], bits: usize) -> String {
     let mut result = String::new();
     let mut sep = '[';
 
     // Note: this is a little endian printout of bytes.
 
+    // i tracks how many bits we have printed so far.
     let mut i = 0;
     for &word in words.iter() {
         let mut v = word;
-        for _ in 0..mem::size_of::<usize>() {
-            let byte = v & 0xFF;
-            if i >= bytes {
-                assert!(byte == 0);
-            } else {
-                result.push(sep);
-                result.push_str(&format!("{:02x}", byte));
-            }
+        loop { // for each byte in `v`:
+            let remain = bits - i;
+            // If less than a byte remains, then mask just that many bits.
+            let mask = if remain <= 8 { (1 << remain) - 1 } else { 0xFF };
+            assert!(mask <= 0xFF);
+            let byte = v & mask;
+
+            result.push(sep);
+            result.push_str(&format!("{:02x}", byte));
+
+            if remain <= 8 { break; }
             v >>= 8;
-            i += 1;
+            i += 8;
             sep = '-';
         }
     }