blib/lib/MARC/Charset.pm

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