File Coverage

blib/lib/String/LCSS.pm
Criterion Covered Total %
statement 26 26 100.0
branch 14 14 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1             package String::LCSS;
2              
3 2     2   42083 use warnings;
  2         4  
  2         58  
4 2     2   10 use strict;
  2         4  
  2         674  
5              
6             require Exporter;
7             our @ISA = qw(Exporter);
8             our @EXPORT_OK = qw(lcss);
9              
10             our $VERSION = '1.00';
11              
12             sub lcss {
13 13     13 1 4808 my $solns0 = (_lcss($_[0], $_[1]))[0];
14 13 100       53 return unless $solns0;
15 12         15 my @match = @{ $solns0 };
  12         31  
16 12 100       33 return if length $match[0] == 1;
17 11 100       64 return wantarray ? @match : $match[0];
18             }
19              
20             sub _lcss {
21             # Return array-of-arrays of longest substrings and indices
22 13     13   24 my( $r1, $r2 ) = @_;
23 13         24 my( $l1, $l2, $swap ) = ( length $r1, length $r2, 0 );
24 13 100       37 ( $r1, $r2, $l1, $l2, $swap ) = ( $r2, $r1, $l2, $l1, 1 ) if $l1 > $l2;
25              
26 13         26 my( $best, @solns ) = 0;
27 13         31 for my $start ( 0 .. $l2 - 1 ) {
28 379         651 for my $l ( reverse 1 .. $l1 - $start ) {
29 6445         8070 my $substr = substr( $r1, $start, $l );
30 6445         8973 my $o = index( $r2, $substr );
31 6445 100       12433 next if $o < 0;
32 2401 100       5802 if( $l > $best ) {
    100          
33 19         25 $best = length $substr;
34 19         74 @solns = [ $substr, $start, $o ];
35             }
36             elsif( $l == $best ) {
37 3         11 push @solns, [ $substr, $start, $o ];
38             }
39             }
40             }
41 13         36 return @solns;
42             }
43              
44              
45             #########################################################
46             # Do not change this, Do not put anything below this.
47             # File must return "true" value at termination
48             1;
49             ##########################################################
50              
51              
52              
53             =head1 NAME
54              
55             String::LCSS - Find The Longest Common Substring of Two Strings.
56              
57             =head1 VERSION
58              
59             This document refers to String::LCSS version 1.00.
60              
61             =head1 SYNOPSIS
62              
63             use String::LCSS;
64             my $longest = lcss( "zyzxx", "abczyzefg" );
65             print $longest, "\n";
66              
67             my @result = lcss( "zyzxx", "abczyzefg" );
68             print "$result[0] ($result[1],$result[2])\n";
69              
70             =head1 DESCRIPTION
71              
72             String::LCSS provides the function C to ferret out the longest common
73             substring shared by two strings passed as arguments.
74              
75             =head1 SUBROUTINES
76              
77             =over 4
78              
79             =item lcss($string1, $string2)
80              
81             C is returned if the susbstring length is one char or less.
82              
83             In scalar context, returns the substring.
84              
85             When used in an array context, C will return the indexi of the match
86             root in the two args.
87              
88             =back
89              
90             =head1 EXPORT
91              
92             The C function is exported by default.
93              
94             =head1 BUGS
95              
96             There are no known bugs in this module.
97              
98             =head1 SEE ALSO
99              
100             L is not pure Perl, but it was created to be faster.
101              
102             =head1 COPYRIGHT
103              
104             This module is free software; you can redistribute it and/or modify it under
105             the same terms as Perl itself. See L.
106              
107             =head1 AUTHOR
108              
109             The original author is Daniel Yacob (CPAN ID: DYACOB).
110              
111             Gene Sullivan (gsullivan@cpan.org) is a co-maintainer.
112              
113             =head1 ACKNOWLEDGEMENTS
114              
115             Code provided by BrowserUk from PerlMonks.
116              
117             =cut
118