Move regex helpers to new module
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent a3060f9 · Browse files · 3 files·+38 added−37 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+38 added−37 removed
- other:+38 added−37 removed
Provenance
- Commit
809fd8f- Tree
a81d2d0- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
a3060f9
3 files changed+38 added−37 removed
modified:src/internal/mod.rs-36 removed
1 use std::borrow::Cow;2 use std::ffi::{OsStr, OsString};3 4-use regex_syntax::hir::Hir;5-use regex_syntax::ParserBuilder;6-4 #[cfg(any(unix, target_os = "redox"))]5 pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {6 use std::os::unix::ffi::OsStrExt;7 Cow::Borrowed(input.as_bytes())8 unmodified lines16 Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),17 }18 }19 23-/// Determine if a regex pattern contains a literal uppercase character.24-pub fn pattern_has_uppercase_char(pattern: &str) -> bool {25- let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();26-27- parser28- .parse(pattern)29- .map(|hir| hir_has_uppercase_char(&hir))30- .unwrap_or(false)31-}32-33-/// Determine if a regex expression contains a literal uppercase character.34-fn hir_has_uppercase_char(hir: &Hir) -> bool {35- use regex_syntax::hir::*;36-37- match *hir.kind() {38- HirKind::Literal(Literal::Unicode(c)) => c.is_uppercase(),39- HirKind::Literal(Literal::Byte(b)) => char::from(b).is_uppercase(),40- HirKind::Class(Class::Unicode(ref ranges)) => ranges41- .iter()42- .any(|r| r.start().is_uppercase() || r.end().is_uppercase()),43- HirKind::Class(Class::Bytes(ref ranges)) => ranges44- .iter()45- .any(|r| char::from(r.start()).is_uppercase() || char::from(r.end()).is_uppercase()),46- HirKind::Group(Group { ref hir, .. }) | HirKind::Repetition(Repetition { ref hir, .. }) => {47- hir_has_uppercase_char(hir)48- }49- HirKind::Concat(ref hirs) | HirKind::Alternation(ref hirs) => {50- hirs.iter().any(hir_has_uppercase_char)51- }52- _ => false,53- }54-}55-20 /// Maximum size of the output buffer before flushing results to the console21 pub const MAX_BUFFER_LENGTH: usize = 1000;22 23 /// Traverse args_os, looking for -exec and replacing it with --exec.113 unmodified linesmodified:src/main.rs+3 added-1 removed
8 unmodified lines9 mod fshelper;10 mod internal;11 mod options;12 mod output;13+mod regex_helper;14 mod walk;15 16 use std::env;17 use std::path::{Path, PathBuf};8 unmodified lines26 27 use crate::exec::CommandTemplate;28 use crate::filetypes::FileTypes;29 use crate::filter::{SizeFilter, TimeFilter};29-use crate::internal::{pattern_has_uppercase_char, transform_args_with_exec};30+use crate::internal::transform_args_with_exec;31 use crate::options::Options;32+use crate::regex_helper::pattern_has_uppercase_char;33 34 // We use jemalloc for performance reasons, see https://github.com/sharkdp/fd/pull/48135 #[cfg(all(not(windows), not(target_env = "musl")))]36 #[global_allocator]285 unmodified linesadded:src/regex_helper.rs+35 added
1+use regex_syntax::hir::Hir;2+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();7+8+ parser9+ .parse(pattern)10+ .map(|hir| hir_has_uppercase_char(&hir))11+ .unwrap_or(false)12+}13+14+/// Determine if a regex expression contains a literal uppercase character.15+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(),21+ HirKind::Class(Class::Unicode(ref ranges)) => ranges22+ .iter()23+ .any(|r| r.start().is_uppercase() || r.end().is_uppercase()),24+ HirKind::Class(Class::Bytes(ref ranges)) => ranges25+ .iter()26+ .any(|r| char::from(r.start()).is_uppercase() || char::from(r.end()).is_uppercase()),27+ HirKind::Group(Group { ref hir, .. }) | HirKind::Repetition(Repetition { ref hir, .. }) => {28+ hir_has_uppercase_char(hir)29+ }30+ HirKind::Concat(ref hirs) | HirKind::Alternation(ref hirs) => {31+ hirs.iter().any(hir_has_uppercase_char)32+ }33+ _ => false,34+ }35+}