Skip to content

Commit

Permalink
sligtly reduce bytecode size using smaller oct escape
Browse files Browse the repository at this point in the history
  • Loading branch information
blahgeek committed Jan 7, 2024
1 parent 702e2d5 commit 9ce4887
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ impl LispObject {
let mut result = String::new();
result.reserve(vec.len() * 4 + 2);
result.push('"');
let mut last_oct_escape_not_full = false;
for c in vec {
let mut oct_escape_not_full = false;
match *c {
7 => result += "\\a",
8 => result += "\\b",
Expand All @@ -78,10 +80,21 @@ impl LispObject {
result += &format!("\\^{}", (*c as u32 + 64) as u8 as char);
},
27..=31 | 128..=255 | 34 | 92 => { // oct, for unprintable and '"' and '\\'
result += &format!("\\{:03o}", *c as u32);
let oct_s = format!("\\{:o}", *c as u32);
if oct_s.len() < 4 {
oct_escape_not_full = true;
}
result += &oct_s;
},
_ => { // printable
// https://www.gnu.org/software/emacs/manual/html_node/elisp/Non_002dASCII-in-St
if last_oct_escape_not_full && ('0'..='7').contains(&(*c as char)) {
result += "\\ ";
}
result.push(*c as char);
},
_ => result.push(*c as char), // printable
}
last_oct_escape_not_full = oct_escape_not_full;
}
result.push('"');
result
Expand Down
2 changes: 1 addition & 1 deletion tests/bytecode_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn run_one_test(json_str: &str, object_type: bytecode::ObjectType) -> Result<()>
#[test]
fn test_bytecode() {
// unicode test
run_one_test(r#"{"a":"ÀÁÂÃÄÅÆÇÈÉÊËÌ abcd \n 你好世界"}"#, bytecode::ObjectType::Plist).unwrap();
run_one_test(r#"{"a":"ÀÁÂÃÄÅÆÇÈÉÊËÌ\u0000 abcd \n 你好世界"}"#, bytecode::ObjectType::Plist).unwrap();

for object_type in vec![bytecode::ObjectType::Plist,
bytecode::ObjectType::Alist,
Expand Down

0 comments on commit 9ce4887

Please sign in to comment.