diff --git a/src/matcher.rs b/src/matcher.rs index d9cf846..532c4e3 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -215,6 +215,10 @@ impl CompiledPattern { // GNU grep supports `{,n}` as an alias for `{0,n}`. syntax.enable_behavior(SyntaxBehavior::SYNTAX_BEHAVIOR_ALLOW_INTERVAL_LOW_ABBREV); } + if config.regex_mode == RegexMode::Basic { + // GNU grep treats `\s` and `\S` as whitespace shorthands in BRE mode. + syntax.enable_operators(SyntaxOperator::SYNTAX_OPERATOR_ESC_S_WHITE_SPACE); + } if config.regex_mode == RegexMode::Perl { // GNU grep supports `(?P...)`. // Unfortunately, the onig crate defines the OP2 flag without the diff --git a/tests/test_grep.rs b/tests/test_grep.rs index d061918..4451634 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -39,7 +39,7 @@ fn bre_default_metacharacters() { #[test] fn bre_gnu_extensions() { - // \+ \? \| \{m,n\} \< \> \b \w plus backreferences and leading `*`. + // \+ \? \| \{m,n\} \< \> \b \w \s \S plus backreferences and leading `*`. let (_s, mut c) = ucmd(); c.args(&[r"o\+"]) .pipe_in("o\noo\nx\n") @@ -70,6 +70,18 @@ fn bre_gnu_extensions() { .succeeds() .stdout_only("contain\n"); + let (_s, mut c) = ucmd(); + c.args(&[r"\s"]) + .pipe_in("a b\nxy\n") + .succeeds() + .stdout_only("a b\n"); + + let (_s, mut c) = ucmd(); + c.args(&["-c", r"\S"]) + .pipe_in("aS b\n \nx\n") + .succeeds() + .stdout_only("2\n"); + // BRE backreference: repeated adjacent word. let (_s, mut c) = ucmd(); c.args(&[r"\(\b\w\+\b\) \1"])