File Coverage

blib/lib/Code/TidyAll/Util/Zglob.pm
Criterion Covered Total %
statement 41 52 78.8
branch 22 50 44.0
condition 12 30 40.0
subroutine 7 7 100.0
pod 0 3 0.0
total 82 142 57.7


line stmt bran cond sub pod time code
1             # This is a copy of Text::Glob, modified to support "**/"
2             #
3             package Code::TidyAll::Util::Zglob;
4              
5 35     35   224 use strict;
  35         76  
  35         1463  
6 35     35   192 use warnings;
  35         65  
  35         2671  
7              
8             our $VERSION = '0.85';
9              
10 35     35   236 use Exporter qw(import);
  35         69  
  35         3316  
11              
12             our @EXPORT_OK = qw( zglobs_to_regex zglob_to_regex );
13              
14             our $strict_leading_dot = 1;
15             our $strict_wildcard_slash = 1;
16              
17 35     35   242 use constant debug => 0;
  35         78  
  35         27590  
18              
19             sub zglobs_to_regex {
20 186     186 0 10433 my @globs = @_;
21             return @globs
22 186 100       2728 ? do {
23 101         265 my $re = join( '|', map { "(?:" . zglob_to_regex($_) . ")" } @globs );
  101         312  
24 101         5749 qr/$re/;
25             }
26             : qr/(?!)/;
27             }
28              
29             sub zglob_to_regex {
30 104     104 0 687496 my $glob = shift;
31 104         346 my $regex = zglob_to_regex_string($glob);
32 104         4744 return qr/^$regex$/;
33             }
34              
35             sub zglob_to_regex_string {
36 104     104 0 216 my $glob = shift;
37 104         294 my ( $regex, $in_curlies, $escaping );
38 104         217 local $_;
39 104         187 my $first_byte = 1;
40 104         493 $glob =~ s/\*\*\//\cZ/g; # convert **/ to single character
41 104         1362 for ( $glob =~ m/(.)/gs ) {
42 814 100       1703 if ($first_byte) {
43 141 50       411 if ($strict_leading_dot) {
44 141 50       655 $regex .= '(?=[^\.])' unless $_ eq '.';
45             }
46 141         322 $first_byte = 0;
47             }
48 814 100       1679 if ( $_ eq '/' ) {
49 37         426 $first_byte = 1;
50             }
51 814 100 66     12103 if ( $_ eq '.'
    100 66        
    100 33        
    50 33        
    50 33        
    50 33        
    50 33        
    50 33        
      33        
      33        
52             || $_ eq '('
53             || $_ eq ')'
54             || $_ eq '|'
55             || $_ eq '+'
56             || $_ eq '^'
57             || $_ eq '$'
58             || $_ eq '@'
59             || $_ eq '%' ) {
60 33         75 $regex .= "\\$_";
61             }
62             elsif ( $_ eq "\cZ" ) { # handle **/ - if escaping, only escape first *
63 43 0       167 $regex
    50          
64             .= $escaping
65             ? ( "\\*" . ( $strict_wildcard_slash ? "[^/]*" : ".*" ) . "/" )
66             : ".*";
67             }
68             elsif ( $_ eq '*' ) {
69 104 50       408 $regex
    50          
70             .= $escaping ? "\\*"
71             : $strict_wildcard_slash ? "[^/]*"
72             : ".*";
73             }
74             elsif ( $_ eq '?' ) {
75 0 0       0 $regex
    0          
76             .= $escaping ? "\\?"
77             : $strict_wildcard_slash ? "[^/]"
78             : ".";
79             }
80             elsif ( $_ eq '{' ) {
81 0 0       0 $regex .= $escaping ? "\\{" : "(";
82 0 0       0 ++$in_curlies unless $escaping;
83             }
84             elsif ( $_ eq '}' && $in_curlies ) {
85 0 0       0 $regex .= $escaping ? "}" : ")";
86 0 0       0 --$in_curlies unless $escaping;
87             }
88             elsif ( $_ eq ',' && $in_curlies ) {
89 0 0       0 $regex .= $escaping ? "," : "|";
90             }
91             elsif ( $_ eq "\\" ) {
92 0 0       0 if ($escaping) {
93 0         0 $regex .= "\\\\";
94 0         0 $escaping = 0;
95             }
96             else {
97 0         0 $escaping = 1;
98             }
99 0         0 next;
100             }
101             else {
102 634         1148 $regex .= $_;
103 634         935 $escaping = 0;
104             }
105 814         1350 $escaping = 0;
106             }
107 104         283 print "# $glob $regex\n" if debug;
108              
109 104         444 return $regex;
110             }
111              
112             1;
113              
114             # ABSTRACT: Test::Glob hacked up to support "**/*"
115              
116             __END__
117              
118             =pod
119              
120             =encoding UTF-8
121              
122             =head1 NAME
123              
124             Code::TidyAll::Util::Zglob - Test::Glob hacked up to support "**/*"
125              
126             =head1 VERSION
127              
128             version 0.85
129              
130             =head1 SUPPORT
131              
132             Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
133              
134             =head1 SOURCE
135              
136             The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
137              
138             =head1 AUTHORS
139              
140             =over 4
141              
142             =item *
143              
144             Jonathan Swartz <swartz@pobox.com>
145              
146             =item *
147              
148             Dave Rolsky <autarch@urth.org>
149              
150             =back
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is copyright (c) 2011 - 2025 by Jonathan Swartz.
155              
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158              
159             The full text of the license can be found in the
160             F<LICENSE> file included with this distribution.
161              
162             =cut