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