aboutsummaryrefslogtreecommitdiffstats
path: root/src/regex/simple.rs
blob: e75ff1439264ae57d522c47588253f2a42965758 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::regex::Class;

use super::{Match, Pattern, RegexEngine};

fn empty_match() -> Option<Match> {
    Some(Match {
        submatches: [].into(),
    })
}

pub struct Anything;

#[derive(Debug, Clone)]
pub struct NotASimpleWildcard;

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)
        }
    }

    fn run(&self, _input: &[u8]) -> Option<Match> {
        empty_match()
    }
}

pub struct Nothing;
#[derive(Debug, Clone)]
pub struct NotASimpleNothing;

impl RegexEngine for Nothing {
    type CompileError = NotASimpleNothing;

    fn compile(pat: Pattern) -> Result<Self, Self::CompileError> {
        match pat {
            Pattern::Range(a, b) if a > b => Ok(Nothing),
            Pattern::CharacterClass(Class::Nothing) => Ok(Nothing),
            Pattern::Alt(pats) => {
                let all_impossible = pats.into_iter().map(Self::compile).all(|p| p.is_ok());
                if all_impossible {
                    Ok(Nothing)
                } else {
                    Err(NotASimpleNothing)
                }
            }
            Pattern::Concat(pats) => {
                if let Some(pat) = pats.into_iter().next() {
                    Self::compile(pat)
                } else {
                    Err(NotASimpleNothing)
                }
            }
            Pattern::Rep(_, x, Some(y), _) if y < x => Ok(Nothing),
            Pattern::Rep(_, 0, None, _) => Err(NotASimpleNothing),
            Pattern::Rep(pat, _gt_0, _, _) => Self::compile(*pat),
            Pattern::Submatch(pat) => Self::compile(*pat),
            _ => Err(NotASimpleNothing),
        }
    }

    fn run(&self, _input: &[u8]) -> Option<Match> {
        None
    }
}

pub struct Exact {
    bytes: Vec<u8>,
}

const MEM_LIMIT: usize = 25_000;

#[derive(Debug, Clone)]
pub struct NotSimplyAString;

fn ce(pat: Pattern) -> Option<Vec<u8>> {
    match pat {
        Pattern::Byte(b) => Some(vec![b]),
        Pattern::Concat(patterns) => {
            let mut pats = patterns.into_iter().map(ce).collect::<Option<Vec<_>>>()?;
            let mut out = Vec::new();
            for p in pats.iter_mut() {
                out.append(p);
            }
            Some(out)
        }
        Pattern::Rep(pat, min, Some(max), _) if min == max => {
            if let Some(bytes) = ce(*pat)
                && bytes.len() * (min as usize) < MEM_LIMIT
            {
                Some(bytes.repeat(min as usize))
            } else {
                None
            }
        }
        Pattern::Submatch(_) => None, // TODO: submatches could be stored as constant offsets
        Pattern::Nothing => Some(Vec::new()),
        _ => None,
    }
}

impl RegexEngine for Exact {
    type CompileError = NotSimplyAString;

    fn compile(pat: Pattern) -> Result<Self, Self::CompileError> {
        match ce(pat) {
            Some(bytes) => Ok(Self { bytes }),
            None => Err(NotSimplyAString),
        }
    }

    fn run(&self, input: &[u8]) -> Option<Match> {
        if input == self.bytes {
            empty_match()
        } else {
            None
        }
    }
}