Skip to content

Commit

Permalink
fix byte compression
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Dec 30, 2024
1 parent 9e5a36c commit a72d469
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
25 changes: 10 additions & 15 deletions src/bytecompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ impl ByteCompressor {
self.map_cache[&e]
}

fn add_single_byte(&mut self, b: u8) {
if self.mapping[b as usize] == INVALID_MAPPING {
self.mapping[b as usize] = self.alphabet_size as u8;
self.alphabet_size += 1;
}
}

pub fn compress(&mut self, exprset: &ExprSet, rx_list: &[ExprRef]) -> (ExprSet, Vec<ExprRef>) {
self.mapping = vec![INVALID_MAPPING; exprset.alphabet_size()];

Expand All @@ -93,23 +100,11 @@ impl ByteCompressor {
visited[e.as_usize()] = true;
todo.extend_from_slice(exprset.get_args(e));
match exprset.get(e) {
Expr::Byte(b) => {
assert!(
self.mapping[b as usize] == INVALID_MAPPING,
"visiting the same byte the second time"
);
self.mapping[b as usize] = self.alphabet_size as u8;
self.alphabet_size += 1;
}
Expr::ByteSet(bs) => {
self.bytesets.push(bs.to_vec());
}
Expr::Byte(b) => self.add_single_byte(b),
Expr::ByteSet(bs) => self.bytesets.push(bs.to_vec()),
Expr::RemainderIs(_, _) => {
for b in exprset.digits {
if self.mapping[b as usize] == INVALID_MAPPING {
self.mapping[b as usize] = self.alphabet_size as u8;
self.alphabet_size += 1;
}
self.add_single_byte(b);
}
}
_ => {}
Expand Down
7 changes: 2 additions & 5 deletions tests/emptiness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,13 @@ fn remainder_is_check(should_be_empty: bool, d: u32, other_rx: &str) {
let mut bld = RegexBuilder::new();
let id = bld
.mk(&RegexAst::And(vec![
RegexAst::MultipleOf(d),
RegexAst::Regex(other_rx.to_string()),
RegexAst::MultipleOf(d),
]))
.unwrap();
let mut rx = bld.to_regex(id);
if rx.always_empty() != should_be_empty {
panic!(
"empty({} % & {:?}) != {}",
d, other_rx, should_be_empty
);
panic!("empty({} % & {:?}) != {}", d, other_rx, should_be_empty);
}
}

Expand Down

0 comments on commit a72d469

Please sign in to comment.