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