Render `bytes` as `b'...'` (printable ASCII verbatim, rest escaped). */
(buf: &[u8])
| 17 | |
| 18 | /* Render `bytes` as `b'...'` (printable ASCII verbatim, rest escaped). */ |
| 19 | fn format_bytes(buf: &[u8]) -> String { |
| 20 | let mut out = String::with_capacity(buf.len() + 3); |
| 21 | out.push_str("b'"); |
| 22 | for &b in buf { |
| 23 | match b { |
| 24 | b'\\' => out.push_str("\\\\"), |
| 25 | b'\'' => out.push_str("\\'"), |
| 26 | b'\n' => out.push_str("\\n"), |
| 27 | b'\r' => out.push_str("\\r"), |
| 28 | b'\t' => out.push_str("\\t"), |
| 29 | 0x20..=0x7E => out.push(b as char), |
| 30 | _ => { |
| 31 | out.push_str("\\x"); |
| 32 | const HEX: &[u8; 16] = b"0123456789abcdef"; |
| 33 | out.push(HEX[(b >> 4) as usize] as char); |
| 34 | out.push(HEX[(b & 0x0F) as usize] as char); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | out.push('\''); |
| 39 | out |
| 40 | } |
| 41 | |
| 42 | /* `repr` of a str: quote selection (' unless the text has ' but not ") and backslash escapes for control chars; printable text (incl. non-ASCII) is verbatim. */ |
| 43 | fn repr_str(s: &str) -> String { |