| 1 | //! Minimal case-insensitive regex subset for layer names: literals, `.`, `[...]` classes (ranges, `^` |
| 2 | //! negation), `\` escapes, `*` `+` `?` quantifiers, top-level `|`, and `^` / `$` anchors. |
| 3 | //! Unanchored patterns match anywhere in the name. |
| 4 | |
| 5 | enum Atom { |
| 6 | Any, |
| 7 | Lit(char), |
| 8 | Class { ranges: Vec<(char, char)>, negated: bool }, |
| 9 | } |
| 10 | |
| 11 | struct Token { |
| 12 | atom: Atom, |
| 13 | min: usize, |
| 14 | max: usize, |
| 15 | } |
| 16 | |
| 17 | struct Branch { |
| 18 | start_anchored: bool, |
| 19 | end_anchored: bool, |
| 20 | tokens: Vec<Token>, |
| 21 | } |
| 22 | |
| 23 | impl Atom { |
| 24 | fn matches(&self, c: char) -> bool { |
| 25 | match self { |
| 26 | Atom::Any => true, |
| 27 | Atom::Lit(l) => *l == c, |
| 28 | Atom::Class { ranges, negated } => ranges.iter().any(|&(lo, hi)| (lo..=hi).contains(&c)) != *negated, |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn parse_branch(pattern: &str) -> Branch { |
| 34 | let chars: Vec<char> = pattern.to_lowercase().chars().collect(); |
| 35 | let (mut i, mut tokens) = (0, Vec::new()); |
| 36 | let start_anchored = chars.first() == Some(&'^'); |
| 37 | i += usize::from(start_anchored); |
| 38 | let escaped_dollar = chars.len() >= 2 && chars[chars.len() - 2] == '\\'; |
| 39 | let end_anchored = chars.len() > i && chars.last() == Some(&'$') && !escaped_dollar; |
| 40 | let end = chars.len() - usize::from(end_anchored); |
| 41 | while i < end { |
| 42 | let atom = match chars[i] { |
| 43 | '.' => Atom::Any, |
| 44 | '\\' if i + 1 < end => { |
| 45 | i += 1; |
| 46 | Atom::Lit(chars[i]) |
| 47 | } |
| 48 | '[' => { |
| 49 | let mut j = i + 1; |
| 50 | let negated = chars.get(j) == Some(&'^'); |
| 51 | j += usize::from(negated); |
| 52 | let mut ranges = Vec::new(); |
| 53 | while j < end && chars[j] != ']' { |
| 54 | let lo = chars[j]; |
| 55 | if chars.get(j + 1) == Some(&'-') && j + 2 < end && chars[j + 2] != ']' { |
| 56 | ranges.push((lo, chars[j + 2])); |
| 57 | j += 3; |
| 58 | } else { |
| 59 | ranges.push((lo, lo)); |
| 60 | j += 1; |
| 61 | } |
| 62 | } |
| 63 | i = j; |
| 64 | Atom::Class { ranges, negated } |
| 65 | } |
| 66 | c => Atom::Lit(c), |
| 67 | }; |
| 68 | i += 1; |
| 69 | let (min, max) = match chars.get(i).filter(|_| i < end) { |
| 70 | Some('*') => (0, usize::MAX), |
| 71 | Some('+') => (1, usize::MAX), |
| 72 | Some('?') => (0, 1), |
| 73 | _ => (1, 1), |
| 74 | }; |
| 75 | if (min, max) != (1, 1) { |
| 76 | i += 1; |
| 77 | } |
| 78 | tokens.push(Token { atom, min, max }); |
| 79 | } |
| 80 | Branch { start_anchored, end_anchored, tokens } |
| 81 | } |
| 82 | |
| 83 | fn match_from(tokens: &[Token], text: &[char], end_anchored: bool) -> bool { |
| 84 | let Some((tok, rest)) = tokens.split_first() else { |
| 85 | return !end_anchored || text.is_empty(); |
| 86 | }; |
| 87 | let available = text.iter().take(tok.max).take_while(|&&c| tok.atom.matches(c)).count(); |
| 88 | (tok.min..=available).rev().any(|n| match_from(rest, &text[n..], end_anchored)) |
| 89 | } |
| 90 | |
| 91 | /// True when any `|` alternative of `pattern` matches `text`. |
| 92 | pub(super) fn is_match(pattern: &str, text: &str) -> bool { |
| 93 | let text: Vec<char> = text.to_lowercase().chars().collect(); |
| 94 | split_alternatives(pattern).into_iter().any(|alt| { |
| 95 | let branch = parse_branch(alt); |
| 96 | let last_start = if branch.start_anchored { 0 } else { text.len() }; |
| 97 | (0..=last_start).any(|s| match_from(&branch.tokens, &text[s..], branch.end_anchored)) |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | fn split_alternatives(pattern: &str) -> Vec<&str> { |
| 102 | let (mut parts, mut start, mut escaped, mut in_class) = (Vec::new(), 0, false, false); |
| 103 | for (i, c) in pattern.char_indices() { |
| 104 | match c { |
| 105 | _ if escaped => escaped = false, |
| 106 | '\\' => escaped = true, |
| 107 | '[' => in_class = true, |
| 108 | ']' => in_class = false, |
| 109 | '|' if !in_class => { |
| 110 | parts.push(&pattern[start..i]); |
| 111 | start = i + 1; |
| 112 | } |
| 113 | _ => {} |
| 114 | } |
| 115 | } |
| 116 | parts.push(&pattern[start..]); |
| 117 | parts |
| 118 | } |
| 119 | |
| 120 | #[cfg(test)] |
| 121 | mod tests { |
| 122 | use super::is_match; |
| 123 | |
| 124 | #[test] |
| 125 | fn subset_semantics() { |
| 126 | assert!(is_match(".*", "Normal")); |
| 127 | assert!(is_match("^z$", "Z")); |
| 128 | assert!(!is_match("^z$", "BackZ")); |
| 129 | assert!(is_match("vec", "BackVector")); |
| 130 | assert!(!is_match("^vec", "BackVector")); |
| 131 | assert!(is_match("^vector$", "Vector")); |
| 132 | assert!(is_match("^z$|^normal$", "Normal")); |
| 133 | assert!(!is_match("^z$|^normal$", "Vector")); |
| 134 | assert!(is_match("^[a-c]+x$", "abcx")); |
| 135 | assert!(!is_match("^[^a-c]+x$", "abcx")); |
| 136 | assert!(is_match("^back?vector$", "BackVector")); |
| 137 | assert!(is_match("^ba?ck?vector$", "bcvector")); |
| 138 | } |
| 139 | } |