Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
do submatch with cursors, not substr and ~~
Not only is this more efficient, it also doesn't break anchors
or introduce spurious scanning.
  • Loading branch information
TimToady committed Apr 23, 2015
1 parent 7bef4a3 commit 1fb4820
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/core/Cursor.pm
Expand Up @@ -151,6 +151,7 @@ my class Cursor does NQPCursorRole {
# Call it if it is a routine. This will capture if requested.
return (var)(self) if nqp::istype(var,Callable);
my $maxlen := -1;
my $maxmatch;
my $cur := self.'!cursor_start_cur'();
my $pos := nqp::getattr_i($cur, $?CLASS, '$!from');
my $tgt := $cur.target;
Expand Down Expand Up @@ -225,18 +226,13 @@ my class Cursor does NQPCursorRole {
return $cur.'!cursor_start_cur'()
if nqp::istype($topic,Associative);
my $rx := MAKE_REGEX($topic, :$i);
$match := (nqp::substr($tgt, $pos, $eos - $pos) ~~ $rx).Str;
$len := nqp::chars( $match );
$match := self.$rx;
$len := $match.pos - $match.from;
}
elsif nqp::istype($topic,Regex) {
# A Regex already.
$match := nqp::substr($tgt, $pos, $eos - $pos) ~~ $topic;

# In order to return the correct result we need to match from the
# current position only.
next if $match.from;
$match := ~$match;
$len := nqp::chars( $match );
$match := self.$topic;
$len := $match.pos - $match.from;
}
else {
# The pattern is a string.
Expand All @@ -249,10 +245,12 @@ my class Cursor does NQPCursorRole {

if $match && $len > $maxlen && $pos + $len <= $eos {
$maxlen := $len;
$maxmatch := $match;
last if $s; # stop here for sequential alternation
}
}

return $maxmatch if nqp::istype($maxmatch, Cursor);
$cur.'!cursor_pass'($pos + $maxlen, '') if $maxlen >= 0;
$cur
}
Expand Down Expand Up @@ -298,8 +296,7 @@ sub MAKE_REGEX($arg, :$i) {
$arg.regex
}
else {
# XXX the following misuses ^ to turn off scanning
my $rx := $i ?? EVAL("anon regex \{ :i ^ $arg\}") !! EVAL("anon regex \{ ^ $arg\}");
my $rx := $i ?? EVAL("anon regex \{ :i $arg\}") !! EVAL("anon regex \{ $arg\}");
$arg does CachedCompiledRegex($rx);
$rx
}
Expand Down

0 comments on commit 1fb4820

Please sign in to comment.