3 * Copyright (c) 1998-2000, Larry Wall
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.
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.'
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?'
19 * ...the travellers perceived that the floor was paved with stones of many
20 * hues; branching runes and strange devices intertwined beneath their feet.
24 #define PERL_IN_UTF8_C
30 Perl_uv_to_utf8(pTHX_ U8 *d, UV uv)
37 *d++ = (( uv >> 6) | 0xc0);
38 *d++ = (( uv & 0x3f) | 0x80);
42 *d++ = (( uv >> 12) | 0xe0);
43 *d++ = (((uv >> 6) & 0x3f) | 0x80);
44 *d++ = (( uv & 0x3f) | 0x80);
48 *d++ = (( uv >> 18) | 0xf0);
49 *d++ = (((uv >> 12) & 0x3f) | 0x80);
50 *d++ = (((uv >> 6) & 0x3f) | 0x80);
51 *d++ = (( uv & 0x3f) | 0x80);
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);
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);
72 if (uv < UTF8_QUAD_MAX)
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);
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);
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. */
108 Perl_is_utf8_char(pTHX_ U8 *s)
117 if (u >= 0x80 && u <= 0xbf)
122 if (len < 2 || (u >= 0xc0 && u <= 0xfd && s[1] < 0x80))
130 if ((*s & 0xc0) != 0x80)
132 uv = (uv << 6) | (*s & 0x3f);
139 if (UNISKIP(uv) < len)
146 =for apidoc Am|is_utf8_string|U8 *s|STRLEN len
148 Returns true if first C<len> bytes of the given string form valid a UTF8
149 string, false otherwise.
155 Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
174 =for apidoc Am|U8* s|utf8_to_uv|STRLEN curlen|I32 *retlen|U32 flags
176 Returns the character value of the first character in the string C<s>
177 which is assumed to be in UTF8 encoding and no longer than C<curlen>;
178 C<retlen> will be set to the length, in bytes, of that character,
179 and the pointer C<s> will be advanced to the end of the character.
181 If C<s> does not point to a well-formed UTF8 character, the behaviour
182 is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
183 it is assumed that the caller will raise a warning, and this function
184 will set C<retlen> to C<-1> and return. The C<flags> can also contain
185 various flags to allow deviations from the strict UTF-8 encoding.
190 Perl_utf8_to_uv(pTHX_ U8* s, STRLEN curlen, STRLEN* retlen, U32 flags)
195 bool dowarn = ckWARN_d(WARN_UTF8);
196 STRLEN expectlen = 0;
200 Perl_warner(aTHX_ WARN_UTF8,
201 "Malformed UTF-8 character (an empty string)");
205 if (uv <= 0x7f) { /* Pure ASCII. */
211 if ((uv >= 0x80 && uv <= 0xbf) &&
212 !(flags & UTF8_ALLOW_CONTINUATION)) {
214 Perl_warner(aTHX_ WARN_UTF8,
215 "Malformed UTF-8 character (unexpected continuation byte 0x%02"UVxf")",
220 if ((uv >= 0xc0 && uv <= 0xfd && curlen > 1 && s[1] < 0x80) &&
221 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
223 Perl_warner(aTHX_ WARN_UTF8,
224 "Malformed UTF-8 character (unexpected non-continuation byte 0x%02"UVxf" after byte 0x%02"UVxf")",
229 if ((uv == 0xfe || uv == 0xff) &&
230 !(flags & UTF8_ALLOW_FE_FF)) {
232 Perl_warner(aTHX_ WARN_UTF8,
233 "Malformed UTF-8 character (byte 0x%02"UVxf")",
238 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
239 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
240 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
241 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
242 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
243 else if (!(uv & 0x01)) { len = 7; uv = 0; }
244 else { len = 13; uv = 0; } /* whoa! */
251 if ((curlen < expectlen) &&
252 !(flags & UTF8_ALLOW_SHORT)) {
254 Perl_warner(aTHX_ WARN_UTF8,
255 "Malformed UTF-8 character (%d byte%s, need %d)",
256 curlen, curlen == 1 ? "" : "s", expectlen);
265 if ((*s & 0xc0) != 0x80) {
267 Perl_warner(aTHX_ WARN_UTF8,
268 "Malformed UTF-8 character (unexpected continuation byte 0x%02x)",
273 uv = (uv << 6) | (*s & 0x3f);
275 /* This cannot be allowed. */
277 Perl_warner(aTHX_ WARN_UTF8,
278 "Malformed UTF-8 character (overflow at 0x%"UVxf", byte 0x%02x)",
286 if ((uv >= 0xd800 && uv <= 0xdfff) &&
287 !(flags & UTF8_ALLOW_SURROGATE)) {
289 Perl_warner(aTHX_ WARN_UTF8,
290 "Malformed UTF-8 character (UTF-16 surrogate 0x%04"UVxf")",
293 } else if ((uv == 0xfffe) &&
294 !(flags & UTF8_ALLOW_BOM)) {
296 Perl_warner(aTHX_ WARN_UTF8,
297 "Malformed UTF-8 character (byte order mark 0x%04"UVxf")",
300 } else if ((uv == 0xffff) &&
301 !(flags & UTF8_ALLOW_FFFF)) {
303 Perl_warner(aTHX_ WARN_UTF8,
304 "Malformed UTF-8 character (character 0x%04"UVxf")",
307 } else if ((expectlen > UNISKIP(uv)) &&
308 !(flags & UTF8_ALLOW_LONG)) {
310 Perl_warner(aTHX_ WARN_UTF8,
311 "Malformed UTF-8 character (%d byte%s, need %d)",
312 expectlen, expectlen == 1 ? "": "s", UNISKIP(uv));
320 if (flags & UTF8_CHECK_ONLY) {
327 *retlen = expectlen ? expectlen : len;
329 return UNICODE_REPLACEMENT_CHARACTER;
333 =for apidoc Am|U8* s|utf8_to_uv_simple|STRLEN *retlen
335 Returns the character value of the first character in the string C<s>
336 which is assumed to be in UTF8 encoding; C<retlen> will be set to the
337 length, in bytes, of that character, and the pointer C<s> will be
338 advanced to the end of the character.
340 If C<s> does not point to a well-formed UTF8 character, zero is
341 returned and retlen is set, if possible, to -1.
347 Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
349 return Perl_utf8_to_uv(aTHX_ s, (STRLEN)-1, retlen, 0);
352 /* utf8_distance(a,b) returns the number of UTF8 characters between
353 the pointers a and b */
356 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
374 /* WARNING: do not use the following unless you *know* off is within bounds */
377 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
387 while ((*s & 0xc0) == 0x80)
396 =for apidoc Am|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
398 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
399 Unlike C<bytes_to_utf8>, this over-writes the original string, and
400 updates len to contain the new length.
401 Returns zero on failure, setting C<len> to -1.
407 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
413 /* ensure valid UTF8 and chars < 256 before updating string */
414 for (send = s + *len; s < send; ) {
419 ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
432 *d++ = (U8)utf8_to_uv_simple(s, &ulen);
442 =for apidoc Am|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
444 Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
445 Returns a pointer to the newly-created string, and sets C<len> to
446 reflect the new length.
452 Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
460 Newz(801, d, (*len) * 2 + 1, U8);
468 *d++ = (( uv >> 6) | 0xc0);
469 *d++ = (( uv & 0x3f) | 0x80);
478 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
480 * Destination must be pre-extended to 3/2 source. Do not use in-place.
481 * We optimize for native, for obvious reasons. */
484 Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
490 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
495 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
502 *d++ = (( uv >> 6) | 0xc0);
503 *d++ = (( uv & 0x3f) | 0x80);
506 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
509 if (low < 0xdc00 || low >= 0xdfff)
510 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
511 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
514 *d++ = (( uv >> 12) | 0xe0);
515 *d++ = (((uv >> 6) & 0x3f) | 0x80);
516 *d++ = (( uv & 0x3f) | 0x80);
520 *d++ = (( uv >> 18) | 0xf0);
521 *d++ = (((uv >> 12) & 0x3f) | 0x80);
522 *d++ = (((uv >> 6) & 0x3f) | 0x80);
523 *d++ = (( uv & 0x3f) | 0x80);
527 *newlen = d - dstart;
531 /* Note: this one is slightly destructive of the source. */
534 Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
537 U8* send = s + bytelen;
544 return utf16_to_utf8(p, d, bytelen, newlen);
547 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
550 Perl_is_uni_alnum(pTHX_ U32 c)
552 U8 tmpbuf[UTF8_MAXLEN];
553 uv_to_utf8(tmpbuf, (UV)c);
554 return is_utf8_alnum(tmpbuf);
558 Perl_is_uni_alnumc(pTHX_ U32 c)
560 U8 tmpbuf[UTF8_MAXLEN];
561 uv_to_utf8(tmpbuf, (UV)c);
562 return is_utf8_alnumc(tmpbuf);
566 Perl_is_uni_idfirst(pTHX_ U32 c)
568 U8 tmpbuf[UTF8_MAXLEN];
569 uv_to_utf8(tmpbuf, (UV)c);
570 return is_utf8_idfirst(tmpbuf);
574 Perl_is_uni_alpha(pTHX_ U32 c)
576 U8 tmpbuf[UTF8_MAXLEN];
577 uv_to_utf8(tmpbuf, (UV)c);
578 return is_utf8_alpha(tmpbuf);
582 Perl_is_uni_ascii(pTHX_ U32 c)
584 U8 tmpbuf[UTF8_MAXLEN];
585 uv_to_utf8(tmpbuf, (UV)c);
586 return is_utf8_ascii(tmpbuf);
590 Perl_is_uni_space(pTHX_ U32 c)
592 U8 tmpbuf[UTF8_MAXLEN];
593 uv_to_utf8(tmpbuf, (UV)c);
594 return is_utf8_space(tmpbuf);
598 Perl_is_uni_digit(pTHX_ U32 c)
600 U8 tmpbuf[UTF8_MAXLEN];
601 uv_to_utf8(tmpbuf, (UV)c);
602 return is_utf8_digit(tmpbuf);
606 Perl_is_uni_upper(pTHX_ U32 c)
608 U8 tmpbuf[UTF8_MAXLEN];
609 uv_to_utf8(tmpbuf, (UV)c);
610 return is_utf8_upper(tmpbuf);
614 Perl_is_uni_lower(pTHX_ U32 c)
616 U8 tmpbuf[UTF8_MAXLEN];
617 uv_to_utf8(tmpbuf, (UV)c);
618 return is_utf8_lower(tmpbuf);
622 Perl_is_uni_cntrl(pTHX_ U32 c)
624 U8 tmpbuf[UTF8_MAXLEN];
625 uv_to_utf8(tmpbuf, (UV)c);
626 return is_utf8_cntrl(tmpbuf);
630 Perl_is_uni_graph(pTHX_ U32 c)
632 U8 tmpbuf[UTF8_MAXLEN];
633 uv_to_utf8(tmpbuf, (UV)c);
634 return is_utf8_graph(tmpbuf);
638 Perl_is_uni_print(pTHX_ U32 c)
640 U8 tmpbuf[UTF8_MAXLEN];
641 uv_to_utf8(tmpbuf, (UV)c);
642 return is_utf8_print(tmpbuf);
646 Perl_is_uni_punct(pTHX_ U32 c)
648 U8 tmpbuf[UTF8_MAXLEN];
649 uv_to_utf8(tmpbuf, (UV)c);
650 return is_utf8_punct(tmpbuf);
654 Perl_is_uni_xdigit(pTHX_ U32 c)
656 U8 tmpbuf[UTF8_MAXLEN];
657 uv_to_utf8(tmpbuf, (UV)c);
658 return is_utf8_xdigit(tmpbuf);
662 Perl_to_uni_upper(pTHX_ U32 c)
664 U8 tmpbuf[UTF8_MAXLEN];
665 uv_to_utf8(tmpbuf, (UV)c);
666 return to_utf8_upper(tmpbuf);
670 Perl_to_uni_title(pTHX_ U32 c)
672 U8 tmpbuf[UTF8_MAXLEN];
673 uv_to_utf8(tmpbuf, (UV)c);
674 return to_utf8_title(tmpbuf);
678 Perl_to_uni_lower(pTHX_ U32 c)
680 U8 tmpbuf[UTF8_MAXLEN];
681 uv_to_utf8(tmpbuf, (UV)c);
682 return to_utf8_lower(tmpbuf);
685 /* for now these all assume no locale info available for Unicode > 255 */
688 Perl_is_uni_alnum_lc(pTHX_ U32 c)
690 return is_uni_alnum(c); /* XXX no locale support yet */
694 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
696 return is_uni_alnumc(c); /* XXX no locale support yet */
700 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
702 return is_uni_idfirst(c); /* XXX no locale support yet */
706 Perl_is_uni_alpha_lc(pTHX_ U32 c)
708 return is_uni_alpha(c); /* XXX no locale support yet */
712 Perl_is_uni_ascii_lc(pTHX_ U32 c)
714 return is_uni_ascii(c); /* XXX no locale support yet */
718 Perl_is_uni_space_lc(pTHX_ U32 c)
720 return is_uni_space(c); /* XXX no locale support yet */
724 Perl_is_uni_digit_lc(pTHX_ U32 c)
726 return is_uni_digit(c); /* XXX no locale support yet */
730 Perl_is_uni_upper_lc(pTHX_ U32 c)
732 return is_uni_upper(c); /* XXX no locale support yet */
736 Perl_is_uni_lower_lc(pTHX_ U32 c)
738 return is_uni_lower(c); /* XXX no locale support yet */
742 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
744 return is_uni_cntrl(c); /* XXX no locale support yet */
748 Perl_is_uni_graph_lc(pTHX_ U32 c)
750 return is_uni_graph(c); /* XXX no locale support yet */
754 Perl_is_uni_print_lc(pTHX_ U32 c)
756 return is_uni_print(c); /* XXX no locale support yet */
760 Perl_is_uni_punct_lc(pTHX_ U32 c)
762 return is_uni_punct(c); /* XXX no locale support yet */
766 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
768 return is_uni_xdigit(c); /* XXX no locale support yet */
772 Perl_to_uni_upper_lc(pTHX_ U32 c)
774 return to_uni_upper(c); /* XXX no locale support yet */
778 Perl_to_uni_title_lc(pTHX_ U32 c)
780 return to_uni_title(c); /* XXX no locale support yet */
784 Perl_to_uni_lower_lc(pTHX_ U32 c)
786 return to_uni_lower(c); /* XXX no locale support yet */
790 Perl_is_utf8_alnum(pTHX_ U8 *p)
792 if (!is_utf8_char(p))
795 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
796 * descendant of isalnum(3), in other words, it doesn't
797 * contain the '_'. --jhi */
798 PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
799 return swash_fetch(PL_utf8_alnum, p);
800 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
801 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
803 PL_utf8_alnum = swash_init("utf8", "",
804 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
805 return swash_fetch(PL_utf8_alnum, p);
810 Perl_is_utf8_alnumc(pTHX_ U8 *p)
812 if (!is_utf8_char(p))
815 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
816 return swash_fetch(PL_utf8_alnum, p);
817 /* return is_utf8_alpha(p) || is_utf8_digit(p); */
818 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
820 PL_utf8_alnum = swash_init("utf8", "",
821 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
822 return swash_fetch(PL_utf8_alnum, p);
827 Perl_is_utf8_idfirst(pTHX_ U8 *p)
829 return *p == '_' || is_utf8_alpha(p);
833 Perl_is_utf8_alpha(pTHX_ U8 *p)
835 if (!is_utf8_char(p))
838 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
839 return swash_fetch(PL_utf8_alpha, p);
843 Perl_is_utf8_ascii(pTHX_ U8 *p)
845 if (!is_utf8_char(p))
848 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
849 return swash_fetch(PL_utf8_ascii, p);
853 Perl_is_utf8_space(pTHX_ U8 *p)
855 if (!is_utf8_char(p))
858 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
859 return swash_fetch(PL_utf8_space, p);
863 Perl_is_utf8_digit(pTHX_ U8 *p)
865 if (!is_utf8_char(p))
868 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
869 return swash_fetch(PL_utf8_digit, p);
873 Perl_is_utf8_upper(pTHX_ U8 *p)
875 if (!is_utf8_char(p))
878 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
879 return swash_fetch(PL_utf8_upper, p);
883 Perl_is_utf8_lower(pTHX_ U8 *p)
885 if (!is_utf8_char(p))
888 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
889 return swash_fetch(PL_utf8_lower, p);
893 Perl_is_utf8_cntrl(pTHX_ U8 *p)
895 if (!is_utf8_char(p))
898 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
899 return swash_fetch(PL_utf8_cntrl, p);
903 Perl_is_utf8_graph(pTHX_ U8 *p)
905 if (!is_utf8_char(p))
908 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
909 return swash_fetch(PL_utf8_graph, p);
913 Perl_is_utf8_print(pTHX_ U8 *p)
915 if (!is_utf8_char(p))
918 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
919 return swash_fetch(PL_utf8_print, p);
923 Perl_is_utf8_punct(pTHX_ U8 *p)
925 if (!is_utf8_char(p))
928 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
929 return swash_fetch(PL_utf8_punct, p);
933 Perl_is_utf8_xdigit(pTHX_ U8 *p)
935 if (!is_utf8_char(p))
938 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
939 return swash_fetch(PL_utf8_xdigit, p);
943 Perl_is_utf8_mark(pTHX_ U8 *p)
945 if (!is_utf8_char(p))
948 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
949 return swash_fetch(PL_utf8_mark, p);
953 Perl_to_utf8_upper(pTHX_ U8 *p)
957 if (!PL_utf8_toupper)
958 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
959 uv = swash_fetch(PL_utf8_toupper, p);
960 return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
964 Perl_to_utf8_title(pTHX_ U8 *p)
968 if (!PL_utf8_totitle)
969 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
970 uv = swash_fetch(PL_utf8_totitle, p);
971 return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
975 Perl_to_utf8_lower(pTHX_ U8 *p)
979 if (!PL_utf8_tolower)
980 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
981 uv = swash_fetch(PL_utf8_tolower, p);
982 return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
985 /* a "swash" is a swatch hash */
988 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
994 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
996 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
1000 PUSHSTACKi(PERLSI_MAGIC);
1003 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
1004 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
1006 PUSHs(sv_2mortal(newSViv(minbits)));
1007 PUSHs(sv_2mortal(newSViv(none)));
1013 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
1014 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
1015 if (call_method("SWASHNEW", G_SCALAR))
1016 retval = newSVsv(*PL_stack_sp--);
1018 retval = &PL_sv_undef;
1021 if (PL_curcop == &PL_compiling) {
1022 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
1023 PL_curcop->op_private = PL_hints;
1025 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
1026 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
1031 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
1033 HV* hv = (HV*)SvRV(sv);
1034 U32 klen = UTF8SKIP(ptr) - 1;
1035 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
1037 STRLEN needents = (klen ? 64 : 128);
1043 * This single-entry cache saves about 1/3 of the utf8 overhead in test
1044 * suite. (That is, only 7-8% overall over just a hash cache. Still,
1045 * it's nothing to sniff at.) Pity we usually come through at least
1046 * two function calls to get here...
1048 * NB: this code assumes that swatches are never modified, once generated!
1051 if (hv == PL_last_swash_hv &&
1052 klen == PL_last_swash_klen &&
1053 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
1055 tmps = PL_last_swash_tmps;
1056 slen = PL_last_swash_slen;
1059 /* Try our second-level swatch cache, kept in a hash. */
1060 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
1062 /* If not cached, generate it via utf8::SWASHGET */
1063 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
1068 PUSHSTACKi(PERLSI_MAGIC);
1072 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, STRLEN_MAX, 0, 0) & ~(needents - 1))));
1073 PUSHs(sv_2mortal(newSViv(needents)));
1075 if (call_method("SWASHGET", G_SCALAR))
1076 retval = newSVsv(*PL_stack_sp--);
1078 retval = &PL_sv_undef;
1082 if (PL_curcop == &PL_compiling)
1083 PL_curcop->op_private = PL_hints;
1085 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
1087 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
1088 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
1091 PL_last_swash_hv = hv;
1092 PL_last_swash_klen = klen;
1093 PL_last_swash_tmps = tmps;
1094 PL_last_swash_slen = slen;
1096 Copy(ptr, PL_last_swash_key, klen, U8);
1099 switch ((int)((slen << 3) / needents)) {
1101 bit = 1 << (off & 7);
1103 return (tmps[off] & bit) != 0;
1108 return (tmps[off] << 8) + tmps[off + 1] ;
1111 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
1113 Perl_croak(aTHX_ "panic: swash_fetch");