CommonPrefix.pm

filter: Statementstmt 38.7% 24 / 62 Branchbran 21.4% 6 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 100% 2 / 2 totaltotal 37.7% 37 / 98 CC 20 SCAR 49.8
CC
20
Cov
33%
majority_prefix
240.0
common_prefix
6.5
BEGIN
1.0
CRAP 145.7
↑ Summary CommonSuffix.pm »
1 package String::CommonPrefix;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 2 our $DATE = '2025-08-02'; # DATE
11 2 our $DIST = 'String-CommonPrefix'; # DIST
12 2 our $VERSION = '0.021'; # VERSION
13  
14 2 our @EXPORT_OK = qw(
15 common_prefix
16 majority_prefix
17 );
18  
19 6 ▶sub common_prefix {
Subroutine common_prefix: called CC=6 SCAR=18.7
Pod documented
20 6 ▶ return undef unless @_; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 6 6
21 6 my $i;
22 L1:
23 6 ▶ for ($i=0; $i < length($_[0]); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $_[0]) 6 3 9
24 6 for (@_[1..$#_]) {
25 9 ▶ if (length($_) < $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ < $i) { } 0 9 9
26 0 $i--; last L1;
27 } else {
28 9 ▶ last L1 if substr($_, $i, 1) ne substr($_[0], $i, 1);
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, $i, 1) ne substr($_[0], $i, 1) 3 6 9
29 }
30 }
31 }
32 6 substr($_[0], 0, $i);
33 }
34  
35 0 ▶sub majority_prefix {
Subroutine majority_prefix: not called CC=15 SCAR=54.8
Pod documented
36 #my $opts = ref($_[0]) eq 'HASH' ? {%{shift}} : {};
37 0 ▶ return undef unless @_; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
38  
39 0 my @excluded = map {0} @_;
40 0 my $min_items_for_majority = int(@_/2) + 1;
41 0 my $prefix_len = 0;
42 0 while (1) {
43 0 my @items;
44 # take the next character from items that are still eligible
45 0 for my $i (0..$#_) {
46 0 my $char;
47 0 ▶ if ($excluded[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($excluded[$i]) { } 0 0 0
elsif (length $_[$i] < $prefix_len) { } 0 0 0
48 0 $char = undef;
49 } elsif (length($_[$i]) < $prefix_len) {
50 0 $excluded[$i]++;
51 0 $char = undef;
52 } else {
53 0 $char = substr($_[$i], $prefix_len, 1);
54 }
55 0 push @items, $char;
56 }
57 #use DD; dd \@excluded;
58 # determine whether there's still a character that belong to the
59 # majority
60 0 my %freqs;
61 0 ▶ for (@items) { next unless defined; $freqs{$_}++ }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
62 0 ▶ last unless keys %freqs;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %freqs 0 0 0
63 #use DD; dd \%freqs;
64 0 my @freqs = sort { $freqs{$b} <=> $freqs{$a} } keys %freqs;
65 0 my $majority_char = $freqs[0];
66 0 ▶ last if $freqs{$majority_char} < $min_items_for_majority;
Branch: 1 branch
BranchTrueFalseTotal
if $freqs{$majority_char} < $min_items_for_majority 0 0 0
67 0 ▶ for (0 .. $#items) { next unless defined $items[$_]; unless ($items[$_] eq $majority_char) { $excluded[$_]++ } }
Branch: 2 branches
BranchTrueFalseTotal
unless defined $items[$_] 0 0 0
unless ($items[$_] eq $majority_char) 0 0 0
68 0 $prefix_len++;
69 #say "D: prefix_len=$prefix_len";
70 }
71  
72 0 my $first_included;
73 0 ▶ for (0 .. $#_) { unless ($excluded[$_]) { $first_included = $_; last } }
Branch: 1 branch
BranchTrueFalseTotal
unless ($excluded[$_]) 0 0 0
74 0 ▶ return undef unless defined $first_included; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_included 0 0 0
75 #say "D: first_included=$first_included";
76 0 substr($_[$first_included], 0, $prefix_len);
77 }
78  
79 2 1;
80 # ABSTRACT: Find prefix common to all strings
81  
82 __END__
↑ Summary CommonSuffix.pm »