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) /* the d must be UTF8_MAXLEN+1 deep */
38 *d++ = (( uv >> 6) | 0xc0);
39 *d++ = (( uv & 0x3f) | 0x80);
44 *d++ = (( uv >> 12) | 0xe0);
45 *d++ = (((uv >> 6) & 0x3f) | 0x80);
46 *d++ = (( uv & 0x3f) | 0x80);
51 *d++ = (( uv >> 18) | 0xf0);
52 *d++ = (((uv >> 12) & 0x3f) | 0x80);
53 *d++ = (((uv >> 6) & 0x3f) | 0x80);
54 *d++ = (( uv & 0x3f) | 0x80);
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);
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);
78 if (uv < UTF8_QUAD_MAX)
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);
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);
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. */
116 Perl_is_utf8_char(pTHX_ U8 *s)
125 if (u >= 0x80 && u <= 0xbf)
130 if (len < 2 || (u >= 0xc0 && u <= 0xfd && s[1] < 0x80))
138 if ((*s & 0xc0) != 0x80)
140 uv = UTF8_ACCUMULATE(uv, *s);
147 if (UNISKIP(uv) < len)
154 =for apidoc Am|is_utf8_string|U8 *s|STRLEN len
156 Returns true if first C<len> bytes of the given string form valid a UTF8
157 string, false otherwise.
163 Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
182 =for apidoc Am|U8* s|utf8_to_uv|STRLEN curlen|STRLEN *retlen|U32 flags
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.
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>).
201 Perl_utf8_to_uv(pTHX_ U8* s, STRLEN curlen, STRLEN* retlen, U32 flags)
208 bool dowarn = ckWARN_d(WARN_UTF8);
210 STRLEN expectlen = 0;
214 Perl_warner(aTHX_ WARN_UTF8,
215 "Malformed UTF-8 character (an empty string)");
219 if (UTF8_IS_ASCII(uv)) {
225 if (UTF8_IS_CONTINUATION(uv) &&
226 !(flags & UTF8_ALLOW_CONTINUATION)) {
228 Perl_warner(aTHX_ WARN_UTF8,
229 "Malformed UTF-8 character (unexpected continuation byte 0x%02"UVxf")",
234 if (UTF8_IS_START(uv) && curlen > 1 && !UTF8_IS_CONTINUATION(s[1]) &&
235 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
237 Perl_warner(aTHX_ WARN_UTF8,
238 "Malformed UTF-8 character (unexpected non-continuation byte 0x%02"UVxf" after start byte 0x%02"UVxf")",
243 if ((uv == 0xfe || uv == 0xff) &&
244 !(flags & UTF8_ALLOW_FE_FF)) {
246 Perl_warner(aTHX_ WARN_UTF8,
247 "Malformed UTF-8 character (byte 0x%02"UVxf")",
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! */
265 if ((curlen < expectlen) &&
266 !(flags & UTF8_ALLOW_SHORT)) {
268 Perl_warner(aTHX_ WARN_UTF8,
269 "Malformed UTF-8 character (%d byte%s, need %d)",
270 curlen, curlen == 1 ? "" : "s", expectlen);
279 if (!UTF8_IS_CONTINUATION(*s) &&
280 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
282 Perl_warner(aTHX_ WARN_UTF8,
283 "Malformed UTF-8 character (unexpected non-continuation byte 0x%02x)",
288 uv = UTF8_ACCUMULATE(uv, *s);
290 /* This cannot be allowed. */
292 Perl_warner(aTHX_ WARN_UTF8,
293 "Malformed UTF-8 character (overflow at 0x%"UVxf", byte 0x%02x)",
301 if (UNICODE_IS_SURROGATE(uv) &&
302 !(flags & UTF8_ALLOW_SURROGATE)) {
304 Perl_warner(aTHX_ WARN_UTF8,
305 "Malformed UTF-8 character (UTF-16 surrogate 0x%04"UVxf")",
308 } else if (UNICODE_IS_BYTE_ORDER_MARK(uv) &&
309 !(flags & UTF8_ALLOW_BOM)) {
311 Perl_warner(aTHX_ WARN_UTF8,
312 "Malformed UTF-8 character (byte order mark 0x%04"UVxf")",
315 } else if ((expectlen > UNISKIP(uv)) &&
316 !(flags & UTF8_ALLOW_LONG)) {
318 Perl_warner(aTHX_ WARN_UTF8,
319 "Malformed UTF-8 character (%d byte%s, need %d)",
320 expectlen, expectlen == 1 ? "": "s", UNISKIP(uv));
322 } else if (UNICODE_IS_ILLEGAL(uv) &&
323 !(flags & UTF8_ALLOW_FFFF)) {
325 Perl_warner(aTHX_ WARN_UTF8,
326 "Malformed UTF-8 character (character 0x%04"UVxf")",
335 if (flags & UTF8_CHECK_ONLY) {
344 return UNICODE_REPLACEMENT;
348 =for apidoc Am|U8* s|utf8_to_uv_simple|STRLEN *retlen
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.
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.
362 Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
364 return Perl_utf8_to_uv(aTHX_ s, UTF8_MAXLEN, retlen, 0);
368 =for apidoc Am|STRLEN|utf8_length|U8* s|U8 *e
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.
378 Perl_utf8_length(pTHX_ U8* s, U8* e)
382 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
383 * the bitops (especially ~) can create illegal UTF-8.
384 * In other words: in Perl UTF-8 is not just for Unicode. */
387 Perl_croak(aTHX_ "panic: utf8_length: unexpected end");
392 Perl_croak(aTHX_ "panic: utf8_length: unaligned end");
401 =for apidoc Am|IV|utf8_distance|U8 *a|U8 *b
403 Returns the number of UTF8 characters between the UTF-8 pointers C<a>
406 WARNING: use only if you *know* that the pointers point inside the
412 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
416 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
417 * the bitops (especially ~) can create illegal UTF-8.
418 * In other words: in Perl UTF-8 is not just for Unicode. */
425 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
435 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
445 =for apidoc Am|U8*|utf8_hop|U8 *s|I32 off
447 Return the UTF-8 pointer C<s> displaced by C<off> characters, either
450 WARNING: do not use the following unless you *know* C<off> is within
451 the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned
452 on the first byte of character or just after the last byte of a character.
457 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
459 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
460 * the bitops (especially ~) can create illegal UTF-8.
461 * In other words: in Perl UTF-8 is not just for Unicode. */
470 while (UTF8_IS_CONTINUATION(*s))
478 =for apidoc Am|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
480 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
481 Unlike C<bytes_to_utf8>, this over-writes the original string, and
482 updates len to contain the new length.
483 Returns zero on failure, setting C<len> to -1.
489 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
495 /* ensure valid UTF8 and chars < 256 before updating string */
496 for (send = s + *len; s < send; ) {
501 ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
514 *d++ = (U8)utf8_to_uv_simple(s, &ulen);
524 =for apidoc Am|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
526 Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
527 Returns a pointer to the newly-created string, and sets C<len> to
528 reflect the new length.
534 Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
541 Newz(801, d, (*len) * 2 + 1, U8);
549 *d++ = (( uv >> 6) | 0xc0);
550 *d++ = (( uv & 0x3f) | 0x80);
559 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
561 * Destination must be pre-extended to 3/2 source. Do not use in-place.
562 * We optimize for native, for obvious reasons. */
565 Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
571 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
576 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
583 *d++ = (( uv >> 6) | 0xc0);
584 *d++ = (( uv & 0x3f) | 0x80);
587 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
589 if (low < 0xdc00 || low >= 0xdfff)
590 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
591 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
594 *d++ = (( uv >> 12) | 0xe0);
595 *d++ = (((uv >> 6) & 0x3f) | 0x80);
596 *d++ = (( uv & 0x3f) | 0x80);
600 *d++ = (( uv >> 18) | 0xf0);
601 *d++ = (((uv >> 12) & 0x3f) | 0x80);
602 *d++ = (((uv >> 6) & 0x3f) | 0x80);
603 *d++ = (( uv & 0x3f) | 0x80);
607 *newlen = d - dstart;
611 /* Note: this one is slightly destructive of the source. */
614 Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
617 U8* send = s + bytelen;
624 return utf16_to_utf8(p, d, bytelen, newlen);
627 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
630 Perl_is_uni_alnum(pTHX_ U32 c)
632 U8 tmpbuf[UTF8_MAXLEN+1];
633 uv_to_utf8(tmpbuf, (UV)c);
634 return is_utf8_alnum(tmpbuf);
638 Perl_is_uni_alnumc(pTHX_ U32 c)
640 U8 tmpbuf[UTF8_MAXLEN+1];
641 uv_to_utf8(tmpbuf, (UV)c);
642 return is_utf8_alnumc(tmpbuf);
646 Perl_is_uni_idfirst(pTHX_ U32 c)
648 U8 tmpbuf[UTF8_MAXLEN+1];
649 uv_to_utf8(tmpbuf, (UV)c);
650 return is_utf8_idfirst(tmpbuf);
654 Perl_is_uni_alpha(pTHX_ U32 c)
656 U8 tmpbuf[UTF8_MAXLEN+1];
657 uv_to_utf8(tmpbuf, (UV)c);
658 return is_utf8_alpha(tmpbuf);
662 Perl_is_uni_ascii(pTHX_ U32 c)
664 U8 tmpbuf[UTF8_MAXLEN+1];
665 uv_to_utf8(tmpbuf, (UV)c);
666 return is_utf8_ascii(tmpbuf);
670 Perl_is_uni_space(pTHX_ U32 c)
672 U8 tmpbuf[UTF8_MAXLEN+1];
673 uv_to_utf8(tmpbuf, (UV)c);
674 return is_utf8_space(tmpbuf);
678 Perl_is_uni_digit(pTHX_ U32 c)
680 U8 tmpbuf[UTF8_MAXLEN+1];
681 uv_to_utf8(tmpbuf, (UV)c);
682 return is_utf8_digit(tmpbuf);
686 Perl_is_uni_upper(pTHX_ U32 c)
688 U8 tmpbuf[UTF8_MAXLEN+1];
689 uv_to_utf8(tmpbuf, (UV)c);
690 return is_utf8_upper(tmpbuf);
694 Perl_is_uni_lower(pTHX_ U32 c)
696 U8 tmpbuf[UTF8_MAXLEN+1];
697 uv_to_utf8(tmpbuf, (UV)c);
698 return is_utf8_lower(tmpbuf);
702 Perl_is_uni_cntrl(pTHX_ U32 c)
704 U8 tmpbuf[UTF8_MAXLEN+1];
705 uv_to_utf8(tmpbuf, (UV)c);
706 return is_utf8_cntrl(tmpbuf);
710 Perl_is_uni_graph(pTHX_ U32 c)
712 U8 tmpbuf[UTF8_MAXLEN+1];
713 uv_to_utf8(tmpbuf, (UV)c);
714 return is_utf8_graph(tmpbuf);
718 Perl_is_uni_print(pTHX_ U32 c)
720 U8 tmpbuf[UTF8_MAXLEN+1];
721 uv_to_utf8(tmpbuf, (UV)c);
722 return is_utf8_print(tmpbuf);
726 Perl_is_uni_punct(pTHX_ U32 c)
728 U8 tmpbuf[UTF8_MAXLEN+1];
729 uv_to_utf8(tmpbuf, (UV)c);
730 return is_utf8_punct(tmpbuf);
734 Perl_is_uni_xdigit(pTHX_ U32 c)
736 U8 tmpbuf[UTF8_MAXLEN+1];
737 uv_to_utf8(tmpbuf, (UV)c);
738 return is_utf8_xdigit(tmpbuf);
742 Perl_to_uni_upper(pTHX_ U32 c)
744 U8 tmpbuf[UTF8_MAXLEN+1];
745 uv_to_utf8(tmpbuf, (UV)c);
746 return to_utf8_upper(tmpbuf);
750 Perl_to_uni_title(pTHX_ U32 c)
752 U8 tmpbuf[UTF8_MAXLEN+1];
753 uv_to_utf8(tmpbuf, (UV)c);
754 return to_utf8_title(tmpbuf);
758 Perl_to_uni_lower(pTHX_ U32 c)
760 U8 tmpbuf[UTF8_MAXLEN+1];
761 uv_to_utf8(tmpbuf, (UV)c);
762 return to_utf8_lower(tmpbuf);
765 /* for now these all assume no locale info available for Unicode > 255 */
768 Perl_is_uni_alnum_lc(pTHX_ U32 c)
770 return is_uni_alnum(c); /* XXX no locale support yet */
774 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
776 return is_uni_alnumc(c); /* XXX no locale support yet */
780 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
782 return is_uni_idfirst(c); /* XXX no locale support yet */
786 Perl_is_uni_alpha_lc(pTHX_ U32 c)
788 return is_uni_alpha(c); /* XXX no locale support yet */
792 Perl_is_uni_ascii_lc(pTHX_ U32 c)
794 return is_uni_ascii(c); /* XXX no locale support yet */
798 Perl_is_uni_space_lc(pTHX_ U32 c)
800 return is_uni_space(c); /* XXX no locale support yet */
804 Perl_is_uni_digit_lc(pTHX_ U32 c)
806 return is_uni_digit(c); /* XXX no locale support yet */
810 Perl_is_uni_upper_lc(pTHX_ U32 c)
812 return is_uni_upper(c); /* XXX no locale support yet */
816 Perl_is_uni_lower_lc(pTHX_ U32 c)
818 return is_uni_lower(c); /* XXX no locale support yet */
822 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
824 return is_uni_cntrl(c); /* XXX no locale support yet */
828 Perl_is_uni_graph_lc(pTHX_ U32 c)
830 return is_uni_graph(c); /* XXX no locale support yet */
834 Perl_is_uni_print_lc(pTHX_ U32 c)
836 return is_uni_print(c); /* XXX no locale support yet */
840 Perl_is_uni_punct_lc(pTHX_ U32 c)
842 return is_uni_punct(c); /* XXX no locale support yet */
846 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
848 return is_uni_xdigit(c); /* XXX no locale support yet */
852 Perl_to_uni_upper_lc(pTHX_ U32 c)
854 return to_uni_upper(c); /* XXX no locale support yet */
858 Perl_to_uni_title_lc(pTHX_ U32 c)
860 return to_uni_title(c); /* XXX no locale support yet */
864 Perl_to_uni_lower_lc(pTHX_ U32 c)
866 return to_uni_lower(c); /* XXX no locale support yet */
870 Perl_is_utf8_alnum(pTHX_ U8 *p)
872 if (!is_utf8_char(p))
875 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
876 * descendant of isalnum(3), in other words, it doesn't
877 * contain the '_'. --jhi */
878 PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
879 return swash_fetch(PL_utf8_alnum, p);
880 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
881 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
883 PL_utf8_alnum = swash_init("utf8", "",
884 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
885 return swash_fetch(PL_utf8_alnum, p);
890 Perl_is_utf8_alnumc(pTHX_ U8 *p)
892 if (!is_utf8_char(p))
895 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
896 return swash_fetch(PL_utf8_alnum, p);
897 /* return is_utf8_alpha(p) || is_utf8_digit(p); */
898 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
900 PL_utf8_alnum = swash_init("utf8", "",
901 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
902 return swash_fetch(PL_utf8_alnum, p);
907 Perl_is_utf8_idfirst(pTHX_ U8 *p)
909 return *p == '_' || is_utf8_alpha(p);
913 Perl_is_utf8_alpha(pTHX_ U8 *p)
915 if (!is_utf8_char(p))
918 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
919 return swash_fetch(PL_utf8_alpha, p);
923 Perl_is_utf8_ascii(pTHX_ U8 *p)
925 if (!is_utf8_char(p))
928 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
929 return swash_fetch(PL_utf8_ascii, p);
933 Perl_is_utf8_space(pTHX_ U8 *p)
935 if (!is_utf8_char(p))
938 PL_utf8_space = swash_init("utf8", "IsSpacePerl", &PL_sv_undef, 0, 0);
939 return swash_fetch(PL_utf8_space, p);
943 Perl_is_utf8_digit(pTHX_ U8 *p)
945 if (!is_utf8_char(p))
948 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
949 return swash_fetch(PL_utf8_digit, p);
953 Perl_is_utf8_upper(pTHX_ U8 *p)
955 if (!is_utf8_char(p))
958 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
959 return swash_fetch(PL_utf8_upper, p);
963 Perl_is_utf8_lower(pTHX_ U8 *p)
965 if (!is_utf8_char(p))
968 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
969 return swash_fetch(PL_utf8_lower, p);
973 Perl_is_utf8_cntrl(pTHX_ U8 *p)
975 if (!is_utf8_char(p))
978 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
979 return swash_fetch(PL_utf8_cntrl, p);
983 Perl_is_utf8_graph(pTHX_ U8 *p)
985 if (!is_utf8_char(p))
988 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
989 return swash_fetch(PL_utf8_graph, p);
993 Perl_is_utf8_print(pTHX_ U8 *p)
995 if (!is_utf8_char(p))
998 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
999 return swash_fetch(PL_utf8_print, p);
1003 Perl_is_utf8_punct(pTHX_ U8 *p)
1005 if (!is_utf8_char(p))
1008 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
1009 return swash_fetch(PL_utf8_punct, p);
1013 Perl_is_utf8_xdigit(pTHX_ U8 *p)
1015 if (!is_utf8_char(p))
1017 if (!PL_utf8_xdigit)
1018 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
1019 return swash_fetch(PL_utf8_xdigit, p);
1023 Perl_is_utf8_mark(pTHX_ U8 *p)
1025 if (!is_utf8_char(p))
1028 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
1029 return swash_fetch(PL_utf8_mark, p);
1033 Perl_to_utf8_upper(pTHX_ U8 *p)
1037 if (!PL_utf8_toupper)
1038 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
1039 uv = swash_fetch(PL_utf8_toupper, p);
1040 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1044 Perl_to_utf8_title(pTHX_ U8 *p)
1048 if (!PL_utf8_totitle)
1049 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
1050 uv = swash_fetch(PL_utf8_totitle, p);
1051 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1055 Perl_to_utf8_lower(pTHX_ U8 *p)
1059 if (!PL_utf8_tolower)
1060 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
1061 uv = swash_fetch(PL_utf8_tolower, p);
1062 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1065 /* a "swash" is a swatch hash */
1068 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
1074 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
1076 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
1080 PUSHSTACKi(PERLSI_MAGIC);
1083 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
1084 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
1086 PUSHs(sv_2mortal(newSViv(minbits)));
1087 PUSHs(sv_2mortal(newSViv(none)));
1093 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
1094 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
1095 if (call_method("SWASHNEW", G_SCALAR))
1096 retval = newSVsv(*PL_stack_sp--);
1098 retval = &PL_sv_undef;
1101 if (PL_curcop == &PL_compiling) {
1102 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
1103 PL_curcop->op_private = PL_hints;
1105 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
1106 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
1111 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
1113 HV* hv = (HV*)SvRV(sv);
1114 U32 klen = UTF8SKIP(ptr) - 1;
1115 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
1117 STRLEN needents = (klen ? 64 : 128);
1123 * This single-entry cache saves about 1/3 of the utf8 overhead in test
1124 * suite. (That is, only 7-8% overall over just a hash cache. Still,
1125 * it's nothing to sniff at.) Pity we usually come through at least
1126 * two function calls to get here...
1128 * NB: this code assumes that swatches are never modified, once generated!
1131 if (hv == PL_last_swash_hv &&
1132 klen == PL_last_swash_klen &&
1133 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
1135 tmps = PL_last_swash_tmps;
1136 slen = PL_last_swash_slen;
1139 /* Try our second-level swatch cache, kept in a hash. */
1140 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
1142 /* If not cached, generate it via utf8::SWASHGET */
1143 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
1148 PUSHSTACKi(PERLSI_MAGIC);
1152 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, UTF8_MAXLEN, 0, 0) & ~(needents - 1))));
1153 PUSHs(sv_2mortal(newSViv(needents)));
1155 if (call_method("SWASHGET", G_SCALAR))
1156 retval = newSVsv(*PL_stack_sp--);
1158 retval = &PL_sv_undef;
1162 if (PL_curcop == &PL_compiling)
1163 PL_curcop->op_private = PL_hints;
1165 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
1167 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
1168 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
1171 PL_last_swash_hv = hv;
1172 PL_last_swash_klen = klen;
1173 PL_last_swash_tmps = tmps;
1174 PL_last_swash_slen = slen;
1176 Copy(ptr, PL_last_swash_key, klen, U8);
1179 switch ((int)((slen << 3) / needents)) {
1181 bit = 1 << (off & 7);
1183 return (tmps[off] & bit) != 0;
1188 return (tmps[off] << 8) + tmps[off + 1] ;
1191 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
1193 Perl_croak(aTHX_ "panic: swash_fetch");