`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. */
(s: &str)
| 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 { |
| 44 | use core::fmt::Write; |
| 45 | let quote = if s.contains('\'') && !s.contains('"') { '"' } else { '\'' }; |
| 46 | let mut out = String::with_capacity(s.len() + 2); |
| 47 | out.push(quote); |
| 48 | for c in s.chars() { |
| 49 | match c { |
| 50 | '\\' => out.push_str("\\\\"), |
| 51 | '\n' => out.push_str("\\n"), |
| 52 | '\r' => out.push_str("\\r"), |
| 53 | '\t' => out.push_str("\\t"), |
| 54 | c if c == quote => { out.push('\\'); out.push(c); } |
| 55 | c if c.is_control() => { |
| 56 | let n = c as u32; |
| 57 | if n <= 0xff { let _ = write!(out, "\\x{:02x}", n); } |
| 58 | else if n <= 0xffff { let _ = write!(out, "\\u{:04x}", n); } |
| 59 | else { let _ = write!(out, "\\U{:08x}", n); } |
| 60 | } |
| 61 | c => out.push(c), |
| 62 | } |
| 63 | } |
| 64 | out.push(quote); |
| 65 | out |
| 66 | } |
| 67 | |
| 68 | /* Coerce a numeric pair to f64; returns None if neither operand is a float. */ |
| 69 | fn coerce_floats(a: Val, b: Val, heap: &HeapPool) -> Option<(f64, f64)> { |