This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regen perltoc.
[perl5.git] / utf8.c
CommitLineData
a0ed51b3
LW
1/* utf8.c
2 *
3818b22b 3 * Copyright (c) 1998-2000, 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 }
6b8eaf93 71#ifdef HAS_QUAD
628e1a40 72 if (uv < 0x1000000000LL)
a0ed51b3
LW
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 }
6b8eaf93 84#ifdef HAS_QUAD
a0ed51b3
LW
85 {
86 *d++ = 0xff; /* Can't match U+FFFE! */
3c77ea2b
GS
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);
a0ed51b3
LW
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);
99 return d;
100 }
101#endif
102}
103
386d01d6
GS
104/* Tests if some arbitrary number of bytes begins in a valid UTF-8 character.
105 * The actual number of bytes in the UTF-8 character will be returned if it
106 * is valid, otherwise 0. */
107int
108Perl_is_utf8_char(pTHX_ U8 *s)
109{
110 U8 u = *s;
111 int slen, len;
112
113 if (!(u & 0x80))
114 return 1;
115
116 if (!(u & 0x40))
117 return 0;
118
119 if (!(u & 0x20)) { len = 2; }
120 else if (!(u & 0x10)) { len = 3; }
121 else if (!(u & 0x08)) { len = 4; }
122 else if (!(u & 0x04)) { len = 5; }
123 else if (!(u & 0x02)) { len = 6; }
124 else if (!(u & 0x01)) { len = 7; }
125 else { len = 13; } /* whoa! */
126
127 slen = len - 1;
128 s++;
129 while (slen--) {
130 if ((*s & 0xc0) != 0x80)
131 return 0;
132 s++;
133 }
134 return len;
135}
136
6662521e
GS
137/*
138=for apidoc Am|bool_utf8_string|U8 *s|STRLEN len
139
140Returns true if first C<len> bytes of the given string form valid a UTF8
141string, false otherwise.
142
143=cut
144*/
145
146bool
147Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
148{
149 U8* x=s;
150 U8* send=s+len;
151 int c;
152 while (x < send) {
153 c = is_utf8_char(x);
154 x += c;
155 if (!c || x > send)
156 return 0;
157 }
158 return 1;
159}
160
a0ed51b3 161UV
864dbfa3 162Perl_utf8_to_uv(pTHX_ U8* s, I32* retlen)
a0ed51b3
LW
163{
164 UV uv = *s;
165 int len;
166 if (!(uv & 0x80)) {
167 if (retlen)
168 *retlen = 1;
169 return *s;
170 }
171 if (!(uv & 0x40)) {
0453d815
PM
172 dTHR;
173 if (ckWARN_d(WARN_UTF8))
174 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
a0ed51b3
LW
175 if (retlen)
176 *retlen = 1;
177 return *s;
178 }
179
180 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
181 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
182 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
183 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
184 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
3c77ea2b
GS
185 else if (!(uv & 0x01)) { len = 7; uv = 0; }
186 else { len = 13; uv = 0; } /* whoa! */
a0ed51b3
LW
187
188 if (retlen)
189 *retlen = len;
190 --len;
191 s++;
192 while (len--) {
193 if ((*s & 0xc0) != 0x80) {
0453d815
PM
194 dTHR;
195 if (ckWARN_d(WARN_UTF8))
196 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
a0ed51b3
LW
197 if (retlen)
198 *retlen -= len + 1;
199 return 0xfffd;
200 }
201 else
202 uv = (uv << 6) | (*s++ & 0x3f);
203 }
204 return uv;
205}
206
207/* utf8_distance(a,b) is intended to be a - b in pointer arithmetic */
208
209I32
864dbfa3 210Perl_utf8_distance(pTHX_ U8 *a, U8 *b)
a0ed51b3
LW
211{
212 I32 off = 0;
213 if (a < b) {
214 while (a < b) {
215 a += UTF8SKIP(a);
216 off--;
217 }
218 }
219 else {
220 while (b < a) {
221 b += UTF8SKIP(b);
222 off++;
223 }
224 }
225 return off;
226}
227
228/* WARNING: do not use the following unless you *know* off is within bounds */
229
230U8 *
864dbfa3 231Perl_utf8_hop(pTHX_ U8 *s, I32 off)
a0ed51b3
LW
232{
233 if (off >= 0) {
234 while (off--)
235 s += UTF8SKIP(s);
236 }
237 else {
238 while (off++) {
239 s--;
240 if (*s & 0x80) {
241 while ((*s & 0xc0) == 0x80)
242 s--;
243 }
244 }
245 }
246 return s;
247}
248
6940069f 249/*
497711e7 250=for apidoc Am|U8 *|utf8_to_bytes|U8 *s|STRLEN len
6940069f
GS
251
252Converts a string C<s> of length C<len> from UTF8 into ASCII encoding.
253Unlike C<bytes_to_utf8>, this over-writes the original string.
6662521e 254Returns zero on failure after converting as much as possible.
6940069f
GS
255
256=cut
257*/
258
259U8 *
260Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN len)
261{
262 dTHR;
263 U8 *send;
264 U8 *d;
265 U8 *save;
266
267 send = s + len;
268 d = save = s;
269 while (s < send) {
270 if (*s < 0x80)
271 *d++ = *s++;
272 else {
273 I32 ulen;
274 UV uv = utf8_to_uv(s, &ulen);
6662521e
GS
275 if (uv > 255) {
276 *d = '\0';
277 return 0;
278 }
6940069f
GS
279 s += ulen;
280 *d++ = (U8)uv;
281 }
282 }
283 *d = '\0';
284 return save;
285}
286
287/*
6662521e 288=for apidoc Am|U8 *|bytes_to_utf8|U8 *s|STRLEN *len
6940069f
GS
289
290Converts a string C<s> of length C<len> from ASCII into UTF8 encoding.
6662521e
GS
291Returns a pointer to the newly-created string, and sets C<len> to
292reflect the new length.
6940069f 293
497711e7 294=cut
6940069f
GS
295*/
296
297U8*
6662521e 298Perl_bytes_to_utf8(pTHX_ U8* s, STRLEN *len)
6940069f
GS
299{
300 dTHR;
301 U8 *send;
302 U8 *d;
303 U8 *dst;
6662521e 304 send = s + (*len);
6940069f 305
6662521e 306 Newz(801, d, (*len) * 2 + 1, U8);
6940069f
GS
307 dst = d;
308
309 while (s < send) {
310 if (*s < 0x80)
311 *d++ = *s++;
312 else {
313 UV uv = *s++;
314 *d++ = (( uv >> 6) | 0xc0);
315 *d++ = (( uv & 0x3f) | 0x80);
316 }
317 }
318 *d = '\0';
6662521e 319 *len = d-dst;
6940069f
GS
320 return dst;
321}
322
a0ed51b3 323/*
dea0fc0b 324 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
a0ed51b3
LW
325 *
326 * Destination must be pre-extended to 3/2 source. Do not use in-place.
327 * We optimize for native, for obvious reasons. */
328
329U8*
dea0fc0b 330Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
a0ed51b3 331{
dea0fc0b
JH
332 U8* pend;
333 U8* dstart = d;
334
335 if (bytelen & 1)
a7867d0a 336 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen");
dea0fc0b
JH
337
338 pend = p + bytelen;
339
a0ed51b3 340 while (p < pend) {
dea0fc0b
JH
341 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
342 p += 2;
a0ed51b3
LW
343 if (uv < 0x80) {
344 *d++ = uv;
345 continue;
346 }
347 if (uv < 0x800) {
348 *d++ = (( uv >> 6) | 0xc0);
349 *d++ = (( uv & 0x3f) | 0x80);
350 continue;
351 }
352 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
0453d815 353 dTHR;
dea0fc0b
JH
354 UV low = *p++;
355 if (low < 0xdc00 || low >= 0xdfff)
356 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
a0ed51b3
LW
357 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
358 }
359 if (uv < 0x10000) {
360 *d++ = (( uv >> 12) | 0xe0);
361 *d++ = (((uv >> 6) & 0x3f) | 0x80);
362 *d++ = (( uv & 0x3f) | 0x80);
363 continue;
364 }
365 else {
366 *d++ = (( uv >> 18) | 0xf0);
367 *d++ = (((uv >> 12) & 0x3f) | 0x80);
368 *d++ = (((uv >> 6) & 0x3f) | 0x80);
369 *d++ = (( uv & 0x3f) | 0x80);
370 continue;
371 }
372 }
dea0fc0b 373 *newlen = d - dstart;
a0ed51b3
LW
374 return d;
375}
376
377/* Note: this one is slightly destructive of the source. */
378
379U8*
dea0fc0b 380Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
a0ed51b3
LW
381{
382 U8* s = (U8*)p;
383 U8* send = s + bytelen;
384 while (s < send) {
385 U8 tmp = s[0];
386 s[0] = s[1];
387 s[1] = tmp;
388 s += 2;
389 }
dea0fc0b 390 return utf16_to_utf8(p, d, bytelen, newlen);
a0ed51b3
LW
391}
392
393/* for now these are all defined (inefficiently) in terms of the utf8 versions */
394
395bool
864dbfa3 396Perl_is_uni_alnum(pTHX_ U32 c)
a0ed51b3 397{
aa6ffa16 398 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
399 uv_to_utf8(tmpbuf, (UV)c);
400 return is_utf8_alnum(tmpbuf);
401}
402
403bool
b8c5462f
JH
404Perl_is_uni_alnumc(pTHX_ U32 c)
405{
aa6ffa16 406 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
407 uv_to_utf8(tmpbuf, (UV)c);
408 return is_utf8_alnumc(tmpbuf);
409}
410
411bool
864dbfa3 412Perl_is_uni_idfirst(pTHX_ U32 c)
a0ed51b3 413{
aa6ffa16 414 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
415 uv_to_utf8(tmpbuf, (UV)c);
416 return is_utf8_idfirst(tmpbuf);
417}
418
419bool
864dbfa3 420Perl_is_uni_alpha(pTHX_ U32 c)
a0ed51b3 421{
aa6ffa16 422 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
423 uv_to_utf8(tmpbuf, (UV)c);
424 return is_utf8_alpha(tmpbuf);
425}
426
427bool
4d61ec05
GS
428Perl_is_uni_ascii(pTHX_ U32 c)
429{
aa6ffa16 430 U8 tmpbuf[UTF8_MAXLEN];
4d61ec05
GS
431 uv_to_utf8(tmpbuf, (UV)c);
432 return is_utf8_ascii(tmpbuf);
433}
434
435bool
864dbfa3 436Perl_is_uni_space(pTHX_ U32 c)
a0ed51b3 437{
aa6ffa16 438 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
439 uv_to_utf8(tmpbuf, (UV)c);
440 return is_utf8_space(tmpbuf);
441}
442
443bool
864dbfa3 444Perl_is_uni_digit(pTHX_ U32 c)
a0ed51b3 445{
aa6ffa16 446 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
447 uv_to_utf8(tmpbuf, (UV)c);
448 return is_utf8_digit(tmpbuf);
449}
450
451bool
864dbfa3 452Perl_is_uni_upper(pTHX_ U32 c)
a0ed51b3 453{
aa6ffa16 454 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
455 uv_to_utf8(tmpbuf, (UV)c);
456 return is_utf8_upper(tmpbuf);
457}
458
459bool
864dbfa3 460Perl_is_uni_lower(pTHX_ U32 c)
a0ed51b3 461{
aa6ffa16 462 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
463 uv_to_utf8(tmpbuf, (UV)c);
464 return is_utf8_lower(tmpbuf);
465}
466
467bool
b8c5462f
JH
468Perl_is_uni_cntrl(pTHX_ U32 c)
469{
aa6ffa16 470 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
471 uv_to_utf8(tmpbuf, (UV)c);
472 return is_utf8_cntrl(tmpbuf);
473}
474
475bool
476Perl_is_uni_graph(pTHX_ U32 c)
477{
aa6ffa16 478 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
479 uv_to_utf8(tmpbuf, (UV)c);
480 return is_utf8_graph(tmpbuf);
481}
482
483bool
864dbfa3 484Perl_is_uni_print(pTHX_ U32 c)
a0ed51b3 485{
aa6ffa16 486 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
487 uv_to_utf8(tmpbuf, (UV)c);
488 return is_utf8_print(tmpbuf);
489}
490
b8c5462f 491bool
f248d071 492Perl_is_uni_punct(pTHX_ U32 c)
b8c5462f 493{
aa6ffa16 494 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
495 uv_to_utf8(tmpbuf, (UV)c);
496 return is_utf8_punct(tmpbuf);
497}
498
4d61ec05
GS
499bool
500Perl_is_uni_xdigit(pTHX_ U32 c)
501{
aa6ffa16 502 U8 tmpbuf[UTF8_MAXLEN];
4d61ec05
GS
503 uv_to_utf8(tmpbuf, (UV)c);
504 return is_utf8_xdigit(tmpbuf);
505}
506
a0ed51b3 507U32
864dbfa3 508Perl_to_uni_upper(pTHX_ U32 c)
a0ed51b3 509{
aa6ffa16 510 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
511 uv_to_utf8(tmpbuf, (UV)c);
512 return to_utf8_upper(tmpbuf);
513}
514
515U32
864dbfa3 516Perl_to_uni_title(pTHX_ U32 c)
a0ed51b3 517{
aa6ffa16 518 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
519 uv_to_utf8(tmpbuf, (UV)c);
520 return to_utf8_title(tmpbuf);
521}
522
523U32
864dbfa3 524Perl_to_uni_lower(pTHX_ U32 c)
a0ed51b3 525{
aa6ffa16 526 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
527 uv_to_utf8(tmpbuf, (UV)c);
528 return to_utf8_lower(tmpbuf);
529}
530
531/* for now these all assume no locale info available for Unicode > 255 */
532
533bool
864dbfa3 534Perl_is_uni_alnum_lc(pTHX_ U32 c)
a0ed51b3
LW
535{
536 return is_uni_alnum(c); /* XXX no locale support yet */
537}
538
539bool
b8c5462f
JH
540Perl_is_uni_alnumc_lc(pTHX_ U32 c)
541{
542 return is_uni_alnumc(c); /* XXX no locale support yet */
543}
544
545bool
864dbfa3 546Perl_is_uni_idfirst_lc(pTHX_ U32 c)
a0ed51b3
LW
547{
548 return is_uni_idfirst(c); /* XXX no locale support yet */
549}
550
551bool
864dbfa3 552Perl_is_uni_alpha_lc(pTHX_ U32 c)
a0ed51b3
LW
553{
554 return is_uni_alpha(c); /* XXX no locale support yet */
555}
556
557bool
4d61ec05
GS
558Perl_is_uni_ascii_lc(pTHX_ U32 c)
559{
560 return is_uni_ascii(c); /* XXX no locale support yet */
561}
562
563bool
864dbfa3 564Perl_is_uni_space_lc(pTHX_ U32 c)
a0ed51b3
LW
565{
566 return is_uni_space(c); /* XXX no locale support yet */
567}
568
569bool
864dbfa3 570Perl_is_uni_digit_lc(pTHX_ U32 c)
a0ed51b3
LW
571{
572 return is_uni_digit(c); /* XXX no locale support yet */
573}
574
575bool
864dbfa3 576Perl_is_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
577{
578 return is_uni_upper(c); /* XXX no locale support yet */
579}
580
581bool
864dbfa3 582Perl_is_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
583{
584 return is_uni_lower(c); /* XXX no locale support yet */
585}
586
587bool
b8c5462f
JH
588Perl_is_uni_cntrl_lc(pTHX_ U32 c)
589{
590 return is_uni_cntrl(c); /* XXX no locale support yet */
591}
592
593bool
594Perl_is_uni_graph_lc(pTHX_ U32 c)
595{
596 return is_uni_graph(c); /* XXX no locale support yet */
597}
598
599bool
864dbfa3 600Perl_is_uni_print_lc(pTHX_ U32 c)
a0ed51b3
LW
601{
602 return is_uni_print(c); /* XXX no locale support yet */
603}
604
b8c5462f
JH
605bool
606Perl_is_uni_punct_lc(pTHX_ U32 c)
607{
608 return is_uni_punct(c); /* XXX no locale support yet */
609}
610
4d61ec05
GS
611bool
612Perl_is_uni_xdigit_lc(pTHX_ U32 c)
613{
614 return is_uni_xdigit(c); /* XXX no locale support yet */
615}
616
a0ed51b3 617U32
864dbfa3 618Perl_to_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
619{
620 return to_uni_upper(c); /* XXX no locale support yet */
621}
622
623U32
864dbfa3 624Perl_to_uni_title_lc(pTHX_ U32 c)
a0ed51b3
LW
625{
626 return to_uni_title(c); /* XXX no locale support yet */
627}
628
629U32
864dbfa3 630Perl_to_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
631{
632 return to_uni_lower(c); /* XXX no locale support yet */
633}
634
a0ed51b3 635bool
864dbfa3 636Perl_is_utf8_alnum(pTHX_ U8 *p)
a0ed51b3 637{
386d01d6
GS
638 if (!is_utf8_char(p))
639 return FALSE;
a0ed51b3 640 if (!PL_utf8_alnum)
e24b16f9 641 PL_utf8_alnum = swash_init("utf8", "IsAlnum", &PL_sv_undef, 0, 0);
a0ed51b3
LW
642 return swash_fetch(PL_utf8_alnum, p);
643/* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
644#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
645 if (!PL_utf8_alnum)
646 PL_utf8_alnum = swash_init("utf8", "",
647 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
648 return swash_fetch(PL_utf8_alnum, p);
649#endif
650}
651
652bool
b8c5462f
JH
653Perl_is_utf8_alnumc(pTHX_ U8 *p)
654{
386d01d6
GS
655 if (!is_utf8_char(p))
656 return FALSE;
b8c5462f
JH
657 if (!PL_utf8_alnum)
658 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
659 return swash_fetch(PL_utf8_alnum, p);
660/* return is_utf8_alpha(p) || is_utf8_digit(p); */
661#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
662 if (!PL_utf8_alnum)
663 PL_utf8_alnum = swash_init("utf8", "",
664 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
665 return swash_fetch(PL_utf8_alnum, p);
666#endif
667}
668
669bool
864dbfa3 670Perl_is_utf8_idfirst(pTHX_ U8 *p)
a0ed51b3
LW
671{
672 return *p == '_' || is_utf8_alpha(p);
673}
674
675bool
864dbfa3 676Perl_is_utf8_alpha(pTHX_ U8 *p)
a0ed51b3 677{
386d01d6
GS
678 if (!is_utf8_char(p))
679 return FALSE;
a0ed51b3 680 if (!PL_utf8_alpha)
e24b16f9 681 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
a0ed51b3
LW
682 return swash_fetch(PL_utf8_alpha, p);
683}
684
685bool
b8c5462f
JH
686Perl_is_utf8_ascii(pTHX_ U8 *p)
687{
386d01d6
GS
688 if (!is_utf8_char(p))
689 return FALSE;
b8c5462f
JH
690 if (!PL_utf8_ascii)
691 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
692 return swash_fetch(PL_utf8_ascii, p);
693}
694
695bool
864dbfa3 696Perl_is_utf8_space(pTHX_ U8 *p)
a0ed51b3 697{
386d01d6
GS
698 if (!is_utf8_char(p))
699 return FALSE;
a0ed51b3 700 if (!PL_utf8_space)
e24b16f9 701 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
a0ed51b3
LW
702 return swash_fetch(PL_utf8_space, p);
703}
704
705bool
864dbfa3 706Perl_is_utf8_digit(pTHX_ U8 *p)
a0ed51b3 707{
386d01d6
GS
708 if (!is_utf8_char(p))
709 return FALSE;
a0ed51b3 710 if (!PL_utf8_digit)
e24b16f9 711 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
a0ed51b3
LW
712 return swash_fetch(PL_utf8_digit, p);
713}
714
715bool
864dbfa3 716Perl_is_utf8_upper(pTHX_ U8 *p)
a0ed51b3 717{
386d01d6
GS
718 if (!is_utf8_char(p))
719 return FALSE;
a0ed51b3 720 if (!PL_utf8_upper)
e24b16f9 721 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
a0ed51b3
LW
722 return swash_fetch(PL_utf8_upper, p);
723}
724
725bool
864dbfa3 726Perl_is_utf8_lower(pTHX_ U8 *p)
a0ed51b3 727{
386d01d6
GS
728 if (!is_utf8_char(p))
729 return FALSE;
a0ed51b3 730 if (!PL_utf8_lower)
e24b16f9 731 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
a0ed51b3
LW
732 return swash_fetch(PL_utf8_lower, p);
733}
734
735bool
b8c5462f
JH
736Perl_is_utf8_cntrl(pTHX_ U8 *p)
737{
386d01d6
GS
738 if (!is_utf8_char(p))
739 return FALSE;
b8c5462f
JH
740 if (!PL_utf8_cntrl)
741 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
742 return swash_fetch(PL_utf8_cntrl, p);
743}
744
745bool
746Perl_is_utf8_graph(pTHX_ U8 *p)
747{
386d01d6
GS
748 if (!is_utf8_char(p))
749 return FALSE;
b8c5462f
JH
750 if (!PL_utf8_graph)
751 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
752 return swash_fetch(PL_utf8_graph, p);
753}
754
755bool
864dbfa3 756Perl_is_utf8_print(pTHX_ U8 *p)
a0ed51b3 757{
386d01d6
GS
758 if (!is_utf8_char(p))
759 return FALSE;
a0ed51b3 760 if (!PL_utf8_print)
e24b16f9 761 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
a0ed51b3
LW
762 return swash_fetch(PL_utf8_print, p);
763}
764
765bool
b8c5462f
JH
766Perl_is_utf8_punct(pTHX_ U8 *p)
767{
386d01d6
GS
768 if (!is_utf8_char(p))
769 return FALSE;
b8c5462f
JH
770 if (!PL_utf8_punct)
771 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
772 return swash_fetch(PL_utf8_punct, p);
773}
774
775bool
776Perl_is_utf8_xdigit(pTHX_ U8 *p)
777{
386d01d6
GS
778 if (!is_utf8_char(p))
779 return FALSE;
b8c5462f
JH
780 if (!PL_utf8_xdigit)
781 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
782 return swash_fetch(PL_utf8_xdigit, p);
783}
784
785bool
864dbfa3 786Perl_is_utf8_mark(pTHX_ U8 *p)
a0ed51b3 787{
386d01d6
GS
788 if (!is_utf8_char(p))
789 return FALSE;
a0ed51b3 790 if (!PL_utf8_mark)
e24b16f9 791 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
a0ed51b3
LW
792 return swash_fetch(PL_utf8_mark, p);
793}
794
2104c8d9 795UV
864dbfa3 796Perl_to_utf8_upper(pTHX_ U8 *p)
a0ed51b3
LW
797{
798 UV uv;
799
800 if (!PL_utf8_toupper)
e24b16f9 801 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
a0ed51b3
LW
802 uv = swash_fetch(PL_utf8_toupper, p);
803 return uv ? uv : utf8_to_uv(p,0);
804}
805
2104c8d9 806UV
864dbfa3 807Perl_to_utf8_title(pTHX_ U8 *p)
a0ed51b3
LW
808{
809 UV uv;
810
811 if (!PL_utf8_totitle)
e24b16f9 812 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
a0ed51b3
LW
813 uv = swash_fetch(PL_utf8_totitle, p);
814 return uv ? uv : utf8_to_uv(p,0);
815}
816
2104c8d9 817UV
864dbfa3 818Perl_to_utf8_lower(pTHX_ U8 *p)
a0ed51b3
LW
819{
820 UV uv;
821
822 if (!PL_utf8_tolower)
e24b16f9 823 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
a0ed51b3
LW
824 uv = swash_fetch(PL_utf8_tolower, p);
825 return uv ? uv : utf8_to_uv(p,0);
826}
827
828/* a "swash" is a swatch hash */
829
830SV*
864dbfa3 831Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
a0ed51b3
LW
832{
833 SV* retval;
834 char tmpbuf[256];
835 dSP;
ce3b816e
GS
836
837 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
838 ENTER;
839 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
840 LEAVE;
841 }
842 SPAGAIN;
a0ed51b3
LW
843 PUSHSTACKi(PERLSI_MAGIC);
844 PUSHMARK(SP);
845 EXTEND(SP,5);
846 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
847 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
848 PUSHs(listsv);
849 PUSHs(sv_2mortal(newSViv(minbits)));
850 PUSHs(sv_2mortal(newSViv(none)));
851 PUTBACK;
852 ENTER;
853 SAVEI32(PL_hints);
854 PL_hints = 0;
855 save_re_context();
e24b16f9 856 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
a0ed51b3 857 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
864dbfa3 858 if (call_method("SWASHNEW", G_SCALAR))
e24b16f9 859 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 860 else
e24b16f9 861 retval = &PL_sv_undef;
a0ed51b3
LW
862 LEAVE;
863 POPSTACK;
e24b16f9 864 if (PL_curcop == &PL_compiling) {
a0ed51b3 865 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
e24b16f9 866 PL_curcop->op_private = PL_hints;
a0ed51b3
LW
867 }
868 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
cea2e8a9 869 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
a0ed51b3
LW
870 return retval;
871}
872
873UV
864dbfa3 874Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
a0ed51b3
LW
875{
876 HV* hv = (HV*)SvRV(sv);
877 U32 klen = UTF8SKIP(ptr) - 1;
878 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
879 STRLEN slen;
880 STRLEN needents = (klen ? 64 : 128);
dfe13c55 881 U8 *tmps;
a0ed51b3
LW
882 U32 bit;
883 SV *retval;
884
885 /*
886 * This single-entry cache saves about 1/3 of the utf8 overhead in test
887 * suite. (That is, only 7-8% overall over just a hash cache. Still,
888 * it's nothing to sniff at.) Pity we usually come through at least
889 * two function calls to get here...
890 *
891 * NB: this code assumes that swatches are never modified, once generated!
892 */
893
894 if (hv == PL_last_swash_hv &&
895 klen == PL_last_swash_klen &&
12ae5dfc 896 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
a0ed51b3
LW
897 {
898 tmps = PL_last_swash_tmps;
899 slen = PL_last_swash_slen;
900 }
901 else {
902 /* Try our second-level swatch cache, kept in a hash. */
dfe13c55 903 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
a0ed51b3
LW
904
905 /* If not cached, generate it via utf8::SWASHGET */
dfe13c55 906 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
a0ed51b3
LW
907 dSP;
908 ENTER;
909 SAVETMPS;
910 save_re_context();
911 PUSHSTACKi(PERLSI_MAGIC);
912 PUSHMARK(SP);
913 EXTEND(SP,3);
914 PUSHs((SV*)sv);
915 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, 0) & ~(needents - 1))));
916 PUSHs(sv_2mortal(newSViv(needents)));
917 PUTBACK;
864dbfa3 918 if (call_method("SWASHGET", G_SCALAR))
e24b16f9 919 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 920 else
e24b16f9 921 retval = &PL_sv_undef;
a0ed51b3
LW
922 POPSTACK;
923 FREETMPS;
924 LEAVE;
e24b16f9
GS
925 if (PL_curcop == &PL_compiling)
926 PL_curcop->op_private = PL_hints;
a0ed51b3 927
dfe13c55 928 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
a0ed51b3 929
dfe13c55 930 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
cea2e8a9 931 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
a0ed51b3
LW
932 }
933
934 PL_last_swash_hv = hv;
935 PL_last_swash_klen = klen;
936 PL_last_swash_tmps = tmps;
937 PL_last_swash_slen = slen;
938 if (klen)
939 Copy(ptr, PL_last_swash_key, klen, U8);
940 }
941
942 switch ((slen << 3) / needents) {
943 case 1:
944 bit = 1 << (off & 7);
945 off >>= 3;
946 return (tmps[off] & bit) != 0;
947 case 8:
948 return tmps[off];
949 case 16:
950 off <<= 1;
951 return (tmps[off] << 8) + tmps[off + 1] ;
952 case 32:
953 off <<= 2;
954 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
955 }
cea2e8a9 956 Perl_croak(aTHX_ "panic: swash_fetch");
a0ed51b3
LW
957 return 0;
958}