Skip to content

Commit

Permalink
use oct escape for byte 0..=7 for smaller size
Browse files Browse the repository at this point in the history
  • Loading branch information
blahgeek committed Jan 7, 2024
1 parent 9ce4887 commit f1df0b7
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ impl LispObject {
13 => result += "\\r",
127 => result += "\\d",
27 => result += "\\e",
0..=26 => { // \^@ \^A \^B ... \^Z
// NOTE: do not use 0..=7 in this branch, because it takes one more byte than the next branch
8..=26 => { // \^@ \^A \^B ... \^Z
result += &format!("\\^{}", (*c as u32 + 64) as u8 as char);
},
27..=31 | 128..=255 | 34 | 92 => { // oct, for unprintable and '"' and '\\'
0..=7 | 27..=31 | 128..=255 | 34 | 92 => { // oct, for unprintable and '"' and '\\'
let oct_s = format!("\\{:o}", *c as u32);
if oct_s.len() < 4 {
oct_escape_not_full = true;
Expand Down Expand Up @@ -441,7 +442,7 @@ pub fn generate_bytecode_repl(value: &json::Value, options: BytecodeOptions) ->

#[test]
fn test_string_repl() {
assert_eq!(LispObject::UnibyteStr("\x00".into()).to_repl(), r#""\^@""#);
assert_eq!(LispObject::UnibyteStr("\x00".into()).to_repl(), r#""\0""#);
assert_eq!(LispObject::UnibyteStr("\x1a".into()).to_repl(), r#""\^Z""#);
assert_eq!(LispObject::UnibyteStr("\x20".into()).to_repl(), r#"" ""#);
assert_eq!(LispObject::UnibyteStr("\x7f".into()).to_repl(), r#""\d""#);
Expand Down

0 comments on commit f1df0b7

Please sign in to comment.