diff --git a/src/bytecompress.rs b/src/bytecompress.rs index 76e01f8..6372bf7 100644 --- a/src/bytecompress.rs +++ b/src/bytecompress.rs @@ -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) { self.mapping = vec![INVALID_MAPPING; exprset.alphabet_size()]; @@ -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); } } _ => {} diff --git a/tests/emptiness.rs b/tests/emptiness.rs index 6e32b97..6b2280f 100644 --- a/tests/emptiness.rs +++ b/tests/emptiness.rs @@ -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); } }