]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
glob2re: return a precompiled, anchored case-insensitive regex
[ikiwiki.git] / IkiWiki.pm
index 08a3d7875da6b9941000b4531755fcb2a6f42d49..bb7d46ccfb63f8de96a4246b49de1dea67d9b8ea 100644 (file)
@@ -2388,7 +2388,7 @@ sub glob2re ($) {
        my $re=quotemeta(shift);
        $re=~s/\\\*/.*/g;
        $re=~s/\\\?/./g;
-       return $re;
+       return qr/^$re$/i;
 }
 
 package IkiWiki::FailReason;
@@ -2482,6 +2482,8 @@ sub derel ($$) {
        return $path;
 }
 
+my %glob_cache;
+
 sub match_glob ($$;@) {
        my $page=shift;
        my $glob=shift;
@@ -2489,8 +2491,13 @@ sub match_glob ($$;@) {
        
        $glob=derel($glob, $params{location});
 
-       my $regexp=IkiWiki::glob2re($glob);
-       if ($page=~/^$regexp$/i) {
+       # Instead of converting the glob to a regex every time,
+       # cache the compiled regex to save time.
+       if (!defined $glob_cache{$glob}) {
+               my $re = IkiWiki::glob2re($glob);
+               $glob_cache{$glob} = qr/^$re$/i;
+       }
+       if ($page=~ $glob_cache{$glob}) {
                if (! IkiWiki::isinternal($page) || $params{internal}) {
                        return IkiWiki::SuccessReason->new("$glob matches $page");
                }