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
Provenance
- Commit
a248607- Tree
e1e1971- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
7d357a6- Parent 2
ed23fb9
3 files changed+42 added−16 removed
modified:src/regex_helper.rs+13 added-8 removed
1 unmodified line2 use regex_syntax::ParserBuilder;3 4 /// Determine if a regex pattern contains a literal uppercase character.5 pub 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();7 8 parser9 .parse(pattern)10 .map(|hir| hir_has_uppercase_char(&hir))4 unmodified lines15 fn hir_has_uppercase_char(hir: &Hir) -> bool {16 use regex_syntax::hir::*;17 18 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 lines37 }38 39 /// Determine if a regex pattern only matches strings starting with a literal dot (hidden files)40 pub 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();42 43 parser44 .parse(pattern)45 .map(|hir| hir_matches_strings_with_leading_dot(&hir))11 unmodified lines57 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 }67 68 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 linesmodified:Cargo.lock+27 added-6 removed
10 unmodified lines11 "memchr",12 ]13 14 [[package]]15+name = "aho-corasick"16+version = "1.0.1"17+source = "registry+https://github.com/rust-lang/crates.io-index"18+checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04"19+dependencies = [20+ "memchr",21+]22+23+[[package]]24 name = "android-tzdata"25 version = "0.1.1"26 source = "registry+https://github.com/rust-lang/crates.io-index"27 checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"343 unmodified lines371 version = "0.4.10"372 source = "registry+https://github.com/rust-lang/crates.io-index"373 checksum = "029d74589adefde59de1a0c4f4732695c32805624aec7b68d91503d4dba79afc"374 dependencies = [366- "aho-corasick",375+ "aho-corasick 0.7.20",376 "bstr",377 "fnv",378 "log",379 "regex",318 unmodified lines698 ]699 700 [[package]]701 name = "regex"693-version = "1.7.3"702+version = "1.9.1"703 source = "registry+https://github.com/rust-lang/crates.io-index"695-checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d"704+checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575"705 dependencies = [697- "aho-corasick",706+ "aho-corasick 1.0.1",707 "memchr",708+ "regex-automata",709 "regex-syntax",710 ]711 712 [[package]]713+name = "regex-automata"714+version = "0.3.3"715+source = "registry+https://github.com/rust-lang/crates.io-index"716+checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310"717+dependencies = [718+ "aho-corasick 1.0.1",719+ "memchr",720+ "regex-syntax",721+]722+723+[[package]]724 name = "regex-syntax"704-version = "0.6.29"725+version = "0.7.4"726 source = "registry+https://github.com/rust-lang/crates.io-index"706-checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"727+checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"728 729 [[package]]730 name = "rustix"731 version = "0.36.10"412 unmodified linesmodified:Cargo.toml+2 added-2 removed
36 unmodified lines37383940414241424344454649 unmodified lines36 unmodified linesnu-ansi-term = "0.49"argmax = "0.3.1"ignore = "0.4.20"num_cpus = "1.16"regex = "1.7.3"regex-syntax = "0.6"regex = "1.9.1"regex-syntax = "0.7"ctrlc = "3.2"humantime = "2.1"globset = "0.4"anyhow = "1.0"49 unmodified lines