line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Glob; |
2
|
2
|
|
|
2
|
|
28284
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
49
|
|
3
|
2
|
|
|
2
|
|
6
|
use Exporter; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
81
|
|
4
|
2
|
|
|
|
|
183
|
use vars qw/$VERSION @ISA @EXPORT_OK |
5
|
2
|
|
|
2
|
|
7
|
$strict_leading_dot $strict_wildcard_slash/; |
|
2
|
|
|
|
|
6
|
|
6
|
|
|
|
|
|
|
$VERSION = '0.11'; |
7
|
|
|
|
|
|
|
@ISA = 'Exporter'; |
8
|
|
|
|
|
|
|
@EXPORT_OK = qw( glob_to_regex glob_to_regex_string match_glob ); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$strict_leading_dot = 1; |
11
|
|
|
|
|
|
|
$strict_wildcard_slash = 1; |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
6
|
use constant debug => 0; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1027
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub glob_to_regex { |
16
|
48
|
|
|
48
|
1
|
4652
|
my $glob = shift; |
17
|
48
|
|
|
|
|
55
|
my $regex = glob_to_regex_string($glob); |
18
|
48
|
|
|
|
|
725
|
return qr/^$regex$/; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub glob_to_regex_string |
22
|
|
|
|
|
|
|
{ |
23
|
48
|
|
|
48
|
1
|
34
|
my $glob = shift; |
24
|
|
|
|
|
|
|
|
25
|
48
|
|
|
|
|
38
|
my $seperator = $Text::Glob::seperator; |
26
|
48
|
100
|
|
|
|
79
|
$seperator = "/" unless defined $seperator; |
27
|
48
|
|
|
|
|
48
|
$seperator = quotemeta($seperator); |
28
|
|
|
|
|
|
|
|
29
|
48
|
|
|
|
|
36
|
my ($regex, $in_curlies, $escaping); |
30
|
48
|
|
|
|
|
34
|
local $_; |
31
|
48
|
|
|
|
|
30
|
my $first_byte = 1; |
32
|
48
|
|
|
|
|
224
|
for ($glob =~ m/(.)/gs) { |
33
|
371
|
100
|
|
|
|
426
|
if ($first_byte) { |
34
|
51
|
100
|
|
|
|
65
|
if ($strict_leading_dot) { |
35
|
50
|
100
|
|
|
|
92
|
$regex .= '(?=[^\.])' unless $_ eq '.'; |
36
|
|
|
|
|
|
|
} |
37
|
51
|
|
|
|
|
38
|
$first_byte = 0; |
38
|
|
|
|
|
|
|
} |
39
|
371
|
100
|
|
|
|
398
|
if ($_ eq '/') { |
40
|
3
|
|
|
|
|
2
|
$first_byte = 1; |
41
|
|
|
|
|
|
|
} |
42
|
371
|
100
|
100
|
|
|
4682
|
if ($_ eq '.' || $_ eq '(' || $_ eq ')' || $_ eq '|' || |
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
43
|
|
|
|
|
|
|
$_ eq '+' || $_ eq '^' || $_ eq '$' || $_ eq '@' || $_ eq '%' ) { |
44
|
39
|
|
|
|
|
33
|
$regex .= "\\$_"; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
elsif ($_ eq '*') { |
47
|
21
|
100
|
|
|
|
43
|
$regex .= $escaping ? "\\*" : |
|
|
100
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$strict_wildcard_slash ? "(?:(?!$seperator).)*" : ".*"; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
elsif ($_ eq '?') { |
51
|
10
|
100
|
|
|
|
25
|
$regex .= $escaping ? "\\?" : |
|
|
50
|
|
|
|
|
|
52
|
|
|
|
|
|
|
$strict_wildcard_slash ? "(?!$seperator)." : "."; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
elsif ($_ eq '{') { |
55
|
13
|
100
|
|
|
|
16
|
$regex .= $escaping ? "\\{" : "("; |
56
|
13
|
100
|
|
|
|
19
|
++$in_curlies unless $escaping; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
elsif ($_ eq '}' && $in_curlies) { |
59
|
11
|
50
|
|
|
|
16
|
$regex .= $escaping ? "}" : ")"; |
60
|
11
|
50
|
|
|
|
14
|
--$in_curlies unless $escaping; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
elsif ($_ eq ',' && $in_curlies) { |
63
|
11
|
50
|
|
|
|
13
|
$regex .= $escaping ? "," : "|"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
elsif ($_ eq "\\") { |
66
|
4
|
50
|
|
|
|
7
|
if ($escaping) { |
67
|
0
|
|
|
|
|
0
|
$regex .= "\\\\"; |
68
|
0
|
|
|
|
|
0
|
$escaping = 0; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
4
|
|
|
|
|
3
|
$escaping = 1; |
72
|
|
|
|
|
|
|
} |
73
|
4
|
|
|
|
|
4
|
next; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
else { |
76
|
262
|
|
|
|
|
180
|
$regex .= $_; |
77
|
262
|
|
|
|
|
155
|
$escaping = 0; |
78
|
|
|
|
|
|
|
} |
79
|
367
|
|
|
|
|
274
|
$escaping = 0; |
80
|
|
|
|
|
|
|
} |
81
|
48
|
|
|
|
|
55
|
print "# $glob $regex\n" if debug; |
82
|
|
|
|
|
|
|
|
83
|
48
|
|
|
|
|
78
|
return $regex; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub match_glob { |
87
|
40
|
|
|
40
|
1
|
1340
|
print "# ", join(', ', map { "'$_'" } @_), "\n" if debug; |
88
|
40
|
|
|
|
|
38
|
my $glob = shift; |
89
|
40
|
|
|
|
|
43
|
my $regex = glob_to_regex $glob; |
90
|
40
|
|
|
|
|
33
|
local $_; |
91
|
40
|
|
|
|
|
57
|
grep { $_ =~ $regex } @_; |
|
40
|
|
|
|
|
294
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
__END__ |