Use regex::bytes::* instead of regex::*
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 2545aaa · Browse files · 4 files·+39 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
4 files+39 added−16 removed
- other:+39 added−16 removed
Provenance
- Commit
641594c- Tree
22eac78- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
2545aaa
4 files changed+39 added−16 removed
modified:src/internal/mod.rs+19 added-1 removed
4 unmodified lines5 // at your option. All files in the project carrying such6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9+use std::borrow::Cow;10+use std::ffi::{OsStr, OsString};11+12 use regex_syntax::hir::Hir;13 use regex_syntax::Parser;11-use std::ffi::OsString;14 15 pub use self::file_types::FileTypes;16 17 mod file_types;10 unmodified lines28 ::std::process::exit(1);29 };30 }31 32+#[cfg(any(unix, target_os = "redox"))]33+pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {34+ use std::os::unix::ffi::OsStrExt;35+ Cow::Borrowed(input.as_bytes())36+}37+38+#[cfg(windows)]39+pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {40+ let string = input.to_string_lossy();41+42+ match string {43+ Cow::Owned(string) => Cow::Owned(string.into_bytes()),44+ Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),45+ }46+}47+48 /// Determine if a regex pattern contains a literal uppercase character.49 pub fn pattern_has_uppercase_char(pattern: &str) -> bool {50 Parser::new()51 .parse(pattern)140 unmodified linesmodified:src/internal/opts.rs+1 added-1 removed
2 unmodified lines3 filter::{SizeFilter, TimeFilter},4 FileTypes,5 };6 use lscolors::LsColors;7-use regex::RegexSet;7+use regex::bytes::RegexSet;8 use std::{path::PathBuf, sync::Arc, time::Duration};9 10 /// Configuration options for *fd*.11 pub struct FdOptions {70 unmodified linesmodified:src/main.rs+1 added-1 removed
23 unmodified lines24 use std::time;25 26 use atty::Stream;27 use lscolors::LsColors;28-use regex::{RegexBuilder, RegexSetBuilder};28+use regex::bytes::{RegexBuilder, RegexSetBuilder};29 30 use crate::exec::CommandTemplate;31 use crate::internal::{32 filter::{SizeFilter, TimeFilter},237 unmodified linesmodified:src/walk.rs+18 added-13 removed
7 unmodified lines8 9 use crate::exec;10 use crate::exit_codes::ExitCode;11 use crate::fshelper;12-use crate::internal::{opts::FdOptions, MAX_BUFFER_LENGTH};12+use crate::internal::{opts::FdOptions, osstr_to_bytes, MAX_BUFFER_LENGTH};13 use crate::output;14 15+use std::borrow::Cow;16 use std::error::Error;17+use std::ffi::OsStr;18 use std::io;19 use std::path::PathBuf;20 use std::process;21 use std::sync::atomic::{AtomicBool, Ordering};3 unmodified lines25 use std::time;26 27 use ignore::overrides::OverrideBuilder;28 use ignore::{self, WalkBuilder};27-use regex::Regex;29+use regex::bytes::Regex;30 31 /// The receiver thread can either be buffering results or directly streaming to the console.32 enum ReceiverMode {33 /// Receiver is still buffering in order to sort the results, if the search finishes fast246 unmodified lines280 return ignore::WalkState::Continue;281 }282 283 // Check the name first, since it doesn't require metadata282-284 let entry_path = entry.path();285 285- let search_str_o = if config.search_full_path {286+ let search_str: Cow<OsStr> = if config.search_full_path {287 match fshelper::path_absolute_form(entry_path) {287- Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),288+ Ok(path_abs_buf) => Cow::Owned(path_abs_buf.as_os_str().to_os_string()),289 Err(_) => {290 print_error_and_exit!("Unable to retrieve absolute path.");291 }292 }293 } else {293- entry_path.file_name().map(|f| f.to_string_lossy())294+ match entry_path.file_name() {295+ Some(filename) => Cow::Borrowed(filename),296+ None => unreachable!(297+ "Encountered file system entry without a file name. This should only \298+ happen for paths like 'foo/bar/..' or '/' which are not supposed to \299+ appear in a file system traversal."300+ ),301+ }302 };303 296- if let Some(search_str) = search_str_o {297- if !pattern.is_match(&*search_str) {298- return ignore::WalkState::Continue;304+ if !pattern.is_match(&osstr_to_bytes(search_str.as_ref())) {305+ return ignore::WalkState::Continue;299- }306 }307 308 // Filter out unwanted extensions.309 if let Some(ref exts_regex) = config.extensions {304- if let Some(path_str) = entry_path.file_name().and_then(|s| s.to_str()) {305- if !exts_regex.is_match(path_str) {310+ if let Some(path_str) = entry_path.file_name() {311+ if !exts_regex.is_match(&osstr_to_bytes(path_str)) {312 return ignore::WalkState::Continue;313 }314 } else {315 return ignore::WalkState::Continue;316 }317 }318 319 // Filter out unwanted file types.314-320 if let Some(ref file_types) = config.file_types {321 if let Some(ref entry_type) = entry.file_type() {322 if (!file_types.files && entry_type.is_file())323 || (!file_types.directories && entry_type.is_dir())68 unmodified lines