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