MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / format_bytes

Function format_bytes

compiler/src/modules/vm/ops.rs:19–40  ·  view source on GitHub ↗

Render `bytes` as `b'...'` (printable ASCII verbatim, rest escaped). */

(buf: &[u8])

Source from the content-addressed store, hash-verified

17
18/* Render `bytes` as `b'...'` (printable ASCII verbatim, rest escaped). */
19fn 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. */
43fn repr_str(s: &str) -> String {

Callers 1

display_dMethod · 0.85

Calls 2

pushMethod · 0.80
lenMethod · 0.45

Tested by

no test coverage detected