This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Find green threads before native threads.
[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
LW
323/*
324 * Convert native or reversed UTF-16 to UTF-8.
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
4ac469dc
G
329/* There are several problems with utf16_to_utf8().
330 * (1) U16 is not necessarily *exactly* two bytes.
331 * (2) Secondly, no check is made for odd length.
332 * (3) Thirdly, the "Malformed UTF-16 surrogate" should probably be
333 * a hard error (and it should be listed in perldiag).
334 * (4) The tests (in comp/t/require.t) are a joke: the UTF16 BOM
335 * really ought to be followed by valid UTF16 characters.
350e0994 336 * See swallow_bom() in toke.c.
4ac469dc 337 * --Mike Guy */
a0ed51b3 338U8*
864dbfa3 339Perl_utf16_to_utf8(pTHX_ U16* p, U8* d, I32 bytelen)
a0ed51b3
LW
340{
341 U16* pend = p + bytelen / 2;
342 while (p < pend) {
343 UV uv = *p++;
344 if (uv < 0x80) {
345 *d++ = uv;
346 continue;
347 }
348 if (uv < 0x800) {
349 *d++ = (( uv >> 6) | 0xc0);
350 *d++ = (( uv & 0x3f) | 0x80);
351 continue;
352 }
353 if (uv >= 0xd800 && uv < 0xdbff) { /* surrogates */
0453d815 354 dTHR;
a0ed51b3
LW
355 int low = *p++;
356 if (low < 0xdc00 || low >= 0xdfff) {
0453d815
PM
357 if (ckWARN_d(WARN_UTF8))
358 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-16 surrogate");
a0ed51b3
LW
359 p--;
360 uv = 0xfffd;
361 }
362 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
363 }
364 if (uv < 0x10000) {
365 *d++ = (( uv >> 12) | 0xe0);
366 *d++ = (((uv >> 6) & 0x3f) | 0x80);
367 *d++ = (( uv & 0x3f) | 0x80);
368 continue;
369 }
370 else {
371 *d++ = (( uv >> 18) | 0xf0);
372 *d++ = (((uv >> 12) & 0x3f) | 0x80);
373 *d++ = (((uv >> 6) & 0x3f) | 0x80);
374 *d++ = (( uv & 0x3f) | 0x80);
375 continue;
376 }
377 }
378 return d;
379}
380
381/* Note: this one is slightly destructive of the source. */
382
383U8*
864dbfa3 384Perl_utf16_to_utf8_reversed(pTHX_ U16* p, U8* d, I32 bytelen)
a0ed51b3
LW
385{
386 U8* s = (U8*)p;
387 U8* send = s + bytelen;
388 while (s < send) {
389 U8 tmp = s[0];
390 s[0] = s[1];
391 s[1] = tmp;
392 s += 2;
393 }
394 return utf16_to_utf8(p, d, bytelen);
395}
396
397/* for now these are all defined (inefficiently) in terms of the utf8 versions */
398
399bool
864dbfa3 400Perl_is_uni_alnum(pTHX_ U32 c)
a0ed51b3 401{
aa6ffa16 402 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
403 uv_to_utf8(tmpbuf, (UV)c);
404 return is_utf8_alnum(tmpbuf);
405}
406
407bool
b8c5462f
JH
408Perl_is_uni_alnumc(pTHX_ U32 c)
409{
aa6ffa16 410 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
411 uv_to_utf8(tmpbuf, (UV)c);
412 return is_utf8_alnumc(tmpbuf);
413}
414
415bool
864dbfa3 416Perl_is_uni_idfirst(pTHX_ U32 c)
a0ed51b3 417{
aa6ffa16 418 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
419 uv_to_utf8(tmpbuf, (UV)c);
420 return is_utf8_idfirst(tmpbuf);
421}
422
423bool
864dbfa3 424Perl_is_uni_alpha(pTHX_ U32 c)
a0ed51b3 425{
aa6ffa16 426 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
427 uv_to_utf8(tmpbuf, (UV)c);
428 return is_utf8_alpha(tmpbuf);
429}
430
431bool
4d61ec05
GS
432Perl_is_uni_ascii(pTHX_ U32 c)
433{
aa6ffa16 434 U8 tmpbuf[UTF8_MAXLEN];
4d61ec05
GS
435 uv_to_utf8(tmpbuf, (UV)c);
436 return is_utf8_ascii(tmpbuf);
437}
438
439bool
864dbfa3 440Perl_is_uni_space(pTHX_ U32 c)
a0ed51b3 441{
aa6ffa16 442 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
443 uv_to_utf8(tmpbuf, (UV)c);
444 return is_utf8_space(tmpbuf);
445}
446
447bool
864dbfa3 448Perl_is_uni_digit(pTHX_ U32 c)
a0ed51b3 449{
aa6ffa16 450 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
451 uv_to_utf8(tmpbuf, (UV)c);
452 return is_utf8_digit(tmpbuf);
453}
454
455bool
864dbfa3 456Perl_is_uni_upper(pTHX_ U32 c)
a0ed51b3 457{
aa6ffa16 458 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
459 uv_to_utf8(tmpbuf, (UV)c);
460 return is_utf8_upper(tmpbuf);
461}
462
463bool
864dbfa3 464Perl_is_uni_lower(pTHX_ U32 c)
a0ed51b3 465{
aa6ffa16 466 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
467 uv_to_utf8(tmpbuf, (UV)c);
468 return is_utf8_lower(tmpbuf);
469}
470
471bool
b8c5462f
JH
472Perl_is_uni_cntrl(pTHX_ U32 c)
473{
aa6ffa16 474 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
475 uv_to_utf8(tmpbuf, (UV)c);
476 return is_utf8_cntrl(tmpbuf);
477}
478
479bool
480Perl_is_uni_graph(pTHX_ U32 c)
481{
aa6ffa16 482 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
483 uv_to_utf8(tmpbuf, (UV)c);
484 return is_utf8_graph(tmpbuf);
485}
486
487bool
864dbfa3 488Perl_is_uni_print(pTHX_ U32 c)
a0ed51b3 489{
aa6ffa16 490 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
491 uv_to_utf8(tmpbuf, (UV)c);
492 return is_utf8_print(tmpbuf);
493}
494
b8c5462f 495bool
f248d071 496Perl_is_uni_punct(pTHX_ U32 c)
b8c5462f 497{
aa6ffa16 498 U8 tmpbuf[UTF8_MAXLEN];
b8c5462f
JH
499 uv_to_utf8(tmpbuf, (UV)c);
500 return is_utf8_punct(tmpbuf);
501}
502
4d61ec05
GS
503bool
504Perl_is_uni_xdigit(pTHX_ U32 c)
505{
aa6ffa16 506 U8 tmpbuf[UTF8_MAXLEN];
4d61ec05
GS
507 uv_to_utf8(tmpbuf, (UV)c);
508 return is_utf8_xdigit(tmpbuf);
509}
510
a0ed51b3 511U32
864dbfa3 512Perl_to_uni_upper(pTHX_ U32 c)
a0ed51b3 513{
aa6ffa16 514 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
515 uv_to_utf8(tmpbuf, (UV)c);
516 return to_utf8_upper(tmpbuf);
517}
518
519U32
864dbfa3 520Perl_to_uni_title(pTHX_ U32 c)
a0ed51b3 521{
aa6ffa16 522 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
523 uv_to_utf8(tmpbuf, (UV)c);
524 return to_utf8_title(tmpbuf);
525}
526
527U32
864dbfa3 528Perl_to_uni_lower(pTHX_ U32 c)
a0ed51b3 529{
aa6ffa16 530 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
531 uv_to_utf8(tmpbuf, (UV)c);
532 return to_utf8_lower(tmpbuf);
533}
534
535/* for now these all assume no locale info available for Unicode > 255 */
536
537bool
864dbfa3 538Perl_is_uni_alnum_lc(pTHX_ U32 c)
a0ed51b3
LW
539{
540 return is_uni_alnum(c); /* XXX no locale support yet */
541}
542
543bool
b8c5462f
JH
544Perl_is_uni_alnumc_lc(pTHX_ U32 c)
545{
546 return is_uni_alnumc(c); /* XXX no locale support yet */
547}
548
549bool
864dbfa3 550Perl_is_uni_idfirst_lc(pTHX_ U32 c)
a0ed51b3
LW
551{
552 return is_uni_idfirst(c); /* XXX no locale support yet */
553}
554
555bool
864dbfa3 556Perl_is_uni_alpha_lc(pTHX_ U32 c)
a0ed51b3
LW
557{
558 return is_uni_alpha(c); /* XXX no locale support yet */
559}
560
561bool
4d61ec05
GS
562Perl_is_uni_ascii_lc(pTHX_ U32 c)
563{
564 return is_uni_ascii(c); /* XXX no locale support yet */
565}
566
567bool
864dbfa3 568Perl_is_uni_space_lc(pTHX_ U32 c)
a0ed51b3
LW
569{
570 return is_uni_space(c); /* XXX no locale support yet */
571}
572
573bool
864dbfa3 574Perl_is_uni_digit_lc(pTHX_ U32 c)
a0ed51b3
LW
575{
576 return is_uni_digit(c); /* XXX no locale support yet */
577}
578
579bool
864dbfa3 580Perl_is_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
581{
582 return is_uni_upper(c); /* XXX no locale support yet */
583}
584
585bool
864dbfa3 586Perl_is_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
587{
588 return is_uni_lower(c); /* XXX no locale support yet */
589}
590
591bool
b8c5462f
JH
592Perl_is_uni_cntrl_lc(pTHX_ U32 c)
593{
594 return is_uni_cntrl(c); /* XXX no locale support yet */
595}
596
597bool
598Perl_is_uni_graph_lc(pTHX_ U32 c)
599{
600 return is_uni_graph(c); /* XXX no locale support yet */
601}
602
603bool
864dbfa3 604Perl_is_uni_print_lc(pTHX_ U32 c)
a0ed51b3
LW
605{
606 return is_uni_print(c); /* XXX no locale support yet */
607}
608
b8c5462f
JH
609bool
610Perl_is_uni_punct_lc(pTHX_ U32 c)
611{
612 return is_uni_punct(c); /* XXX no locale support yet */
613}
614
4d61ec05
GS
615bool
616Perl_is_uni_xdigit_lc(pTHX_ U32 c)
617{
618 return is_uni_xdigit(c); /* XXX no locale support yet */
619}
620
a0ed51b3 621U32
864dbfa3 622Perl_to_uni_upper_lc(pTHX_ U32 c)
a0ed51b3
LW
623{
624 return to_uni_upper(c); /* XXX no locale support yet */
625}
626
627U32
864dbfa3 628Perl_to_uni_title_lc(pTHX_ U32 c)
a0ed51b3
LW
629{
630 return to_uni_title(c); /* XXX no locale support yet */
631}
632
633U32
864dbfa3 634Perl_to_uni_lower_lc(pTHX_ U32 c)
a0ed51b3
LW
635{
636 return to_uni_lower(c); /* XXX no locale support yet */
637}
638
a0ed51b3 639bool
864dbfa3 640Perl_is_utf8_alnum(pTHX_ U8 *p)
a0ed51b3 641{
386d01d6
GS
642 if (!is_utf8_char(p))
643 return FALSE;
a0ed51b3 644 if (!PL_utf8_alnum)
e24b16f9 645 PL_utf8_alnum = swash_init("utf8", "IsAlnum", &PL_sv_undef, 0, 0);
a0ed51b3
LW
646 return swash_fetch(PL_utf8_alnum, p);
647/* return *p == '_' || is_utf8_alpha(p) || is_utf8_digit(p); */
648#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
649 if (!PL_utf8_alnum)
650 PL_utf8_alnum = swash_init("utf8", "",
651 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
652 return swash_fetch(PL_utf8_alnum, p);
653#endif
654}
655
656bool
b8c5462f
JH
657Perl_is_utf8_alnumc(pTHX_ U8 *p)
658{
386d01d6
GS
659 if (!is_utf8_char(p))
660 return FALSE;
b8c5462f
JH
661 if (!PL_utf8_alnum)
662 PL_utf8_alnum = swash_init("utf8", "IsAlnumC", &PL_sv_undef, 0, 0);
663 return swash_fetch(PL_utf8_alnum, p);
664/* return is_utf8_alpha(p) || is_utf8_digit(p); */
665#ifdef SURPRISINGLY_SLOWER /* probably because alpha is usually true */
666 if (!PL_utf8_alnum)
667 PL_utf8_alnum = swash_init("utf8", "",
668 sv_2mortal(newSVpv("+utf8::IsAlpha\n+utf8::IsDigit\n005F\n",0)), 0, 0);
669 return swash_fetch(PL_utf8_alnum, p);
670#endif
671}
672
673bool
864dbfa3 674Perl_is_utf8_idfirst(pTHX_ U8 *p)
a0ed51b3
LW
675{
676 return *p == '_' || is_utf8_alpha(p);
677}
678
679bool
864dbfa3 680Perl_is_utf8_alpha(pTHX_ U8 *p)
a0ed51b3 681{
386d01d6
GS
682 if (!is_utf8_char(p))
683 return FALSE;
a0ed51b3 684 if (!PL_utf8_alpha)
e24b16f9 685 PL_utf8_alpha = swash_init("utf8", "IsAlpha", &PL_sv_undef, 0, 0);
a0ed51b3
LW
686 return swash_fetch(PL_utf8_alpha, p);
687}
688
689bool
b8c5462f
JH
690Perl_is_utf8_ascii(pTHX_ U8 *p)
691{
386d01d6
GS
692 if (!is_utf8_char(p))
693 return FALSE;
b8c5462f
JH
694 if (!PL_utf8_ascii)
695 PL_utf8_ascii = swash_init("utf8", "IsAscii", &PL_sv_undef, 0, 0);
696 return swash_fetch(PL_utf8_ascii, p);
697}
698
699bool
864dbfa3 700Perl_is_utf8_space(pTHX_ U8 *p)
a0ed51b3 701{
386d01d6
GS
702 if (!is_utf8_char(p))
703 return FALSE;
a0ed51b3 704 if (!PL_utf8_space)
e24b16f9 705 PL_utf8_space = swash_init("utf8", "IsSpace", &PL_sv_undef, 0, 0);
a0ed51b3
LW
706 return swash_fetch(PL_utf8_space, p);
707}
708
709bool
864dbfa3 710Perl_is_utf8_digit(pTHX_ U8 *p)
a0ed51b3 711{
386d01d6
GS
712 if (!is_utf8_char(p))
713 return FALSE;
a0ed51b3 714 if (!PL_utf8_digit)
e24b16f9 715 PL_utf8_digit = swash_init("utf8", "IsDigit", &PL_sv_undef, 0, 0);
a0ed51b3
LW
716 return swash_fetch(PL_utf8_digit, p);
717}
718
719bool
864dbfa3 720Perl_is_utf8_upper(pTHX_ U8 *p)
a0ed51b3 721{
386d01d6
GS
722 if (!is_utf8_char(p))
723 return FALSE;
a0ed51b3 724 if (!PL_utf8_upper)
e24b16f9 725 PL_utf8_upper = swash_init("utf8", "IsUpper", &PL_sv_undef, 0, 0);
a0ed51b3
LW
726 return swash_fetch(PL_utf8_upper, p);
727}
728
729bool
864dbfa3 730Perl_is_utf8_lower(pTHX_ U8 *p)
a0ed51b3 731{
386d01d6
GS
732 if (!is_utf8_char(p))
733 return FALSE;
a0ed51b3 734 if (!PL_utf8_lower)
e24b16f9 735 PL_utf8_lower = swash_init("utf8", "IsLower", &PL_sv_undef, 0, 0);
a0ed51b3
LW
736 return swash_fetch(PL_utf8_lower, p);
737}
738
739bool
b8c5462f
JH
740Perl_is_utf8_cntrl(pTHX_ U8 *p)
741{
386d01d6
GS
742 if (!is_utf8_char(p))
743 return FALSE;
b8c5462f
JH
744 if (!PL_utf8_cntrl)
745 PL_utf8_cntrl = swash_init("utf8", "IsCntrl", &PL_sv_undef, 0, 0);
746 return swash_fetch(PL_utf8_cntrl, p);
747}
748
749bool
750Perl_is_utf8_graph(pTHX_ U8 *p)
751{
386d01d6
GS
752 if (!is_utf8_char(p))
753 return FALSE;
b8c5462f
JH
754 if (!PL_utf8_graph)
755 PL_utf8_graph = swash_init("utf8", "IsGraph", &PL_sv_undef, 0, 0);
756 return swash_fetch(PL_utf8_graph, p);
757}
758
759bool
864dbfa3 760Perl_is_utf8_print(pTHX_ U8 *p)
a0ed51b3 761{
386d01d6
GS
762 if (!is_utf8_char(p))
763 return FALSE;
a0ed51b3 764 if (!PL_utf8_print)
e24b16f9 765 PL_utf8_print = swash_init("utf8", "IsPrint", &PL_sv_undef, 0, 0);
a0ed51b3
LW
766 return swash_fetch(PL_utf8_print, p);
767}
768
769bool
b8c5462f
JH
770Perl_is_utf8_punct(pTHX_ U8 *p)
771{
386d01d6
GS
772 if (!is_utf8_char(p))
773 return FALSE;
b8c5462f
JH
774 if (!PL_utf8_punct)
775 PL_utf8_punct = swash_init("utf8", "IsPunct", &PL_sv_undef, 0, 0);
776 return swash_fetch(PL_utf8_punct, p);
777}
778
779bool
780Perl_is_utf8_xdigit(pTHX_ U8 *p)
781{
386d01d6
GS
782 if (!is_utf8_char(p))
783 return FALSE;
b8c5462f
JH
784 if (!PL_utf8_xdigit)
785 PL_utf8_xdigit = swash_init("utf8", "IsXDigit", &PL_sv_undef, 0, 0);
786 return swash_fetch(PL_utf8_xdigit, p);
787}
788
789bool
864dbfa3 790Perl_is_utf8_mark(pTHX_ U8 *p)
a0ed51b3 791{
386d01d6
GS
792 if (!is_utf8_char(p))
793 return FALSE;
a0ed51b3 794 if (!PL_utf8_mark)
e24b16f9 795 PL_utf8_mark = swash_init("utf8", "IsM", &PL_sv_undef, 0, 0);
a0ed51b3
LW
796 return swash_fetch(PL_utf8_mark, p);
797}
798
2104c8d9 799UV
864dbfa3 800Perl_to_utf8_upper(pTHX_ U8 *p)
a0ed51b3
LW
801{
802 UV uv;
803
804 if (!PL_utf8_toupper)
e24b16f9 805 PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
a0ed51b3
LW
806 uv = swash_fetch(PL_utf8_toupper, p);
807 return uv ? uv : utf8_to_uv(p,0);
808}
809
2104c8d9 810UV
864dbfa3 811Perl_to_utf8_title(pTHX_ U8 *p)
a0ed51b3
LW
812{
813 UV uv;
814
815 if (!PL_utf8_totitle)
e24b16f9 816 PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
a0ed51b3
LW
817 uv = swash_fetch(PL_utf8_totitle, p);
818 return uv ? uv : utf8_to_uv(p,0);
819}
820
2104c8d9 821UV
864dbfa3 822Perl_to_utf8_lower(pTHX_ U8 *p)
a0ed51b3
LW
823{
824 UV uv;
825
826 if (!PL_utf8_tolower)
e24b16f9 827 PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
a0ed51b3
LW
828 uv = swash_fetch(PL_utf8_tolower, p);
829 return uv ? uv : utf8_to_uv(p,0);
830}
831
832/* a "swash" is a swatch hash */
833
834SV*
864dbfa3 835Perl_swash_init(pTHX_ char* pkg, char* name, SV *listsv, I32 minbits, I32 none)
a0ed51b3
LW
836{
837 SV* retval;
838 char tmpbuf[256];
839 dSP;
ce3b816e
GS
840
841 if (!gv_stashpv(pkg, 0)) { /* demand load utf8 */
842 ENTER;
843 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpv(pkg,0), Nullsv);
844 LEAVE;
845 }
846 SPAGAIN;
a0ed51b3
LW
847 PUSHSTACKi(PERLSI_MAGIC);
848 PUSHMARK(SP);
849 EXTEND(SP,5);
850 PUSHs(sv_2mortal(newSVpvn(pkg, strlen(pkg))));
851 PUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
852 PUSHs(listsv);
853 PUSHs(sv_2mortal(newSViv(minbits)));
854 PUSHs(sv_2mortal(newSViv(none)));
855 PUTBACK;
856 ENTER;
857 SAVEI32(PL_hints);
858 PL_hints = 0;
859 save_re_context();
e24b16f9 860 if (PL_curcop == &PL_compiling) /* XXX ought to be handled by lex_start */
a0ed51b3 861 strncpy(tmpbuf, PL_tokenbuf, sizeof tmpbuf);
864dbfa3 862 if (call_method("SWASHNEW", G_SCALAR))
e24b16f9 863 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 864 else
e24b16f9 865 retval = &PL_sv_undef;
a0ed51b3
LW
866 LEAVE;
867 POPSTACK;
e24b16f9 868 if (PL_curcop == &PL_compiling) {
a0ed51b3 869 strncpy(PL_tokenbuf, tmpbuf, sizeof tmpbuf);
e24b16f9 870 PL_curcop->op_private = PL_hints;
a0ed51b3
LW
871 }
872 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV)
cea2e8a9 873 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
a0ed51b3
LW
874 return retval;
875}
876
877UV
864dbfa3 878Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
a0ed51b3
LW
879{
880 HV* hv = (HV*)SvRV(sv);
881 U32 klen = UTF8SKIP(ptr) - 1;
882 U32 off = ptr[klen] & 127; /* NB: 64 bit always 0 when len > 1 */
883 STRLEN slen;
884 STRLEN needents = (klen ? 64 : 128);
dfe13c55 885 U8 *tmps;
a0ed51b3
LW
886 U32 bit;
887 SV *retval;
888
889 /*
890 * This single-entry cache saves about 1/3 of the utf8 overhead in test
891 * suite. (That is, only 7-8% overall over just a hash cache. Still,
892 * it's nothing to sniff at.) Pity we usually come through at least
893 * two function calls to get here...
894 *
895 * NB: this code assumes that swatches are never modified, once generated!
896 */
897
898 if (hv == PL_last_swash_hv &&
899 klen == PL_last_swash_klen &&
12ae5dfc 900 (!klen || memEQ((char *)ptr,(char *)PL_last_swash_key,klen)) )
a0ed51b3
LW
901 {
902 tmps = PL_last_swash_tmps;
903 slen = PL_last_swash_slen;
904 }
905 else {
906 /* Try our second-level swatch cache, kept in a hash. */
dfe13c55 907 SV** svp = hv_fetch(hv, (char*)ptr, klen, FALSE);
a0ed51b3
LW
908
909 /* If not cached, generate it via utf8::SWASHGET */
dfe13c55 910 if (!svp || !SvPOK(*svp) || !(tmps = (U8*)SvPV(*svp, slen))) {
a0ed51b3
LW
911 dSP;
912 ENTER;
913 SAVETMPS;
914 save_re_context();
915 PUSHSTACKi(PERLSI_MAGIC);
916 PUSHMARK(SP);
917 EXTEND(SP,3);
918 PUSHs((SV*)sv);
919 PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, 0) & ~(needents - 1))));
920 PUSHs(sv_2mortal(newSViv(needents)));
921 PUTBACK;
864dbfa3 922 if (call_method("SWASHGET", G_SCALAR))
e24b16f9 923 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 924 else
e24b16f9 925 retval = &PL_sv_undef;
a0ed51b3
LW
926 POPSTACK;
927 FREETMPS;
928 LEAVE;
e24b16f9
GS
929 if (PL_curcop == &PL_compiling)
930 PL_curcop->op_private = PL_hints;
a0ed51b3 931
dfe13c55 932 svp = hv_store(hv, (char*)ptr, klen, retval, 0);
a0ed51b3 933
dfe13c55 934 if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) || slen < 8)
cea2e8a9 935 Perl_croak(aTHX_ "SWASHGET didn't return result of proper length");
a0ed51b3
LW
936 }
937
938 PL_last_swash_hv = hv;
939 PL_last_swash_klen = klen;
940 PL_last_swash_tmps = tmps;
941 PL_last_swash_slen = slen;
942 if (klen)
943 Copy(ptr, PL_last_swash_key, klen, U8);
944 }
945
946 switch ((slen << 3) / needents) {
947 case 1:
948 bit = 1 << (off & 7);
949 off >>= 3;
950 return (tmps[off] & bit) != 0;
951 case 8:
952 return tmps[off];
953 case 16:
954 off <<= 1;
955 return (tmps[off] << 8) + tmps[off + 1] ;
956 case 32:
957 off <<= 2;
958 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
959 }
cea2e8a9 960 Perl_croak(aTHX_ "panic: swash_fetch");
a0ed51b3
LW
961 return 0;
962}