@@ -36,6 +36,7 @@ pub enum BadSequence {
3636 ClassExceptLowerUpperInSet2 ,
3737 ClassInSet2NotMatchedBySet1 ,
3838 Set1LongerSet2EndsInClass ,
39+ ComplementMoreThanOneUniqueInSet2 ,
3940}
4041
4142impl Display for BadSequence {
@@ -66,6 +67,9 @@ impl Display for BadSequence {
6667 Self :: Set1LongerSet2EndsInClass => {
6768 write ! ( f, "when translating with string1 longer than string2,\n the latter string must not end with a character class" )
6869 }
70+ Self :: ComplementMoreThanOneUniqueInSet2 => {
71+ write ! ( f, "when translating with complemented character classes,\n string2 must map all characters in the domain to one" )
72+ }
6973 }
7074 }
7175}
@@ -224,7 +228,6 @@ impl Sequence {
224228 . count ( ) ;
225229
226230 let star_compensate_len = set1_len. saturating_sub ( set2_len) ;
227-
228231 //Replace CharStar with CharRepeat
229232 set2 = set2
230233 . iter ( )
@@ -263,6 +266,21 @@ impl Sequence {
263266 . filter_map ( to_u8)
264267 . collect ( ) ;
265268
269+ // Calculate the set of unique characters in set2
270+ let mut set2_uniques = set2_solved. clone ( ) ;
271+ set2_uniques. sort ( ) ;
272+ set2_uniques. dedup ( ) ;
273+
274+ //If the complement flag is used in translate mode, only one unique character may appear in
275+ //set2. Validate this with the set of uniques in set2 that we just generated.
276+ if set1. iter ( ) . any ( |x| matches ! ( x, Self :: Class ( _) ) )
277+ && translating
278+ && complement_flag
279+ && set2_uniques. len ( ) > 1
280+ {
281+ return Err ( BadSequence :: ComplementMoreThanOneUniqueInSet2 ) ;
282+ }
283+
266284 if set2_solved. len ( ) < set1_solved. len ( )
267285 && !truncate_set1_flag
268286 && matches ! (
0 commit comments