Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ impl CompiledPattern {
if config.ignore_case {
options |= RegexOptions::REGEX_OPTION_IGNORECASE;
}
// In GNU grep's Basic/Extended modes, `-z` makes newline ordinary data
// for `.`, but PCRE keeps its existing non-DOTALL behavior. The GNU
// `pcre-context` test documents this as current behavior until PCRE2.
if config.null_data && matches!(config.regex_mode, RegexMode::Basic | RegexMode::Extended) {
options |= RegexOptions::REGEX_OPTION_MULTILINE;
}

fn compile_with(pattern: &str, syntax: &Syntax, options: RegexOptions) -> UResult<Regex> {
Regex::with_options_and_encoding(pattern, options, syntax).map_err(|err| {
Expand Down
21 changes: 21 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,27 @@ fn null_data_mode_records() {
.succeeds()
.stdout_is_bytes(b"hello\0");

// With NUL-delimited records, newline is ordinary data and `.` matches it.
let (_s, mut c) = ucmd();
c.args(&["-z", "-o", "."])
.pipe_in(&b"a\nb"[..])
.succeeds()
.stdout_is_bytes(b"a\0\n\0b\0");

// GNU grep's PCRE path currently does not let `.*` consume the extra
// newline here under -z; this mirrors the GNU pcre-context test.
let (_s, mut c) = ucmd();
c.args(&["-P", "-z", "-o", r"(?<=\n\n\n).*"])
.pipe_in(
&b"NUL preceded by 0 empty lines.\0\
\nNUL preceded by 1 empty line.\0\
\n\nNUL preceded by 2 empty lines.\0\
\n\n\nNUL preceded by 3 empty lines.\0\
\n\n\n\nNUL preceded by 4 empty lines.\0\n"[..],
)
.succeeds()
.stdout_is_bytes(b"NUL preceded by 3 empty lines.\0NUL preceded by 4 empty lines.\0");

// Counting works under -z.
let (_s, mut c) = ucmd();
c.args(&["-z", "-c", "hello"])
Expand Down
Loading