1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

tr: calculate complement set early

Fixes #6163 and adds a test to verify that a regression is not caused.

Instead of inverting the conditions to check (e.g. delete characters **not** present in set1) invert
set1 when passed the complement flag (`-c`, `-C`, `--complement`). This is done by calculating set1
then "inverting" it by subtracting from the "full" (universe) set (0..=u8::MAX).

This fixes issue 6163 because it was caused by a combination of the `-c` and `-t` flag. `-c` is the
abovementioned complement flag and `-t`/`--truncate-set1` truncates set1 to the length of set2. What
happened in issue 6163 is that `set1={b'Y'}` and `set2={b'Z'}`, when truncated set1 stays the same
and we proceed. The problem is GNU utils does not consider set1 to be `{b'Y'}`, but the complement
of `{b'Y'}`, that is `U \ {b'Y'}={0, 1, ..., b'X', b'Z', ...}`, thus it is truncated to `{0}`.

We can verify this by doing: `printf '\0' | tr -c -t Y Z`, which prints `Z` to stdout as expected.

Additionally, by calculating the complement of set1 we no longer need to consider the complement
flag when doing the translate operation, this allows us to delete a lot of code.
This commit is contained in:
Jalil David Salamé Messina 2024-05-04 15:26:47 +02:00 committed by Ben Wiederhake
parent 3b96ff1d10
commit 3c47f27698
3 changed files with 56 additions and 136 deletions

View file

@ -1313,3 +1313,24 @@ fn check_regression_class_blank() {
.no_stderr()
.stdout_only("a12b");
}
// Check regression found in https://github.com/uutils/coreutils/issues/6163
#[test]
fn check_regression_issue_6163_no_match() {
new_ucmd!()
.args(&["-c", "-t", "Y", "Z"])
.pipe_in("X\n")
.succeeds()
.no_stderr()
.stdout_only("X\n");
}
#[test]
fn check_regression_issue_6163_match() {
new_ucmd!()
.args(&["-c", "-t", "Y", "Z"])
.pipe_in("\0\n")
.succeeds()
.no_stderr()
.stdout_only("Z\n");
}