Skip to content

Commit

Permalink
Introduce Rakudo::Iterator.MatchCursor
Browse files Browse the repository at this point in the history
Given a regex, a string and a way of moving the cursor (:g, :ov, :ex)
create an iterator producing the cursors.  Future workhorse of all
iterators that are based on regular expressions, such as .split and .comb.

Not used yet.
  • Loading branch information
lizmat committed Jun 5, 2020
1 parent bf01f84 commit da9943a
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/core.c/Rakudo/Iterator.pm6
Expand Up @@ -2584,6 +2584,60 @@ class Rakudo::Iterator {
}
method Mappy-values(\map) { Mappy-values.new(map) }

# cache cursor initialization lookup
my $initialize-cursor := Match.^lookup("!cursor_init");

my &POPULATE := Match.^lookup("MATCH" ); # fully populate Match object

my $movers := nqp::list(
Match.^lookup("CURSOR_MORE"), # :g
Match.^lookup("CURSOR_OVERLAP"), # :ov
Match.^lookup("CURSOR_NEXT") # :ex
);

# Iterate a cursor according to a given regex, string and mover
my class MatchCursor does Iterator {
has Mu $!cursor;
has Mu $!mover;
method !SET-SELF(&regex, \string, int $mover) {
$!cursor := regex($initialize-cursor(Match, string, :0c));
$!mover := nqp::atpos($movers,$mover);
self
}
method new(\regex, \string, \mover) {
nqp::create(self)!SET-SELF(regex, string, mover)
}
method pull-one() is raw {
nqp::if(
nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
nqp::stmts(
(my $current := $!cursor),
($!cursor := $!mover($!cursor)),
$current
),
IterationEnd
)
}
method skip-one() is raw {
nqp::if(
nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
($!cursor := $!mover($!cursor)),
)
}
method push-all(\target --> IterationEnd) {
nqp::while(
nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
nqp::stmts(
target.push($!cursor),
($!cursor := $!mover($!cursor))
)
)
}
}
method MatchCursor(\regex, \string, \mover) {
MatchCursor.new(regex, string, mover)
}

# Return an iterator that will iterate over a source iterator and an
# iterator generating monotonically increasing index values from a
# given offset. Optionally, call block if an out-of-sequence index
Expand Down

0 comments on commit da9943a

Please sign in to comment.