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 < 0x1000000000LL)
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;
198 if (uv <= 0x7f) { /* Pure ASCII. */
204 if ((uv >= 0x80 && uv <= 0xbf) &&
205 !(flags & UTF8_ALLOW_CONTINUATION)) {
207 Perl_warner(aTHX_ WARN_UTF8,
208 "Malformed UTF-8 character (unexpected continuation byte 0x%02"UVxf")",
213 if ((uv >= 0xc0 && uv <= 0xfd && curlen >1 && s[1] < 0x80) &&
214 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
216 Perl_warner(aTHX_ WARN_UTF8,
217 "Malformed UTF-8 character (unexpected non-continuation byte 0x%02"UVxf" after byte 0x%02"UVxf")",
222 if ((uv == 0xfe || uv == 0xff) &&
223 !(flags & UTF8_ALLOW_FE_FF)) {
225 Perl_warner(aTHX_ WARN_UTF8,
226 "Malformed UTF-8 character (byte 0x%02"UVxf")",
231 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
232 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
233 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
234 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
235 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
236 else if (!(uv & 0x01)) { len = 7; uv = 0; }
237 else { len = 13; uv = 0; } /* whoa! */
244 if ((curlen < expectlen) &&
245 !(flags & UTF8_ALLOW_SHORT)) {
247 Perl_warner(aTHX_ WARN_UTF8,
248 "Malformed UTF-8 character (%d byte%s, need %d)",
249 curlen, curlen > 1 ? "s" : "", expectlen);
258 if ((*s & 0xc0) != 0x80) {
260 Perl_warner(aTHX_ WARN_UTF8,
261 "Malformed UTF-8 character (unexpected continuation byte 0x%02x)",
266 uv = (uv << 6) | (*s & 0x3f);
268 /* This cannot be allowed. */
270 Perl_warner(aTHX_ WARN_UTF8,
271 "Malformed UTF-8 character (overflow at 0x%"UVxf", byte 0x%02x)",
279 if ((uv >= 0xd800 && uv <= 0xdfff) &&
280 !(flags & UTF8_ALLOW_SURROGATE)) {
282 Perl_warner(aTHX_ WARN_UTF8,
283 "Malformed UTF-8 character (UTF-16 surrogate 0x%04"UVxf")",
286 } else if ((uv == 0xfffe) &&
287 !(flags & UTF8_ALLOW_BOM)) {
289 Perl_warner(aTHX_ WARN_UTF8,
290 "Malformed UTF-8 character (byte order mark 0x%04"UVxf")",
293 } else if ((uv == 0xffff) &&
294 !(flags & UTF8_ALLOW_FFFF)) {
296 Perl_warner(aTHX_ WARN_UTF8,
297 "Malformed UTF-8 character (character 0x%04"UVxf")",
300 } else if ((expectlen > UNISKIP(uv)) &&
301 !(flags & UTF8_ALLOW_LONG)) {
303 Perl_warner(aTHX_ WARN_UTF8,
304 "Malformed UTF-8 character (%d byte%s, need %d)",
305 expectlen, expectlen > 1 ? "s": "", UNISKIP(uv));
313 if (flags & UTF8_CHECK_ONLY) {
322 return UNICODE_REPLACEMENT_CHARACTER;
326 =for apidoc Am|U8* s|utf8_to_uv_simple|STRLEN *retlen
328 Returns the character value of the first character in the string C<s>
329 which is assumed to be in UTF8 encoding; C<retlen> will be set to the
330 length, in bytes, of that character, and the pointer C<s> will be
331 advanced to the end of the character.
333 If C<s> does not point to a well-formed UTF8 character, zero is
334 returned and retlen is set, if possible, to -1.
340 Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
342 return Perl_utf8_to_uv(aTHX_ s, (STRLEN)-1, retlen, 0);
345 /* utf8_distance(a,b) returns the number of UTF8 characters between
346 the pointers a and b */
349 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
367 /* WARNING: do not use the following unless you *know* off is within bounds */
370 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
380 while ((*s & 0xc0) == 0x80)
389 =for apidoc Am|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
391 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
392 Unlike C<bytes_to_utf8>, this over-writes the original string, and
393 updates len to contain the new length.
394 Returns zero on failure, setting C<len> to -1.
400 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
406 /* ensure valid UTF8 and chars < 256 before updating string */
407 for (send = s + *len; s < send; ) {
412 ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
425 *d++ = (U8)utf8_to_uv_simple(s, &ulen);
435 =for apidoc Am|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
437 Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
438 Returns a pointer to the newly-created string, and sets C<len> to
439 reflect the new length.
445 Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
453 Newz(801, d, (*len) * 2 + 1, U8);
461 *d++ = (( uv >> 6) | 0xc0);
462 *d++ = (( uv & 0x3f) | 0x80);
471 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
473 * Destination must be pre-extended to 3/2 source. Do not use in-place.
474 * We optimize for native, for obvious reasons. */
477 Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
483 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
488 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
495 *d++ = (( uv >> 6) | 0xc0);
496 *d++ = (( uv & 0x3f) | 0x80);
499 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
502 if (low < 0xdc00 || low >= 0xdfff)
503 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
504 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
507 *d++ = (( uv >> 12) | 0xe0);
508 *d++ = (((uv >> 6) & 0x3f) | 0x80);
509 *d++ = (( uv & 0x3f) | 0x80);
513 *d++ = (( uv >> 18) | 0xf0);
514 *d++ = (((uv >> 12) & 0x3f) | 0x80);
515 *d++ = (((uv >> 6) & 0x3f) | 0x80);
516 *d++ = (( uv & 0x3f) | 0x80);
520 *newlen = d - dstart;
524 /* Note: this one is slightly destructive of the source. */
527 Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
530 U8* send = s + bytelen;
537 return utf16_to_utf8(p, d, bytelen, newlen);
540 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
543 Perl_is_uni_alnum(pTHX_ U32 c)
545 U8 tmpbuf[UTF8_MAXLEN];
546 uv_to_utf8(tmpbuf, (UV)c);
547 return is_utf8_alnum(tmpbuf);
551 Perl_is_uni_alnumc(pTHX_ U32 c)
553 U8 tmpbuf[UTF8_MAXLEN];
554 uv_to_utf8(tmpbuf, (UV)c);
555 return is_utf8_alnumc(tmpbuf);
559 Perl_is_uni_idfirst(pTHX_ U32 c)
561 U8 tmpbuf[UTF8_MAXLEN];
562 uv_to_utf8(tmpbuf, (UV)c);
563 return is_utf8_idfirst(tmpbuf);
567 Perl_is_uni_alpha(pTHX_ U32 c)
569 U8 tmpbuf[UTF8_MAXLEN];
570 uv_to_utf8(tmpbuf, (UV)c);
571 return is_utf8_alpha(tmpbuf);
575 Perl_is_uni_ascii(pTHX_ U32 c)
577 U8 tmpbuf[UTF8_MAXLEN];
578 uv_to_utf8(tmpbuf, (UV)c);
579 return is_utf8_ascii(tmpbuf);
583 Perl_is_uni_space(pTHX_ U32 c)
585 U8 tmpbuf[UTF8_MAXLEN];
586 uv_to_utf8(tmpbuf, (UV)c);
587 return is_utf8_space(tmpbuf);
591 Perl_is_uni_digit(pTHX_ U32 c)
593 U8 tmpbuf[UTF8_MAXLEN];
594 uv_to_utf8(tmpbuf, (UV)c);
595 return is_utf8_digit(tmpbuf);
599 Perl_is_uni_upper(pTHX_ U32 c)
601 U8 tmpbuf[UTF8_MAXLEN];
602 uv_to_utf8(tmpbuf, (UV)c);
603 return is_utf8_upper(tmpbuf);
607 Perl_is_uni_lower(pTHX_ U32 c)
609 U8 tmpbuf[UTF8_MAXLEN];
610 uv_to_utf8(tmpbuf, (UV)c);
611 return is_utf8_lower(tmpbuf);
615 Perl_is_uni_cntrl(pTHX_ U32 c)
617 U8 tmpbuf[UTF8_MAXLEN];
618 uv_to_utf8(tmpbuf, (UV)c);
619 return is_utf8_cntrl(tmpbuf);
623 Perl_is_uni_graph(pTHX_ U32 c)
625 U8 tmpbuf[UTF8_MAXLEN];
626 uv_to_utf8(tmpbuf, (UV)c);
627 return is_utf8_graph(tmpbuf);
631 Perl_is_uni_print(pTHX_ U32 c)
633 U8 tmpbuf[UTF8_MAXLEN];
634 uv_to_utf8(tmpbuf, (UV)c);
635 return is_utf8_print(tmpbuf);
639 Perl_is_uni_punct(pTHX_ U32 c)
641 U8 tmpbuf[UTF8_MAXLEN];
642 uv_to_utf8(tmpbuf, (UV)c);
643 return is_utf8_punct(tmpbuf);
647 Perl_is_uni_xdigit(pTHX_ U32 c)
649 U8 tmpbuf[UTF8_MAXLEN];
650 uv_to_utf8(tmpbuf, (UV)c);
651 return is_utf8_xdigit(tmpbuf);
655 Perl_to_uni_upper(pTHX_ U32 c)
657 U8 tmpbuf[UTF8_MAXLEN];
658 uv_to_utf8(tmpbuf, (UV)c);
659 return to_utf8_upper(tmpbuf);
663 Perl_to_uni_title(pTHX_ U32 c)
665 U8 tmpbuf[UTF8_MAXLEN];
666 uv_to_utf8(tmpbuf, (UV)c);
667 return to_utf8_title(tmpbuf);
671 Perl_to_uni_lower(pTHX_ U32 c)
673 U8 tmpbuf[UTF8_MAXLEN];
674 uv_to_utf8(tmpbuf, (UV)c);
675 return to_utf8_lower(tmpbuf);
678 /* for now these all assume no locale info available for Unicode > 255 */
681 Perl_is_uni_alnum_lc(pTHX_ U32 c)
683 return is_uni_alnum(c); /* XXX no locale support yet */
687 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
689 return is_uni_alnumc(c); /* XXX no locale support yet */
693 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
695 return is_uni_idfirst(c); /* XXX no locale support yet */
699 Perl_is_uni_alpha_lc(pTHX_ U32 c)
701 return is_uni_alpha(c); /* XXX no locale support yet */
705 Perl_is_uni_ascii_lc(pTHX_ U32 c)
707 return is_uni_ascii(c); /* XXX no locale support yet */
711 Perl_is_uni_space_lc(pTHX_ U32 c)
713 return is_uni_space(c); /* XXX no locale support yet */
717 Perl_is_uni_digit_lc(pTHX_ U32 c)
719 return is_uni_digit(c); /* XXX no locale support yet */
723 Perl_is_uni_upper_lc(pTHX_ U32 c)
725 return is_uni_upper(c); /* XXX no locale support yet */
729 Perl_is_uni_lower_lc(pTHX_ U32 c)
731 return is_uni_lower(c); /* XXX no locale support yet */
735 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
737 return is_uni_cntrl(c); /* XXX no locale support yet */
741 Perl_is_uni_graph_lc(pTHX_ U32 c)
743 return is_uni_graph(c); /* XXX no locale support yet */
747 Perl_is_uni_print_lc(pTHX_ U32 c)
749 return is_uni_print(c); /* XXX no locale support yet */
753 Perl_is_uni_punct_lc(pTHX_ U32 c)
755 return is_uni_punct(c); /* XXX no locale support yet */
759 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
761 return is_uni_xdigit(c); /* XXX no locale support yet */
765 Perl_to_uni_upper_lc(pTHX_ U32 c)
767 return to_uni_upper(c); /* XXX no locale support yet */
771 Perl_to_uni_title_lc(pTHX_ U32 c)
773 return to_uni_title(c); /* XXX no locale support yet */
777 Perl_to_uni_lower_lc(pTHX_ U32 c)
779 return to_uni_lower(c); /* XXX no locale support yet */
783 Perl_is_utf8_alnum(pTHX_ U8 *p)
785 if (!is_utf8_char(p))
788 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
789 * descendant of isalnum(3), in other words, it doesn't
790 * contain the '_'. --jhi */
791 PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
792 return swash_fetch(PL_utf8_alnum, p);
793 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
794 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
796 PL_utf8_alnum = swash_init("utf8", "",
797 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
798 return swash_fetch(PL_utf8_alnum, p);
803 Perl_is_utf8_alnumc(pTHX_ U8 *p)
805 if (!is_utf8_char(p))
808 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
809 return swash_fetch(PL_utf8_alnum, p);
810 /* return is_utf8_alpha(p) || is_utf8_digit(p); */
811 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
813 PL_utf8_alnum = swash_init("utf8", "",
814 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
815 return swash_fetch(PL_utf8_alnum, p);
820 Perl_is_utf8_idfirst(pTHX_ U8 *p)
822 return *p == '_' || is_utf8_alpha(p);
826 Perl_is_utf8_alpha(pTHX_ U8 *p)
828 if (!is_utf8_char(p))
831 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
832 return swash_fetch(PL_utf8_alpha, p);
836 Perl_is_utf8_ascii(pTHX_ U8 *p)
838 if (!is_utf8_char(p))
841 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
842 return swash_fetch(PL_utf8_ascii, p);
846 Perl_is_utf8_space(pTHX_ U8 *p)
848 if (!is_utf8_char(p))
851 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
852 return swash_fetch(PL_utf8_space, p);
856 Perl_is_utf8_digit(pTHX_ U8 *p)
858 if (!is_utf8_char(p))
861 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
862 return swash_fetch(PL_utf8_digit, p);
866 Perl_is_utf8_upper(pTHX_ U8 *p)
868 if (!is_utf8_char(p))
871 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
872 return swash_fetch(PL_utf8_upper, p);
876 Perl_is_utf8_lower(pTHX_ U8 *p)
878 if (!is_utf8_char(p))
881 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
882 return swash_fetch(PL_utf8_lower, p);
886 Perl_is_utf8_cntrl(pTHX_ U8 *p)
888 if (!is_utf8_char(p))
891 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
892 return swash_fetch(PL_utf8_cntrl, p);
896 Perl_is_utf8_graph(pTHX_ U8 *p)
898 if (!is_utf8_char(p))
901 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
902 return swash_fetch(PL_utf8_graph, p);
906 Perl_is_utf8_print(pTHX_ U8 *p)
908 if (!is_utf8_char(p))
911 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
912 return swash_fetch(PL_utf8_print, p);
916 Perl_is_utf8_punct(pTHX_ U8 *p)
918 if (!is_utf8_char(p))
921 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
922 return swash_fetch(PL_utf8_punct, p);
926 Perl_is_utf8_xdigit(pTHX_ U8 *p)
928 if (!is_utf8_char(p))
931 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
932 return swash_fetch(PL_utf8_xdigit, p);
936 Perl_is_utf8_mark(pTHX_ U8 *p)
938 if (!is_utf8_char(p))
941 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
942 return swash_fetch(PL_utf8_mark, p);
946 Perl_to_utf8_upper(pTHX_ U8 *p)
950 if (!PL_utf8_toupper)
951 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
952 uv = swash_fetch(PL_utf8_toupper, p);
953 return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
957 Perl_to_utf8_title(pTHX_ U8 *p)
961 if (!PL_utf8_totitle)
962 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
963 uv = swash_fetch(PL_utf8_totitle, p);
964 return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
968 Perl_to_utf8_lower(pTHX_ U8 *p)
972 if (!PL_utf8_tolower)
973 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
974 uv = swash_fetch(PL_utf8_tolower, p);
975 return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
978 /* a "swash" is a swatch hash */
981 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
987 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
989 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
993 PUSHSTACKi(PERLSI_MAGIC);
996 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
997 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
999 PUSHs(sv_2mortal(newSViv(minbits)));
1000 PUSHs(sv_2mortal(newSViv(none)));
1006 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
1007 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
1008 if (call_method("SWASHNEW", G_SCALAR))
1009 retval = newSVsv(*PL_stack_sp--);
1011 retval = &PL_sv_undef;
1014 if (PL_curcop == &PL_compiling) {
1015 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
1016 PL_curcop->op_private = PL_hints;
1018 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
1019 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
1024 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
1026 HV* hv = (HV*)SvRV(sv);
1027 U32 klen = UTF8SKIP(ptr) - 1;
1028 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
1030 STRLEN needents = (klen ? 64 : 128);
1036 * This single-entry cache saves about 1/3 of the utf8 overhead in test
1037 * suite. (That is, only 7-8% overall over just a hash cache. Still,
1038 * it's nothing to sniff at.) Pity we usually come through at least
1039 * two function calls to get here...
1041 * NB: this code assumes that swatches are never modified, once generated!
1044 if (hv == PL_last_swash_hv &&
1045 klen == PL_last_swash_klen &&
1046 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
1048 tmps = PL_last_swash_tmps;
1049 slen = PL_last_swash_slen;
1052 /* Try our second-level swatch cache, kept in a hash. */
1053 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
1055 /* If not cached, generate it via utf8::SWASHGET */
1056 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
1061 PUSHSTACKi(PERLSI_MAGIC);
1065 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, STRLEN_MAX, 0, 0) & ~(needents - 1))));
1066 PUSHs(sv_2mortal(newSViv(needents)));
1068 if (call_method("SWASHGET", G_SCALAR))
1069 retval = newSVsv(*PL_stack_sp--);
1071 retval = &PL_sv_undef;
1075 if (PL_curcop == &PL_compiling)
1076 PL_curcop->op_private = PL_hints;
1078 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
1080 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
1081 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
1084 PL_last_swash_hv = hv;
1085 PL_last_swash_klen = klen;
1086 PL_last_swash_tmps = tmps;
1087 PL_last_swash_slen = slen;
1089 Copy(ptr, PL_last_swash_key, klen, U8);
1092 switch ((slen << 3) / needents) {
1094 bit = 1 << (off & 7);
1096 return (tmps[off] & bit) != 0;
1101 return (tmps[off] << 8) + tmps[off + 1] ;
1104 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
1106 Perl_croak(aTHX_ "panic: swash_fetch");