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