blib/lib/Unicode/Collate/Locale.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 89.2% 25 / 28 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 9 / 9 Podpod 66.6% 2 / 3 totaltotal 95.2% 119 / 125 CC 18 SCAR 19.6
CC
18
Cov
95%
_locale
12.0
new
5.2
_fetchpl
3.0
CRAP 7.1
« blib/lib/Unicode/Collate.pm ↑ Summary blib/lib/Unicode/Collate/CJK/Big5.pm »
1 package Unicode::Collate::Locale;
2  
3 102 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 102 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 102 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 102 ▶use base qw(Unicode::Collate);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 102 our $VERSION = '1.31';
9  
10 102 my $PL_EXT = '.pl';
11  
12 102 my %LocaleFile = map { ($_, $_) } qw(
13 af ar as az be bn ca cs cu cy da dsb ee eo es et fa fi fil fo gu
14 ha haw he hi hr hu hy ig is ja kk kl kn ko kok lkt ln lt lv
15 mk ml mr mt nb nn nso om or pa pl ro sa se si sk sl sq sr sv
16 ta te th tn to tr uk ur vi vo wae wo yo zh
17 );
18 102 $LocaleFile{'default'} = '';
19 # aliases
20 102 $LocaleFile{'bs'} = 'hr';
21 102 $LocaleFile{'bs_Cyrl'} = 'sr';
22 102 $LocaleFile{'sr_Latn'} = 'hr';
23 # short file names
24 102 $LocaleFile{'de__phonebook'} = 'de_phone';
25 102 $LocaleFile{'de_AT_phonebook'} = 'de_at_ph';
26 102 $LocaleFile{'es__traditional'} = 'es_trad';
27 102 $LocaleFile{'fr_CA'} = 'fr_ca';
28 102 $LocaleFile{'fi__phonebook'} = 'fi_phone';
29 102 $LocaleFile{'si__dictionary'} = 'si_dict';
30 102 $LocaleFile{'sv__reformed'} = 'sv_refo';
31 102 $LocaleFile{'ug_Cyrl'} = 'ug_cyrl';
32 102 $LocaleFile{'zh__big5han'} = 'zh_big5';
33 102 $LocaleFile{'zh__gb2312han'} = 'zh_gb';
34 102 $LocaleFile{'zh__pinyin'} = 'zh_pin';
35 102 $LocaleFile{'zh__stroke'} = 'zh_strk';
36 102 $LocaleFile{'zh__zhuyin'} = 'zh_zhu';
37  
38 102 my %TypeAlias = qw(
39 phone phonebook
40 phonebk phonebook
41 dict dictionary
42 reform reformed
43 trad traditional
44 big5 big5han
45 gb2312 gb2312han
46 );
47  
48 135 ▶sub _locale {
Subroutine _locale: called CC=12 SCAR=24.8
49 135 my $locale = shift;
50 135 ▶ if ($locale) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale) 133 2 135
51 133 $locale = lc $locale;
52 133 $locale =~ tr/\-\ \./_/;
53 133 ▶ $locale =~ s/_([0-9a-z]+)\z/$TypeAlias{$1} ?
Branch: 1 branch
BranchTrueFalseTotal
$TypeAlias{$1} ? : 10 32 42
54 "_$TypeAlias{$1}" : "_$1"/e;
55 133 ▶ $LocaleFile{$locale} and return $locale;
Branch: 1 branch
BranchTrueFalseTotal
$LocaleFile{$locale} and return $locale 99 34 133
56  
57 34 my @code = split /_/, $locale;
58 34 my $lan = shift @code;
59 34 ▶ my $scr = @code && length $code[0] == 4 ? ucfirst shift @code : '';
Branch: 1 branch
BranchTrueFalseTotal
@code && length $code[0] == 4 ? : 9 25 34
Condition100%3/3
@code and length $code[0] == 4
!ll&&!rl&&r
9169
MC/DC100%2/2
@code and length $code[0] == 4
@code length $code[0] == 4
Truth table100%3/3
A and B
A= @codeB= length $code[0] == 4
ABresult
0X0
100
111
60 34 ▶ my $reg = @code && length $code[0] < 4 ? uc shift @code : '';
Branch: 1 branch
BranchTrueFalseTotal
@code && length $code[0] < 4 ? : 10 24 34
Condition100%3/3
@code and length $code[0] < 4
!ll&&!rl&&r
17710
MC/DC100%2/2
@code and length $code[0] < 4
@code length $code[0] < 4
Truth table100%3/3
A and B
A= @codeB= length $code[0] < 4
ABresult
0X0
100
111
61 34 ▶ my $var = @code ? shift @code : '';
Branch: 1 branch
BranchTrueFalseTotal
@code ? : 12 22 34
62  
63 34 my @list;
64 34 ▶ push @list, (
Branch: 1 branch
BranchTrueFalseTotal
if $var ne "" 12 22 34
65 "${lan}_${scr}_${reg}_$var",
66 "${lan}_${scr}__$var", # empty $scr should not be ${lan}__$var.
67 "${lan}_${reg}_$var", # empty $reg may be ${lan}__$var.
68 "${lan}__$var",
69 ) if $var ne '';
70 34 push @list, (
71 "${lan}_${scr}_${reg}",
72 "${lan}_${scr}",
73 "${lan}_${reg}",
74 ${lan},
75 );
76 34 for my $loc (@list) {
77 113 ▶ $LocaleFile{$loc} and return $loc;
Branch: 1 branch
BranchTrueFalseTotal
$LocaleFile{$loc} and return $loc 22 91 113
78 }
79 }
80 14 return 'default';
81 }
82  
83 122 ▶sub getlocale {
Subroutine getlocale: called CC=1 SCAR=0.0
Pod documented
84 122 return shift->{accepted_locale};
85 }
86  
87 3 ▶sub locale_version {
Subroutine locale_version: called CC=1 SCAR=0.0
Pod documented
88 3 return shift->{locale_version};
89 }
90  
91 123 ▶sub _fetchpl {
Subroutine _fetchpl: called CC=3 SCAR=11.0
92 123 my $accepted = shift;
93 123 my $f = $LocaleFile{$accepted};
94 123 ▶ return if !$f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 12 111 123
95 111 $f .= $PL_EXT;
96  
97 # allow to search @INC
98 # use File::Spec;
99 # my $path = File::Spec->catfile('Unicode', 'Collate', 'Locale', $f);
100 111 my $path = "Unicode/Collate/Locale/$f";
101 111 my $h = do $path;
102 111 ▶ croak "Unicode/Collate/Locale/$f can't be found" if !$h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 111 111
103 111 return $h;
104 }
105  
106 123 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod undocumented
107 123 my $class = shift;
108 123 my %hash = @_;
109 123 $hash{accepted_locale} = _locale($hash{locale});
110  
111 123 ▶ if (exists $hash{table}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash{"table"}) 0 123 123
112 0 croak "your table can't be used with Unicode::Collate::Locale";
113 }
114  
115 123 my $href = _fetchpl($hash{accepted_locale});
116 123 ▶ while (my($k,$v) = each %$href) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$href) 251 123 374
117 251 ▶ if (!exists $hash{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $hash{$k}) { } 250 1 251
elsif ($k eq 'entry') { } 1 0 1
118 250 $hash{$k} = $v;
119 } elsif ($k eq 'entry') {
120 1 $hash{$k} = $v.$hash{$k};
121 } else {
122 0 croak "$k is reserved by $hash{locale}, can't be overwritten";
123 }
124 }
125 123 return $class->SUPER::new(%hash);
126 }
127  
128 102 1;
129 __END__
« blib/lib/Unicode/Collate.pm ↑ Summary blib/lib/Unicode/Collate/CJK/Big5.pm »