blib/lib/MARC/Charset/Code.pm

filter: Statementstmt 100% 50 / 50 Branchbran 88.8% 16 / 18 Conditioncond 82.4% 47 / 57 MC/DCmcdc 69.5% 16 / 23 Subroutinesub 100% 16 / 16 Podpod 90.0% 9 / 10 totaltotal 88.5% 154 / 174 CC 29 SCAR 22.0
CC
29
Cov
90%
get_escape
16.4
default_charset_group
11.1
marc_value
3.0
CRAP 9.0
« blib/lib/MARC/Charset.pm ↑ Summary blib/lib/MARC/Charset/Compiler.pm »
1 package MARC::Charset::Code;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use MARC::Charset::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 MARC::Charset::Code
11 ->mk_accessors(qw(marc ucs name charset is_combining alt
12 marc_right_half marc_left_half));
13  
14 =head1 NAME
15  
16 MARC::Charset::Code - represents a MARC-8/UTF-8 mapping
17  
18 =head1 SYNOPSIS
19  
20 =head1 DESCRIPTION
21  
22 Each mapping from a MARC-8 value to a UTF-8 value is represented by
23 a MARC::Charset::Code object in a MARC::Charset::Table.
24  
25 =head1 METHODS
26  
27 =head2 new()
28  
29 The constructor.
30  
31 =head2 name()
32  
33 A descriptive name for the code point.
34  
35 =head2 marc()
36  
37 A string representing the MARC-8 bytes codes.
38  
39 =head2 ucs()
40  
41 A string representing the UCS code point in hex.
42  
43 =head2 charset_code()
44  
45 The MARC-8 character set code.
46  
47 =head2 is_combining()
48  
49 Returns true/false to tell if the character is a combining character.
50  
51 =head2 marc_left_half()
52  
53 If the character is the right half of a "double diacritic", returns
54 a hex string representing the MARC-8 value of the left half.
55  
56 =head2 marc_right_half()
57  
58 If the character is the left half of a "double diacritic", returns
59 a hex string representing the MARC-8 value of the right half.
60  
61 =head2 to_string()
62  
63 A stringified version of the object suitable for pretty printing.
64  
65 =head2 char_value()
66  
67 Returns the unicode character. Essentially just a helper around
68 ucs().
69  
70 =cut
71  
72 sub char_value
73 391 ▶{
Subroutine char_value: called CC=1 SCAR=0.0
Pod documented
74 391 return chr(hex(shift->ucs()));
75 }
76  
77 =head2 g0_marc_value()
78  
79 The string representing the MARC-8 encoding
80 for lookup.
81  
82 =cut
83  
84 sub g0_marc_value
85 16411 ▶{
Subroutine g0_marc_value: called CC=2 SCAR=6.9
Pod documented
86 16411 my $code = shift;
87 16411 my $marc = $code->marc();
88 16411 ▶ if ($code->charset_name eq 'CJK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->charset_name eq 'CJK') { } 15747 664 16411
89 return
90 15747 chr(hex(substr($marc,0,2))) .
91 chr(hex(substr($marc,2,2))) .
92 chr(hex(substr($marc,4,2)));
93 } else {
94 664 return chr(hex($marc));
95 }
96 }
97  
98 =head2 marc_value()
99  
100 The string representing the MARC-8 encodingA
101 for output.
102  
103 =cut
104  
105 sub marc_value
106 132 ▶{
Subroutine marc_value: called CC=3 SCAR=11.0
Pod documented
107 132 my $code = shift;
108 132 my $marc = $code->marc();
109 132 ▶ if ($code->charset_name eq 'CJK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->charset_name eq 'CJK') { } 1 131 132
110 return
111 1 chr(hex(substr($marc,0,2))) .
112 chr(hex(substr($marc,2,2))) .
113 chr(hex(substr($marc,4,2)));
114 } else {
115 131 ▶ if ($code->default_charset_group() eq 'G0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->default_charset_group eq 'G0') { } 105 26 131
116 105 return chr(hex($marc));
117 } else {
118 26 return chr(hex($marc) + 128);
119 }
120 }
121 }
122  
123  
124 =head2 charset_name()
125  
126 Returns the name of the character set, instead of the code.
127  
128 =cut
129  
130 sub charset_name
131 16545 ▶{
Subroutine charset_name: called CC=1 SCAR=0.0
Pod documented
132 16545 return MARC::Charset::Constants::charset_name(shift->charset_value());
133 }
134  
135 =head2 to_string()
136  
137 Returns a stringified version of the object.
138  
139 =cut
140  
141 sub to_string
142 8 ▶{
Subroutine to_string: called CC=2 SCAR=7.0
Pod documented
143 8 my $self = shift;
144 8 my $str =
145 $self->name() . ': ' .
146 'charset_code=' . $self->charset() . ' ' .
147 'marc=' . $self->marc() . ' ' .
148 'ucs=' . $self->ucs() . ' ';
149  
150 8 ▶ $str .= ' combining' if $self->is_combining();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_combining 0 8 8
151 8 return $str;
152 }
153  
154  
155 =head2 marc8_hash_code()
156  
157 Returns a hash code for this Code object for looking up the object using
158 MARC8. First portion is the character set code and the second is the
159 MARC-8 value.
160  
161 =cut
162  
163 sub marc8_hash_code
164 16411 ▶{
Subroutine marc8_hash_code: called CC=1 SCAR=0.0
Pod documented
165 16411 my $self = shift;
166 16411 return sprintf('%s:%s', $self->charset_value(), $self->g0_marc_value());
167 }
168  
169  
170 =head2 utf8_hash_code()
171  
172 Returns a hash code for uniquely identifying a Code by it's UCS value.
173  
174 =cut
175  
176 sub utf8_hash_code
177 16411 ▶{
Subroutine utf8_hash_code: called CC=1 SCAR=0.0
Pod documented
178 16411 return int(hex(shift->ucs()));
179 }
180  
181  
182 =head2 default_charset_group
183  
184 Returns 'G0' or 'G1' indicating where the character is typicalling used
185 in the MARC-8 environment.
186  
187 =cut
188  
189 sub default_charset_group
190 248 ▶{
Subroutine default_charset_group: called CC=11 SCAR=24.1
Pod documented
191 248 my $charset = shift->charset_value();
192  
193 248 ▶ return 'G0'
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S" or $charset eq 2 or $charset eq 1 212 36 248
Condition88%24/27
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S" or $charset eq 2 or $charset eq 1
l!l&&r!l&&!r
211136
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S" or $charset eq 2
l!l&&r!l&&!r
207437
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S"
l!l&&r!l&&!r
200741
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B" or $charset eq 3 or $charset eq "N"
l!l&&r!l&&!r
194648
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B" or $charset eq 3
l!l&&r!l&&!r
192254
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B"
l!l&&r!l&&!r
418856
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p"
l!l&&r!l&&!r
22244
$charset eq "s" or $charset eq "g" or $charset eq "b"
l!l&&r!l&&!r
02246
$charset eq "s" or $charset eq "g"
l!l&&r!l&&!r
00248
MC/DC80%8/10
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S" or $charset eq 2 or $charset eq 1
$charset eq "s" $charset eq "g" $charset eq "b" $charset eq "p" $charset eq "B" $charset eq 3 $charset eq "N" $charset eq "S" $charset eq 2 $charset eq 1
Truth table81%9/11
A or B or C or D or E or F or G or H or I or J
A= $charset eq "s"B= $charset eq "g"C= $charset eq "b"D= $charset eq "p"E= $charset eq "B"F= $charset eq 3G= $charset eq "N"H= $charset eq "S"I= $charset eq 2J= $charset eq 1
ABCDEFGHIJresult
1XXXXXXXXX1
01XXXXXXXX1
001XXXXXXX1
0001XXXXXX1
00001XXXXX1
000001XXXX1
0000001XXX1
00000001XX1
000000001X1
00000000011
00000000000
194 if $charset eq ASCII_DEFAULT
195 or $charset eq GREEK_SYMBOLS
196 or $charset eq SUBSCRIPTS
197 or $charset eq SUPERSCRIPTS
198 or $charset eq BASIC_LATIN
199 or $charset eq BASIC_ARABIC
200 or $charset eq BASIC_CYRILLIC
201 or $charset eq BASIC_GREEK
202 or $charset eq BASIC_HEBREW
203 or $charset eq CJK;
204  
205 36 return 'G1';
206 }
207  
208  
209 =head2 get_marc8_escape
210  
211 Returns an escape sequence to move to the Code from another marc-8 character
212 set.
213  
214 =cut
215  
216 sub get_escape
217 13 ▶{
Subroutine get_escape: called CC=15 SCAR=28.0
Pod undocumented
218 13 my $charset = shift->charset_value();
219  
220 13 ▶ return ESCAPE . $charset
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p" 2 11 13
Condition66%6/9
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p"
l!l&&r!l&&!r
1111
$charset eq "s" or $charset eq "g" or $charset eq "b"
l!l&&r!l&&!r
0112
$charset eq "s" or $charset eq "g"
l!l&&r!l&&!r
0013
MC/DC50%2/4
$charset eq "s" or $charset eq "g" or $charset eq "b" or $charset eq "p"
$charset eq "s" $charset eq "g" $charset eq "b" $charset eq "p"
Truth table60%3/5
A or B or C or D
A= $charset eq "s"B= $charset eq "g"C= $charset eq "b"D= $charset eq "p"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
221 if $charset eq ASCII_DEFAULT
222 or $charset eq GREEK_SYMBOLS
223 or $charset eq SUBSCRIPTS
224 or $charset eq SUPERSCRIPTS;
225  
226 11 ▶ return ESCAPE . SINGLE_G0_A . $charset
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq "s" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S" or $charset eq 2 8 3 11
Condition80%12/15
$charset eq "s" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S" or $charset eq 2
l!l&&r!l&&!r
623
$charset eq "s" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S"
l!l&&r!l&&!r
425
$charset eq "s" or $charset eq "B" or $charset eq 3 or $charset eq "N"
l!l&&r!l&&!r
137
$charset eq "s" or $charset eq "B" or $charset eq 3
l!l&&r!l&&!r
0110
$charset eq "s" or $charset eq "B"
l!l&&r!l&&!r
0011
MC/DC66%4/6
$charset eq "s" or $charset eq "B" or $charset eq 3 or $charset eq "N" or $charset eq "S" or $charset eq 2
$charset eq "s" $charset eq "B" $charset eq 3 $charset eq "N" $charset eq "S" $charset eq 2
Truth table71%5/7
A or B or C or D or E or F
A= $charset eq "s"B= $charset eq "B"C= $charset eq 3D= $charset eq "N"E= $charset eq "S"F= $charset eq 2
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
227 if $charset eq ASCII_DEFAULT
228 or $charset eq BASIC_LATIN
229 or $charset eq BASIC_ARABIC
230 or $charset eq BASIC_CYRILLIC
231 or $charset eq BASIC_GREEK
232 or $charset eq BASIC_HEBREW;
233  
234 3 ▶ return ESCAPE . SINGLE_G1_A . $charset
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq 4 or $charset eq "E" or $charset eq "Q" 2 1 3
Condition83%5/6
$charset eq 4 or $charset eq "E" or $charset eq "Q"
l!l&&r!l&&!r
111
$charset eq 4 or $charset eq "E"
l!l&&r!l&&!r
102
MC/DC66%2/3
$charset eq 4 or $charset eq "E" or $charset eq "Q"
$charset eq 4 $charset eq "E" $charset eq "Q"
Truth table75%3/4
A or B or C
A= $charset eq 4B= $charset eq "E"C= $charset eq "Q"
ABCresult
1XX1
01X1
0011
0000
235 if $charset eq EXTENDED_ARABIC
236 or $charset eq EXTENDED_LATIN
237 or $charset eq EXTENDED_CYRILLIC;
238  
239 1 ▶ return ESCAPE . MULTI_G0_A . CJK
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq 1 1 0 1
240 if $charset eq CJK;
241 }
242  
243 =head2 charset_value
244  
245 Returns the charset value, not the hex sequence.
246  
247 =cut
248  
249 sub charset_value
250 33328 ▶{
Subroutine charset_value: called CC=1 SCAR=0.0
Pod documented
251 33328 return chr(hex(shift->charset()));
252 }
253  
254  
255  
256 21 1;
« blib/lib/MARC/Charset.pm ↑ Summary blib/lib/MARC/Charset/Compiler.pm »