Algorithm::RabinKarp::Util - utility methods for use with Rabin-Karp hash generation.


Algorithm-RabinKarp documentation Contained in the Algorithm-RabinKarp distribution.

Index


Code Index:

NAME

Top

Algorithm::RabinKarp::Util - utility methods for use with Rabin-Karp hash generation.

GENERATORS

Top

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.

filter_regexp ( REGEXP, CODEREF )

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

stream_fh ( FileHandle )

Iterates across values in a file handle.

stream_string ( $scalar )

Iterates across characters in a string.

AUTHOR

Top

  Norman Nunley, Jr <nnunley@cpan.org>

1;


Algorithm-RabinKarp documentation Contained in the Algorithm-RabinKarp distribution.
package Algorithm::RabinKarp::Util;

use base qw(Exporter);
our @EXPORT_OK = qw( filter_regexp stream_fh stream_string); 

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;
  };  
}

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);
  };
}

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;
  };
}