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