blib/lib/EBook/Ishmael/CharDet/UTF8.pm

filter: Statementstmt 81.8% 45 / 55 Branchbran 70.8% 17 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 5 totaltotal 70.4% 69 / 98 CC 15 SCAR 23.2
CC
15
Cov
76%
confidence
20.0
take
12.2
bad
2.0
CRAP 10.2
« blib/lib/EBook/Ishmael.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet.pm »
1 package EBook::Ishmael::CharDet::UTF8;
2 18 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 our $VERSION = '2.03';
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use EBook::Ishmael::CharDet::Constants qw(:CONSTANTS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 my $ONE_CHAR_PROB = 0.5;
10 18 my $THRESHOLD = 7;
11  
12 sub new {
13  
14 67 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15  
16 67 my $self = {
17 Code => undef,
18 Left => 0,
19 MBs => 0,
20 Total => 0,
21 Bad => 0,
22 };
23 67 return bless $self, $class;
24  
25 }
26  
27 sub take {
28  
29 3410 ▶ my ($self, $bytes) = @_;
Subroutine take: called CC=12 SCAR=25.0
Pod undocumented
30  
31 3410 ▶ return TAKE_BAD if $self->{Bad};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Bad"} 0 3410 3410
32 3410 ▶ return TAKE_MUST_BE if $self->{MBs} >= $THRESHOLD;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"MBs"} >= $THRESHOLD 0 3410 3410
33  
34 3410 for my $i (0 .. length($bytes)-1) {
35 54002 my $b = ord(substr $bytes, $i, 1) & 0xff;
36 54002 ▶ if (not defined $self->{Code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'Code'}) { } 53653 349 54002
37 # ASCII
38 53653 ▶ if (not $b & 0x80) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $b & 128) { } 53386 267 53653
elsif ($b >> 5 == 6) { } 150 117 267
elsif ($b >> 4 == 14) { } 102 15 117
elsif ($b >> 3 == 30) { } 0 15 15
39 53386 $self->{Total}++;
40 53386 next;
41 # 2-byte character (0b110...)
42 } elsif ($b >> 5 == 0b110) {
43 150 $self->{Code} = $b & 0b11111;
44 150 $self->{Left} = 1;
45 # 3-byte character (0b1110...)
46 } elsif ($b >> 4 == 0b1110) {
47 102 $self->{Code} = $b & 0b1111;
48 102 $self->{Left} = 2;
49 # 4-byte character (0b11110...)
50 } elsif ($b >> 3 == 0b11110) {
51 0 $self->{Code} = $b & 0b111;
52 0 $self->{Left} = 3;
53 # Invalid UTF8
54 } else {
55 15 $self->{Bad} = 1;
56 15 return TAKE_BAD;
57 }
58 } else {
59 349 ▶ if ($b >> 6 != 0b10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b >> 6 != 2) 19 330 349
60 19 $self->{Bad} = 1;
61 19 return TAKE_BAD;
62 }
63 330 $self->{Code} = ($self->{Code} << 6) | ($b & 0b111111);
64 330 $self->{Left}--;
65 330 ▶ if ($self->{Left} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Left"} == 0) 233 97 330
66 233 $self->{Total}++;
67 233 $self->{MBs}++;
68 233 undef $self->{Code};
69 233 ▶ return TAKE_MUST_BE if $self->{MBs} >= $THRESHOLD;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"MBs"} >= $THRESHOLD 33 200 233
70 }
71 }
72 }
73  
74 3343 return TAKE_OK;
75  
76 }
77  
78 sub confidence {
79  
80 0 ▶ my ($self) = @_;
Subroutine confidence: not called CC=4 SCAR=30.0
Pod undocumented
81  
82 0 ▶ if ($self->{Bad} or $self->{MBs} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Bad"} or $self->{"MBs"} == 0) 0 0 0
Condition0%0/3
$self->{"Bad"} or $self->{"MBs"} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"Bad"} or $self->{"MBs"} == 0
$self->{"Bad"} $self->{"MBs"} == 0
Truth table0%0/3
A or B
A= $self->{"Bad"}B= $self->{"MBs"} == 0
ABresult
1X1
011
000
83 0 return 0;
84 }
85  
86 # >= 6, we effectively get 0.99
87 0 ▶ if ($self->{MBs} < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'MBs'} < 6) { } 0 0 0
88 0 return 1.0 - ($ONE_CHAR_PROB ** $self->{MBs});
89 } else {
90 0 return 0.99;
91 }
92  
93 }
94  
95 sub bad {
96  
97 0 ▶ my ($self) = @_;
Subroutine bad: not called CC=1 SCAR=6.9
Pod undocumented
98  
99 0 return $self->{Bad};
100  
101 }
102  
103 33 ▶sub encoding { 'UTF-8' }
Subroutine encoding: called CC=1 SCAR=0.0
Pod undocumented
104  
105 18 1;
« blib/lib/EBook/Ishmael.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet.pm »