Skip to content

Commit

Permalink
tr: Raise error if set2 is too big on complemented class
Browse files Browse the repository at this point in the history
  • Loading branch information
RenjiSann committed Jul 12, 2024
1 parent 47d1bee commit 5ad3321
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,14 @@ impl Sequence {
set2_uniques.sort();
set2_uniques.dedup();

//If the complement flag is used in translate mode, only one unique character may appear in
//set2. Validate this with the set of uniques in set2 that we just generated.
// If the complement flag is used in translate mode, only one unique
// character may appear in set2. Validate this with the set of uniques
// in set2 that we just generated.
// Also, set2 must not overgrow set1, otherwise the mapping can't be 1:1.
if set1.iter().any(|x| matches!(x, Self::Class(_)))
&& translating
&& complement_flag
&& set2_uniques.len() > 1
&& (set2_uniques.len() > 1 || set2_solved.len() > set1_len)
{
return Err(BadSequence::ComplementMoreThanOneUniqueInSet2);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,3 +1413,12 @@ fn check_complement_1_unique_in_set2() {
.args(&["-c", "[:upper:]", arg.as_str()])
.succeeds();
}

#[test]
fn check_complement_set2_too_big() {
let x231 = "x".repeat(231);

Check failure on line 1420 in tests/by-util/test_tr.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'tests/by-util/test_tr.rs', line:1420; use `cargo fmt -- "tests/by-util/test_tr.rs"`)
// [:upper:] expands to 230 characters, putting more characters in set2
// should fails.
new_ucmd!().args(&["-c", "[:upper:]", x231.as_str()]).fails();
}

0 comments on commit 5ad3321

Please sign in to comment.