Recode.pm

filter: Statementstmt 59.3% 38 / 64 Branchbran 45.0% 9 / 20 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 4 / 7 Podpod 66.6% 4 / 6 totaltotal 53.2% 57 / 107 CC 16 SCAR 20.3
CC
16
Cov
54%
getCharsets
12.0
new
10.0
recode
5.8
CRAP 7.6
« TextDomain.pm ↑ Summary gettext_dumb.pm »
1 #! /bin/false
2  
3 # vim: set autoindent shiftwidth=4 tabstop=4:
4  
5 # Portable character conversion for Perl.
6 # Copyright (C) 2002-2026 Guido Flohr <guido.flohr@cantanea.com>,
7 # all rights reserved.
8  
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13  
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18  
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21  
22 package Locale::Recode;
23  
24 161 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 161 require Locale::Recode::_Conversions;
27  
28 161 my $loaded = {};
29 161 my $has_encode;
30  
31 sub new
32 549 ▶{
Subroutine new: called CC=8 SCAR=23.1
Pod undocumented
33 549 ▶ my $class = ref($_[0]) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
05490
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
34 549 shift;
35 549 my %args = @_;
36  
37 549 my $self = bless {}, $class;
38  
39 549 my ($from_codeset, $to_codeset) = @args{qw (from to)};
40
41 549 ▶ unless ($from_codeset && $to_codeset) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from_codeset and $to_codeset) 0 549 549
Condition33%1/3
$from_codeset and $to_codeset
!ll&&!rl&&r
00549
MC/DC0%0/2
$from_codeset and $to_codeset
$from_codeset $to_codeset
Truth table33%1/3
A and B
A= $from_codesetB= $to_codeset
ABresult
0X0
100
111
42 0 require Carp;
43 0 Carp::croak (<<EOF);
44 Usage: $class->new (from => FROM_CODESET, to => TO_CODESET);
45 EOF
46 }
47  
48 # Find a conversion path.
49 549 my $path = Locale::Recode::_Conversions->findPath ($from_codeset,
50 $to_codeset);
51 549 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 549 549
52 0 $self->{__error} = 'EINVAL';
53 0 return $self;
54 }
55  
56 549 my @conversions = ();
57 549 foreach (@$path) {
58 549 my ($module, $from, $to) = @$_;
59
60 549 ▶ unless ($loaded->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded->{$module}) 139 410 549
61 139 eval "require Locale::RecodeData::$module";
62 139 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 139 139
63 0 $self->{__error} = $@;
64 0 return $self;
65 }
66
67 139 $loaded->{$module} = 1;
68 }
69
70 549 my $module_name = "Locale::RecodeData::$module";
71 549 my $method = 'new';
72 549 my $object = $module_name->$method (from => $from,
73 to => $to);
74
75 549 push @conversions, $object;
76 }
77  
78 549 $self->{__conversions} = \@conversions;
79
80 549 return $self;
81 }
82  
83 sub resolveAlias
84 0 ▶{
Subroutine resolveAlias: not called CC=1 SCAR=6.9
Pod undocumented
85 0 my ($class, $alias) = @_;
86  
87 0 return Locale::Recode::_Conversions->resolveAlias ($alias);
88 }
89  
90 sub getSupported
91 0 ▶{
Subroutine getSupported: not called CC=1 SCAR=6.9
Pod documented
92 0 return [ Locale::Recode::_Conversions->listSupported ];
93 }
94  
95 sub getCharsets
96 0 ▶{
Subroutine getCharsets: not called CC=3 SCAR=24.8
Pod documented
97 0 my $self = shift;
98 0 my %all = map { $_ => 1 } @{&getSupported};
99  
100 0 require Locale::Recode::_Aliases;
101  
102 0 my $conversions = Locale::Recode::_Conversions->listSupported;
103 0 foreach my $charset (keys %{Locale::Recode::_Aliases::ALIASES()}) {
104 0 my $mime_name = $self->resolveAlias ($charset);
105 0 ▶ next unless exists $all{$mime_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $all{$mime_name} 0 0 0
106 0 $all{$charset} = 1;
107 }
108
109 0 return [ keys %all ];
110 }
111  
112 sub recode
113 199610 ▶{
Subroutine recode: called CC=5 SCAR=17.5
Pod documented
114 199610 my $self = $_[0];
115  
116 199610 ▶ return if $self->{__error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__error"} 0 199610 199610
117  
118 199610 ▶ return 1 unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 199610 199610
119  
120 199610 my $chain = $self->{__conversions};
121
122 199610 foreach my $module (@$chain) {
123 199610 my $success = $module->_recode ($_[1]);
124
125 199610 ▶ unless ($success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 199610 199610
126 0 $self->{__error} = $module->_getError;
127 0 return;
128 }
129 }
130  
131 199610 return 1;
132 }
133  
134 sub getError
135 412 ▶{
Subroutine getError: called CC=3 SCAR=17.3
Pod documented
136 412 my $self = shift;
137 412 ▶ my $error = $self->{__error} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $error = $self->{"__error"} or return 0 412 412
138  
139 0 ▶ if ('EINVAL' eq $error) {
Branch: 1 branch
BranchTrueFalseTotal
if ('EINVAL' eq $error) { } 0 0 0
140 0 return 'Invalid conversion';
141 } else {
142 0 return $error;
143 }
144 }
145  
146 161 1;
147  
148 __END__
« TextDomain.pm ↑ Summary gettext_dumb.pm »