# Same as hek2.pl, but change one line
#
# 20030618 MJD

use warnings;
use strict;

sub repeated_substring {
  my $size = length $_[0];
  my $maxsize = int ($size / 2);
  my ($f_index, $f_offset) = (0,0);

OUTER:
  for (my $index=0; $index < $size; $index++) {

    print "> ", substr($_[0], $index, 45), "...\n";
    # The big optimizer 
    # (added a bounds check to keep us from going beyond the end of the data)
    if ($f_offset > 20 && $index + $f_offset <= $size) {
      for (my $t_index = $index + $f_offset - 15; $t_index >= $index; $t_index =
$t_index - 15) {
        my $z = substr($_[0], $t_index, $index+$f_offset-$t_index);
        print "  pos $index: $z";
        if (index($_[0], $z, $index+$f_offset) == -1) {
          print "- skipping\n"; 
          $index = $t_index +1;   # MJD added the '+1' here
          next OUTER;
        } else {
          print "\n";
        }
      }
    }

    for (my $offset=$f_offset+1; $offset <= $maxsize && $index + $offset <=
$size; $offset++) {
      last unless index($_[0], substr($_[0], $index, $offset), $index+$offset) >= 0;
      $f_index  = $index;
      $f_offset = $offset;
    }
  }
  return substr($_[0], $f_index, $f_offset);
}

