line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Glob; |
2
|
1
|
|
|
1
|
|
69144
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
52
|
|
3
|
1
|
|
|
1
|
|
7
|
use Exporter; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
67
|
|
4
|
1
|
|
|
|
|
187
|
use vars qw/$VERSION @ISA @EXPORT_OK |
5
|
1
|
|
|
1
|
|
7
|
$strict_leading_dot $strict_wildcard_slash/; |
|
1
|
|
|
|
|
8
|
|
6
|
|
|
|
|
|
|
$VERSION = '0.09'; |
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
|
1
|
|
|
1
|
|
6
|
use constant debug => 0; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
720
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub glob_to_regex { |
16
|
41
|
|
|
41
|
1
|
65
|
my $glob = shift; |
17
|
41
|
|
|
|
|
76
|
my $regex = glob_to_regex_string($glob); |
18
|
41
|
|
|
|
|
1076
|
return qr/^$regex$/; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub glob_to_regex_string |
22
|
|
|
|
|
|
|
{ |
23
|
41
|
|
|
41
|
1
|
42
|
my $glob = shift; |
24
|
41
|
|
|
|
|
50
|
my ($regex, $in_curlies, $escaping); |
25
|
41
|
|
|
|
|
45
|
local $_; |
26
|
41
|
|
|
|
|
548
|
my $first_byte = 1; |
27
|
41
|
|
|
|
|
324
|
for ($glob =~ m/(.)/gs) { |
28
|
320
|
100
|
|
|
|
576
|
if ($first_byte) { |
29
|
44
|
100
|
|
|
|
75
|
if ($strict_leading_dot) { |
30
|
43
|
100
|
|
|
|
115
|
$regex .= '(?=[^\.])' unless $_ eq '.'; |
31
|
|
|
|
|
|
|
} |
32
|
44
|
|
|
|
|
52
|
$first_byte = 0; |
33
|
|
|
|
|
|
|
} |
34
|
320
|
100
|
|
|
|
527
|
if ($_ eq '/') { |
35
|
3
|
|
|
|
|
5
|
$first_byte = 1; |
36
|
|
|
|
|
|
|
} |
37
|
320
|
100
|
100
|
|
|
6002
|
if ($_ eq '.' || $_ eq '(' || $_ eq ')' || $_ eq '|' || |
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
38
|
|
|
|
|
|
|
$_ eq '+' || $_ eq '^' || $_ eq '$' || $_ eq '@' || $_ eq '%' ) { |
39
|
36
|
|
|
|
|
49
|
$regex .= "\\$_"; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
elsif ($_ eq '*') { |
42
|
18
|
100
|
|
|
|
41
|
$regex .= $escaping ? "\\*" : |
|
|
100
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$strict_wildcard_slash ? "[^/]*" : ".*"; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
elsif ($_ eq '?') { |
46
|
5
|
100
|
|
|
|
16
|
$regex .= $escaping ? "\\?" : |
|
|
50
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$strict_wildcard_slash ? "[^/]" : "."; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
elsif ($_ eq '{') { |
50
|
13
|
100
|
|
|
|
23
|
$regex .= $escaping ? "\\{" : "("; |
51
|
13
|
100
|
|
|
|
30
|
++$in_curlies unless $escaping; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ($_ eq '}' && $in_curlies) { |
54
|
11
|
50
|
|
|
|
19
|
$regex .= $escaping ? "}" : ")"; |
55
|
11
|
50
|
|
|
|
24
|
--$in_curlies unless $escaping; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ($_ eq ',' && $in_curlies) { |
58
|
11
|
50
|
|
|
|
22
|
$regex .= $escaping ? "," : "|"; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
elsif ($_ eq "\\") { |
61
|
4
|
50
|
|
|
|
10
|
if ($escaping) { |
62
|
0
|
|
|
|
|
0
|
$regex .= "\\\\"; |
63
|
0
|
|
|
|
|
0
|
$escaping = 0; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
4
|
|
|
|
|
3
|
$escaping = 1; |
67
|
|
|
|
|
|
|
} |
68
|
4
|
|
|
|
|
6
|
next; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
222
|
|
|
|
|
287
|
$regex .= $_; |
72
|
222
|
|
|
|
|
269
|
$escaping = 0; |
73
|
|
|
|
|
|
|
} |
74
|
316
|
|
|
|
|
441
|
$escaping = 0; |
75
|
|
|
|
|
|
|
} |
76
|
41
|
|
|
|
|
78
|
print "# $glob $regex\n" if debug; |
77
|
|
|
|
|
|
|
|
78
|
41
|
|
|
|
|
107
|
return $regex; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub match_glob { |
82
|
40
|
|
|
40
|
1
|
4229
|
print "# ", join(', ', map { "'$_'" } @_), "\n" if debug; |
83
|
40
|
|
|
|
|
56
|
my $glob = shift; |
84
|
40
|
|
|
|
|
82
|
my $regex = glob_to_regex $glob; |
85
|
40
|
|
|
|
|
68
|
local $_; |
86
|
40
|
|
|
|
|
77
|
grep { $_ =~ $regex } @_; |
|
40
|
|
|
|
|
754
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |
90
|
|
|
|
|
|
|
__END__ |