Skip to content

Commit

Permalink
Fix the output format. Probably.
Browse files Browse the repository at this point in the history
  • Loading branch information
tifv committed Jul 6, 2024
1 parent bd3c115 commit 9815c5e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ impl std::ops::Deref for Ascii {
fn deref(&self) -> &u8 { &self.0 }
}

impl AsRef<u8> for Ascii {
fn as_ref(&self) -> &u8 { self }
}

impl TryFrom<u8> for Ascii {
type Error = AsciiError;
#[inline]
Expand Down
17 changes: 12 additions & 5 deletions src/dumper/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ pub(crate) fn compress<'b>(
let mut writer = AsciiWriter::with_capacity(36);
writer.write_slice(prefix);
let mut zipped = None;
let (len, body) = if body.len() <= 32 { (0, body) } else {
let len = body.len();
let zipped = zipped.insert(zip(body));
(len, &**zipped)
let (len, body) = {
let zipped: &_ = zipped.insert(zip(body));
if body.len() <= zipped.len() {
(0, body)
} else {
(body.len(), zipped.as_ref())
}
};
write_len_base31(&mut writer, len);
let checksum = write_encoded(&mut writer, body);
Expand Down Expand Up @@ -136,7 +139,11 @@ impl<'w> Base62Encoder<'w> {
fn consume_final_word(&mut self) {
let word = self.take_word();
self.checksum += word;
let (start, encoded) = Self::encode_word(word);
let (mut start, encoded) = Self::encode_word(word);
if !encoded[start].is_ascii_digit() {
assert!(start > 0);
start -= 1;
}
self.writer.write_slice(&encoded[start..]);
}
#[inline]
Expand Down
2 changes: 0 additions & 2 deletions src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ impl From<Instruction> for Value {
table.assoc_insert("op", Some(Value::String(this.operation)));
if let Some(next_value) = this.next.into() {
table.assoc_insert("next", Some(next_value));
} else {
table.assoc_insert_dead("next");
}
for (key, ReprValue(value)) in this.extra {
table.assoc_insert(key, Some(value));
Expand Down
4 changes: 4 additions & 0 deletions src/loader/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ impl<'r> Base62Decoder<'r> {
}
fn emit_final_word(&mut self) -> Result<u32, Error> {
let slice = self.reader.read_rest();
if !slice[0].is_ascii_digit() {
return Err(Error::from(
"the final word encoding should start with a digit" ))
}
let word = Self::decode_word(slice)?;
self.checksum += word;
Ok(word)
Expand Down

0 comments on commit 9815c5e

Please sign in to comment.