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