| 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} ||= {}
MC/DC0%0/1
$ref->{$char} ||= {}
$ref->{$char}
Truth table0%0/2
A
A= $ref->{$char}
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->{""} and scalar keys %$self == 1
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if (defined (my $recurse = _regexp($self->{$char}))){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $result = @alt == 1 ? $alt[0] : '(?:' . join('|', @alt) . ')'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $q and $result = $cconly ? "$result?" : "(?:$result)?"; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||