Data/Sah/Compiler/perl/TH/cistr.pm

filter: Statementstmt 82.0% 73 / 89 Branchbran 65.6% 21 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 6 totaltotal 74.6% 106 / 142 CC 17 SCAR 15.4
CC
17
Cov
78%
superclause_sortable
9.3
__ANON__
6.0
superclause_comparable
4.1
CRAP 4.7
« Data/Sah/Util/Role.pm ↑ Summary Data/Sah/Compiler/human/TH/Sortable.pm »
1 package Data::Sah::Compiler::perl::TH::cistr;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::Any '$log';
7  
8 3 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 extends 'Data::Sah::Compiler::perl::TH::str';
12 3 with 'Data::Sah::Type::cistr';
13  
14 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
15 3 our $DATE = '2024-02-16'; # DATE
16 3 our $DIST = 'Data-Sah'; # DIST
17 3 our $VERSION = '0.917'; # VERSION
18  
19 521 ▶sub before_all_clauses {
Subroutine before_all_clauses: called CC=1 SCAR=0.0
Pod undocumented
20 521 my ($self, $cd) = @_;
21 521 my $c = $self->compiler;
22 521 my $dt = $cd->{data_term};
23  
24 # XXX only do this when there are clauses
25  
26 # convert to lowercase so we don't lc() the data repeatedly
27 521 $self->set_tmp_data_term($cd, "lc($dt)");
28 }
29  
30 517 ▶sub after_all_clauses {
Subroutine after_all_clauses: called CC=1 SCAR=0.0
Pod undocumented
31 517 my ($self, $cd) = @_;
32 517 my $c = $self->compiler;
33 517 my $dt = $cd->{data_term};
34  
35 517 $self->restore_data_term($cd);
36 }
37  
38 289 ▶sub superclause_comparable {
Subroutine superclause_comparable: called CC=4 SCAR=14.2
Pod undocumented
39 289 my ($self, $which, $cd) = @_;
40 289 my $c = $self->compiler;
41 289 my $ct = $cd->{cl_term};
42 289 my $dt = $cd->{data_term};
43  
44 289 ▶ if ($which eq 'is') {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'is') { } 150 139 289
elsif ($which eq 'in') { } 139 0 139
45 150 $c->add_ccl($cd, "$dt eq lc($ct)");
46 } elsif ($which eq 'in') {
47 139 ▶ if ($dt =~ /\$_\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt =~ /\$_\b/) { } 0 139 139
48 0 $c->add_ccl($cd, "do { my \$_sahv_dt = $dt; grep { lc(\$_) eq \$_sahv_dt } \@{ $ct } }");
49 } else {
50 139 $c->add_ccl($cd, "grep { lc(\$_) eq $dt } \@{ $ct }");
51 }
52 }
53 }
54  
55 192 ▶sub superclause_sortable {
Subroutine superclause_sortable: called CC=9 SCAR=22.3
Pod undocumented
56 192 my ($self, $which, $cd) = @_;
57 192 my $c = $self->compiler;
58 192 my $cv = $cd->{cl_value};
59 192 my $ct = $cd->{cl_term};
60 192 my $dt = $cd->{data_term};
61  
62 192 ▶ if ($which eq 'min') {
Branch: 6 branches
BranchTrueFalseTotal
if ($which eq 'min') { } 9 183 192
elsif ($which eq 'xmin') { } 9 174 183
elsif ($which eq 'max') { } 9 165 174
elsif ($which eq 'xmax') { } 9 156 165
elsif ($which eq 'between') { } 144 12 156
elsif ($which eq 'xbetween') { } 12 0 12
63 9 $c->add_ccl($cd, "$dt ge lc($ct)");
64 } elsif ($which eq 'xmin') {
65 9 $c->add_ccl($cd, "$dt gt lc($ct)");
66 } elsif ($which eq 'max') {
67 9 $c->add_ccl($cd, "$dt le lc($ct)");
68 } elsif ($which eq 'xmax') {
69 9 $c->add_ccl($cd, "$dt lt lc($ct)");
70 } elsif ($which eq 'between') {
71 144 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 144 144
72 0 $c->add_ccl($cd, "$dt ge lc($ct\->[0]) && ".
73 "$dt le lc($ct\->[1])");
74 } else {
75 # simplify code
76 144 $c->add_ccl($cd, "$dt ge ".$c->literal(lc $cv->[0]).
77 " && $dt le ".$c->literal(lc $cv->[1]));
78 }
79 } elsif ($which eq 'xbetween') {
80 12 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 12 12
81 0 $c->add_ccl($cd, "$dt gt lc($ct\->[0]) && ".
82 "$dt lt lc($ct\->[1])");
83 } else {
84 # simplify code
85 12 $c->add_ccl($cd, "$dt gt ".$c->literal(lc $cv->[0]).
86 " && $dt lt ".$c->literal(lc $cv->[1]));
87 }
88 }
89 }
90  
91 73 ▶sub superclause_has_elems {
Subroutine superclause_has_elems: called CC=2 SCAR=6.9
Pod undocumented
92 73 my ($self_th, $which, $cd) = @_;
93 73 my $c = $self_th->compiler;
94 73 my $cv = $cd->{cl_value};
95 73 my $ct = $cd->{cl_term};
96 73 my $dt = $cd->{data_term};
97  
98 73 ▶ if ($which eq 'has') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq 'has') { } 18 55 73
99 18 $c->add_ccl($cd, "index($dt, lc($ct)) > -1");
100 } else {
101 55 $self_th->SUPER::superclause_has_elems($which, $cd);
102 }
103 }
104  
105 # turn "(?-xism:blah)" to "(?i-xsm:blah)"
106 30 ▶sub __change_re_str_switch {
Subroutine __change_re_str_switch: called CC=2 SCAR=12.9
107 30 my $re = shift;
108  
109 30 ▶ if ($^V ge v5.14.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^V ge v5.14.0) { } 30 0 30
110 0 ▶ state $sub = sub { my $s = shift; $s =~ /i/ ? $s : "i$s" };
Subroutine __ANON__: not called CC=2 SCAR=14.0
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /i/ ? : 0 0 0
111 0 $re =~ s/\A\(\?\^(\w*):/"(?".$sub->($1).":"/e;
112 } else {
113 0 ▶ state $subl = sub { my $s = shift; $s =~ /i/ ? $s : "i$s" };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /i/ ? : 0 0 0
114 0 ▶ state $subr = sub { my $s = shift; $s =~ s/i//; $s };
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 $re =~ s/\A\(\?(\w*)-(\w*):/"(?".$subl->($1)."-".$subr->($2).":"/e;
116 }
117 30 return $re;
118 }
119  
120 31 ▶sub clause_match {
Subroutine clause_match: called CC=2 SCAR=7.0
Pod undocumented
121 31 my ($self, $cd) = @_;
122 31 my $c = $self->compiler;
123 31 my $cv = $cd->{cl_value};
124 31 my $ct = $cd->{cl_term};
125 31 my $dt = $cd->{data_term};
126  
127 31 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 31 31
128 0 $c->add_ccl($cd, join(
129 "",
130 "ref($ct) eq 'Regexp' ? $dt =~ qr/$ct/i : ",
131 "do { my \$_sahv_re = $ct; eval { \$_sahv_re = /\$_sahv_re/i; 1 } && ",
132 "$dt =~ \$_sahv_re }",
133 ));
134 } else {
135 # simplify code and we can check regex at compile time
136 31 my $re = $c->_str2reliteral($cd, $cv);
137 30 $re = __change_re_str_switch($re);
138 30 $c->add_ccl($cd, "$dt =~ /$re/i");
139 }
140 }
141  
142 3 1;
143 # ABSTRACT: perl's type handler for type "cistr"
144  
145 __END__
« Data/Sah/Util/Role.pm ↑ Summary Data/Sah/Compiler/human/TH/Sortable.pm »