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 Perl_uv_to_utf8(pTHX_ U8 *d, UV uv) /* the d must be UTF8_MAXLEN+1 deep */
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)
114 if (UTF8_IS_ASCII(u))
117 if (!UTF8_IS_START(u))
122 if (len < 2 || !UTF8_IS_CONTINUATION(s[1]))
130 if (!UTF8_IS_CONTINUATION(*s))
132 uv = UTF8_ACCUMULATE(uv, *s);
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)
162 len = strlen((char *)s);
178 =for apidoc Am|U8* s|utf8_to_uv|STRLEN curlen|STRLEN *retlen|U32 flags
180 Returns the character value of the first character in the string C<s>
181 which is assumed to be in UTF8 encoding and no longer than C<curlen>;
182 C<retlen> will be set to the length, in bytes, of that character.
184 If C<s> does not point to a well-formed UTF8 character, the behaviour
185 is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
186 it is assumed that the caller will raise a warning, and this function
187 will silently just set C<retlen> to C<-1> and return zero. If the
188 C<flags> does not contain UTF8_CHECK_ONLY, warnings about
189 malformations will be given, C<retlen> will be set to the expected
190 length of the UTF-8 character in bytes, and zero will be returned.
192 The C<flags> can also contain various flags to allow deviations from
193 the strict UTF-8 encoding (see F<utf8.h>).
198 Perl_utf8_to_uv(pTHX_ U8* s, STRLEN curlen, STRLEN* retlen, U32 flags)
205 bool dowarn = ckWARN_d(WARN_UTF8);
207 STRLEN expectlen = 0;
210 /* This list is a superset of the UTF8_ALLOW_XXX. */
212 #define UTF8_WARN_EMPTY 1
213 #define UTF8_WARN_CONTINUATION 2
214 #define UTF8_WARN_NON_CONTINUATION 3
215 #define UTF8_WARN_FE_FF 4
216 #define UTF8_WARN_SHORT 5
217 #define UTF8_WARN_OVERFLOW 6
218 #define UTF8_WARN_SURROGATE 7
219 #define UTF8_WARN_BOM 8
220 #define UTF8_WARN_LONG 9
221 #define UTF8_WARN_FFFF 10
224 !(flags & UTF8_ALLOW_EMPTY)) {
225 warning = UTF8_WARN_EMPTY;
229 if (UTF8_IS_ASCII(uv)) {
235 if (UTF8_IS_CONTINUATION(uv) &&
236 !(flags & UTF8_ALLOW_CONTINUATION)) {
237 warning = UTF8_WARN_CONTINUATION;
241 if (UTF8_IS_START(uv) && curlen > 1 && !UTF8_IS_CONTINUATION(s[1]) &&
242 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
243 warning = UTF8_WARN_NON_CONTINUATION;
247 if ((uv == 0xfe || uv == 0xff) &&
248 !(flags & UTF8_ALLOW_FE_FF)) {
249 warning = UTF8_WARN_FE_FF;
253 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
254 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
255 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
256 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
257 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
258 else if (!(uv & 0x01)) { len = 7; uv = 0; }
259 else { len = 13; uv = 0; } /* whoa! */
266 if ((curlen < expectlen) &&
267 !(flags & UTF8_ALLOW_SHORT)) {
268 warning = UTF8_WARN_SHORT;
277 if (!UTF8_IS_CONTINUATION(*s) &&
278 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
280 warning = UTF8_WARN_NON_CONTINUATION;
284 uv = UTF8_ACCUMULATE(uv, *s);
286 /* These cannot be allowed. */
288 if (!(flags & UTF8_ALLOW_LONG)) {
289 warning = UTF8_WARN_LONG;
293 else { /* uv < ouv */
294 /* This cannot be allowed. */
295 warning = UTF8_WARN_OVERFLOW;
303 if (UNICODE_IS_SURROGATE(uv) &&
304 !(flags & UTF8_ALLOW_SURROGATE)) {
305 warning = UTF8_WARN_SURROGATE;
307 } else if (UNICODE_IS_BYTE_ORDER_MARK(uv) &&
308 !(flags & UTF8_ALLOW_BOM)) {
309 warning = UTF8_WARN_BOM;
311 } else if ((expectlen > UNISKIP(uv)) &&
312 !(flags & UTF8_ALLOW_LONG)) {
313 warning = UTF8_WARN_LONG;
315 } else if (UNICODE_IS_ILLEGAL(uv) &&
316 !(flags & UTF8_ALLOW_FFFF)) {
317 warning = UTF8_WARN_FFFF;
325 if (flags & UTF8_CHECK_ONLY) {
332 SV* sv = sv_2mortal(newSVpv("Malformed UTF-8 character ", 0));
335 case 0: /* Intentionally empty. */ break;
336 case UTF8_WARN_EMPTY:
337 Perl_sv_catpvf(aTHX_ sv, "(empty string)");
339 case UTF8_WARN_CONTINUATION:
340 Perl_sv_catpvf(aTHX_ sv, "(unexpected continuation byte 0x%02"UVxf")", uv);
342 case UTF8_WARN_NON_CONTINUATION:
343 Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf" after start byte 0x%02"UVxf")",
346 case UTF8_WARN_FE_FF:
347 Perl_sv_catpvf(aTHX_ sv, "(byte 0x%02"UVxf")", uv);
349 case UTF8_WARN_SHORT:
350 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d)",
351 curlen, curlen == 1 ? "" : "s", expectlen);
353 case UTF8_WARN_OVERFLOW:
354 Perl_sv_catpvf(aTHX_ sv, "(overflow at 0x%"UVxf", byte 0x%02x)",
357 case UTF8_WARN_SURROGATE:
358 Perl_sv_catpvf(aTHX_ sv, "(UTF-16 surrogate 0x%04"UVxf")", uv);
361 Perl_sv_catpvf(aTHX_ sv, "(byte order mark 0x%04"UVxf")", uv);
364 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d)",
365 expectlen, expectlen == 1 ? "": "s", UNISKIP(uv));
368 Perl_sv_catpvf(aTHX_ sv, "(character 0x%04"UVxf")", uv);
371 Perl_sv_catpvf(aTHX_ sv, "(unknown reason)");
379 Perl_warner(aTHX_ WARN_UTF8,
380 "%s in %s", s, PL_op_desc[PL_op->op_type]);
382 Perl_warner(aTHX_ WARN_UTF8, "%s", s);
387 *retlen = expectlen ? expectlen : len;
393 =for apidoc Am|U8* s|utf8_to_uv_simple|STRLEN *retlen
395 Returns the character value of the first character in the string C<s>
396 which is assumed to be in UTF8 encoding; C<retlen> will be set to the
397 length, in bytes, of that character.
399 If C<s> does not point to a well-formed UTF8 character, zero is
400 returned and retlen is set, if possible, to -1.
406 Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
408 return Perl_utf8_to_uv(aTHX_ s, UTF8_MAXLEN, retlen, 0);
412 =for apidoc Am|STRLEN|utf8_length|U8* s|U8 *e
414 Return the length of the UTF-8 char encoded string C<s> in characters.
415 Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end
416 up past C<e>, croaks.
422 Perl_utf8_length(pTHX_ U8* s, U8* e)
426 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
427 * the bitops (especially ~) can create illegal UTF-8.
428 * In other words: in Perl UTF-8 is not just for Unicode. */
431 Perl_croak(aTHX_ "panic: utf8_length: unexpected end");
436 Perl_croak(aTHX_ "panic: utf8_length: unaligned end");
445 =for apidoc Am|IV|utf8_distance|U8 *a|U8 *b
447 Returns the number of UTF8 characters between the UTF-8 pointers C<a>
450 WARNING: use only if you *know* that the pointers point inside the
456 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
460 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
461 * the bitops (especially ~) can create illegal UTF-8.
462 * In other words: in Perl UTF-8 is not just for Unicode. */
469 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
479 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
489 =for apidoc Am|U8*|utf8_hop|U8 *s|I32 off
491 Return the UTF-8 pointer C<s> displaced by C<off> characters, either
494 WARNING: do not use the following unless you *know* C<off> is within
495 the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned
496 on the first byte of character or just after the last byte of a character.
501 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
503 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
504 * the bitops (especially ~) can create illegal UTF-8.
505 * In other words: in Perl UTF-8 is not just for Unicode. */
514 while (UTF8_IS_CONTINUATION(*s))
522 =for apidoc Am|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
524 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
525 Unlike C<bytes_to_utf8>, this over-writes the original string, and
526 updates len to contain the new length.
527 Returns zero on failure, setting C<len> to -1.
533 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
539 /* ensure valid UTF8 and chars < 256 before updating string */
540 for (send = s + *len; s < send; ) {
545 ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
554 *d++ = (U8)utf8_to_uv_simple(s, &ulen);
563 =for apidoc Am|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
565 Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
566 Returns a pointer to the newly-created string, and sets C<len> to
567 reflect the new length.
573 Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
580 Newz(801, d, (*len) * 2 + 1, U8);
588 *d++ = (( uv >> 6) | 0xc0);
589 *d++ = (( uv & 0x3f) | 0x80);
598 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
600 * Destination must be pre-extended to 3/2 source. Do not use in-place.
601 * We optimize for native, for obvious reasons. */
604 Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
610 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
615 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
622 *d++ = (( uv >> 6) | 0xc0);
623 *d++ = (( uv & 0x3f) | 0x80);
626 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
628 if (low < 0xdc00 || low >= 0xdfff)
629 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
630 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
633 *d++ = (( uv >> 12) | 0xe0);
634 *d++ = (((uv >> 6) & 0x3f) | 0x80);
635 *d++ = (( uv & 0x3f) | 0x80);
639 *d++ = (( uv >> 18) | 0xf0);
640 *d++ = (((uv >> 12) & 0x3f) | 0x80);
641 *d++ = (((uv >> 6) & 0x3f) | 0x80);
642 *d++ = (( uv & 0x3f) | 0x80);
646 *newlen = d - dstart;
650 /* Note: this one is slightly destructive of the source. */
653 Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
656 U8* send = s + bytelen;
663 return utf16_to_utf8(p, d, bytelen, newlen);
666 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
669 Perl_is_uni_alnum(pTHX_ U32 c)
671 U8 tmpbuf[UTF8_MAXLEN+1];
672 uv_to_utf8(tmpbuf, (UV)c);
673 return is_utf8_alnum(tmpbuf);
677 Perl_is_uni_alnumc(pTHX_ U32 c)
679 U8 tmpbuf[UTF8_MAXLEN+1];
680 uv_to_utf8(tmpbuf, (UV)c);
681 return is_utf8_alnumc(tmpbuf);
685 Perl_is_uni_idfirst(pTHX_ U32 c)
687 U8 tmpbuf[UTF8_MAXLEN+1];
688 uv_to_utf8(tmpbuf, (UV)c);
689 return is_utf8_idfirst(tmpbuf);
693 Perl_is_uni_alpha(pTHX_ U32 c)
695 U8 tmpbuf[UTF8_MAXLEN+1];
696 uv_to_utf8(tmpbuf, (UV)c);
697 return is_utf8_alpha(tmpbuf);
701 Perl_is_uni_ascii(pTHX_ U32 c)
703 U8 tmpbuf[UTF8_MAXLEN+1];
704 uv_to_utf8(tmpbuf, (UV)c);
705 return is_utf8_ascii(tmpbuf);
709 Perl_is_uni_space(pTHX_ U32 c)
711 U8 tmpbuf[UTF8_MAXLEN+1];
712 uv_to_utf8(tmpbuf, (UV)c);
713 return is_utf8_space(tmpbuf);
717 Perl_is_uni_digit(pTHX_ U32 c)
719 U8 tmpbuf[UTF8_MAXLEN+1];
720 uv_to_utf8(tmpbuf, (UV)c);
721 return is_utf8_digit(tmpbuf);
725 Perl_is_uni_upper(pTHX_ U32 c)
727 U8 tmpbuf[UTF8_MAXLEN+1];
728 uv_to_utf8(tmpbuf, (UV)c);
729 return is_utf8_upper(tmpbuf);
733 Perl_is_uni_lower(pTHX_ U32 c)
735 U8 tmpbuf[UTF8_MAXLEN+1];
736 uv_to_utf8(tmpbuf, (UV)c);
737 return is_utf8_lower(tmpbuf);
741 Perl_is_uni_cntrl(pTHX_ U32 c)
743 U8 tmpbuf[UTF8_MAXLEN+1];
744 uv_to_utf8(tmpbuf, (UV)c);
745 return is_utf8_cntrl(tmpbuf);
749 Perl_is_uni_graph(pTHX_ U32 c)
751 U8 tmpbuf[UTF8_MAXLEN+1];
752 uv_to_utf8(tmpbuf, (UV)c);
753 return is_utf8_graph(tmpbuf);
757 Perl_is_uni_print(pTHX_ U32 c)
759 U8 tmpbuf[UTF8_MAXLEN+1];
760 uv_to_utf8(tmpbuf, (UV)c);
761 return is_utf8_print(tmpbuf);
765 Perl_is_uni_punct(pTHX_ U32 c)
767 U8 tmpbuf[UTF8_MAXLEN+1];
768 uv_to_utf8(tmpbuf, (UV)c);
769 return is_utf8_punct(tmpbuf);
773 Perl_is_uni_xdigit(pTHX_ U32 c)
775 U8 tmpbuf[UTF8_MAXLEN+1];
776 uv_to_utf8(tmpbuf, (UV)c);
777 return is_utf8_xdigit(tmpbuf);
781 Perl_to_uni_upper(pTHX_ U32 c)
783 U8 tmpbuf[UTF8_MAXLEN+1];
784 uv_to_utf8(tmpbuf, (UV)c);
785 return to_utf8_upper(tmpbuf);
789 Perl_to_uni_title(pTHX_ U32 c)
791 U8 tmpbuf[UTF8_MAXLEN+1];
792 uv_to_utf8(tmpbuf, (UV)c);
793 return to_utf8_title(tmpbuf);
797 Perl_to_uni_lower(pTHX_ U32 c)
799 U8 tmpbuf[UTF8_MAXLEN+1];
800 uv_to_utf8(tmpbuf, (UV)c);
801 return to_utf8_lower(tmpbuf);
804 /* for now these all assume no locale info available for Unicode > 255 */
807 Perl_is_uni_alnum_lc(pTHX_ U32 c)
809 return is_uni_alnum(c); /* XXX no locale support yet */
813 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
815 return is_uni_alnumc(c); /* XXX no locale support yet */
819 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
821 return is_uni_idfirst(c); /* XXX no locale support yet */
825 Perl_is_uni_alpha_lc(pTHX_ U32 c)
827 return is_uni_alpha(c); /* XXX no locale support yet */
831 Perl_is_uni_ascii_lc(pTHX_ U32 c)
833 return is_uni_ascii(c); /* XXX no locale support yet */
837 Perl_is_uni_space_lc(pTHX_ U32 c)
839 return is_uni_space(c); /* XXX no locale support yet */
843 Perl_is_uni_digit_lc(pTHX_ U32 c)
845 return is_uni_digit(c); /* XXX no locale support yet */
849 Perl_is_uni_upper_lc(pTHX_ U32 c)
851 return is_uni_upper(c); /* XXX no locale support yet */
855 Perl_is_uni_lower_lc(pTHX_ U32 c)
857 return is_uni_lower(c); /* XXX no locale support yet */
861 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
863 return is_uni_cntrl(c); /* XXX no locale support yet */
867 Perl_is_uni_graph_lc(pTHX_ U32 c)
869 return is_uni_graph(c); /* XXX no locale support yet */
873 Perl_is_uni_print_lc(pTHX_ U32 c)
875 return is_uni_print(c); /* XXX no locale support yet */
879 Perl_is_uni_punct_lc(pTHX_ U32 c)
881 return is_uni_punct(c); /* XXX no locale support yet */
885 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
887 return is_uni_xdigit(c); /* XXX no locale support yet */
891 Perl_to_uni_upper_lc(pTHX_ U32 c)
893 return to_uni_upper(c); /* XXX no locale support yet */
897 Perl_to_uni_title_lc(pTHX_ U32 c)
899 return to_uni_title(c); /* XXX no locale support yet */
903 Perl_to_uni_lower_lc(pTHX_ U32 c)
905 return to_uni_lower(c); /* XXX no locale support yet */
909 Perl_is_utf8_alnum(pTHX_ U8 *p)
911 if (!is_utf8_char(p))
914 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
915 * descendant of isalnum(3), in other words, it doesn't
916 * contain the '_'. --jhi */
917 PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
918 return swash_fetch(PL_utf8_alnum, p);
919 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
920 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
922 PL_utf8_alnum = swash_init("utf8", "",
923 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
924 return swash_fetch(PL_utf8_alnum, p);
929 Perl_is_utf8_alnumc(pTHX_ U8 *p)
931 if (!is_utf8_char(p))
934 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
935 return swash_fetch(PL_utf8_alnum, p);
936 /* return is_utf8_alpha(p) || is_utf8_digit(p); */
937 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
939 PL_utf8_alnum = swash_init("utf8", "",
940 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
941 return swash_fetch(PL_utf8_alnum, p);
946 Perl_is_utf8_idfirst(pTHX_ U8 *p)
948 return *p == '_' || is_utf8_alpha(p);
952 Perl_is_utf8_alpha(pTHX_ U8 *p)
954 if (!is_utf8_char(p))
957 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
958 return swash_fetch(PL_utf8_alpha, p);
962 Perl_is_utf8_ascii(pTHX_ U8 *p)
964 if (!is_utf8_char(p))
967 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
968 return swash_fetch(PL_utf8_ascii, p);
972 Perl_is_utf8_space(pTHX_ U8 *p)
974 if (!is_utf8_char(p))
977 PL_utf8_space = swash_init("utf8", "IsSpacePerl", &PL_sv_undef, 0, 0);
978 return swash_fetch(PL_utf8_space, p);
982 Perl_is_utf8_digit(pTHX_ U8 *p)
984 if (!is_utf8_char(p))
987 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
988 return swash_fetch(PL_utf8_digit, p);
992 Perl_is_utf8_upper(pTHX_ U8 *p)
994 if (!is_utf8_char(p))
997 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
998 return swash_fetch(PL_utf8_upper, p);
1002 Perl_is_utf8_lower(pTHX_ U8 *p)
1004 if (!is_utf8_char(p))
1007 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
1008 return swash_fetch(PL_utf8_lower, p);
1012 Perl_is_utf8_cntrl(pTHX_ U8 *p)
1014 if (!is_utf8_char(p))
1017 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
1018 return swash_fetch(PL_utf8_cntrl, p);
1022 Perl_is_utf8_graph(pTHX_ U8 *p)
1024 if (!is_utf8_char(p))
1027 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
1028 return swash_fetch(PL_utf8_graph, p);
1032 Perl_is_utf8_print(pTHX_ U8 *p)
1034 if (!is_utf8_char(p))
1037 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
1038 return swash_fetch(PL_utf8_print, p);
1042 Perl_is_utf8_punct(pTHX_ U8 *p)
1044 if (!is_utf8_char(p))
1047 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
1048 return swash_fetch(PL_utf8_punct, p);
1052 Perl_is_utf8_xdigit(pTHX_ U8 *p)
1054 if (!is_utf8_char(p))
1056 if (!PL_utf8_xdigit)
1057 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
1058 return swash_fetch(PL_utf8_xdigit, p);
1062 Perl_is_utf8_mark(pTHX_ U8 *p)
1064 if (!is_utf8_char(p))
1067 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
1068 return swash_fetch(PL_utf8_mark, p);
1072 Perl_to_utf8_upper(pTHX_ U8 *p)
1076 if (!PL_utf8_toupper)
1077 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
1078 uv = swash_fetch(PL_utf8_toupper, p);
1079 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1083 Perl_to_utf8_title(pTHX_ U8 *p)
1087 if (!PL_utf8_totitle)
1088 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
1089 uv = swash_fetch(PL_utf8_totitle, p);
1090 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1094 Perl_to_utf8_lower(pTHX_ U8 *p)
1098 if (!PL_utf8_tolower)
1099 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
1100 uv = swash_fetch(PL_utf8_tolower, p);
1101 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1104 /* a "swash" is a swatch hash */
1107 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
1110 SV* tokenbufsv = sv_2mortal(NEWSV(0,0));
1113 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
1115 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
1119 PUSHSTACKi(PERLSI_MAGIC);
1122 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
1123 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
1125 PUSHs(sv_2mortal(newSViv(minbits)));
1126 PUSHs(sv_2mortal(newSViv(none)));
1132 if (PL_curcop == &PL_compiling)
1133 /* XXX ought to be handled by lex_start */
1134 sv_setpv(tokenbufsv, PL_tokenbuf);
1135 if (call_method("SWASHNEW", G_SCALAR))
1136 retval = newSVsv(*PL_stack_sp--);
1138 retval = &PL_sv_undef;
1141 if (PL_curcop == &PL_compiling) {
1143 char* pv = SvPV(tokenbufsv, len);
1145 Copy(pv, PL_tokenbuf, len+1, char);
1146 PL_curcop->op_private = PL_hints;
1148 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
1149 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
1154 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
1156 HV* hv = (HV*)SvRV(sv);
1157 U32 klen = UTF8SKIP(ptr) - 1;
1158 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
1160 STRLEN needents = (klen ? 64 : 128);
1166 * This single-entry cache saves about 1/3 of the utf8 overhead in test
1167 * suite. (That is, only 7-8% overall over just a hash cache. Still,
1168 * it's nothing to sniff at.) Pity we usually come through at least
1169 * two function calls to get here...
1171 * NB: this code assumes that swatches are never modified, once generated!
1174 if (hv == PL_last_swash_hv &&
1175 klen == PL_last_swash_klen &&
1176 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
1178 tmps = PL_last_swash_tmps;
1179 slen = PL_last_swash_slen;
1182 /* Try our second-level swatch cache, kept in a hash. */
1183 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
1185 /* If not cached, generate it via utf8::SWASHGET */
1186 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
1191 PUSHSTACKi(PERLSI_MAGIC);
1195 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, UTF8_MAXLEN, 0, 0) & ~(needents - 1))));
1196 PUSHs(sv_2mortal(newSViv(needents)));
1198 if (call_method("SWASHGET", G_SCALAR))
1199 retval = newSVsv(*PL_stack_sp--);
1201 retval = &PL_sv_undef;
1205 if (PL_curcop == &PL_compiling)
1206 PL_curcop->op_private = PL_hints;
1208 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
1210 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
1211 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
1214 PL_last_swash_hv = hv;
1215 PL_last_swash_klen = klen;
1216 PL_last_swash_tmps = tmps;
1217 PL_last_swash_slen = slen;
1219 Copy(ptr, PL_last_swash_key, klen, U8);
1222 switch ((int)((slen << 3) / needents)) {
1224 bit = 1 << (off & 7);
1226 return (tmps[off] & bit) != 0;
1231 return (tmps[off] << 8) + tmps[off + 1] ;
1234 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
1236 Perl_croak(aTHX_ "panic: swash_fetch");