ere_core/config.rs
1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum AmbiguousSubmatchMode {
3 /// While not POSIX standard, the greedy method for determining ambiguous submatches is
4 /// able to be implemented the most efficiently in the general case.
5 /// This is based on the popular Perl regex rules for ambiguous submatching.
6 ///
7 /// - Quantifiers match as much as possible while allowing the rest to match
8 /// - Alternations prioritize the earlier options
9 /// - Since it is greedy, the leftmost quantifier/alternations will match first.
10 Greedy,
11 // /// The mode based on `REG_MINIMAL=false` in C header [`<regex.h>`](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/regex.h.html).
12 // /// While the crate attempts to provide optimizations, there are no known ways to implement this as efficiently as the greedy mode.
13 // ERELongest,
14 // /// The mode based on `REG_MINIMAL=true` in C header [`<regex.h>`](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/regex.h.html)
15 // /// While the crate attempts to provide optimizations, there are no known ways to implement this as efficiently as the greedy mode.
16 // EREShortest,
17}
18
19#[derive(Debug, Clone)]
20pub struct Config {
21 // case_insensitive: bool,
22 /// When finding submatches during [`crate::Regex::exec`], determines the rules for what should happen if the
23 /// capture group submatches are ambiguous (e.g. `^(a*)(a*)$` on `aaaa` is ambiguous as to which capture group gets how many `a`'s).
24 ambiguous_submatches: AmbiguousSubmatchMode,
25}
26impl Config {
27 pub const fn quantifiers_prefer_longest(&self) -> bool {
28 return match self.ambiguous_submatches {
29 AmbiguousSubmatchMode::Greedy => true,
30 };
31 }
32 pub const fn builder() -> ConfigBuilder {
33 return ConfigBuilder::new();
34 }
35 pub const fn const_default() -> Self {
36 return Config {
37 ambiguous_submatches: AmbiguousSubmatchMode::Greedy,
38 };
39 }
40}
41impl Default for Config {
42 fn default() -> Self {
43 return Config::const_default();
44 }
45}
46
47#[derive(Debug)]
48pub struct ConfigBuilder(Config);
49impl ConfigBuilder {
50 pub const fn new() -> ConfigBuilder {
51 return ConfigBuilder(Config::const_default());
52 }
53 pub const fn build(self) -> Config {
54 return self.0;
55 }
56 pub const fn greedy(mut self) -> ConfigBuilder {
57 self.0.ambiguous_submatches = AmbiguousSubmatchMode::Greedy;
58 return self;
59 }
60}