|  line  | 
 stmt  | 
 bran  | 
 cond  | 
 sub  | 
 pod  | 
 time  | 
 code  | 
| 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 package Algorithm::RabinKarp::Util;  | 
| 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
3
 | 
3
 | 
 
 | 
 
 | 
  
3
  
 | 
 
 | 
435967
 | 
 use base qw(Exporter);  | 
| 
 
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
7
 | 
    | 
| 
 
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
1370
 | 
    | 
| 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our @EXPORT_OK = qw( filter_regexp stream_fh stream_string);   | 
| 
5
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
6
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =head1 NAME  | 
| 
7
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 Algorithm::RabinKarp::Util - utility methods for use with Rabin-Karp hash generation.  | 
| 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
10
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
11
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =head1 GENERATORS   | 
| 
12
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
13
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =over 4  | 
| 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
15
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 These are generator functions that all create a subroutine closure which  | 
| 
16
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 produce pairs of ( value, position ) until their source is exhaused, and undef  | 
| 
17
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 when no values remain.  | 
| 
18
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
19
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =item filter_regexp ( REGEXP, CODEREF )  | 
| 
20
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 Given a coderef matching the signature given for L,  | 
| 
22
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 this method will create a generator that skips all characters that match a  | 
| 
23
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 given regexp.  | 
| 
24
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
25
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =cut  | 
| 
26
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
27
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub filter_regexp {  | 
| 
28
 | 
1
 | 
 
 | 
 
 | 
  
1
  
 | 
  
1
  
 | 
2
 | 
   my $regexp = shift;  | 
| 
29
 | 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
2
 | 
   my $coderef = shift;  | 
| 
30
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
   sub {  | 
| 
31
 | 
6
 | 
 
 | 
 
 | 
  
6
  
 | 
 
 | 
28
 | 
     my ($c, $pos);  | 
| 
32
 | 
6
 | 
 
 | 
 
 | 
 
 | 
 
 | 
11
 | 
     CHAR: while (($c, @rest) = $coderef->()) {  | 
| 
33
 | 
8
 | 
  
100
  
 | 
 
 | 
 
 | 
 
 | 
50
 | 
        last CHAR if chr($c) !~ $regexp;  | 
| 
34
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     }   | 
| 
35
 | 
6
 | 
  
100
  
 | 
 
 | 
 
 | 
 
 | 
58
 | 
     return unless $c;  | 
| 
36
 | 
5
 | 
 
 | 
 
 | 
 
 | 
 
 | 
20
 | 
     return $c, @rest;  | 
| 
37
 | 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
8
 | 
   };    | 
| 
38
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
39
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
40
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =item stream_fh ( FileHandle )  | 
| 
41
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
42
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 Iterates across values in a file handle.  | 
| 
43
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
44
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =cut  | 
| 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
46
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub stream_fh {   | 
| 
47
 | 
2
 | 
 
 | 
 
 | 
  
2
  
 | 
  
1
  
 | 
962
 | 
   my $fh = shift;  | 
| 
48
 | 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
6
 | 
   my $line = 0;  | 
| 
49
 | 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
4
 | 
   my $col = -1;  | 
| 
50
 | 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
5
 | 
   my $nl = ord("\n");  | 
| 
51
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
   sub {  | 
| 
52
 | 
18
 | 
  
100
  
 | 
 
 | 
  
18
  
 | 
 
 | 
7277
 | 
       return if $fh->eof;  | 
| 
53
 | 
3
 | 
 
 | 
 
 | 
  
3
  
 | 
 
 | 
4542
 | 
       use bytes;  | 
| 
 
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
39
 | 
    | 
| 
 
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
15
 | 
    | 
| 
54
 | 
16
 | 
 
 | 
 
 | 
 
 | 
 
 | 
123
 | 
       my $pos = tell($fh);  | 
| 
55
 | 
16
 | 
 
 | 
 
 | 
 
 | 
 
 | 
51
 | 
       my $char = ord($fh->getc);  | 
| 
56
 | 
16
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
100
 | 
       if ($char == $nl)  {  | 
| 
57
 | 
0
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
         $col = 0; $line++;  | 
| 
 
 | 
0
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
    | 
| 
58
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
       } else {  | 
| 
59
 | 
16
 | 
 
 | 
 
 | 
 
 | 
 
 | 
34
 | 
         $col ++;  | 
| 
60
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
       }  | 
| 
61
 | 
16
 | 
 
 | 
 
 | 
 
 | 
 
 | 
54
 | 
       ($char, $pos, $col, $line);  | 
| 
62
 | 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
24
 | 
   };  | 
| 
63
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
64
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
65
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =item stream_string ( $scalar )  | 
| 
66
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
67
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 Iterates across characters in a string.  | 
| 
68
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
69
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =cut   | 
| 
70
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
71
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub stream_string {  | 
| 
72
 | 
3
 | 
 
 | 
 
 | 
  
3
  
 | 
  
1
  
 | 
6
 | 
   my $string = shift;  | 
| 
73
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
7
 | 
   my $pos = 0;  | 
| 
74
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
   sub {  | 
| 
75
 | 
22
 | 
  
100
  
 | 
 
 | 
  
22
  
 | 
 
 | 
42
 | 
       return if ($pos >= length($string));  | 
| 
76
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
38
 | 
       my @ret = (ord(substr($string, $pos, 1)), $pos);  | 
| 
77
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
16
 | 
       $pos++;  | 
| 
78
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
61
 | 
       return @ret;  | 
| 
79
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
22
 | 
   };  | 
| 
80
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
81
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
82
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =back  | 
| 
83
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
84
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 =head1 AUTHOR  | 
| 
85
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
86
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
   Norman Nunley, Jr   | 
| 
87
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
88
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 1;  |