Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
10 changes: 9 additions & 1 deletion src/uu/tac/src/tac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,19 @@ fn buffer_tac(data: &[u8], before: bool, separator: &str) -> std::io::Result<()>
Ok(())
}

// Make the regex flavor compatible with `regex` crate
fn translate_regex_flavor(regex: &str) -> String {
let result: String = regex.replace("\\|", "|");
Copy link
Contributor

Choose a reason for hiding this comment

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

you only handles \| but BRE has other differences (\(, \), \{, \} for grouping/repetition).
maybe do that now (and add tests)

also unnecessary explicit type annotation

// TODO: are more translations needed?

result
}

#[allow(clippy::cognitive_complexity)]
fn tac(filenames: &[OsString], before: bool, regex: bool, separator: &str) -> UResult<()> {
// Compile the regular expression pattern if it is provided.
let maybe_pattern = if regex {
match regex::bytes::Regex::new(separator) {
match regex::bytes::Regex::new(&translate_regex_flavor(separator)) {
Ok(p) => Some(p),
Err(e) => return Err(TacError::InvalidRegex(e).into()),
}
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_tac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,12 @@ fn test_stdin_bad_tmpdir_fallback() {
.succeeds()
.stdout_is("c\nb\na\n");
}

#[test]
fn test_regex_or_operator() {
new_ucmd!()
.args(&["-r", "-s", r"[^x]\|x"])
.pipe_in("abc")
.succeeds()
.stdout_contains("cba");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.stdout_contains("cba");
.stdout_is("cba");

}
Loading