Sidef/Types/Regex/Regex.pm untested

filter: Statementstmt 0.0% 0 / 153 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 255 CC 29 SCAR 31.8
CC
29
Cov
0%
global_matches
72.0
new
42.0
union
42.0
CRAP 24.0
1 package Sidef::Types::Regex::Regex;
2  
3 use utf8;
4 use 5.016;
5  
6 use re 'eval'; # XXX: do we really want this?
7  
8 use parent qw(Sidef::Object::Object);
9 use Sidef::Types::Number::Number;
10  
11 use overload
12 q{""} => \&get_value,
13 q{bool} => \&get_value,
14 q{0+} => \&get_value;
15  
16 sub new {
17 my (undef, $regex, $mode) = @_;
18  
19 $regex = defined($regex) ? ref($regex) ? "$regex" : $regex : '';
20 $mode = defined($mode) ? ref($mode) ? "$mode" : $mode : '';
21  
22 my $global_mode = $mode =~ tr/g//d;
23 my $compiled_re = $mode eq '' ? qr{$regex} : qr{(?$mode:$regex)};
24  
25 bless {
26 regex => $compiled_re,
27 raw => $regex,
28 flags => $mode,
29 global => $global_mode,
30 pos => 0,
31 },
32 __PACKAGE__;
33 }
34  
35 *call = \&new;
36  
37 sub get_value { $_[0]{regex} }
38  
39 sub eq {
40 my ($x, $y) = @_;
41  
42 ($x->{regex} eq $y->{regex})
43 ? (Sidef::Types::Bool::Bool::TRUE)
44 : (Sidef::Types::Bool::Bool::FALSE);
45 }
46  
47 sub ne {
48 my ($x, $y) = @_;
49  
50 ($x->{regex} ne $y->{regex})
51 ? (Sidef::Types::Bool::Bool::TRUE)
52 : (Sidef::Types::Bool::Bool::FALSE);
53 }
54  
55 sub cmp {
56 my ($x, $y) = @_;
57  
58 my $cmp = $x->{regex} cmp $y->{regex};
59  
60 return Sidef::Types::Number::Number::MONE if ($cmp < 0);
61 return Sidef::Types::Number::Number::ONE if ($cmp > 0);
62 return Sidef::Types::Number::Number::ZERO;
63 }
64  
65 sub lt {
66 my ($x, $y) = @_;
67 $x->cmp($y)->lt(Sidef::Types::Number::Number::ZERO)
68 ? Sidef::Types::Bool::Bool::TRUE
69 : Sidef::Types::Bool::Bool::FALSE;
70 }
71  
72 sub le {
73 my ($x, $y) = @_;
74 $x->cmp($y)->le(Sidef::Types::Number::Number::ZERO)
75 ? Sidef::Types::Bool::Bool::TRUE
76 : Sidef::Types::Bool::Bool::FALSE;
77 }
78  
79 sub gt {
80 my ($x, $y) = @_;
81 $x->cmp($y)->gt(Sidef::Types::Number::Number::ZERO)
82 ? Sidef::Types::Bool::Bool::TRUE
83 : Sidef::Types::Bool::Bool::FALSE;
84 }
85  
86 sub ge {
87 my ($x, $y) = @_;
88 $x->cmp($y)->ge(Sidef::Types::Number::Number::ZERO)
89 ? Sidef::Types::Bool::Bool::TRUE
90 : Sidef::Types::Bool::Bool::FALSE;
91 }
92  
93 sub to_regex { $_[0] }
94 *to_re = \&to_regex;
95  
96 sub match {
97 my ($self, $object, $pos) = @_;
98  
99 # Return a new Match object
100 Sidef::Types::Regex::Match->new(string => "$object", regex => $self, pos => $pos);
101 }
102  
103 *run = \&match;
104  
105 sub is_match {
106 my ($self, $object, $pos) = @_;
107 $self->match($object, $pos)->matched;
108 }
109  
110 sub global_match {
111 my ($self, $obj, $pos) = @_;
112 local $self->{global} = 1;
113 local $self->{pos} = CORE::int($pos) if defined($pos);
114 $self->match($obj);
115 }
116  
117 *gmatch = \&global_match;
118  
119 sub global_matches {
120 my ($self, $obj, $third, $fourth) = @_;
121  
122 my ($pos, $block) = (0, undef);
123  
124 if (UNIVERSAL::isa($third, 'Sidef::Types::Number::Number')) {
125 $pos = $third;
126 }
127 elsif (UNIVERSAL::isa($third, 'Sidef::Types::Block::Block')) {
128 $block = $third;
129 }
130  
131 if (UNIVERSAL::isa($fourth, 'Sidef::Types::Number::Number')) {
132 $pos = $fourth;
133 }
134 elsif (UNIVERSAL::isa($fourth, 'Sidef::Types::Block::Block')) {
135 $block = $fourth;
136 }
137  
138 my @matches;
139 local $self->{global} = 1;
140 local $self->{pos} = CORE::int($pos);
141  
142 if (defined($block)) {
143 my ($cpos, $lpos) = ($self->{pos}, $self->{pos});
144 my $i = 0;
145  
146 while ((my $m = $self->match($obj))->{matched}) {
147 $lpos = $cpos;
148 last if ($lpos == ($cpos = $self->{pos}));
149 CORE::push(@matches, $block->run(Sidef::Types::Number::Number::_set_int($i++), $m));
150 }
151 }
152 else {
153 my ($cpos, $lpos) = ($self->{pos}, $self->{pos});
154 while ((my $m = $self->match($obj))->{matched}) {
155 $lpos = $cpos;
156 last if ($lpos == ($cpos = $self->{pos}));
157 CORE::push(@matches, $m);
158 }
159 }
160  
161 Sidef::Types::Array::Array->new(\@matches);
162 }
163  
164 *gmatches = \&global_matches;
165 *all_matches = \&global_matches;
166 *map_matches = \&global_matches;
167 *repeated_match = \&global_matches;
168  
169 sub add {
170 my ($self, $other) = @_;
171  
172 my $x = $self->{regex};
173 my $y = ref($other) eq __PACKAGE__ ? $other->{regex} : "\Q$other\E";
174  
175 __PACKAGE__->new("$x$y");
176 }
177  
178 *concat = \&add;
179  
180 sub union {
181 my ($self, $other, $extra_flags) = @_;
182  
183 my ($x, $x_flags, $x_global) = ($self->{raw}, $self->{flags}, $self->{global});
184 my ($y, $y_flags, $y_global) = (undef, '', 0);
185  
186 my %x_flags;
187 my %y_flags;
188  
189 if (ref($other) eq __PACKAGE__) {
190 $y = $other->{raw};
191 $y_flags = $other->{flags};
192 $y_global = $other->{global};
193 }
194 else {
195 $y = "\Q$other\E";
196 }
197  
198 $x_flags{$_}++ for split(//, $x_flags);
199 $y_flags{$_}++ for split(//, $y_flags);
200  
201 my %union = %x_flags;
202  
203 foreach my $k (keys %y_flags) {
204 if (exists $union{$k}) {
205  
206 my $c1 = $x_flags{$k};
207 my $c2 = $y_flags{$k};
208  
209 if ($c2 > $c1) {
210 $union{$k} = $c2;
211 }
212 }
213 else {
214 $union{$k} = $y_flags{$k};
215 }
216 }
217  
218 my $global = ($x_global || $y_global) ? 'g' : '';
219 my $flags = join('', map { $_ x $union{$_} } sort keys %union);
220  
221 $flags .= $extra_flags if defined($extra_flags);
222  
223 __PACKAGE__->new("$x|$y", join('', sort split(//, $flags . $global)));
224 }
225  
226 sub dump {
227 my ($self) = @_;
228  
229 my $str = $self->{raw};
230 my $flags = $self->{flags};
231  
232 Sidef::Types::String::String->new('/' . $str =~ s{(?<!\\)(?:\\\\)*\K/}{\\/}gr . '/' . $flags . ($self->{global} ? 'g' : ''));
233 }
234  
235 *to_s = \&dump;
236 *to_str = \&dump;
237  
238 {
239 no strict 'refs';
240 *{__PACKAGE__ . '::' . '=~'} = \&match;
241 *{__PACKAGE__ . '::' . '=='} = \&eq;
242 *{__PACKAGE__ . '::' . '!='} = \&ne;
243 *{__PACKAGE__ . '::' . '≠'} = \&ne;
244 *{__PACKAGE__ . '::' . '<=>'} = \&cmp;
245 *{__PACKAGE__ . '::' . '<'} = \&lt;
246 *{__PACKAGE__ . '::' . '<='} = \&le;
247 *{__PACKAGE__ . '::' . '≤'} = \&le;
248 *{__PACKAGE__ . '::' . '>'} = \&gt;
249 *{__PACKAGE__ . '::' . '≥'} = \&ge;
250 *{__PACKAGE__ . '::' . '>='} = \&ge;
251 *{__PACKAGE__ . '::' . '+'} = \&concat;
252 *{__PACKAGE__ . '::' . '|'} = \&union;
253 }
254  
255 1