blib/lib/Text/Nunjucks/Runtime/Regex.pm

filter: Statementstmt 63.5% 164 / 258 Branchbran 57.2% 87 / 152 Conditioncond 51.2% 20 / 39 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 65.2% 15 / 23 Podpod 0.0% 0 / 8 totaltotal 59.2% 296 / 500 CC 111 SCAR 33.9
CC
111
Cov
60%
_capture_groups
74.3
_capture_groups
74.3
exec
23.2
CRAP 29.6
« blib/lib/Text/Nunjucks/Parser/Parser.pm ↑ Summary blib/lib/Text/Nunjucks/Runtime.pm »
1 package Text::Nunjucks::Runtime::Regex;
2 # ABSTRACT: A template regex literal, with the members of a JS RegExp (ADR 0013)
3  
4 23 our $VERSION = '0.001';
5  
6 23 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use overload
8 23 ▶ '""' => \&_stringify,
Subroutine BEGIN: called CC=1 SCAR=0.2
9 0 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
10 23 fallback => 1;
11 23 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Regex -- template regex literal (ADR 0013), a port addition to runtime.js at
16 # its ADR-designated home beside Text::Nunjucks::Runtime (whose ->Regex names
17 # this class).
18 #
19 # A template `/.../flags` becomes a blessed Regex object with the members
20 # templates read on a JS RegExp (source, flags, global) and call (test, exec).
21 # The attributes are the JS ones verbatim (source, flags) -- per the k43
22 # discipline, `flags` is a Moo attribute (auto-generated accessor), NEVER a
23 # hand-rolled `sub flags` (the previous half-attempt collided with Moo's reader
24 # and made the whole class unloadable; the rewrite is fresh, not patched).
25  
26 23 has source => ( is => 'ro', required => 1 );
27 23 has flags => ( is => 'ro', required => 1 );
28 23 has _qr => ( is => 'ro', required => 1 );
29 # _qr_anchored is the same pattern with a leading \G so it can only match at
30 # the string's current pos(). ADR 0013 requires JS /y (sticky) to behave that
31 # way: match anchored at lastIndex, advance lastIndex past the match on
32 # success, reset to 0 on failure. Built unconditionally; test/exec pick which
33 # one to use from the g/y flags.
34 23 has _qr_anchored => ( is => 'ro', required => 1 );
35 # _captures_count is the number of native (numbered or named) capture groups
36 # in the JS regex body. Cached at construction time (k48) so exec can detect
37 # the no-groups case -- Perl 5.40's list-context `=~` returns the boolean
38 # match result instead of an empty list when the qr// has no groups, which
39 # the JS RegExp.prototype.exec contract never produces. Zero means: drop the
40 # boolean before assembling the [full, ...captures] array.
41 23 has _captures_count => ( is => 'ro', required => 1 );
42 # _group_names is every group name of the JS regex body in source order, each
43 # once (a duplicate name keeps its first place), cached at construction like
44 # _captures_count. Perl's %+ / %- carry no order, but JS builds a match's
45 # groups object in group order (oracle: 'ab' with r/(?<z>a)(?<b>b)/ iterates
46 # z, then b), so the engine-built groups object is filled from this list.
47 23 has _group_names => ( is => 'ro', required => 1 );
48 # lastIndex is the JS RegExp's mutable match position. JS resets it on every
49 # successful match (g/y) and on every failed match (g/y); for non-g/non-y
50 # regexes it is left alone. The current value is template-visible
51 # ({{ r.lastIndex }}).
52 23 has lastIndex => ( is => 'rw', default => 0 );
53  
54 # JS RegExp.global: true iff the g flag was set. Templates read it as a
55 # property ({{ r.global }}); the g flag is also tracked separately on the
56 # object so test/exec honour it (a Perl qr// is single-match by default).
57 # Returns a Perl boolean (!!1 / !!0) so Runtime::_js_to_string routes it
58 # through the is_bool branch and renders "true" / "false", matching
59 # RegExp.prototype.global's JS boolean.
60 4 ▶sub global {
Subroutine global: called CC=2 SCAR=9.2
Pod undocumented
61 0 ▶ my ($self) = @_;
Subroutine global: not called CC=2 SCAR=9.2
Pod undocumented
62 0 ▶ return ( index( $self->flags, 'g' ) >= 0 ) ? !!1 : !!0;
Branch: 2 branches
BranchTrueFalseTotal
(index($self->flags, 'g') != -1) ? : 2 2 4
(index($self->flags, 'g') != -1) ? : 0 0 0
63 }
64  
65 # True iff the y (sticky) flag is set. Tracked as a separate predicate so
66 # test/exec don't re-scan flags on every call.
67 278 ▶sub _is_sticky {
Subroutine _is_sticky: called CC=2 SCAR=9.2
68 0 ▶ my ($self) = @_;
Subroutine _is_sticky: not called CC=2 SCAR=9.2
69 0 ▶ return index( $self->flags, 'y' ) >= 0 ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
index($self->flags, 'y') != -1 ? : 38 240 278
index($self->flags, 'y') != -1 ? : 0 0 0
70 }
71  
72 # RegExp.prototype.test -- ADR 0013 g/y semantics:
73 # * y (sticky): match anchored at lastIndex via the \G-prefixed qr//. On
74 # success, advance lastIndex by the match length. On failure, reset to 0.
75 # * g (global, not y): match starting from lastIndex (set pos($str) before
76 # matching). On success, advance lastIndex by the match length. On
77 # failure, reset to 0.
78 # * neither: stateless match; lastIndex is left alone (JS does not touch
79 # it for non-g/non-y regexes -- oracle-checked).
80 # Returns a Perl boolean (!!1/!!0) so _js_to_string renders "true"/"false".
81 #
82 # Perl implementation note: pos() only advances on m//g matches (perldoc
83 # perlvar). The qr// we hold has no /g, so a plain `$str =~ $self->_qr`
84 # updates neither pos nor lastIndex. The stateful probe is therefore
85 # written as `$str =~ /$qr/g`, which interpolates the qr object and
86 # applies /g, advancing pos past the found match. The original k46 v1
87 # mistakenly used `=~ $self->_qr` and silently never advanced lastIndex.
88 #
89 # CAVEAT: $self overloads "" to render /source/flags (RegExp.prototype.
90 # toString). Interpolation of the qr through a method call (e.g.
91 # /$self->_qr/g) forces stringification of the qr and Perl sees a junk
92 # pattern like `/x/y->_qr`. Cache the qr in a local first.
93 28 ▶sub test {
Subroutine test: called CC=7 SCAR=26.1
Pod undocumented
94 0 ▶ my ( $self, $str ) = @_;
Subroutine test: not called CC=7 SCAR=26.1
Pod undocumented
95 0 ▶ $str = defined($str) ? "$str" : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $str ? : 28 0 28
defined $str ? : 0 0 0
96 0 my $sticky = $self->_is_sticky;
97 0 my $global = index( $self->flags, 'g' ) >= 0;
98  
99 0 ▶ if ($sticky) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sticky) 14 14 28
if ($sticky) 0 0 0
100 0 my $qr_a = $self->_qr_anchored;
101 0 my $start = $self->lastIndex;
102 0 pos($str) = $start;
103 0 ▶ if ( $str =~ /$qr_a/g ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /$qr_a/g) 8 6 14
if ($str =~ /$qr_a/g) 0 0 0
104 0 $self->lastIndex( pos($str) );
105 0 return !!1;
106 }
107 0 $self->lastIndex(0);
108 0 return !!0;
109 }
110 0 ▶ if ($global) {
Branch: 2 branches
BranchTrueFalseTotal
if ($global) 8 6 14
if ($global) 0 0 0
111 0 my $qr = $self->_qr;
112 0 my $start = $self->lastIndex;
113 0 pos($str) = $start;
114 0 ▶ if ( $str =~ /$qr/g ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /$qr/g) 5 3 8
if ($str =~ /$qr/g) 0 0 0
115 0 $self->lastIndex( pos($str) );
116 0 return !!1;
117 }
118 0 $self->lastIndex(0);
119 0 return !!0;
120 }
121 0 ▶ return ( $str =~ $self->_qr ) ? !!1 : !!0;
Branch: 2 branches
BranchTrueFalseTotal
$str =~ $self->_qr ? : 3 3 6
$str =~ $self->_qr ? : 0 0 0
122 }
123  
124 # RegExp.prototype.exec -- same g/y semantics as test. Returns an Array of
125 # [full match, cap1, cap2, ...] (JS renders as 'a,b,c' via toString; that
126 # is what `{{ r/(.)(.).exec('ab') }}` wants). On no-match returns null
127 # (undef, which the engine renders as empty). The Perl qr// match in list
128 # context returns only the capture groups; we prepend $& (the full match)
129 # so the array matches RegExp.prototype.exec's shape.
130 #
131 # k48: when the pattern has no capture groups, Perl 5.40's list-context
132 # match returns the boolean match result (builtin::true / builtin::false)
133 # instead of an empty list. JS never does that. _captures_count == 0
134 # means drop the boolean from @captures before assembling the result.
135 #
136 # Implementation note: pos() is only honoured as a START position by
137 # m//g matches (perldoc perlop). So a plain list-context `$str =~ $qr`
138 # silently ignores pos() and always re-finds the first match from offset
139 # 0, breaking global/sticky iteration. Compounded: list-context
140 # `$str =~ /$qr/g` iterates ALL matches and returns ALL captures (perldoc
141 # perlop), not the captures from one anchored match. Two failure modes
142 # need two different probes:
143 #
144 # * Sticky (y): the prebuilt _qr_anchored pattern starts with \G, which
145 # only matches at pos(). A non-/g list-context match against that
146 # pattern is therefore pos-anchored; on success it returns the captures
147 # from the one match starting at pos, with no skip-ahead. New lastIndex
148 # is start + length of full match.
149 # * Global (g, no y): we cannot use a \G-anchored pattern (global skips
150 # forward to the next match, sticky does not). Run the qr in scalar
151 # /g context (which DOES honour pos as start AND advances it past the
152 # found match, skipping non-matches); then substring the matched
153 # region and run a fresh non-/g list-context match on it to grab the
154 # captures. The slice is short (one match's width) so the cost is
155 # bounded.
156 #
157 # CAVEAT (same as test above): Regex overloads "" to render /source/flags,
158 # so `$self->_qr` interpolates as a string. Cache in a local first.
159 52 ▶sub exec {
Subroutine exec: called CC=10 SCAR=31.5
Pod undocumented
160 0 ▶ my ( $self, $str ) = @_;
Subroutine exec: not called CC=10 SCAR=31.5
Pod undocumented
161 0 ▶ $str = defined($str) ? "$str" : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $str ? : 52 0 52
defined $str ? : 0 0 0
162 0 my $sticky = $self->_is_sticky;
163 0 my $global = index( $self->flags, 'g' ) >= 0;
164 0 my $no_caps = $self->_captures_count == 0;
165  
166 0 ▶ if ($sticky) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sticky) 6 46 52
if ($sticky) 0 0 0
167 0 my $qr_a = $self->_qr_anchored;
168 0 my $start = $self->lastIndex;
169 0 pos($str) = $start;
170 0 my @captures = $str =~ $qr_a; # non-/g; \G anchors at pos
171 0 ▶ if (@captures) {
Branch: 2 branches
BranchTrueFalseTotal
if (@captures) 2 4 6
if (@captures) 0 0 0
172 0 ▶ @captures = () if $no_caps; # k48: drop the bool for no-groups patterns
Branch: 2 branches
BranchTrueFalseTotal
if $no_caps 1 1 2
if $no_caps 0 0 0
173 0 $self->lastIndex( $start + length($&) );
174 0 return [ $&, @captures ];
175 }
176 0 $self->lastIndex(0);
177 0 return undef;
178 }
179 0 ▶ if ($global) {
Branch: 2 branches
BranchTrueFalseTotal
if ($global) 21 25 46
if ($global) 0 0 0
180 0 my $qr = $self->_qr;
181 0 my $start = $self->lastIndex;
182 0 pos($str) = $start;
183 0 ▶ if ( $str =~ /$qr/g ) { # scalar /g: honours pos, advances it
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /$qr/g) 17 4 21
if ($str =~ /$qr/g) 0 0 0
184 0 my $full = $&;
185 0 my $end = pos($str);
186 # Slice the matched region; non-/g list-context match returns
187 # exactly the captures from this one match.
188 0 my @captures = substr( $str, $start, $end - $start ) =~ $qr;
189 0 ▶ @captures = () if $no_caps; # k48: drop the bool for no-groups patterns
Branch: 2 branches
BranchTrueFalseTotal
if $no_caps 8 9 17
if $no_caps 0 0 0
190 0 $self->lastIndex($end);
191 0 return [ $full, @captures ];
192 }
193 0 $self->lastIndex(0);
194 0 return undef;
195 }
196 0 my @captures = $str =~ $self->_qr;
197 0 ▶ return undef unless @captures;
Branch: 2 branches
BranchTrueFalseTotal
unless @captures 8 17 25
unless @captures 0 0 0
198 0 ▶ @captures = () if $no_caps; # k48: drop the bool for no-groups patterns
Branch: 2 branches
BranchTrueFalseTotal
if $no_caps 10 7 17
if $no_caps 0 0 0
199 0 return [ $&, @captures ];
200 }
201  
202 # k48: the capture groups of a JS regex body, in source order: one entry per
203 # group, its name for a named group, undef for a numbered one (the count is
204 # the array's size). ECMAScript regex syntax:
205 # `(` starts a group; `(?:` / `(?=` / `(?!` / `(?<=` / `(?<!` / `(?#` are
206 # non-capturing / zero-width / comment forms; `(?<NAME>` is a NAMED
207 # capture group (counts); everything else is a numbered capture group.
208 # Backslash escapes any following byte; `[...]` is a character class
209 # where parens are literals. Walk the body byte-by-byte (the JS body is
210 # ASCII for every pattern in the corpus; UTF-8 non-ASCII literals would
211 # still walk correctly because substr/skip is over bytes, not codepoints).
212 220 ▶sub _capture_groups {
Subroutine _capture_groups: called CC=19 SCAR=43.1
213 0 ▶ my ( $self, $body ) = @_;
Subroutine _capture_groups: not called CC=19 SCAR=43.1
214 0 my @groups;
215 0 my $i = 0;
216 0 my $len = length $body;
217 0 my $in_class = 0;
218 0 ▶ while ( $i < $len ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $len) 729 220 949
if ($i < $len) 0 0 0
219 0 my $c = substr( $body, $i, 1 );
220 0 ▶ if ( $c eq '\\' ) { # escape: skip 2 bytes
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "\\") 17 712 729
if ($c eq "\\") 0 0 0
221 0 $i += 2;
222 0 next;
223 }
224 0 ▶ if ($in_class) { # inside [...] -- parens literal
Branch: 2 branches
BranchTrueFalseTotal
if ($in_class) 32 680 712
if ($in_class) 0 0 0
225 0 ▶ $in_class = 0 if $c eq ']';
Branch: 2 branches
BranchTrueFalseTotal
if $c eq "]" 7 25 32
if $c eq "]" 0 0 0
226 0 $i++;
227 0 next;
228 }
229 0 ▶ if ( $c eq '[' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "[") 7 673 680
if ($c eq "[") 0 0 0
230 0 $in_class = 1;
231 0 $i++;
232 0 next;
233 }
234 0 ▶ if ( $c eq '(' && $i + 1 < $len && substr( $body, $i + 1, 1 ) eq '?' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq '(' and $i + 1 < $len and substr($body, $i + 1, 1) eq '?') { } 48 625 673
elsif ($c eq '(') { } 44 581 625
if ($c eq '(' and $i + 1 < $len and substr($body, $i + 1, 1) eq '?') { } 0 0 0
elsif ($c eq '(') { } 0 0 0
Condition41%5/12
$c eq "(" and $i + 1 < $len and substr($body, $i + 1, 1) eq "?"
!ll&&!rl&&r
5814448
$c eq "(" and $i + 1 < $len
!ll&&!rl&&r
581092
$c eq "(" and $i + 1 < $len and substr($body, $i + 1, 1) eq "?"
!ll&&!rl&&r
000
$c eq "(" and $i + 1 < $len
!ll&&!rl&&r
000
MC/DC66%2/3
$c eq "(" and $i + 1 < $len and substr($body, $i + 1, 1) eq "?"
$c eq "(" $i + 1 < $len substr($body, $i + 1, 1) eq "?"
MC/DC0%0/3
$c eq "(" and $i + 1 < $len and substr($body, $i + 1, 1) eq "?"
$c eq "(" $i + 1 < $len substr($body, $i + 1, 1) eq "?"
Truth table75%3/4
A and B and C
A= $c eq "("B= $i + 1 < $lenC= substr($body, $i + 1, 1) eq "?"
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $c eq "("B= $i + 1 < $lenC= substr($body, $i + 1, 1) eq "?"
ABCresult
0XX0
10X0
1100
1111
235 0 ▶ my $after = $i + 2 < $len ? substr( $body, $i + 2, 1 ) : '';
Branch: 2 branches
BranchTrueFalseTotal
$i + 2 < $len ? : 48 0 48
$i + 2 < $len ? : 0 0 0
236 0 ▶ if ( $after eq ':' || $after eq '=' || $after eq '!' || $after eq '#' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($after eq ':' or $after eq '=' or $after eq '!' or $after eq '#') { } 7 41 48
elsif ($after eq '<') { } 41 0 41
if ($after eq ':' or $after eq '=' or $after eq '!' or $after eq '#') { } 0 0 0
elsif ($after eq '<') { } 0 0 0
Condition50%9/18
$after eq ":" or $after eq "=" or $after eq "!" or $after eq "#"
l!l&&r!l&&!r
6141
$after eq ":" or $after eq "=" or $after eq "!"
l!l&&r!l&&!r
5142
$after eq ":" or $after eq "="
l!l&&r!l&&!r
4143
$after eq ":" or $after eq "=" or $after eq "!" or $after eq "#"
l!l&&r!l&&!r
000
$after eq ":" or $after eq "=" or $after eq "!"
l!l&&r!l&&!r
000
$after eq ":" or $after eq "="
l!l&&r!l&&!r
000
MC/DC100%4/4
$after eq ":" or $after eq "=" or $after eq "!" or $after eq "#"
$after eq ":" $after eq "=" $after eq "!" $after eq "#"
MC/DC0%0/4
$after eq ":" or $after eq "=" or $after eq "!" or $after eq "#"
$after eq ":" $after eq "=" $after eq "!" $after eq "#"
Truth table100%5/5
A or B or C or D
A= $after eq ":"B= $after eq "="C= $after eq "!"D= $after eq "#"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table0%0/5
A or B or C or D
A= $after eq ":"B= $after eq "="C= $after eq "!"D= $after eq "#"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
237 # (?: (?= (?! (?# -- non-capturing / zero-width / comment
238 }
239 elsif ( $after eq '<' ) {
240 0 ▶ my $after_lt = $i + 3 < $len ? substr( $body, $i + 3, 1 ) : '';
Branch: 2 branches
BranchTrueFalseTotal
$i + 3 < $len ? : 41 0 41
$i + 3 < $len ? : 0 0 0
241 0 ▶ if ( $after_lt ne '=' && $after_lt ne '!' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($after_lt ne "=" and $after_lt ne "!") 39 2 41
if ($after_lt ne "=" and $after_lt ne "!") 0 0 0
Condition50%3/6
$after_lt ne "=" and $after_lt ne "!"
!ll&&!rl&&r
1139
$after_lt ne "=" and $after_lt ne "!"
!ll&&!rl&&r
000
MC/DC100%2/2
$after_lt ne "=" and $after_lt ne "!"
$after_lt ne "=" $after_lt ne "!"
MC/DC0%0/2
$after_lt ne "=" and $after_lt ne "!"
$after_lt ne "=" $after_lt ne "!"
Truth table100%3/3
A and B
A= $after_lt ne "="B= $after_lt ne "!"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $after_lt ne "="B= $after_lt ne "!"
ABresult
0X0
100
111
242 # (?<NAME> -- named capture; the name runs to the next '>' (the
243 # pattern already compiled, so there is one)
244 0 my $gt = index( $body, '>', $i + 3 );
245 0 push @groups, substr( $body, $i + 3, $gt - ( $i + 3 ) );
246 }
247 # (?<= / (?<! -- lookbehind, non-capturing
248 }
249 else {
250 0 push @groups, undef; # (?xyz -- invalid ECMAScript; count defensively
251 }
252 }
253 elsif ( $c eq '(' ) {
254 0 push @groups, undef; # plain ( ... ) -- numbered capture
255 }
256 0 $i++;
257 }
258 0 return \@groups;
259 }
260  
261 # Stringification: `/source/flags` -- matches RegExp.prototype.toString so
262 # `{{ r/x/i }}` renders the right thing (oracle proves it: rendering
263 # `{{ r/name [0-9] \\// }}` produces `/name [0-9] \\//`).
264 9 ▶sub _stringify {
Subroutine _stringify: called CC=1 SCAR=1.2
265 0 ▶ my ($self) = @_;
Subroutine _stringify: not called CC=1 SCAR=1.2
266 0 return '/' . $self->source . '/' . $self->flags;
267 }
268  
269 0 ▶sub to_string { return '/' . $_[0]->source . '/' . $_[0]->flags }
Subroutine to_string: not called CC=1 SCAR=6.9
Pod undocumented
270  
271 120 ▶sub _runtime { 'Text::Nunjucks::Runtime' }
Subroutine _runtime: called CC=1 SCAR=0.0
272 106 ▶sub _lib { 'Text::Nunjucks::Lib' }
Subroutine _lib: called CC=1 SCAR=0.0
273  
274 # RegExp.prototype[Symbol.replace](string, replaceValue) -- what
275 # String.prototype.replace runs for a regex pattern, so the replace filter's
276 # regex branch (str.replace(old, new_)) lands here (ADR 0013). A coderef
277 # replaceValue is called once per match with (matched, captures..., position,
278 # string[, groups]) and its result goes through ToString; anything else goes
279 # through ToString and then GetSubstitution (Lib js_get_substitution: $&,
280 # $1..$99, $<name>, $`, $', $$). The g / y state follows RegExpBuiltinExec: g
281 # resets lastIndex to 0 and replaces every match, an empty match stepping
282 # lastIndex one on; y matches only at lastIndex; a regex with neither replaces
283 # the first match from 0 and leaves lastIndex alone. Oracle: 'aaa' with
284 # r/a*/g -> '--', 'ab' with r/x*|a/g -> '-a-b-' (Perl's own s///g gives
285 # '--b-'), 'aab' with r/a/y -> '-ab' and with r/a/gy -> '--b'.
286 # Matching and lastIndex reckon in code points, as test / exec do (k53); the
287 # position GetSubstitution and a replacer see is counted in code units (ADR
288 # 0012); the pieces are joined through js_string_append.
289 106 ▶sub symbol_replace {
Subroutine symbol_replace: called CC=11 SCAR=24.0
Pod undocumented
290 106 my ( $self, $string, $replace_value ) = @_;
291 106 my $rt = $self->_runtime;
292 106 my $lib = $self->_lib;
293  
294 106 my $s = $rt->_js_to_string($string);
295 106 my $functional = $lib->is_function($replace_value);
296 106 ▶ $replace_value = $rt->_js_to_string($replace_value) unless $functional;
Branch: 1 branch
BranchTrueFalseTotal
unless $functional 90 16 106
297 106 my $global = index( $self->flags, 'g' ) >= 0;
298 106 ▶ $self->lastIndex(0) if $global;
Branch: 1 branch
BranchTrueFalseTotal
if $global 38 68 106
299  
300 106 my @results;
301 106 ▶ while ( my $result = $self->_regexp_exec($s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $self->_regexp_exec($s)) 156 42 198
302 156 push @results, $result;
303 156 ▶ last unless $global;
Branch: 1 branch
BranchTrueFalseTotal
unless $global 64 92 156
304 # AdvanceStringIndex past an empty match
305 92 ▶ $self->lastIndex( $self->lastIndex + 1 ) if $result->{matched} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"matched"} eq "" 33 59 92
306 }
307  
308 106 my $accumulated_result = '';
309 106 my $next_source_position = 0;
310 # the code-unit position of each match, counted on from the previous one
311 106 my ( $chars, $units ) = ( 0, 0 );
312 106 for my $result (@results) {
313 156 my $position = $result->{index};
314 156 $units += $lib->js_string_length( substr( $s, $chars, $position - $chars ) );
315 156 $chars = $position;
316 my $replacement = $functional
317 ? $rt->_js_to_string(
318 $replace_value->(
319 20 ▶ $result->{matched}, @{ $result->{captures} }, $units, $s,
Branch: 1 branch
BranchTrueFalseTotal
defined $result->{'named'} ? : 10 10 20
320 defined( $result->{named} ) ? $result->{named} : (),
321 ) )
322 : $lib->js_get_substitution( $result->{matched}, $s, $units, $result->{captures},
323 156 ▶ $result->{named}, $replace_value );
Branch: 1 branch
BranchTrueFalseTotal
$functional ? : 20 136 156
324 156 ▶ if ( $position >= $next_source_position ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position >= $next_source_position) 156 0 156
325 156 $lib->js_string_append( $accumulated_result,
326 substr( $s, $next_source_position, $position - $next_source_position ) );
327 156 $lib->js_string_append( $accumulated_result, $replacement );
328 156 $next_source_position = $position + length( $result->{matched} );
329 }
330 }
331 106 ▶ return $accumulated_result if $next_source_position >= length($s);
Branch: 1 branch
BranchTrueFalseTotal
if $next_source_position >= length $s 48 58 106
332 58 $lib->js_string_append( $accumulated_result, substr( $s, $next_source_position ) );
333 58 return $accumulated_result;
334 }
335  
336 # RegExpBuiltinExec for symbol_replace: the next match under the g / y state as
337 # { matched, index (a character offset), captures (undef for a group that did
338 # not take part), named (the groups object, or undef if the regex has no named
339 # group) }, or nothing. The groups object is engine-built, so it is an ordered
340 # dict (ADR 0011) with every group name in group order (_group_names), undef
341 # for a group that did not take part. lastIndex is read and moved only for a g
342 # or y regex; a failed match resets it to 0.
343 198 ▶sub _regexp_exec {
Subroutine _regexp_exec: called CC=11 SCAR=24.0
344 198 my ( $self, $s ) = @_;
345 198 my $sticky = $self->_is_sticky;
346 198 ▶ my $stateful = $sticky || index( $self->flags, 'g' ) >= 0;
Condition100%3/3
$sticky || index($self->flags, 'g') != -1
l!l&&r!l&&!r
1812258
MC/DC100%2/2
$sticky || index($self->flags, 'g') != -1
$sticky index($self->flags, 'g') != -1
Truth table100%3/3
A || B
A= $stickyB= index($self->flags, 'g') != -1
ABresult
1X1
011
000
347 198 ▶ my $last_index = $stateful ? $self->lastIndex : 0;
Branch: 1 branch
BranchTrueFalseTotal
$stateful ? : 140 58 198
348 198 ▶ if ( $last_index > length($s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_index > length $s) 11 187 198
349 11 ▶ $self->lastIndex(0) if $stateful;
Branch: 1 branch
BranchTrueFalseTotal
if $stateful 11 0 11
350 11 return;
351 }
352 # CAVEAT (see the test method): the qr must sit in a lexical before interpolation
353 187 ▶ my $qr = $sticky ? $self->_qr_anchored : $self->_qr;
Branch: 1 branch
BranchTrueFalseTotal
$sticky ? : 18 169 187
354 187 pos($s) = $last_index;
355 187 ▶ if ( !( $s =~ /$qr/g ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s =~ /$qr/g) 31 156 187
356 31 ▶ $self->lastIndex(0) if $stateful;
Branch: 1 branch
BranchTrueFalseTotal
if $stateful 30 1 31
357 31 return;
358 }
359 # every group of the pattern ($#+), not @{^CAPTURE}: that one stops at the
360 # last group that took part (oracle: 'ab' with r/(a)|(z)/ and '[$1|$2]'
361 # gives '[a|]', $2 being a group that exists)
362 my %result = (
363 matched => substr( $s, $-[0], $+[0] - $-[0] ),
364 index => $-[0],
365 53 ▶ captures => [ map { defined( $-[$_] ) ? substr( $s, $-[$_], $+[$_] - $-[$_] ) : undef } 1 .. $#+ ],
Branch: 2 branches
BranchTrueFalseTotal
defined $-[$_] ? : 43 10 53
@{$self->_group_names;} ? : 14 142 156
366 156 named => @{ $self->_group_names }
367 156 ? $self->_runtime->make_dict( map { $_ => $+{$_} } @{ $self->_group_names } )
368 : undef,
369 );
370 156 ▶ $self->lastIndex( $+[0] ) if $stateful;
Branch: 1 branch
BranchTrueFalseTotal
if $stateful 99 57 156
371 156 return \%result;
372 }
373  
374 23 1;
375  
376 __END__
« blib/lib/Text/Nunjucks/Parser/Parser.pm ↑ Summary blib/lib/Text/Nunjucks/Runtime.pm »