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