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