RecodeData/UTF_8.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 126 CC 15 SCAR 39.9
CC
15
Cov
0%
_toInternal
90.0
_fromInternal
42.0
_recode
6.0
CRAP 54.0
1 #! /bin/false
2  
3 # vim: set autoindent shiftwidth=4 tabstop=4:
4  
5 # Conversion routines for UTF-8 (perl < 5.8.0).
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::RecodeData::UTF_8;
23  
24 use strict;
25  
26 require Locale::RecodeData;
27 use base qw(Locale::RecodeData);
28  
29 sub _recode
30 {
31 if ($_[0]->{_from} eq 'INTERNAL') {
32 return $_[0]->_fromInternal ($_[1]);
33 } else {
34 return $_[0]->_toInternal ($_[1]);
35 }
36 }
37  
38 # This routine assumes that the internal representation is always sane
39 # and contains valid codes only.
40 sub _fromInternal
41 {
42 $_[1] = join '', map {
43 if ($_ <= 0x7f) {
44 chr $_;
45 } elsif ($_ <= 0x7ff) {
46 pack ("C2",
47 (0xc0 | (($_ >> 6) & 0x1f)),
48 (0x80 | ($_ & 0x3f)));
49 } elsif ($_ <= 0xffff) {
50 pack ("C3",
51 (0xe0 | (($_ >> 12) & 0xf)),
52 (0x80 | (($_ >> 6) & 0x3f)),
53 (0x80 | ($_ & 0x3f)));
54 } elsif ($_ <= 0x1fffff) {
55 pack ("C4",
56 (0xf0 | (($_ >> 18) & 0x7)),
57 (0x80 | (($_ >> 12) & 0x3f)),
58 (0x80 | (($_ >> 6) & 0x3f)),
59 (0x80 | ($_ & 0x3f)));
60 } elsif ($_ <= 0x3ffffff) {
61 pack ("C5",
62 (0xf0 | (($_ >> 24) & 0x3)),
63 (0x80 | (($_ >> 18) & 0x3f)),
64 (0x80 | (($_ >> 12) & 0x3f)),
65 (0x80 | (($_ >> 6) & 0x3f)),
66 (0x80 | ($_ & 0x3f)));
67 } else {
68 pack ("C6",
69 (0xf0 | (($_ >> 30) & 0x3)),
70 (0x80 | (($_ >> 24) & 0x1)),
71 (0x80 | (($_ >> 18) & 0x3f)),
72 (0x80 | (($_ >> 12) & 0x3f)),
73 (0x80 | (($_ >> 6) & 0x3f)),
74 (0x80 | ($_ & 0x3f)));
75 }
76 } @{$_[1]};
77 return 1;
78 }
79  
80 # Decode UTF-8 into integers. We do not bother to care about possibly
81 # configured replacement characters here and simply fall back to 0xfffd.
82 # Rationale: the internal format is never output directly and the other
83 # encoders will handle the replacement character correctly.
84 sub _toInternal
85 {
86 if ($] >= 5.006) {
87 $_[1] = [ unpack "U*", $_[1] ];
88 return 1;
89 }
90  
91 # Sigh, we have to decode ourselves. FIXME: Should be optimized.
92 # The routine is awfully slow.
93 # It also does not necessarily detect illegal multi-byte sequences.
94  
95 my @chars = ();
96 my @bytes = unpack "C*", $_[1];
97  
98 BYTE: while (@bytes) {
99 my $byte = shift @bytes;
100 if ($byte < 0x80) {
101 push @chars, $byte;
102 } elsif ($byte < 0xc0 || $byte > 0xfd) {
103 push @chars, 0xfffd;
104 } else {
105 my $num_bytes;
106 my $char;
107 if ($byte < 0xe0) {
108 $char = $byte & 0x1f;
109 $num_bytes = 1;
110 } elsif ($byte < 0xf0) {
111 $char = $byte & 0xf;
112 $num_bytes = 2;
113 } elsif ($byte < 0xf8) {
114 $char = $byte & 0x7;
115 $num_bytes = 3;
116 } elsif ($byte < 0xfc) {
117 $char = $byte & 0x3;
118 $num_bytes = 4;
119 } else {
120 $char = $byte & 0x1;
121 $num_bytes = 5;
122 }
123 for (my $i = 0; $i < $num_bytes; ++$i) {
124 my $next = shift @bytes;
125 if (!defined $next || $next < 0x80 || $next > 0xbf) {
126 push @chars, 0xfffd;
127 next BYTE;
128 } else {
129 $char <<= 6;
130 $char |= $next & 0x3f;
131 }
132 }
133 push @chars, $char;
134 }
135 }
136
137 $_[1] = \@chars;
138
139 return 1;
140 }
141  
142 1;
143  
144 __END__