| 1 | package EBook::Ishmael::CharDet; | ||||||||||||||||||||||||||||||||
| 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 Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 18 | our @EXPORT = qw(chardet); | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use EBook::Ishmael::CharDet::Constants qw(:CONSTANTS); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use EBook::Ishmael::CharDet::Big5; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use EBook::Ishmael::CharDet::CP1250; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use EBook::Ishmael::CharDet::CP1251; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 18 | ▶ | use EBook::Ishmael::CharDet::CP1252; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 18 | ▶ | use EBook::Ishmael::CharDet::CP1253; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 18 | ▶ | use EBook::Ishmael::CharDet::CP1254; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 18 | ▶ | use EBook::Ishmael::CharDet::CP1255; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 18 | ▶ | use EBook::Ishmael::CharDet::CP1256; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 18 | ▶ | use EBook::Ishmael::CharDet::EUCJP; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 18 | ▶ | use EBook::Ishmael::CharDet::EUCKR; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 18 | ▶ | use EBook::Ishmael::CharDet::GB2312; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | 18 | ▶ | use EBook::Ishmael::CharDet::HZ; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 18 | ▶ | use EBook::Ishmael::CharDet::ISO2022JP; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 18 | ▶ | use EBook::Ishmael::CharDet::ISO2022KR; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | 18 | ▶ | use EBook::Ishmael::CharDet::ISO88595; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | 18 | ▶ | use EBook::Ishmael::CharDet::ShiftJIS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | 18 | ▶ | use EBook::Ishmael::CharDet::UTF8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # Based encoding detection algorithm off of the chardet python library: | ||||||||||||||||||||||||||||||||
| 30 | # - https://chardet.readthedocs.io/en/latest/ | ||||||||||||||||||||||||||||||||
| 31 | # These particular documents were especially useful: | ||||||||||||||||||||||||||||||||
| 32 | # - https://chardet.readthedocs.io/en/latest/how-it-works.html | ||||||||||||||||||||||||||||||||
| 33 | # - https://www-archive.mozilla.org/projects/intl/universalcharsetdetection | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # TODO: Optimize performance | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | # TODO: UTF16{BE,LE} support? | ||||||||||||||||||||||||||||||||
| 38 | # TODO: UTF32{BE,LE} support? | ||||||||||||||||||||||||||||||||
| 39 | # TODO: ISO-8859-* support? | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | sub has_high_bit { | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 90 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
has_high_bit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 90 | return $str =~ /[\x80-\xff]/; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | sub chardet_bom { | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 90 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet_bom: called CC=6 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 90 | ▶ | if ($str =~ /^\xef\xbb\xbf/) { | ||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||
| 54 | 0 | return 'UTF-8'; | |||||||||||||||||||||||||||||||
| 55 | } elsif ($str =~ /^\xfe\xff/) { | ||||||||||||||||||||||||||||||||
| 56 | 0 | return 'UTF-16BE'; | |||||||||||||||||||||||||||||||
| 57 | } elsif ($str =~ /^\xff\xfe/) { | ||||||||||||||||||||||||||||||||
| 58 | 0 | return 'UTF-16LE'; | |||||||||||||||||||||||||||||||
| 59 | } elsif ($str =~ /^\x00\x00\xfe\xff/) { | ||||||||||||||||||||||||||||||||
| 60 | 0 | return 'UTF-32BE'; | |||||||||||||||||||||||||||||||
| 61 | # This could be wrong, as this could also be UTF-16LE starting with a | ||||||||||||||||||||||||||||||||
| 62 | # null character, but I believe its most likely to be a UTF-32. | ||||||||||||||||||||||||||||||||
| 63 | } elsif ($str =~ /^\xff\xfe\x00\x00/) { | ||||||||||||||||||||||||||||||||
| 64 | 0 | return 'UTF-32LE'; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 90 | return undef; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | sub chardet_utf16 { | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 90 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet_utf16: called CC=11 SCAR=28.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 90 | ▶ | if (length($str) % 2 != 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 51 | return undef; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 39 | ▶ | my $len = length $str > 1024 ? 1024 : length $str; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 39 | my $leading_null = 0; # UTF-16BE | |||||||||||||||||||||||||||||||
| 82 | 39 | my $trailling_null = 0; # UTF-16LE | |||||||||||||||||||||||||||||||
| 83 | 39 | my $none = 0; | |||||||||||||||||||||||||||||||
| 84 | 39 | ▶ | for (my $i = 0; $i < $len; $i += 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 19968 | my $char = substr $str, $i, 2; | |||||||||||||||||||||||||||||||
| 86 | 19968 | my $got_null = 0; | |||||||||||||||||||||||||||||||
| 87 | 19968 | ▶ | if ($char =~ /^\0/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 0 | $leading_null++; | |||||||||||||||||||||||||||||||
| 89 | 0 | $got_null = 1; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | 19968 | ▶ | if ($char =~ /\0$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 0 | $trailling_null++; | |||||||||||||||||||||||||||||||
| 93 | 0 | $got_null = 1; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | 19968 | ▶ | if (!$got_null) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 19968 | $none++; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 39 | ▶ | if ($leading_null > $trailling_null && $leading_null > $none) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$leading_null > $trailling_null and $leading_null > $none
MC/DC0%0/2
$leading_null > $trailling_null and $leading_null > $none
$leading_null > $trailling_null
$leading_null > $none
Truth table33%1/3
A and B
A= $leading_null > $trailling_nullB= $leading_null > $none
| |||||||||||||||||||||||||||||||||
| 101 | 0 | return 'UTF-16BE'; | |||||||||||||||||||||||||||||||
| 102 | ▶ | } elsif ($trailling_null > $leading_null && $trailling_null > $none) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$trailling_null > $leading_null and $trailling_null > $none
MC/DC0%0/2
$trailling_null > $leading_null and $trailling_null > $none
$trailling_null > $leading_null
$trailling_null > $none
Truth table33%1/3
A and B
A= $trailling_null > $leading_nullB= $trailling_null > $none
| |||||||||||||||||||||||||||||||||
| 103 | 0 | return 'UTF-16LE'; | |||||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||||||
| 105 | 39 | return undef; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | sub chardet_utf32 { | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 90 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet_utf32: called CC=11 SCAR=28.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 90 | ▶ | if (length($str) % 4 != 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 71 | return undef; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 19 | ▶ | my $len = length $str > 1024 ? 1024 : length $str; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 19 | my $leading_null = 0; # UTF-32BE | |||||||||||||||||||||||||||||||
| 121 | 19 | my $trailling_null = 0; # UTF-32LE | |||||||||||||||||||||||||||||||
| 122 | 19 | my $none = 0; | |||||||||||||||||||||||||||||||
| 123 | 19 | ▶ | for (my $i = 0; $i < $len; $i += 4) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 4864 | my $char = substr $str, $i, 4; | |||||||||||||||||||||||||||||||
| 125 | 4864 | my $got_null = 0; | |||||||||||||||||||||||||||||||
| 126 | 4864 | ▶ | if ($char =~ /^\0\0\0/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | 0 | $leading_null++; | |||||||||||||||||||||||||||||||
| 128 | 0 | $got_null = 1; | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | 4864 | ▶ | if ($char =~ /\0\0\0$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 0 | $trailling_null++; | |||||||||||||||||||||||||||||||
| 132 | 0 | $got_null = 1; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | 4864 | ▶ | if (!$got_null) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | 4864 | $none++; | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 19 | ▶ | if ($leading_null > $trailling_null && $leading_null > $none) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$leading_null > $trailling_null and $leading_null > $none
MC/DC0%0/2
$leading_null > $trailling_null and $leading_null > $none
$leading_null > $trailling_null
$leading_null > $none
Truth table33%1/3
A and B
A= $leading_null > $trailling_nullB= $leading_null > $none
| |||||||||||||||||||||||||||||||||
| 140 | 0 | return 'UTF-32BE'; | |||||||||||||||||||||||||||||||
| 141 | ▶ | } elsif ($trailling_null > $leading_null && $trailling_null > $none) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$trailling_null > $leading_null and $trailling_null > $none
MC/DC0%0/2
$trailling_null > $leading_null and $trailling_null > $none
$trailling_null > $leading_null
$trailling_null > $none
Truth table33%1/3
A and B
A= $trailling_null > $leading_nullB= $trailling_null > $none
| |||||||||||||||||||||||||||||||||
| 142 | 0 | return 'UTF-32LE'; | |||||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||||||
| 144 | 19 | return undef; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | sub chardet_with { | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 157 | ▶ | my ($str, @guessers) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet_with: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 157 | ▶ | my $len = length $str > 8192 ? 8192 : length $str; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 157 | ▶ | for (my $i = 0; $i < $len; $i += 16) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 156 | 63023 | my $c = substr $str, $i, 16; | |||||||||||||||||||||||||||||||
| 157 | 63023 | for my $j (0 .. $#guessers) { | |||||||||||||||||||||||||||||||
| 158 | 346011 | ▶ | if ($j > $#guessers) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | 91 | last; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | 345920 | my $take = $guessers[$j]->take($c); | |||||||||||||||||||||||||||||||
| 162 | 345920 | ▶ | if ($take == TAKE_MUST_BE) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 163 | 39 | return ([ $guessers[$j]->encoding, 1.0 ]); | |||||||||||||||||||||||||||||||
| 164 | } elsif ($take == TAKE_BAD) { | ||||||||||||||||||||||||||||||||
| 165 | 218 | splice @guessers, $j, 1; | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | 62984 | ▶ | if (!@guessers) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | 0 | return (); | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 118 | return map { [ $_->encoding, $_->confidence ] } @guessers; | |||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | sub chardet_7bit { | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 23 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet_7bit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 23 | my @guesses = chardet_with( | |||||||||||||||||||||||||||||||
| 182 | $str, | ||||||||||||||||||||||||||||||||
| 183 | EBook::Ishmael::CharDet::HZ->new, | ||||||||||||||||||||||||||||||||
| 184 | EBook::Ishmael::CharDet::ISO2022JP->new, | ||||||||||||||||||||||||||||||||
| 185 | EBook::Ishmael::CharDet::ISO2022KR->new, | ||||||||||||||||||||||||||||||||
| 186 | ); | ||||||||||||||||||||||||||||||||
| 187 | 23 | push @guesses, [ 'ASCII', 0.75 ]; | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 23 | return @guesses; | |||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | sub chardet_multibyte { | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 67 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet_multibyte: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 67 | return chardet_with( | |||||||||||||||||||||||||||||||
| 198 | $str, | ||||||||||||||||||||||||||||||||
| 199 | EBook::Ishmael::CharDet::UTF8->new, | ||||||||||||||||||||||||||||||||
| 200 | EBook::Ishmael::CharDet::Big5->new, | ||||||||||||||||||||||||||||||||
| 201 | EBook::Ishmael::CharDet::ShiftJIS->new, | ||||||||||||||||||||||||||||||||
| 202 | EBook::Ishmael::CharDet::EUCJP->new, | ||||||||||||||||||||||||||||||||
| 203 | EBook::Ishmael::CharDet::EUCKR->new, | ||||||||||||||||||||||||||||||||
| 204 | EBook::Ishmael::CharDet::GB2312->new, | ||||||||||||||||||||||||||||||||
| 205 | ); | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | sub chardet_singlebyte { | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 67 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet_singlebyte: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 67 | return chardet_with( | |||||||||||||||||||||||||||||||
| 214 | $str, | ||||||||||||||||||||||||||||||||
| 215 | EBook::Ishmael::CharDet::CP1250->new, | ||||||||||||||||||||||||||||||||
| 216 | EBook::Ishmael::CharDet::CP1251->new, | ||||||||||||||||||||||||||||||||
| 217 | EBook::Ishmael::CharDet::CP1252->new, | ||||||||||||||||||||||||||||||||
| 218 | EBook::Ishmael::CharDet::CP1253->new, | ||||||||||||||||||||||||||||||||
| 219 | EBook::Ishmael::CharDet::CP1254->new, | ||||||||||||||||||||||||||||||||
| 220 | EBook::Ishmael::CharDet::CP1255->new, | ||||||||||||||||||||||||||||||||
| 221 | EBook::Ishmael::CharDet::CP1256->new, | ||||||||||||||||||||||||||||||||
| 222 | EBook::Ishmael::CharDet::ISO88595->new, | ||||||||||||||||||||||||||||||||
| 223 | ); | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | sub chardet { | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 90 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
chardet: called CC=7 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | 90 | my $char = chardet_bom($str); | |||||||||||||||||||||||||||||||
| 232 | 90 | ▶ | if (defined $char) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 233 | 0 | return $char; | |||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 90 | $char = chardet_utf16($str); | |||||||||||||||||||||||||||||||
| 237 | 90 | ▶ | if (defined $char) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 0 | return $char; | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | 90 | $char = chardet_utf32($str); | |||||||||||||||||||||||||||||||
| 242 | 90 | ▶ | if (defined $char) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | 0 | return $char; | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 90 | my @chars; | |||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | 90 | ▶ | if (!has_high_bit($str)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 249 | 23 | @chars = sort { $b->[1] <=> $a->[1] } chardet_7bit($str); | |||||||||||||||||||||||||||||||
| 250 | 23 | ▶ | return @chars > 0 ? $chars[0]->[0] : 'ASCII'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | 67 | @chars = chardet_multibyte($str); | |||||||||||||||||||||||||||||||
| 254 | 67 | push @chars, chardet_singlebyte($str); | |||||||||||||||||||||||||||||||
| 255 | 67 | @chars = sort { $b->[1] <=> $a->[1] } @chars; | |||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 67 | ▶ | return @chars > 0 ? $chars[0]->[0] : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | 18 | 1; | |||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||
| 267 | EBook::Ishmael::CharDet - Guess the character encoding of given text | ||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | use EBook::Ishmael::CharDet; | ||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | use Encode qw(encode); | ||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||
| 275 | # $encoding should be 'CP1250' | ||||||||||||||||||||||||||||||||
| 276 | my $encoding = chardet(encode('CP1250', 'Obecná veřejná')); | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | B<EBook::Ishmael::CharDet> is a module that provides the C<chardet()> subroutine | ||||||||||||||||||||||||||||||||
| 281 | which guesses character encoding of given text. This is a private module, | ||||||||||||||||||||||||||||||||
| 282 | please consult the L<ishmael> manual for user documentation. | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | =over 4 | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | =item $encoding = chardet($text) | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | Guesses the encoding for the encoded text C<$text> through a series of | ||||||||||||||||||||||||||||||||
| 291 | heuristics. If C<chardet()> cannot come to a conclusion, C<undef> is returned. | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | The follow encodings are supported so far: | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | =over 2 | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | =item ASCII | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | =item UTF-8 | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | =item UTF-16BE | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | =item UTF-16LE | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | =item UTF-32BE | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | =item UTF-32LE | ||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | =item GB2312 | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | =item CP1250 | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | =item CP1251 | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | =item CP1252 | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | =item CP1253 | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | =item CP1254 | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | =item CP1255 | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | =item CP1256 | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | =item HZ | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | =item ISO-2022-JP | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | =item ISO-2022-KR | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | =item ISO-8859-5 | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | =item EUC-JP | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | =item EUC-KR | ||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | =item Big5 | ||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | =item Shift_JIS | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | =back | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | =back | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>. | ||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | This project's source can be found on its | ||||||||||||||||||||||||||||||||
| 350 | L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull | ||||||||||||||||||||||||||||||||
| 351 | requests are welcome! | ||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | Copyright (C) 2025-2026 Samuel Young | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||
| 358 | it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||
| 359 | the Free Software Foundation, either version 3 of the License, or | ||||||||||||||||||||||||||||||||
| 360 | (at your option) any later version. | ||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | =cut | ||||||||||||||||||||||||||||||||