3 * Copyright (c) 1998-1999, 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.
29 uv_to_utf8(U8 *d, UV uv)
36 *d++ = (( uv >> 6) | 0xc0);
37 *d++ = (( uv & 0x3f) | 0x80);
41 *d++ = (( uv >> 12) | 0xe0);
42 *d++ = (((uv >> 6) & 0x3f) | 0x80);
43 *d++ = (( uv & 0x3f) | 0x80);
47 *d++ = (( uv >> 18) | 0xf0);
48 *d++ = (((uv >> 12) & 0x3f) | 0x80);
49 *d++ = (((uv >> 6) & 0x3f) | 0x80);
50 *d++ = (( uv & 0x3f) | 0x80);
54 *d++ = (( uv >> 24) | 0xf8);
55 *d++ = (((uv >> 18) & 0x3f) | 0x80);
56 *d++ = (((uv >> 12) & 0x3f) | 0x80);
57 *d++ = (((uv >> 6) & 0x3f) | 0x80);
58 *d++ = (( uv & 0x3f) | 0x80);
61 if (uv < 0x80000000) {
62 *d++ = (( uv >> 30) | 0xfc);
63 *d++ = (((uv >> 24) & 0x3f) | 0x80);
64 *d++ = (((uv >> 18) & 0x3f) | 0x80);
65 *d++ = (((uv >> 12) & 0x3f) | 0x80);
66 *d++ = (((uv >> 6) & 0x3f) | 0x80);
67 *d++ = (( uv & 0x3f) | 0x80);
71 if (uv < 0x2000000000)
74 *d++ = 0xfe; /* Can't match U+FEFF! */
75 *d++ = (((uv >> 30) & 0x3f) | 0x80);
76 *d++ = (((uv >> 24) & 0x3f) | 0x80);
77 *d++ = (((uv >> 18) & 0x3f) | 0x80);
78 *d++ = (((uv >> 12) & 0x3f) | 0x80);
79 *d++ = (((uv >> 6) & 0x3f) | 0x80);
80 *d++ = (( uv & 0x3f) | 0x80);
85 *d++ = 0xff; /* Can't match U+FFFE! */
86 *d++ = (((uv >> 36) & 0x3f) | 0x80);
87 *d++ = (((uv >> 30) & 0x3f) | 0x80);
88 *d++ = (((uv >> 24) & 0x3f) | 0x80);
89 *d++ = (((uv >> 18) & 0x3f) | 0x80);
90 *d++ = (((uv >> 12) & 0x3f) | 0x80);
91 *d++ = (((uv >> 6) & 0x3f) | 0x80);
92 *d++ = (( uv & 0x3f) | 0x80);
99 utf8_to_uv(U8* s, I32* retlen)
109 warn("Malformed UTF-8 character");
115 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
116 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
117 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
118 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
119 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
120 else if (!(uv & 0x01)) { len = 7; uv &= 0x00; }
121 else len = 8; /* whoa! */
128 if ((*s & 0xc0) != 0x80) {
129 warn("Malformed UTF-8 character");
135 uv = (uv << 6) | (*s++ & 0x3f);
140 /* utf8_distance(a,b) is intended to be a - b in pointer arithmetic */
143 utf8_distance(U8 *a, U8 *b)
161 /* WARNING: do not use the following unless you *know* off is within bounds */
164 utf8_hop(U8 *s, I32 off)
174 while ((*s & 0xc0) == 0x80)
182 /* XXX NOTHING CALLS THE FOLLOWING TWO ROUTINES YET!!! */
184 * Convert native or reversed UTF-16 to UTF-8.
186 * Destination must be pre-extended to 3/2 source. Do not use in-place.
187 * We optimize for native, for obvious reasons. */
190 utf16_to_utf8(U16* p, U8* d, I32 bytelen)
192 U16* pend = p + bytelen / 2;
200 *d++ = (( uv >> 6) | 0xc0);
201 *d++ = (( uv & 0x3f) | 0x80);
204 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
206 if (low < 0xdc00 || low >= 0xdfff) {
207 warn("Malformed UTF-16 surrogate");
211 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
214 *d++ = (( uv >> 12) | 0xe0);
215 *d++ = (((uv >> 6) & 0x3f) | 0x80);
216 *d++ = (( uv & 0x3f) | 0x80);
220 *d++ = (( uv >> 18) | 0xf0);
221 *d++ = (((uv >> 12) & 0x3f) | 0x80);
222 *d++ = (((uv >> 6) & 0x3f) | 0x80);
223 *d++ = (( uv & 0x3f) | 0x80);
230 /* Note: this one is slightly destructive of the source. */
233 utf16_to_utf8_reversed(U16* p, U8* d, I32 bytelen)
236 U8* send = s + bytelen;
243 return utf16_to_utf8(p, d, bytelen);
246 /* for now these are all defined (inefficiently) in terms of the utf8 versions */
252 uv_to_utf8(tmpbuf, (UV)c);
253 return is_utf8_alnum(tmpbuf);
257 is_uni_idfirst(U32 c)
260 uv_to_utf8(tmpbuf, (UV)c);
261 return is_utf8_idfirst(tmpbuf);
268 uv_to_utf8(tmpbuf, (UV)c);
269 return is_utf8_alpha(tmpbuf);
276 uv_to_utf8(tmpbuf, (UV)c);
277 return is_utf8_space(tmpbuf);
284 uv_to_utf8(tmpbuf, (UV)c);
285 return is_utf8_digit(tmpbuf);
292 uv_to_utf8(tmpbuf, (UV)c);
293 return is_utf8_upper(tmpbuf);
300 uv_to_utf8(tmpbuf, (UV)c);
301 return is_utf8_lower(tmpbuf);
308 uv_to_utf8(tmpbuf, (UV)c);
309 return is_utf8_print(tmpbuf);
316 uv_to_utf8(tmpbuf, (UV)c);
317 return to_utf8_upper(tmpbuf);
324 uv_to_utf8(tmpbuf, (UV)c);
325 return to_utf8_title(tmpbuf);
332 uv_to_utf8(tmpbuf, (UV)c);
333 return to_utf8_lower(tmpbuf);
336 /* for now these all assume no locale info available for Unicode > 255 */
339 is_uni_alnum_lc(U32 c)
341 return is_uni_alnum(c); /* XXX no locale support yet */
345 is_uni_idfirst_lc(U32 c)
347 return is_uni_idfirst(c); /* XXX no locale support yet */
351 is_uni_alpha_lc(U32 c)
353 return is_uni_alpha(c); /* XXX no locale support yet */
357 is_uni_space_lc(U32 c)
359 return is_uni_space(c); /* XXX no locale support yet */
363 is_uni_digit_lc(U32 c)
365 return is_uni_digit(c); /* XXX no locale support yet */
369 is_uni_upper_lc(U32 c)
371 return is_uni_upper(c); /* XXX no locale support yet */
375 is_uni_lower_lc(U32 c)
377 return is_uni_lower(c); /* XXX no locale support yet */
381 is_uni_print_lc(U32 c)
383 return is_uni_print(c); /* XXX no locale support yet */
387 to_uni_upper_lc(U32 c)
389 return to_uni_upper(c); /* XXX no locale support yet */
393 to_uni_title_lc(U32 c)
395 return to_uni_title(c); /* XXX no locale support yet */
399 to_uni_lower_lc(U32 c)
401 return to_uni_lower(c); /* XXX no locale support yet */
409 PL_utf8_alnum = swash_init("utf8", "IsAlnum", &PL_sv_undef, 0, 0);
410 return swash_fetch(PL_utf8_alnum, p);
411 /* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
412 #ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
414 PL_utf8_alnum = swash_init("utf8", "",
415 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
416 return swash_fetch(PL_utf8_alnum, p);
421 is_utf8_idfirst(U8 *p)
423 return *p == '_' || is_utf8_alpha(p);
430 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
431 return swash_fetch(PL_utf8_alpha, p);
438 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
439 return swash_fetch(PL_utf8_space, p);
446 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
447 return swash_fetch(PL_utf8_digit, p);
454 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
455 return swash_fetch(PL_utf8_upper, p);
462 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
463 return swash_fetch(PL_utf8_lower, p);
470 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
471 return swash_fetch(PL_utf8_print, p);
478 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
479 return swash_fetch(PL_utf8_mark, p);
487 if (!PL_utf8_toupper)
488 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
489 uv = swash_fetch(PL_utf8_toupper, p);
490 return uv ? uv : utf8_to_uv(p,0);
498 if (!PL_utf8_totitle)
499 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
500 uv = swash_fetch(PL_utf8_totitle, p);
501 return uv ? uv : utf8_to_uv(p,0);
509 if (!PL_utf8_tolower)
510 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
511 uv = swash_fetch(PL_utf8_tolower, p);
512 return uv ? uv : utf8_to_uv(p,0);
515 /* a "swash" is a swatch hash */
518 swash_init(char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
523 PUSHSTACKi(PERLSI_MAGIC);
526 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
527 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
529 PUSHs(sv_2mortal(newSViv(minbits)));
530 PUSHs(sv_2mortal(newSViv(none)));
536 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
537 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
538 if (perl_call_method("SWASHNEW", G_SCALAR))
539 retval = newSVsv(*PL_stack_sp--);
541 retval = &PL_sv_undef;
544 if (PL_curcop == &PL_compiling) {
545 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
546 PL_curcop->op_private = PL_hints;
548 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
549 croak("SWASHNEW didn't return an HV ref");
554 swash_fetch(SV *sv, U8 *ptr)
556 HV* hv = (HV*)SvRV(sv);
557 U32 klen = UTF8SKIP(ptr) - 1;
558 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
560 STRLEN needents = (klen ? 64 : 128);
566 * This single-entry cache saves about 1/3 of the utf8 overhead in test
567 * suite. (That is, only 7-8% overall over just a hash cache. Still,
568 * it's nothing to sniff at.) Pity we usually come through at least
569 * two function calls to get here...
571 * NB: this code assumes that swatches are never modified, once generated!
574 if (hv == PL_last_swash_hv &&
575 klen == PL_last_swash_klen &&
576 (!klen || memEQ(ptr,PL_last_swash_key,klen)) )
578 tmps = PL_last_swash_tmps;
579 slen = PL_last_swash_slen;
582 /* Try our second-level swatch cache, kept in a hash. */
583 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
585 /* If not cached, generate it via utf8::SWASHGET */
586 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
591 PUSHSTACKi(PERLSI_MAGIC);
595 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, 0) & ~(needents - 1))));
596 PUSHs(sv_2mortal(newSViv(needents)));
598 if (perl_call_method("SWASHGET", G_SCALAR))
599 retval = newSVsv(*PL_stack_sp--);
601 retval = &PL_sv_undef;
605 if (PL_curcop == &PL_compiling)
606 PL_curcop->op_private = PL_hints;
608 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
610 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
611 croak("SWASHGET didn't return result of proper length");
614 PL_last_swash_hv = hv;
615 PL_last_swash_klen = klen;
616 PL_last_swash_tmps = tmps;
617 PL_last_swash_slen = slen;
619 Copy(ptr, PL_last_swash_key, klen, U8);
622 switch ((slen << 3) / needents) {
624 bit = 1 << (off & 7);
626 return (tmps[off] & bit) != 0;
631 return (tmps[off] << 8) + tmps[off + 1] ;
634 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
636 croak("panic: swash_fetch");