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