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