Skip to content

Commit

Permalink
Refuse to translate if set2 contains more than one unique characters …
Browse files Browse the repository at this point in the history
…and set1 contains a character class (#6472)

* Refuse to translate if set2 contains > 1 unique characters
  • Loading branch information
cvonelm authored Jun 22, 2024
1 parent 7766257 commit 0ae6d43
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum BadSequence {
ClassExceptLowerUpperInSet2,
ClassInSet2NotMatchedBySet1,
Set1LongerSet2EndsInClass,
ComplementMoreThanOneUniqueInSet2,
}

impl Display for BadSequence {
Expand Down Expand Up @@ -66,6 +67,9 @@ impl Display for BadSequence {
Self::Set1LongerSet2EndsInClass => {
write!(f, "when translating with string1 longer than string2,\nthe latter string must not end with a character class")
}
Self::ComplementMoreThanOneUniqueInSet2 => {
write!(f, "when translating with complemented character classes,\nstring2 must map all characters in the domain to one")
}
}
}
}
Expand Down Expand Up @@ -224,7 +228,6 @@ impl Sequence {
.count();

let star_compensate_len = set1_len.saturating_sub(set2_len);

//Replace CharStar with CharRepeat
set2 = set2
.iter()
Expand Down Expand Up @@ -263,6 +266,21 @@ impl Sequence {
.filter_map(to_u8)
.collect();

// Calculate the set of unique characters in set2
let mut set2_uniques = set2_solved.clone();
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 set1.iter().any(|x| matches!(x, Self::Class(_)))
&& translating
&& complement_flag
&& set2_uniques.len() > 1
{
return Err(BadSequence::ComplementMoreThanOneUniqueInSet2);
}

if set2_solved.len() < set1_solved.len()
&& !truncate_set1_flag
&& matches!(
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,3 +1386,23 @@ fn check_set1_longer_set2_ends_in_class_with_trunc() {
.args(&["-t", "[:lower:]a", "[:upper:]"])
.succeeds();
}

#[test]
fn check_complement_2_unique_in_set2() {
let x226 = "x".repeat(226);

// [y*] is expanded tp "y" here
let arg = x226 + "[y*]xxx";
new_ucmd!().args(&["-c", "[:upper:]", arg.as_str()]).fails();
}

#[test]
fn check_complement_1_unique_in_set2() {
let x226 = "x".repeat(226);

// [y*] is expanded to "" here
let arg = x226 + "[y*]xxxx";
new_ucmd!()
.args(&["-c", "[:upper:]", arg.as_str()])
.succeeds();
}

0 comments on commit 0ae6d43

Please sign in to comment.