Sidef/Types/Regex/Match.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 184 CC 6 SCAR 21.0
CC
6
Cov
0%
new
30.0
matched
6.0
get_value
2.0
CRAP 8.2
1 package Sidef::Types::Regex::Match;
2  
3 use utf8;
4 use 5.016;
5  
6 use overload
7 q{bool} => \&get_value,
8 q{""} => sub { CORE::join(' ', @{$_[0]->{captures}}) },
9 q{@{}} => sub {
10 $_[0]->{_cached_cap} //= [map { Sidef::Types::String::String->new($_) } @{$_[0]->{captures}}];
11 };
12  
13 use parent qw(
14 Sidef::Object::Object
15 );
16  
17 use Sidef::Types::Bool::Bool;
18  
19 sub new {
20 my (undef, %hash) = @_;
21  
22 my @captures;
23 if ($hash{regex}{global}) {
24  
25 pos($hash{string}) = CORE::int($hash{regex}{pos});
26 my $match = $hash{string} =~ /$hash{regex}{regex}/g;
27  
28 if ($match) {
29 $hash{regex}{pos} = pos($hash{string});
30  
31 foreach my $i (1 .. $#{+}) {
32 push @captures, substr($hash{string}, $-[$i], $+[$i] - $-[$i]);
33 }
34  
35 $hash{matched} = 1;
36 }
37 else {
38 $hash{regex}{pos} = 0;
39 $hash{matched} = 0;
40 }
41  
42 foreach my $key (keys %+) {
43 $hash{named_captures}{$key} = $+{$key};
44 }
45 }
46 else {
47 $hash{pos} = CORE::int($hash{pos} // 0);
48 $hash{pos} = 0 if ($hash{pos} < 0);
49 @captures = substr($hash{string}, $hash{pos}) =~ $hash{regex}{regex};
50  
51 $hash{matched} = (@captures != 0);
52 $hash{match_pos} = $hash{matched} ? [$-[0] + $hash{pos}, $+[0] + $hash{pos}] : [];
53  
54 foreach my $key (keys %+) {
55 $hash{named_captures}{$key} = $+{$key};
56 }
57 }
58  
59 $hash{captures} = \@captures;
60  
61 bless \%hash, __PACKAGE__;
62 }
63  
64 sub get_value {
65 $_[0]->{matched};
66 }
67  
68 sub matched {
69 $_[0]->{matched}
70 ? (Sidef::Types::Bool::Bool::TRUE)
71 : (Sidef::Types::Bool::Bool::FALSE);
72 }
73  
74 *to_bool = \&matched;
75 *is_successful = \&matched;
76  
77 sub pos {
78 my ($self) = @_;
79 Sidef::Types::Array::Array->new([@{$self->{_cached_pos} //= [map { Sidef::Types::Number::Number::_set_int($_) } @{$self->{match_pos}}]}]);
80 }
81  
82 *match_pos = \&pos;
83  
84 sub pre_match {
85 my ($self) = @_;
86 my $mp = $self->{match_pos} or return undef;
87 @$mp or return undef;
88 Sidef::Types::String::String->new(CORE::substr($self->{string}, 0, $mp->[0]));
89 }
90  
91 sub post_match {
92 my ($self) = @_;
93 my $mp = $self->{match_pos} or return undef;
94 @$mp or return undef;
95 Sidef::Types::String::String->new(CORE::substr($self->{string}, $mp->[1]));
96 }
97  
98 sub captures {
99 my ($self) = @_;
100 Sidef::Types::Array::Array->new([@{$self->{_cached_cap} //= [map { Sidef::Types::String::String->new($_) } @{$self->{captures}}]}]);
101 }
102  
103 *cap = \&captures;
104 *caps = \&captures;
105 *to_a = \&captures;
106 *to_array = \&captures;
107 *groups = \&captures;
108  
109 sub named_captures {
110 my ($self) = @_;
111  
112 my $hash = Sidef::Types::Hash::Hash->new;
113 while (my ($key, $value) = each %{$self->{named_captures}}) {
114 $hash->{$key} = Sidef::Types::String::String->new($value);
115 }
116 $hash;
117 }
118  
119 *ncap = \&named_captures;
120 *ncaps = \&named_captures;
121 *named_groups = \&named_captures;
122  
123 sub join {
124 my ($self, $sep) = @_;
125 Sidef::Types::String::String->new(CORE::join("$sep", @{$self->{captures}}));
126 }
127  
128 sub string {
129 my ($self) = @_;
130 Sidef::Types::String::String->new($self->{string});
131 }
132  
133 sub regex {
134 my ($self) = @_;
135 $self->{regex};
136 }
137  
138 sub to_str {
139 my ($self) = @_;
140 Sidef::Types::String::String->new("$self");
141 }
142  
143 *to_s = \&to_str;
144  
145 sub dump {
146 my ($self) = @_;
147 my $re = $self->{regex}->dump;
148 my $str = Sidef::Types::String::String->new($self->{string})->dump;
149 Sidef::Types::String::String->new("($str =~ $re)");
150 }
151  
152 {
153 no strict 'refs';
154 *{__PACKAGE__ . '::' . '...'} = sub {
155 @{$_[0]->{_cached_cap} //= [map { Sidef::Types::String::String->new($_) } @{$_[0]->{captures}}]};
156 };
157 }
158  
159 1