aboutsummaryrefslogtreecommitdiffstats
path: root/src/regex
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-06-06 13:45:54 +0200
committerJonas Maier <jonas@x77.dev>2026-06-06 13:45:54 +0200
commitd39ed8fc77981f937c35fa84a7ff5d288d0c7181 (patch)
treec17ba6e489a66e732853e9720eba4c16fa86d441 /src/regex
parent1c0c0c35f3dff0abc8ac24ec90dedf554697cec5 (diff)
downloadpish-d39ed8fc77981f937c35fa84a7ff5d288d0c7181.tar.gz
clean up
Diffstat (limited to 'src/regex')
-rw-r--r--src/regex/byte_range.rs4
-rw-r--r--src/regex/dfa.rs9
-rw-r--r--src/regex/mod.rs3
-rw-r--r--src/regex/simple.rs24
4 files changed, 32 insertions, 8 deletions
diff --git a/src/regex/byte_range.rs b/src/regex/byte_range.rs
index 5123da5..d549a55 100644
--- a/src/regex/byte_range.rs
+++ b/src/regex/byte_range.rs
@@ -24,6 +24,10 @@ impl ByteRange {
Self::new_range(c, c)
}
+ pub fn all() -> Self {
+ Self::new_range(0, 255)
+ }
+
pub fn contains(&self, c: u8) -> bool {
self.from <= c && c <= self.to
}
diff --git a/src/regex/dfa.rs b/src/regex/dfa.rs
index c55d99d..78a216c 100644
--- a/src/regex/dfa.rs
+++ b/src/regex/dfa.rs
@@ -374,6 +374,15 @@ impl DFA {
pub fn minify(&mut self) {
for state in self.states.iter_mut() {
state.trans.retain(|_, to| *to != state.default_trans);
+ if state.trans.len() == 1
+ && state
+ .trans
+ .iter()
+ .all(|t| *t.0 == ByteRange::new_range(0, 255))
+ {
+ state.default_trans = state.trans.iter().map(|x| *x.1).next().unwrap();
+ state.trans.clear();
+ }
}
self.hopcroft_minimization();
diff --git a/src/regex/mod.rs b/src/regex/mod.rs
index 2c9f3d1..438361a 100644
--- a/src/regex/mod.rs
+++ b/src/regex/mod.rs
@@ -283,8 +283,9 @@ impl RegexEngine for CompiledPattern {
macro_rules! all_engines {
($ty_name:ident, $($x:ident : $ty:ty,)*) => {
+ #[derive(Debug)]
pub struct $ty_name {
- $($x: Option<$ty>,)*
+ $(pub $x: Option<$ty>,)*
}
impl RegexEngine for $ty_name {
type CompileError = ();
diff --git a/src/regex/simple.rs b/src/regex/simple.rs
index e75ff14..4286322 100644
--- a/src/regex/simple.rs
+++ b/src/regex/simple.rs
@@ -8,6 +8,7 @@ fn empty_match() -> Option<Match> {
})
}
+#[derive(Debug)]
pub struct Anything;
#[derive(Debug, Clone)]
@@ -17,12 +18,19 @@ impl RegexEngine for Anything {
type CompileError = NotASimpleWildcard;
fn compile(pat: Pattern) -> Result<Self, Self::CompileError> {
- if let Pattern::Rep(pat, 0, None, _) = pat
- && let Pattern::CharacterClass(Class::Everything) = *pat
- {
- Ok(Anything)
- } else {
- Err(NotASimpleWildcard)
+ match pat {
+ Pattern::Rep(pat, 0, None, _) => match *pat {
+ Pattern::CharacterClass(Class::Everything) => Ok(Anything),
+ _ => Err(NotASimpleWildcard),
+ },
+ Pattern::Concat(pats) | Pattern::Alt(pats) => {
+ if !pats.is_empty() && pats.into_iter().all(|p| Anything::compile(p).is_ok()) {
+ Ok(Anything)
+ } else {
+ Err(NotASimpleWildcard)
+ }
+ }
+ _ => Err(NotASimpleWildcard),
}
}
@@ -31,6 +39,7 @@ impl RegexEngine for Anything {
}
}
+#[derive(Debug)]
pub struct Nothing;
#[derive(Debug, Clone)]
pub struct NotASimpleNothing;
@@ -70,8 +79,9 @@ impl RegexEngine for Nothing {
}
}
+#[derive(Debug)]
pub struct Exact {
- bytes: Vec<u8>,
+ pub bytes: Vec<u8>,
}
const MEM_LIMIT: usize = 25_000;