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

filter: Statementstmt 93.6% 44 / 47 Branchbran 85.7% 12 / 14 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 5 totaltotal 78.8% 71 / 90 CC 12 SCAR 17.9
CC
12
Cov
87%
take
10.0
confidence
3.8
bad
2.0
CRAP 6.0
« blib/lib/EBook/Ishmael/CharDet/EUCKR.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/ShiftJIS.pm »
1 package EBook::Ishmael::CharDet::GB2312;
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 # Generated from contrib/cjk2encodings.pl
10 18 my %GB2312_FREQS = map { $_ => 1 } (
11 41379,53947,45755,51403,49611,46532,54958,41400,41401,54224,51911,52946,46528,
12 54490,55251,52219,46323,53938,51663,51365,50403,46531,54992,53186,48338,53972,
13 46840,46525,46283,50916,51413,46846,51706,46070,51453,55032,41378,48845,51431,
14 50375,52460,54230,51906,54954,53930,53409,55252,51886,49359,54751,54459,47531,
15 49097,49076,52217,51387,47822,53444,45265,51120,48088,48625,47811,50410,54971,
16 41398,41399,53456,51645,52725,57825,50150,53969,60598,50396,48624,50928,53216,
17 54977,46552,53192,54781,52164,51693,46816,55031,50606,51926,50171,45539,55263,
18 53716,50167,55287,47577,52932,48592,46780,52652,52706,52160,47090,53984,52965,
19 52395,47037,49391,46536,52916,52729,46322,51697,54257,47562,45271,54002,54466,
20 46025,48893,53986,48040,53227,45502,51701,52983,47821,50379,54211,47062,45528,
21 52142,45273,47327,55201,51444,55286,52923,53971,45560,48308,45234,51903,46556,
22 55288,49847,51897,51435,54758,51887,47288,49114,50172,51154,47047,51673,50371,
23 53462,55254,47039,51173,45765,51664,45259,48838,49390,46036,53417,50383,53945,
24 54489,48596,45499,47334,46760,53414,48588,51904,48314,51177,54470,53445,49657,
25 50911,48837,47862,46554,52964,50415,54234,47016,47314,47351,46530,49570,46507,
26 52204,53723,52173,50360,47597,51168,47859,51361,49389,52648,50877,54442,46286,
27 47278,46023,53170,45291,54517,53954,53234,47045,48304,55005,52419,46521,53461,
28 51919,48328,51111,52961,47586,48610,48557,55004,46839,51187,54445,54267,54520,
29 54503,52975,47779,45987,55000,46266,54961,45489,46518,51371,53219,49646,47861,
30 51627,46252,45277,52731,49135,47611,49574,48114,49101,52422,45806,53467,53193,
31 52221,51932,47354,48611,48825,51927,53442,47557,54217,48034,45999,45475,55267,
32 54178,54262,46842,48851,50367,48097,55279,41395,41394,50669,49622,47823,48571,
33 48809,49652,50086,48112,52166,46333,47335,54225,49655,55479,52174,48877,48835,
34 46012,45729,48305,49892,47356,49132,54251,47555,50355,51895,50910,46069,51890,
35 48595,55258,48047,54974,47827,51928,46570,47524,50626,54458,52156,51879,52717,
36 50161,47088,54741,50112,49109,50366,47580,51172,51654,48636,51925,47102,49332,
37 47267,48840,53484,54968,50346,53460,51694,47858,47028,54240,50111,49353,52906,
38 45237,47086,49120,53458,48868,46330,51891,52714,49103,55283,52158,51421,45756,
39 47860,47574,41402,54703,45500,52195,
40 );
41  
42 # https://www-archive.mozilla.org/projects/intl/universalcharsetdetection
43 18 my $DIST_RATIO = 0.75;
44  
45 sub new {
46  
47 67 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
48  
49 67 my $self = {
50 Code => 0,
51 Left => 0,
52 Freqs => 0,
53 MBs => 0,
54 Total => 0,
55 Bad => 0,
56 };
57  
58 67 return bless $self, $class;
59  
60 }
61  
62 sub take {
63  
64 4434 ▶ my ($self, $bytes) = @_;
Subroutine take: called CC=10 SCAR=23.1
Pod undocumented
65  
66 4434 ▶ return TAKE_BAD if $self->{Bad};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Bad"} 0 4434 4434
67  
68 4434 for my $i (0 .. length($bytes) - 1) {
69 70492 my $b = ord(substr $bytes, $i, 1) & 0xff;
70 70492 ▶ if ($self->{Left}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'Left'}) { } 15629 54863 70492
elsif ($b >= 161 and $b <= 254) { } 15631 39232 54863
elsif ($b & 128) { } 9 39223 39232
71 15629 ▶ if ($b >= 0xa1 && $b <= 0xfe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b >= 161 and $b <= 254) { } 15584 45 15629
Condition66%2/3
$b >= 161 and $b <= 254
!ll&&!rl&&r
45015584
MC/DC50%1/2
$b >= 161 and $b <= 254
$b >= 161 $b <= 254
Truth table66%2/3
A and B
A= $b >= 161B= $b <= 254
ABresult
0X0
100
111
72 15584 $self->{Code} = ($self->{Code} << 8) | $b;
73 15584 $self->{Left}--;
74 15584 ▶ if (exists $GB2312_FREQS{ $self->{Code} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $GB2312_FREQS{$self->{"Code"}}) 4265 11319 15584
75 4265 $self->{Freqs}++;
76 }
77 15584 $self->{MBs}++;
78 15584 $self->{Total}++;
79 15584 $self->{Code} = 0;
80 } else {
81 45 $self->{Bad} = 1;
82 45 return TAKE_BAD;
83 }
84 ▶ } elsif ($b >= 0xa1 && $b <= 0xfe) {
Condition66%2/3
$b >= 161 and $b <= 254
!ll&&!rl&&r
39232015631
MC/DC50%1/2
$b >= 161 and $b <= 254
$b >= 161 $b <= 254
Truth table66%2/3
A and B
A= $b >= 161B= $b <= 254
ABresult
0X0
100
111
85 15631 $self->{Code} = $b;
86 15631 $self->{Left} = 1;
87 } elsif ($b & 0x80) {
88 9 $self->{Bad} = 1;
89 9 return TAKE_BAD;
90 } else {
91 39223 $self->{Total}++;
92 }
93 }
94  
95 4380 return TAKE_OK;
96  
97 }
98  
99 sub confidence {
100  
101 5 ▶ my ($self) = @_;
Subroutine confidence: called CC=3 SCAR=13.3
Pod undocumented
102  
103 5 ▶ if ($self->{Bad} or $self->{MBs} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Bad"} or $self->{"MBs"} == 0) 0 5 5
Condition33%1/3
$self->{"Bad"} or $self->{"MBs"} == 0
l!l&&r!l&&!r
005
MC/DC0%0/2
$self->{"Bad"} or $self->{"MBs"} == 0
$self->{"Bad"} $self->{"MBs"} == 0
Truth table33%1/3
A or B
A= $self->{"Bad"}B= $self->{"MBs"} == 0
ABresult
1X1
011
000
104 0 return 0;
105 }
106  
107 5 return $self->{Freqs} / $self->{MBs};
108  
109 }
110  
111 sub bad {
112  
113 0 ▶ my ($self) = @_;
Subroutine bad: not called CC=1 SCAR=6.9
Pod undocumented
114  
115 0 return $self->{Bad};
116  
117 }
118  
119 5 ▶sub encoding { 'GB2312' }
Subroutine encoding: called CC=1 SCAR=0.0
Pod undocumented
120  
121 18 1;
122  
« blib/lib/EBook/Ishmael/CharDet/EUCKR.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/ShiftJIS.pm »