mirror of
https://github.com/sharkdp/fd.git
synced 2024-11-18 09:50:34 +01:00
Use regex::bytes::* instead of regex::*
This commit is contained in:
parent
2545aaabd2
commit
641594c2c6
4 changed files with 39 additions and 16 deletions
|
@ -6,9 +6,11 @@
|
||||||
// notice may not be copied, modified, or distributed except
|
// notice may not be copied, modified, or distributed except
|
||||||
// according to those terms.
|
// according to those terms.
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::ffi::{OsStr, OsString};
|
||||||
|
|
||||||
use regex_syntax::hir::Hir;
|
use regex_syntax::hir::Hir;
|
||||||
use regex_syntax::Parser;
|
use regex_syntax::Parser;
|
||||||
use std::ffi::OsString;
|
|
||||||
|
|
||||||
pub use self::file_types::FileTypes;
|
pub use self::file_types::FileTypes;
|
||||||
|
|
||||||
|
@ -27,6 +29,22 @@ macro_rules! print_error_and_exit {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(unix, target_os = "redox"))]
|
||||||
|
pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {
|
||||||
|
use std::os::unix::ffi::OsStrExt;
|
||||||
|
Cow::Borrowed(input.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {
|
||||||
|
let string = input.to_string_lossy();
|
||||||
|
|
||||||
|
match string {
|
||||||
|
Cow::Owned(string) => Cow::Owned(string.into_bytes()),
|
||||||
|
Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Determine if a regex pattern contains a literal uppercase character.
|
/// Determine if a regex pattern contains a literal uppercase character.
|
||||||
pub fn pattern_has_uppercase_char(pattern: &str) -> bool {
|
pub fn pattern_has_uppercase_char(pattern: &str) -> bool {
|
||||||
Parser::new()
|
Parser::new()
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::internal::{
|
||||||
FileTypes,
|
FileTypes,
|
||||||
};
|
};
|
||||||
use lscolors::LsColors;
|
use lscolors::LsColors;
|
||||||
use regex::RegexSet;
|
use regex::bytes::RegexSet;
|
||||||
use std::{path::PathBuf, sync::Arc, time::Duration};
|
use std::{path::PathBuf, sync::Arc, time::Duration};
|
||||||
|
|
||||||
/// Configuration options for *fd*.
|
/// Configuration options for *fd*.
|
||||||
|
|
|
@ -25,7 +25,7 @@ use std::time;
|
||||||
|
|
||||||
use atty::Stream;
|
use atty::Stream;
|
||||||
use lscolors::LsColors;
|
use lscolors::LsColors;
|
||||||
use regex::{RegexBuilder, RegexSetBuilder};
|
use regex::bytes::{RegexBuilder, RegexSetBuilder};
|
||||||
|
|
||||||
use crate::exec::CommandTemplate;
|
use crate::exec::CommandTemplate;
|
||||||
use crate::internal::{
|
use crate::internal::{
|
||||||
|
|
31
src/walk.rs
31
src/walk.rs
|
@ -9,10 +9,12 @@
|
||||||
use crate::exec;
|
use crate::exec;
|
||||||
use crate::exit_codes::ExitCode;
|
use crate::exit_codes::ExitCode;
|
||||||
use crate::fshelper;
|
use crate::fshelper;
|
||||||
use crate::internal::{opts::FdOptions, MAX_BUFFER_LENGTH};
|
use crate::internal::{opts::FdOptions, osstr_to_bytes, MAX_BUFFER_LENGTH};
|
||||||
use crate::output;
|
use crate::output;
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::ffi::OsStr;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
@ -24,7 +26,7 @@ use std::time;
|
||||||
|
|
||||||
use ignore::overrides::OverrideBuilder;
|
use ignore::overrides::OverrideBuilder;
|
||||||
use ignore::{self, WalkBuilder};
|
use ignore::{self, WalkBuilder};
|
||||||
use regex::Regex;
|
use regex::bytes::Regex;
|
||||||
|
|
||||||
/// The receiver thread can either be buffering results or directly streaming to the console.
|
/// The receiver thread can either be buffering results or directly streaming to the console.
|
||||||
enum ReceiverMode {
|
enum ReceiverMode {
|
||||||
|
@ -279,30 +281,34 @@ fn spawn_senders(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the name first, since it doesn't require metadata
|
// Check the name first, since it doesn't require metadata
|
||||||
|
|
||||||
let entry_path = entry.path();
|
let entry_path = entry.path();
|
||||||
|
|
||||||
let search_str_o = if config.search_full_path {
|
let search_str: Cow<OsStr> = if config.search_full_path {
|
||||||
match fshelper::path_absolute_form(entry_path) {
|
match fshelper::path_absolute_form(entry_path) {
|
||||||
Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),
|
Ok(path_abs_buf) => Cow::Owned(path_abs_buf.as_os_str().to_os_string()),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
print_error_and_exit!("Unable to retrieve absolute path.");
|
print_error_and_exit!("Unable to retrieve absolute path.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
entry_path.file_name().map(|f| f.to_string_lossy())
|
match entry_path.file_name() {
|
||||||
|
Some(filename) => Cow::Borrowed(filename),
|
||||||
|
None => unreachable!(
|
||||||
|
"Encountered file system entry without a file name. This should only \
|
||||||
|
happen for paths like 'foo/bar/..' or '/' which are not supposed to \
|
||||||
|
appear in a file system traversal."
|
||||||
|
),
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(search_str) = search_str_o {
|
if !pattern.is_match(&osstr_to_bytes(search_str.as_ref())) {
|
||||||
if !pattern.is_match(&*search_str) {
|
return ignore::WalkState::Continue;
|
||||||
return ignore::WalkState::Continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter out unwanted extensions.
|
// Filter out unwanted extensions.
|
||||||
if let Some(ref exts_regex) = config.extensions {
|
if let Some(ref exts_regex) = config.extensions {
|
||||||
if let Some(path_str) = entry_path.file_name().and_then(|s| s.to_str()) {
|
if let Some(path_str) = entry_path.file_name() {
|
||||||
if !exts_regex.is_match(path_str) {
|
if !exts_regex.is_match(&osstr_to_bytes(path_str)) {
|
||||||
return ignore::WalkState::Continue;
|
return ignore::WalkState::Continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -311,7 +317,6 @@ fn spawn_senders(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter out unwanted file types.
|
// Filter out unwanted file types.
|
||||||
|
|
||||||
if let Some(ref file_types) = config.file_types {
|
if let Some(ref file_types) = config.file_types {
|
||||||
if let Some(ref entry_type) = entry.file_type() {
|
if let Some(ref entry_type) = entry.file_type() {
|
||||||
if (!file_types.files && entry_type.is_file())
|
if (!file_types.files && entry_type.is_file())
|
||||||
|
|
Loading…
Reference in a new issue