line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::Query::To::Regexp; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
75137
|
use 5.010001; |
|
1
|
|
|
|
|
16
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
22
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use Exporter 'import'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
479
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw(query2re); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
11
|
|
|
|
|
|
|
our $DATE = '2022-11-02'; # DATE |
12
|
|
|
|
|
|
|
our $DIST = 'String-Query-To-Regexp'; # DIST |
13
|
|
|
|
|
|
|
our $VERSION = '0.003'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub query2re { |
16
|
11
|
100
|
|
11
|
1
|
14908
|
my $opts = ref($_[0]) eq 'HASH' ? {%{shift()}} : {}; |
|
7
|
|
|
|
|
25
|
|
17
|
11
|
|
100
|
|
|
45
|
my $bool = delete $opts->{bool} // 'and'; |
18
|
11
|
|
|
|
|
21
|
my $ci = delete $opts->{ci}; |
19
|
11
|
|
|
|
|
17
|
my $word = delete $opts->{word}; |
20
|
11
|
|
|
|
|
58
|
my $opt_re = delete $opts->{re}; |
21
|
11
|
100
|
|
|
|
51
|
die "query2re(): Unknown option(s): ". |
22
|
|
|
|
|
|
|
join(", ", sort keys %$opts) if keys %$opts; |
23
|
|
|
|
|
|
|
|
24
|
10
|
50
|
|
|
|
26
|
return qr// unless @_; |
25
|
10
|
|
|
|
|
15
|
my @re_parts; |
26
|
10
|
|
|
|
|
21
|
for my $query0 (@_) { |
27
|
17
|
|
|
|
|
96
|
my ($neg, $query) = $query0 =~ /\A(-?)(.*)/; |
28
|
|
|
|
|
|
|
|
29
|
17
|
100
|
|
|
|
36
|
if ($opt_re) { |
30
|
4
|
100
|
|
|
|
14
|
if (ref $query0 eq 'Regexp') { |
31
|
1
|
|
|
|
|
5
|
$query = $query0; |
32
|
|
|
|
|
|
|
} else { |
33
|
3
|
|
|
|
|
736
|
require Regexp::From::String; |
34
|
3
|
|
|
|
|
865
|
$query = Regexp::From::String::str_maybe_to_re($query); |
35
|
3
|
100
|
|
|
|
187
|
$query = quotemeta($query) unless ref $query eq 'Regexp'; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} else { |
38
|
13
|
|
|
|
|
23
|
$query = quotemeta $query; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
17
|
100
|
|
|
|
32
|
if ($word) { |
42
|
1
|
50
|
|
|
|
7
|
push @re_parts, $neg ? "(?!.*\\b$query\\b)" : "(?=.*\\b$query\\b)"; |
43
|
|
|
|
|
|
|
} else { |
44
|
16
|
100
|
|
|
|
54
|
push @re_parts, $neg ? "(?!.*$query)" : "(?=.*$query)"; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
10
|
100
|
|
|
|
35
|
my $re = $bool eq 'or' ? "(?:".join("|", @re_parts).")" : join("", @re_parts); |
48
|
10
|
100
|
|
|
|
234
|
return $ci ? qr/\A$re.*\z/is : qr/\A$re.*\z/s; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
1; |
52
|
|
|
|
|
|
|
# ABSTRACT: Convert query to regular expression |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
__END__ |