This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fixes for logical bugs in the lexwarn patch; other tweaks to avoid
[perl5.git] / utf8.c
CommitLineData
a0ed51b3
LW
1/* utf8.c
2 *
4eb8286e 3 * Copyright (c) 1998-1999, Larry Wall
a0ed51b3
LW
4 *
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.
7 *
8 */
9
10/*
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.'
14 *
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?'
18 *
19 * ...the travellers perceived that the floor was paved with stones of many
20 * hues; branching runes and strange devices intertwined beneath their feet.
21 */
22
23#include "EXTERN.h"
864dbfa3 24#define PERL_IN_UTF8_C
a0ed51b3
LW
25#include "perl.h"
26
27/* Unicode support */
28
dfe13c55 29U8 *
864dbfa3 30Perl_uv_to_utf8(pTHX_ U8 *d, UV uv)
a0ed51b3
LW
31{
32 if (uv < 0x80) {
33 *d++ = uv;
34 return d;
35 }
36 if (uv < 0x800) {
37 *d++ = (( uv >> 6) | 0xc0);
38 *d++ = (( uv & 0x3f) | 0x80);
39 return d;
40 }
41 if (uv < 0x10000) {
42 *d++ = (( uv >> 12) | 0xe0);
43 *d++ = (((uv >> 6) & 0x3f) | 0x80);
44 *d++ = (( uv & 0x3f) | 0x80);
45 return d;
46 }
47 if (uv < 0x200000) {
48 *d++ = (( uv >> 18) | 0xf0);
49 *d++ = (((uv >> 12) & 0x3f) | 0x80);
50 *d++ = (((uv >> 6) & 0x3f) | 0x80);
51 *d++ = (( uv & 0x3f) | 0x80);
52 return d;
53 }
54 if (uv < 0x4000000) {
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);
60 return d;
61 }
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);
69 return d;
70 }
71#ifdef Quad_t
72 if (uv < 0x2000000000)
73#endif
74 {
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);
82 return d;
83 }
84#ifdef Quad_t
85 {
86 *d++ = 0xff; /* Can't match U+FFFE! */
87 *d++ = (((uv >> 36) & 0x3f) | 0x80);
88 *d++ = (((uv >> 30) & 0x3f) | 0x80);
89 *d++ = (((uv >> 24) & 0x3f) | 0x80);
90 *d++ = (((uv >> 18) & 0x3f) | 0x80);
91 *d++ = (((uv >> 12) & 0x3f) | 0x80);
92 *d++ = (((uv >> 6) & 0x3f) | 0x80);
93 *d++ = (( uv & 0x3f) | 0x80);
94 return d;
95 }
96#endif
97}
98
99UV
864dbfa3 100Perl_utf8_to_uv(pTHX_ U8* s, I32* retlen)
a0ed51b3
LW
101{
102 UV uv = *s;
103 int len;
104 if (!(uv & 0x80)) {
105 if (retlen)
106 *retlen = 1;
107 return *s;
108 }
109 if (!(uv & 0x40)) {
0453d815
PM
110 dTHR;
111 if (ckWARN_d(WARN_UTF8))
112 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
a0ed51b3
LW
113 if (retlen)
114 *retlen = 1;
115 return *s;
116 }
117
118 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
119 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
120 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
121 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
122 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
123 else if (!(uv & 0x01)) { len = 7; uv &= 0x00; }
124 else len = 8; /* whoa! */
125
126 if (retlen)
127 *retlen = len;
128 --len;
129 s++;
130 while (len--) {
131 if ((*s & 0xc0) != 0x80) {
0453d815
PM
132 dTHR;
133 if (ckWARN_d(WARN_UTF8))
134 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
a0ed51b3
LW
135 if (retlen)
136 *retlen -= len + 1;
137 return 0xfffd;
138 }
139 else
140 uv = (uv << 6) | (*s++ & 0x3f);
141 }
142 return uv;
143}
144
145/* utf8_distance(a,b) is intended to be a - b in pointer arithmetic */
146
147I32
864dbfa3 148Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
a0ed51b3
LW
149{
150 I32 off = 0;
151 if (a < b) {
152 while (a < b) {
153 a += UTF8SKIP(a);
154 off--;
155 }
156 }
157 else {
158 while (b < a) {
159 b += UTF8SKIP(b);
160 off++;
161 }
162 }
163 return off;
164}
165
166/* WARNING: do not use the following unless you *know* off is within bounds */
167
168U8 *
864dbfa3 169Perl_utf8_hop(pTHX_ U8 *s, I32 off)
a0ed51b3
LW
170{
171 if (off >= 0) {
172 while (off--)
173 s += UTF8SKIP(s);
174 }
175 else {
176 while (off++) {
177 s--;
178 if (*s & 0x80) {
179 while ((*s & 0xc0) == 0x80)
180 s--;
181 }
182 }
183 }
184 return s;
185}
186
187/* XXX NOTHING CALLS THE FOLLOWING TWO ROUTINES YET!!! */
188/*
189 * Convert native or reversed UTF-16 to UTF-8.
190 *
191 * Destination must be pre-extended to 3/2 source. Do not use in-place.
192 * We optimize for native, for obvious reasons. */
193
194U8*
864dbfa3 195Perl_utf16_to_utf8(pTHX_ U16* p, U8* d, I32 bytelen)
a0ed51b3
LW
196{
197 U16* pend = p + bytelen / 2;
198 while (p < pend) {
199 UV uv = *p++;
200 if (uv < 0x80) {
201 *d++ = uv;
202 continue;
203 }
204 if (uv < 0x800) {
205 *d++ = (( uv >> 6) | 0xc0);
206 *d++ = (( uv & 0x3f) | 0x80);
207 continue;
208 }
209 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
0453d815 210 dTHR;
a0ed51b3
LW
211 int low = *p++;
212 if (low < 0xdc00 || low >= 0xdfff) {
0453d815
PM
213 if (ckWARN_d(WARN_UTF8))
214 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-16 surrogate");
a0ed51b3
LW
215 p--;
216 uv = 0xfffd;
217 }
218 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
219 }
220 if (uv < 0x10000) {
221 *d++ = (( uv >> 12) | 0xe0);
222 *d++ = (((uv >> 6) & 0x3f) | 0x80);
223 *d++ = (( uv & 0x3f) | 0x80);
224 continue;
225 }
226 else {
227 *d++ = (( uv >> 18) | 0xf0);
228 *d++ = (((uv >> 12) & 0x3f) | 0x80);
229 *d++ = (((uv >> 6) & 0x3f) | 0x80);
230 *d++ = (( uv & 0x3f) | 0x80);
231 continue;
232 }
233 }
234 return d;
235}
236
237/* Note: this one is slightly destructive of the source. */
238
239U8*
864dbfa3 240Perl_utf16_to_utf8_reversed(pTHX_ U16* p, U8* d, I32 bytelen)
a0ed51b3
LW
241{
242 U8* s = (U8*)p;
243 U8* send = s + bytelen;
244 while (s < send) {
245 U8 tmp = s[0];
246 s[0] = s[1];
247 s[1] = tmp;
248 s += 2;
249 }
250 return utf16_to_utf8(p, d, bytelen);
251}
252
253/* for now these are all defined (inefficiently) in terms of the utf8 versions */
254
255bool
864dbfa3 256Perl_is_uni_alnum(pTHX_ U32 c)
a0ed51b3 257{
dfe13c55 258 U8 tmpbuf[10];
a0ed51b3
LW
259 uv_to_utf8(tmpbuf, (UV)c);
260 return is_utf8_alnum(tmpbuf);
261}
262
263bool
b8c5462f
JH
264Perl_is_uni_alnumc(pTHX_ U32 c)
265{
266 U8 tmpbuf[10];
267 uv_to_utf8(tmpbuf, (UV)c);
268 return is_utf8_alnumc(tmpbuf);
269}
270
271bool
864dbfa3 272Perl_is_uni_idfirst(pTHX_ U32 c)
a0ed51b3 273{
dfe13c55 274 U8 tmpbuf[10];
a0ed51b3
LW
275 uv_to_utf8(tmpbuf, (UV)c);
276 return is_utf8_idfirst(tmpbuf);
277}
278
279bool
864dbfa3 280Perl_is_uni_alpha(pTHX_ U32 c)
a0ed51b3 281{
dfe13c55 282 U8 tmpbuf[10];
a0ed51b3
LW
283 uv_to_utf8(tmpbuf, (UV)c);
284 return is_utf8_alpha(tmpbuf);
285}
286
287bool
864dbfa3 288Perl_is_uni_space(pTHX_ U32 c)
a0ed51b3 289{
dfe13c55 290 U8 tmpbuf[10];
a0ed51b3
LW
291 uv_to_utf8(tmpbuf, (UV)c);
292 return is_utf8_space(tmpbuf);
293}
294
295bool
864dbfa3 296Perl_is_uni_digit(pTHX_ U32 c)
a0ed51b3 297{
dfe13c55 298 U8 tmpbuf[10];
a0ed51b3
LW
299 uv_to_utf8(tmpbuf, (UV)c);
300 return is_utf8_digit(tmpbuf);
301}
302
303bool
864dbfa3 304Perl_is_uni_upper(pTHX_ U32 c)
a0ed51b3 305{
dfe13c55 306 U8 tmpbuf[10];
a0ed51b3
LW
307 uv_to_utf8(tmpbuf, (UV)c);
308 return is_utf8_upper(tmpbuf);
309}
310
311bool
864dbfa3 312Perl_is_uni_lower(pTHX_ U32 c)
a0ed51b3 313{
dfe13c55 314 U8 tmpbuf[10];
a0ed51b3
LW
315 uv_to_utf8(tmpbuf, (UV)c);
316 return is_utf8_lower(tmpbuf);
317}
318
319bool
b8c5462f
JH
320Perl_is_uni_cntrl(pTHX_ U32 c)
321{
322 U8 tmpbuf[10];
323 uv_to_utf8(tmpbuf, (UV)c);
324 return is_utf8_cntrl(tmpbuf);
325}
326
327bool
328Perl_is_uni_graph(pTHX_ U32 c)
329{
330 U8 tmpbuf[10];
331 uv_to_utf8(tmpbuf, (UV)c);
332 return is_utf8_graph(tmpbuf);
333}
334
335bool
864dbfa3 336Perl_is_uni_print(pTHX_ U32 c)
a0ed51b3 337{
dfe13c55 338 U8 tmpbuf[10];
a0ed51b3
LW
339 uv_to_utf8(tmpbuf, (UV)c);
340 return is_utf8_print(tmpbuf);
341}
342
b8c5462f 343bool
f248d071 344Perl_is_uni_punct(pTHX_ U32 c)
b8c5462f
JH
345{
346 U8 tmpbuf[10];
347 uv_to_utf8(tmpbuf, (UV)c);
348 return is_utf8_punct(tmpbuf);
349}
350
a0ed51b3 351U32
864dbfa3 352Perl_to_uni_upper(pTHX_ U32 c)
a0ed51b3 353{
dfe13c55 354 U8 tmpbuf[10];
a0ed51b3
LW
355 uv_to_utf8(tmpbuf, (UV)c);
356 return to_utf8_upper(tmpbuf);
357}
358
359U32
864dbfa3 360Perl_to_uni_title(pTHX_ U32 c)
a0ed51b3 361{
dfe13c55 362 U8 tmpbuf[10];
a0ed51b3
LW
363 uv_to_utf8(tmpbuf, (UV)c);
364 return to_utf8_title(tmpbuf);
365}
366
367U32
864dbfa3 368Perl_to_uni_lower(pTHX_ U32 c)
a0ed51b3 369{
dfe13c55 370 U8 tmpbuf[10];
a0ed51b3
LW
371 uv_to_utf8(tmpbuf, (UV)c);
372 return to_utf8_lower(tmpbuf);
373}
374
375/* for now these all assume no locale info available for Unicode > 255 */
376
377bool
864dbfa3 378Perl_is_uni_alnum_lc(pTHX_ U32 c)
a0ed51b3
LW
379{
380 return is_uni_alnum(c); /* XXX no locale support yet */
381}
382
383bool
b8c5462f
JH
384Perl_is_uni_alnumc_lc(pTHX_ U32 c)
385{
386 return is_uni_alnumc(c); /* XXX no locale support yet */
387}
388
389bool
864dbfa3 390Perl_is_uni_idfirst_lc(pTHX_ U32 c)
a0ed51b3
LW
391{
392 return is_uni_idfirst(c); /* XXX no locale support yet */
393}
394
395bool
864dbfa3 396Perl_is_uni_alpha_lc(pTHX_ U32 c)
a0ed51b3
LW
397{
398 return is_uni_alpha(c); /* XXX no locale support yet */
399}
400
401bool
864dbfa3 402Perl_is_uni_space_lc(pTHX_ U32 c)
a0ed51b3
LW
403{
404 return is_uni_space(c); /* XXX no locale support yet */
405}
406
407bool
864dbfa3 408Perl_is_uni_digit_lc(pTHX_ U32 c)
a0ed51b3
LW
409{
410 return is_uni_digit(c); /* XXX no locale support yet */
411}
412
413bool
864dbfa3 414Perl_is_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
415{
416 return is_uni_upper(c); /* XXX no locale support yet */
417}
418
419bool
864dbfa3 420Perl_is_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
421{
422 return is_uni_lower(c); /* XXX no locale support yet */
423}
424
425bool
b8c5462f
JH
426Perl_is_uni_cntrl_lc(pTHX_ U32 c)
427{
428 return is_uni_cntrl(c); /* XXX no locale support yet */
429}
430
431bool
432Perl_is_uni_graph_lc(pTHX_ U32 c)
433{
434 return is_uni_graph(c); /* XXX no locale support yet */
435}
436
437bool
864dbfa3 438Perl_is_uni_print_lc(pTHX_ U32 c)
a0ed51b3
LW
439{
440 return is_uni_print(c); /* XXX no locale support yet */
441}
442
b8c5462f
JH
443bool
444Perl_is_uni_punct_lc(pTHX_ U32 c)
445{
446 return is_uni_punct(c); /* XXX no locale support yet */
447}
448
a0ed51b3 449U32
864dbfa3 450Perl_to_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
451{
452 return to_uni_upper(c); /* XXX no locale support yet */
453}
454
455U32
864dbfa3 456Perl_to_uni_title_lc(pTHX_ U32 c)
a0ed51b3
LW
457{
458 return to_uni_title(c); /* XXX no locale support yet */
459}
460
461U32
864dbfa3 462Perl_to_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
463{
464 return to_uni_lower(c); /* XXX no locale support yet */
465}
466
a0ed51b3 467bool
864dbfa3 468Perl_is_utf8_alnum(pTHX_ U8 *p)
a0ed51b3
LW
469{
470 if (!PL_utf8_alnum)
e24b16f9 471 PL_utf8_alnum = swash_init("utf8", "IsAlnum", &PL_sv_undef, 0, 0);
a0ed51b3
LW
472 return swash_fetch(PL_utf8_alnum, p);
473/* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
474#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
475 if (!PL_utf8_alnum)
476 PL_utf8_alnum = swash_init("utf8", "",
477 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
478 return swash_fetch(PL_utf8_alnum, p);
479#endif
480}
481
482bool
b8c5462f
JH
483Perl_is_utf8_alnumc(pTHX_ U8 *p)
484{
485 if (!PL_utf8_alnum)
486 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
487 return swash_fetch(PL_utf8_alnum, p);
488/* return is_utf8_alpha(p) || is_utf8_digit(p); */
489#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
490 if (!PL_utf8_alnum)
491 PL_utf8_alnum = swash_init("utf8", "",
492 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
493 return swash_fetch(PL_utf8_alnum, p);
494#endif
495}
496
497bool
864dbfa3 498Perl_is_utf8_idfirst(pTHX_ U8 *p)
a0ed51b3
LW
499{
500 return *p == '_' || is_utf8_alpha(p);
501}
502
503bool
864dbfa3 504Perl_is_utf8_alpha(pTHX_ U8 *p)
a0ed51b3
LW
505{
506 if (!PL_utf8_alpha)
e24b16f9 507 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
a0ed51b3
LW
508 return swash_fetch(PL_utf8_alpha, p);
509}
510
511bool
b8c5462f
JH
512Perl_is_utf8_ascii(pTHX_ U8 *p)
513{
514 if (!PL_utf8_ascii)
515 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
516 return swash_fetch(PL_utf8_ascii, p);
517}
518
519bool
864dbfa3 520Perl_is_utf8_space(pTHX_ U8 *p)
a0ed51b3
LW
521{
522 if (!PL_utf8_space)
e24b16f9 523 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
a0ed51b3
LW
524 return swash_fetch(PL_utf8_space, p);
525}
526
527bool
864dbfa3 528Perl_is_utf8_digit(pTHX_ U8 *p)
a0ed51b3
LW
529{
530 if (!PL_utf8_digit)
e24b16f9 531 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
a0ed51b3
LW
532 return swash_fetch(PL_utf8_digit, p);
533}
534
535bool
864dbfa3 536Perl_is_utf8_upper(pTHX_ U8 *p)
a0ed51b3
LW
537{
538 if (!PL_utf8_upper)
e24b16f9 539 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
a0ed51b3
LW
540 return swash_fetch(PL_utf8_upper, p);
541}
542
543bool
864dbfa3 544Perl_is_utf8_lower(pTHX_ U8 *p)
a0ed51b3
LW
545{
546 if (!PL_utf8_lower)
e24b16f9 547 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
a0ed51b3
LW
548 return swash_fetch(PL_utf8_lower, p);
549}
550
551bool
b8c5462f
JH
552Perl_is_utf8_cntrl(pTHX_ U8 *p)
553{
554 if (!PL_utf8_cntrl)
555 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
556 return swash_fetch(PL_utf8_cntrl, p);
557}
558
559bool
560Perl_is_utf8_graph(pTHX_ U8 *p)
561{
562 if (!PL_utf8_graph)
563 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
564 return swash_fetch(PL_utf8_graph, p);
565}
566
567bool
864dbfa3 568Perl_is_utf8_print(pTHX_ U8 *p)
a0ed51b3
LW
569{
570 if (!PL_utf8_print)
e24b16f9 571 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
a0ed51b3
LW
572 return swash_fetch(PL_utf8_print, p);
573}
574
575bool
b8c5462f
JH
576Perl_is_utf8_punct(pTHX_ U8 *p)
577{
578 if (!PL_utf8_punct)
579 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
580 return swash_fetch(PL_utf8_punct, p);
581}
582
583bool
584Perl_is_utf8_xdigit(pTHX_ U8 *p)
585{
586 if (!PL_utf8_xdigit)
587 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
588 return swash_fetch(PL_utf8_xdigit, p);
589}
590
591bool
864dbfa3 592Perl_is_utf8_mark(pTHX_ U8 *p)
a0ed51b3
LW
593{
594 if (!PL_utf8_mark)
e24b16f9 595 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
a0ed51b3
LW
596 return swash_fetch(PL_utf8_mark, p);
597}
598
2104c8d9 599UV
864dbfa3 600Perl_to_utf8_upper(pTHX_ U8 *p)
a0ed51b3
LW
601{
602 UV uv;
603
604 if (!PL_utf8_toupper)
e24b16f9 605 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
a0ed51b3
LW
606 uv = swash_fetch(PL_utf8_toupper, p);
607 return uv ? uv : utf8_to_uv(p,0);
608}
609
2104c8d9 610UV
864dbfa3 611Perl_to_utf8_title(pTHX_ U8 *p)
a0ed51b3
LW
612{
613 UV uv;
614
615 if (!PL_utf8_totitle)
e24b16f9 616 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
a0ed51b3
LW
617 uv = swash_fetch(PL_utf8_totitle, p);
618 return uv ? uv : utf8_to_uv(p,0);
619}
620
2104c8d9 621UV
864dbfa3 622Perl_to_utf8_lower(pTHX_ U8 *p)
a0ed51b3
LW
623{
624 UV uv;
625
626 if (!PL_utf8_tolower)
e24b16f9 627 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
a0ed51b3
LW
628 uv = swash_fetch(PL_utf8_tolower, p);
629 return uv ? uv : utf8_to_uv(p,0);
630}
631
632/* a "swash" is a swatch hash */
633
634SV*
864dbfa3 635Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
a0ed51b3
LW
636{
637 SV* retval;
638 char tmpbuf[256];
639 dSP;
640 PUSHSTACKi(PERLSI_MAGIC);
641 PUSHMARK(SP);
642 EXTEND(SP,5);
643 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
644 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
645 PUSHs(listsv);
646 PUSHs(sv_2mortal(newSViv(minbits)));
647 PUSHs(sv_2mortal(newSViv(none)));
648 PUTBACK;
649 ENTER;
650 SAVEI32(PL_hints);
651 PL_hints = 0;
652 save_re_context();
e24b16f9 653 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
a0ed51b3 654 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
864dbfa3 655 if (call_method("SWASHNEW", G_SCALAR))
e24b16f9 656 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 657 else
e24b16f9 658 retval = &PL_sv_undef;
a0ed51b3
LW
659 LEAVE;
660 POPSTACK;
e24b16f9 661 if (PL_curcop == &PL_compiling) {
a0ed51b3 662 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
e24b16f9 663 PL_curcop->op_private = PL_hints;
a0ed51b3
LW
664 }
665 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
cea2e8a9 666 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
a0ed51b3
LW
667 return retval;
668}
669
670UV
864dbfa3 671Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
a0ed51b3
LW
672{
673 HV* hv = (HV*)SvRV(sv);
674 U32 klen = UTF8SKIP(ptr) - 1;
675 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
676 STRLEN slen;
677 STRLEN needents = (klen ? 64 : 128);
dfe13c55 678 U8 *tmps;
a0ed51b3
LW
679 U32 bit;
680 SV *retval;
681
682 /*
683 * This single-entry cache saves about 1/3 of the utf8 overhead in test
684 * suite. (That is, only 7-8% overall over just a hash cache. Still,
685 * it's nothing to sniff at.) Pity we usually come through at least
686 * two function calls to get here...
687 *
688 * NB: this code assumes that swatches are never modified, once generated!
689 */
690
691 if (hv == PL_last_swash_hv &&
692 klen == PL_last_swash_klen &&
693 (!klen || memEQ(ptr,PL_last_swash_key,klen)) )
694 {
695 tmps = PL_last_swash_tmps;
696 slen = PL_last_swash_slen;
697 }
698 else {
699 /* Try our second-level swatch cache, kept in a hash. */
dfe13c55 700 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
a0ed51b3
LW
701
702 /* If not cached, generate it via utf8::SWASHGET */
dfe13c55 703 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
a0ed51b3
LW
704 dSP;
705 ENTER;
706 SAVETMPS;
707 save_re_context();
708 PUSHSTACKi(PERLSI_MAGIC);
709 PUSHMARK(SP);
710 EXTEND(SP,3);
711 PUSHs((SV*)sv);
712 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, 0) & ~(needents - 1))));
713 PUSHs(sv_2mortal(newSViv(needents)));
714 PUTBACK;
864dbfa3 715 if (call_method("SWASHGET", G_SCALAR))
e24b16f9 716 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 717 else
e24b16f9 718 retval = &PL_sv_undef;
a0ed51b3
LW
719 POPSTACK;
720 FREETMPS;
721 LEAVE;
e24b16f9
GS
722 if (PL_curcop == &PL_compiling)
723 PL_curcop->op_private = PL_hints;
a0ed51b3 724
dfe13c55 725 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
a0ed51b3 726
dfe13c55 727 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
cea2e8a9 728 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
a0ed51b3
LW
729 }
730
731 PL_last_swash_hv = hv;
732 PL_last_swash_klen = klen;
733 PL_last_swash_tmps = tmps;
734 PL_last_swash_slen = slen;
735 if (klen)
736 Copy(ptr, PL_last_swash_key, klen, U8);
737 }
738
739 switch ((slen << 3) / needents) {
740 case 1:
741 bit = 1 << (off & 7);
742 off >>= 3;
743 return (tmps[off] & bit) != 0;
744 case 8:
745 return tmps[off];
746 case 16:
747 off <<= 1;
748 return (tmps[off] << 8) + tmps[off + 1] ;
749 case 32:
750 off <<= 2;
751 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
752 }
cea2e8a9 753 Perl_croak(aTHX_ "panic: swash_fetch");
a0ed51b3
LW
754 return 0;
755}