3 * Copyright (c) 1998-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * 'What a fix!' said Sam. 'That's the one place in all the lands we've ever
12 * heard of that we don't want to see any closer; and that's the one place
13 * we're trying to get to! And that's just where we can't get, nohow.'
15 * 'Well do I understand your speech,' he answered in the same language;
16 * 'yet few strangers do so. Why then do you not speak in the Common Tongue,
17 * as is the custom in the West, if you wish to be answered?'
19 * ...the travellers perceived that the floor was paved with stones of many
20 * hues; branching runes and strange devices intertwined beneath their feet.
24 #define PERL_IN_UTF8_C
30 Perl_uv_to_utf8(pTHX_ U8 *d, UV uv)
37 *d++ = (( uv >> 6) | 0xc0);
38 *d++ = (( uv & 0x3f) | 0x80);
42 *d++ = (( uv >> 12) | 0xe0);
43 *d++ = (((uv >> 6) & 0x3f) | 0x80);
44 *d++ = (( uv & 0x3f) | 0x80);
48 *d++ = (( uv >> 18) | 0xf0);
49 *d++ = (((uv >> 12) & 0x3f) | 0x80);
50 *d++ = (((uv >> 6) & 0x3f) | 0x80);
51 *d++ = (( uv & 0x3f) | 0x80);
55 *d++ = (( uv >> 24) | 0xf8);
56 *d++ = (((uv >> 18) & 0x3f) | 0x80);
57 *d++ = (((uv >> 12) & 0x3f) | 0x80);
58 *d++ = (((uv >> 6) & 0x3f) | 0x80);
59 *d++ = (( uv & 0x3f) | 0x80);
62 if (uv < 0x80000000) {
63 *d++ = (( uv >> 30) | 0xfc);
64 *d++ = (((uv >> 24) & 0x3f) | 0x80);
65 *d++ = (((uv >> 18) & 0x3f) | 0x80);
66 *d++ = (((uv >> 12) & 0x3f) | 0x80);
67 *d++ = (((uv >> 6) & 0x3f) | 0x80);
68 *d++ = (( uv & 0x3f) | 0x80);
72 if (uv < 0x1000000000LL)
75 *d++ = 0xfe; /* Can't match U+FEFF! */
76 *d++ = (((uv >> 30) & 0x3f) | 0x80);
77 *d++ = (((uv >> 24) & 0x3f) | 0x80);
78 *d++ = (((uv >> 18) & 0x3f) | 0x80);
79 *d++ = (((uv >> 12) & 0x3f) | 0x80);
80 *d++ = (((uv >> 6) & 0x3f) | 0x80);
81 *d++ = (( uv & 0x3f) | 0x80);
86 *d++ = 0xff; /* Can't match U+FFFE! */
87 *d++ = 0x80; /* 6 Reserved bits */
88 *d++ = (((uv >> 60) & 0x0f) | 0x80); /* 2 Reserved bits */
89 *d++ = (((uv >> 54) & 0x3f) | 0x80);
90 *d++ = (((uv >> 48) & 0x3f) | 0x80);
91 *d++ = (((uv >> 42) & 0x3f) | 0x80);
92 *d++ = (((uv >> 36) & 0x3f) | 0x80);
93 *d++ = (((uv >> 30) & 0x3f) | 0x80);
94 *d++ = (((uv >> 24) & 0x3f) | 0x80);
95 *d++ = (((uv >> 18) & 0x3f) | 0x80);
96 *d++ = (((uv >> 12) & 0x3f) | 0x80);
97 *d++ = (((uv >> 6) & 0x3f) | 0x80);
98 *d++ = (( uv & 0x3f) | 0x80);
105 Perl_utf8_to_uv(pTHX_ U8* s, I32* retlen)
116 if (ckWARN_d(WARN_UTF8))
117 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
123 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
124 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
125 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
126 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
127 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
128 else if (!(uv & 0x01)) { len = 7; uv = 0; }
129 else { len = 13; uv = 0; } /* whoa! */
136 if ((*s & 0xc0) != 0x80) {
138 if (ckWARN_d(WARN_UTF8))
139 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
145 uv = (uv << 6) | (*s++ & 0x3f);
150 /* utf8_distance(a,b) is intended to be a - b in pointer arithmetic */
153 Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
171 /* WARNING: do not use the following unless you *know* off is within bounds */
174 Perl_utf8_hop(pTHX_ U8 *s, I32 off)
184 while ((*s & 0xc0) == 0x80)
192 /* XXX NOTHING CALLS THE FOLLOWING TWO ROUTINES YET!!! */
194 * Convert native or reversed UTF-16 to UTF-8.
196 * Destination must be pre-extended to 3/2 source. Do not use in-place.
197 * We optimize for native, for obvious reasons. */
200 Perl_utf16_to_utf8(pTHX_ U16* p, U8* d, I32 bytelen)
202 U16* pend = p + bytelen / 2;
210 *d++ = (( uv >> 6) | 0xc0);
211 *d++ = (( uv & 0x3f) | 0x80);
214 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
217 if (low < 0xdc00 || low >= 0xdfff) {
218 if (ckWARN_d(WARN_UTF8))
219 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-16 surrogate");
223 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
226 *d++ = (( uv >> 12) | 0xe0);
227 *d++ = (((uv >> 6) & 0x3f) | 0x80);
228 *d++ = (( uv & 0x3f) | 0x80);
232 *d++ = (( uv >> 18) | 0xf0);
233 *d++ = (((uv >> 12) & 0x3f) | 0x80);
234 *d++ = (((uv >> 6) & 0x3f) | 0x80);
235 *d++ = (( uv & 0x3f) | 0x80);
242 /* Note: this one is slightly destructive of the source. */
245 Perl_utf16_to_utf8_reversed(pTHX_ U16* p, U8* d, I32 bytelen)
248 U8* send = s + bytelen;
255 return utf16_to_utf8(p, d, bytelen);
258 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
261 Perl_is_uni_alnum(pTHX_ U32 c)
263 U8 tmpbuf[UTF8_MAXLEN];
264 uv_to_utf8(tmpbuf, (UV)c);
265 return is_utf8_alnum(tmpbuf);
269 Perl_is_uni_alnumc(pTHX_ U32 c)
271 U8 tmpbuf[UTF8_MAXLEN];
272 uv_to_utf8(tmpbuf, (UV)c);
273 return is_utf8_alnumc(tmpbuf);
277 Perl_is_uni_idfirst(pTHX_ U32 c)
279 U8 tmpbuf[UTF8_MAXLEN];
280 uv_to_utf8(tmpbuf, (UV)c);
281 return is_utf8_idfirst(tmpbuf);
285 Perl_is_uni_alpha(pTHX_ U32 c)
287 U8 tmpbuf[UTF8_MAXLEN];
288 uv_to_utf8(tmpbuf, (UV)c);
289 return is_utf8_alpha(tmpbuf);
293 Perl_is_uni_ascii(pTHX_ U32 c)
295 U8 tmpbuf[UTF8_MAXLEN];
296 uv_to_utf8(tmpbuf, (UV)c);
297 return is_utf8_ascii(tmpbuf);
301 Perl_is_uni_space(pTHX_ U32 c)
303 U8 tmpbuf[UTF8_MAXLEN];
304 uv_to_utf8(tmpbuf, (UV)c);
305 return is_utf8_space(tmpbuf);
309 Perl_is_uni_digit(pTHX_ U32 c)
311 U8 tmpbuf[UTF8_MAXLEN];
312 uv_to_utf8(tmpbuf, (UV)c);
313 return is_utf8_digit(tmpbuf);
317 Perl_is_uni_upper(pTHX_ U32 c)
319 U8 tmpbuf[UTF8_MAXLEN];
320 uv_to_utf8(tmpbuf, (UV)c);
321 return is_utf8_upper(tmpbuf);
325 Perl_is_uni_lower(pTHX_ U32 c)
327 U8 tmpbuf[UTF8_MAXLEN];
328 uv_to_utf8(tmpbuf, (UV)c);
329 return is_utf8_lower(tmpbuf);
333 Perl_is_uni_cntrl(pTHX_ U32 c)
335 U8 tmpbuf[UTF8_MAXLEN];
336 uv_to_utf8(tmpbuf, (UV)c);
337 return is_utf8_cntrl(tmpbuf);
341 Perl_is_uni_graph(pTHX_ U32 c)
343 U8 tmpbuf[UTF8_MAXLEN];
344 uv_to_utf8(tmpbuf, (UV)c);
345 return is_utf8_graph(tmpbuf);
349 Perl_is_uni_print(pTHX_ U32 c)
351 U8 tmpbuf[UTF8_MAXLEN];
352 uv_to_utf8(tmpbuf, (UV)c);
353 return is_utf8_print(tmpbuf);
357 Perl_is_uni_punct(pTHX_ U32 c)
359 U8 tmpbuf[UTF8_MAXLEN];
360 uv_to_utf8(tmpbuf, (UV)c);
361 return is_utf8_punct(tmpbuf);
365 Perl_is_uni_xdigit(pTHX_ U32 c)
367 U8 tmpbuf[UTF8_MAXLEN];
368 uv_to_utf8(tmpbuf, (UV)c);
369 return is_utf8_xdigit(tmpbuf);
373 Perl_to_uni_upper(pTHX_ U32 c)
375 U8 tmpbuf[UTF8_MAXLEN];
376 uv_to_utf8(tmpbuf, (UV)c);
377 return to_utf8_upper(tmpbuf);
381 Perl_to_uni_title(pTHX_ U32 c)
383 U8 tmpbuf[UTF8_MAXLEN];
384 uv_to_utf8(tmpbuf, (UV)c);
385 return to_utf8_title(tmpbuf);
389 Perl_to_uni_lower(pTHX_ U32 c)
391 U8 tmpbuf[UTF8_MAXLEN];
392 uv_to_utf8(tmpbuf, (UV)c);
393 return to_utf8_lower(tmpbuf);
396 /* for now these all assume no locale info available for Unicode > 255 */
399 Perl_is_uni_alnum_lc(pTHX_ U32 c)
401 return is_uni_alnum(c); /* XXX no locale support yet */
405 Perl_is_uni_alnumc_lc(pTHX_ U32 c)
407 return is_uni_alnumc(c); /* XXX no locale support yet */
411 Perl_is_uni_idfirst_lc(pTHX_ U32 c)
413 return is_uni_idfirst(c); /* XXX no locale support yet */
417 Perl_is_uni_alpha_lc(pTHX_ U32 c)
419 return is_uni_alpha(c); /* XXX no locale support yet */
423 Perl_is_uni_ascii_lc(pTHX_ U32 c)
425 return is_uni_ascii(c); /* XXX no locale support yet */
429 Perl_is_uni_space_lc(pTHX_ U32 c)
431 return is_uni_space(c); /* XXX no locale support yet */
435 Perl_is_uni_digit_lc(pTHX_ U32 c)
437 return is_uni_digit(c); /* XXX no locale support yet */
441 Perl_is_uni_upper_lc(pTHX_ U32 c)
443 return is_uni_upper(c); /* XXX no locale support yet */
447 Perl_is_uni_lower_lc(pTHX_ U32 c)
449 return is_uni_lower(c); /* XXX no locale support yet */
453 Perl_is_uni_cntrl_lc(pTHX_ U32 c)
455 return is_uni_cntrl(c); /* XXX no locale support yet */
459 Perl_is_uni_graph_lc(pTHX_ U32 c)
461 return is_uni_graph(c); /* XXX no locale support yet */
465 Perl_is_uni_print_lc(pTHX_ U32 c)
467 return is_uni_print(c); /* XXX no locale support yet */
471 Perl_is_uni_punct_lc(pTHX_ U32 c)
473 return is_uni_punct(c); /* XXX no locale support yet */
477 Perl_is_uni_xdigit_lc(pTHX_ U32 c)
479 return is_uni_xdigit(c); /* XXX no locale support yet */
483 Perl_to_uni_upper_lc(pTHX_ U32 c)
485 return to_uni_upper(c); /* XXX no locale support yet */
489 Perl_to_uni_title_lc(pTHX_ U32 c)
491 return to_uni_title(c); /* XXX no locale support yet */
495 Perl_to_uni_lower_lc(pTHX_ U32 c)
497 return to_uni_lower(c); /* XXX no locale support yet */
501 Perl_is_utf8_alnum(pTHX_ U8 *p)
504 PL_utf8_alnum = swash_init("utf8", "IsAlnum", &PL_sv_undef, 0, 0);
505 return swash_fetch(PL_utf8_alnum, p);
506 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
507 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
509 PL_utf8_alnum = swash_init("utf8", "",
510 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
511 return swash_fetch(PL_utf8_alnum, p);
516 Perl_is_utf8_alnumc(pTHX_ U8 *p)
519 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
520 return swash_fetch(PL_utf8_alnum, p);
521 /* return is_utf8_alpha(p) || is_utf8_digit(p); */
522 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
524 PL_utf8_alnum = swash_init("utf8", "",
525 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
526 return swash_fetch(PL_utf8_alnum, p);
531 Perl_is_utf8_idfirst(pTHX_ U8 *p)
533 return *p == '_' || is_utf8_alpha(p);
537 Perl_is_utf8_alpha(pTHX_ U8 *p)
540 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
541 return swash_fetch(PL_utf8_alpha, p);
545 Perl_is_utf8_ascii(pTHX_ U8 *p)
548 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
549 return swash_fetch(PL_utf8_ascii, p);
553 Perl_is_utf8_space(pTHX_ U8 *p)
556 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
557 return swash_fetch(PL_utf8_space, p);
561 Perl_is_utf8_digit(pTHX_ U8 *p)
564 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
565 return swash_fetch(PL_utf8_digit, p);
569 Perl_is_utf8_upper(pTHX_ U8 *p)
572 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
573 return swash_fetch(PL_utf8_upper, p);
577 Perl_is_utf8_lower(pTHX_ U8 *p)
580 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
581 return swash_fetch(PL_utf8_lower, p);
585 Perl_is_utf8_cntrl(pTHX_ U8 *p)
588 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
589 return swash_fetch(PL_utf8_cntrl, p);
593 Perl_is_utf8_graph(pTHX_ U8 *p)
596 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
597 return swash_fetch(PL_utf8_graph, p);
601 Perl_is_utf8_print(pTHX_ U8 *p)
604 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
605 return swash_fetch(PL_utf8_print, p);
609 Perl_is_utf8_punct(pTHX_ U8 *p)
612 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
613 return swash_fetch(PL_utf8_punct, p);
617 Perl_is_utf8_xdigit(pTHX_ U8 *p)
620 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
621 return swash_fetch(PL_utf8_xdigit, p);
625 Perl_is_utf8_mark(pTHX_ U8 *p)
628 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
629 return swash_fetch(PL_utf8_mark, p);
633 Perl_to_utf8_upper(pTHX_ U8 *p)
637 if (!PL_utf8_toupper)
638 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
639 uv = swash_fetch(PL_utf8_toupper, p);
640 return uv ? uv : utf8_to_uv(p,0);
644 Perl_to_utf8_title(pTHX_ U8 *p)
648 if (!PL_utf8_totitle)
649 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
650 uv = swash_fetch(PL_utf8_totitle, p);
651 return uv ? uv : utf8_to_uv(p,0);
655 Perl_to_utf8_lower(pTHX_ U8 *p)
659 if (!PL_utf8_tolower)
660 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
661 uv = swash_fetch(PL_utf8_tolower, p);
662 return uv ? uv : utf8_to_uv(p,0);
665 /* a "swash" is a swatch hash */
668 Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
673 PUSHSTACKi(PERLSI_MAGIC);
676 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
677 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
679 PUSHs(sv_2mortal(newSViv(minbits)));
680 PUSHs(sv_2mortal(newSViv(none)));
686 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
687 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
688 if (call_method("SWASHNEW", G_SCALAR))
689 retval = newSVsv(*PL_stack_sp--);
691 retval = &PL_sv_undef;
694 if (PL_curcop == &PL_compiling) {
695 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
696 PL_curcop->op_private = PL_hints;
698 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
699 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
704 Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
706 HV* hv = (HV*)SvRV(sv);
707 U32 klen = UTF8SKIP(ptr) - 1;
708 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
710 STRLEN needents = (klen ? 64 : 128);
716 * This single-entry cache saves about 1/3 of the utf8 overhead in test
717 * suite. (That is, only 7-8% overall over just a hash cache. Still,
718 * it's nothing to sniff at.) Pity we usually come through at least
719 * two function calls to get here...
721 * NB: this code assumes that swatches are never modified, once generated!
724 if (hv == PL_last_swash_hv &&
725 klen == PL_last_swash_klen &&
726 (!klen || memEQ(ptr,PL_last_swash_key,klen)) )
728 tmps = PL_last_swash_tmps;
729 slen = PL_last_swash_slen;
732 /* Try our second-level swatch cache, kept in a hash. */
733 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
735 /* If not cached, generate it via utf8::SWASHGET */
736 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
741 PUSHSTACKi(PERLSI_MAGIC);
745 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, 0) & ~(needents - 1))));
746 PUSHs(sv_2mortal(newSViv(needents)));
748 if (call_method("SWASHGET", G_SCALAR))
749 retval = newSVsv(*PL_stack_sp--);
751 retval = &PL_sv_undef;
755 if (PL_curcop == &PL_compiling)
756 PL_curcop->op_private = PL_hints;
758 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
760 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
761 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
764 PL_last_swash_hv = hv;
765 PL_last_swash_klen = klen;
766 PL_last_swash_tmps = tmps;
767 PL_last_swash_slen = slen;
769 Copy(ptr, PL_last_swash_key, klen, U8);
772 switch ((slen << 3) / needents) {
774 bit = 1 << (off & 7);
776 return (tmps[off] & bit) != 0;
781 return (tmps[off] << 8) + tmps[off + 1] ;
784 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
786 Perl_croak(aTHX_ "panic: swash_fetch");