Skip to content

Commit

Permalink
Refuse to translate if set2 contains > 1 unique characters
Browse files Browse the repository at this point in the history
  • Loading branch information
cvonelm committed Jun 15, 2024
1 parent 6a13c48 commit 08717c3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum BadSequence {
EmptySet2WhenNotTruncatingSet1,
ClassExceptLowerUpperInSet2,
ClassInSet2NotMatchedBySet1,
ComplementMoreThanOneUniqueInSet2,
}

impl Display for BadSequence {
Expand Down Expand Up @@ -62,6 +63,9 @@ impl Display for BadSequence {
Self::ClassInSet2NotMatchedBySet1 => {
write!(f, "when translating, every 'upper'/'lower' in set2 must be matched by a 'upper'/'lower' in the same position in set1")
}
Self::ComplementMoreThanOneUniqueInSet2 => {
write!(f, "when translating with complemented character classes,\nstring2 must map all characters in the domain to one")
}
}
}
}
Expand Down Expand Up @@ -220,7 +224,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 @@ -258,6 +261,17 @@ impl Sequence {
.flat_map(Self::flatten_all)
.filter_map(to_u8)
.collect();
let mut set2_uniques = set2_solved.clone();
set2_uniques.sort();
set2_uniques.dedup();

if set1.iter().any(|x| matches!(x, Self::Class(_)))
&& translating
&& complement_flag
&& set2_uniques.len() > 1
{
return Err(BadSequence::ComplementMoreThanOneUniqueInSet2);
}

//Truncation is done dead last. It has no influence on the other conversion steps
if truncate_set1_flag {
Expand Down

0 comments on commit 08717c3

Please sign in to comment.