Skip to content

Commit f1df0b7

Browse files
committed
use oct escape for byte 0..=7 for smaller size
1 parent 9ce4887 commit f1df0b7

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/bytecode.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ impl LispObject {
7676
13 => result += "\\r",
7777
127 => result += "\\d",
7878
27 => result += "\\e",
79-
0..=26 => { // \^@ \^A \^B ... \^Z
79+
// NOTE: do not use 0..=7 in this branch, because it takes one more byte than the next branch
80+
8..=26 => { // \^@ \^A \^B ... \^Z
8081
result += &format!("\\^{}", (*c as u32 + 64) as u8 as char);
8182
},
82-
27..=31 | 128..=255 | 34 | 92 => { // oct, for unprintable and '"' and '\\'
83+
0..=7 | 27..=31 | 128..=255 | 34 | 92 => { // oct, for unprintable and '"' and '\\'
8384
let oct_s = format!("\\{:o}", *c as u32);
8485
if oct_s.len() < 4 {
8586
oct_escape_not_full = true;
@@ -441,7 +442,7 @@ pub fn generate_bytecode_repl(value: &json::Value, options: BytecodeOptions) ->
441442

442443
#[test]
443444
fn test_string_repl() {
444-
assert_eq!(LispObject::UnibyteStr("\x00".into()).to_repl(), r#""\^@""#);
445+
assert_eq!(LispObject::UnibyteStr("\x00".into()).to_repl(), r#""\0""#);
445446
assert_eq!(LispObject::UnibyteStr("\x1a".into()).to_repl(), r#""\^Z""#);
446447
assert_eq!(LispObject::UnibyteStr("\x20".into()).to_repl(), r#"" ""#);
447448
assert_eq!(LispObject::UnibyteStr("\x7f".into()).to_repl(), r#""\d""#);

0 commit comments

Comments
 (0)