Skip to content
Myr

Merge pull request #1350 from tmccombs/regex-upgrade

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Regex upgrade

authored by Thayne McCombs · committed by GitHub · parents 7d357a6, ed23fb9 · Browse files · 3 files+42 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+42 added−16 removed

  • other:+42 added−16 removed

3 files+42 added−16 removed

modified:src/regex_helper.rs+13 added-8 removed
1 unmodified line
2use regex_syntax::ParserBuilder;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().allow_invalid_utf8(true).build();6 let mut parser = ParserBuilder::new().utf8(false).build();78 parser9 .parse(pattern)10 .map(|hir| hir_has_uppercase_char(&hir))
4 unmodified lines
15fn hir_has_uppercase_char(hir: &Hir) -> bool {16 use regex_syntax::hir::*;1718 match hir.kind() {19 HirKind::Literal(Literal::Unicode(c)) => c.is_uppercase(),20 HirKind::Literal(Literal::Byte(b)) => char::from(*b).is_uppercase(),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()),27 HirKind::Group(Group { hir, .. }) | HirKind::Repetition(Repetition { hir, .. }) => {28 hir_has_uppercase_char(hir)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 }
2 unmodified lines
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 {39 let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();41 let mut parser = ParserBuilder::new().utf8(false).build();4243 parser44 .parse(pattern)45 .map(|hir| hir_matches_strings_with_leading_dot(&hir))
11 unmodified lines
57 match hir.kind() {58 HirKind::Concat(hirs) => {59 let mut hirs = hirs.iter();60 if let Some(hir) = hirs.next() {59 if hir.kind() != &HirKind::Anchor(Anchor::StartText) {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() {67 hir.kind() == &HirKind::Literal(Literal::Unicode('.'))69 match hir.kind() {70 HirKind::Literal(Literal(bytes)) => bytes.starts_with(&[b'.']),71 _ => false,72 }73 } else {74 false75 }76 }
29 unmodified lines
modified:Cargo.lock+27 added-6 removed
10 unmodified lines
11 "memchr",12]1314[[package]]15name = "aho-corasick"16version = "1.0.1"17source = "registry+https://github.com/rust-lang/crates.io-index"18checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04"19dependencies = [20 "memchr",21]2223[[package]]24name = "android-tzdata"25version = "0.1.1"26source = "registry+https://github.com/rust-lang/crates.io-index"27checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
343 unmodified lines
371version = "0.4.10"372source = "registry+https://github.com/rust-lang/crates.io-index"373checksum = "029d74589adefde59de1a0c4f4732695c32805624aec7b68d91503d4dba79afc"374dependencies = [366 "aho-corasick",375 "aho-corasick 0.7.20",376 "bstr",377 "fnv",378 "log",379 "regex",
318 unmodified lines
698]699700[[package]]701name = "regex"693version = "1.7.3"702version = "1.9.1"703source = "registry+https://github.com/rust-lang/crates.io-index"695checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d"704checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575"705dependencies = [697 "aho-corasick",706 "aho-corasick 1.0.1",707 "memchr",708 "regex-automata",709 "regex-syntax",710]711712[[package]]713name = "regex-automata"714version = "0.3.3"715source = "registry+https://github.com/rust-lang/crates.io-index"716checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310"717dependencies = [718 "aho-corasick 1.0.1",719 "memchr",720 "regex-syntax",721]722723[[package]]724name = "regex-syntax"704version = "0.6.29"725version = "0.7.4"726source = "registry+https://github.com/rust-lang/crates.io-index"706checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"727checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"728729[[package]]730name = "rustix"731version = "0.36.10"
412 unmodified lines
modified:Cargo.toml+2 added-2 removed