This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlio:
[perl5.git] / utf8.c
CommitLineData
a0ed51b3
LW
1/* utf8.c
2 *
3818b22b 3 * Copyright (c) 1998-2000, Larry Wall
a0ed51b3
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * 'What a fix!' said Sam. 'That's the one place in all the lands we've ever
12 * heard of that we don't want to see any closer; and that's the one place
13 * we're trying to get to! And that's just where we can't get, nohow.'
14 *
15 * 'Well do I understand your speech,' he answered in the same language;
16 * 'yet few strangers do so. Why then do you not speak in the Common Tongue,
17 * as is the custom in the West, if you wish to be answered?'
18 *
19 * ...the travellers perceived that the floor was paved with stones of many
20 * hues; branching runes and strange devices intertwined beneath their feet.
21 */
22
23#include "EXTERN.h"
864dbfa3 24#define PERL_IN_UTF8_C
a0ed51b3
LW
25#include "perl.h"
26
27/* Unicode support */
28
dfe13c55 29U8 *
ad391ad9 30Perl_uv_to_utf8(pTHX_ U8 *d, UV uv) /* the d must be UTF8_MAXLEN+1 deep */
a0ed51b3
LW
31{
32 if (uv < 0x80) {
33 *d++ = uv;
ad391ad9 34 *d = 0;
a0ed51b3
LW
35 return d;
36 }
37 if (uv < 0x800) {
38 *d++ = (( uv >> 6) | 0xc0);
39 *d++ = (( uv & 0x3f) | 0x80);
ad391ad9 40 *d = 0;
a0ed51b3
LW
41 return d;
42 }
43 if (uv < 0x10000) {
44 *d++ = (( uv >> 12) | 0xe0);
45 *d++ = (((uv >> 6) & 0x3f) | 0x80);
46 *d++ = (( uv & 0x3f) | 0x80);
ad391ad9 47 *d = 0;
a0ed51b3
LW
48 return d;
49 }
50 if (uv < 0x200000) {
51 *d++ = (( uv >> 18) | 0xf0);
52 *d++ = (((uv >> 12) & 0x3f) | 0x80);
53 *d++ = (((uv >> 6) & 0x3f) | 0x80);
54 *d++ = (( uv & 0x3f) | 0x80);
ad391ad9 55 *d = 0;
a0ed51b3
LW
56 return d;
57 }
58 if (uv < 0x4000000) {
59 *d++ = (( uv >> 24) | 0xf8);
60 *d++ = (((uv >> 18) & 0x3f) | 0x80);
61 *d++ = (((uv >> 12) & 0x3f) | 0x80);
62 *d++ = (((uv >> 6) & 0x3f) | 0x80);
63 *d++ = (( uv & 0x3f) | 0x80);
ad391ad9 64 *d = 0;
a0ed51b3
LW
65 return d;
66 }
67 if (uv < 0x80000000) {
68 *d++ = (( uv >> 30) | 0xfc);
69 *d++ = (((uv >> 24) & 0x3f) | 0x80);
70 *d++ = (((uv >> 18) & 0x3f) | 0x80);
71 *d++ = (((uv >> 12) & 0x3f) | 0x80);
72 *d++ = (((uv >> 6) & 0x3f) | 0x80);
73 *d++ = (( uv & 0x3f) | 0x80);
ad391ad9 74 *d = 0;
a0ed51b3
LW
75 return d;
76 }
6b8eaf93 77#ifdef HAS_QUAD
d7578b48 78 if (uv < UTF8_QUAD_MAX)
a0ed51b3
LW
79#endif
80 {
81 *d++ = 0xfe; /* Can't match U+FEFF! */
82 *d++ = (((uv >> 30) & 0x3f) | 0x80);
83 *d++ = (((uv >> 24) & 0x3f) | 0x80);
84 *d++ = (((uv >> 18) & 0x3f) | 0x80);
85 *d++ = (((uv >> 12) & 0x3f) | 0x80);
86 *d++ = (((uv >> 6) & 0x3f) | 0x80);
87 *d++ = (( uv & 0x3f) | 0x80);
ad391ad9 88 *d = 0;
a0ed51b3
LW
89 return d;
90 }
6b8eaf93 91#ifdef HAS_QUAD
a0ed51b3
LW
92 {
93 *d++ = 0xff; /* Can't match U+FFFE! */
3c77ea2b
GS
94 *d++ = 0x80; /* 6 Reserved bits */
95 *d++ = (((uv >> 60) & 0x0f) | 0x80); /* 2 Reserved bits */
96 *d++ = (((uv >> 54) & 0x3f) | 0x80);
97 *d++ = (((uv >> 48) & 0x3f) | 0x80);
98 *d++ = (((uv >> 42) & 0x3f) | 0x80);
a0ed51b3
LW
99 *d++ = (((uv >> 36) & 0x3f) | 0x80);
100 *d++ = (((uv >> 30) & 0x3f) | 0x80);
101 *d++ = (((uv >> 24) & 0x3f) | 0x80);
102 *d++ = (((uv >> 18) & 0x3f) | 0x80);
103 *d++ = (((uv >> 12) & 0x3f) | 0x80);
104 *d++ = (((uv >> 6) & 0x3f) | 0x80);
105 *d++ = (( uv & 0x3f) | 0x80);
ad391ad9 106 *d = 0;
a0ed51b3
LW
107 return d;
108 }
109#endif
110}
111
386d01d6
GS
112/* Tests if some arbitrary number of bytes begins in a valid UTF-8 character.
113 * The actual number of bytes in the UTF-8 character will be returned if it
114 * is valid, otherwise 0. */
067a85ef 115STRLEN
386d01d6
GS
116Perl_is_utf8_char(pTHX_ U8 *s)
117{
118 U8 u = *s;
067a85ef
A
119 STRLEN slen, len;
120 UV uv, ouv;
386d01d6 121
067a85ef 122 if (u <= 0x7f)
386d01d6
GS
123 return 1;
124
067a85ef 125 if (u >= 0x80 && u <= 0xbf)
386d01d6
GS
126 return 0;
127
9f07fdcd 128 len = UTF8SKIP(s);
386d01d6 129
067a85ef
A
130 if (len < 2 || (u >= 0xc0 && u <= 0xfd && s[1] < 0x80))
131 return 0;
132
386d01d6
GS
133 slen = len - 1;
134 s++;
067a85ef
A
135 uv = u;
136 ouv = uv;
386d01d6
GS
137 while (slen--) {
138 if ((*s & 0xc0) != 0x80)
139 return 0;
067a85ef
A
140 uv = (uv << 6) | (*s & 0x3f);
141 if (uv < ouv)
142 return 0;
143 ouv = uv;
386d01d6
GS
144 s++;
145 }
067a85ef 146
5bbb0b5a 147 if (UNISKIP(uv) < len)
067a85ef
A
148 return 0;
149
386d01d6
GS
150 return len;
151}
152
6662521e 153/*
b2a2e44b 154=for apidoc Am|is_utf8_string|U8 *s|STRLEN len
6662521e
GS
155
156Returns true if first C<len> bytes of the given string form valid a UTF8
157string, false otherwise.
158
159=cut
160*/
161
8e84507e 162bool
6662521e
GS
163Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
164{
067a85ef
A
165 U8* x = s;
166 U8* send = s + len;
167 STRLEN c;
168
6662521e
GS
169 while (x < send) {
170 c = is_utf8_char(x);
067a85ef
A
171 if (!c)
172 return FALSE;
6662521e 173 x += c;
067a85ef
A
174 if (x > send)
175 return FALSE;
6662521e 176 }
067a85ef
A
177
178 return TRUE;
6662521e
GS
179}
180
67e989fb 181/*
be2c7115 182=for apidoc Am|U8* s|utf8_to_uv|STRLEN curlen|STRLEN *retlen|U32 flags
67e989fb
JH
183
184Returns the character value of the first character in the string C<s>
ba210ebe
JH
185which is assumed to be in UTF8 encoding and no longer than C<curlen>;
186C<retlen> will be set to the length, in bytes, of that character,
187and the pointer C<s> will be advanced to the end of the character.
67e989fb
JH
188
189If C<s> does not point to a well-formed UTF8 character, the behaviour
dcad2880
JH
190is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
191it is assumed that the caller will raise a warning, and this function
192will set C<retlen> to C<-1> and return. The C<flags> can also contain
be2c7115
PP
193various flags to allow deviations from the strict UTF-8 encoding
194(see F<utf8.h>).
67e989fb 195
dcad2880 196=cut */
67e989fb 197
a0ed51b3 198UV
dcad2880 199Perl_utf8_to_uv(pTHX_ U8* s, STRLEN curlen, STRLEN* retlen, U32 flags)
a0ed51b3 200{
ba210ebe
JH
201 UV uv = *s, ouv;
202 STRLEN len = 1;
7bf1b6bb
PP
203#ifdef EBCDIC
204 bool dowarn = 0;
205#else
ba210ebe 206 bool dowarn = ckWARN_d(WARN_UTF8);
7bf1b6bb 207#endif
ba210ebe
JH
208 STRLEN expectlen = 0;
209
0c443dc2
JH
210 if (curlen == 0) {
211 if (dowarn)
212 Perl_warner(aTHX_ WARN_UTF8,
213 "Malformed UTF-8 character (an empty string)");
214 goto malformed;
215 }
216
ba210ebe 217 if (uv <= 0x7f) { /* Pure ASCII. */
a0ed51b3
LW
218 if (retlen)
219 *retlen = 1;
220 return *s;
221 }
67e989fb 222
fcc8fcf6
JH
223 if ((uv >= 0x80 && uv <= 0xbf) &&
224 !(flags & UTF8_ALLOW_CONTINUATION)) {
ba210ebe
JH
225 if (dowarn)
226 Perl_warner(aTHX_ WARN_UTF8,
efbcad09 227 "Malformed UTF-8 character (unexpected continuation byte 0x%02"UVxf")",
ba210ebe
JH
228 uv);
229 goto malformed;
230 }
231
0c443dc2 232 if ((uv >= 0xc0 && uv <= 0xfd && curlen > 1 && s[1] < 0x80) &&
fcc8fcf6 233 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
ba210ebe
JH
234 if (dowarn)
235 Perl_warner(aTHX_ WARN_UTF8,
efbcad09 236 "Malformed UTF-8 character (unexpected non-continuation byte 0x%02"UVxf" after byte 0x%02"UVxf")",
9e29e5ff 237 (UV)s[1], uv);
ba210ebe
JH
238 goto malformed;
239 }
fcc8fcf6
JH
240
241 if ((uv == 0xfe || uv == 0xff) &&
242 !(flags & UTF8_ALLOW_FE_FF)) {
ba210ebe
JH
243 if (dowarn)
244 Perl_warner(aTHX_ WARN_UTF8,
efbcad09 245 "Malformed UTF-8 character (byte 0x%02"UVxf")",
ba210ebe
JH
246 uv);
247 goto malformed;
a0ed51b3 248 }
fcc8fcf6 249
ba210ebe
JH
250 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
251 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
252 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
253 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
254 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
255 else if (!(uv & 0x01)) { len = 7; uv = 0; }
3c77ea2b 256 else { len = 13; uv = 0; } /* whoa! */
fcc8fcf6 257
a0ed51b3
LW
258 if (retlen)
259 *retlen = len;
ba210ebe
JH
260
261 expectlen = len;
262
fcc8fcf6
JH
263 if ((curlen < expectlen) &&
264 !(flags & UTF8_ALLOW_SHORT)) {
ba210ebe
JH
265 if (dowarn)
266 Perl_warner(aTHX_ WARN_UTF8,
267 "Malformed UTF-8 character (%d byte%s, need %d)",
0c443dc2 268 curlen, curlen == 1 ? "" : "s", expectlen);
ba210ebe
JH
269 goto malformed;
270 }
271
272 len--;
a0ed51b3 273 s++;
ba210ebe
JH
274 ouv = uv;
275
a0ed51b3
LW
276 while (len--) {
277 if ((*s & 0xc0) != 0x80) {
ba210ebe
JH
278 if (dowarn)
279 Perl_warner(aTHX_ WARN_UTF8,
280 "Malformed UTF-8 character (unexpected continuation byte 0x%02x)",
281 *s);
282 goto malformed;
a0ed51b3
LW
283 }
284 else
ba210ebe
JH
285 uv = (uv << 6) | (*s & 0x3f);
286 if (uv < ouv) {
fcc8fcf6 287 /* This cannot be allowed. */
ba210ebe
JH
288 if (dowarn)
289 Perl_warner(aTHX_ WARN_UTF8,
290 "Malformed UTF-8 character (overflow at 0x%"UVxf", byte 0x%02x)",
291 ouv, *s);
292 goto malformed;
293 }
294 s++;
295 ouv = uv;
296 }
297
fcc8fcf6
JH
298 if ((uv >= 0xd800 && uv <= 0xdfff) &&
299 !(flags & UTF8_ALLOW_SURROGATE)) {
ba210ebe
JH
300 if (dowarn)
301 Perl_warner(aTHX_ WARN_UTF8,
302 "Malformed UTF-8 character (UTF-16 surrogate 0x%04"UVxf")",
303 uv);
304 goto malformed;
fcc8fcf6
JH
305 } else if ((uv == 0xfffe) &&
306 !(flags & UTF8_ALLOW_BOM)) {
ba210ebe
JH
307 if (dowarn)
308 Perl_warner(aTHX_ WARN_UTF8,
309 "Malformed UTF-8 character (byte order mark 0x%04"UVxf")",
310 uv);
311 goto malformed;
fcc8fcf6
JH
312 } else if ((expectlen > UNISKIP(uv)) &&
313 !(flags & UTF8_ALLOW_LONG)) {
ba210ebe
JH
314 if (dowarn)
315 Perl_warner(aTHX_ WARN_UTF8,
316 "Malformed UTF-8 character (%d byte%s, need %d)",
0c443dc2 317 expectlen, expectlen == 1 ? "": "s", UNISKIP(uv));
ba210ebe 318 goto malformed;
a9917092
JH
319 } else if ((uv == 0xffff) &&
320 !(flags & UTF8_ALLOW_FFFF)) {
321 if (dowarn)
322 Perl_warner(aTHX_ WARN_UTF8,
323 "Malformed UTF-8 character (character 0x%04"UVxf")",
324 uv);
325 goto malformed;
a0ed51b3 326 }
ba210ebe 327
a0ed51b3 328 return uv;
ba210ebe
JH
329
330malformed:
331
fcc8fcf6 332 if (flags & UTF8_CHECK_ONLY) {
ba210ebe 333 if (retlen)
cc366d4b 334 *retlen = -1;
ba210ebe
JH
335 return 0;
336 }
337
338 if (retlen)
cc366d4b 339 *retlen = expectlen ? expectlen : len;
ba210ebe
JH
340
341 return UNICODE_REPLACEMENT_CHARACTER;
a0ed51b3
LW
342}
343
8e84507e 344/*
dcad2880 345=for apidoc Am|U8* s|utf8_to_uv_simple|STRLEN *retlen
8e84507e
NIS
346
347Returns the character value of the first character in the string C<s>
348which is assumed to be in UTF8 encoding; C<retlen> will be set to the
349length, in bytes, of that character, and the pointer C<s> will be
350advanced to the end of the character.
351
ba210ebe
JH
352If C<s> does not point to a well-formed UTF8 character, zero is
353returned and retlen is set, if possible, to -1.
8e84507e
NIS
354
355=cut
356*/
357
358UV
dcad2880 359Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
8e84507e 360{
2e4dc9fc 361 return Perl_utf8_to_uv(aTHX_ s, UTF8_MAXLEN, retlen, 0);
8e84507e
NIS
362}
363
b76347f2
JH
364/*
365=for apidoc|utf8_length|U8 *s|U8 *e
366
367Return the length of the UTF-8 char encoded string C<s> in characters.
02eb7b47
JH
368Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end
369up past C<e>, croaks.
b76347f2
JH
370
371=cut
372*/
373
374STRLEN
375Perl_utf8_length(pTHX_ U8* s, U8* e)
376{
377 STRLEN len = 0;
378
379 if (e < s)
02eb7b47 380 Perl_croak(aTHX_ "panic: utf8_length: unexpected end");
b76347f2 381 while (s < e) {
02eb7b47 382 U8 t = UTF8SKIP(s);
b76347f2
JH
383
384 if (e - s < t)
02eb7b47 385 Perl_croak(aTHX_ "panic: utf8_length: unaligned end");
b76347f2
JH
386 s += t;
387 len++;
388 }
389
390 return len;
391}
392
246fae53
MG
393/* utf8_distance(a,b) returns the number of UTF8 characters between
394 the pointers a and b */
a0ed51b3 395
02eb7b47 396IV
864dbfa3 397Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
a0ed51b3 398{
02eb7b47
JH
399 IV off = 0;
400
a0ed51b3
LW
401 if (a < b) {
402 while (a < b) {
02eb7b47
JH
403 U8 c = UTF8SKIP(a);
404
405 if (b - a < c)
406 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
407 a += c;
a0ed51b3
LW
408 off--;
409 }
410 }
411 else {
412 while (b < a) {
02eb7b47
JH
413 U8 c = UTF8SKIP(b);
414
415 if (a - b < c)
416 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
417 b += c;
a0ed51b3
LW
418 off++;
419 }
420 }
02eb7b47 421
a0ed51b3
LW
422 return off;
423}
424
425/* WARNING: do not use the following unless you *know* off is within bounds */
426
427U8 *
864dbfa3 428Perl_utf8_hop(pTHX_ U8 *s, I32 off)
a0ed51b3
LW
429{
430 if (off >= 0) {
431 while (off--)
432 s += UTF8SKIP(s);
433 }
434 else {
435 while (off++) {
436 s--;
437 if (*s & 0x80) {
438 while ((*s & 0xc0) == 0x80)
439 s--;
440 }
441 }
442 }
443 return s;
444}
445
6940069f 446/*
246fae53 447=for apidoc Am|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
6940069f 448
246fae53
MG
449Converts a string C<s> of length C<len> from UTF8 into byte encoding.
450Unlike C<bytes_to_utf8>, this over-writes the original string, and
451updates len to contain the new length.
67e989fb 452Returns zero on failure, setting C<len> to -1.
6940069f
GS
453
454=cut
455*/
456
457U8 *
246fae53 458Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
6940069f 459{
6940069f
GS
460 U8 *send;
461 U8 *d;
dcad2880 462 U8 *save = s;
246fae53
MG
463
464 /* ensure valid UTF8 and chars < 256 before updating string */
dcad2880
JH
465 for (send = s + *len; s < send; ) {
466 U8 c = *s++;
467
9f9ab905 468 if (c >= 0x80 &&
dcad2880
JH
469 ((s >= send) ||
470 ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
471 *len = -1;
472 return 0;
473 }
246fae53 474 }
dcad2880
JH
475
476 d = s = save;
6940069f 477 while (s < send) {
dcad2880
JH
478 if (*s < 0x80) {
479 *d++ = *s++;
480 }
6940069f 481 else {
ba210ebe 482 STRLEN ulen;
dcad2880 483 *d++ = (U8)utf8_to_uv_simple(s, &ulen);
6940069f 484 s += ulen;
6940069f
GS
485 }
486 }
487 *d = '\0';
246fae53 488 *len = d - save;
6940069f
GS
489 return save;
490}
491
492/*
6662521e 493=for apidoc Am|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
6940069f
GS
494
495Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
6662521e
GS
496Returns a pointer to the newly-created string, and sets C<len> to
497reflect the new length.
6940069f 498
497711e7 499=cut
6940069f
GS
500*/
501
502U8*
6662521e 503Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
6940069f 504{
6940069f
GS
505 U8 *send;
506 U8 *d;
507 U8 *dst;
6662521e 508 send = s + (*len);
6940069f 509
6662521e 510 Newz(801, d, (*len) * 2 + 1, U8);
6940069f
GS
511 dst = d;
512
513 while (s < send) {
514 if (*s < 0x80)
515 *d++ = *s++;
516 else {
517 UV uv = *s++;
518 *d++ = (( uv >> 6) | 0xc0);
519 *d++ = (( uv & 0x3f) | 0x80);
520 }
521 }
522 *d = '\0';
6662521e 523 *len = d-dst;
6940069f
GS
524 return dst;
525}
526
a0ed51b3 527/*
dea0fc0b 528 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
a0ed51b3
LW
529 *
530 * Destination must be pre-extended to 3/2 source. Do not use in-place.
531 * We optimize for native, for obvious reasons. */
532
533U8*
dea0fc0b 534Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
a0ed51b3 535{
dea0fc0b
JH
536 U8* pend;
537 U8* dstart = d;
538
539 if (bytelen & 1)
a7867d0a 540 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
dea0fc0b
JH
541
542 pend = p + bytelen;
543
a0ed51b3 544 while (p < pend) {
dea0fc0b
JH
545 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
546 p += 2;
a0ed51b3
LW
547 if (uv < 0x80) {
548 *d++ = uv;
549 continue;
550 }
551 if (uv < 0x800) {
552 *d++ = (( uv >> 6) | 0xc0);
553 *d++ = (( uv & 0x3f) | 0x80);
554 continue;
555 }
556 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
dea0fc0b
JH
557 UV low = *p++;
558 if (low < 0xdc00 || low >= 0xdfff)
559 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
a0ed51b3
LW
560 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
561 }
562 if (uv < 0x10000) {
563 *d++ = (( uv >> 12) | 0xe0);
564 *d++ = (((uv >> 6) & 0x3f) | 0x80);
565 *d++ = (( uv & 0x3f) | 0x80);
566 continue;
567 }
568 else {
569 *d++ = (( uv >> 18) | 0xf0);
570 *d++ = (((uv >> 12) & 0x3f) | 0x80);
571 *d++ = (((uv >> 6) & 0x3f) | 0x80);
572 *d++ = (( uv & 0x3f) | 0x80);
573 continue;
574 }
575 }
dea0fc0b 576 *newlen = d - dstart;
a0ed51b3
LW
577 return d;
578}
579
580/* Note: this one is slightly destructive of the source. */
581
582U8*
dea0fc0b 583Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
a0ed51b3
LW
584{
585 U8* s = (U8*)p;
586 U8* send = s + bytelen;
587 while (s < send) {
588 U8 tmp = s[0];
589 s[0] = s[1];
590 s[1] = tmp;
591 s += 2;
592 }
dea0fc0b 593 return utf16_to_utf8(p, d, bytelen, newlen);
a0ed51b3
LW
594}
595
596/* for now these are all defined (inefficiently) in terms of the utf8 versions */
597
598bool
864dbfa3 599Perl_is_uni_alnum(pTHX_ U32 c)
a0ed51b3 600{
ad391ad9 601 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
602 uv_to_utf8(tmpbuf, (UV)c);
603 return is_utf8_alnum(tmpbuf);
604}
605
606bool
b8c5462f
JH
607Perl_is_uni_alnumc(pTHX_ U32 c)
608{
ad391ad9 609 U8 tmpbuf[UTF8_MAXLEN+1];
b8c5462f
JH
610 uv_to_utf8(tmpbuf, (UV)c);
611 return is_utf8_alnumc(tmpbuf);
612}
613
614bool
864dbfa3 615Perl_is_uni_idfirst(pTHX_ U32 c)
a0ed51b3 616{
ad391ad9 617 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
618 uv_to_utf8(tmpbuf, (UV)c);
619 return is_utf8_idfirst(tmpbuf);
620}
621
622bool
864dbfa3 623Perl_is_uni_alpha(pTHX_ U32 c)
a0ed51b3 624{
ad391ad9 625 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
626 uv_to_utf8(tmpbuf, (UV)c);
627 return is_utf8_alpha(tmpbuf);
628}
629
630bool
4d61ec05
GS
631Perl_is_uni_ascii(pTHX_ U32 c)
632{
ad391ad9 633 U8 tmpbuf[UTF8_MAXLEN+1];
4d61ec05
GS
634 uv_to_utf8(tmpbuf, (UV)c);
635 return is_utf8_ascii(tmpbuf);
636}
637
638bool
864dbfa3 639Perl_is_uni_space(pTHX_ U32 c)
a0ed51b3 640{
ad391ad9 641 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
642 uv_to_utf8(tmpbuf, (UV)c);
643 return is_utf8_space(tmpbuf);
644}
645
646bool
864dbfa3 647Perl_is_uni_digit(pTHX_ U32 c)
a0ed51b3 648{
ad391ad9 649 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
650 uv_to_utf8(tmpbuf, (UV)c);
651 return is_utf8_digit(tmpbuf);
652}
653
654bool
864dbfa3 655Perl_is_uni_upper(pTHX_ U32 c)
a0ed51b3 656{
ad391ad9 657 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
658 uv_to_utf8(tmpbuf, (UV)c);
659 return is_utf8_upper(tmpbuf);
660}
661
662bool
864dbfa3 663Perl_is_uni_lower(pTHX_ U32 c)
a0ed51b3 664{
ad391ad9 665 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
666 uv_to_utf8(tmpbuf, (UV)c);
667 return is_utf8_lower(tmpbuf);
668}
669
670bool
b8c5462f
JH
671Perl_is_uni_cntrl(pTHX_ U32 c)
672{
ad391ad9 673 U8 tmpbuf[UTF8_MAXLEN+1];
b8c5462f
JH
674 uv_to_utf8(tmpbuf, (UV)c);
675 return is_utf8_cntrl(tmpbuf);
676}
677
678bool
679Perl_is_uni_graph(pTHX_ U32 c)
680{
ad391ad9 681 U8 tmpbuf[UTF8_MAXLEN+1];
b8c5462f
JH
682 uv_to_utf8(tmpbuf, (UV)c);
683 return is_utf8_graph(tmpbuf);
684}
685
686bool
864dbfa3 687Perl_is_uni_print(pTHX_ U32 c)
a0ed51b3 688{
ad391ad9 689 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
690 uv_to_utf8(tmpbuf, (UV)c);
691 return is_utf8_print(tmpbuf);
692}
693
b8c5462f 694bool
f248d071 695Perl_is_uni_punct(pTHX_ U32 c)
b8c5462f 696{
ad391ad9 697 U8 tmpbuf[UTF8_MAXLEN+1];
b8c5462f
JH
698 uv_to_utf8(tmpbuf, (UV)c);
699 return is_utf8_punct(tmpbuf);
700}
701
4d61ec05
GS
702bool
703Perl_is_uni_xdigit(pTHX_ U32 c)
704{
ad391ad9 705 U8 tmpbuf[UTF8_MAXLEN+1];
4d61ec05
GS
706 uv_to_utf8(tmpbuf, (UV)c);
707 return is_utf8_xdigit(tmpbuf);
708}
709
a0ed51b3 710U32
864dbfa3 711Perl_to_uni_upper(pTHX_ U32 c)
a0ed51b3 712{
ad391ad9 713 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
714 uv_to_utf8(tmpbuf, (UV)c);
715 return to_utf8_upper(tmpbuf);
716}
717
718U32
864dbfa3 719Perl_to_uni_title(pTHX_ U32 c)
a0ed51b3 720{
ad391ad9 721 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
722 uv_to_utf8(tmpbuf, (UV)c);
723 return to_utf8_title(tmpbuf);
724}
725
726U32
864dbfa3 727Perl_to_uni_lower(pTHX_ U32 c)
a0ed51b3 728{
ad391ad9 729 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
730 uv_to_utf8(tmpbuf, (UV)c);
731 return to_utf8_lower(tmpbuf);
732}
733
734/* for now these all assume no locale info available for Unicode > 255 */
735
736bool
864dbfa3 737Perl_is_uni_alnum_lc(pTHX_ U32 c)
a0ed51b3
LW
738{
739 return is_uni_alnum(c); /* XXX no locale support yet */
740}
741
742bool
b8c5462f
JH
743Perl_is_uni_alnumc_lc(pTHX_ U32 c)
744{
745 return is_uni_alnumc(c); /* XXX no locale support yet */
746}
747
748bool
864dbfa3 749Perl_is_uni_idfirst_lc(pTHX_ U32 c)
a0ed51b3
LW
750{
751 return is_uni_idfirst(c); /* XXX no locale support yet */
752}
753
754bool
864dbfa3 755Perl_is_uni_alpha_lc(pTHX_ U32 c)
a0ed51b3
LW
756{
757 return is_uni_alpha(c); /* XXX no locale support yet */
758}
759
760bool
4d61ec05
GS
761Perl_is_uni_ascii_lc(pTHX_ U32 c)
762{
763 return is_uni_ascii(c); /* XXX no locale support yet */
764}
765
766bool
864dbfa3 767Perl_is_uni_space_lc(pTHX_ U32 c)
a0ed51b3
LW
768{
769 return is_uni_space(c); /* XXX no locale support yet */
770}
771
772bool
864dbfa3 773Perl_is_uni_digit_lc(pTHX_ U32 c)
a0ed51b3
LW
774{
775 return is_uni_digit(c); /* XXX no locale support yet */
776}
777
778bool
864dbfa3 779Perl_is_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
780{
781 return is_uni_upper(c); /* XXX no locale support yet */
782}
783
784bool
864dbfa3 785Perl_is_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
786{
787 return is_uni_lower(c); /* XXX no locale support yet */
788}
789
790bool
b8c5462f
JH
791Perl_is_uni_cntrl_lc(pTHX_ U32 c)
792{
793 return is_uni_cntrl(c); /* XXX no locale support yet */
794}
795
796bool
797Perl_is_uni_graph_lc(pTHX_ U32 c)
798{
799 return is_uni_graph(c); /* XXX no locale support yet */
800}
801
802bool
864dbfa3 803Perl_is_uni_print_lc(pTHX_ U32 c)
a0ed51b3
LW
804{
805 return is_uni_print(c); /* XXX no locale support yet */
806}
807
b8c5462f
JH
808bool
809Perl_is_uni_punct_lc(pTHX_ U32 c)
810{
811 return is_uni_punct(c); /* XXX no locale support yet */
812}
813
4d61ec05
GS
814bool
815Perl_is_uni_xdigit_lc(pTHX_ U32 c)
816{
817 return is_uni_xdigit(c); /* XXX no locale support yet */
818}
819
a0ed51b3 820U32
864dbfa3 821Perl_to_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
822{
823 return to_uni_upper(c); /* XXX no locale support yet */
824}
825
826U32
864dbfa3 827Perl_to_uni_title_lc(pTHX_ U32 c)
a0ed51b3
LW
828{
829 return to_uni_title(c); /* XXX no locale support yet */
830}
831
832U32
864dbfa3 833Perl_to_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
834{
835 return to_uni_lower(c); /* XXX no locale support yet */
836}
837
a0ed51b3 838bool
864dbfa3 839Perl_is_utf8_alnum(pTHX_ U8 *p)
a0ed51b3 840{
386d01d6
GS
841 if (!is_utf8_char(p))
842 return FALSE;
a0ed51b3 843 if (!PL_utf8_alnum)
289d4f09
ML
844 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
845 * descendant of isalnum(3), in other words, it doesn't
846 * contain the '_'. --jhi */
847 PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
a0ed51b3
LW
848 return swash_fetch(PL_utf8_alnum, p);
849/* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
850#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
851 if (!PL_utf8_alnum)
852 PL_utf8_alnum = swash_init("utf8", "",
853 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
854 return swash_fetch(PL_utf8_alnum, p);
855#endif
856}
857
858bool
b8c5462f
JH
859Perl_is_utf8_alnumc(pTHX_ U8 *p)
860{
386d01d6
GS
861 if (!is_utf8_char(p))
862 return FALSE;
b8c5462f
JH
863 if (!PL_utf8_alnum)
864 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
865 return swash_fetch(PL_utf8_alnum, p);
866/* return is_utf8_alpha(p) || is_utf8_digit(p); */
867#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
868 if (!PL_utf8_alnum)
869 PL_utf8_alnum = swash_init("utf8", "",
870 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
871 return swash_fetch(PL_utf8_alnum, p);
872#endif
873}
874
875bool
864dbfa3 876Perl_is_utf8_idfirst(pTHX_ U8 *p)
a0ed51b3
LW
877{
878 return *p == '_' || is_utf8_alpha(p);
879}
880
881bool
864dbfa3 882Perl_is_utf8_alpha(pTHX_ U8 *p)
a0ed51b3 883{
386d01d6
GS
884 if (!is_utf8_char(p))
885 return FALSE;
a0ed51b3 886 if (!PL_utf8_alpha)
e24b16f9 887 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
a0ed51b3
LW
888 return swash_fetch(PL_utf8_alpha, p);
889}
890
891bool
b8c5462f
JH
892Perl_is_utf8_ascii(pTHX_ U8 *p)
893{
386d01d6
GS
894 if (!is_utf8_char(p))
895 return FALSE;
b8c5462f
JH
896 if (!PL_utf8_ascii)
897 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
898 return swash_fetch(PL_utf8_ascii, p);
899}
900
901bool
864dbfa3 902Perl_is_utf8_space(pTHX_ U8 *p)
a0ed51b3 903{
386d01d6
GS
904 if (!is_utf8_char(p))
905 return FALSE;
a0ed51b3 906 if (!PL_utf8_space)
3bec3564 907 PL_utf8_space = swash_init("utf8", "IsSpacePerl", &PL_sv_undef, 0, 0);
a0ed51b3
LW
908 return swash_fetch(PL_utf8_space, p);
909}
910
911bool
864dbfa3 912Perl_is_utf8_digit(pTHX_ U8 *p)
a0ed51b3 913{
386d01d6
GS
914 if (!is_utf8_char(p))
915 return FALSE;
a0ed51b3 916 if (!PL_utf8_digit)
e24b16f9 917 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
a0ed51b3
LW
918 return swash_fetch(PL_utf8_digit, p);
919}
920
921bool
864dbfa3 922Perl_is_utf8_upper(pTHX_ U8 *p)
a0ed51b3 923{
386d01d6
GS
924 if (!is_utf8_char(p))
925 return FALSE;
a0ed51b3 926 if (!PL_utf8_upper)
e24b16f9 927 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
a0ed51b3
LW
928 return swash_fetch(PL_utf8_upper, p);
929}
930
931bool
864dbfa3 932Perl_is_utf8_lower(pTHX_ U8 *p)
a0ed51b3 933{
386d01d6
GS
934 if (!is_utf8_char(p))
935 return FALSE;
a0ed51b3 936 if (!PL_utf8_lower)
e24b16f9 937 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
a0ed51b3
LW
938 return swash_fetch(PL_utf8_lower, p);
939}
940
941bool
b8c5462f
JH
942Perl_is_utf8_cntrl(pTHX_ U8 *p)
943{
386d01d6
GS
944 if (!is_utf8_char(p))
945 return FALSE;
b8c5462f
JH
946 if (!PL_utf8_cntrl)
947 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
948 return swash_fetch(PL_utf8_cntrl, p);
949}
950
951bool
952Perl_is_utf8_graph(pTHX_ U8 *p)
953{
386d01d6
GS
954 if (!is_utf8_char(p))
955 return FALSE;
b8c5462f
JH
956 if (!PL_utf8_graph)
957 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
958 return swash_fetch(PL_utf8_graph, p);
959}
960
961bool
864dbfa3 962Perl_is_utf8_print(pTHX_ U8 *p)
a0ed51b3 963{
386d01d6
GS
964 if (!is_utf8_char(p))
965 return FALSE;
a0ed51b3 966 if (!PL_utf8_print)
e24b16f9 967 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
a0ed51b3
LW
968 return swash_fetch(PL_utf8_print, p);
969}
970
971bool
b8c5462f
JH
972Perl_is_utf8_punct(pTHX_ U8 *p)
973{
386d01d6
GS
974 if (!is_utf8_char(p))
975 return FALSE;
b8c5462f
JH
976 if (!PL_utf8_punct)
977 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
978 return swash_fetch(PL_utf8_punct, p);
979}
980
981bool
982Perl_is_utf8_xdigit(pTHX_ U8 *p)
983{
386d01d6
GS
984 if (!is_utf8_char(p))
985 return FALSE;
b8c5462f
JH
986 if (!PL_utf8_xdigit)
987 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
988 return swash_fetch(PL_utf8_xdigit, p);
989}
990
991bool
864dbfa3 992Perl_is_utf8_mark(pTHX_ U8 *p)
a0ed51b3 993{
386d01d6
GS
994 if (!is_utf8_char(p))
995 return FALSE;
a0ed51b3 996 if (!PL_utf8_mark)
e24b16f9 997 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
a0ed51b3
LW
998 return swash_fetch(PL_utf8_mark, p);
999}
1000
2104c8d9 1001UV
864dbfa3 1002Perl_to_utf8_upper(pTHX_ U8 *p)
a0ed51b3
LW
1003{
1004 UV uv;
1005
1006 if (!PL_utf8_toupper)
e24b16f9 1007 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
a0ed51b3 1008 uv = swash_fetch(PL_utf8_toupper, p);
756820e3 1009 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
a0ed51b3
LW
1010}
1011
2104c8d9 1012UV
864dbfa3 1013Perl_to_utf8_title(pTHX_ U8 *p)
a0ed51b3
LW
1014{
1015 UV uv;
1016
1017 if (!PL_utf8_totitle)
e24b16f9 1018 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
a0ed51b3 1019 uv = swash_fetch(PL_utf8_totitle, p);
756820e3 1020 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
a0ed51b3
LW
1021}
1022
2104c8d9 1023UV
864dbfa3 1024Perl_to_utf8_lower(pTHX_ U8 *p)
a0ed51b3
LW
1025{
1026 UV uv;
1027
1028 if (!PL_utf8_tolower)
e24b16f9 1029 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
a0ed51b3 1030 uv = swash_fetch(PL_utf8_tolower, p);
756820e3 1031 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
a0ed51b3
LW
1032}
1033
1034/* a "swash" is a swatch hash */
1035
1036SV*
864dbfa3 1037Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
a0ed51b3
LW
1038{
1039 SV* retval;
1040 char tmpbuf[256];
8e84507e 1041 dSP;
ce3b816e
GS
1042
1043 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
1044 ENTER;
1045 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
1046 LEAVE;
1047 }
1048 SPAGAIN;
a0ed51b3
LW
1049 PUSHSTACKi(PERLSI_MAGIC);
1050 PUSHMARK(SP);
1051 EXTEND(SP,5);
1052 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
1053 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
1054 PUSHs(listsv);
1055 PUSHs(sv_2mortal(newSViv(minbits)));
1056 PUSHs(sv_2mortal(newSViv(none)));
1057 PUTBACK;
1058 ENTER;
1059 SAVEI32(PL_hints);
1060 PL_hints = 0;
1061 save_re_context();
e24b16f9 1062 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
a0ed51b3 1063 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
864dbfa3 1064 if (call_method("SWASHNEW", G_SCALAR))
8e84507e 1065 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 1066 else
e24b16f9 1067 retval = &PL_sv_undef;
a0ed51b3
LW
1068 LEAVE;
1069 POPSTACK;
e24b16f9 1070 if (PL_curcop == &PL_compiling) {
a0ed51b3 1071 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
e24b16f9 1072 PL_curcop->op_private = PL_hints;
a0ed51b3
LW
1073 }
1074 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
cea2e8a9 1075 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
a0ed51b3
LW
1076 return retval;
1077}
1078
1079UV
864dbfa3 1080Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
a0ed51b3
LW
1081{
1082 HV* hv = (HV*)SvRV(sv);
1083 U32 klen = UTF8SKIP(ptr) - 1;
1084 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
1085 STRLEN slen;
1086 STRLEN needents = (klen ? 64 : 128);
dfe13c55 1087 U8 *tmps;
a0ed51b3
LW
1088 U32 bit;
1089 SV *retval;
1090
1091 /*
1092 * This single-entry cache saves about 1/3 of the utf8 overhead in test
1093 * suite. (That is, only 7-8% overall over just a hash cache. Still,
1094 * it's nothing to sniff at.) Pity we usually come through at least
1095 * two function calls to get here...
1096 *
1097 * NB: this code assumes that swatches are never modified, once generated!
1098 */
1099
1100 if (hv == PL_last_swash_hv &&
1101 klen == PL_last_swash_klen &&
12ae5dfc 1102 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
a0ed51b3
LW
1103 {
1104 tmps = PL_last_swash_tmps;
1105 slen = PL_last_swash_slen;
1106 }
1107 else {
1108 /* Try our second-level swatch cache, kept in a hash. */
dfe13c55 1109 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
a0ed51b3
LW
1110
1111 /* If not cached, generate it via utf8::SWASHGET */
dfe13c55 1112 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
a0ed51b3
LW
1113 dSP;
1114 ENTER;
1115 SAVETMPS;
1116 save_re_context();
1117 PUSHSTACKi(PERLSI_MAGIC);
1118 PUSHMARK(SP);
1119 EXTEND(SP,3);
1120 PUSHs((SV*)sv);
756820e3 1121 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, UTF8_MAXLEN, 0, 0) & ~(needents - 1))));
a0ed51b3
LW
1122 PUSHs(sv_2mortal(newSViv(needents)));
1123 PUTBACK;
864dbfa3 1124 if (call_method("SWASHGET", G_SCALAR))
8e84507e 1125 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 1126 else
e24b16f9 1127 retval = &PL_sv_undef;
a0ed51b3
LW
1128 POPSTACK;
1129 FREETMPS;
1130 LEAVE;
e24b16f9
GS
1131 if (PL_curcop == &PL_compiling)
1132 PL_curcop->op_private = PL_hints;
a0ed51b3 1133
dfe13c55 1134 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
a0ed51b3 1135
dfe13c55 1136 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
cea2e8a9 1137 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
a0ed51b3
LW
1138 }
1139
1140 PL_last_swash_hv = hv;
1141 PL_last_swash_klen = klen;
1142 PL_last_swash_tmps = tmps;
1143 PL_last_swash_slen = slen;
1144 if (klen)
1145 Copy(ptr, PL_last_swash_key, klen, U8);
1146 }
1147
9faf8d75 1148 switch ((int)((slen << 3) / needents)) {
a0ed51b3
LW
1149 case 1:
1150 bit = 1 << (off & 7);
1151 off >>= 3;
1152 return (tmps[off] & bit) != 0;
1153 case 8:
1154 return tmps[off];
1155 case 16:
1156 off <<= 1;
1157 return (tmps[off] << 8) + tmps[off + 1] ;
1158 case 32:
1159 off <<= 2;
1160 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
1161 }
cea2e8a9 1162 Perl_croak(aTHX_ "panic: swash_fetch");
a0ed51b3
LW
1163 return 0;
1164}