Skip to content

Commit

Permalink
Always look in Capture stack first for :exists
Browse files Browse the repository at this point in the history
Since we only want to know whether something exists, we can first go
through the capture stack, as that will short-circuit when the name
is found.  Only if that fails, check for the name in the capnames
hash.
  • Loading branch information
lizmat committed Jun 27, 2020
1 parent c56c996 commit bda3a5d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/core.c/Match.pm6
Expand Up @@ -139,7 +139,10 @@ my class Match is Cool does NQPMatchRole {
}

# API function for $/[0]:exists ...
method EXISTS-POS(int $pos --> Bool:D) { self.EXISTS-KEY(my str $ = $pos) }
method EXISTS-POS(int $pos --> Bool:D) {
# $!from is good enough here
nqp::hllbool(nqp::isge_i($!pos,$!from) && self!exists(my str $ = $pos))
}

# Positional API functions that are not supported
method ASSIGN-POS(int $pos, \val) {
Expand Down Expand Up @@ -167,13 +170,8 @@ my class Match is Cool does NQPMatchRole {

# API function for $/<foo>:exists ...
method EXISTS-KEY(str $name --> Bool:D) {
nqp::hllbool(
nqp::isge_i($!pos,$!from) # $!from is good enough here
&& nqp::not_i(nqp::isnull(my $max-captures :=
nqp::atkey(nqp::getattr($!regexsub,Regex,'$!capnames'),$name)
))
&& (nqp::isge_i($max-captures,2) || self!exists($name))
)
# $!from is good enough here
nqp::hllbool(nqp::isge_i($!pos,$!from) && self!exists($name))
}

# Associative API functions that are not supported
Expand Down Expand Up @@ -218,7 +216,10 @@ my class Match is Cool does NQPMatchRole {
)
);

0
# check for multiple capture
nqp::not_i(nqp::isnull(my $max-captures :=
nqp::atkey(nqp::getattr($!regexsub,Regex,'$!capnames'),$name)
)) && nqp::isge_i($max-captures,2)
}

# find a single capture like (.), knowing there are captures
Expand Down

0 comments on commit bda3a5d

Please sign in to comment.