This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
patch up egcs-1.1.2-mingw32 build (builds a working miniperl, but not
[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
4d61ec05
GS
288Perl_is_uni_ascii(pTHX_ U32 c)
289{
290 U8 tmpbuf[10];
291 uv_to_utf8(tmpbuf, (UV)c);
292 return is_utf8_ascii(tmpbuf);
293}
294
295bool
864dbfa3 296Perl_is_uni_space(pTHX_ U32 c)
a0ed51b3 297{
dfe13c55 298 U8 tmpbuf[10];
a0ed51b3
LW
299 uv_to_utf8(tmpbuf, (UV)c);
300 return is_utf8_space(tmpbuf);
301}
302
303bool
864dbfa3 304Perl_is_uni_digit(pTHX_ U32 c)
a0ed51b3 305{
dfe13c55 306 U8 tmpbuf[10];
a0ed51b3
LW
307 uv_to_utf8(tmpbuf, (UV)c);
308 return is_utf8_digit(tmpbuf);
309}
310
311bool
864dbfa3 312Perl_is_uni_upper(pTHX_ U32 c)
a0ed51b3 313{
dfe13c55 314 U8 tmpbuf[10];
a0ed51b3
LW
315 uv_to_utf8(tmpbuf, (UV)c);
316 return is_utf8_upper(tmpbuf);
317}
318
319bool
864dbfa3 320Perl_is_uni_lower(pTHX_ U32 c)
a0ed51b3 321{
dfe13c55 322 U8 tmpbuf[10];
a0ed51b3
LW
323 uv_to_utf8(tmpbuf, (UV)c);
324 return is_utf8_lower(tmpbuf);
325}
326
327bool
b8c5462f
JH
328Perl_is_uni_cntrl(pTHX_ U32 c)
329{
330 U8 tmpbuf[10];
331 uv_to_utf8(tmpbuf, (UV)c);
332 return is_utf8_cntrl(tmpbuf);
333}
334
335bool
336Perl_is_uni_graph(pTHX_ U32 c)
337{
338 U8 tmpbuf[10];
339 uv_to_utf8(tmpbuf, (UV)c);
340 return is_utf8_graph(tmpbuf);
341}
342
343bool
864dbfa3 344Perl_is_uni_print(pTHX_ U32 c)
a0ed51b3 345{
dfe13c55 346 U8 tmpbuf[10];
a0ed51b3
LW
347 uv_to_utf8(tmpbuf, (UV)c);
348 return is_utf8_print(tmpbuf);
349}
350
b8c5462f 351bool
f248d071 352Perl_is_uni_punct(pTHX_ U32 c)
b8c5462f
JH
353{
354 U8 tmpbuf[10];
355 uv_to_utf8(tmpbuf, (UV)c);
356 return is_utf8_punct(tmpbuf);
357}
358
4d61ec05
GS
359bool
360Perl_is_uni_xdigit(pTHX_ U32 c)
361{
362 U8 tmpbuf[10];
363 uv_to_utf8(tmpbuf, (UV)c);
364 return is_utf8_xdigit(tmpbuf);
365}
366
a0ed51b3 367U32
864dbfa3 368Perl_to_uni_upper(pTHX_ U32 c)
a0ed51b3 369{
dfe13c55 370 U8 tmpbuf[10];
a0ed51b3
LW
371 uv_to_utf8(tmpbuf, (UV)c);
372 return to_utf8_upper(tmpbuf);
373}
374
375U32
864dbfa3 376Perl_to_uni_title(pTHX_ U32 c)
a0ed51b3 377{
dfe13c55 378 U8 tmpbuf[10];
a0ed51b3
LW
379 uv_to_utf8(tmpbuf, (UV)c);
380 return to_utf8_title(tmpbuf);
381}
382
383U32
864dbfa3 384Perl_to_uni_lower(pTHX_ U32 c)
a0ed51b3 385{
dfe13c55 386 U8 tmpbuf[10];
a0ed51b3
LW
387 uv_to_utf8(tmpbuf, (UV)c);
388 return to_utf8_lower(tmpbuf);
389}
390
391/* for now these all assume no locale info available for Unicode > 255 */
392
393bool
864dbfa3 394Perl_is_uni_alnum_lc(pTHX_ U32 c)
a0ed51b3
LW
395{
396 return is_uni_alnum(c); /* XXX no locale support yet */
397}
398
399bool
b8c5462f
JH
400Perl_is_uni_alnumc_lc(pTHX_ U32 c)
401{
402 return is_uni_alnumc(c); /* XXX no locale support yet */
403}
404
405bool
864dbfa3 406Perl_is_uni_idfirst_lc(pTHX_ U32 c)
a0ed51b3
LW
407{
408 return is_uni_idfirst(c); /* XXX no locale support yet */
409}
410
411bool
864dbfa3 412Perl_is_uni_alpha_lc(pTHX_ U32 c)
a0ed51b3
LW
413{
414 return is_uni_alpha(c); /* XXX no locale support yet */
415}
416
417bool
4d61ec05
GS
418Perl_is_uni_ascii_lc(pTHX_ U32 c)
419{
420 return is_uni_ascii(c); /* XXX no locale support yet */
421}
422
423bool
864dbfa3 424Perl_is_uni_space_lc(pTHX_ U32 c)
a0ed51b3
LW
425{
426 return is_uni_space(c); /* XXX no locale support yet */
427}
428
429bool
864dbfa3 430Perl_is_uni_digit_lc(pTHX_ U32 c)
a0ed51b3
LW
431{
432 return is_uni_digit(c); /* XXX no locale support yet */
433}
434
435bool
864dbfa3 436Perl_is_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
437{
438 return is_uni_upper(c); /* XXX no locale support yet */
439}
440
441bool
864dbfa3 442Perl_is_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
443{
444 return is_uni_lower(c); /* XXX no locale support yet */
445}
446
447bool
b8c5462f
JH
448Perl_is_uni_cntrl_lc(pTHX_ U32 c)
449{
450 return is_uni_cntrl(c); /* XXX no locale support yet */
451}
452
453bool
454Perl_is_uni_graph_lc(pTHX_ U32 c)
455{
456 return is_uni_graph(c); /* XXX no locale support yet */
457}
458
459bool
864dbfa3 460Perl_is_uni_print_lc(pTHX_ U32 c)
a0ed51b3
LW
461{
462 return is_uni_print(c); /* XXX no locale support yet */
463}
464
b8c5462f
JH
465bool
466Perl_is_uni_punct_lc(pTHX_ U32 c)
467{
468 return is_uni_punct(c); /* XXX no locale support yet */
469}
470
4d61ec05
GS
471bool
472Perl_is_uni_xdigit_lc(pTHX_ U32 c)
473{
474 return is_uni_xdigit(c); /* XXX no locale support yet */
475}
476
a0ed51b3 477U32
864dbfa3 478Perl_to_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
479{
480 return to_uni_upper(c); /* XXX no locale support yet */
481}
482
483U32
864dbfa3 484Perl_to_uni_title_lc(pTHX_ U32 c)
a0ed51b3
LW
485{
486 return to_uni_title(c); /* XXX no locale support yet */
487}
488
489U32
864dbfa3 490Perl_to_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
491{
492 return to_uni_lower(c); /* XXX no locale support yet */
493}
494
a0ed51b3 495bool
864dbfa3 496Perl_is_utf8_alnum(pTHX_ U8 *p)
a0ed51b3
LW
497{
498 if (!PL_utf8_alnum)
e24b16f9 499 PL_utf8_alnum = swash_init("utf8", "IsAlnum", &PL_sv_undef, 0, 0);
a0ed51b3
LW
500 return swash_fetch(PL_utf8_alnum, p);
501/* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
502#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
503 if (!PL_utf8_alnum)
504 PL_utf8_alnum = swash_init("utf8", "",
505 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
506 return swash_fetch(PL_utf8_alnum, p);
507#endif
508}
509
510bool
b8c5462f
JH
511Perl_is_utf8_alnumc(pTHX_ U8 *p)
512{
513 if (!PL_utf8_alnum)
514 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
515 return swash_fetch(PL_utf8_alnum, p);
516/* return is_utf8_alpha(p) || is_utf8_digit(p); */
517#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
518 if (!PL_utf8_alnum)
519 PL_utf8_alnum = swash_init("utf8", "",
520 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
521 return swash_fetch(PL_utf8_alnum, p);
522#endif
523}
524
525bool
864dbfa3 526Perl_is_utf8_idfirst(pTHX_ U8 *p)
a0ed51b3
LW
527{
528 return *p == '_' || is_utf8_alpha(p);
529}
530
531bool
864dbfa3 532Perl_is_utf8_alpha(pTHX_ U8 *p)
a0ed51b3
LW
533{
534 if (!PL_utf8_alpha)
e24b16f9 535 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
a0ed51b3
LW
536 return swash_fetch(PL_utf8_alpha, p);
537}
538
539bool
b8c5462f
JH
540Perl_is_utf8_ascii(pTHX_ U8 *p)
541{
542 if (!PL_utf8_ascii)
543 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
544 return swash_fetch(PL_utf8_ascii, p);
545}
546
547bool
864dbfa3 548Perl_is_utf8_space(pTHX_ U8 *p)
a0ed51b3
LW
549{
550 if (!PL_utf8_space)
e24b16f9 551 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
a0ed51b3
LW
552 return swash_fetch(PL_utf8_space, p);
553}
554
555bool
864dbfa3 556Perl_is_utf8_digit(pTHX_ U8 *p)
a0ed51b3
LW
557{
558 if (!PL_utf8_digit)
e24b16f9 559 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
a0ed51b3
LW
560 return swash_fetch(PL_utf8_digit, p);
561}
562
563bool
864dbfa3 564Perl_is_utf8_upper(pTHX_ U8 *p)
a0ed51b3
LW
565{
566 if (!PL_utf8_upper)
e24b16f9 567 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
a0ed51b3
LW
568 return swash_fetch(PL_utf8_upper, p);
569}
570
571bool
864dbfa3 572Perl_is_utf8_lower(pTHX_ U8 *p)
a0ed51b3
LW
573{
574 if (!PL_utf8_lower)
e24b16f9 575 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
a0ed51b3
LW
576 return swash_fetch(PL_utf8_lower, p);
577}
578
579bool
b8c5462f
JH
580Perl_is_utf8_cntrl(pTHX_ U8 *p)
581{
582 if (!PL_utf8_cntrl)
583 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
584 return swash_fetch(PL_utf8_cntrl, p);
585}
586
587bool
588Perl_is_utf8_graph(pTHX_ U8 *p)
589{
590 if (!PL_utf8_graph)
591 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
592 return swash_fetch(PL_utf8_graph, p);
593}
594
595bool
864dbfa3 596Perl_is_utf8_print(pTHX_ U8 *p)
a0ed51b3
LW
597{
598 if (!PL_utf8_print)
e24b16f9 599 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
a0ed51b3
LW
600 return swash_fetch(PL_utf8_print, p);
601}
602
603bool
b8c5462f
JH
604Perl_is_utf8_punct(pTHX_ U8 *p)
605{
606 if (!PL_utf8_punct)
607 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
608 return swash_fetch(PL_utf8_punct, p);
609}
610
611bool
612Perl_is_utf8_xdigit(pTHX_ U8 *p)
613{
614 if (!PL_utf8_xdigit)
615 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
616 return swash_fetch(PL_utf8_xdigit, p);
617}
618
619bool
864dbfa3 620Perl_is_utf8_mark(pTHX_ U8 *p)
a0ed51b3
LW
621{
622 if (!PL_utf8_mark)
e24b16f9 623 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
a0ed51b3
LW
624 return swash_fetch(PL_utf8_mark, p);
625}
626
2104c8d9 627UV
864dbfa3 628Perl_to_utf8_upper(pTHX_ U8 *p)
a0ed51b3
LW
629{
630 UV uv;
631
632 if (!PL_utf8_toupper)
e24b16f9 633 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
a0ed51b3
LW
634 uv = swash_fetch(PL_utf8_toupper, p);
635 return uv ? uv : utf8_to_uv(p,0);
636}
637
2104c8d9 638UV
864dbfa3 639Perl_to_utf8_title(pTHX_ U8 *p)
a0ed51b3
LW
640{
641 UV uv;
642
643 if (!PL_utf8_totitle)
e24b16f9 644 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
a0ed51b3
LW
645 uv = swash_fetch(PL_utf8_totitle, p);
646 return uv ? uv : utf8_to_uv(p,0);
647}
648
2104c8d9 649UV
864dbfa3 650Perl_to_utf8_lower(pTHX_ U8 *p)
a0ed51b3
LW
651{
652 UV uv;
653
654 if (!PL_utf8_tolower)
e24b16f9 655 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
a0ed51b3
LW
656 uv = swash_fetch(PL_utf8_tolower, p);
657 return uv ? uv : utf8_to_uv(p,0);
658}
659
660/* a "swash" is a swatch hash */
661
662SV*
864dbfa3 663Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
a0ed51b3
LW
664{
665 SV* retval;
666 char tmpbuf[256];
667 dSP;
668 PUSHSTACKi(PERLSI_MAGIC);
669 PUSHMARK(SP);
670 EXTEND(SP,5);
671 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
672 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
673 PUSHs(listsv);
674 PUSHs(sv_2mortal(newSViv(minbits)));
675 PUSHs(sv_2mortal(newSViv(none)));
676 PUTBACK;
677 ENTER;
678 SAVEI32(PL_hints);
679 PL_hints = 0;
680 save_re_context();
e24b16f9 681 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
a0ed51b3 682 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
864dbfa3 683 if (call_method("SWASHNEW", G_SCALAR))
e24b16f9 684 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 685 else
e24b16f9 686 retval = &PL_sv_undef;
a0ed51b3
LW
687 LEAVE;
688 POPSTACK;
e24b16f9 689 if (PL_curcop == &PL_compiling) {
a0ed51b3 690 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
e24b16f9 691 PL_curcop->op_private = PL_hints;
a0ed51b3
LW
692 }
693 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
cea2e8a9 694 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
a0ed51b3
LW
695 return retval;
696}
697
698UV
864dbfa3 699Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
a0ed51b3
LW
700{
701 HV* hv = (HV*)SvRV(sv);
702 U32 klen = UTF8SKIP(ptr) - 1;
703 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
704 STRLEN slen;
705 STRLEN needents = (klen ? 64 : 128);
dfe13c55 706 U8 *tmps;
a0ed51b3
LW
707 U32 bit;
708 SV *retval;
709
710 /*
711 * This single-entry cache saves about 1/3 of the utf8 overhead in test
712 * suite. (That is, only 7-8% overall over just a hash cache. Still,
713 * it's nothing to sniff at.) Pity we usually come through at least
714 * two function calls to get here...
715 *
716 * NB: this code assumes that swatches are never modified, once generated!
717 */
718
719 if (hv == PL_last_swash_hv &&
720 klen == PL_last_swash_klen &&
721 (!klen || memEQ(ptr,PL_last_swash_key,klen)) )
722 {
723 tmps = PL_last_swash_tmps;
724 slen = PL_last_swash_slen;
725 }
726 else {
727 /* Try our second-level swatch cache, kept in a hash. */
dfe13c55 728 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
a0ed51b3
LW
729
730 /* If not cached, generate it via utf8::SWASHGET */
dfe13c55 731 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
a0ed51b3
LW
732 dSP;
733 ENTER;
734 SAVETMPS;
735 save_re_context();
736 PUSHSTACKi(PERLSI_MAGIC);
737 PUSHMARK(SP);
738 EXTEND(SP,3);
739 PUSHs((SV*)sv);
740 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, 0) & ~(needents - 1))));
741 PUSHs(sv_2mortal(newSViv(needents)));
742 PUTBACK;
864dbfa3 743 if (call_method("SWASHGET", G_SCALAR))
e24b16f9 744 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 745 else
e24b16f9 746 retval = &PL_sv_undef;
a0ed51b3
LW
747 POPSTACK;
748 FREETMPS;
749 LEAVE;
e24b16f9
GS
750 if (PL_curcop == &PL_compiling)
751 PL_curcop->op_private = PL_hints;
a0ed51b3 752
dfe13c55 753 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
a0ed51b3 754
dfe13c55 755 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
cea2e8a9 756 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
a0ed51b3
LW
757 }
758
759 PL_last_swash_hv = hv;
760 PL_last_swash_klen = klen;
761 PL_last_swash_tmps = tmps;
762 PL_last_swash_slen = slen;
763 if (klen)
764 Copy(ptr, PL_last_swash_key, klen, U8);
765 }
766
767 switch ((slen << 3) / needents) {
768 case 1:
769 bit = 1 << (off & 7);
770 off >>= 3;
771 return (tmps[off] & bit) != 0;
772 case 8:
773 return tmps[off];
774 case 16:
775 off <<= 1;
776 return (tmps[off] << 8) + tmps[off + 1] ;
777 case 32:
778 off <<= 2;
779 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
780 }
cea2e8a9 781 Perl_croak(aTHX_ "panic: swash_fetch");
a0ed51b3
LW
782 return 0;
783}