Skip to content
Myr

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

3 files+38 added−37 removed

modified:src/internal/mod.rs-36 removed
1use std::borrow::Cow;2use std::ffi::{OsStr, OsString};34use regex_syntax::hir::Hir;5use regex_syntax::ParserBuilder;64#[cfg(any(unix, target_os = "redox"))]5pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {6 use std::os::unix::ffi::OsStrExt;7 Cow::Borrowed(input.as_bytes())
8 unmodified lines
16 Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),17 }18}1923/// Determine if a regex pattern contains a literal uppercase character.24pub fn pattern_has_uppercase_char(pattern: &str) -> bool {25 let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();2627 parser28 .parse(pattern)29 .map(|hir| hir_has_uppercase_char(&hir))30 .unwrap_or(false)31}3233/// Determine if a regex expression contains a literal uppercase character.34fn hir_has_uppercase_char(hir: &Hir) -> bool {35 use regex_syntax::hir::*;3637 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}5520/// Maximum size of the output buffer before flushing results to the console21pub const MAX_BUFFER_LENGTH: usize = 1000;2223/// Traverse args_os, looking for -exec and replacing it with --exec.
113 unmodified lines
modified:src/main.rs+3 added-1 removed
8 unmodified lines
9mod fshelper;10mod internal;11mod options;12mod output;13mod regex_helper;14mod walk;1516use std::env;17use std::path::{Path, PathBuf};
8 unmodified lines
2627use crate::exec::CommandTemplate;28use crate::filetypes::FileTypes;29use crate::filter::{SizeFilter, TimeFilter};29use crate::internal::{pattern_has_uppercase_char, transform_args_with_exec};30use crate::internal::transform_args_with_exec;31use crate::options::Options;32use crate::regex_helper::pattern_has_uppercase_char;3334// 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 lines
added:src/regex_helper.rs+35 added
1use regex_syntax::hir::Hir;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();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::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}