unitrans.h

filter: Statementstmt 96.2% 259 / 269 Branchbran 83.4% 182 / 218 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.5% 441 / 487 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary Transform.xs »
1 #ifndef UNITRANS_H
2 #define UNITRANS_H
3  
4 /* Perl 5.6.1 ? */
5 #ifndef uvuni_to_utf8
6 #define uvuni_to_utf8 uv_to_utf8
7 #endif /* uvuni_to_utf8 */
8  
9 /* Perl 5.6.1 ? */
10 #ifndef utf8n_to_uvuni
11 #define utf8n_to_uvuni utf8_to_uv
12 #endif /* utf8n_to_uvuni */
13  
14 /* Perl 5.6.1 ? */
15 #ifndef UTF8_IS_INVARIANT
16 #define UTF8_IS_INVARIANT UTF8_IS_ASCII
17 #endif /* UTF8_IS_INVARIANT */
18  
19 /* UTF8_ALLOW_BOM is used before Perl 5.8.0 */
20 #ifndef UTF8_ALLOW_BOM
21 #define UTF8_ALLOW_BOM (0)
22 #endif /* UTF8_ALLOW_BOM */
23  
24 #ifndef UTF8_ALLOW_SURROGATE
25 #define UTF8_ALLOW_SURROGATE (0)
26 #endif /* UTF8_ALLOW_SURROGATE */
27  
28 #ifndef UTF8_ALLOW_FE_FF
29 #define UTF8_ALLOW_FE_FF (0)
30 #endif /* UTF8_ALLOW_FE_FF */
31  
32 #ifndef UTF8_ALLOW_FFFF
33 #define UTF8_ALLOW_FFFF (0)
34 #endif /* UTF8_ALLOW_FFFF */
35  
36 #define AllowAnyUTF (UTF8_ALLOW_SURROGATE|UTF8_ALLOW_BOM|UTF8_ALLOW_FE_FF|UTF8_ALLOW_FFFF)
37  
38  
39 static UV
40 5446 ord_in_unicode(U8 *s, STRLEN curlen, STRLEN *retlen)
41 {
42  
43 UV uv; STRLEN ret;
44 5446 uv = utf8n_to_uvuni(s, curlen, &ret, AllowAnyUTF|UTF8_CHECK_ONLY);
45  
46 5446 ▶ if (retlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) { 5446 0 5446
47 /* in old Perl (<= 5.7.2), falsely UTF8_ALLOW_LONG == UTF8_CHECK_ONLY */
48 5446 ▶ if (ret == (STRLEN)-1 || ret > (STRLEN) UNISKIP(uv))
Branch: 4 branches
BranchTrueFalseTotal
if (ret == (STRLEN)-1 || ret > (STRLEN) UNISKIP(uv)) 5446 0 5446
if (ret == (STRLEN)-1 || ret > (STRLEN) UNISKIP(uv)) 149 5297 5446
if (ret == (STRLEN)-1 || ret > (STRLEN) UNISKIP(uv)) 149 0 149
if (ret == (STRLEN)-1 || ret > (STRLEN) UNISKIP(uv)) 0 5446 5446
49 0 *retlen = 0;
50 else
51 5446 *retlen = ret;
52 }
53 5446 return uv;
54 }
55  
56 static U8*
57 5400 app_in_unicode(U8* s, UV uv)
58 {
59 5400 return uvuni_to_utf8(s, uv);
60 }
61  
62  
63 /***************************************
64  
65 All UTFs are limited in 0..D7FF and E000..10FFFF for roundtrip.
66  
67 (i) on ASCII platform
68  
69 UTF-8 Bit pattern 1st Byte 2nd Byte 3rd Byte 4th Byte
70  
71 0xxxxxxx 0xxxxxxx
72 00000yyy yyxxxxxx 110yyyyy 10xxxxxx
73 zzzzyyyy yyxxxxxx 1110zzzz 10yyyyyy 10xxxxxx
74 000wwwzz zzzzyyyy yyxxxxxx 11110www 10zzzzzz 10yyyyyy 10xxxxxx
75  
76 UCS UTF-8
77 00000000-0000007F 1
78 00000080-000007FF 2
79 00000800-0000FFFF 3
80 00010000-001FFFFF 4
81 00200000-03FFFFFF 5
82 04000000-7FFFFFFF 6
83  
84 (ii) on EBCDIC platform
85  
86 UTF-8-Mod Bit pattern 1st Byte 2nd Byte 3rd Byte 4th Byte 5th Byte
87 00000000 0xxxxxxx 0xxxxxxx
88 00000000 100xxxxx 100xxxxx
89 000000yy yyyxxxxx 110yyyyy 101xxxxx
90 00zzzzyy yyyxxxxx 1110zzzz 101yyyyy 101xxxxx
91 000000ww wzzzzzyy yyyxxxxx 11110www 101zzzzz 101yyyyy 101xxxxx
92 00vvwwww wzzzzzyy yyyxxxxx 111110vv 101wwwww 101zzzzz 101yyyyy 101xxxxx
93  
94 UCS UTF-EBCDIC
95 00000000-0000009F 1
96 000000A0-000003FF 2
97 00000400-00003FFF 3
98 00004000-0003FFFF 4
99 00040000-003FFFFF 5
100 00400000-03FFFFFF 6
101 04000000-7FFFFFFF 7
102  
103 (iii) length distribution
104  
105 UTF-8 UTF8MOD UTF-16 UTF-32
106 0000.. 007F 1 1 2 4
107 0080.. 009F 2 1 2 4
108 00A0.. 03FF 2 2 2 4
109 0400.. 07FF 2 3 2 4
110 0800.. 3FFF 3 3 2 4
111 4000.. FFFF 3 4 2 4
112 10000.. 3FFFF 4 4 4 4
113 40000.. 10FFFF 4 5 4 4
114 110000.. 1FFFFF 4 5 N/A 4
115 200000.. 3FFFFF 5 5 N/A 4
116 400000.. 3FFFFFF 5 6 N/A 4
117 4000000..7FFFFFFF 6 7 N/A 4
118  
119 * UTF-8 to UTF-8M : Max 1.5 * UTF-16 to UTF-8 : Max 1.5
120 * UTF-8 to UTF-16 : Max 2 * UTF-16 to UTF-8M : Max 2
121 * UTF-8 to UTF-32 : Max 4 * UTF-16 to UTF-32 : Max 2
122 * UTF-8M to UTF-8 : Max 2 * UTF-32 to UTF-8 : Max 1 (or 1.5)
123 * UTF-8M to UTF-16 : Max 2 * UTF-32 to UTF-8M : Max 1.25 (or 1.75)
124 * UTF-8M to UTF-32 : Max 4 * UTF-32 to UTF-16 : Max 1
125  
126 ***************************************/
127  
128 #define UV_Max_UTF16 (0x10FFFF)
129 #define UV_Max_UTF (0x7FFFFFFF)
130 #define UV_Max_UTF32 (0xFFFFFFFF)
131  
132 #define UTF16_IS_SURROG(uv) (0xD800 <= (uv) && (uv) <= 0xDFFF)
133 #define UTF16_HI_SURROG(uv) (0xD800 <= (uv) && (uv) <= 0xDBFF)
134 #define UTF16_LO_SURROG(uv) (0xDC00 <= (uv) && (uv) <= 0xDFFF)
135  
136 #define Is_VALID_UTF(uv) ((uv) <= UV_Max_UTF16 && !UTF16_IS_SURROG(uv))
137  
138 #define UTF8A_SKIP(uv) \
139 ( (uv) < 0x80 ? 1 : \
140 (uv) < 0x800 ? 2 : \
141 (uv) < 0x10000 ? 3 : \
142 (uv) < 0x200000 ? 4 : \
143 (uv) < 0x4000000 ? 5 : \
144 (uv) < 0x80000000 ? 6 : 7 )
145  
146 #define UTF8A_TRAIL(c) (((c) & 0xC0) == 0x80)
147  
148 #define UTF8M_SKIP(uv) \
149 ( (uv) < 0xA0 ? 1 : \
150 (uv) < 0x400 ? 2 : \
151 (uv) < 0x4000 ? 3 : \
152 (uv) < 0x40000 ? 4 : \
153 (uv) < 0x400000 ? 5 : \
154 (uv) < 0x4000000 ? 6 : 7 )
155  
156 #define UTF8M_TRAIL(c) (((c) & 0xE0) == 0xA0)
157  
158 #define UTF8A_LEN(b) \
159 ( (b) < 0x80 ? 1 : \
160 (b) < 0xC0 ? 0 : \
161 (b) < 0xE0 ? 2 : \
162 (b) < 0xF0 ? 3 : \
163 (b) < 0xF8 ? 4 : \
164 (b) < 0xFC ? 5 : \
165 (b) < 0xFE ? 6 : 0)
166  
167 #define UTF8M_LEN(b) \
168 ( (b) < 0xA0 ? 1 : \
169 (b) < 0xC0 ? 0 : \
170 (b) < 0xE0 ? 2 : \
171 (b) < 0xF0 ? 3 : \
172 (b) < 0xF8 ? 4 : \
173 (b) < 0xFC ? 5 : \
174 (b) < 0xFE ? 6 : \
175 7) /* ((b) == 0xFF) */
176  
177 #define UTF8M_MaxLEN (8)
178  
179 static UV
180 213 ord_in_utf16le(U8 *s, STRLEN curlen, STRLEN *retlen)
181 {
182 UV uv, luv;
183 213 U8 *p = s;
184  
185 213 ▶ if (curlen < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (curlen < 2) { 1 212 213
186 1 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 1 0 1
187 1 *retlen = 0;
188 1 return 0;
189 }
190  
191 212 uv = (UV)((p[1] << 8) | p[0]);
192 212 p += 2;
193  
194 212 ▶ if (UTF16_HI_SURROG(uv) && (4 <= curlen)) {
Branch: 3 branches
BranchTrueFalseTotal
if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { 40 172 212
if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { 6 34 40
if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { 5 1 6
195 5 luv = (UV)((p[1] << 8) | p[0]);
196  
197 5 ▶ if (UTF16_LO_SURROG(luv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UTF16_LO_SURROG(luv)) { 5 0 5
if (UTF16_LO_SURROG(luv)) { 5 0 5
198 5 uv = 0x10000 + ((uv-0xD800) * 0x400) + (luv-0xDC00);
199 5 p += 2;
200 }
201 }
202  
203 212 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 212 0 212
204 212 *retlen = p - s;
205 212 return uv;
206 }
207  
208  
209 static UV
210 216 ord_in_utf16be(U8 *s, STRLEN curlen, STRLEN *retlen)
211 {
212 UV uv, luv;
213 216 U8 *p = s;
214  
215 216 ▶ if (curlen < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (curlen < 2) { 2 214 216
216 2 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 2 0 2
217 2 *retlen = 0;
218 2 return 0;
219 }
220  
221 214 uv = (UV)((p[0] << 8) | p[1]);
222 214 p += 2;
223  
224 214 ▶ if (UTF16_HI_SURROG(uv) && (4 <= curlen)) {
Branch: 3 branches
BranchTrueFalseTotal
if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { 40 174 214
if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { 6 34 40
if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { 5 1 6
225 5 luv = (UV)((p[0] << 8) | p[1]);
226  
227 5 ▶ if (UTF16_LO_SURROG(luv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UTF16_LO_SURROG(luv)) { 5 0 5
if (UTF16_LO_SURROG(luv)) { 5 0 5
228 5 uv = 0x10000 + ((uv-0xD800) * 0x400) + (luv-0xDC00);
229 5 p += 2;
230 }
231 }
232  
233 214 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 214 0 214
234 214 *retlen = p - s;
235 214 return uv;
236 }
237  
238  
239 static UV
240 373 ord_in_utf32le(U8 *s, STRLEN curlen, STRLEN *retlen)
241 {
242 UV uv; int i;
243 373 ▶ if (curlen < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (curlen < 4) { 4 369 373
244 4 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 4 0 4
245 4 *retlen = 0;
246 4 return 0;
247 }
248  
249 369 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 369 0 369
250 369 *retlen = 4;
251  
252 369 uv = s[3];
253 1476 ▶ for (i = 2; i >= 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i >= 0; --i) { 1107 369 1476
254 1107 uv <<= 8;
255 1107 uv |= s[i];
256 }
257 369 return uv;
258 }
259  
260  
261 static UV
262 5375 ord_in_utf32be(U8 *s, STRLEN curlen, STRLEN *retlen)
263 {
264 UV uv; U8* e;
265 5375 ▶ if (curlen < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (curlen < 4) { 5 5370 5375
266 5 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 5 0 5
267 5 *retlen = 0;
268 5 return 0;
269 }
270  
271 5370 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 5370 0 5370
272 5370 *retlen = 4;
273  
274 5370 e = s + 4;
275 5370 uv = *s++;
276 21480 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 16110 5370 21480
277 16110 uv <<= 8;
278 16110 uv |= *s++;
279 }
280 5370 return uv;
281 }
282  
283  
284 static UV
285 397 ord_in_utf8(U8 *s, STRLEN curlen, STRLEN *retlen)
286 {
287 397 UV uv = 0;
288 STRLEN len, i;
289  
290 397 ▶ if (curlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (curlen == 0) { 0 397 397
291 0 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 0 0 0
292 0 *retlen = 0;
293 0 return 0;
294 }
295  
296 397 ▶ len = (STRLEN) UTF8A_LEN(*s);
Branch: 7 branches
BranchTrueFalseTotal
len = (STRLEN) UTF8A_LEN(*s); 131 266 397
len = (STRLEN) UTF8A_LEN(*s); 126 5 131
len = (STRLEN) UTF8A_LEN(*s); 107 19 126
len = (STRLEN) UTF8A_LEN(*s); 45 62 107
len = (STRLEN) UTF8A_LEN(*s); 15 30 45
len = (STRLEN) UTF8A_LEN(*s); 11 4 15
len = (STRLEN) UTF8A_LEN(*s); 6 5 11
297  
298 397 ▶ if (curlen < len || len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (curlen < len || len == 0) { 396 1 397
if (curlen < len || len == 0) { 10 386 396
299 11 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 11 0 11
300 11 *retlen = 0;
301 11 return 0;
302 }
303  
304 386 ▶ if (*s < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s < 0x80) { 266 120 386
305 266 uv = (UV)*s;
306 }
307 120 ▶ else if (*s < 0xE0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xE0) { 19 101 120
308 19 uv = (UV)(((s[0] & 0x1f) << 6) | (s[1] & 0x3f));
309 }
310 101 ▶ else if (*s < 0xF0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xF0) { 62 39 101
311 62 uv = (UV)(((s[0] & 0x0f) << 12) |
312 62 ((s[1] & 0x3f) << 6) | (s[2] & 0x3f));
313 }
314 39 ▶ else if (*s < 0xF8) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xF8) { 30 9 39
315 30 uv = (UV)(((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |
316 30 ((s[2] & 0x3f) << 6) | (s[3] & 0x3f));
317 }
318 9 ▶ else if (*s < 0xFC) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xFC) { 4 5 9
319 4 uv = (UV)(((s[0] & 0x03) << 24) | ((s[1] & 0x3f) << 18) |
320 4 ((s[2] & 0x3f) << 12) | ((s[3] & 0x3f) << 6) |
321 4 (s[4] & 0x3f));
322 }
323 5 ▶ else if (*s < 0xFE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xFE) { 5 0 5
324 5 uv = (UV)(((s[0] & 0x01) << 30) | ((s[1] & 0x3f) << 24) |
325 5 ((s[2] & 0x3f) << 18) | ((s[3] & 0x3f) << 12) |
326 5 ((s[4] & 0x3f) << 6) | (s[5] & 0x3f));
327 }
328  
329 656 ▶ for (i = 1; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) { 274 382 656
330 274 ▶ if (!UTF8A_TRAIL(s[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!UTF8A_TRAIL(s[i])) { 4 270 274
331 4 len = 0;
332 4 break;
333 }
334 }
335  
336 386 ▶ if (len != (STRLEN) UTF8A_SKIP(uv))
Branch: 7 branches
BranchTrueFalseTotal
if (len != (STRLEN) UTF8A_SKIP(uv)) 115 271 386
if (len != (STRLEN) UTF8A_SKIP(uv)) 101 14 115
if (len != (STRLEN) UTF8A_SKIP(uv)) 39 62 101
if (len != (STRLEN) UTF8A_SKIP(uv)) 9 30 39
if (len != (STRLEN) UTF8A_SKIP(uv)) 5 4 9
if (len != (STRLEN) UTF8A_SKIP(uv)) 5 0 5
if (len != (STRLEN) UTF8A_SKIP(uv)) 9 377 386
337 9 len = 0;
338  
339 386 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 386 0 386
340 386 *retlen = len;
341 386 return uv;
342 }
343  
344  
345 static UV
346 709 ord_in_utfebcdic(U8 *s, STRLEN curlen, STRLEN *retlen, U8* table)
347 {
348 709 UV uv = 0;
349 U8 ini, *p, *d, buff[UTF8M_MaxLEN];
350 STRLEN len, i;
351  
352 709 ▶ if (curlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (curlen == 0) { 0 709 709
353 0 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 0 0 0
354 0 *retlen = 0;
355 0 return 0;
356 }
357  
358 709 ▶ ini = table ? table[*s] : *s;
Branch: 1 branch
BranchTrueFalseTotal
ini = table ? table[*s] : *s; 355 354 709
359 709 ▶ len = (STRLEN) UTF8M_LEN(ini);
Branch: 7 branches
BranchTrueFalseTotal
len = (STRLEN) UTF8M_LEN(ini); 194 515 709
len = (STRLEN) UTF8M_LEN(ini); 194 0 194
len = (STRLEN) UTF8M_LEN(ini); 192 2 194
len = (STRLEN) UTF8M_LEN(ini); 188 4 192
len = (STRLEN) UTF8M_LEN(ini); 64 124 188
len = (STRLEN) UTF8M_LEN(ini); 8 56 64
len = (STRLEN) UTF8M_LEN(ini); 3 5 8
360  
361 709 ▶ if (curlen < len || len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (curlen < len || len == 0) { 708 1 709
if (curlen < len || len == 0) { 0 708 708
362 1 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 1 0 1
363 1 *retlen = 0;
364 1 return 0;
365 }
366  
367 708 ▶ if (table) {
Branch: 1 branch
BranchTrueFalseTotal
if (table) { 354 354 708
368 1015 ▶ for (p = s, d = buff; (STRLEN)(p - s) < len; d++, p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = s, d = buff; (STRLEN)(p - s) < len; d++, p++) 661 354 1015
369 661 *d = table[*p];
370 354 s = buff;
371 }
372  
373 708 ▶ if (*s < 0xA0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s < 0xA0) { 515 193 708
374 515 uv = (UV)*s;
375 }
376 193 ▶ else if (*s < 0xE0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xE0) { 2 191 193
377 2 uv = (UV)(((s[0] & 0x1f) << 5) | (s[1] & 0x1f));
378 }
379 191 ▶ else if (*s < 0xF0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xF0) { 4 187 191
380 4 uv = (UV)(((s[0] & 0x0f) << 10) |
381 4 ((s[1] & 0x1f) << 5) | (s[2] & 0x1f));
382 }
383 187 ▶ else if (*s < 0xF8) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xF8) { 124 63 187
384 124 uv = (UV)(((s[0] & 0x07) << 15) | ((s[1] & 0x1f) << 10) |
385 124 ((s[2] & 0x1f) << 5) | (s[3] & 0x1f));
386 }
387 63 ▶ else if (*s < 0xFC) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xFC) { 55 8 63
388 55 uv = (UV)(((s[0] & 0x03) << 20) | ((s[1] & 0x1f) << 15) |
389 55 ((s[2] & 0x1f) << 10) | ((s[3] & 0x1f) << 5) |
390 55 (s[4] & 0x1f));
391 }
392 8 ▶ else if (*s < 0xFE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s < 0xFE) { 3 5 8
393 3 uv = (UV)(((s[0] & 0x01) << 25) | ((s[1] & 0x1f) << 20) |
394 3 ((s[2] & 0x1f) << 15) | ((s[3] & 0x1f) << 10) |
395 3 ((s[4] & 0x1f) << 5) | (s[5] & 0x1f));
396 }
397 else { /* (*s == 0xFF) */
398 5 uv = (UV)(((s[0] & 0x01) << 30) | ((s[1] & 0x1f) << 25) |
399 5 ((s[2] & 0x1f) << 20) | ((s[3] & 0x1f) << 15) |
400 5 ((s[4] & 0x1f) << 10) | ((s[5] & 0x1f) << 5) |
401 5 (s[6] & 0x1f));
402 }
403  
404 1355 ▶ for (i = 1; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) 647 708 1355
405 647 ▶ if (!UTF8M_TRAIL(s[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!UTF8M_TRAIL(s[i])) { 0 647 647
406 0 len = 0;
407 0 break;
408 }
409  
410 708 ▶ if (len != (STRLEN) UTF8M_SKIP(uv))
Branch: 7 branches
BranchTrueFalseTotal
if (len != (STRLEN) UTF8M_SKIP(uv)) 193 515 708
if (len != (STRLEN) UTF8M_SKIP(uv)) 191 2 193
if (len != (STRLEN) UTF8M_SKIP(uv)) 187 4 191
if (len != (STRLEN) UTF8M_SKIP(uv)) 63 124 187
if (len != (STRLEN) UTF8M_SKIP(uv)) 8 55 63
if (len != (STRLEN) UTF8M_SKIP(uv)) 3 5 8
if (len != (STRLEN) UTF8M_SKIP(uv)) 0 708 708
411 0 len = 0;
412  
413 708 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 708 0 708
414 708 *retlen = len;
415 708 return uv;
416 }
417  
418  
419 static UV
420 354 ord_in_utf8mod(U8 *s, STRLEN curlen, STRLEN *retlen)
421 {
422 354 return ord_in_utfebcdic(s, curlen, retlen, NULL);
423 }
424  
425  
426 static U8*
427 216 app_in_utf16le(U8* s, UV uv)
428 {
429 216 ▶ if (uv <= 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv <= 0xFFFF) { 209 7 216
430 209 *s++ = (U8)(uv & 0xff);
431 209 *s++ = (U8)(uv >> 8);
432 }
433 7 ▶ else if (uv <= UV_Max_UTF16) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv <= UV_Max_UTF16) { 6 1 7
434 int hi, lo;
435 6 uv -= 0x10000;
436 6 hi = (0xD800 | (uv >> 10));
437 6 lo = (0xDC00 | (uv & 0x3FF));
438 6 *s++ = (U8)(hi & 0xff);
439 6 *s++ = (U8)(hi >> 8);
440 6 *s++ = (U8)(lo & 0xff);
441 6 *s++ = (U8)(lo >> 8);
442 }
443 216 return s;
444 }
445  
446  
447 static U8*
448 216 app_in_utf16be(U8* s, UV uv)
449 {
450 216 ▶ if (uv <= 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv <= 0xFFFF) { 209 7 216
451 209 *s++ = (U8)(uv >> 8);
452 209 *s++ = (U8)(uv & 0xff);
453 }
454 7 ▶ else if (uv <= UV_Max_UTF16) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv <= UV_Max_UTF16) { 6 1 7
455 int hi, lo;
456 6 uv -= 0x10000;
457 6 hi = (0xD800 | (uv >> 10));
458 6 lo = (0xDC00 | (uv & 0x3FF));
459 6 *s++ = (U8)(hi >> 8);
460 6 *s++ = (U8)(hi & 0xff);
461 6 *s++ = (U8)(lo >> 8);
462 6 *s++ = (U8)(lo & 0xff);
463 }
464 216 return s;
465 }
466  
467  
468 static U8*
469 331 app_in_utf32le(U8* s, UV uv)
470 {
471 331 ▶ if (uv <= UV_Max_UTF32) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv <= UV_Max_UTF32) { 331 0 331
472 331 *s++ = (U8)((uv ) & 0xff);
473 331 *s++ = (U8)((uv >> 8) & 0xff);
474 331 *s++ = (U8)((uv >> 16) & 0xff);
475 331 *s++ = (U8)((uv >> 24) & 0xff);
476 }
477 331 return s;
478 }
479  
480  
481 static U8*
482 5330 app_in_utf32be(U8* s, UV uv)
483 {
484 5330 ▶ if (uv <= UV_Max_UTF32) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv <= UV_Max_UTF32) { 5330 0 5330
485 5330 *s++ = (U8)((uv >> 24) & 0xff);
486 5330 *s++ = (U8)((uv >> 16) & 0xff);
487 5330 *s++ = (U8)((uv >> 8) & 0xff);
488 5330 *s++ = (U8)((uv ) & 0xff);
489 }
490 5330 return s;
491 }
492  
493  
494 static U8*
495 333 app_in_utf8(U8* s, UV uv)
496 {
497 333 ▶ if (uv < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv < 0x80) { 260 73 333
498 260 *s++ = (U8)(uv & 0xff);
499 }
500 73 ▶ else if (uv < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x800) { 7 66 73
501 7 *s++ = (U8)(( uv >> 6) | 0xc0);
502 7 *s++ = (U8)(( uv & 0x3f) | 0x80);
503 }
504 66 ▶ else if (uv < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x10000) { 44 22 66
505 44 *s++ = (U8)(( uv >> 12) | 0xe0);
506 44 *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
507 44 *s++ = (U8)(( uv & 0x3f) | 0x80);
508 }
509 22 ▶ else if (uv < 0x200000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x200000) { 13 9 22
510 13 *s++ = (U8)(( uv >> 18) | 0xf0);
511 13 *s++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
512 13 *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
513 13 *s++ = (U8)(( uv & 0x3f) | 0x80);
514 }
515 9 ▶ else if (uv < 0x4000000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x4000000) { 4 5 9
516 4 *s++ = (U8)(( uv >> 24) | 0xf8);
517 4 *s++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
518 4 *s++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
519 4 *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
520 4 *s++ = (U8)(( uv & 0x3f) | 0x80);
521 }
522 5 ▶ else if (uv < 0x80000000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x80000000) { 5 0 5
523 5 *s++ = (U8)(( uv >> 30) | 0xfc);
524 5 *s++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
525 5 *s++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
526 5 *s++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
527 5 *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
528 5 *s++ = (U8)(( uv & 0x3f) | 0x80);
529 }
530 333 return s;
531 }
532  
533  
534 static U8*
535 642 app_in_utfebcdic(U8* s, UV uv, U8* table)
536 {
537 642 U8* p = s;
538  
539 642 ▶ if (uv < 0xa0) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv < 0xa0) { 519 123 642
540 519 *s++ = (U8)(uv & 0xff);
541 }
542 123 ▶ else if (uv < 0x400) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x400) { 3 120 123
543 3 *s++ = (U8)(( uv >> 5) | 0xc0);
544 3 *s++ = (U8)(( uv & 0x1f) | 0xa0);
545 }
546 120 ▶ else if (uv < 0x4000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x4000) { 4 116 120
547 4 *s++ = (U8)(( uv >> 10) | 0xe0);
548 4 *s++ = (U8)(((uv >> 5) & 0x1f) | 0xa0);
549 4 *s++ = (U8)(( uv & 0x1f) | 0xa0);
550 }
551 116 ▶ else if (uv < 0x40000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x40000) { 88 28 116
552 88 *s++ = (U8)(( uv >> 15) | 0xf0);
553 88 *s++ = (U8)(((uv >> 10) & 0x1f) | 0xa0);
554 88 *s++ = (U8)(((uv >> 5) & 0x1f) | 0xa0);
555 88 *s++ = (U8)(( uv & 0x1f) | 0xa0);
556 }
557 28 ▶ else if (uv < 0x400000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x400000) { 20 8 28
558 20 *s++ = (U8)(( uv >> 20) | 0xf8);
559 20 *s++ = (U8)(((uv >> 15) & 0x1f) | 0xa0);
560 20 *s++ = (U8)(((uv >> 10) & 0x1f) | 0xa0);
561 20 *s++ = (U8)(((uv >> 5) & 0x1f) | 0xa0);
562 20 *s++ = (U8)(( uv & 0x1f) | 0xa0);
563 }
564 8 ▶ else if (uv < 0x4000000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x4000000) { 3 5 8
565 3 *s++ = (U8)(( uv >> 25) | 0xfc);
566 3 *s++ = (U8)(((uv >> 20) & 0x1f) | 0xa0);
567 3 *s++ = (U8)(((uv >> 15) & 0x1f) | 0xa0);
568 3 *s++ = (U8)(((uv >> 10) & 0x1f) | 0xa0);
569 3 *s++ = (U8)(((uv >> 5) & 0x1f) | 0xa0);
570 3 *s++ = (U8)(( uv & 0x1f) | 0xa0);
571 }
572 5 ▶ else if (uv < 0x80000000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (uv < 0x80000000) { 5 0 5
573 5 *s++ = (U8)(( uv >> 30) | 0xfe);
574 5 *s++ = (U8)(((uv >> 25) & 0x1f) | 0xa0);
575 5 *s++ = (U8)(((uv >> 20) & 0x1f) | 0xa0);
576 5 *s++ = (U8)(((uv >> 15) & 0x1f) | 0xa0);
577 5 *s++ = (U8)(((uv >> 10) & 0x1f) | 0xa0);
578 5 *s++ = (U8)(((uv >> 5) & 0x1f) | 0xa0);
579 5 *s++ = (U8)(( uv & 0x1f) | 0xa0);
580 }
581  
582 642 ▶ if (table) {
Branch: 1 branch
BranchTrueFalseTotal
if (table) { 319 323 642
583 819 ▶ for ( ; p < s; p++)
Branch: 1 branch
BranchTrueFalseTotal
for ( ; p < s; p++) 500 319 819
584 500 *p = table[*p];
585 }
586  
587 642 return s;
588 }
589  
590  
591 static U8*
592 323 app_in_utf8mod(U8* s, UV uv)
593 {
594 323 return app_in_utfebcdic(s, uv, NULL);
595 }
596  
597  
598 static U8 e2i_cp1047[] = {
599 0x00, 0x01, 0x02, 0x03, 0x9C, 0x09, 0x86, 0x7F,
600 0x97, 0x8D, 0x8E, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
601 0x10, 0x11, 0x12, 0x13, 0x9D, 0x0A, 0x08, 0x87,
602 0x18, 0x19, 0x92, 0x8F, 0x1C, 0x1D, 0x1E, 0x1F,
603 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1B,
604 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x05, 0x06, 0x07,
605 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
606 0x98, 0x99, 0x9A, 0x9B, 0x14, 0x15, 0x9E, 0x1A,
607  
608 0x20, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
609 0xA7, 0xA8, 0xA9, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
610 0x26, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0,
611 0xB1, 0xB2, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0x5E,
612 0x2D, 0x2F, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
613 0xB9, 0xBA, 0xBB, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
614 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3,
615 0xC4, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
616  
617 0xC5, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
618 0x68, 0x69, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB,
619 0xCC, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
620 0x71, 0x72, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2,
621 0xD3, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
622 0x79, 0x7A, 0xD4, 0xD5, 0xD6, 0x5B, 0xD7, 0xD8,
623 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0,
624 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0x5D, 0xE6, 0xE7,
625  
626 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
627 0x48, 0x49, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED,
628 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
629 0x51, 0x52, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3,
630 0x5C, 0xF4, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
631 0x59, 0x5A, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
632 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
633 0x38, 0x39, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x9F,
634 };
635  
636 static UV
637 355 ord_in_utfcp1047(U8 *s, STRLEN curlen, STRLEN *retlen)
638 {
639 355 return ord_in_utfebcdic(s, curlen, retlen, e2i_cp1047);
640 }
641  
642  
643 static U8 i2e_cp1047[] = {
644 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
645 0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
646 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
647 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
648 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
649 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
650 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
651 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
652  
653 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
654 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
655 0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
656 0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
657 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
658 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
659 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
660 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
661  
662 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17,
663 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x09, 0x0A, 0x1B,
664 0x30, 0x31, 0x1A, 0x33, 0x34, 0x35, 0x36, 0x08,
665 0x38, 0x39, 0x3A, 0x3B, 0x04, 0x14, 0x3E, 0xFF,
666 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
667 0x49, 0x4A, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
668 0x57, 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66,
669 0x67, 0x68, 0x69, 0x6A, 0x70, 0x71, 0x72, 0x73,
670  
671 0x74, 0x75, 0x76, 0x77, 0x78, 0x80, 0x8A, 0x8B,
672 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x9A, 0x9B, 0x9C,
673 0x9D, 0x9E, 0x9F, 0xA0, 0xAA, 0xAB, 0xAC, 0xAE,
674 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
675 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBE, 0xBF,
676 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xDA, 0xDB,
677 0xDC, 0xDD, 0xDE, 0xDF, 0xE1, 0xEA, 0xEB, 0xEC,
678 0xED, 0xEE, 0xEF, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE,
679 };
680  
681 static U8*
682 319 app_in_utfcp1047(U8* s, UV uv)
683 {
684 319 return app_in_utfebcdic(s, uv, i2e_cp1047);
685 }
686  
687 #endif /* UNITRANS_H */
↑ Summary Transform.xs »