blib/lib/Regexp/Trie.pm

filter: Statementstmt 27.0% 10 / 37 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 42.8% 3 / 7 Podpod 0.0% 0 / 3 totaltotal 18.3% 13 / 71 CC 13 SCAR 43.7
CC
13
Cov
17%
_regexp
132.0
add
12.0
new
2.0
CRAP 78.7
↑ Summary
1 #
2 # $Id: Trie.pm,v 0.2 2006/04/27 05:24:40 dankogai Exp dankogai $
3 #
4  
5 package Regexp::Trie;
6 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g;
11  
12 # use overload q("") => sub { shift->regexp };
13  
14 0 ▶sub new{ bless {} => shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
15 0 ▶sub add{
Subroutine add: not called CC=3 SCAR=24.8
Pod undocumented
16 0 my $self = shift;
17 0 my $str = shift;
18 0 my $ref = $self;
19 0 for my $char (split //, $str){
20 0 ▶ $ref->{$char} ||= {};
Condition0%0/2
$ref->{$char} ||= {}
l!l
00
MC/DC0%0/1
$ref->{$char} ||= {}
$ref->{$char}
Truth table0%0/2
A
A= $ref->{$char}
Aresult
11
00
21 0 $ref = $ref->{$char};
22 }
23 0 $ref->{''} = 1; # { '' => 1 } as terminator
24 0 $self;
25 }
26 0 ▶sub _regexp{
Subroutine _regexp: not called CC=11 SCAR=48.8
27 0 my $self = shift;
28 0 ▶ return if $self->{''} and scalar keys %$self == 1; # terminator
Branch: 1 branch
BranchTrueFalseTotal
if $self->{""} and scalar keys %$self == 1 0 0 0
Condition0%0/3
$self->{""} and scalar keys %$self == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{""} and scalar keys %$self == 1
$self->{""} scalar keys %$self == 1
Truth table0%0/3
A and B
A= $self->{""}B= scalar keys %$self == 1
ABresult
0X0
100
111
29 0 my (@alt, @cc);
30 0 my $q = 0;
31 0 for my $char (sort keys %$self){
32 0 my $qchar = quotemeta $char;
33 0 ▶ if (ref $self->{$char}){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$char}) { } 0 0 0
34 0 ▶ if (defined (my $recurse = _regexp($self->{$char}))){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $recurse = _regexp($self->{$char}))) { } 0 0 0
35 0 push @alt, $qchar . $recurse;
36 }else{
37 0 push @cc, $qchar;
38 }
39 }else{
40 0 $q = 1;
41 }
42 }
43 0 my $cconly = !@alt;
44 0 ▶ @cc and push @alt, @cc == 1 ? $cc[0] : '['. join('', @cc). ']';
Branch: 2 branches
BranchTrueFalseTotal
@cc and push @alt, @cc == 1 ? $cc[0] : '[' . join("", @cc) . ']' 0 0 0
@cc == 1 ? : 0 0 0
45 0 ▶ my $result = @alt == 1 ? $alt[0] : '(?:' . join('|', @alt) . ')';
Branch: 1 branch
BranchTrueFalseTotal
@alt == 1 ? : 0 0 0
46 0 ▶ $q and $result = $cconly ? "$result?" : "(?:$result)?";
Branch: 2 branches
BranchTrueFalseTotal
$q and $result = $cconly ? "$result?" : "(?:$result)?" 0 0 0
$cconly ? : 0 0 0
47 0 return $result;
48 }
49 0 ▶sub regexp{ my $str = shift->_regexp; qr/$str/ }
Subroutine regexp: not called CC=1 SCAR=6.9
Pod undocumented
50  
51 1 1;
52 __END__
↑ Summary