@@ -21,7 +21,7 @@ impl RegexCompoundCharSet {
2121 self
2222 }
2323
24- pub ( crate ) fn add ( mut self , other : RegexCharSet ) -> Regex {
24+ pub ( crate ) fn add ( mut self , other : RegexCharSet ) -> Option < Regex > {
2525 if other. negative && self . intersections . iter ( ) . all ( |i| i. negative ) {
2626 let mut intersections = self . intersections . into_iter ( ) ;
2727 let mut char_set = intersections. next ( ) . expect ( "Intersection is empty" ) ;
@@ -32,13 +32,19 @@ impl RegexCompoundCharSet {
3232 if self . negative {
3333 char_set = char_set. negate ( ) ;
3434 }
35- Regex :: CharSet ( char_set)
36- } else {
35+ Some ( Regex :: CharSet ( char_set) )
36+ } else if self . may_intersect ( & other ) {
3737 self . intersections . push ( other) ;
38- Regex :: CompoundCharSet ( self )
38+ Some ( Regex :: CompoundCharSet ( self ) )
39+ } else {
40+ None
3941 }
4042 }
4143
44+ fn may_intersect ( & self , other : & RegexCharSet ) -> bool {
45+ self . intersections . iter ( ) . any ( |set| set. may_intersect ( other) )
46+ }
47+
4248 pub ( crate ) fn codegen ( & self , buf : & mut String , flavor : RegexFlavor ) {
4349 if self . negative {
4450 buf. push_str ( "[^" ) ;
@@ -76,6 +82,10 @@ impl RegexCharSet {
7682 self
7783 }
7884
85+ pub ( crate ) fn may_intersect ( & self , other : & Self ) -> bool {
86+ self . negative || other. negative || self . set . may_intersect ( & other. set )
87+ }
88+
7989 pub ( crate ) fn codegen ( & self , buf : & mut String , flavor : RegexFlavor , inside_compound : bool ) {
8090 if self . set . len ( ) == 1 {
8191 if let Some ( range) = self . set . ranges ( ) . next ( ) {
0 commit comments