3 * Copyright (c) 1998-2001, 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 =for apidoc A|U8*|uv_to_utf8|U8 *d|UV uv
32 Adds the UTF8 representation of the Unicode codepoint C<uv> to the end
33 of the string C<d>; C<d> should be have at least C<UTF8_MAXLEN+1> free
34 bytes available. The return value is the pointer to the byte after the
35 end of the new character. In other words,
37 d = uv_to_utf8(d, uv);
39 is the recommended Unicode-aware way of saying
47 Perl_uv_to_utf8(pTHX_ U8 *d, UV uv)
54 *d++ = (( uv >> 6) | 0xc0);
55 *d++ = (( uv & 0x3f) | 0x80);
59 *d++ = (( uv >> 12) | 0xe0);
60 *d++ = (((uv >> 6) & 0x3f) | 0x80);
61 *d++ = (( uv & 0x3f) | 0x80);
65 *d++ = (( uv >> 18) | 0xf0);
66 *d++ = (((uv >> 12) & 0x3f) | 0x80);
67 *d++ = (((uv >> 6) & 0x3f) | 0x80);
68 *d++ = (( uv & 0x3f) | 0x80);
72 *d++ = (( uv >> 24) | 0xf8);
73 *d++ = (((uv >> 18) & 0x3f) | 0x80);
74 *d++ = (((uv >> 12) & 0x3f) | 0x80);
75 *d++ = (((uv >> 6) & 0x3f) | 0x80);
76 *d++ = (( uv & 0x3f) | 0x80);
79 if (uv < 0x80000000) {
80 *d++ = (( uv >> 30) | 0xfc);
81 *d++ = (((uv >> 24) & 0x3f) | 0x80);
82 *d++ = (((uv >> 18) & 0x3f) | 0x80);
83 *d++ = (((uv >> 12) & 0x3f) | 0x80);
84 *d++ = (((uv >> 6) & 0x3f) | 0x80);
85 *d++ = (( uv & 0x3f) | 0x80);
89 if (uv < UTF8_QUAD_MAX)
92 *d++ = 0xfe; /* Can't match U+FEFF! */
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);
103 *d++ = 0xff; /* Can't match U+FFFE! */
104 *d++ = 0x80; /* 6 Reserved bits */
105 *d++ = (((uv >> 60) & 0x0f) | 0x80); /* 2 Reserved bits */
106 *d++ = (((uv >> 54) & 0x3f) | 0x80);
107 *d++ = (((uv >> 48) & 0x3f) | 0x80);
108 *d++ = (((uv >> 42) & 0x3f) | 0x80);
109 *d++ = (((uv >> 36) & 0x3f) | 0x80);
110 *d++ = (((uv >> 30) & 0x3f) | 0x80);
111 *d++ = (((uv >> 24) & 0x3f) | 0x80);
112 *d++ = (((uv >> 18) & 0x3f) | 0x80);
113 *d++ = (((uv >> 12) & 0x3f) | 0x80);
114 *d++ = (((uv >> 6) & 0x3f) | 0x80);
115 *d++ = (( uv & 0x3f) | 0x80);
122 =for apidoc A|STRLEN|is_utf8_char|U8 *s
124 Tests if some arbitrary number of bytes begins in a valid UTF-8 character.
125 The actual number of bytes in the UTF-8 character will be returned if it
126 is valid, otherwise 0.
131 Perl_is_utf8_char(pTHX_ U8 *s)
137 if (UTF8_IS_ASCII(u))
140 if (!UTF8_IS_START(u))
145 if (len < 2 || !UTF8_IS_CONTINUATION(s[1]))
153 if (!UTF8_IS_CONTINUATION(*s))
155 uv = UTF8_ACCUMULATE(uv, *s);
162 if (UNISKIP(uv) < len)
169 =for apidoc A|bool|is_utf8_string|U8 *s|STRLEN len
171 Returns true if first C<len> bytes of the given string form valid a UTF8
172 string, false otherwise.
178 Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
185 len = strlen((char *)s);
201 =for apidoc A|U8* s|utf8_to_uv|STRLEN curlen|STRLEN *retlen|U32 flags
203 Returns the character value of the first character in the string C<s>
204 which is assumed to be in UTF8 encoding and no longer than C<curlen>;
205 C<retlen> will be set to the length, in bytes, of that character.
207 If C<s> does not point to a well-formed UTF8 character, the behaviour
208 is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
209 it is assumed that the caller will raise a warning, and this function
210 will silently just set C<retlen> to C<-1> and return zero. If the
211 C<flags> does not contain UTF8_CHECK_ONLY, warnings about
212 malformations will be given, C<retlen> will be set to the expected
213 length of the UTF-8 character in bytes, and zero will be returned.
215 The C<flags> can also contain various flags to allow deviations from
216 the strict UTF-8 encoding (see F<utf8.h>).
221 Perl_utf8_to_uv(pTHX_ U8* s, STRLEN curlen, STRLEN* retlen, U32 flags)
228 bool dowarn = ckWARN_d(WARN_UTF8);
230 STRLEN expectlen = 0;
233 /* This list is a superset of the UTF8_ALLOW_XXX. */
235 #define UTF8_WARN_EMPTY 1
236 #define UTF8_WARN_CONTINUATION 2
237 #define UTF8_WARN_NON_CONTINUATION 3
238 #define UTF8_WARN_FE_FF 4
239 #define UTF8_WARN_SHORT 5
240 #define UTF8_WARN_OVERFLOW 6
241 #define UTF8_WARN_SURROGATE 7
242 #define UTF8_WARN_BOM 8
243 #define UTF8_WARN_LONG 9
244 #define UTF8_WARN_FFFF 10
247 !(flags & UTF8_ALLOW_EMPTY)) {
248 warning = UTF8_WARN_EMPTY;
252 if (UTF8_IS_ASCII(uv)) {
258 if (UTF8_IS_CONTINUATION(uv) &&
259 !(flags & UTF8_ALLOW_CONTINUATION)) {
260 warning = UTF8_WARN_CONTINUATION;
264 if (UTF8_IS_START(uv) && curlen > 1 && !UTF8_IS_CONTINUATION(s[1]) &&
265 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
266 warning = UTF8_WARN_NON_CONTINUATION;
270 if ((uv == 0xfe || uv == 0xff) &&
271 !(flags & UTF8_ALLOW_FE_FF)) {
272 warning = UTF8_WARN_FE_FF;
276 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
277 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
278 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
279 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
280 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
281 else if (!(uv & 0x01)) { len = 7; uv = 0; }
282 else { len = 13; uv = 0; } /* whoa! */
289 if ((curlen < expectlen) &&
290 !(flags & UTF8_ALLOW_SHORT)) {
291 warning = UTF8_WARN_SHORT;
300 if (!UTF8_IS_CONTINUATION(*s) &&
301 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
303 warning = UTF8_WARN_NON_CONTINUATION;
307 uv = UTF8_ACCUMULATE(uv, *s);
309 /* These cannot be allowed. */
311 if (!(flags & UTF8_ALLOW_LONG)) {
312 warning = UTF8_WARN_LONG;
316 else { /* uv < ouv */
317 /* This cannot be allowed. */
318 warning = UTF8_WARN_OVERFLOW;
326 if (UNICODE_IS_SURROGATE(uv) &&
327 !(flags & UTF8_ALLOW_SURROGATE)) {
328 warning = UTF8_WARN_SURROGATE;
330 } else if (UNICODE_IS_BYTE_ORDER_MARK(uv) &&
331 !(flags & UTF8_ALLOW_BOM)) {
332 warning = UTF8_WARN_BOM;
334 } else if ((expectlen > UNISKIP(uv)) &&
335 !(flags & UTF8_ALLOW_LONG)) {
336 warning = UTF8_WARN_LONG;
338 } else if (UNICODE_IS_ILLEGAL(uv) &&
339 !(flags & UTF8_ALLOW_FFFF)) {
340 warning = UTF8_WARN_FFFF;
348 if (flags & UTF8_CHECK_ONLY) {
355 SV* sv = sv_2mortal(newSVpv("Malformed UTF-8 character ", 0));
358 case 0: /* Intentionally empty. */ break;
359 case UTF8_WARN_EMPTY:
360 Perl_sv_catpvf(aTHX_ sv, "(empty string)");
362 case UTF8_WARN_CONTINUATION:
363 Perl_sv_catpvf(aTHX_ sv, "(unexpected continuation byte 0x%02"UVxf")", uv);
365 case UTF8_WARN_NON_CONTINUATION:
366 Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf" after start byte 0x%02"UVxf")",
369 case UTF8_WARN_FE_FF:
370 Perl_sv_catpvf(aTHX_ sv, "(byte 0x%02"UVxf")", uv);
372 case UTF8_WARN_SHORT:
373 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d)",
374 curlen, curlen == 1 ? "" : "s", expectlen);
376 case UTF8_WARN_OVERFLOW:
377 Perl_sv_catpvf(aTHX_ sv, "(overflow at 0x%"UVxf", byte 0x%02x)",
380 case UTF8_WARN_SURROGATE:
381 Perl_sv_catpvf(aTHX_ sv, "(UTF-16 surrogate 0x%04"UVxf")", uv);
384 Perl_sv_catpvf(aTHX_ sv, "(byte order mark 0x%04"UVxf")", uv);
387 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d)",
388 expectlen, expectlen == 1 ? "": "s", UNISKIP(uv));
391 Perl_sv_catpvf(aTHX_ sv, "(character 0x%04"UVxf")", uv);
394 Perl_sv_catpvf(aTHX_ sv, "(unknown reason)");
402 Perl_warner(aTHX_ WARN_UTF8,
403 "%s in %s", s, PL_op_desc[PL_op->op_type]);
405 Perl_warner(aTHX_ WARN_UTF8, "%s", s);
410 *retlen = expectlen ? expectlen : len;
416 =for apidoc A|U8* s|utf8_to_uv_simple|STRLEN *retlen
418 Returns the character value of the first character in the string C<s>
419 which is assumed to be in UTF8 encoding; C<retlen> will be set to the
420 length, in bytes, of that character.
422 If C<s> does not point to a well-formed UTF8 character, zero is
423 returned and retlen is set, if possible, to -1.
429 Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
431 return Perl_utf8_to_uv(aTHX_ s, UTF8_MAXLEN, retlen, 0);
435 =for apidoc A|STRLEN|utf8_length|U8* s|U8 *e
437 Return the length of the UTF-8 char encoded string C<s> in characters.
438 Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end
439 up past C<e>, croaks.
445 Perl_utf8_length(pTHX_ U8* s, U8* e)
449 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
450 * the bitops (especially ~) can create illegal UTF-8.
451 * In other words: in Perl UTF-8 is not just for Unicode. */
454 Perl_croak(aTHX_ "panic: utf8_length: unexpected end");
459 Perl_croak(aTHX_ "panic: utf8_length: unaligned end");
468 =for apidoc A|IV|utf8_distance|U8 *a|U8 *b
470 Returns the number of UTF8 characters between the UTF-8 pointers C<a>
473 WARNING: use only if you *know* that the pointers point inside the
479 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
483 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
484 * the bitops (especially ~) can create illegal UTF-8.
485 * In other words: in Perl UTF-8 is not just for Unicode. */
492 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
502 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
512 =for apidoc A|U8*|utf8_hop|U8 *s|I32 off
514 Return the UTF-8 pointer C<s> displaced by C<off> characters, either
517 WARNING: do not use the following unless you *know* C<off> is within
518 the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned
519 on the first byte of character or just after the last byte of a character.
524 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
526 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
527 * the bitops (especially ~) can create illegal UTF-8.
528 * In other words: in Perl UTF-8 is not just for Unicode. */
537 while (UTF8_IS_CONTINUATION(*s))
545 =for apidoc A|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
547 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
548 Unlike C<bytes_to_utf8>, this over-writes the original string, and
549 updates len to contain the new length.
550 Returns zero on failure, setting C<len> to -1.
556 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
562 /* ensure valid UTF8 and chars < 256 before updating string */
563 for (send = s + *len; s < send; ) {
568 ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
577 *d++ = (U8)utf8_to_uv_simple(s, &ulen);
586 =for apidoc A|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
588 Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
589 Returns a pointer to the newly-created string, and sets C<len> to
590 reflect the new length.
596 Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
603 Newz(801, d, (*len) * 2 + 1, U8);
611 *d++ = (( uv >> 6) | 0xc0);
612 *d++ = (( uv & 0x3f) | 0x80);
621 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
623 * Destination must be pre-extended to 3/2 source. Do not use in-place.
624 * We optimize for native, for obvious reasons. */
627 Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
633 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
638 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
645 *d++ = (( uv >> 6) | 0xc0);
646 *d++ = (( uv & 0x3f) | 0x80);
649 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
651 if (low < 0xdc00 || low >= 0xdfff)
652 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
653 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
656 *d++ = (( uv >> 12) | 0xe0);
657 *d++ = (((uv >> 6) & 0x3f) | 0x80);
658 *d++ = (( uv & 0x3f) | 0x80);
662 *d++ = (( uv >> 18) | 0xf0);
663 *d++ = (((uv >> 12) & 0x3f) | 0x80);
664 *d++ = (((uv >> 6) & 0x3f) | 0x80);
665 *d++ = (( uv & 0x3f) | 0x80);
669 *newlen = d - dstart;
673 /* Note: this one is slightly destructive of the source. */
676 Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
679 U8* send = s + bytelen;
686 return utf16_to_utf8(p, d, bytelen, newlen);
689 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
692 Perl_is_uni_alnum(pTHX_ U32 c)
694 U8 tmpbuf[UTF8_MAXLEN+1];
695 uv_to_utf8(tmpbuf, (UV)c);
696 return is_utf8_alnum(tmpbuf);
700 Perl_is_uni_alnumc(pTHX_ U32 c)
702 U8 tmpbuf[UTF8_MAXLEN+1];
703 uv_to_utf8(tmpbuf, (UV)c);
704 return is_utf8_alnumc(tmpbuf);
708 Perl_is_uni_idfirst(pTHX_ U32 c)
710 U8 tmpbuf[UTF8_MAXLEN+1];
711 uv_to_utf8(tmpbuf, (UV)c);
712 return is_utf8_idfirst(tmpbuf);
716 Perl_is_uni_alpha(pTHX_ U32 c)
718 U8 tmpbuf[UTF8_MAXLEN+1];
719 uv_to_utf8(tmpbuf, (UV)c);
720 return is_utf8_alpha(tmpbuf);
724 Perl_is_uni_ascii(pTHX_ U32 c)
726 U8 tmpbuf[UTF8_MAXLEN+1];
727 uv_to_utf8(tmpbuf, (UV)c);
728 return is_utf8_ascii(tmpbuf);
732 Perl_is_uni_space(pTHX_ U32 c)
734 U8 tmpbuf[UTF8_MAXLEN+1];
735 uv_to_utf8(tmpbuf, (UV)c);
736 return is_utf8_space(tmpbuf);
740 Perl_is_uni_digit(pTHX_ U32 c)
742 U8 tmpbuf[UTF8_MAXLEN+1];
743 uv_to_utf8(tmpbuf, (UV)c);
744 return is_utf8_digit(tmpbuf);
748 Perl_is_uni_upper(pTHX_ U32 c)
750 U8 tmpbuf[UTF8_MAXLEN+1];
751 uv_to_utf8(tmpbuf, (UV)c);
752 return is_utf8_upper(tmpbuf);
756 Perl_is_uni_lower(pTHX_ U32 c)
758 U8 tmpbuf[UTF8_MAXLEN+1];
759 uv_to_utf8(tmpbuf, (UV)c);
760 return is_utf8_lower(tmpbuf);
764 Perl_is_uni_cntrl(pTHX_ U32 c)
766 U8 tmpbuf[UTF8_MAXLEN+1];
767 uv_to_utf8(tmpbuf, (UV)c);
768 return is_utf8_cntrl(tmpbuf);
772 Perl_is_uni_graph(pTHX_ U32 c)
774 U8 tmpbuf[UTF8_MAXLEN+1];
775 uv_to_utf8(tmpbuf, (UV)c);
776 return is_utf8_graph(tmpbuf);
780 Perl_is_uni_print(pTHX_ U32 c)
782 U8 tmpbuf[UTF8_MAXLEN+1];
783 uv_to_utf8(tmpbuf, (UV)c);
784 return is_utf8_print(tmpbuf);
788 Perl_is_uni_punct(pTHX_ U32 c)
790 U8 tmpbuf[UTF8_MAXLEN+1];
791 uv_to_utf8(tmpbuf, (UV)c);
792 return is_utf8_punct(tmpbuf);
796 Perl_is_uni_xdigit(pTHX_ U32 c)
798 U8 tmpbuf[UTF8_MAXLEN+1];
799 uv_to_utf8(tmpbuf, (UV)c);
800 return is_utf8_xdigit(tmpbuf);
804 Perl_to_uni_upper(pTHX_ U32 c)
806 U8 tmpbuf[UTF8_MAXLEN+1];
807 uv_to_utf8(tmpbuf, (UV)c);
808 return to_utf8_upper(tmpbuf);
812 Perl_to_uni_title(pTHX_ U32 c)
814 U8 tmpbuf[UTF8_MAXLEN+1];
815 uv_to_utf8(tmpbuf, (UV)c);
816 return to_utf8_title(tmpbuf);
820 Perl_to_uni_lower(pTHX_ U32 c)
822 U8 tmpbuf[UTF8_MAXLEN+1];
823 uv_to_utf8(tmpbuf, (UV)c);
824 return to_utf8_lower(tmpbuf);
827 /* for now these all assume no locale info available for Unicode > 255 */
830 Perl_is_uni_alnum_lc(pTHX_ U32 c)
832 return is_uni_alnum(c); /* XXX no locale support yet */
836 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
838 return is_uni_alnumc(c); /* XXX no locale support yet */
842 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
844 return is_uni_idfirst(c); /* XXX no locale support yet */
848 Perl_is_uni_alpha_lc(pTHX_ U32 c)
850 return is_uni_alpha(c); /* XXX no locale support yet */
854 Perl_is_uni_ascii_lc(pTHX_ U32 c)
856 return is_uni_ascii(c); /* XXX no locale support yet */
860 Perl_is_uni_space_lc(pTHX_ U32 c)
862 return is_uni_space(c); /* XXX no locale support yet */
866 Perl_is_uni_digit_lc(pTHX_ U32 c)
868 return is_uni_digit(c); /* XXX no locale support yet */
872 Perl_is_uni_upper_lc(pTHX_ U32 c)
874 return is_uni_upper(c); /* XXX no locale support yet */
878 Perl_is_uni_lower_lc(pTHX_ U32 c)
880 return is_uni_lower(c); /* XXX no locale support yet */
884 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
886 return is_uni_cntrl(c); /* XXX no locale support yet */
890 Perl_is_uni_graph_lc(pTHX_ U32 c)
892 return is_uni_graph(c); /* XXX no locale support yet */
896 Perl_is_uni_print_lc(pTHX_ U32 c)
898 return is_uni_print(c); /* XXX no locale support yet */
902 Perl_is_uni_punct_lc(pTHX_ U32 c)
904 return is_uni_punct(c); /* XXX no locale support yet */
908 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
910 return is_uni_xdigit(c); /* XXX no locale support yet */
914 Perl_to_uni_upper_lc(pTHX_ U32 c)
916 return to_uni_upper(c); /* XXX no locale support yet */
920 Perl_to_uni_title_lc(pTHX_ U32 c)
922 return to_uni_title(c); /* XXX no locale support yet */
926 Perl_to_uni_lower_lc(pTHX_ U32 c)
928 return to_uni_lower(c); /* XXX no locale support yet */
932 Perl_is_utf8_alnum(pTHX_ U8 *p)
934 if (!is_utf8_char(p))
937 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
938 * descendant of isalnum(3), in other words, it doesn't
939 * contain the '_'. --jhi */
940 PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
941 return swash_fetch(PL_utf8_alnum, p);
942 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
943 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
945 PL_utf8_alnum = swash_init("utf8", "",
946 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
947 return swash_fetch(PL_utf8_alnum, p);
952 Perl_is_utf8_alnumc(pTHX_ U8 *p)
954 if (!is_utf8_char(p))
957 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
958 return swash_fetch(PL_utf8_alnum, p);
959 /* return is_utf8_alpha(p) || is_utf8_digit(p); */
960 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
962 PL_utf8_alnum = swash_init("utf8", "",
963 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
964 return swash_fetch(PL_utf8_alnum, p);
969 Perl_is_utf8_idfirst(pTHX_ U8 *p)
971 return *p == '_' || is_utf8_alpha(p);
975 Perl_is_utf8_alpha(pTHX_ U8 *p)
977 if (!is_utf8_char(p))
980 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
981 return swash_fetch(PL_utf8_alpha, p);
985 Perl_is_utf8_ascii(pTHX_ U8 *p)
987 if (!is_utf8_char(p))
990 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
991 return swash_fetch(PL_utf8_ascii, p);
995 Perl_is_utf8_space(pTHX_ U8 *p)
997 if (!is_utf8_char(p))
1000 PL_utf8_space = swash_init("utf8", "IsSpacePerl", &PL_sv_undef, 0, 0);
1001 return swash_fetch(PL_utf8_space, p);
1005 Perl_is_utf8_digit(pTHX_ U8 *p)
1007 if (!is_utf8_char(p))
1010 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
1011 return swash_fetch(PL_utf8_digit, p);
1015 Perl_is_utf8_upper(pTHX_ U8 *p)
1017 if (!is_utf8_char(p))
1020 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
1021 return swash_fetch(PL_utf8_upper, p);
1025 Perl_is_utf8_lower(pTHX_ U8 *p)
1027 if (!is_utf8_char(p))
1030 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
1031 return swash_fetch(PL_utf8_lower, p);
1035 Perl_is_utf8_cntrl(pTHX_ U8 *p)
1037 if (!is_utf8_char(p))
1040 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
1041 return swash_fetch(PL_utf8_cntrl, p);
1045 Perl_is_utf8_graph(pTHX_ U8 *p)
1047 if (!is_utf8_char(p))
1050 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
1051 return swash_fetch(PL_utf8_graph, p);
1055 Perl_is_utf8_print(pTHX_ U8 *p)
1057 if (!is_utf8_char(p))
1060 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
1061 return swash_fetch(PL_utf8_print, p);
1065 Perl_is_utf8_punct(pTHX_ U8 *p)
1067 if (!is_utf8_char(p))
1070 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
1071 return swash_fetch(PL_utf8_punct, p);
1075 Perl_is_utf8_xdigit(pTHX_ U8 *p)
1077 if (!is_utf8_char(p))
1079 if (!PL_utf8_xdigit)
1080 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
1081 return swash_fetch(PL_utf8_xdigit, p);
1085 Perl_is_utf8_mark(pTHX_ U8 *p)
1087 if (!is_utf8_char(p))
1090 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
1091 return swash_fetch(PL_utf8_mark, p);
1095 Perl_to_utf8_upper(pTHX_ U8 *p)
1099 if (!PL_utf8_toupper)
1100 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
1101 uv = swash_fetch(PL_utf8_toupper, p);
1102 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1106 Perl_to_utf8_title(pTHX_ U8 *p)
1110 if (!PL_utf8_totitle)
1111 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
1112 uv = swash_fetch(PL_utf8_totitle, p);
1113 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1117 Perl_to_utf8_lower(pTHX_ U8 *p)
1121 if (!PL_utf8_tolower)
1122 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
1123 uv = swash_fetch(PL_utf8_tolower, p);
1124 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1127 /* a "swash" is a swatch hash */
1130 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
1133 SV* tokenbufsv = sv_2mortal(NEWSV(0,0));
1136 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
1138 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
1142 PUSHSTACKi(PERLSI_MAGIC);
1145 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
1146 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
1148 PUSHs(sv_2mortal(newSViv(minbits)));
1149 PUSHs(sv_2mortal(newSViv(none)));
1155 if (PL_curcop == &PL_compiling)
1156 /* XXX ought to be handled by lex_start */
1157 sv_setpv(tokenbufsv, PL_tokenbuf);
1158 if (call_method("SWASHNEW", G_SCALAR))
1159 retval = newSVsv(*PL_stack_sp--);
1161 retval = &PL_sv_undef;
1164 if (PL_curcop == &PL_compiling) {
1166 char* pv = SvPV(tokenbufsv, len);
1168 Copy(pv, PL_tokenbuf, len+1, char);
1169 PL_curcop->op_private = PL_hints;
1171 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
1172 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
1177 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
1179 HV* hv = (HV*)SvRV(sv);
1180 U32 klen = UTF8SKIP(ptr) - 1;
1181 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
1183 STRLEN needents = (klen ? 64 : 128);
1189 * This single-entry cache saves about 1/3 of the utf8 overhead in test
1190 * suite. (That is, only 7-8% overall over just a hash cache. Still,
1191 * it's nothing to sniff at.) Pity we usually come through at least
1192 * two function calls to get here...
1194 * NB: this code assumes that swatches are never modified, once generated!
1197 if (hv == PL_last_swash_hv &&
1198 klen == PL_last_swash_klen &&
1199 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
1201 tmps = PL_last_swash_tmps;
1202 slen = PL_last_swash_slen;
1205 /* Try our second-level swatch cache, kept in a hash. */
1206 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
1208 /* If not cached, generate it via utf8::SWASHGET */
1209 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
1214 PUSHSTACKi(PERLSI_MAGIC);
1218 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, UTF8_MAXLEN, 0, 0) & ~(needents - 1))));
1219 PUSHs(sv_2mortal(newSViv(needents)));
1221 if (call_method("SWASHGET", G_SCALAR))
1222 retval = newSVsv(*PL_stack_sp--);
1224 retval = &PL_sv_undef;
1228 if (PL_curcop == &PL_compiling)
1229 PL_curcop->op_private = PL_hints;
1231 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
1233 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
1234 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
1237 PL_last_swash_hv = hv;
1238 PL_last_swash_klen = klen;
1239 PL_last_swash_tmps = tmps;
1240 PL_last_swash_slen = slen;
1242 Copy(ptr, PL_last_swash_key, klen, U8);
1245 switch ((int)((slen << 3) / needents)) {
1247 bit = 1 << (off & 7);
1249 return (tmps[off] & bit) != 0;
1254 return (tmps[off] << 8) + tmps[off + 1] ;
1257 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
1259 Perl_croak(aTHX_ "panic: swash_fetch");