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