Skip to content
Myr

regex_helper.rs

This file

Path

src/regex_helper.rs

105 lines · 3.5 KiB · Rust

Last changed by

Merge pull request #1813 from tmccombs/rust-versionThayne McCombsbefore adoptiond23ac06

History of this file

Take it

1use regex_syntax::ParserBuilder;2use regex_syntax::hir::Hir;34/// Determine if a regex pattern contains a literal uppercase character.5pub fn pattern_has_uppercase_char(pattern: &str) -> bool {6    let mut parser = ParserBuilder::new().utf8(false).build();78    parser9        .parse(pattern)10        .map(|hir| hir_has_uppercase_char(&hir))11        .unwrap_or(false)12}1314/// Determine if a regex expression contains a literal uppercase character.15fn hir_has_uppercase_char(hir: &Hir) -> bool {16    use regex_syntax::hir::*;1718    match hir.kind() {19        HirKind::Literal(Literal(bytes)) => match std::str::from_utf8(bytes) {20            Ok(s) => s.chars().any(|c| c.is_uppercase()),21            Err(_) => bytes.iter().any(|b| char::from(*b).is_uppercase()),22        },23        HirKind::Class(Class::Unicode(ranges)) => ranges24            .iter()25            .any(|r| r.start().is_uppercase() || r.end().is_uppercase()),26        HirKind::Class(Class::Bytes(ranges)) => ranges27            .iter()28            .any(|r| char::from(r.start()).is_uppercase() || char::from(r.end()).is_uppercase()),29        HirKind::Capture(Capture { sub, .. }) | HirKind::Repetition(Repetition { sub, .. }) => {30            hir_has_uppercase_char(sub)31        }32        HirKind::Concat(hirs) | HirKind::Alternation(hirs) => {33            hirs.iter().any(hir_has_uppercase_char)34        }35        _ => false,36    }37}3839/// Determine if a regex pattern only matches strings starting with a literal dot (hidden files)40pub fn pattern_matches_strings_with_leading_dot(pattern: &str) -> bool {41    let mut parser = ParserBuilder::new().utf8(false).build();4243    parser44        .parse(pattern)45        .map(|hir| hir_matches_strings_with_leading_dot(&hir))46        .unwrap_or(false)47}4849/// See above.50fn hir_matches_strings_with_leading_dot(hir: &Hir) -> bool {51    use regex_syntax::hir::*;5253    // Note: this only really detects the simplest case where a regex starts with54    // "^\\.", i.e. a start text anchor and a literal dot character. There are a lot55    // of other patterns that ONLY match hidden files, e.g. ^(\\.foo|\\.bar) which are56    // not (yet) detected by this algorithm.57    match hir.kind() {58        HirKind::Concat(hirs) => {59            let mut hirs = hirs.iter();60            if let Some(hir) = hirs.next() {61                if hir.kind() != &HirKind::Look(Look::Start) {62                    return false;63                }64            } else {65                return false;66            }6768            if let Some(hir) = hirs.next() {69                match hir.kind() {70                    HirKind::Literal(Literal(bytes)) => bytes.starts_with(b"."),71                    _ => false,72                }73            } else {74                false75            }76        }77        _ => false,78    }79}8081#[test]82fn pattern_has_uppercase_char_simple() {83    assert!(pattern_has_uppercase_char("A"));84    assert!(pattern_has_uppercase_char("foo.EXE"));8586    assert!(!pattern_has_uppercase_char("a"));87    assert!(!pattern_has_uppercase_char("foo.exe123"));88}8990#[test]91fn pattern_has_uppercase_char_advanced() {92    assert!(pattern_has_uppercase_char("foo.[a-zA-Z]"));9394    assert!(!pattern_has_uppercase_char(r"\Acargo"));95    assert!(!pattern_has_uppercase_char(r"carg\x6F"));96}9798#[test]99fn matches_strings_with_leading_dot_simple() {100    assert!(pattern_matches_strings_with_leading_dot("^\\.gitignore"));101102    assert!(!pattern_matches_strings_with_leading_dot("^.gitignore"));103    assert!(!pattern_matches_strings_with_leading_dot("\\.gitignore"));104    assert!(!pattern_matches_strings_with_leading_dot("^gitignore"));105}