blib/lib/MARC/Charset.pm

filter: Statementstmt 89.8% 150 / 167 Branchbran 77.6% 73 / 94 Conditioncond 62.5% 30 / 48 MC/DCmcdc 55.1% 16 / 29 Subroutinesub 87.5% 14 / 16 Podpod 83.3% 5 / 6 totaltotal 80.0% 288 / 360 CC 65 SCAR 30.6
CC
65
Cov
82%
_process_escape
34.3
marc8_to_utf8
22.4
utf8_to_marc8
22.3
CRAP 21.3
↑ Summary blib/lib/MARC/Charset/Code.pm »
1 package MARC::Charset;
2  
3 18 ▶use v5.8.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 our $VERSION = '1.36';
9  
10 18 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 our @EXPORT_OK = qw(marc8_to_utf8 utf8_to_marc8);
12  
13 18 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Encode 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use charnames ':full';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use MARC::Charset::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use MARC::Charset::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 MARC::Charset - convert MARC-8 encoded strings to UTF-8
22  
23 =head1 SYNOPSIS
24  
25 # import the marc8_to_utf8 function
26 use MARC::Charset 'marc8_to_utf8';
27
28 # prepare STDOUT for utf8
29 binmode(STDOUT, 'utf8');
30  
31 # print out some marc8 as utf8
32 print marc8_to_utf8($marc8_string);
33  
34 =head1 DESCRIPTION
35  
36 MARC::Charset allows you to turn MARC-8 encoded strings into UTF-8
37 strings. MARC-8 is a single byte character encoding that predates unicode, and
38 allows you to put non-Roman scripts in MARC bibliographic records.
39  
40 http://www.loc.gov/marc/specifications/spechome.html
41  
42 =head1 EXPORTS
43  
44 =cut
45  
46 # get the mapping table
47 18 our $table = MARC::Charset::Table->new();
48  
49 # set default character sets
50 # these are viewable at the package level
51 # in case someone wants to set them
52 18 our $DEFAULT_G0 = ASCII_DEFAULT;
53 18 our $DEFAULT_G1 = EXTENDED_LATIN;
54  
55 18 our %SPECIAL_DECOMPOSABLE = (
56 chr(0x01a0) => chr(0x01a0), # uppercase o-hook
57 chr(0x01af) => chr(0x01af), # uppercase u-hook
58 chr(0x01a1) => chr(0x01a1), # lowercase o-hook
59 chr(0x01b0) => chr(0x01b0), # lowercase u-hook
60 chr(0x1ef1) => chr(0x01b0) . chr(0x0323), # lowercase u-hook with dot below
61 chr(0x1ee9) => chr(0x01b0) . chr(0x0301), # lowercase u-hook with acute
62 # Arabic to not decompose
63 chr(0x0622) => chr(0x0622),
64 chr(0x0623) => chr(0x0623),
65 chr(0x0624) => chr(0x0624),
66 chr(0x0625) => chr(0x0625),
67 chr(0x0626) => chr(0x0626),
68 chr(0x0649) => chr(0x0649),
69 chr(0x0671) => chr(0x0671),
70 chr(0x06c0) => chr(0x06c0),
71 chr(0x06D3) => chr(0x06D3),
72 # Cyrillic to not decompose
73 chr(0x0439) => chr(0x0439),
74 chr(0x0419) => chr(0x0419),
75 chr(0x0453) => chr(0x0453),
76 chr(0x0451) => chr(0x0451),
77 chr(0x0457) => chr(0x0457),
78 chr(0x045C) => chr(0x045C),
79 chr(0x045E) => chr(0x045E),
80 chr(0x0403) => chr(0x0403),
81 chr(0x0401) => chr(0x0401),
82 chr(0x0407) => chr(0x0407),
83 chr(0x040C) => chr(0x040C),
84 chr(0x040E) => chr(0x040E),
85 # Katakana to not decompose
86 chr(0x309B) => chr(0x309B),
87 chr(0x309C) => chr(0x309C),
88 chr(0x30AC) => chr(0x30AC),
89 chr(0x30AE) => chr(0x30AE),
90 chr(0x30B0) => chr(0x30B0),
91 chr(0x30B2) => chr(0x30B2),
92 chr(0x30B4) => chr(0x30B4),
93 chr(0x30B6) => chr(0x30B6),
94 chr(0x30B8) => chr(0x30B8),
95 chr(0x30BA) => chr(0x30BA),
96 chr(0x30BC) => chr(0x30BC),
97 chr(0x30BE) => chr(0x30BE),
98 chr(0x30C0) => chr(0x30C0),
99 chr(0x30C2) => chr(0x30C2),
100 chr(0x30C5) => chr(0x30C5),
101 chr(0x30C7) => chr(0x30C7),
102 chr(0x30C9) => chr(0x30C9),
103 chr(0x30D0) => chr(0x30D0),
104 chr(0x30D1) => chr(0x30D1),
105 chr(0x30D3) => chr(0x30D3),
106 chr(0x30D4) => chr(0x30D4),
107 chr(0x30D6) => chr(0x30D6),
108 chr(0x30D7) => chr(0x30D7),
109 chr(0x30D9) => chr(0x30D9),
110 chr(0x30DA) => chr(0x30DA),
111 chr(0x30DC) => chr(0x30DC),
112 chr(0x30DD) => chr(0x30DD),
113 chr(0x30F4) => chr(0x30F4),
114 chr(0x30F7) => chr(0x30F7),
115 chr(0x30F8) => chr(0x30F8),
116 chr(0x30F9) => chr(0x30F9),
117 chr(0x30FA) => chr(0x30FA),
118 chr(0x30FE) => chr(0x30FE),
119 chr(0x30FF) => chr(0x30FF),
120 );
121  
122 =head2 ignore_errors()
123  
124 Tells MARC::Charset whether or not to ignore all encoding errors, and
125 returns the current setting. This is helpful if you have records that
126 contain both MARC8 and UNICODE characters.
127  
128 my $ignore = MARC::Charset->ignore_errors();
129
130 MARC::Charset->ignore_errors(1); # ignore errors
131 MARC::Charset->ignore_errors(0); # DO NOT ignore errors
132  
133 =cut
134  
135  
136 18 our $_ignore_errors = 0;
137 3 ▶sub ignore_errors {
Subroutine ignore_errors: called CC=2 SCAR=6.9
Pod documented
138 3 my ($self,$i) = @_;
139 3 ▶ $_ignore_errors = $i if (defined($i));
Branch: 1 branch
BranchTrueFalseTotal
if defined $i 2 1 3
140 3 return $_ignore_errors;
141 }
142  
143  
144 =head2 assume_unicode()
145  
146 Tells MARC::Charset whether or not to assume UNICODE when an error is
147 encountered in ignore_errors mode and returns the current setting.
148 This is helpful if you have records that contain both MARC8 and UNICODE
149 characters.
150  
151 my $setting = MARC::Charset->assume_unicode();
152
153 MARC::Charset->assume_unicode(1); # assume characters are unicode (utf-8)
154 MARC::Charset->assume_unicode(0); # DO NOT assume characters are unicode
155  
156 =cut
157  
158  
159 18 our $_assume = '';
160 0 ▶sub assume_unicode {
Subroutine assume_unicode: not called CC=4 SCAR=30.0
Pod documented
161 0 my ($self,$i) = @_;
162 0 ▶ $_assume = 'utf8' if (defined($i) and $i);
Branch: 1 branch
BranchTrueFalseTotal
if defined $i and $i 0 0 0
Condition0%0/3
defined $i and $i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $i and $i
defined $i $i
Truth table0%0/3
A and B
A= defined $iB= $i
ABresult
0X0
100
111
163 0 ▶ return 1 if ($_assume eq 'utf8');
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::Charset::_assume eq "utf8" 0 0 0
164 }
165  
166  
167 =head2 assume_encoding()
168  
169 Tells MARC::Charset whether or not to assume a specific encoding when an error
170 is encountered in ignore_errors mode and returns the current setting. This
171 is helpful if you have records that contain both MARC8 and other characters.
172  
173 my $setting = MARC::Charset->assume_encoding();
174
175 MARC::Charset->assume_encoding('cp850'); # assume characters are cp850
176 MARC::Charset->assume_encoding(''); # DO NOT assume any encoding
177  
178 =cut
179  
180  
181 0 ▶sub assume_encoding {
Subroutine assume_encoding: not called CC=2 SCAR=17.9
Pod documented
182 0 my ($self,$i) = @_;
183 0 ▶ $_assume = $i if (defined($i));
Branch: 1 branch
BranchTrueFalseTotal
if defined $i 0 0 0
184 0 return $_assume;
185 }
186  
187  
188 # place holders for working graphical character sets
189 18 my $G0;
190 my $G1;
191  
192 =head2 marc8_to_utf8()
193  
194 Converts a MARC-8 encoded string to UTF-8.
195  
196 my $utf8 = marc8_to_utf8($marc8);
197  
198 If you'd like to ignore errors pass in a true value as the 2nd
199 parameter or call MARC::Charset->ignore_errors() with a true
200 value:
201  
202 my $utf8 = marc8_to_utf8($marc8, 'ignore-errors');
203  
204 or
205
206 MARC::Charset->ignore_errors(1);
207 my $utf8 = marc8_to_utf8($marc8);
208  
209 =cut
210  
211  
212 sub marc8_to_utf8
213 30 ▶{
Subroutine marc8_to_utf8: called CC=22 SCAR=31.1
Pod documented
214 30 my ($marc8, $ignore_errors) = @_;
215 30 reset_charsets();
216  
217 30 ▶ $ignore_errors = $_ignore_errors if (!defined($ignore_errors));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ignore_errors 29 1 30
218  
219 # holder for our utf8
220 30 my $utf8 = '';
221  
222 30 my $index = 0;
223 30 my $length = length($marc8);
224 30 my $combining = '';
225 30 ▶ CHAR_LOOP: while ($index < $length)
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $length) 527 29 556
226 {
227 # whitespace, line feeds and carriage returns just get added on unmolested
228 527 ▶ if (substr($marc8, $index, 1) =~ m/(\s+|\x0A+|\x0D+)/so)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($marc8, $index, 1) =~ /(\s+|\x0A+|\x0D+)/so) 65 462 527
229 {
230 65 $utf8 .= $1;
231 65 $index += 1;
232 65 next CHAR_LOOP;
233 }
234  
235 # look for any escape sequences
236 462 my $new_index = _process_escape(\$marc8, $index, $length);
237 462 ▶ if ($new_index > $index)
Branch: 1 branch
BranchTrueFalseTotal
if ($new_index > $index) 64 398 462
238 {
239 64 $index = $new_index;
240 64 next CHAR_LOOP;
241 }
242  
243 398 my $found;
244 398 CHARSET_LOOP: foreach my $charset ($G0, $G1)
245 {
246  
247 # cjk characters are a string of three chars
248 400 ▶ my $char_size = $charset eq CJK ? 3 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$charset eq '1' ? : 25 375 400
249  
250 # extract the next code point to examine
251 400 my $chunk = substr($marc8, $index, $char_size);
252  
253 400 my $code;
254 400 ▶ if ($char_size == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char_size == 1) { } 375 25 400
255 375 my $codepoint = ord($chunk);
256 375 ▶ if ($codepoint >= 0x21 && $codepoint <= 0x7e) {
Branch: 4 branches
BranchTrueFalseTotal
if ($codepoint >= 33 and $codepoint <= 126) { } 349 26 375
elsif ($codepoint >= 161 and $codepoint <= 254) { } 23 3 26
elsif ($codepoint >= 136 and $codepoint <= 142) { } 1 2 3
elsif ($codepoint >= 27 and $codepoint <= 31) { } 2 0 2
Condition100%3/3
$codepoint >= 33 and $codepoint <= 126
!ll&&!rl&&r
224349
MC/DC100%2/2
$codepoint >= 33 and $codepoint <= 126
$codepoint >= 33 $codepoint <= 126
Truth table100%3/3
A and B
A= $codepoint >= 33B= $codepoint <= 126
ABresult
0X0
100
111
257 # character is G0
258 349 $code = $table->lookup_by_marc8($G0, $chunk);
259 ▶ } elsif ($codepoint >= 0xa1 && $codepoint <= 0xfe) {
Condition66%2/3
$codepoint >= 161 and $codepoint <= 254
!ll&&!rl&&r
3023
MC/DC50%1/2
$codepoint >= 161 and $codepoint <= 254
$codepoint >= 161 $codepoint <= 254
Truth table66%2/3
A and B
A= $codepoint >= 161B= $codepoint <= 254
ABresult
0X0
100
111
260 # character is G1, map it to G0 before atttempting lookup
261 23 $code = $table->lookup_by_marc8($G1, chr($codepoint - 128));
262 ▶ } elsif ($codepoint >= 0x88 && $codepoint <= 0x8e) {
Condition66%2/3
$codepoint >= 136 and $codepoint <= 142
!ll&&!rl&&r
201
MC/DC50%1/2
$codepoint >= 136 and $codepoint <= 142
$codepoint >= 136 $codepoint <= 142
Truth table66%2/3
A and B
A= $codepoint >= 136B= $codepoint <= 142
ABresult
0X0
100
111
263 # in the C1 range used by MARC8
264 1 $code = $table->lookup_by_marc8(EXTENDED_LATIN, $chunk);
265 ▶ } elsif ($codepoint >= 0x1b && $codepoint <= 0x1f) {
Condition33%1/3
$codepoint >= 27 and $codepoint <= 31
!ll&&!rl&&r
002
MC/DC0%0/2
$codepoint >= 27 and $codepoint <= 31
$codepoint >= 27 $codepoint <= 31
Truth table33%1/3
A and B
A= $codepoint >= 27B= $codepoint <= 31
ABresult
0X0
100
111
266 # in the C0 range used by MARC8
267 2 $code = $table->lookup_by_marc8(BASIC_LATIN, $chunk);
268 }
269 } else {
270 # EACC doesn't need G0/G1 conversion
271 25 $code = $table->lookup_by_marc8($charset, $chunk);
272 }
273  
274 # try the next character set if no mapping was found
275 400 ▶ next CHARSET_LOOP if ! $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 4 396 400
276 396 $found = 1;
277  
278 # gobble up all combining characters for appending later
279 # this is necessary because combinging characters precede
280 # the character they modify in MARC-8, whereas they follow
281 # the character they modify in UTF-8.
282 396 ▶ if ($code->is_combining())
Branch: 1 branch
BranchTrueFalseTotal
if ($code->is_combining) { } 13 383 396
283 {
284 # If the current character is the right half of a MARC-8
285 # ligature or double tilde, we don't want to include
286 # it in the UTF-8 output. For the explanation, see
287 # http://lcweb2.loc.gov/diglib/codetables/45.html#Note1
288 # Note that if the MARC-8 string includes a right half
289 # without the corresponding left half, the right half will
290 # get dropped instead of being mapped to its UCS alternate.
291 # That's OK since including only one half of a double diacritic
292 # was presumably a mistake to begin with.
293 13 ▶ unless (defined $code->marc_left_half())
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code->marc_left_half) 8 5 13
294 {
295 8 $combining .= $code->char_value();
296 }
297 }
298 else
299 {
300 383 $utf8 .= $code->char_value() . $combining;
301 383 $combining = '';
302 }
303  
304 396 $index += $char_size;
305 396 next CHAR_LOOP;
306 }
307  
308 2 ▶ if (!$found)
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 2 0 2
309 {
310 2 warn(sprintf("no mapping found for [0x\%X] at position $index ",
311 unpack('C',substr($marc8,$index,1))) .
312 " in $marc8 " .
313 "g0=".MARC::Charset::Constants::charset_name($G0) . " " .
314 "g1=".MARC::Charset::Constants::charset_name($G1));
315 2 ▶ if (!$ignore_errors)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_errors) 1 1 2
316 {
317 1 reset_charsets();
318 1 return;
319 }
320 1 ▶ if ($_assume)
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Charset::_assume) 0 1 1
321 {
322 0 reset_charsets();
323 0 return NFC(decode($_assume => $marc8));
324 }
325 1 $index += 1;
326 }
327  
328 }
329  
330 # return the utf8
331 29 reset_charsets();
332 29 utf8::upgrade($utf8);
333 29 return $utf8;
334 }
335  
336  
337  
338 =head2 utf8_to_marc8()
339  
340 Will attempt to translate utf8 into marc8.
341  
342 my $marc8 = utf8_to_marc8($utf8);
343  
344 If you'd like to ignore errors, or characters that can't be
345 converted to marc8 then pass in a true value as the second
346 parameter:
347  
348 my $marc8 = utf8_to_marc8($utf8, 'ignore-errors');
349  
350 or
351
352 MARC::Charset->ignore_errors(1);
353 my $utf8 = marc8_to_utf8($marc8);
354  
355 =cut
356  
357 sub utf8_to_marc8
358 26 ▶{
Subroutine utf8_to_marc8: called CC=22 SCAR=31.1
Pod documented
359 26 my ($utf8, $ignore_errors) = @_;
360 26 reset_charsets();
361  
362 26 ▶ $ignore_errors = $_ignore_errors if (!defined($ignore_errors));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ignore_errors 24 2 26
363  
364 # decompose combined characters
365 $utf8 = join('',
366 26 ▶ map { exists $SPECIAL_DECOMPOSABLE{$_} ? $SPECIAL_DECOMPOSABLE{$_} : NFD($_) }
Branch: 1 branch
BranchTrueFalseTotal
exists $MARC::Charset::SPECIAL_DECOMPOSABLE{$_} ? : 2 148 150
367 split //, $utf8
368 );
369  
370 26 my $len = length($utf8);
371 26 my $marc8 = '';
372 26 ▶ for (my $i=0; $i<$len; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 153 24 177
373 {
374 153 my $slice = substr($utf8, $i, 1);
375  
376 # spaces are copied from utf8 into marc8
377 153 ▶ if ($slice eq ' ')
Branch: 1 branch
BranchTrueFalseTotal
if ($slice eq " ") 16 137 153
378 {
379 16 $marc8 .= ' ';
380 16 next;
381 }
382
383 # try to find the code point in our mapping table
384 137 my $code = $table->lookup_by_utf8($slice);
385  
386 137 ▶ if (! $code)
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 5 132 137
387 {
388 5 warn("no mapping found at position $i in $utf8");
389 5 ▶ if (!$ignore_errors)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_errors) 2 3 5
390 {
391 2 reset_charsets();
392 2 return;
393 }
394 # drop the character we can't map and carry on
395 3 next;
396 }
397  
398 # if it's a combining character move it around
399 132 ▶ if ($code->is_combining())
Branch: 1 branch
BranchTrueFalseTotal
if ($code->is_combining) 21 111 132
400 {
401 21 my $prev = chop($marc8);
402 21 ▶ if ($code->marc_left_half())
Branch: 1 branch
BranchTrueFalseTotal
if ($code->marc_left_half) 2 19 21
403 {
404 # don't add the MARC-8 right half character
405 # if it was already inserted when the double
406 # diacritic was converted from UTF-8
407 2 ▶ if ($code->marc_value() eq substr($marc8, -1, 1))
Branch: 1 branch
BranchTrueFalseTotal
if ($code->marc_value eq substr($marc8, -1, 1)) 2 0 2
408 {
409 2 $marc8 .= $prev;
410 2 next;
411 }
412 }
413 19 $marc8 .= $code->marc_value() . $prev;
414 19 ▶ if ($code->marc_right_half())
Branch: 1 branch
BranchTrueFalseTotal
if ($code->marc_right_half) 4 15 19
415 {
416 4 $marc8 .= chr(hex($code->marc_right_half()));
417 }
418 19 next;
419 }
420  
421 # look to see if we need to escape to a new G0 charset
422 111 my $charset_value = $code->charset_value();
423  
424 111 ▶ if ($code->default_charset_group() eq 'G0'
Branch: 2 branches
BranchTrueFalseTotal
if ($code->default_charset_group eq 'G0' and $G0 ne $charset_value) { } 105 6 111
elsif ($code->default_charset_group eq 'G1' and $G1 ne $charset_value) { } 2 5 7
Condition100%3/3
$code->default_charset_group eq "G0" and $G0 ne $charset_value
!ll&&!rl&&r
51105
MC/DC100%2/2
$code->default_charset_group eq "G0" and $G0 ne $charset_value
$code->default_charset_group eq "G0" $G0 ne $charset_value
Truth table100%3/3
A and B
A= $code->default_charset_group eq "G0"B= $G0 ne $charset_value
ABresult
0X0
100
111
425 and $G0 ne $charset_value)
426 {
427 105 ▶ if ($G0 eq ASCII_DEFAULT and $charset_value eq BASIC_LATIN)
Branch: 1 branch
BranchTrueFalseTotal
if ($G0 eq 's' and $charset_value eq 'B') { } 94 11 105
Condition100%3/3
$G0 eq "s" and $charset_value eq "B"
!ll&&!rl&&r
11094
MC/DC100%2/2
$G0 eq "s" and $charset_value eq "B"
$G0 eq "s" $charset_value eq "B"
Truth table100%3/3
A and B
A= $G0 eq "s"B= $charset_value eq "B"
ABresult
0X0
100
111
428 {
429 # don't bother escaping, they're functionally the same
430 }
431 else
432 {
433 11 $marc8 .= $code->get_escape();
434 11 $G0 = $charset_value;
435 }
436 }
437  
438 # look to see if we need to escape to a new G1 charset
439 ▶ elsif ($code->default_charset_group() eq 'G1'
Condition100%3/3
$code->default_charset_group eq "G1" and $G1 ne $charset_value
!ll&&!rl&&r
132
MC/DC100%2/2
$code->default_charset_group eq "G1" and $G1 ne $charset_value
$code->default_charset_group eq "G1" $G1 ne $charset_value
Truth table100%3/3
A and B
A= $code->default_charset_group eq "G1"B= $G1 ne $charset_value
ABresult
0X0
100
111
440 and $G1 ne $charset_value)
441 {
442 2 $marc8 .= $code->get_escape();
443 2 $G1 = $charset_value;
444 }
445  
446 111 $marc8 .= $code->marc_value();
447 }
448  
449 # escape back to default G0 if necessary
450 24 ▶ if ($G0 ne $DEFAULT_G0)
Branch: 1 branch
BranchTrueFalseTotal
if ($G0 ne $MARC::Charset::DEFAULT_G0) 10 14 24
451 {
452 10 ▶ if ($DEFAULT_G0 eq ASCII_DEFAULT) { $marc8 .= ESCAPE . ASCII_DEFAULT; }
Branch: 2 branches
BranchTrueFalseTotal
if ($MARC::Charset::DEFAULT_G0 eq 's') { } 10 0 10
elsif ($MARC::Charset::DEFAULT_G0 eq '1') { } 0 0 0
453 0 elsif ($DEFAULT_G0 eq CJK) { $marc8 .= ESCAPE . MULTI_G0_A . CJK; }
454 0 else { $marc8 .= ESCAPE . SINGLE_G0_A . $DEFAULT_G0; }
455 }
456  
457 # escape back to default G1 if necessary
458 24 ▶ if ($G1 ne $DEFAULT_G1)
Branch: 1 branch
BranchTrueFalseTotal
if ($G1 ne $MARC::Charset::DEFAULT_G1) 2 22 24
459 {
460 0 ▶ if ($DEFAULT_G1 eq CJK) { $marc8 .= ESCAPE . MULTI_G1_A . $DEFAULT_G1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Charset::DEFAULT_G1 eq '1') { } 0 2 2
461 2 else { $marc8 .= ESCAPE . SINGLE_G1_A . $DEFAULT_G1; }
462 }
463  
464 24 return $marc8;
465 }
466  
467  
468  
469 =head1 DEFAULT CHARACTER SETS
470  
471 If you need to alter the default character sets you can set the
472 $MARC::Charset::DEFAULT_G0 and $MARC::Charset::DEFAULT_G1 variables to the
473 appropriate character set code:
474  
475 use MARC::Charset::Constants qw(:all);
476 $MARC::Charset::DEFAULT_G0 = BASIC_ARABIC;
477 $MARC::Charset::DEFAULT_G1 = EXTENDED_ARABIC;
478  
479 =head1 SEE ALSO
480  
481 =over 4
482  
483 =item * L<MARC::Charset::Constant>
484  
485 =item * L<MARC::Charset::Table>
486  
487 =item * L<MARC::Charset::Code>
488  
489 =item * L<MARC::Charset::Compiler>
490  
491 =item * L<MARC::Record>
492  
493 =item * L<MARC::XML>
494  
495 =back
496  
497 =head1 AUTHOR
498  
499 Ed Summers (ehs@pobox.com)
500  
501 =cut
502  
503  
504 sub _process_escape
505 462 ▶{
Subroutine _process_escape: called CC=18 SCAR=35.4
506 ## this stuff is kind of scary ... for an explanation of what is
507 ## going on here check out the MARC-8 specs at LC.
508 ## http://lcweb.loc.gov/marc/specifications/speccharmarc8.html
509 462 my ($str_ref, $left, $right) = @_;
510  
511 # first char needs to be an escape or else this isn't an escape sequence
512 462 ▶ return $left unless substr($$str_ref, $left, 1) eq ESCAPE;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($$str_ref, $left, 1) eq "\e" 398 64 462
513  
514 ## if we don't have at least one character after the escape
515 ## then this can't be a character escape sequence
516 64 ▶ return $left if ($left+1 >= $right);
Branch: 1 branch
BranchTrueFalseTotal
if $left + 1 >= $right 0 64 64
517  
518 ## pull off the first escape
519 64 my $esc_char_1 = substr($$str_ref, $left+1, 1);
520  
521 ## the first method of escaping to small character sets
522 64 ▶ if ( $esc_char_1 eq GREEK_SYMBOLS
Branch: 1 branch
BranchTrueFalseTotal
if ($esc_char_1 eq "g" or $esc_char_1 eq "b" or $esc_char_1 eq "p" or $esc_char_1 eq "s") 14 50 64
Condition100%9/9
$esc_char_1 eq "g" or $esc_char_1 eq "b" or $esc_char_1 eq "p" or $esc_char_1 eq "s"
l!l&&r!l&&!r
7750
$esc_char_1 eq "g" or $esc_char_1 eq "b" or $esc_char_1 eq "p"
l!l&&r!l&&!r
4357
$esc_char_1 eq "g" or $esc_char_1 eq "b"
l!l&&r!l&&!r
1360
MC/DC100%4/4
$esc_char_1 eq "g" or $esc_char_1 eq "b" or $esc_char_1 eq "p" or $esc_char_1 eq "s"
$esc_char_1 eq "g" $esc_char_1 eq "b" $esc_char_1 eq "p" $esc_char_1 eq "s"
Truth table100%5/5
A or B or C or D
A= $esc_char_1 eq "g"B= $esc_char_1 eq "b"C= $esc_char_1 eq "p"D= $esc_char_1 eq "s"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
523 or $esc_char_1 eq SUBSCRIPTS
524 or $esc_char_1 eq SUPERSCRIPTS
525 or $esc_char_1 eq ASCII_DEFAULT)
526 {
527 14 $G0 = $esc_char_1;
528 14 return $left+2;
529 }
530  
531 ## the second more complicated method of escaping to bigger charsets
532 50 ▶ return $left if $left+2 >= $right;
Branch: 1 branch
BranchTrueFalseTotal
if $left + 2 >= $right 0 50 50
533  
534 50 my $esc_char_2 = substr($$str_ref, $left+2, 1);
535 50 my $esc_chars = $esc_char_1 . $esc_char_2;
536  
537 50 ▶ if ($esc_char_1 eq SINGLE_G0_A
Branch: 5 branches
BranchTrueFalseTotal
if ($esc_char_1 eq '(' or $esc_char_1 eq ',') { } 37 13 50
elsif ($esc_char_1 eq ')' or $esc_char_1 eq '-') { } 7 6 13
elsif ($esc_char_1 eq '$') { } 6 0 6
elsif ($esc_chars eq '$,' and $left + 3 < $right) { } 0 0 0
elsif ($esc_chars eq '$)' || $esc_chars eq '$-' and $left + 3 < $right) { } 0 0 0
Condition66%2/3
$esc_char_1 eq "(" or $esc_char_1 eq ","
l!l&&r!l&&!r
37013
MC/DC50%1/2
$esc_char_1 eq "(" or $esc_char_1 eq ","
$esc_char_1 eq "(" $esc_char_1 eq ","
Truth table66%2/3
A or B
A= $esc_char_1 eq "("B= $esc_char_1 eq ","
ABresult
1X1
011
000
538 or $esc_char_1 eq SINGLE_G0_B)
539 {
540 37 $G0 = $esc_char_2;
541 37 return $left+3;
542 }
543  
544 ▶ elsif ($esc_char_1 eq SINGLE_G1_A
Condition66%2/3
$esc_char_1 eq ")" or $esc_char_1 eq "-"
l!l&&r!l&&!r
706
MC/DC50%1/2
$esc_char_1 eq ")" or $esc_char_1 eq "-"
$esc_char_1 eq ")" $esc_char_1 eq "-"
Truth table66%2/3
A or B
A= $esc_char_1 eq ")"B= $esc_char_1 eq "-"
ABresult
1X1
011
000
545 or $esc_char_1 eq SINGLE_G1_B)
546 {
547 7 $G1 = $esc_char_2;
548 7 return $left+3;
549 }
550  
551 elsif ( $esc_char_1 eq MULTI_G0_A ) {
552 6 $G0 = $esc_char_2;
553 6 return $left+3;
554 }
555  
556 ▶ elsif ($esc_chars eq MULTI_G0_B
Condition0%0/3
$esc_chars eq "\$," and $left + 3 < $right
!ll&&!rl&&r
000
MC/DC0%0/2
$esc_chars eq "\$," and $left + 3 < $right
$esc_chars eq "\$," $left + 3 < $right
Truth table0%0/3
A and B
A= $esc_chars eq "\$,"B= $left + 3 < $right
ABresult
0X0
100
111
557 and ($left+3 < $right))
558 {
559 0 $G0 = substr($$str_ref, $left+3, 1);
560 0 return $left+4;
561 }
562  
563 ▶ elsif (($esc_chars eq MULTI_G1_A or $esc_chars eq MULTI_G1_B)
Condition0%0/6
$esc_chars eq "\$)" || $esc_chars eq "\$-" and $left + 3 < $right
!ll&&!rl&&r
000
$esc_chars eq "\$)" or $esc_chars eq "\$-"
l!l&&r!l&&!r
000
MC/DC0%0/3
$esc_chars eq "\$)" || $esc_chars eq "\$-" and $left + 3 < $right
$esc_chars eq "\$)" $esc_chars eq "\$-" $left + 3 < $right
Truth table0%0/5
A or B and C
A= $esc_chars eq "\$)"B= $esc_chars eq "\$-"C= $left + 3 < $right
ABCresult
00X0
1X00
0100
1X11
0111
564 and ($left + 3 < $right))
565 {
566 0 $G1 = substr($$str_ref, $left+3, 1);
567 0 return $left+4;
568 }
569  
570 # we should never get here
571 0 warn("seem to have fallen through in _process_escape()");
572 0 return $left;
573 }
574  
575 sub reset_charsets
576 88 ▶{
Subroutine reset_charsets: called CC=1 SCAR=0.0
Pod undocumented
577 88 $G0 = $DEFAULT_G0;
578 88 $G1 = $DEFAULT_G1;
579 }
580  
581 18 1;
↑ Summary blib/lib/MARC/Charset/Code.pm »