Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/find/matchers/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use std::{error::Error, fmt, str::FromStr};

use onig::{Regex, RegexOptions, Syntax};
use onig::{Regex, RegexOptions, Syntax, SyntaxBehavior};

use super::{Matcher, MatcherIO, WalkEntry};

Expand Down Expand Up @@ -85,22 +85,31 @@ impl RegexMatcher {
pattern: &str,
ignore_case: bool,
) -> Result<Self, Box<dyn Error>> {
// GNU find matches the whole path, including embedded newlines. Onig's
// grep syntax excludes newline from negated character classes, so turn
// that behavior off while keeping grep's other semantics intact.
let mut grep_syntax = Syntax::grep().clone();
grep_syntax.disable_behavior(SyntaxBehavior::SYNTAX_BEHAVIOR_NOT_NEWLINE_IN_NEGATIVE_CC);
let syntax = match regex_type {
RegexType::Emacs => Syntax::emacs(),
RegexType::Grep => Syntax::grep(),
RegexType::Grep => &grep_syntax,
RegexType::PosixBasic => Syntax::posix_basic(),
RegexType::PosixExtended => Syntax::posix_extended(),
};

let regex = Regex::with_options(
pattern,
if ignore_case {
RegexOptions::REGEX_OPTION_IGNORECASE
} else {
RegexOptions::REGEX_OPTION_NONE
},
syntax,
)?;
let mut options = if ignore_case {
RegexOptions::REGEX_OPTION_IGNORECASE
} else {
RegexOptions::REGEX_OPTION_NONE
};
// GNU's "grep" regex type matches newlines in paths. Oniguruma's grep
// syntax is line-oriented by default, so let '.' match newline and
// clear SINGLELINE (so negated brackets also match newline).
if regex_type == RegexType::Grep {
options.insert(RegexOptions::REGEX_OPTION_MULTILINE);
options.insert(RegexOptions::REGEX_OPTION_NEGATE_SINGLELINE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this makes [^...] match a newline?
i think grep syntax excludes it through a syntax flag, not SINGLELINE
please add a test with a negated bracket, like .*/new[^a]line\.php

}
let regex = Regex::with_options(pattern, options, syntax)?;
Ok(Self { regex })
}
}
Expand Down
52 changes: 40 additions & 12 deletions src/locate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{

use clap::{self, crate_version, value_parser, Arg, ArgAction, ArgMatches, Command, Id};
use itertools::Itertools;
use onig::{Regex, RegexOptions, Syntax};
use onig::{Regex, RegexOptions, Syntax, SyntaxBehavior};
use thiserror::Error;
use uucore::error::{ClapErrorWrapper, UClapError, UError, UResult};

Expand Down Expand Up @@ -225,22 +225,28 @@ pub struct ParsedInfo {
}

fn make_regex(ty: RegexType, config: &Config, pattern: &str) -> Result<Regex, onig::Error> {
// Let negated character classes match newlines like GNU find does.
let mut grep_syntax = Syntax::grep().clone();
grep_syntax.disable_behavior(SyntaxBehavior::SYNTAX_BEHAVIOR_NOT_NEWLINE_IN_NEGATIVE_CC);
let syntax = match ty {
RegexType::Emacs => Syntax::emacs(),
RegexType::Grep => Syntax::grep(),
RegexType::Grep => &grep_syntax,
RegexType::PosixBasic => Syntax::posix_basic(),
RegexType::PosixExtended => Syntax::posix_extended(),
};

Regex::with_options(
pattern,
if config.ignore_case {
RegexOptions::REGEX_OPTION_IGNORECASE
} else {
RegexOptions::REGEX_OPTION_NONE
},
syntax,
)
let mut options = if config.ignore_case {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now the same code as in RegexMatcher::new, could be dedup, no?

RegexOptions::REGEX_OPTION_IGNORECASE
} else {
RegexOptions::REGEX_OPTION_NONE
};
// Match GNU's "grep" regex type: let '.' match newline and clear
// SINGLELINE so negated brackets match newline too.
if ty == RegexType::Grep {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a locate test too (tests/db_tests.rs)

options.insert(RegexOptions::REGEX_OPTION_MULTILINE);
options.insert(RegexOptions::REGEX_OPTION_NEGATE_SINGLELINE);
}
Regex::with_options(pattern, options, syntax)
}

impl TryFrom<ArgMatches> for ParsedInfo {
Expand Down Expand Up @@ -734,7 +740,29 @@ mod tests {
use std::io::{self, Write};
use std::path::Path;

use super::{is_db_too_old, path_exists, DbReader, Statistics};
use super::{
is_db_too_old, make_regex, path_exists, Config, DbReader, ExistenceMode, Mode, RegexType,
Statistics,
};

#[test]
fn grep_regex_matches_newline_in_path() {
let config = Config {
all: false,
basename: false,
mode: Mode::Normal,
db: vec![],
existing: ExistenceMode::default(),
follow_symlinks: false,
ignore_case: false,
limit: None,
max_age: 0,
null_bytes: false,
print: false,
};
let re = make_regex(RegexType::Grep, &config, r".*/new[^a]line\.php").unwrap();
assert!(re.find("up/new\nline.php").is_some());
}

/// A writer that always fails, to emulate stdout with no space left
/// (`> /dev/full`) or a closed pipe.
Expand Down
33 changes: 33 additions & 0 deletions tests/test_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,39 @@ fn regex_types() {
.stdout_contains("teeest");
}

#[test]
#[cfg(unix)]
fn find_grep_regex_matches_newline_in_path() {
let temp_dir = Builder::new().prefix("find_grep_nl_").tempdir().unwrap();
File::create(temp_dir.path().join("plain.php")).expect("create plain file");
File::create(temp_dir.path().join("new\nline.php")).expect("create newline file");

ucmd()
.args(&[
temp_dir.path().to_str().unwrap(),
"-regextype",
"grep",
"-regex",
r".*\.php",
])
.succeeds()
.stdout_contains("plain.php")
.stdout_contains("new\nline.php");

// A negated bracket expression must also match the newline, so a
// pattern spanning the newline via [^a] finds the same file.
ucmd()
.args(&[
temp_dir.path().to_str().unwrap(),
"-regextype",
"grep",
"-regex",
r".*/new[^a]line\.php",
])
.succeeds()
.stdout_contains("new\nline.php");
}

#[rstest::rstest]
#[case::inherited(
&["-regextype", "posix-basic", "("],
Expand Down
Loading