Algorithm-RabinKarp

 view release on metacpan or  search on metacpan

lib/Algorithm/RabinKarp/Util.pm  view on Meta::CPAN

These are generator functions that all create a subroutine closure which
produce pairs of ( value, position ) until their source is exhaused, and undef
when no values remain.

=item filter_regexp ( REGEXP, CODEREF )

Given a coderef matching the signature given for L<Algorithm::RabinKarp>,
this method will create a generator that skips all characters that match a
given regexp.

=cut

sub filter_regexp {
  my $regexp = shift;
  my $coderef = shift;
  sub {
    my ($c, $pos);
    CHAR: while (($c, @rest) = $coderef->()) {
       last CHAR if chr($c) !~ $regexp;
    } 
    return unless $c;
    return $c, @rest;
  };  
}

=item stream_fh ( FileHandle )

Iterates across values in a file handle.

=cut

sub stream_fh { 
  my $fh = shift;
  my $line = 0;
  my $col = -1;
  my $nl = ord("\n");
  sub {
      return if $fh->eof;
      use bytes;
      my $pos = tell($fh);
      my $char = ord($fh->getc);
      if ($char == $nl)  {
        $col = 0; $line++;
      } else {
        $col ++;
      }
      ($char, $pos, $col, $line);
  };
}

=item stream_string ( $scalar )

Iterates across characters in a string.

=cut 

sub stream_string {
  my $string = shift;
  my $pos = 0;
  sub {
      return if ($pos >= length($string));
      my @ret = (ord(substr($string, $pos, 1)), $pos);
      $pos++;
      return @ret;
  };
}

=back

=head1 AUTHOR

  Norman Nunley, Jr <nnunley@cpan.org>

1;



( run in 1.985 second using v1.01-cache-2.11-cpan-acebb50784d )