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