blib/lib/Lingua/Charsets.pm

filter: Statementstmt 100% 34 / 34 Branchbran 66.6% 4 / 6 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 83.6% 51 / 61 CC 9 SCAR 15.3
CC
9
Cov
88%
_build_language_charsets
7.2
charsets_for
3.3
BEGIN
1.0
CRAP 4.6
↑ Summary
1 package Lingua::Charsets;
2 {
3 2 $Lingua::Charsets::VERSION = 0.04;
4 }
5  
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 has language_charsets => (
11 isa => 'HashRef',
12 is => 'ro',
13 lazy => 1,
14 builder => '_build_language_charsets',
15 );
16  
17 2 has language_charsets_raw_reference => (
18 isa => 'Str',
19 is => 'ro',
20 lazy => 1,
21 default => q[
22 af (Afrikaans) iso-8859-1 cp1252
23 sq (Albanian) iso-8859-1 cp1252 iso-8859-16
24 ar (Arabic) iso-8859-6 cp864 cp1256 MacArabic
25 eu (Basque) iso-8859-1 cp1252
26 bg (Bulgarian) iso-8859-5
27 be (Byelorussian) iso-8859-5
28 ca (Catalan) iso-8859-1 cp1252
29 zh (Chinese) gb2312 big5-eten gbk euc-cn
30 hr (Croatian) iso-8859-2 cp1250 MacCroatian iso-8859-16
31 cs (Czech) iso-8859-2
32 da (Danish) iso-8859-1 cp1252
33 nl (Dutch) iso-8859-1 cp1252, iso-8859-15
34 en (English) iso-8859-1 cp1252
35 eo (Esperanto) iso-8859-3*
36 et (Estonian) iso-8859-15 cp1257
37 fo (Faroese) iso-8859-1 cp1252
38 fi (Finnish) iso-8859-1 cp1252
39 fr (French) iso-8859-1 cp1252
40 gl (Galician) iso-8859-1 cp1252
41 de (German) iso-8859-1 cp1252
42 ka (Georgian) geostd8
43 el (Greek) iso-8859-7 cp869 cp1253 MacGreek
44 iw (Hebrew) iso-8859-8 cp1255 MacHebrew
45 hu (Hungarian) iso-8859-2 cp1250 iso-8859-16
46 is (Icelandic) iso-8859-1 cp1252 MacIceland iso-8859-10
47 id (Indonesian) iso-8859-1
48 ga (Irish) iso-8859-1 cp1252
49 it (Italian) iso-8859-1 cp1252
50 ja (Japanese) shiftjis iso-2022-jp euc-jp 7bit-jis iso-2022-jp-1 MacJapanese cp932 jis0201-raw jis0208-raw jis0212-raw
51 ko (Korean) euc-kr ksc5601-raw cp949 MacKorean johab iso-2022-kr
52 lv (Latvian) iso-8859-13 cp1257
53 lt (Lithuanian) iso-8859-13 cp1257
54 mk (Macedonian) iso-8859-5 cp1251
55 mt (Maltese) iso-8859-3*
56 no (Norwegian) iso-8859-1 cp1252
57 pl (Polish) iso-8859-2 cp852 cp1250 iso-8859-13 iso-8859-16
58 pt (Portuguese) iso-8859-1 cp1252
59 ro (Romanian) iso-8859-2 MacRomania
60 ru (Russian) koi8-r cp1251 cp866 iso-8859-5 x-mac-cyrillic
61 gd (Scottish) iso-8859-1 cp1252
62 sr (Serbian cyrillic) cp1251 iso-8859-5***
63 sr (Serbian latin) iso-8859-2 cp1250
64 sk (Slovak) iso-8859-2
65 sl (Slovenian) iso-8859-2 cp1250
66 es (Spanish) iso-8859-1 cp1252
67 sv (Swedish) iso-8859-1 cp1252
68 th (Thai) cp874 tis-620 MacThai iso-8859-11
69 tr (Turkish) iso-8859-9 cp857 cp1254 MacTurkish
70 uk (Ukrainian) iso-8859-5
71 vi (Vietnamese) cp1258
72 ]
73 );
74  
75 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 2 ▶sub charsets_for {
Subroutine charsets_for: called CC=3 SCAR=12.0
Pod documented
78 2 my ($self,$lang) = @_;
79 my $charsets
80 = $lang
81 2 ▶ ? $self->language_charsets->{ $lang }{ charsets } || []
Branch: 1 branch
BranchTrueFalseTotal
$lang ? : 2 0 2
Condition50%1/2
$self->language_charsets->{$lang}{"charsets"} or []
l!l
20
MC/DC0%0/1
$self->language_charsets->{$lang}{"charsets"} or []
$self->language_charsets->{$lang}{"charsets"}
Truth table50%1/2
A
A= $self->language_charsets->{$lang}{"charsets"}
Aresult
11
00
82 : [];
83 }
84  
85 1 ▶sub _build_language_charsets {
Subroutine _build_language_charsets: called CC=7 SCAR=19.7
86 1 my $self = shift;
87 1 my $dict = {};
88 1 my $data = $self->language_charsets_raw_reference;
89 1 ▶ my @lines = split /\n/, $data||'';
Condition50%1/2
$data || ''
l!l
10
MC/DC0%0/1
$data || ''
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
90 1 for my $line ( @lines ) {
91 51 ▶ next unless $line = join ' ', split ' ', $line||'';
Branch: 1 branch
BranchTrueFalseTotal
unless $line = join(" ", split(' ', $line || "", 0)) 1 50 51
Condition100%2/2
$line || ''
l!l
501
MC/DC100%1/1
$line || ''
$line
Truth table100%2/2
A
A= $line
Aresult
11
00
92 50 $line =~ s|\([^\(\)]+\)| |g;
93 50 $line =~ s|\*||g;
94 50 my @parts = split ' ', $line;
95 50 my $lang = shift @parts;
96 50 ▶ if ( $lang =~ m|\A[a-z]{2}\Z| && @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang =~ /\A[a-z]{2}\Z/ and @parts) 50 0 50
Condition33%1/3
$lang =~ /\A[a-z]{2}\Z/ and @parts
!ll&&!rl&&r
0050
MC/DC0%0/2
$lang =~ /\A[a-z]{2}\Z/ and @parts
$lang =~ /\A[a-z]{2}\Z/ @parts
Truth table33%1/3
A and B
A= $lang =~ /\A[a-z]{2}\Z/B= @parts
ABresult
0X0
100
111
97 50 push @{ $dict->{ $lang }{charsets} }, @parts;
98 }
99 }
100 1 return $dict;
101 }
102  
103 2 __PACKAGE__->meta->make_immutable;
104 2 1;
105  
106 # ABSTRACT: Language-based charset reference
107  
108 =head1 NAME
109  
110 Lingua::Charsets - Provides a list of charsets by language.
111  
112 =head1 SYNOPSIS
113  
114 use Lingua::Charsets;
115  
116 my $lc = Lingua::Charsets->new;
117  
118 my $zh_charsets = $lc->charsets_for( 'zh' );
119  
120 =head1 DESCRIPTION
121  
122 This module provides a listing of charsets by language. It can be used
123 in conjunction with a charset guessing algorithm such as Encode::Guess.
124  
125 The data used comes from a variety of locations; however, the initial
126 source was The W3C charset listing available at:
127  
128 L<http://www.w3.org/International/O-charset-lang.html>
129  
130 =head1 METHODS
131  
132 =head2 language_charsets
133  
134 Returns a HashRef consisting of ISO-639-1 language codes as hashref keys.
135 The hashref values are hashrefs with the key 'charsets' that consists
136 of an arrayref of charsets.
137  
138 =head2 charsets_for( $iso_639_1_code )
139  
140 Returns an ArrayRef[Str] of charsets for the language's ISO-639-1 code. An
141 empty ArrayRef is returned if the language isn't found.
142  
143 =head1 SEE ALSO
144  
145 L<Encode::Guess>, L<Encode::JP>, L<Encode::KR>, L<Lingua::RU::Charset>
146  
147 =head1 REFERENCES
148  
149 W3.org "I18N languages, countries and character sets" page at:
150  
151 L<http://www.w3.org/International/O-charset-lang.html>
152  
153 Additional information can be found at:
154  
155 L<http://aspell.net/charsets/>
156  
157 L<http://en.wikipedia.org/wiki/Character_encoding>
158  
159 L<http://www.mnogosearch.org/doc/msearch-international.html>
160  
161 Aliases are listed in the mnoGoSearch webpage; however, they are not
162 currently included as aliases would not be useful for decoding.
163 They may be added as a separate reference in the future if they are
164 deemed useful.
165  
166 =head1 AUTHOR
167  
168 John Wang E<lt>johncwang@gmail.comE<gt>
169  
170 =head1 COPYRIGHT
171  
172 Copyright 2013 by John Wang E<lt>johncwang@gmail.comE<gt>.
173  
174 This software is released under the MIT license cited below.
175  
176 =head2 The "MIT" License
177  
178 Permission is hereby granted, free of charge, to any person obtaining a copy
179 of this software and associated documentation files (the "Software"), to deal
180 in the Software without restriction, including without limitation the rights
181 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
182 copies of the Software, and to permit persons to whom the Software is
183 furnished to do so, subject to the following conditions:
184  
185 The above copyright notice and this permission notice shall be included in
186 all copies or substantial portions of the Software.
187  
188 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
189 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
190 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
191 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
193 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
194 DEALINGS IN THE SOFTWARE.
195  
196 =cut
↑ Summary