grep: honor GNU buffer anchors (#56)

This commit is contained in:
Wondr
2026-06-05 17:33:23 +02:00
committed by GitHub
parent f2f86ef572
commit e925ff4ac8
2 changed files with 19 additions and 0 deletions
+4
View File
@@ -269,6 +269,10 @@ impl CompiledPattern {
// GNU grep supports `{,n}` as an alias for `{0,n}`.
syntax.enable_behavior(SyntaxBehavior::SYNTAX_BEHAVIOR_ALLOW_INTERVAL_LOW_ABBREV);
}
if matches!(config.regex_mode, RegexMode::Basic | RegexMode::Extended) {
// GNU grep supports \` and \' as buffer anchors in BRE and ERE.
syntax.enable_operators(SyntaxOperator::SYNTAX_OPERATOR_ESC_GNU_BUF_ANCHOR);
}
if config.regex_mode == RegexMode::Perl {
// GNU grep supports `(?P<name>...)`.
// Unfortunately, the onig crate defines the OP2 flag without the
+15
View File
@@ -85,6 +85,21 @@ fn bre_gnu_extensions() {
.stdout_only("*foo\n**foo\n");
}
#[test]
fn gnu_buffer_anchors() {
let (_s, mut c) = ucmd();
c.args(&[r"\`c\|r\'"])
.pipe_in("cat\nscat\ntar\ndog\n")
.succeeds()
.stdout_only("cat\ntar\n");
let (_s, mut c) = ucmd();
c.args(&["-E", r"\`c|r\'"])
.pipe_in("cat\nscat\ntar\ndog\n")
.succeeds()
.stdout_only("cat\ntar\n");
}
#[test]
fn ere_metacharacters() {
let cases: &[(&[&str], &str, &str)] = &[