@@ -60,6 +60,11 @@ impl GlobPattern {
6060 pub fn as_str ( & self ) -> & str {
6161 self . glob . glob ( )
6262 }
63+
64+ /// Converts this glob pattern to a bytes regex.
65+ pub fn to_regex ( & self ) -> regex:: bytes:: Regex {
66+ self . regex . clone ( )
67+ }
6368}
6469
6570impl Debug for GlobPattern {
@@ -289,6 +294,35 @@ impl StringPattern {
289294 }
290295 }
291296
297+ /// Converts the pattern into a bytes regex.
298+ pub fn to_regex ( & self ) -> regex:: bytes:: Regex {
299+ match self {
300+ Self :: Exact ( literal) => {
301+ regex:: bytes:: RegexBuilder :: new ( & format ! ( "^{}$" , regex:: escape( literal) ) )
302+ . build ( )
303+ . expect ( "impossible to fail to compile regex of literal" )
304+ }
305+ Self :: ExactI ( literal) => {
306+ regex:: bytes:: RegexBuilder :: new ( & format ! ( "^{}$" , regex:: escape( literal) ) )
307+ . case_insensitive ( true )
308+ . build ( )
309+ . expect ( "impossible to fail to compile regex of literal" )
310+ }
311+ Self :: Substring ( literal) => regex:: bytes:: RegexBuilder :: new ( & regex:: escape ( literal) )
312+ . build ( )
313+ . expect ( "impossible to fail to compile regex of literal" ) ,
314+ Self :: SubstringI ( literal) => regex:: bytes:: RegexBuilder :: new ( & regex:: escape ( literal) )
315+ . case_insensitive ( true )
316+ . build ( )
317+ . expect ( "impossible to fail to compile regex of literal" ) ,
318+ Self :: Glob ( glob_pattern) => glob_pattern. to_regex ( ) ,
319+ // The regex generated represents the case insensitivity itself
320+ Self :: GlobI ( glob_pattern) => glob_pattern. to_regex ( ) ,
321+ Self :: Regex ( regex) => regex. clone ( ) ,
322+ Self :: RegexI ( regex) => regex. clone ( ) ,
323+ }
324+ }
325+
292326 /// Iterates entries of the given `map` whose string keys match this
293327 /// pattern.
294328 pub fn filter_btree_map < ' a , ' b , K : Borrow < str > + Ord , V > (
@@ -487,4 +521,25 @@ mod tests {
487521 . is_match( "\u{c0} " )
488522 ) ;
489523 }
524+
525+ #[ test]
526+ fn test_string_pattern_to_regex ( ) {
527+ let check = |pattern : StringPattern , match_to : & str | {
528+ let regex = pattern. to_regex ( ) ;
529+ regex. is_match ( match_to. as_bytes ( ) )
530+ } ;
531+ assert ! ( check( StringPattern :: exact( "$a" ) , "$a" ) ) ;
532+ assert ! ( !check( StringPattern :: exact( "$a" ) , "$A" ) ) ;
533+ assert ! ( !check( StringPattern :: exact( "a" ) , "aa" ) ) ;
534+ assert ! ( !check( StringPattern :: exact( "a" ) , "aa" ) ) ;
535+ assert ! ( check( StringPattern :: exact_i( "a" ) , "A" ) ) ;
536+ assert ! ( check( StringPattern :: substring( "$a" ) , "$abc" ) ) ;
537+ assert ! ( !check( StringPattern :: substring( "$a" ) , "$Abc" ) ) ;
538+ assert ! ( check( StringPattern :: substring_i( "$a" ) , "$Abc" ) ) ;
539+ assert ! ( !check( StringPattern :: glob( "a" ) . unwrap( ) , "A" ) ) ;
540+ assert ! ( check( StringPattern :: glob_i( "a" ) . unwrap( ) , "A" ) ) ;
541+ assert ! ( check( StringPattern :: regex( "^a{1,3}" ) . unwrap( ) , "abcde" ) ) ;
542+ assert ! ( !check( StringPattern :: regex( "^a{1,3}" ) . unwrap( ) , "Abcde" ) ) ;
543+ assert ! ( check( StringPattern :: regex_i( "^a{1,3}" ) . unwrap( ) , "Abcde" ) ) ;
544+ }
490545}
0 commit comments