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