Replace #7084 with
[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)
31 {
32     if (uv < 0x80) {
33         *d++ = uv;
34         return d;
35     }
36     if (uv < 0x800) {
37         *d++ = (( uv >>  6)         | 0xc0);
38         *d++ = (( uv        & 0x3f) | 0x80);
39         return d;
40     }
41     if (uv < 0x10000) {
42         *d++ = (( uv >> 12)         | 0xe0);
43         *d++ = (((uv >>  6) & 0x3f) | 0x80);
44         *d++ = (( uv        & 0x3f) | 0x80);
45         return d;
46     }
47     if (uv < 0x200000) {
48         *d++ = (( uv >> 18)         | 0xf0);
49         *d++ = (((uv >> 12) & 0x3f) | 0x80);
50         *d++ = (((uv >>  6) & 0x3f) | 0x80);
51         *d++ = (( uv        & 0x3f) | 0x80);
52         return d;
53     }
54     if (uv < 0x4000000) {
55         *d++ = (( uv >> 24)         | 0xf8);
56         *d++ = (((uv >> 18) & 0x3f) | 0x80);
57         *d++ = (((uv >> 12) & 0x3f) | 0x80);
58         *d++ = (((uv >>  6) & 0x3f) | 0x80);
59         *d++ = (( uv        & 0x3f) | 0x80);
60         return d;
61     }
62     if (uv < 0x80000000) {
63         *d++ = (( uv >> 30)         | 0xfc);
64         *d++ = (((uv >> 24) & 0x3f) | 0x80);
65         *d++ = (((uv >> 18) & 0x3f) | 0x80);
66         *d++ = (((uv >> 12) & 0x3f) | 0x80);
67         *d++ = (((uv >>  6) & 0x3f) | 0x80);
68         *d++ = (( uv        & 0x3f) | 0x80);
69         return d;
70     }
71 #ifdef HAS_QUAD
72     if (uv < 0x1000000000LL)
73 #endif
74     {
75         *d++ =                        0xfe;     /* Can't match U+FEFF! */
76         *d++ = (((uv >> 30) & 0x3f) | 0x80);
77         *d++ = (((uv >> 24) & 0x3f) | 0x80);
78         *d++ = (((uv >> 18) & 0x3f) | 0x80);
79         *d++ = (((uv >> 12) & 0x3f) | 0x80);
80         *d++ = (((uv >>  6) & 0x3f) | 0x80);
81         *d++ = (( uv        & 0x3f) | 0x80);
82         return d;
83     }
84 #ifdef HAS_QUAD
85     {
86         *d++ =                        0xff;     /* Can't match U+FFFE! */
87         *d++ =                        0x80;     /* 6 Reserved bits */
88         *d++ = (((uv >> 60) & 0x0f) | 0x80);    /* 2 Reserved bits */
89         *d++ = (((uv >> 54) & 0x3f) | 0x80);
90         *d++ = (((uv >> 48) & 0x3f) | 0x80);
91         *d++ = (((uv >> 42) & 0x3f) | 0x80);
92         *d++ = (((uv >> 36) & 0x3f) | 0x80);
93         *d++ = (((uv >> 30) & 0x3f) | 0x80);
94         *d++ = (((uv >> 24) & 0x3f) | 0x80);
95         *d++ = (((uv >> 18) & 0x3f) | 0x80);
96         *d++ = (((uv >> 12) & 0x3f) | 0x80);
97         *d++ = (((uv >>  6) & 0x3f) | 0x80);
98         *d++ = (( uv        & 0x3f) | 0x80);
99         return d;
100     }
101 #endif
102 }
103
104 /* Tests if some arbitrary number of bytes begins in a valid UTF-8 character.
105  * The actual number of bytes in the UTF-8 character will be returned if it
106  * is valid, otherwise 0. */
107 int
108 Perl_is_utf8_char(pTHX_ U8 *s)
109 {
110     U8 u = *s;
111     int slen, len;
112
113     if (!(u & 0x80))
114         return 1;
115
116     if (!(u & 0x40))
117         return 0;
118
119     if      (!(u & 0x20))       { len = 2; }
120     else if (!(u & 0x10))       { len = 3; }
121     else if (!(u & 0x08))       { len = 4; }
122     else if (!(u & 0x04))       { len = 5; }
123     else if (!(u & 0x02))       { len = 6; }
124     else if (!(u & 0x01))       { len = 7; }
125     else                        { len = 13; } /* whoa! */
126
127     slen = len - 1;
128     s++;
129     while (slen--) {
130         if ((*s & 0xc0) != 0x80)
131             return 0;
132         s++;
133     }
134     return len;
135 }
136
137 /*
138 =for apidoc Am|is_utf8_string|U8 *s|STRLEN len
139
140 Returns true if first C<len> bytes of the given string form valid a UTF8
141 string, false otherwise.
142
143 =cut
144 */
145
146 bool 
147 Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
148 {
149     U8* x=s;
150     U8* send=s+len;
151     int c;
152     while (x < send) {
153         c = is_utf8_char(x);
154         x += c;
155         if (!c || x > send)
156             return 0;
157     }
158     return 1;
159 }
160
161 /*
162 =for apidoc Am|utf8_to_uv|U8 *s|I32 *retlen|I32 checking
163
164 Returns the character value of the first character in the string C<s>
165 which is assumed to be in UTF8 encoding; C<retlen> will be set to the
166 length, in bytes, of that character, and the pointer C<s> will be
167 advanced to the end of the character.
168
169 If C<s> does not point to a well-formed UTF8 character, the behaviour
170 is dependent on the value of C<checking>: if this is true, it is
171 assumed that the caller will raise a warning, and this function will
172 set C<retlen> to C<-1> and return. If C<checking> is not true, an optional UTF8
173 warning is produced.
174
175 =cut
176 */
177
178 UV
179 Perl_utf8_to_uv(pTHX_ U8* s, I32* retlen, bool checking)
180 {
181     UV uv = *s;
182     int len;
183     if (!(uv & 0x80)) {
184         if (retlen)
185             *retlen = 1;
186         return *s;
187     }
188     if (!(uv & 0x40)) {
189         dTHR;
190         if (checking && retlen) {
191             *retlen = -1;
192             return 0;
193         }
194
195         if (ckWARN_d(WARN_UTF8))     
196             Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
197         if (retlen)
198             *retlen = 1;
199         return *s;
200     }
201
202     if      (!(uv & 0x20))      { len = 2; uv &= 0x1f; }
203     else if (!(uv & 0x10))      { len = 3; uv &= 0x0f; }
204     else if (!(uv & 0x08))      { len = 4; uv &= 0x07; }
205     else if (!(uv & 0x04))      { len = 5; uv &= 0x03; }
206     else if (!(uv & 0x02))      { len = 6; uv &= 0x01; }
207     else if (!(uv & 0x01))      { len = 7;  uv = 0; }
208     else                        { len = 13; uv = 0; } /* whoa! */
209
210     if (retlen)
211         *retlen = len;
212     --len;
213     s++;
214     while (len--) {
215         if ((*s & 0xc0) != 0x80) {
216             dTHR;
217             if (checking && retlen) {
218                 *retlen = -1;
219                 return 0;
220             }
221
222             if (ckWARN_d(WARN_UTF8))     
223                 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
224             if (retlen)
225                 *retlen -= len + 1;
226             return 0xfffd;
227         }
228         else
229             uv = (uv << 6) | (*s++ & 0x3f);
230     }
231     return uv;
232 }
233
234 /* utf8_distance(a,b) returns the number of UTF8 characters between
235    the pointers a and b                                                 */
236
237 I32
238 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
239 {
240     I32 off = 0;
241     if (a < b) {
242         while (a < b) {
243             a += UTF8SKIP(a);
244             off--;
245         }
246     }
247     else {
248         while (b < a) {
249             b += UTF8SKIP(b);
250             off++;
251         }
252     }
253     return off;
254 }
255
256 /* WARNING: do not use the following unless you *know* off is within bounds */
257
258 U8 *
259 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
260 {
261     if (off >= 0) {
262         while (off--)
263             s += UTF8SKIP(s);
264     }
265     else {
266         while (off++) {
267             s--;
268             if (*s & 0x80) {
269                 while ((*s & 0xc0) == 0x80)
270                     s--;
271             }
272         }
273     }
274     return s;
275 }
276
277 /*
278 =for apidoc Am|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
279
280 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
281 Unlike C<bytes_to_utf8>, this over-writes the original string, and
282 updates len to contain the new length.
283 Returns zero on failure, setting C<len> to -1.
284
285 =cut
286 */
287
288 U8 *
289 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
290 {
291     dTHR;
292     U8 *send;
293     U8 *d;
294     U8 *save;
295
296     send = s + *len;
297     d = save = s;
298
299     /* ensure valid UTF8 and chars < 256 before updating string */
300     while (s < send) {
301         U8 c = *s++;
302         if (c >= 0x80 &&
303             ( (s >= send) || ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
304             *len = -1;
305             return 0;    
306         }
307     }
308     s = save;
309     while (s < send) {
310         if (*s < 0x80)
311             *d++ = *s++;
312         else {
313             I32 ulen;
314             *d++ = (U8)utf8_to_uv(s, &ulen, 0);
315             s += ulen;
316         }
317     }
318     *d = '\0';
319     *len = d - save;
320     return save;
321 }
322
323 /*
324 =for apidoc Am|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
325
326 Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
327 Returns a pointer to the newly-created string, and sets C<len> to
328 reflect the new length.
329
330 =cut
331 */
332
333 U8*
334 Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
335 {
336     dTHR;
337     U8 *send;
338     U8 *d;
339     U8 *dst;
340     send = s + (*len);
341
342     Newz(801, d, (*len) * 2 + 1, U8);
343     dst = d;
344
345     while (s < send) {
346         if (*s < 0x80)
347             *d++ = *s++;
348         else {
349             UV uv = *s++;
350             *d++ = (( uv >>  6)         | 0xc0);
351             *d++ = (( uv        & 0x3f) | 0x80);
352         }
353     }
354     *d = '\0';
355     *len = d-dst;
356     return dst;
357 }
358
359 /*
360  * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
361  *
362  * Destination must be pre-extended to 3/2 source.  Do not use in-place.
363  * We optimize for native, for obvious reasons. */
364
365 U8*
366 Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
367 {
368     U8* pend;
369     U8* dstart = d;
370
371     if (bytelen & 1)
372         Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
373
374     pend = p + bytelen;
375
376     while (p < pend) {
377         UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
378         p += 2;
379         if (uv < 0x80) {
380             *d++ = uv;
381             continue;
382         }
383         if (uv < 0x800) {
384             *d++ = (( uv >>  6)         | 0xc0);
385             *d++ = (( uv        & 0x3f) | 0x80);
386             continue;
387         }
388         if (uv >= 0xd800 && uv < 0xdbff) {      /* surrogates */
389             dTHR;
390             UV low = *p++;
391             if (low < 0xdc00 || low >= 0xdfff)
392                 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
393             uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
394         }
395         if (uv < 0x10000) {
396             *d++ = (( uv >> 12)         | 0xe0);
397             *d++ = (((uv >>  6) & 0x3f) | 0x80);
398             *d++ = (( uv        & 0x3f) | 0x80);
399             continue;
400         }
401         else {
402             *d++ = (( uv >> 18)         | 0xf0);
403             *d++ = (((uv >> 12) & 0x3f) | 0x80);
404             *d++ = (((uv >>  6) & 0x3f) | 0x80);
405             *d++ = (( uv        & 0x3f) | 0x80);
406             continue;
407         }
408     }
409     *newlen = d - dstart;
410     return d;
411 }
412
413 /* Note: this one is slightly destructive of the source. */
414
415 U8*
416 Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
417 {
418     U8* s = (U8*)p;
419     U8* send = s + bytelen;
420     while (s < send) {
421         U8 tmp = s[0];
422         s[0] = s[1];
423         s[1] = tmp;
424         s += 2;
425     }
426     return utf16_to_utf8(p, d, bytelen, newlen);
427 }
428
429 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
430
431 bool
432 Perl_is_uni_alnum(pTHX_ U32 c)
433 {
434     U8 tmpbuf[UTF8_MAXLEN];
435     uv_to_utf8(tmpbuf, (UV)c);
436     return is_utf8_alnum(tmpbuf);
437 }
438
439 bool
440 Perl_is_uni_alnumc(pTHX_ U32 c)
441 {
442     U8 tmpbuf[UTF8_MAXLEN];
443     uv_to_utf8(tmpbuf, (UV)c);
444     return is_utf8_alnumc(tmpbuf);
445 }
446
447 bool
448 Perl_is_uni_idfirst(pTHX_ U32 c)
449 {
450     U8 tmpbuf[UTF8_MAXLEN];
451     uv_to_utf8(tmpbuf, (UV)c);
452     return is_utf8_idfirst(tmpbuf);
453 }
454
455 bool
456 Perl_is_uni_alpha(pTHX_ U32 c)
457 {
458     U8 tmpbuf[UTF8_MAXLEN];
459     uv_to_utf8(tmpbuf, (UV)c);
460     return is_utf8_alpha(tmpbuf);
461 }
462
463 bool
464 Perl_is_uni_ascii(pTHX_ U32 c)
465 {
466     U8 tmpbuf[UTF8_MAXLEN];
467     uv_to_utf8(tmpbuf, (UV)c);
468     return is_utf8_ascii(tmpbuf);
469 }
470
471 bool
472 Perl_is_uni_space(pTHX_ U32 c)
473 {
474     U8 tmpbuf[UTF8_MAXLEN];
475     uv_to_utf8(tmpbuf, (UV)c);
476     return is_utf8_space(tmpbuf);
477 }
478
479 bool
480 Perl_is_uni_digit(pTHX_ U32 c)
481 {
482     U8 tmpbuf[UTF8_MAXLEN];
483     uv_to_utf8(tmpbuf, (UV)c);
484     return is_utf8_digit(tmpbuf);
485 }
486
487 bool
488 Perl_is_uni_upper(pTHX_ U32 c)
489 {
490     U8 tmpbuf[UTF8_MAXLEN];
491     uv_to_utf8(tmpbuf, (UV)c);
492     return is_utf8_upper(tmpbuf);
493 }
494
495 bool
496 Perl_is_uni_lower(pTHX_ U32 c)
497 {
498     U8 tmpbuf[UTF8_MAXLEN];
499     uv_to_utf8(tmpbuf, (UV)c);
500     return is_utf8_lower(tmpbuf);
501 }
502
503 bool
504 Perl_is_uni_cntrl(pTHX_ U32 c)
505 {
506     U8 tmpbuf[UTF8_MAXLEN];
507     uv_to_utf8(tmpbuf, (UV)c);
508     return is_utf8_cntrl(tmpbuf);
509 }
510
511 bool
512 Perl_is_uni_graph(pTHX_ U32 c)
513 {
514     U8 tmpbuf[UTF8_MAXLEN];
515     uv_to_utf8(tmpbuf, (UV)c);
516     return is_utf8_graph(tmpbuf);
517 }
518
519 bool
520 Perl_is_uni_print(pTHX_ U32 c)
521 {
522     U8 tmpbuf[UTF8_MAXLEN];
523     uv_to_utf8(tmpbuf, (UV)c);
524     return is_utf8_print(tmpbuf);
525 }
526
527 bool
528 Perl_is_uni_punct(pTHX_ U32 c)
529 {
530     U8 tmpbuf[UTF8_MAXLEN];
531     uv_to_utf8(tmpbuf, (UV)c);
532     return is_utf8_punct(tmpbuf);
533 }
534
535 bool
536 Perl_is_uni_xdigit(pTHX_ U32 c)
537 {
538     U8 tmpbuf[UTF8_MAXLEN];
539     uv_to_utf8(tmpbuf, (UV)c);
540     return is_utf8_xdigit(tmpbuf);
541 }
542
543 U32
544 Perl_to_uni_upper(pTHX_ U32 c)
545 {
546     U8 tmpbuf[UTF8_MAXLEN];
547     uv_to_utf8(tmpbuf, (UV)c);
548     return to_utf8_upper(tmpbuf);
549 }
550
551 U32
552 Perl_to_uni_title(pTHX_ U32 c)
553 {
554     U8 tmpbuf[UTF8_MAXLEN];
555     uv_to_utf8(tmpbuf, (UV)c);
556     return to_utf8_title(tmpbuf);
557 }
558
559 U32
560 Perl_to_uni_lower(pTHX_ U32 c)
561 {
562     U8 tmpbuf[UTF8_MAXLEN];
563     uv_to_utf8(tmpbuf, (UV)c);
564     return to_utf8_lower(tmpbuf);
565 }
566
567 /* for now these all assume no locale info available for Unicode > 255 */
568
569 bool
570 Perl_is_uni_alnum_lc(pTHX_ U32 c)
571 {
572     return is_uni_alnum(c);     /* XXX no locale support yet */
573 }
574
575 bool
576 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
577 {
578     return is_uni_alnumc(c);    /* XXX no locale support yet */
579 }
580
581 bool
582 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
583 {
584     return is_uni_idfirst(c);   /* XXX no locale support yet */
585 }
586
587 bool
588 Perl_is_uni_alpha_lc(pTHX_ U32 c)
589 {
590     return is_uni_alpha(c);     /* XXX no locale support yet */
591 }
592
593 bool
594 Perl_is_uni_ascii_lc(pTHX_ U32 c)
595 {
596     return is_uni_ascii(c);     /* XXX no locale support yet */
597 }
598
599 bool
600 Perl_is_uni_space_lc(pTHX_ U32 c)
601 {
602     return is_uni_space(c);     /* XXX no locale support yet */
603 }
604
605 bool
606 Perl_is_uni_digit_lc(pTHX_ U32 c)
607 {
608     return is_uni_digit(c);     /* XXX no locale support yet */
609 }
610
611 bool
612 Perl_is_uni_upper_lc(pTHX_ U32 c)
613 {
614     return is_uni_upper(c);     /* XXX no locale support yet */
615 }
616
617 bool
618 Perl_is_uni_lower_lc(pTHX_ U32 c)
619 {
620     return is_uni_lower(c);     /* XXX no locale support yet */
621 }
622
623 bool
624 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
625 {
626     return is_uni_cntrl(c);     /* XXX no locale support yet */
627 }
628
629 bool
630 Perl_is_uni_graph_lc(pTHX_ U32 c)
631 {
632     return is_uni_graph(c);     /* XXX no locale support yet */
633 }
634
635 bool
636 Perl_is_uni_print_lc(pTHX_ U32 c)
637 {
638     return is_uni_print(c);     /* XXX no locale support yet */
639 }
640
641 bool
642 Perl_is_uni_punct_lc(pTHX_ U32 c)
643 {
644     return is_uni_punct(c);     /* XXX no locale support yet */
645 }
646
647 bool
648 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
649 {
650     return is_uni_xdigit(c);    /* XXX no locale support yet */
651 }
652
653 U32
654 Perl_to_uni_upper_lc(pTHX_ U32 c)
655 {
656     return to_uni_upper(c);     /* XXX no locale support yet */
657 }
658
659 U32
660 Perl_to_uni_title_lc(pTHX_ U32 c)
661 {
662     return to_uni_title(c);     /* XXX no locale support yet */
663 }
664
665 U32
666 Perl_to_uni_lower_lc(pTHX_ U32 c)
667 {
668     return to_uni_lower(c);     /* XXX no locale support yet */
669 }
670
671 bool
672 Perl_is_utf8_alnum(pTHX_ U8 *p)
673 {
674     if (!is_utf8_char(p))
675         return FALSE;
676     if (!PL_utf8_alnum)
677         /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
678          * descendant of isalnum(3), in other words, it doesn't
679          * contain the '_'. --jhi */
680         PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
681     return swash_fetch(PL_utf8_alnum, p);
682 /*    return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
683 #ifdef SURPRISINGLY_SLOWER  /* probably because alpha is usually true */
684     if (!PL_utf8_alnum)
685         PL_utf8_alnum = swash_init("utf8", "",
686             sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
687     return swash_fetch(PL_utf8_alnum, p);
688 #endif
689 }
690
691 bool
692 Perl_is_utf8_alnumc(pTHX_ U8 *p)
693 {
694     if (!is_utf8_char(p))
695         return FALSE;
696     if (!PL_utf8_alnum)
697         PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
698     return swash_fetch(PL_utf8_alnum, p);
699 /*    return is_utf8_alpha(p) || is_utf8_digit(p); */
700 #ifdef SURPRISINGLY_SLOWER  /* probably because alpha is usually true */
701     if (!PL_utf8_alnum)
702         PL_utf8_alnum = swash_init("utf8", "",
703             sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
704     return swash_fetch(PL_utf8_alnum, p);
705 #endif
706 }
707
708 bool
709 Perl_is_utf8_idfirst(pTHX_ U8 *p)
710 {
711     return *p == '_' || is_utf8_alpha(p);
712 }
713
714 bool
715 Perl_is_utf8_alpha(pTHX_ U8 *p)
716 {
717     if (!is_utf8_char(p))
718         return FALSE;
719     if (!PL_utf8_alpha)
720         PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
721     return swash_fetch(PL_utf8_alpha, p);
722 }
723
724 bool
725 Perl_is_utf8_ascii(pTHX_ U8 *p)
726 {
727     if (!is_utf8_char(p))
728         return FALSE;
729     if (!PL_utf8_ascii)
730         PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
731     return swash_fetch(PL_utf8_ascii, p);
732 }
733
734 bool
735 Perl_is_utf8_space(pTHX_ U8 *p)
736 {
737     if (!is_utf8_char(p))
738         return FALSE;
739     if (!PL_utf8_space)
740         PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
741     return swash_fetch(PL_utf8_space, p);
742 }
743
744 bool
745 Perl_is_utf8_digit(pTHX_ U8 *p)
746 {
747     if (!is_utf8_char(p))
748         return FALSE;
749     if (!PL_utf8_digit)
750         PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
751     return swash_fetch(PL_utf8_digit, p);
752 }
753
754 bool
755 Perl_is_utf8_upper(pTHX_ U8 *p)
756 {
757     if (!is_utf8_char(p))
758         return FALSE;
759     if (!PL_utf8_upper)
760         PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
761     return swash_fetch(PL_utf8_upper, p);
762 }
763
764 bool
765 Perl_is_utf8_lower(pTHX_ U8 *p)
766 {
767     if (!is_utf8_char(p))
768         return FALSE;
769     if (!PL_utf8_lower)
770         PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
771     return swash_fetch(PL_utf8_lower, p);
772 }
773
774 bool
775 Perl_is_utf8_cntrl(pTHX_ U8 *p)
776 {
777     if (!is_utf8_char(p))
778         return FALSE;
779     if (!PL_utf8_cntrl)
780         PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
781     return swash_fetch(PL_utf8_cntrl, p);
782 }
783
784 bool
785 Perl_is_utf8_graph(pTHX_ U8 *p)
786 {
787     if (!is_utf8_char(p))
788         return FALSE;
789     if (!PL_utf8_graph)
790         PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
791     return swash_fetch(PL_utf8_graph, p);
792 }
793
794 bool
795 Perl_is_utf8_print(pTHX_ U8 *p)
796 {
797     if (!is_utf8_char(p))
798         return FALSE;
799     if (!PL_utf8_print)
800         PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
801     return swash_fetch(PL_utf8_print, p);
802 }
803
804 bool
805 Perl_is_utf8_punct(pTHX_ U8 *p)
806 {
807     if (!is_utf8_char(p))
808         return FALSE;
809     if (!PL_utf8_punct)
810         PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
811     return swash_fetch(PL_utf8_punct, p);
812 }
813
814 bool
815 Perl_is_utf8_xdigit(pTHX_ U8 *p)
816 {
817     if (!is_utf8_char(p))
818         return FALSE;
819     if (!PL_utf8_xdigit)
820         PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
821     return swash_fetch(PL_utf8_xdigit, p);
822 }
823
824 bool
825 Perl_is_utf8_mark(pTHX_ U8 *p)
826 {
827     if (!is_utf8_char(p))
828         return FALSE;
829     if (!PL_utf8_mark)
830         PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
831     return swash_fetch(PL_utf8_mark, p);
832 }
833
834 UV
835 Perl_to_utf8_upper(pTHX_ U8 *p)
836 {
837     UV uv;
838
839     if (!PL_utf8_toupper)
840         PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
841     uv = swash_fetch(PL_utf8_toupper, p);
842     return uv ? uv : utf8_to_uv(p,0,0);
843 }
844
845 UV
846 Perl_to_utf8_title(pTHX_ U8 *p)
847 {
848     UV uv;
849
850     if (!PL_utf8_totitle)
851         PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
852     uv = swash_fetch(PL_utf8_totitle, p);
853     return uv ? uv : utf8_to_uv(p,0,0);
854 }
855
856 UV
857 Perl_to_utf8_lower(pTHX_ U8 *p)
858 {
859     UV uv;
860
861     if (!PL_utf8_tolower)
862         PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
863     uv = swash_fetch(PL_utf8_tolower, p);
864     return uv ? uv : utf8_to_uv(p,0,0);
865 }
866
867 /* a "swash" is a swatch hash */
868
869 SV*
870 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
871 {
872     SV* retval;
873     char tmpbuf[256];
874     dSP;    
875
876     if (!gv_stashpv(pkg, 0)) {  /* demand load utf8 */
877         ENTER;
878         Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
879         LEAVE;
880     }
881     SPAGAIN;
882     PUSHSTACKi(PERLSI_MAGIC);
883     PUSHMARK(SP);
884     EXTEND(SP,5);
885     PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
886     PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
887     PUSHs(listsv);
888     PUSHs(sv_2mortal(newSViv(minbits)));
889     PUSHs(sv_2mortal(newSViv(none)));
890     PUTBACK;
891     ENTER;
892     SAVEI32(PL_hints);
893     PL_hints = 0;
894     save_re_context();
895     if (PL_curcop == &PL_compiling)     /* XXX ought to be handled by lex_start */
896         strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
897     if (call_method("SWASHNEW", G_SCALAR))
898         retval = newSVsv(*PL_stack_sp--);    
899     else
900         retval = &PL_sv_undef;
901     LEAVE;
902     POPSTACK;
903     if (PL_curcop == &PL_compiling) {
904         strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
905         PL_curcop->op_private = PL_hints;
906     }
907     if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
908         Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
909     return retval;
910 }
911
912 UV
913 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
914 {
915     HV* hv = (HV*)SvRV(sv);
916     U32 klen = UTF8SKIP(ptr) - 1;
917     U32 off = ptr[klen] & 127;  /* NB: 64 bit always 0 when len > 1 */
918     STRLEN slen;
919     STRLEN needents = (klen ? 64 : 128);
920     U8 *tmps;
921     U32 bit;
922     SV *retval;
923
924     /*
925      * This single-entry cache saves about 1/3 of the utf8 overhead in test
926      * suite.  (That is, only 7-8% overall over just a hash cache.  Still,
927      * it's nothing to sniff at.)  Pity we usually come through at least
928      * two function calls to get here...
929      *
930      * NB: this code assumes that swatches are never modified, once generated!
931      */
932
933     if (hv == PL_last_swash_hv &&
934         klen == PL_last_swash_klen &&
935         (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
936     {
937         tmps = PL_last_swash_tmps;
938         slen = PL_last_swash_slen;
939     }
940     else {
941         /* Try our second-level swatch cache, kept in a hash. */
942         SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
943
944         /* If not cached, generate it via utf8::SWASHGET */
945         if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
946             dSP;
947             ENTER;
948             SAVETMPS;
949             save_re_context();
950             PUSHSTACKi(PERLSI_MAGIC);
951             PUSHMARK(SP);
952             EXTEND(SP,3);
953             PUSHs((SV*)sv);
954             PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, 0, 0) & ~(needents - 1))));
955             PUSHs(sv_2mortal(newSViv(needents)));
956             PUTBACK;
957             if (call_method("SWASHGET", G_SCALAR))
958                 retval = newSVsv(*PL_stack_sp--);    
959             else
960                 retval = &PL_sv_undef;
961             POPSTACK;
962             FREETMPS;
963             LEAVE;
964             if (PL_curcop == &PL_compiling)
965                 PL_curcop->op_private = PL_hints;
966
967             svp = hv_store(hv, (char*)ptr, klen, retval, 0);
968
969             if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
970                 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
971         }
972
973         PL_last_swash_hv = hv;
974         PL_last_swash_klen = klen;
975         PL_last_swash_tmps = tmps;
976         PL_last_swash_slen = slen;
977         if (klen)
978             Copy(ptr, PL_last_swash_key, klen, U8);
979     }
980
981     switch ((slen << 3) / needents) {
982     case 1:
983         bit = 1 << (off & 7);
984         off >>= 3;
985         return (tmps[off] & bit) != 0;
986     case 8:
987         return tmps[off];
988     case 16:
989         off <<= 1;
990         return (tmps[off] << 8) + tmps[off + 1] ;
991     case 32:
992         off <<= 2;
993         return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
994     }
995     Perl_croak(aTHX_ "panic: swash_fetch");
996     return 0;
997 }