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