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
125 character. Note that an ASCII character is a valid UTF-8 character.
126 The actual number of bytes in the UTF-8 character will be returned if
127 it 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 a valid UTF8
172 string, false otherwise. Note that 'a valid UTF8 string' does not mean
173 'a string that contains UTF8' because a valid ASCII string is a valid
180 Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
187 len = strlen((char *)s);
203 =for apidoc A|U8* s|utf8_to_uv|STRLEN curlen|STRLEN *retlen|U32 flags
205 Returns the character value of the first character in the string C<s>
206 which is assumed to be in UTF8 encoding and no longer than C<curlen>;
207 C<retlen> will be set to the length, in bytes, of that character.
209 If C<s> does not point to a well-formed UTF8 character, the behaviour
210 is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
211 it is assumed that the caller will raise a warning, and this function
212 will silently just set C<retlen> to C<-1> and return zero. If the
213 C<flags> does not contain UTF8_CHECK_ONLY, warnings about
214 malformations will be given, C<retlen> will be set to the expected
215 length of the UTF-8 character in bytes, and zero will be returned.
217 The C<flags> can also contain various flags to allow deviations from
218 the strict UTF-8 encoding (see F<utf8.h>).
223 Perl_utf8_to_uv(pTHX_ U8* s, STRLEN curlen, STRLEN* retlen, U32 flags)
230 bool dowarn = ckWARN_d(WARN_UTF8);
232 STRLEN expectlen = 0;
235 /* This list is a superset of the UTF8_ALLOW_XXX. */
237 #define UTF8_WARN_EMPTY 1
238 #define UTF8_WARN_CONTINUATION 2
239 #define UTF8_WARN_NON_CONTINUATION 3
240 #define UTF8_WARN_FE_FF 4
241 #define UTF8_WARN_SHORT 5
242 #define UTF8_WARN_OVERFLOW 6
243 #define UTF8_WARN_SURROGATE 7
244 #define UTF8_WARN_BOM 8
245 #define UTF8_WARN_LONG 9
246 #define UTF8_WARN_FFFF 10
249 !(flags & UTF8_ALLOW_EMPTY)) {
250 warning = UTF8_WARN_EMPTY;
254 if (UTF8_IS_ASCII(uv)) {
260 if (UTF8_IS_CONTINUATION(uv) &&
261 !(flags & UTF8_ALLOW_CONTINUATION)) {
262 warning = UTF8_WARN_CONTINUATION;
266 if (UTF8_IS_START(uv) && curlen > 1 && !UTF8_IS_CONTINUATION(s[1]) &&
267 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
268 warning = UTF8_WARN_NON_CONTINUATION;
272 if ((uv == 0xfe || uv == 0xff) &&
273 !(flags & UTF8_ALLOW_FE_FF)) {
274 warning = UTF8_WARN_FE_FF;
278 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
279 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
280 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
281 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
282 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
283 else if (!(uv & 0x01)) { len = 7; uv = 0; }
284 else { len = 13; uv = 0; } /* whoa! */
291 if ((curlen < expectlen) &&
292 !(flags & UTF8_ALLOW_SHORT)) {
293 warning = UTF8_WARN_SHORT;
302 if (!UTF8_IS_CONTINUATION(*s) &&
303 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
305 warning = UTF8_WARN_NON_CONTINUATION;
309 uv = UTF8_ACCUMULATE(uv, *s);
311 /* These cannot be allowed. */
313 if (!(flags & UTF8_ALLOW_LONG)) {
314 warning = UTF8_WARN_LONG;
318 else { /* uv < ouv */
319 /* This cannot be allowed. */
320 warning = UTF8_WARN_OVERFLOW;
328 if (UNICODE_IS_SURROGATE(uv) &&
329 !(flags & UTF8_ALLOW_SURROGATE)) {
330 warning = UTF8_WARN_SURROGATE;
332 } else if (UNICODE_IS_BYTE_ORDER_MARK(uv) &&
333 !(flags & UTF8_ALLOW_BOM)) {
334 warning = UTF8_WARN_BOM;
336 } else if ((expectlen > UNISKIP(uv)) &&
337 !(flags & UTF8_ALLOW_LONG)) {
338 warning = UTF8_WARN_LONG;
340 } else if (UNICODE_IS_ILLEGAL(uv) &&
341 !(flags & UTF8_ALLOW_FFFF)) {
342 warning = UTF8_WARN_FFFF;
350 if (flags & UTF8_CHECK_ONLY) {
357 SV* sv = sv_2mortal(newSVpv("Malformed UTF-8 character ", 0));
360 case 0: /* Intentionally empty. */ break;
361 case UTF8_WARN_EMPTY:
362 Perl_sv_catpvf(aTHX_ sv, "(empty string)");
364 case UTF8_WARN_CONTINUATION:
365 Perl_sv_catpvf(aTHX_ sv, "(unexpected continuation byte 0x%02"UVxf")", uv);
367 case UTF8_WARN_NON_CONTINUATION:
368 Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf" after start byte 0x%02"UVxf")",
371 case UTF8_WARN_FE_FF:
372 Perl_sv_catpvf(aTHX_ sv, "(byte 0x%02"UVxf")", uv);
374 case UTF8_WARN_SHORT:
375 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d)",
376 curlen, curlen == 1 ? "" : "s", expectlen);
378 case UTF8_WARN_OVERFLOW:
379 Perl_sv_catpvf(aTHX_ sv, "(overflow at 0x%"UVxf", byte 0x%02x)",
382 case UTF8_WARN_SURROGATE:
383 Perl_sv_catpvf(aTHX_ sv, "(UTF-16 surrogate 0x%04"UVxf")", uv);
386 Perl_sv_catpvf(aTHX_ sv, "(byte order mark 0x%04"UVxf")", uv);
389 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d)",
390 expectlen, expectlen == 1 ? "": "s", UNISKIP(uv));
393 Perl_sv_catpvf(aTHX_ sv, "(character 0x%04"UVxf")", uv);
396 Perl_sv_catpvf(aTHX_ sv, "(unknown reason)");
404 Perl_warner(aTHX_ WARN_UTF8,
405 "%s in %s", s, PL_op_desc[PL_op->op_type]);
407 Perl_warner(aTHX_ WARN_UTF8, "%s", s);
412 *retlen = expectlen ? expectlen : len;
418 =for apidoc A|U8* s|utf8_to_uv_simple|STRLEN *retlen
420 Returns the character value of the first character in the string C<s>
421 which is assumed to be in UTF8 encoding; C<retlen> will be set to the
422 length, in bytes, of that character.
424 If C<s> does not point to a well-formed UTF8 character, zero is
425 returned and retlen is set, if possible, to -1.
431 Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
433 return Perl_utf8_to_uv(aTHX_ s, UTF8_MAXLEN, retlen, 0);
437 =for apidoc A|STRLEN|utf8_length|U8* s|U8 *e
439 Return the length of the UTF-8 char encoded string C<s> in characters.
440 Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end
441 up past C<e>, croaks.
447 Perl_utf8_length(pTHX_ U8* s, U8* e)
451 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
452 * the bitops (especially ~) can create illegal UTF-8.
453 * In other words: in Perl UTF-8 is not just for Unicode. */
456 Perl_croak(aTHX_ "panic: utf8_length: unexpected end");
461 Perl_croak(aTHX_ "panic: utf8_length: unaligned end");
470 =for apidoc A|IV|utf8_distance|U8 *a|U8 *b
472 Returns the number of UTF8 characters between the UTF-8 pointers C<a>
475 WARNING: use only if you *know* that the pointers point inside the
481 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
485 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
486 * the bitops (especially ~) can create illegal UTF-8.
487 * In other words: in Perl UTF-8 is not just for Unicode. */
494 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
504 Perl_croak(aTHX_ "panic: utf8_distance: unaligned end");
514 =for apidoc A|U8*|utf8_hop|U8 *s|I32 off
516 Return the UTF-8 pointer C<s> displaced by C<off> characters, either
519 WARNING: do not use the following unless you *know* C<off> is within
520 the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned
521 on the first byte of character or just after the last byte of a character.
526 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
528 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
529 * the bitops (especially ~) can create illegal UTF-8.
530 * In other words: in Perl UTF-8 is not just for Unicode. */
539 while (UTF8_IS_CONTINUATION(*s))
547 =for apidoc A|U8 *|utf8_to_bytes|U8 *s|STRLEN *len
549 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
550 Unlike C<bytes_to_utf8>, this over-writes the original string, and
551 updates len to contain the new length.
552 Returns zero on failure, setting C<len> to -1.
558 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
564 /* ensure valid UTF8 and chars < 256 before updating string */
565 for (send = s + *len; s < send; ) {
570 ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
579 *d++ = (U8)utf8_to_uv_simple(s, &ulen);
588 =for apidoc A|U8 *|bytes_from_utf8|U8 *s|STRLEN *len|bool *is_utf8
590 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
591 Unlike <utf8_to_bytes> but like C<bytes_to_utf8>, returns a pointer to
592 the newly-created string, and updates C<len> to contain the new
593 length. Returns the original string if no conversion occurs, C<len>
594 is unchanged. Do nothing if C<is_utf8> points to 0. Sets C<is_utf8> to
595 0 if C<s> is converted or contains all 7bit characters.
600 Perl_bytes_from_utf8(pTHX_ U8* s, STRLEN *len, bool *is_utf8)
610 /* ensure valid UTF8 and chars < 256 before converting string */
611 for (send = s + *len; s < send;) {
613 if (!UTF8_IS_ASCII(c)) {
614 if (UTF8_IS_CONTINUATION(c) || s >= send ||
615 !UTF8_IS_CONTINUATION(*s) || UTF8_IS_DOWNGRADEABLE_START(c))
626 Newz(801, d, (*len) - count + 1, U8);
627 s = start; start = d;
631 if (UTF8_IS_ASCII(c))
634 *d++ = UTF8_ACCUMULATE(c, *s++);
642 =for apidoc A|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
644 Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
645 Returns a pointer to the newly-created string, and sets C<len> to
646 reflect the new length.
652 Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
659 Newz(801, d, (*len) * 2 + 1, U8);
663 if (UTF8_IS_ASCII(*s))
668 *d++ = UTF8_EIGHT_BIT_HI(uv);
669 *d++ = UTF8_EIGHT_BIT_LO(uv);
678 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
680 * Destination must be pre-extended to 3/2 source. Do not use in-place.
681 * We optimize for native, for obvious reasons. */
684 Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
690 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
695 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
702 *d++ = (( uv >> 6) | 0xc0);
703 *d++ = (( uv & 0x3f) | 0x80);
706 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
708 if (low < 0xdc00 || low >= 0xdfff)
709 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
710 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
713 *d++ = (( uv >> 12) | 0xe0);
714 *d++ = (((uv >> 6) & 0x3f) | 0x80);
715 *d++ = (( uv & 0x3f) | 0x80);
719 *d++ = (( uv >> 18) | 0xf0);
720 *d++ = (((uv >> 12) & 0x3f) | 0x80);
721 *d++ = (((uv >> 6) & 0x3f) | 0x80);
722 *d++ = (( uv & 0x3f) | 0x80);
726 *newlen = d - dstart;
730 /* Note: this one is slightly destructive of the source. */
733 Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
736 U8* send = s + bytelen;
743 return utf16_to_utf8(p, d, bytelen, newlen);
746 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
749 Perl_is_uni_alnum(pTHX_ U32 c)
751 U8 tmpbuf[UTF8_MAXLEN+1];
752 uv_to_utf8(tmpbuf, (UV)c);
753 return is_utf8_alnum(tmpbuf);
757 Perl_is_uni_alnumc(pTHX_ U32 c)
759 U8 tmpbuf[UTF8_MAXLEN+1];
760 uv_to_utf8(tmpbuf, (UV)c);
761 return is_utf8_alnumc(tmpbuf);
765 Perl_is_uni_idfirst(pTHX_ U32 c)
767 U8 tmpbuf[UTF8_MAXLEN+1];
768 uv_to_utf8(tmpbuf, (UV)c);
769 return is_utf8_idfirst(tmpbuf);
773 Perl_is_uni_alpha(pTHX_ U32 c)
775 U8 tmpbuf[UTF8_MAXLEN+1];
776 uv_to_utf8(tmpbuf, (UV)c);
777 return is_utf8_alpha(tmpbuf);
781 Perl_is_uni_ascii(pTHX_ U32 c)
783 U8 tmpbuf[UTF8_MAXLEN+1];
784 uv_to_utf8(tmpbuf, (UV)c);
785 return is_utf8_ascii(tmpbuf);
789 Perl_is_uni_space(pTHX_ U32 c)
791 U8 tmpbuf[UTF8_MAXLEN+1];
792 uv_to_utf8(tmpbuf, (UV)c);
793 return is_utf8_space(tmpbuf);
797 Perl_is_uni_digit(pTHX_ U32 c)
799 U8 tmpbuf[UTF8_MAXLEN+1];
800 uv_to_utf8(tmpbuf, (UV)c);
801 return is_utf8_digit(tmpbuf);
805 Perl_is_uni_upper(pTHX_ U32 c)
807 U8 tmpbuf[UTF8_MAXLEN+1];
808 uv_to_utf8(tmpbuf, (UV)c);
809 return is_utf8_upper(tmpbuf);
813 Perl_is_uni_lower(pTHX_ U32 c)
815 U8 tmpbuf[UTF8_MAXLEN+1];
816 uv_to_utf8(tmpbuf, (UV)c);
817 return is_utf8_lower(tmpbuf);
821 Perl_is_uni_cntrl(pTHX_ U32 c)
823 U8 tmpbuf[UTF8_MAXLEN+1];
824 uv_to_utf8(tmpbuf, (UV)c);
825 return is_utf8_cntrl(tmpbuf);
829 Perl_is_uni_graph(pTHX_ U32 c)
831 U8 tmpbuf[UTF8_MAXLEN+1];
832 uv_to_utf8(tmpbuf, (UV)c);
833 return is_utf8_graph(tmpbuf);
837 Perl_is_uni_print(pTHX_ U32 c)
839 U8 tmpbuf[UTF8_MAXLEN+1];
840 uv_to_utf8(tmpbuf, (UV)c);
841 return is_utf8_print(tmpbuf);
845 Perl_is_uni_punct(pTHX_ U32 c)
847 U8 tmpbuf[UTF8_MAXLEN+1];
848 uv_to_utf8(tmpbuf, (UV)c);
849 return is_utf8_punct(tmpbuf);
853 Perl_is_uni_xdigit(pTHX_ U32 c)
855 U8 tmpbuf[UTF8_MAXLEN+1];
856 uv_to_utf8(tmpbuf, (UV)c);
857 return is_utf8_xdigit(tmpbuf);
861 Perl_to_uni_upper(pTHX_ U32 c)
863 U8 tmpbuf[UTF8_MAXLEN+1];
864 uv_to_utf8(tmpbuf, (UV)c);
865 return to_utf8_upper(tmpbuf);
869 Perl_to_uni_title(pTHX_ U32 c)
871 U8 tmpbuf[UTF8_MAXLEN+1];
872 uv_to_utf8(tmpbuf, (UV)c);
873 return to_utf8_title(tmpbuf);
877 Perl_to_uni_lower(pTHX_ U32 c)
879 U8 tmpbuf[UTF8_MAXLEN+1];
880 uv_to_utf8(tmpbuf, (UV)c);
881 return to_utf8_lower(tmpbuf);
884 /* for now these all assume no locale info available for Unicode > 255 */
887 Perl_is_uni_alnum_lc(pTHX_ U32 c)
889 return is_uni_alnum(c); /* XXX no locale support yet */
893 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
895 return is_uni_alnumc(c); /* XXX no locale support yet */
899 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
901 return is_uni_idfirst(c); /* XXX no locale support yet */
905 Perl_is_uni_alpha_lc(pTHX_ U32 c)
907 return is_uni_alpha(c); /* XXX no locale support yet */
911 Perl_is_uni_ascii_lc(pTHX_ U32 c)
913 return is_uni_ascii(c); /* XXX no locale support yet */
917 Perl_is_uni_space_lc(pTHX_ U32 c)
919 return is_uni_space(c); /* XXX no locale support yet */
923 Perl_is_uni_digit_lc(pTHX_ U32 c)
925 return is_uni_digit(c); /* XXX no locale support yet */
929 Perl_is_uni_upper_lc(pTHX_ U32 c)
931 return is_uni_upper(c); /* XXX no locale support yet */
935 Perl_is_uni_lower_lc(pTHX_ U32 c)
937 return is_uni_lower(c); /* XXX no locale support yet */
941 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
943 return is_uni_cntrl(c); /* XXX no locale support yet */
947 Perl_is_uni_graph_lc(pTHX_ U32 c)
949 return is_uni_graph(c); /* XXX no locale support yet */
953 Perl_is_uni_print_lc(pTHX_ U32 c)
955 return is_uni_print(c); /* XXX no locale support yet */
959 Perl_is_uni_punct_lc(pTHX_ U32 c)
961 return is_uni_punct(c); /* XXX no locale support yet */
965 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
967 return is_uni_xdigit(c); /* XXX no locale support yet */
971 Perl_to_uni_upper_lc(pTHX_ U32 c)
973 return to_uni_upper(c); /* XXX no locale support yet */
977 Perl_to_uni_title_lc(pTHX_ U32 c)
979 return to_uni_title(c); /* XXX no locale support yet */
983 Perl_to_uni_lower_lc(pTHX_ U32 c)
985 return to_uni_lower(c); /* XXX no locale support yet */
989 Perl_is_utf8_alnum(pTHX_ U8 *p)
991 if (!is_utf8_char(p))
994 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
995 * descendant of isalnum(3), in other words, it doesn't
996 * contain the '_'. --jhi */
997 PL_utf8_alnum = swash_init("utf8", "IsWord", &PL_sv_undef, 0, 0);
998 return swash_fetch(PL_utf8_alnum, p);
999 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
1000 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
1002 PL_utf8_alnum = swash_init("utf8", "",
1003 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
1004 return swash_fetch(PL_utf8_alnum, p);
1009 Perl_is_utf8_alnumc(pTHX_ U8 *p)
1011 if (!is_utf8_char(p))
1014 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
1015 return swash_fetch(PL_utf8_alnum, p);
1016 /* return is_utf8_alpha(p) || is_utf8_digit(p); */
1017 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
1019 PL_utf8_alnum = swash_init("utf8", "",
1020 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
1021 return swash_fetch(PL_utf8_alnum, p);
1026 Perl_is_utf8_idfirst(pTHX_ U8 *p)
1028 return *p == '_' || is_utf8_alpha(p);
1032 Perl_is_utf8_alpha(pTHX_ U8 *p)
1034 if (!is_utf8_char(p))
1037 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
1038 return swash_fetch(PL_utf8_alpha, p);
1042 Perl_is_utf8_ascii(pTHX_ U8 *p)
1044 if (!is_utf8_char(p))
1047 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
1048 return swash_fetch(PL_utf8_ascii, p);
1052 Perl_is_utf8_space(pTHX_ U8 *p)
1054 if (!is_utf8_char(p))
1057 PL_utf8_space = swash_init("utf8", "IsSpacePerl", &PL_sv_undef, 0, 0);
1058 return swash_fetch(PL_utf8_space, p);
1062 Perl_is_utf8_digit(pTHX_ U8 *p)
1064 if (!is_utf8_char(p))
1067 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
1068 return swash_fetch(PL_utf8_digit, p);
1072 Perl_is_utf8_upper(pTHX_ U8 *p)
1074 if (!is_utf8_char(p))
1077 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
1078 return swash_fetch(PL_utf8_upper, p);
1082 Perl_is_utf8_lower(pTHX_ U8 *p)
1084 if (!is_utf8_char(p))
1087 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
1088 return swash_fetch(PL_utf8_lower, p);
1092 Perl_is_utf8_cntrl(pTHX_ U8 *p)
1094 if (!is_utf8_char(p))
1097 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
1098 return swash_fetch(PL_utf8_cntrl, p);
1102 Perl_is_utf8_graph(pTHX_ U8 *p)
1104 if (!is_utf8_char(p))
1107 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
1108 return swash_fetch(PL_utf8_graph, p);
1112 Perl_is_utf8_print(pTHX_ U8 *p)
1114 if (!is_utf8_char(p))
1117 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
1118 return swash_fetch(PL_utf8_print, p);
1122 Perl_is_utf8_punct(pTHX_ U8 *p)
1124 if (!is_utf8_char(p))
1127 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
1128 return swash_fetch(PL_utf8_punct, p);
1132 Perl_is_utf8_xdigit(pTHX_ U8 *p)
1134 if (!is_utf8_char(p))
1136 if (!PL_utf8_xdigit)
1137 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
1138 return swash_fetch(PL_utf8_xdigit, p);
1142 Perl_is_utf8_mark(pTHX_ U8 *p)
1144 if (!is_utf8_char(p))
1147 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
1148 return swash_fetch(PL_utf8_mark, p);
1152 Perl_to_utf8_upper(pTHX_ U8 *p)
1156 if (!PL_utf8_toupper)
1157 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
1158 uv = swash_fetch(PL_utf8_toupper, p);
1159 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1163 Perl_to_utf8_title(pTHX_ U8 *p)
1167 if (!PL_utf8_totitle)
1168 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
1169 uv = swash_fetch(PL_utf8_totitle, p);
1170 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1174 Perl_to_utf8_lower(pTHX_ U8 *p)
1178 if (!PL_utf8_tolower)
1179 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
1180 uv = swash_fetch(PL_utf8_tolower, p);
1181 return uv ? uv : utf8_to_uv(p,UTF8_MAXLEN,0,0);
1184 /* a "swash" is a swatch hash */
1187 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
1190 SV* tokenbufsv = sv_2mortal(NEWSV(0,0));
1193 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
1195 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
1199 PUSHSTACKi(PERLSI_MAGIC);
1202 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
1203 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
1205 PUSHs(sv_2mortal(newSViv(minbits)));
1206 PUSHs(sv_2mortal(newSViv(none)));
1212 if (PL_curcop == &PL_compiling)
1213 /* XXX ought to be handled by lex_start */
1214 sv_setpv(tokenbufsv, PL_tokenbuf);
1215 if (call_method("SWASHNEW", G_SCALAR))
1216 retval = newSVsv(*PL_stack_sp--);
1218 retval = &PL_sv_undef;
1221 if (PL_curcop == &PL_compiling) {
1223 char* pv = SvPV(tokenbufsv, len);
1225 Copy(pv, PL_tokenbuf, len+1, char);
1226 PL_curcop->op_private = PL_hints;
1228 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
1229 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
1234 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
1236 HV* hv = (HV*)SvRV(sv);
1237 U32 klen = UTF8SKIP(ptr) - 1;
1238 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
1240 STRLEN needents = (klen ? 64 : 128);
1246 * This single-entry cache saves about 1/3 of the utf8 overhead in test
1247 * suite. (That is, only 7-8% overall over just a hash cache. Still,
1248 * it's nothing to sniff at.) Pity we usually come through at least
1249 * two function calls to get here...
1251 * NB: this code assumes that swatches are never modified, once generated!
1254 if (hv == PL_last_swash_hv &&
1255 klen == PL_last_swash_klen &&
1256 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
1258 tmps = PL_last_swash_tmps;
1259 slen = PL_last_swash_slen;
1262 /* Try our second-level swatch cache, kept in a hash. */
1263 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
1265 /* If not cached, generate it via utf8::SWASHGET */
1266 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
1271 PUSHSTACKi(PERLSI_MAGIC);
1275 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, UTF8_MAXLEN, 0, 0) & ~(needents - 1))));
1276 PUSHs(sv_2mortal(newSViv(needents)));
1278 if (call_method("SWASHGET", G_SCALAR))
1279 retval = newSVsv(*PL_stack_sp--);
1281 retval = &PL_sv_undef;
1285 if (PL_curcop == &PL_compiling)
1286 PL_curcop->op_private = PL_hints;
1288 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
1290 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
1291 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
1294 PL_last_swash_hv = hv;
1295 PL_last_swash_klen = klen;
1296 PL_last_swash_tmps = tmps;
1297 PL_last_swash_slen = slen;
1299 Copy(ptr, PL_last_swash_key, klen, U8);
1302 switch ((int)((slen << 3) / needents)) {
1304 bit = 1 << (off & 7);
1306 return (tmps[off] & bit) != 0;
1311 return (tmps[off] << 8) + tmps[off + 1] ;
1314 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
1316 Perl_croak(aTHX_ "panic: swash_fetch");