Commit | Line | Data |
---|---|---|
a0ed51b3 LW |
1 | /* utf8.c |
2 | * | |
1129b882 | 3 | * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
b94e2f88 | 4 | * by Larry Wall and others |
a0ed51b3 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
9 | */ | |
10 | ||
11 | /* | |
4ac71550 TC |
12 | * 'What a fix!' said Sam. 'That's the one place in all the lands we've ever |
13 | * heard of that we don't want to see any closer; and that's the one place | |
14 | * we're trying to get to! And that's just where we can't get, nohow.' | |
15 | * | |
cdad3b53 | 16 | * [p.603 of _The Lord of the Rings_, IV/I: "The Taming of Sméagol"] |
a0ed51b3 LW |
17 | * |
18 | * 'Well do I understand your speech,' he answered in the same language; | |
19 | * 'yet few strangers do so. Why then do you not speak in the Common Tongue, | |
4ac71550 | 20 | * as is the custom in the West, if you wish to be answered?' |
cdad3b53 | 21 | * --Gandalf, addressing Théoden's door wardens |
4ac71550 TC |
22 | * |
23 | * [p.508 of _The Lord of the Rings_, III/vi: "The King of the Golden Hall"] | |
a0ed51b3 LW |
24 | * |
25 | * ...the travellers perceived that the floor was paved with stones of many | |
26 | * hues; branching runes and strange devices intertwined beneath their feet. | |
4ac71550 TC |
27 | * |
28 | * [p.512 of _The Lord of the Rings_, III/vi: "The King of the Golden Hall"] | |
a0ed51b3 LW |
29 | */ |
30 | ||
31 | #include "EXTERN.h" | |
864dbfa3 | 32 | #define PERL_IN_UTF8_C |
a0ed51b3 LW |
33 | #include "perl.h" |
34 | ||
a0c21aa1 | 35 | #ifndef EBCDIC |
970ea3cb | 36 | /* Separate prototypes needed because in ASCII systems these are |
a0c21aa1 JH |
37 | * usually macros but they still are compiled as code, too. */ |
38 | PERL_CALLCONV UV Perl_utf8n_to_uvchr(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags); | |
39 | PERL_CALLCONV U8* Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv); | |
40 | #endif | |
41 | ||
27da23d5 JH |
42 | static const char unees[] = |
43 | "Malformed UTF-8 character (unexpected end of string)"; | |
901b21bf | 44 | |
48ef279e | 45 | /* |
ccfc67b7 | 46 | =head1 Unicode Support |
a0ed51b3 | 47 | |
166f8a29 DM |
48 | This file contains various utility functions for manipulating UTF8-encoded |
49 | strings. For the uninitiated, this is a method of representing arbitrary | |
61296642 | 50 | Unicode characters as a variable number of bytes, in such a way that |
56da48f7 DM |
51 | characters in the ASCII range are unmodified, and a zero byte never appears |
52 | within non-zero characters. | |
166f8a29 | 53 | |
eaf7a4d2 CS |
54 | =cut |
55 | */ | |
56 | ||
57 | /* | |
58 | =for apidoc is_ascii_string | |
59 | ||
a1433954 | 60 | Returns true if the first C<len> bytes of the string C<s> are the same whether |
970ea3cb KW |
61 | or not the string is encoded in UTF-8 (or UTF-EBCDIC on EBCDIC machines). That |
62 | is, if they are invariant. On ASCII-ish machines, only ASCII characters | |
63 | fit this definition, hence the function's name. | |
eaf7a4d2 | 64 | |
9f7e3d64 MH |
65 | If C<len> is 0, it will be calculated using C<strlen(s)>. |
66 | ||
a1433954 | 67 | See also L</is_utf8_string>(), L</is_utf8_string_loclen>(), and L</is_utf8_string_loc>(). |
eaf7a4d2 CS |
68 | |
69 | =cut | |
70 | */ | |
71 | ||
72 | bool | |
668b6d8d | 73 | Perl_is_ascii_string(const U8 *s, STRLEN len) |
eaf7a4d2 CS |
74 | { |
75 | const U8* const send = s + (len ? len : strlen((const char *)s)); | |
76 | const U8* x = s; | |
77 | ||
78 | PERL_ARGS_ASSERT_IS_ASCII_STRING; | |
eaf7a4d2 CS |
79 | |
80 | for (; x < send; ++x) { | |
81 | if (!UTF8_IS_INVARIANT(*x)) | |
82 | break; | |
83 | } | |
84 | ||
85 | return x == send; | |
86 | } | |
87 | ||
88 | /* | |
87cea99e | 89 | =for apidoc uvuni_to_utf8_flags |
eebe1485 | 90 | |
6ee84de2 KW |
91 | Adds the UTF-8 representation of the code point C<uv> to the end |
92 | of the string C<d>; C<d> should have at least C<UTF8_MAXBYTES+1> free | |
eebe1485 | 93 | bytes available. The return value is the pointer to the byte after the |
9041c2e3 | 94 | end of the new character. In other words, |
eebe1485 | 95 | |
b851fbc1 JH |
96 | d = uvuni_to_utf8_flags(d, uv, flags); |
97 | ||
98 | or, in most cases, | |
99 | ||
9041c2e3 | 100 | d = uvuni_to_utf8(d, uv); |
eebe1485 | 101 | |
b851fbc1 JH |
102 | (which is equivalent to) |
103 | ||
104 | d = uvuni_to_utf8_flags(d, uv, 0); | |
105 | ||
949cf498 | 106 | This is the recommended Unicode-aware way of saying |
eebe1485 SC |
107 | |
108 | *(d++) = uv; | |
109 | ||
949cf498 KW |
110 | This function will convert to UTF-8 (and not warn) even code points that aren't |
111 | legal Unicode or are problematic, unless C<flags> contains one or more of the | |
a1433954 KW |
112 | following flags: |
113 | ||
949cf498 KW |
114 | If C<uv> is a Unicode surrogate code point and UNICODE_WARN_SURROGATE is set, |
115 | the function will raise a warning, provided UTF8 warnings are enabled. If instead | |
116 | UNICODE_DISALLOW_SURROGATE is set, the function will fail and return NULL. | |
117 | If both flags are set, the function will both warn and return NULL. | |
118 | ||
119 | The UNICODE_WARN_NONCHAR and UNICODE_DISALLOW_NONCHAR flags correspondingly | |
120 | affect how the function handles a Unicode non-character. And, likewise for the | |
121 | UNICODE_WARN_SUPER and UNICODE_DISALLOW_SUPER flags, and code points that are | |
122 | above the Unicode maximum of 0x10FFFF. Code points above 0x7FFF_FFFF (which are | |
123 | even less portable) can be warned and/or disallowed even if other above-Unicode | |
124 | code points are accepted by the UNICODE_WARN_FE_FF and UNICODE_DISALLOW_FE_FF | |
125 | flags. | |
126 | ||
127 | And finally, the flag UNICODE_WARN_ILLEGAL_INTERCHANGE selects all four of the | |
128 | above WARN flags; and UNICODE_DISALLOW_ILLEGAL_INTERCHANGE selects all four | |
129 | DISALLOW flags. | |
130 | ||
131 | ||
eebe1485 SC |
132 | =cut |
133 | */ | |
134 | ||
dfe13c55 | 135 | U8 * |
b851fbc1 | 136 | Perl_uvuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags) |
a0ed51b3 | 137 | { |
7918f24d NC |
138 | PERL_ARGS_ASSERT_UVUNI_TO_UTF8_FLAGS; |
139 | ||
949cf498 KW |
140 | if (ckWARN_d(WARN_UTF8)) { |
141 | if (UNICODE_IS_SURROGATE(uv)) { | |
142 | if (flags & UNICODE_WARN_SURROGATE) { | |
8457b38f | 143 | Perl_ck_warner_d(aTHX_ packWARN(WARN_SURROGATE), |
949cf498 KW |
144 | "UTF-16 surrogate U+%04"UVXf, uv); |
145 | } | |
146 | if (flags & UNICODE_DISALLOW_SURROGATE) { | |
147 | return NULL; | |
148 | } | |
149 | } | |
150 | else if (UNICODE_IS_SUPER(uv)) { | |
151 | if (flags & UNICODE_WARN_SUPER | |
152 | || (UNICODE_IS_FE_FF(uv) && (flags & UNICODE_WARN_FE_FF))) | |
153 | { | |
8457b38f | 154 | Perl_ck_warner_d(aTHX_ packWARN(WARN_NON_UNICODE), |
949cf498 KW |
155 | "Code point 0x%04"UVXf" is not Unicode, may not be portable", uv); |
156 | } | |
157 | if (flags & UNICODE_DISALLOW_SUPER | |
158 | || (UNICODE_IS_FE_FF(uv) && (flags & UNICODE_DISALLOW_FE_FF))) | |
159 | { | |
160 | return NULL; | |
161 | } | |
162 | } | |
163 | else if (UNICODE_IS_NONCHAR(uv)) { | |
164 | if (flags & UNICODE_WARN_NONCHAR) { | |
8457b38f | 165 | Perl_ck_warner_d(aTHX_ packWARN(WARN_NONCHAR), |
949cf498 KW |
166 | "Unicode non-character U+%04"UVXf" is illegal for open interchange", |
167 | uv); | |
168 | } | |
169 | if (flags & UNICODE_DISALLOW_NONCHAR) { | |
170 | return NULL; | |
171 | } | |
172 | } | |
507b9800 | 173 | } |
c4d5f83a | 174 | if (UNI_IS_INVARIANT(uv)) { |
eb160463 | 175 | *d++ = (U8)UTF_TO_NATIVE(uv); |
a0ed51b3 LW |
176 | return d; |
177 | } | |
2d331972 | 178 | #if defined(EBCDIC) |
1d72bdf6 NIS |
179 | else { |
180 | STRLEN len = UNISKIP(uv); | |
181 | U8 *p = d+len-1; | |
182 | while (p > d) { | |
eb160463 | 183 | *p-- = (U8)UTF_TO_NATIVE((uv & UTF_CONTINUATION_MASK) | UTF_CONTINUATION_MARK); |
1d72bdf6 NIS |
184 | uv >>= UTF_ACCUMULATION_SHIFT; |
185 | } | |
eb160463 | 186 | *p = (U8)UTF_TO_NATIVE((uv & UTF_START_MASK(len)) | UTF_START_MARK(len)); |
1d72bdf6 NIS |
187 | return d+len; |
188 | } | |
189 | #else /* Non loop style */ | |
a0ed51b3 | 190 | if (uv < 0x800) { |
eb160463 GS |
191 | *d++ = (U8)(( uv >> 6) | 0xc0); |
192 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
193 | return d; |
194 | } | |
195 | if (uv < 0x10000) { | |
eb160463 GS |
196 | *d++ = (U8)(( uv >> 12) | 0xe0); |
197 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
198 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
199 | return d; |
200 | } | |
201 | if (uv < 0x200000) { | |
eb160463 GS |
202 | *d++ = (U8)(( uv >> 18) | 0xf0); |
203 | *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80); | |
204 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
205 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
206 | return d; |
207 | } | |
208 | if (uv < 0x4000000) { | |
eb160463 GS |
209 | *d++ = (U8)(( uv >> 24) | 0xf8); |
210 | *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80); | |
211 | *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80); | |
212 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
213 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
214 | return d; |
215 | } | |
216 | if (uv < 0x80000000) { | |
eb160463 GS |
217 | *d++ = (U8)(( uv >> 30) | 0xfc); |
218 | *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80); | |
219 | *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80); | |
220 | *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80); | |
221 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
222 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
223 | return d; |
224 | } | |
6b8eaf93 | 225 | #ifdef HAS_QUAD |
d7578b48 | 226 | if (uv < UTF8_QUAD_MAX) |
a0ed51b3 LW |
227 | #endif |
228 | { | |
eb160463 GS |
229 | *d++ = 0xfe; /* Can't match U+FEFF! */ |
230 | *d++ = (U8)(((uv >> 30) & 0x3f) | 0x80); | |
231 | *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80); | |
232 | *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80); | |
233 | *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80); | |
234 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
235 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
236 | return d; |
237 | } | |
6b8eaf93 | 238 | #ifdef HAS_QUAD |
a0ed51b3 | 239 | { |
eb160463 GS |
240 | *d++ = 0xff; /* Can't match U+FFFE! */ |
241 | *d++ = 0x80; /* 6 Reserved bits */ | |
242 | *d++ = (U8)(((uv >> 60) & 0x0f) | 0x80); /* 2 Reserved bits */ | |
243 | *d++ = (U8)(((uv >> 54) & 0x3f) | 0x80); | |
244 | *d++ = (U8)(((uv >> 48) & 0x3f) | 0x80); | |
245 | *d++ = (U8)(((uv >> 42) & 0x3f) | 0x80); | |
246 | *d++ = (U8)(((uv >> 36) & 0x3f) | 0x80); | |
247 | *d++ = (U8)(((uv >> 30) & 0x3f) | 0x80); | |
248 | *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80); | |
249 | *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80); | |
250 | *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80); | |
251 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
252 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
253 | return d; |
254 | } | |
255 | #endif | |
1d72bdf6 | 256 | #endif /* Loop style */ |
a0ed51b3 | 257 | } |
9041c2e3 | 258 | |
646ca15d JH |
259 | /* |
260 | ||
f7d739d1 | 261 | Tests if the first C<len> bytes of string C<s> form a valid UTF-8 |
646ca15d | 262 | character. Note that an INVARIANT (i.e. ASCII) character is a valid |
f7d739d1 | 263 | UTF-8 character. The number of bytes in the UTF-8 character |
646ca15d JH |
264 | will be returned if it is valid, otherwise 0. |
265 | ||
266 | This is the "slow" version as opposed to the "fast" version which is | |
267 | the "unrolled" IS_UTF8_CHAR(). E.g. for t/uni/class.t the speed | |
268 | difference is a factor of 2 to 3. For lengths (UTF8SKIP(s)) of four | |
269 | or less you should use the IS_UTF8_CHAR(), for lengths of five or more | |
270 | you should use the _slow(). In practice this means that the _slow() | |
271 | will be used very rarely, since the maximum Unicode code point (as of | |
272 | Unicode 4.1) is U+10FFFF, which encodes in UTF-8 to four bytes. Only | |
273 | the "Perl extended UTF-8" (the infamous 'v-strings') will encode into | |
274 | five bytes or more. | |
275 | ||
276 | =cut */ | |
c053b435 | 277 | STATIC STRLEN |
5f66b61c | 278 | S_is_utf8_char_slow(const U8 *s, const STRLEN len) |
646ca15d JH |
279 | { |
280 | U8 u = *s; | |
281 | STRLEN slen; | |
282 | UV uv, ouv; | |
283 | ||
7918f24d NC |
284 | PERL_ARGS_ASSERT_IS_UTF8_CHAR_SLOW; |
285 | ||
646ca15d | 286 | if (UTF8_IS_INVARIANT(u)) |
f7d739d1 | 287 | return len == 1; |
646ca15d JH |
288 | |
289 | if (!UTF8_IS_START(u)) | |
290 | return 0; | |
291 | ||
292 | if (len < 2 || !UTF8_IS_CONTINUATION(s[1])) | |
293 | return 0; | |
294 | ||
295 | slen = len - 1; | |
296 | s++; | |
77263263 TS |
297 | #ifdef EBCDIC |
298 | u = NATIVE_TO_UTF(u); | |
299 | #endif | |
646ca15d JH |
300 | u &= UTF_START_MASK(len); |
301 | uv = u; | |
302 | ouv = uv; | |
303 | while (slen--) { | |
304 | if (!UTF8_IS_CONTINUATION(*s)) | |
305 | return 0; | |
306 | uv = UTF8_ACCUMULATE(uv, *s); | |
48ef279e | 307 | if (uv < ouv) |
646ca15d JH |
308 | return 0; |
309 | ouv = uv; | |
310 | s++; | |
311 | } | |
312 | ||
313 | if ((STRLEN)UNISKIP(uv) < len) | |
314 | return 0; | |
315 | ||
316 | return len; | |
317 | } | |
9041c2e3 NIS |
318 | |
319 | /* | |
492a624f KW |
320 | =for apidoc is_utf8_char_buf |
321 | ||
322 | Returns the number of bytes that comprise the first UTF-8 encoded character in | |
323 | buffer C<buf>. C<buf_end> should point to one position beyond the end of the | |
324 | buffer. 0 is returned if C<buf> does not point to a complete, valid UTF-8 | |
325 | encoded character. | |
326 | ||
327 | Note that an INVARIANT character (i.e. ASCII on non-EBCDIC | |
328 | machines) is a valid UTF-8 character. | |
329 | ||
330 | =cut */ | |
331 | ||
332 | STRLEN | |
333 | Perl_is_utf8_char_buf(const U8 *buf, const U8* buf_end) | |
334 | { | |
335 | ||
336 | STRLEN len; | |
337 | ||
338 | PERL_ARGS_ASSERT_IS_UTF8_CHAR_BUF; | |
339 | ||
340 | if (buf_end <= buf) { | |
341 | return 0; | |
342 | } | |
343 | ||
344 | len = buf_end - buf; | |
345 | if (len > UTF8SKIP(buf)) { | |
346 | len = UTF8SKIP(buf); | |
347 | } | |
348 | ||
349 | #ifdef IS_UTF8_CHAR | |
350 | if (IS_UTF8_CHAR_FAST(len)) | |
351 | return IS_UTF8_CHAR(buf, len) ? len : 0; | |
352 | #endif /* #ifdef IS_UTF8_CHAR */ | |
353 | return is_utf8_char_slow(buf, len); | |
354 | } | |
355 | ||
356 | /* | |
87cea99e | 357 | =for apidoc is_utf8_char |
eebe1485 | 358 | |
76848387 KW |
359 | DEPRECATED! |
360 | ||
5da9da9e | 361 | Tests if some arbitrary number of bytes begins in a valid UTF-8 |
2bbc8d55 SP |
362 | character. Note that an INVARIANT (i.e. ASCII on non-EBCDIC machines) |
363 | character is a valid UTF-8 character. The actual number of bytes in the UTF-8 | |
364 | character will be returned if it is valid, otherwise 0. | |
9041c2e3 | 365 | |
76848387 | 366 | This function is deprecated due to the possibility that malformed input could |
a1433954 | 367 | cause reading beyond the end of the input buffer. Use L</is_utf8_char_buf> |
76848387 | 368 | instead. |
e0328548 | 369 | |
82686b01 | 370 | =cut */ |
76848387 | 371 | |
067a85ef | 372 | STRLEN |
668b6d8d | 373 | Perl_is_utf8_char(const U8 *s) |
386d01d6 | 374 | { |
7918f24d | 375 | PERL_ARGS_ASSERT_IS_UTF8_CHAR; |
492a624f | 376 | |
76848387 | 377 | /* Assumes we have enough space, which is why this is deprecated */ |
492a624f | 378 | return is_utf8_char_buf(s, s + UTF8SKIP(s)); |
386d01d6 GS |
379 | } |
380 | ||
eaf7a4d2 | 381 | |
6662521e | 382 | /* |
87cea99e | 383 | =for apidoc is_utf8_string |
6662521e | 384 | |
a1433954 | 385 | Returns true if the first C<len> bytes of string C<s> form a valid |
9f7e3d64 | 386 | UTF-8 string, false otherwise. If C<len> is 0, it will be calculated |
e0328548 KW |
387 | using C<strlen(s)> (which means if you use this option, that C<s> has to have a |
388 | terminating NUL byte). Note that all characters being ASCII constitute 'a | |
389 | valid UTF-8 string'. | |
6662521e | 390 | |
a1433954 | 391 | See also L</is_ascii_string>(), L</is_utf8_string_loclen>(), and L</is_utf8_string_loc>(). |
768c67ee | 392 | |
6662521e GS |
393 | =cut |
394 | */ | |
395 | ||
8e84507e | 396 | bool |
668b6d8d | 397 | Perl_is_utf8_string(const U8 *s, STRLEN len) |
6662521e | 398 | { |
35da51f7 | 399 | const U8* const send = s + (len ? len : strlen((const char *)s)); |
7fc63493 | 400 | const U8* x = s; |
067a85ef | 401 | |
7918f24d | 402 | PERL_ARGS_ASSERT_IS_UTF8_STRING; |
1aa99e6b | 403 | |
6662521e | 404 | while (x < send) { |
1acdb0da | 405 | /* Inline the easy bits of is_utf8_char() here for speed... */ |
e0328548 KW |
406 | if (UTF8_IS_INVARIANT(*x)) { |
407 | x++; | |
408 | } | |
1acdb0da | 409 | else if (!UTF8_IS_START(*x)) |
e0328548 | 410 | return FALSE; |
1acdb0da JH |
411 | else { |
412 | /* ... and call is_utf8_char() only if really needed. */ | |
e0328548 KW |
413 | const STRLEN c = UTF8SKIP(x); |
414 | const U8* const next_char_ptr = x + c; | |
415 | ||
416 | if (next_char_ptr > send) { | |
417 | return FALSE; | |
418 | } | |
419 | ||
768c67ee JH |
420 | if (IS_UTF8_CHAR_FAST(c)) { |
421 | if (!IS_UTF8_CHAR(x, c)) | |
e0328548 | 422 | return FALSE; |
3c614e38 | 423 | } |
e0328548 KW |
424 | else if (! is_utf8_char_slow(x, c)) { |
425 | return FALSE; | |
426 | } | |
427 | x = next_char_ptr; | |
1acdb0da | 428 | } |
6662521e | 429 | } |
768c67ee | 430 | |
067a85ef | 431 | return TRUE; |
6662521e GS |
432 | } |
433 | ||
67e989fb | 434 | /* |
814fafa7 NC |
435 | Implemented as a macro in utf8.h |
436 | ||
87cea99e | 437 | =for apidoc is_utf8_string_loc |
814fafa7 | 438 | |
a1433954 KW |
439 | Like L</is_utf8_string> but stores the location of the failure (in the |
440 | case of "utf8ness failure") or the location C<s>+C<len> (in the case of | |
814fafa7 NC |
441 | "utf8ness success") in the C<ep>. |
442 | ||
a1433954 | 443 | See also L</is_utf8_string_loclen>() and L</is_utf8_string>(). |
814fafa7 | 444 | |
87cea99e | 445 | =for apidoc is_utf8_string_loclen |
81cd54e3 | 446 | |
a1433954 KW |
447 | Like L</is_utf8_string>() but stores the location of the failure (in the |
448 | case of "utf8ness failure") or the location C<s>+C<len> (in the case of | |
768c67ee JH |
449 | "utf8ness success") in the C<ep>, and the number of UTF-8 |
450 | encoded characters in the C<el>. | |
451 | ||
a1433954 | 452 | See also L</is_utf8_string_loc>() and L</is_utf8_string>(). |
81cd54e3 JH |
453 | |
454 | =cut | |
455 | */ | |
456 | ||
457 | bool | |
668b6d8d | 458 | Perl_is_utf8_string_loclen(const U8 *s, STRLEN len, const U8 **ep, STRLEN *el) |
81cd54e3 | 459 | { |
35da51f7 | 460 | const U8* const send = s + (len ? len : strlen((const char *)s)); |
7fc63493 | 461 | const U8* x = s; |
81cd54e3 | 462 | STRLEN c; |
3ebfea28 | 463 | STRLEN outlen = 0; |
7918f24d NC |
464 | |
465 | PERL_ARGS_ASSERT_IS_UTF8_STRING_LOCLEN; | |
81cd54e3 | 466 | |
81cd54e3 | 467 | while (x < send) { |
e0328548 KW |
468 | const U8* next_char_ptr; |
469 | ||
81cd54e3 JH |
470 | /* Inline the easy bits of is_utf8_char() here for speed... */ |
471 | if (UTF8_IS_INVARIANT(*x)) | |
e0328548 | 472 | next_char_ptr = x + 1; |
768c67ee JH |
473 | else if (!UTF8_IS_START(*x)) |
474 | goto out; | |
81cd54e3 | 475 | else { |
768c67ee | 476 | /* ... and call is_utf8_char() only if really needed. */ |
768c67ee | 477 | c = UTF8SKIP(x); |
e0328548 KW |
478 | next_char_ptr = c + x; |
479 | if (next_char_ptr > send) { | |
480 | goto out; | |
481 | } | |
768c67ee JH |
482 | if (IS_UTF8_CHAR_FAST(c)) { |
483 | if (!IS_UTF8_CHAR(x, c)) | |
484 | c = 0; | |
485 | } else | |
486 | c = is_utf8_char_slow(x, c); | |
768c67ee JH |
487 | if (!c) |
488 | goto out; | |
81cd54e3 | 489 | } |
e0328548 | 490 | x = next_char_ptr; |
3ebfea28 | 491 | outlen++; |
81cd54e3 | 492 | } |
768c67ee JH |
493 | |
494 | out: | |
3ebfea28 AL |
495 | if (el) |
496 | *el = outlen; | |
497 | ||
768c67ee JH |
498 | if (ep) |
499 | *ep = x; | |
3ebfea28 | 500 | return (x == send); |
81cd54e3 JH |
501 | } |
502 | ||
503 | /* | |
768c67ee | 504 | |
87cea99e | 505 | =for apidoc utf8n_to_uvuni |
67e989fb | 506 | |
9041c2e3 | 507 | Bottom level UTF-8 decode routine. |
949cf498 KW |
508 | Returns the code point value of the first character in the string C<s> |
509 | which is assumed to be in UTF-8 (or UTF-EBCDIC) encoding and no longer than | |
510 | C<curlen> bytes; C<retlen> will be set to the length, in bytes, of that | |
511 | character. | |
512 | ||
513 | The value of C<flags> determines the behavior when C<s> does not point to a | |
514 | well-formed UTF-8 character. If C<flags> is 0, when a malformation is found, | |
515 | C<retlen> is set to the expected length of the UTF-8 character in bytes, zero | |
516 | is returned, and if UTF-8 warnings haven't been lexically disabled, a warning | |
517 | is raised. | |
518 | ||
519 | Various ALLOW flags can be set in C<flags> to allow (and not warn on) | |
520 | individual types of malformations, such as the sequence being overlong (that | |
521 | is, when there is a shorter sequence that can express the same code point; | |
522 | overlong sequences are expressly forbidden in the UTF-8 standard due to | |
523 | potential security issues). Another malformation example is the first byte of | |
524 | a character not being a legal first byte. See F<utf8.h> for the list of such | |
525 | flags. Of course, the value returned by this function under such conditions is | |
526 | not reliable. | |
527 | ||
528 | The UTF8_CHECK_ONLY flag overrides the behavior when a non-allowed (by other | |
529 | flags) malformation is found. If this flag is set, the routine assumes that | |
530 | the caller will raise a warning, and this function will silently just set | |
531 | C<retlen> to C<-1> and return zero. | |
532 | ||
533 | Certain code points are considered problematic. These are Unicode surrogates, | |
534 | Unicode non-characters, and code points above the Unicode maximum of 0x10FFF. | |
535 | By default these are considered regular code points, but certain situations | |
5eafe189 | 536 | warrant special handling for them. If C<flags> contains |
949cf498 KW |
537 | UTF8_DISALLOW_ILLEGAL_INTERCHANGE, all three classes are treated as |
538 | malformations and handled as such. The flags UTF8_DISALLOW_SURROGATE, | |
539 | UTF8_DISALLOW_NONCHAR, and UTF8_DISALLOW_SUPER (meaning above the legal Unicode | |
540 | maximum) can be set to disallow these categories individually. | |
541 | ||
542 | The flags UTF8_WARN_ILLEGAL_INTERCHANGE, UTF8_WARN_SURROGATE, | |
543 | UTF8_WARN_NONCHAR, and UTF8_WARN_SUPER will cause warning messages to be raised | |
544 | for their respective categories, but otherwise the code points are considered | |
545 | valid (not malformations). To get a category to both be treated as a | |
546 | malformation and raise a warning, specify both the WARN and DISALLOW flags. | |
547 | (But note that warnings are not raised if lexically disabled nor if | |
548 | UTF8_CHECK_ONLY is also specified.) | |
549 | ||
550 | Very large code points (above 0x7FFF_FFFF) are considered more problematic than | |
551 | the others that are above the Unicode legal maximum. There are several | |
d0460f30 KW |
552 | reasons: they do not fit into a 32-bit word, are not representable on EBCDIC |
553 | platforms, and the original UTF-8 specification never went above | |
949cf498 | 554 | this number (the current 0x10FFF limit was imposed later). The UTF-8 encoding |
5eafe189 | 555 | on ASCII platforms for these large code points begins with a byte containing |
949cf498 KW |
556 | 0xFE or 0xFF. The UTF8_DISALLOW_FE_FF flag will cause them to be treated as |
557 | malformations, while allowing smaller above-Unicode code points. (Of course | |
558 | UTF8_DISALLOW_SUPER will treat all above-Unicode code points, including these, | |
559 | as malformations.) Similarly, UTF8_WARN_FE_FF acts just like the other WARN | |
560 | flags, but applies just to these code points. | |
561 | ||
562 | All other code points corresponding to Unicode characters, including private | |
563 | use and those yet to be assigned, are never considered malformed and never | |
564 | warn. | |
67e989fb | 565 | |
ec5f19d0 | 566 | Most code should use L</utf8_to_uvchr_buf>() rather than call this directly. |
9041c2e3 | 567 | |
37607a96 PK |
568 | =cut |
569 | */ | |
67e989fb | 570 | |
a0ed51b3 | 571 | UV |
7fc63493 | 572 | Perl_utf8n_to_uvuni(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags) |
a0ed51b3 | 573 | { |
97aff369 | 574 | dVAR; |
d4c19fe8 | 575 | const U8 * const s0 = s; |
9c5ffd7c | 576 | UV uv = *s, ouv = 0; |
ba210ebe | 577 | STRLEN len = 1; |
949cf498 | 578 | bool dowarn = ckWARN_d(WARN_UTF8); |
7fc63493 | 579 | const UV startbyte = *s; |
ba210ebe | 580 | STRLEN expectlen = 0; |
a0dbb045 | 581 | U32 warning = 0; |
949cf498 | 582 | SV* sv = NULL; |
a0dbb045 | 583 | |
7918f24d NC |
584 | PERL_ARGS_ASSERT_UTF8N_TO_UVUNI; |
585 | ||
949cf498 | 586 | /* This list is a superset of the UTF8_ALLOW_XXX. */ |
a0dbb045 JH |
587 | |
588 | #define UTF8_WARN_EMPTY 1 | |
589 | #define UTF8_WARN_CONTINUATION 2 | |
590 | #define UTF8_WARN_NON_CONTINUATION 3 | |
f9bc8ed7 KW |
591 | #define UTF8_WARN_SHORT 4 |
592 | #define UTF8_WARN_OVERFLOW 5 | |
593 | #define UTF8_WARN_LONG 6 | |
a0dbb045 JH |
594 | |
595 | if (curlen == 0 && | |
596 | !(flags & UTF8_ALLOW_EMPTY)) { | |
597 | warning = UTF8_WARN_EMPTY; | |
0c443dc2 JH |
598 | goto malformed; |
599 | } | |
600 | ||
1d72bdf6 | 601 | if (UTF8_IS_INVARIANT(uv)) { |
a0ed51b3 LW |
602 | if (retlen) |
603 | *retlen = 1; | |
c4d5f83a | 604 | return (UV) (NATIVE_TO_UTF(*s)); |
a0ed51b3 | 605 | } |
67e989fb | 606 | |
421a8bf2 | 607 | if (UTF8_IS_CONTINUATION(uv) && |
fcc8fcf6 | 608 | !(flags & UTF8_ALLOW_CONTINUATION)) { |
a0dbb045 | 609 | warning = UTF8_WARN_CONTINUATION; |
ba210ebe JH |
610 | goto malformed; |
611 | } | |
612 | ||
421a8bf2 | 613 | if (UTF8_IS_START(uv) && curlen > 1 && !UTF8_IS_CONTINUATION(s[1]) && |
fcc8fcf6 | 614 | !(flags & UTF8_ALLOW_NON_CONTINUATION)) { |
a0dbb045 | 615 | warning = UTF8_WARN_NON_CONTINUATION; |
ba210ebe JH |
616 | goto malformed; |
617 | } | |
9041c2e3 | 618 | |
1d72bdf6 | 619 | #ifdef EBCDIC |
75383841 | 620 | uv = NATIVE_TO_UTF(uv); |
1d72bdf6 | 621 | #else |
949cf498 KW |
622 | if (uv == 0xfe || uv == 0xff) { |
623 | if (flags & (UTF8_WARN_SUPER|UTF8_WARN_FE_FF)) { | |
111d382d | 624 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "Code point beginning with byte 0x%02"UVXf" is not Unicode, and not portable", uv)); |
949cf498 KW |
625 | flags &= ~UTF8_WARN_SUPER; /* Only warn once on this problem */ |
626 | } | |
627 | if (flags & (UTF8_DISALLOW_SUPER|UTF8_DISALLOW_FE_FF)) { | |
628 | goto malformed; | |
629 | } | |
a0ed51b3 | 630 | } |
1d72bdf6 NIS |
631 | #endif |
632 | ||
ba210ebe JH |
633 | if (!(uv & 0x20)) { len = 2; uv &= 0x1f; } |
634 | else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; } | |
635 | else if (!(uv & 0x08)) { len = 4; uv &= 0x07; } | |
636 | else if (!(uv & 0x04)) { len = 5; uv &= 0x03; } | |
1d72bdf6 NIS |
637 | #ifdef EBCDIC |
638 | else if (!(uv & 0x02)) { len = 6; uv &= 0x01; } | |
639 | else { len = 7; uv &= 0x01; } | |
640 | #else | |
ba210ebe JH |
641 | else if (!(uv & 0x02)) { len = 6; uv &= 0x01; } |
642 | else if (!(uv & 0x01)) { len = 7; uv = 0; } | |
1d72bdf6 NIS |
643 | else { len = 13; uv = 0; } /* whoa! */ |
644 | #endif | |
645 | ||
a0ed51b3 LW |
646 | if (retlen) |
647 | *retlen = len; | |
9041c2e3 | 648 | |
ba210ebe JH |
649 | expectlen = len; |
650 | ||
fcc8fcf6 JH |
651 | if ((curlen < expectlen) && |
652 | !(flags & UTF8_ALLOW_SHORT)) { | |
a0dbb045 | 653 | warning = UTF8_WARN_SHORT; |
ba210ebe JH |
654 | goto malformed; |
655 | } | |
656 | ||
657 | len--; | |
a0ed51b3 | 658 | s++; |
949cf498 | 659 | ouv = uv; /* ouv is the value from the previous iteration */ |
ba210ebe | 660 | |
a0ed51b3 | 661 | while (len--) { |
421a8bf2 JH |
662 | if (!UTF8_IS_CONTINUATION(*s) && |
663 | !(flags & UTF8_ALLOW_NON_CONTINUATION)) { | |
a0dbb045 JH |
664 | s--; |
665 | warning = UTF8_WARN_NON_CONTINUATION; | |
ba210ebe | 666 | goto malformed; |
a0ed51b3 LW |
667 | } |
668 | else | |
8850bf83 | 669 | uv = UTF8_ACCUMULATE(uv, *s); |
949cf498 KW |
670 | if (!(uv > ouv)) { /* If the value didn't grow from the previous |
671 | iteration, something is horribly wrong */ | |
a0dbb045 JH |
672 | /* These cannot be allowed. */ |
673 | if (uv == ouv) { | |
75dbc644 | 674 | if (expectlen != 13 && !(flags & UTF8_ALLOW_LONG)) { |
a0dbb045 JH |
675 | warning = UTF8_WARN_LONG; |
676 | goto malformed; | |
677 | } | |
678 | } | |
679 | else { /* uv < ouv */ | |
680 | /* This cannot be allowed. */ | |
681 | warning = UTF8_WARN_OVERFLOW; | |
682 | goto malformed; | |
683 | } | |
ba210ebe JH |
684 | } |
685 | s++; | |
686 | ouv = uv; | |
687 | } | |
688 | ||
949cf498 | 689 | if ((expectlen > (STRLEN)UNISKIP(uv)) && !(flags & UTF8_ALLOW_LONG)) { |
a0dbb045 | 690 | warning = UTF8_WARN_LONG; |
ba210ebe | 691 | goto malformed; |
949cf498 KW |
692 | } else if (flags & (UTF8_DISALLOW_ILLEGAL_INTERCHANGE|UTF8_WARN_ILLEGAL_INTERCHANGE)) { |
693 | if (UNICODE_IS_SURROGATE(uv)) { | |
694 | if ((flags & (UTF8_WARN_SURROGATE|UTF8_CHECK_ONLY)) == UTF8_WARN_SURROGATE) { | |
111d382d | 695 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "UTF-16 surrogate U+%04"UVXf"", uv)); |
949cf498 KW |
696 | } |
697 | if (flags & UTF8_DISALLOW_SURROGATE) { | |
698 | goto disallowed; | |
699 | } | |
700 | } | |
701 | else if (UNICODE_IS_NONCHAR(uv)) { | |
702 | if ((flags & (UTF8_WARN_NONCHAR|UTF8_CHECK_ONLY)) == UTF8_WARN_NONCHAR ) { | |
111d382d | 703 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "Unicode non-character U+%04"UVXf" is illegal for open interchange", uv)); |
949cf498 KW |
704 | } |
705 | if (flags & UTF8_DISALLOW_NONCHAR) { | |
706 | goto disallowed; | |
707 | } | |
708 | } | |
709 | else if ((uv > PERL_UNICODE_MAX)) { | |
710 | if ((flags & (UTF8_WARN_SUPER|UTF8_CHECK_ONLY)) == UTF8_WARN_SUPER) { | |
111d382d | 711 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "Code point 0x%04"UVXf" is not Unicode, may not be portable", uv)); |
949cf498 KW |
712 | } |
713 | if (flags & UTF8_DISALLOW_SUPER) { | |
714 | goto disallowed; | |
715 | } | |
716 | } | |
717 | ||
718 | /* Here, this is not considered a malformed character, so drop through | |
719 | * to return it */ | |
a0ed51b3 | 720 | } |
ba210ebe | 721 | |
a0ed51b3 | 722 | return uv; |
ba210ebe | 723 | |
949cf498 KW |
724 | disallowed: /* Is disallowed, but otherwise not malformed. 'sv' will have been |
725 | set if there is to be a warning. */ | |
726 | if (!sv) { | |
727 | dowarn = 0; | |
728 | } | |
729 | ||
ba210ebe JH |
730 | malformed: |
731 | ||
fcc8fcf6 | 732 | if (flags & UTF8_CHECK_ONLY) { |
ba210ebe | 733 | if (retlen) |
10edeb5d | 734 | *retlen = ((STRLEN) -1); |
ba210ebe JH |
735 | return 0; |
736 | } | |
737 | ||
a0dbb045 | 738 | if (dowarn) { |
949cf498 | 739 | if (! sv) { |
5b311467 | 740 | sv = newSVpvs_flags("Malformed UTF-8 character ", SVs_TEMP); |
949cf498 | 741 | } |
5b311467 | 742 | |
5e3d7cf5 KW |
743 | switch (warning) { |
744 | case 0: /* Intentionally empty. */ break; | |
745 | case UTF8_WARN_EMPTY: | |
746 | sv_catpvs(sv, "(empty string)"); | |
747 | break; | |
748 | case UTF8_WARN_CONTINUATION: | |
749 | Perl_sv_catpvf(aTHX_ sv, "(unexpected continuation byte 0x%02"UVxf", with no preceding start byte)", uv); | |
750 | break; | |
751 | case UTF8_WARN_NON_CONTINUATION: | |
752 | if (s == s0) | |
753 | Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf", immediately after start byte 0x%02"UVxf")", | |
754 | (UV)s[1], startbyte); | |
755 | else { | |
756 | const int len = (int)(s-s0); | |
757 | Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf", %d byte%s after start byte 0x%02"UVxf", expected %d bytes)", | |
758 | (UV)s[1], len, len > 1 ? "s" : "", startbyte, (int)expectlen); | |
759 | } | |
760 | ||
761 | break; | |
762 | case UTF8_WARN_SHORT: | |
763 | Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d, after start byte 0x%02"UVxf")", | |
764 | (int)curlen, curlen == 1 ? "" : "s", (int)expectlen, startbyte); | |
765 | expectlen = curlen; /* distance for caller to skip */ | |
766 | break; | |
767 | case UTF8_WARN_OVERFLOW: | |
768 | Perl_sv_catpvf(aTHX_ sv, "(overflow at 0x%"UVxf", byte 0x%02x, after start byte 0x%02"UVxf")", | |
769 | ouv, *s, startbyte); | |
770 | break; | |
771 | case UTF8_WARN_LONG: | |
772 | Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d, after start byte 0x%02"UVxf")", | |
773 | (int)expectlen, expectlen == 1 ? "": "s", UNISKIP(uv), startbyte); | |
774 | break; | |
775 | default: | |
776 | sv_catpvs(sv, "(unknown reason)"); | |
777 | break; | |
778 | } | |
a0dbb045 | 779 | |
949cf498 | 780 | if (sv) { |
44f8325f | 781 | const char * const s = SvPVX_const(sv); |
a0dbb045 JH |
782 | |
783 | if (PL_op) | |
9014280d | 784 | Perl_warner(aTHX_ packWARN(WARN_UTF8), |
53e06cf0 | 785 | "%s in %s", s, OP_DESC(PL_op)); |
a0dbb045 | 786 | else |
9014280d | 787 | Perl_warner(aTHX_ packWARN(WARN_UTF8), "%s", s); |
a0dbb045 JH |
788 | } |
789 | } | |
790 | ||
ba210ebe | 791 | if (retlen) |
28d3d195 | 792 | *retlen = expectlen ? expectlen : len; |
ba210ebe | 793 | |
28d3d195 | 794 | return 0; |
a0ed51b3 LW |
795 | } |
796 | ||
8e84507e | 797 | /* |
ec5f19d0 KW |
798 | =for apidoc utf8_to_uvchr_buf |
799 | ||
800 | Returns the native code point of the first character in the string C<s> which | |
801 | is assumed to be in UTF-8 encoding; C<send> points to 1 beyond the end of C<s>. | |
802 | C<retlen> will be set to the length, in bytes, of that character. | |
803 | ||
804 | If C<s> does not point to a well-formed UTF-8 character, zero is | |
805 | returned and C<retlen> is set, if possible, to -1. | |
806 | ||
807 | =cut | |
808 | */ | |
809 | ||
810 | ||
811 | UV | |
812 | Perl_utf8_to_uvchr_buf(pTHX_ const U8 *s, const U8 *send, STRLEN *retlen) | |
813 | { | |
814 | PERL_ARGS_ASSERT_UTF8_TO_UVCHR_BUF; | |
815 | ||
816 | assert(s < send); | |
817 | ||
818 | return utf8n_to_uvchr(s, send - s, retlen, | |
819 | ckWARN_d(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY); | |
820 | } | |
821 | ||
822 | /* | |
87cea99e | 823 | =for apidoc utf8_to_uvchr |
9041c2e3 | 824 | |
6ee84de2 | 825 | Returns the native code point of the first character in the string C<s> |
1e54db1a | 826 | which is assumed to be in UTF-8 encoding; C<retlen> will be set to the |
9041c2e3 NIS |
827 | length, in bytes, of that character. |
828 | ||
1e54db1a | 829 | If C<s> does not point to a well-formed UTF-8 character, zero is |
a1433954 | 830 | returned and C<retlen> is set, if possible, to -1. |
9041c2e3 NIS |
831 | |
832 | =cut | |
833 | */ | |
834 | ||
835 | UV | |
7fc63493 | 836 | Perl_utf8_to_uvchr(pTHX_ const U8 *s, STRLEN *retlen) |
9041c2e3 | 837 | { |
7918f24d NC |
838 | PERL_ARGS_ASSERT_UTF8_TO_UVCHR; |
839 | ||
1754c1a1 | 840 | return utf8n_to_uvchr(s, UTF8_MAXBYTES, retlen, |
ae0e24b1 | 841 | ckWARN_d(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY); |
9041c2e3 NIS |
842 | } |
843 | ||
844 | /* | |
ec5f19d0 KW |
845 | =for apidoc utf8_to_uvuni_buf |
846 | ||
847 | Returns the Unicode code point of the first character in the string C<s> which | |
848 | is assumed to be in UTF-8 encoding; C<send> points to 1 beyond the end of C<s>. | |
849 | C<retlen> will be set to the length, in bytes, of that character. | |
850 | ||
851 | This function should only be used when the returned UV is considered | |
852 | an index into the Unicode semantic tables (e.g. swashes). | |
853 | ||
854 | If C<s> does not point to a well-formed UTF-8 character, zero is | |
855 | returned and C<retlen> is set, if possible, to -1. | |
856 | ||
857 | =cut | |
858 | */ | |
859 | ||
860 | UV | |
861 | Perl_utf8_to_uvuni_buf(pTHX_ const U8 *s, const U8 *send, STRLEN *retlen) | |
862 | { | |
863 | PERL_ARGS_ASSERT_UTF8_TO_UVUNI_BUF; | |
864 | ||
865 | assert(send > s); | |
866 | ||
867 | /* Call the low level routine asking for checks */ | |
868 | return Perl_utf8n_to_uvuni(aTHX_ s, send -s, retlen, | |
869 | ckWARN_d(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY); | |
870 | } | |
871 | ||
872 | /* | |
87cea99e | 873 | =for apidoc utf8_to_uvuni |
9041c2e3 NIS |
874 | |
875 | Returns the Unicode code point of the first character in the string C<s> | |
1e54db1a | 876 | which is assumed to be in UTF-8 encoding; C<retlen> will be set to the |
9041c2e3 NIS |
877 | length, in bytes, of that character. |
878 | ||
2bbc8d55 | 879 | This function should only be used when the returned UV is considered |
9041c2e3 NIS |
880 | an index into the Unicode semantic tables (e.g. swashes). |
881 | ||
1e54db1a | 882 | If C<s> does not point to a well-formed UTF-8 character, zero is |
a1433954 | 883 | returned and C<retlen> is set, if possible, to -1. |
8e84507e NIS |
884 | |
885 | =cut | |
886 | */ | |
887 | ||
888 | UV | |
7fc63493 | 889 | Perl_utf8_to_uvuni(pTHX_ const U8 *s, STRLEN *retlen) |
8e84507e | 890 | { |
7918f24d NC |
891 | PERL_ARGS_ASSERT_UTF8_TO_UVUNI; |
892 | ||
9041c2e3 | 893 | /* Call the low level routine asking for checks */ |
89ebb4a3 | 894 | return Perl_utf8n_to_uvuni(aTHX_ s, UTF8_MAXBYTES, retlen, |
ae0e24b1 | 895 | ckWARN_d(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY); |
8e84507e NIS |
896 | } |
897 | ||
b76347f2 | 898 | /* |
87cea99e | 899 | =for apidoc utf8_length |
b76347f2 JH |
900 | |
901 | Return the length of the UTF-8 char encoded string C<s> in characters. | |
02eb7b47 JH |
902 | Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end |
903 | up past C<e>, croaks. | |
b76347f2 JH |
904 | |
905 | =cut | |
906 | */ | |
907 | ||
908 | STRLEN | |
35a4481c | 909 | Perl_utf8_length(pTHX_ const U8 *s, const U8 *e) |
b76347f2 | 910 | { |
97aff369 | 911 | dVAR; |
b76347f2 JH |
912 | STRLEN len = 0; |
913 | ||
7918f24d NC |
914 | PERL_ARGS_ASSERT_UTF8_LENGTH; |
915 | ||
8850bf83 JH |
916 | /* Note: cannot use UTF8_IS_...() too eagerly here since e.g. |
917 | * the bitops (especially ~) can create illegal UTF-8. | |
918 | * In other words: in Perl UTF-8 is not just for Unicode. */ | |
919 | ||
a3b680e6 AL |
920 | if (e < s) |
921 | goto warn_and_return; | |
b76347f2 | 922 | while (s < e) { |
8e91ec7f AV |
923 | if (!UTF8_IS_INVARIANT(*s)) |
924 | s += UTF8SKIP(s); | |
925 | else | |
926 | s++; | |
927 | len++; | |
928 | } | |
929 | ||
930 | if (e != s) { | |
931 | len--; | |
932 | warn_and_return: | |
9b387841 NC |
933 | if (PL_op) |
934 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), | |
935 | "%s in %s", unees, OP_DESC(PL_op)); | |
936 | else | |
61a12c31 | 937 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), "%s", unees); |
b76347f2 JH |
938 | } |
939 | ||
940 | return len; | |
941 | } | |
942 | ||
b06226ff | 943 | /* |
87cea99e | 944 | =for apidoc utf8_distance |
b06226ff | 945 | |
1e54db1a | 946 | Returns the number of UTF-8 characters between the UTF-8 pointers C<a> |
b06226ff JH |
947 | and C<b>. |
948 | ||
949 | WARNING: use only if you *know* that the pointers point inside the | |
950 | same UTF-8 buffer. | |
951 | ||
37607a96 PK |
952 | =cut |
953 | */ | |
a0ed51b3 | 954 | |
02eb7b47 | 955 | IV |
35a4481c | 956 | Perl_utf8_distance(pTHX_ const U8 *a, const U8 *b) |
a0ed51b3 | 957 | { |
7918f24d NC |
958 | PERL_ARGS_ASSERT_UTF8_DISTANCE; |
959 | ||
bf1665bc | 960 | return (a < b) ? -1 * (IV) utf8_length(a, b) : (IV) utf8_length(b, a); |
a0ed51b3 LW |
961 | } |
962 | ||
b06226ff | 963 | /* |
87cea99e | 964 | =for apidoc utf8_hop |
b06226ff | 965 | |
8850bf83 JH |
966 | Return the UTF-8 pointer C<s> displaced by C<off> characters, either |
967 | forward or backward. | |
b06226ff JH |
968 | |
969 | WARNING: do not use the following unless you *know* C<off> is within | |
8850bf83 JH |
970 | the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned |
971 | on the first byte of character or just after the last byte of a character. | |
b06226ff | 972 | |
37607a96 PK |
973 | =cut |
974 | */ | |
a0ed51b3 LW |
975 | |
976 | U8 * | |
4373e329 | 977 | Perl_utf8_hop(pTHX_ const U8 *s, I32 off) |
a0ed51b3 | 978 | { |
7918f24d NC |
979 | PERL_ARGS_ASSERT_UTF8_HOP; |
980 | ||
96a5add6 | 981 | PERL_UNUSED_CONTEXT; |
8850bf83 JH |
982 | /* Note: cannot use UTF8_IS_...() too eagerly here since e.g |
983 | * the bitops (especially ~) can create illegal UTF-8. | |
984 | * In other words: in Perl UTF-8 is not just for Unicode. */ | |
985 | ||
a0ed51b3 LW |
986 | if (off >= 0) { |
987 | while (off--) | |
988 | s += UTF8SKIP(s); | |
989 | } | |
990 | else { | |
991 | while (off++) { | |
992 | s--; | |
8850bf83 JH |
993 | while (UTF8_IS_CONTINUATION(*s)) |
994 | s--; | |
a0ed51b3 LW |
995 | } |
996 | } | |
4373e329 | 997 | return (U8 *)s; |
a0ed51b3 LW |
998 | } |
999 | ||
6940069f | 1000 | /* |
fed3ba5d NC |
1001 | =for apidoc bytes_cmp_utf8 |
1002 | ||
a1433954 KW |
1003 | Compares the sequence of characters (stored as octets) in C<b>, C<blen> with the |
1004 | sequence of characters (stored as UTF-8) in C<u>, C<ulen>. Returns 0 if they are | |
fed3ba5d NC |
1005 | equal, -1 or -2 if the first string is less than the second string, +1 or +2 |
1006 | if the first string is greater than the second string. | |
1007 | ||
1008 | -1 or +1 is returned if the shorter string was identical to the start of the | |
1009 | longer string. -2 or +2 is returned if the was a difference between characters | |
1010 | within the strings. | |
1011 | ||
1012 | =cut | |
1013 | */ | |
1014 | ||
1015 | int | |
1016 | Perl_bytes_cmp_utf8(pTHX_ const U8 *b, STRLEN blen, const U8 *u, STRLEN ulen) | |
1017 | { | |
1018 | const U8 *const bend = b + blen; | |
1019 | const U8 *const uend = u + ulen; | |
1020 | ||
1021 | PERL_ARGS_ASSERT_BYTES_CMP_UTF8; | |
1022 | ||
1023 | PERL_UNUSED_CONTEXT; | |
1024 | ||
1025 | while (b < bend && u < uend) { | |
1026 | U8 c = *u++; | |
1027 | if (!UTF8_IS_INVARIANT(c)) { | |
1028 | if (UTF8_IS_DOWNGRADEABLE_START(c)) { | |
1029 | if (u < uend) { | |
1030 | U8 c1 = *u++; | |
1031 | if (UTF8_IS_CONTINUATION(c1)) { | |
356979f4 | 1032 | c = UNI_TO_NATIVE(TWO_BYTE_UTF8_TO_UNI(c, c1)); |
fed3ba5d NC |
1033 | } else { |
1034 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), | |
1035 | "Malformed UTF-8 character " | |
1036 | "(unexpected non-continuation byte 0x%02x" | |
1037 | ", immediately after start byte 0x%02x)" | |
1038 | /* Dear diag.t, it's in the pod. */ | |
1039 | "%s%s", c1, c, | |
1040 | PL_op ? " in " : "", | |
1041 | PL_op ? OP_DESC(PL_op) : ""); | |
1042 | return -2; | |
1043 | } | |
1044 | } else { | |
1045 | if (PL_op) | |
1046 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), | |
1047 | "%s in %s", unees, OP_DESC(PL_op)); | |
1048 | else | |
61a12c31 | 1049 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), "%s", unees); |
fed3ba5d NC |
1050 | return -2; /* Really want to return undef :-) */ |
1051 | } | |
1052 | } else { | |
1053 | return -2; | |
1054 | } | |
1055 | } | |
1056 | if (*b != c) { | |
1057 | return *b < c ? -2 : +2; | |
1058 | } | |
1059 | ++b; | |
1060 | } | |
1061 | ||
1062 | if (b == bend && u == uend) | |
1063 | return 0; | |
1064 | ||
1065 | return b < bend ? +1 : -1; | |
1066 | } | |
1067 | ||
1068 | /* | |
87cea99e | 1069 | =for apidoc utf8_to_bytes |
6940069f | 1070 | |
2bbc8d55 | 1071 | Converts a string C<s> of length C<len> from UTF-8 into native byte encoding. |
a1433954 KW |
1072 | Unlike L</bytes_to_utf8>, this over-writes the original string, and |
1073 | updates C<len> to contain the new length. | |
67e989fb | 1074 | Returns zero on failure, setting C<len> to -1. |
6940069f | 1075 | |
a1433954 | 1076 | If you need a copy of the string, see L</bytes_from_utf8>. |
95be277c | 1077 | |
6940069f GS |
1078 | =cut |
1079 | */ | |
1080 | ||
1081 | U8 * | |
37607a96 | 1082 | Perl_utf8_to_bytes(pTHX_ U8 *s, STRLEN *len) |
6940069f | 1083 | { |
d4c19fe8 AL |
1084 | U8 * const save = s; |
1085 | U8 * const send = s + *len; | |
6940069f | 1086 | U8 *d; |
246fae53 | 1087 | |
7918f24d NC |
1088 | PERL_ARGS_ASSERT_UTF8_TO_BYTES; |
1089 | ||
1e54db1a | 1090 | /* ensure valid UTF-8 and chars < 256 before updating string */ |
d4c19fe8 | 1091 | while (s < send) { |
dcad2880 JH |
1092 | U8 c = *s++; |
1093 | ||
1d72bdf6 NIS |
1094 | if (!UTF8_IS_INVARIANT(c) && |
1095 | (!UTF8_IS_DOWNGRADEABLE_START(c) || (s >= send) | |
1096 | || !(c = *s++) || !UTF8_IS_CONTINUATION(c))) { | |
10edeb5d | 1097 | *len = ((STRLEN) -1); |
dcad2880 JH |
1098 | return 0; |
1099 | } | |
246fae53 | 1100 | } |
dcad2880 JH |
1101 | |
1102 | d = s = save; | |
6940069f | 1103 | while (s < send) { |
ed646e6e | 1104 | STRLEN ulen; |
9041c2e3 | 1105 | *d++ = (U8)utf8_to_uvchr(s, &ulen); |
ed646e6e | 1106 | s += ulen; |
6940069f GS |
1107 | } |
1108 | *d = '\0'; | |
246fae53 | 1109 | *len = d - save; |
6940069f GS |
1110 | return save; |
1111 | } | |
1112 | ||
1113 | /* | |
87cea99e | 1114 | =for apidoc bytes_from_utf8 |
f9a63242 | 1115 | |
2bbc8d55 | 1116 | Converts a string C<s> of length C<len> from UTF-8 into native byte encoding. |
a1433954 | 1117 | Unlike L</utf8_to_bytes> but like L</bytes_to_utf8>, returns a pointer to |
ef9edfd0 JH |
1118 | the newly-created string, and updates C<len> to contain the new |
1119 | length. Returns the original string if no conversion occurs, C<len> | |
1120 | is unchanged. Do nothing if C<is_utf8> points to 0. Sets C<is_utf8> to | |
2bbc8d55 SP |
1121 | 0 if C<s> is converted or consisted entirely of characters that are invariant |
1122 | in utf8 (i.e., US-ASCII on non-EBCDIC machines). | |
f9a63242 | 1123 | |
37607a96 PK |
1124 | =cut |
1125 | */ | |
f9a63242 JH |
1126 | |
1127 | U8 * | |
e1ec3a88 | 1128 | Perl_bytes_from_utf8(pTHX_ const U8 *s, STRLEN *len, bool *is_utf8) |
f9a63242 | 1129 | { |
f9a63242 | 1130 | U8 *d; |
e1ec3a88 AL |
1131 | const U8 *start = s; |
1132 | const U8 *send; | |
f9a63242 JH |
1133 | I32 count = 0; |
1134 | ||
7918f24d NC |
1135 | PERL_ARGS_ASSERT_BYTES_FROM_UTF8; |
1136 | ||
96a5add6 | 1137 | PERL_UNUSED_CONTEXT; |
f9a63242 | 1138 | if (!*is_utf8) |
73d840c0 | 1139 | return (U8 *)start; |
f9a63242 | 1140 | |
1e54db1a | 1141 | /* ensure valid UTF-8 and chars < 256 before converting string */ |
f9a63242 | 1142 | for (send = s + *len; s < send;) { |
e1ec3a88 | 1143 | U8 c = *s++; |
1d72bdf6 | 1144 | if (!UTF8_IS_INVARIANT(c)) { |
db42d148 NIS |
1145 | if (UTF8_IS_DOWNGRADEABLE_START(c) && s < send && |
1146 | (c = *s++) && UTF8_IS_CONTINUATION(c)) | |
1147 | count++; | |
1148 | else | |
73d840c0 | 1149 | return (U8 *)start; |
db42d148 | 1150 | } |
f9a63242 JH |
1151 | } |
1152 | ||
35da51f7 | 1153 | *is_utf8 = FALSE; |
f9a63242 | 1154 | |
212542aa | 1155 | Newx(d, (*len) - count + 1, U8); |
ef9edfd0 | 1156 | s = start; start = d; |
f9a63242 JH |
1157 | while (s < send) { |
1158 | U8 c = *s++; | |
c4d5f83a NIS |
1159 | if (!UTF8_IS_INVARIANT(c)) { |
1160 | /* Then it is two-byte encoded */ | |
356979f4 | 1161 | c = UNI_TO_NATIVE(TWO_BYTE_UTF8_TO_UNI(c, *s++)); |
c4d5f83a NIS |
1162 | } |
1163 | *d++ = c; | |
f9a63242 JH |
1164 | } |
1165 | *d = '\0'; | |
1166 | *len = d - start; | |
73d840c0 | 1167 | return (U8 *)start; |
f9a63242 JH |
1168 | } |
1169 | ||
1170 | /* | |
87cea99e | 1171 | =for apidoc bytes_to_utf8 |
6940069f | 1172 | |
ff97e5cf KW |
1173 | Converts a string C<s> of length C<len> bytes from the native encoding into |
1174 | UTF-8. | |
6662521e | 1175 | Returns a pointer to the newly-created string, and sets C<len> to |
ff97e5cf | 1176 | reflect the new length in bytes. |
6940069f | 1177 | |
2bbc8d55 SP |
1178 | A NUL character will be written after the end of the string. |
1179 | ||
1180 | If you want to convert to UTF-8 from encodings other than | |
1181 | the native (Latin1 or EBCDIC), | |
a1433954 | 1182 | see L</sv_recode_to_utf8>(). |
c9ada85f | 1183 | |
497711e7 | 1184 | =cut |
6940069f GS |
1185 | */ |
1186 | ||
c682ebef FC |
1187 | /* This logic is duplicated in sv_catpvn_flags, so any bug fixes will |
1188 | likewise need duplication. */ | |
1189 | ||
6940069f | 1190 | U8* |
35a4481c | 1191 | Perl_bytes_to_utf8(pTHX_ const U8 *s, STRLEN *len) |
6940069f | 1192 | { |
35a4481c | 1193 | const U8 * const send = s + (*len); |
6940069f GS |
1194 | U8 *d; |
1195 | U8 *dst; | |
7918f24d NC |
1196 | |
1197 | PERL_ARGS_ASSERT_BYTES_TO_UTF8; | |
96a5add6 | 1198 | PERL_UNUSED_CONTEXT; |
6940069f | 1199 | |
212542aa | 1200 | Newx(d, (*len) * 2 + 1, U8); |
6940069f GS |
1201 | dst = d; |
1202 | ||
1203 | while (s < send) { | |
35a4481c | 1204 | const UV uv = NATIVE_TO_ASCII(*s++); |
c4d5f83a | 1205 | if (UNI_IS_INVARIANT(uv)) |
eb160463 | 1206 | *d++ = (U8)UTF_TO_NATIVE(uv); |
6940069f | 1207 | else { |
eb160463 GS |
1208 | *d++ = (U8)UTF8_EIGHT_BIT_HI(uv); |
1209 | *d++ = (U8)UTF8_EIGHT_BIT_LO(uv); | |
6940069f GS |
1210 | } |
1211 | } | |
1212 | *d = '\0'; | |
6662521e | 1213 | *len = d-dst; |
6940069f GS |
1214 | return dst; |
1215 | } | |
1216 | ||
a0ed51b3 | 1217 | /* |
dea0fc0b | 1218 | * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8. |
a0ed51b3 LW |
1219 | * |
1220 | * Destination must be pre-extended to 3/2 source. Do not use in-place. | |
1221 | * We optimize for native, for obvious reasons. */ | |
1222 | ||
1223 | U8* | |
dea0fc0b | 1224 | Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen) |
a0ed51b3 | 1225 | { |
dea0fc0b JH |
1226 | U8* pend; |
1227 | U8* dstart = d; | |
1228 | ||
7918f24d NC |
1229 | PERL_ARGS_ASSERT_UTF16_TO_UTF8; |
1230 | ||
dea0fc0b | 1231 | if (bytelen & 1) |
f5992bc4 | 1232 | Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen %"UVuf, (UV)bytelen); |
dea0fc0b JH |
1233 | |
1234 | pend = p + bytelen; | |
1235 | ||
a0ed51b3 | 1236 | while (p < pend) { |
dea0fc0b JH |
1237 | UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */ |
1238 | p += 2; | |
a0ed51b3 | 1239 | if (uv < 0x80) { |
e294cc5d JH |
1240 | #ifdef EBCDIC |
1241 | *d++ = UNI_TO_NATIVE(uv); | |
1242 | #else | |
eb160463 | 1243 | *d++ = (U8)uv; |
e294cc5d | 1244 | #endif |
a0ed51b3 LW |
1245 | continue; |
1246 | } | |
1247 | if (uv < 0x800) { | |
eb160463 GS |
1248 | *d++ = (U8)(( uv >> 6) | 0xc0); |
1249 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
1250 | continue; |
1251 | } | |
52b9aa85 | 1252 | if (uv >= 0xd800 && uv <= 0xdbff) { /* surrogates */ |
01ea242b | 1253 | if (p >= pend) { |
dea0fc0b | 1254 | Perl_croak(aTHX_ "Malformed UTF-16 surrogate"); |
01ea242b NC |
1255 | } else { |
1256 | UV low = (p[0] << 8) + p[1]; | |
1257 | p += 2; | |
52b9aa85 | 1258 | if (low < 0xdc00 || low > 0xdfff) |
01ea242b NC |
1259 | Perl_croak(aTHX_ "Malformed UTF-16 surrogate"); |
1260 | uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000; | |
1261 | } | |
dbde1951 NC |
1262 | } else if (uv >= 0xdc00 && uv <= 0xdfff) { |
1263 | Perl_croak(aTHX_ "Malformed UTF-16 surrogate"); | |
a0ed51b3 LW |
1264 | } |
1265 | if (uv < 0x10000) { | |
eb160463 GS |
1266 | *d++ = (U8)(( uv >> 12) | 0xe0); |
1267 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
1268 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
1269 | continue; |
1270 | } | |
1271 | else { | |
eb160463 GS |
1272 | *d++ = (U8)(( uv >> 18) | 0xf0); |
1273 | *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80); | |
1274 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
1275 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
1276 | continue; |
1277 | } | |
1278 | } | |
dea0fc0b | 1279 | *newlen = d - dstart; |
a0ed51b3 LW |
1280 | return d; |
1281 | } | |
1282 | ||
1283 | /* Note: this one is slightly destructive of the source. */ | |
1284 | ||
1285 | U8* | |
dea0fc0b | 1286 | Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen) |
a0ed51b3 LW |
1287 | { |
1288 | U8* s = (U8*)p; | |
d4c19fe8 | 1289 | U8* const send = s + bytelen; |
7918f24d NC |
1290 | |
1291 | PERL_ARGS_ASSERT_UTF16_TO_UTF8_REVERSED; | |
1292 | ||
e0ea5e2d NC |
1293 | if (bytelen & 1) |
1294 | Perl_croak(aTHX_ "panic: utf16_to_utf8_reversed: odd bytelen %"UVuf, | |
1295 | (UV)bytelen); | |
1296 | ||
a0ed51b3 | 1297 | while (s < send) { |
d4c19fe8 | 1298 | const U8 tmp = s[0]; |
a0ed51b3 LW |
1299 | s[0] = s[1]; |
1300 | s[1] = tmp; | |
1301 | s += 2; | |
1302 | } | |
dea0fc0b | 1303 | return utf16_to_utf8(p, d, bytelen, newlen); |
a0ed51b3 LW |
1304 | } |
1305 | ||
c3fd2246 KW |
1306 | /* for now these are all defined (inefficiently) in terms of the utf8 versions. |
1307 | * Note that the macros in handy.h that call these short-circuit calling them | |
1308 | * for Latin-1 range inputs */ | |
a0ed51b3 LW |
1309 | |
1310 | bool | |
84afefe6 | 1311 | Perl_is_uni_alnum(pTHX_ UV c) |
a0ed51b3 | 1312 | { |
89ebb4a3 | 1313 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1314 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1315 | return is_utf8_alnum(tmpbuf); |
1316 | } | |
1317 | ||
1318 | bool | |
84afefe6 | 1319 | Perl_is_uni_idfirst(pTHX_ UV c) |
a0ed51b3 | 1320 | { |
89ebb4a3 | 1321 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1322 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1323 | return is_utf8_idfirst(tmpbuf); |
1324 | } | |
1325 | ||
1326 | bool | |
84afefe6 | 1327 | Perl_is_uni_alpha(pTHX_ UV c) |
a0ed51b3 | 1328 | { |
89ebb4a3 | 1329 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1330 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1331 | return is_utf8_alpha(tmpbuf); |
1332 | } | |
1333 | ||
1334 | bool | |
84afefe6 | 1335 | Perl_is_uni_ascii(pTHX_ UV c) |
4d61ec05 | 1336 | { |
bc39fe24 | 1337 | return isASCII(c); |
4d61ec05 GS |
1338 | } |
1339 | ||
1340 | bool | |
84afefe6 | 1341 | Perl_is_uni_space(pTHX_ UV c) |
a0ed51b3 | 1342 | { |
89ebb4a3 | 1343 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1344 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1345 | return is_utf8_space(tmpbuf); |
1346 | } | |
1347 | ||
1348 | bool | |
84afefe6 | 1349 | Perl_is_uni_digit(pTHX_ UV c) |
a0ed51b3 | 1350 | { |
89ebb4a3 | 1351 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1352 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1353 | return is_utf8_digit(tmpbuf); |
1354 | } | |
1355 | ||
1356 | bool | |
84afefe6 | 1357 | Perl_is_uni_upper(pTHX_ UV c) |
a0ed51b3 | 1358 | { |
89ebb4a3 | 1359 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1360 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1361 | return is_utf8_upper(tmpbuf); |
1362 | } | |
1363 | ||
1364 | bool | |
84afefe6 | 1365 | Perl_is_uni_lower(pTHX_ UV c) |
a0ed51b3 | 1366 | { |
89ebb4a3 | 1367 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1368 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1369 | return is_utf8_lower(tmpbuf); |
1370 | } | |
1371 | ||
1372 | bool | |
84afefe6 | 1373 | Perl_is_uni_cntrl(pTHX_ UV c) |
b8c5462f | 1374 | { |
7b952154 | 1375 | return isCNTRL_L1(c); |
b8c5462f JH |
1376 | } |
1377 | ||
1378 | bool | |
84afefe6 | 1379 | Perl_is_uni_graph(pTHX_ UV c) |
b8c5462f | 1380 | { |
89ebb4a3 | 1381 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1382 | uvchr_to_utf8(tmpbuf, c); |
b8c5462f JH |
1383 | return is_utf8_graph(tmpbuf); |
1384 | } | |
1385 | ||
1386 | bool | |
84afefe6 | 1387 | Perl_is_uni_print(pTHX_ UV c) |
a0ed51b3 | 1388 | { |
89ebb4a3 | 1389 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1390 | uvchr_to_utf8(tmpbuf, c); |
a0ed51b3 LW |
1391 | return is_utf8_print(tmpbuf); |
1392 | } | |
1393 | ||
b8c5462f | 1394 | bool |
84afefe6 | 1395 | Perl_is_uni_punct(pTHX_ UV c) |
b8c5462f | 1396 | { |
89ebb4a3 | 1397 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
230880c1 | 1398 | uvchr_to_utf8(tmpbuf, c); |
b8c5462f JH |
1399 | return is_utf8_punct(tmpbuf); |
1400 | } | |
1401 | ||
4d61ec05 | 1402 | bool |
84afefe6 | 1403 | Perl_is_uni_xdigit(pTHX_ UV c) |
4d61ec05 | 1404 | { |
89ebb4a3 | 1405 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
230880c1 | 1406 | uvchr_to_utf8(tmpbuf, c); |
4d61ec05 GS |
1407 | return is_utf8_xdigit(tmpbuf); |
1408 | } | |
1409 | ||
3a4c58c9 KW |
1410 | UV |
1411 | Perl__to_upper_title_latin1(pTHX_ const U8 c, U8* p, STRLEN *lenp, const char S_or_s) | |
1412 | { | |
1413 | /* We have the latin1-range values compiled into the core, so just use | |
1414 | * those, converting the result to utf8. The only difference between upper | |
1415 | * and title case in this range is that LATIN_SMALL_LETTER_SHARP_S is | |
1416 | * either "SS" or "Ss". Which one to use is passed into the routine in | |
1417 | * 'S_or_s' to avoid a test */ | |
1418 | ||
1419 | UV converted = toUPPER_LATIN1_MOD(c); | |
1420 | ||
1421 | PERL_ARGS_ASSERT__TO_UPPER_TITLE_LATIN1; | |
1422 | ||
1423 | assert(S_or_s == 'S' || S_or_s == 's'); | |
1424 | ||
1425 | if (UNI_IS_INVARIANT(converted)) { /* No difference between the two for | |
1426 | characters in this range */ | |
1427 | *p = (U8) converted; | |
1428 | *lenp = 1; | |
1429 | return converted; | |
1430 | } | |
1431 | ||
1432 | /* toUPPER_LATIN1_MOD gives the correct results except for three outliers, | |
1433 | * which it maps to one of them, so as to only have to have one check for | |
1434 | * it in the main case */ | |
1435 | if (UNLIKELY(converted == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) { | |
1436 | switch (c) { | |
1437 | case LATIN_SMALL_LETTER_Y_WITH_DIAERESIS: | |
1438 | converted = LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS; | |
1439 | break; | |
1440 | case MICRO_SIGN: | |
1441 | converted = GREEK_CAPITAL_LETTER_MU; | |
1442 | break; | |
1443 | case LATIN_SMALL_LETTER_SHARP_S: | |
1444 | *(p)++ = 'S'; | |
1445 | *p = S_or_s; | |
1446 | *lenp = 2; | |
1447 | return 'S'; | |
1448 | default: | |
1449 | Perl_croak(aTHX_ "panic: to_upper_title_latin1 did not expect '%c' to map to '%c'", c, LATIN_SMALL_LETTER_Y_WITH_DIAERESIS); | |
1450 | /* NOTREACHED */ | |
1451 | } | |
1452 | } | |
1453 | ||
1454 | *(p)++ = UTF8_TWO_BYTE_HI(converted); | |
1455 | *p = UTF8_TWO_BYTE_LO(converted); | |
1456 | *lenp = 2; | |
1457 | ||
1458 | return converted; | |
1459 | } | |
1460 | ||
50bda2c3 KW |
1461 | /* Call the function to convert a UTF-8 encoded character to the specified case. |
1462 | * Note that there may be more than one character in the result. | |
1463 | * INP is a pointer to the first byte of the input character | |
1464 | * OUTP will be set to the first byte of the string of changed characters. It | |
1465 | * needs to have space for UTF8_MAXBYTES_CASE+1 bytes | |
1466 | * LENP will be set to the length in bytes of the string of changed characters | |
1467 | * | |
1468 | * The functions return the ordinal of the first character in the string of OUTP */ | |
f90a9a02 KW |
1469 | #define CALL_UPPER_CASE(INP, OUTP, LENP) Perl_to_utf8_case(aTHX_ INP, OUTP, LENP, &PL_utf8_toupper, "ToUc", "utf8::ToSpecUc") |
1470 | #define CALL_TITLE_CASE(INP, OUTP, LENP) Perl_to_utf8_case(aTHX_ INP, OUTP, LENP, &PL_utf8_totitle, "ToTc", "utf8::ToSpecTc") | |
1471 | #define CALL_LOWER_CASE(INP, OUTP, LENP) Perl_to_utf8_case(aTHX_ INP, OUTP, LENP, &PL_utf8_tolower, "ToLc", "utf8::ToSpecLc") | |
50bda2c3 KW |
1472 | |
1473 | /* This additionally has the input parameter SPECIALS, which if non-zero will | |
1474 | * cause this to use the SPECIALS hash for folding (meaning get full case | |
1475 | * folding); otherwise, when zero, this implies a simple case fold */ | |
f90a9a02 | 1476 | #define CALL_FOLD_CASE(INP, OUTP, LENP, SPECIALS) Perl_to_utf8_case(aTHX_ INP, OUTP, LENP, &PL_utf8_tofold, "ToCf", (SPECIALS) ? "utf8::ToSpecCf" : NULL) |
c3fd2246 | 1477 | |
84afefe6 JH |
1478 | UV |
1479 | Perl_to_uni_upper(pTHX_ UV c, U8* p, STRLEN *lenp) | |
a0ed51b3 | 1480 | { |
3a4c58c9 KW |
1481 | dVAR; |
1482 | ||
a1433954 KW |
1483 | /* Convert the Unicode character whose ordinal is <c> to its uppercase |
1484 | * version and store that in UTF-8 in <p> and its length in bytes in <lenp>. | |
1485 | * Note that the <p> needs to be at least UTF8_MAXBYTES_CASE+1 bytes since | |
c3fd2246 KW |
1486 | * the changed version may be longer than the original character. |
1487 | * | |
1488 | * The ordinal of the first character of the changed version is returned | |
1489 | * (but note, as explained above, that there may be more.) */ | |
1490 | ||
7918f24d NC |
1491 | PERL_ARGS_ASSERT_TO_UNI_UPPER; |
1492 | ||
3a4c58c9 KW |
1493 | if (c < 256) { |
1494 | return _to_upper_title_latin1((U8) c, p, lenp, 'S'); | |
1495 | } | |
1496 | ||
0ebc6274 | 1497 | uvchr_to_utf8(p, c); |
3a4c58c9 | 1498 | return CALL_UPPER_CASE(p, p, lenp); |
a0ed51b3 LW |
1499 | } |
1500 | ||
84afefe6 JH |
1501 | UV |
1502 | Perl_to_uni_title(pTHX_ UV c, U8* p, STRLEN *lenp) | |
a0ed51b3 | 1503 | { |
3a4c58c9 KW |
1504 | dVAR; |
1505 | ||
7918f24d NC |
1506 | PERL_ARGS_ASSERT_TO_UNI_TITLE; |
1507 | ||
3a4c58c9 KW |
1508 | if (c < 256) { |
1509 | return _to_upper_title_latin1((U8) c, p, lenp, 's'); | |
1510 | } | |
1511 | ||
0ebc6274 | 1512 | uvchr_to_utf8(p, c); |
3a4c58c9 | 1513 | return CALL_TITLE_CASE(p, p, lenp); |
a0ed51b3 LW |
1514 | } |
1515 | ||
afc16117 KW |
1516 | STATIC U8 |
1517 | S_to_lower_latin1(pTHX_ const U8 c, U8* p, STRLEN *lenp) | |
1518 | { | |
1519 | /* We have the latin1-range values compiled into the core, so just use | |
1520 | * those, converting the result to utf8. Since the result is always just | |
a1433954 | 1521 | * one character, we allow <p> to be NULL */ |
afc16117 KW |
1522 | |
1523 | U8 converted = toLOWER_LATIN1(c); | |
1524 | ||
1525 | if (p != NULL) { | |
1526 | if (UNI_IS_INVARIANT(converted)) { | |
1527 | *p = converted; | |
1528 | *lenp = 1; | |
1529 | } | |
1530 | else { | |
1531 | *p = UTF8_TWO_BYTE_HI(converted); | |
1532 | *(p+1) = UTF8_TWO_BYTE_LO(converted); | |
1533 | *lenp = 2; | |
1534 | } | |
1535 | } | |
1536 | return converted; | |
1537 | } | |
1538 | ||
84afefe6 JH |
1539 | UV |
1540 | Perl_to_uni_lower(pTHX_ UV c, U8* p, STRLEN *lenp) | |
a0ed51b3 | 1541 | { |
968c5e6a KW |
1542 | dVAR; |
1543 | ||
7918f24d NC |
1544 | PERL_ARGS_ASSERT_TO_UNI_LOWER; |
1545 | ||
afc16117 KW |
1546 | if (c < 256) { |
1547 | return to_lower_latin1((U8) c, p, lenp); | |
bca00c02 KW |
1548 | } |
1549 | ||
afc16117 | 1550 | uvchr_to_utf8(p, c); |
968c5e6a | 1551 | return CALL_LOWER_CASE(p, p, lenp); |
a0ed51b3 LW |
1552 | } |
1553 | ||
84afefe6 | 1554 | UV |
f673fad4 | 1555 | Perl__to_fold_latin1(pTHX_ const U8 c, U8* p, STRLEN *lenp, const bool flags) |
a1dde8de | 1556 | { |
a1433954 | 1557 | /* Corresponds to to_lower_latin1(), <flags> is TRUE if to use full case |
f673fad4 KW |
1558 | * folding */ |
1559 | ||
a1dde8de KW |
1560 | UV converted; |
1561 | ||
1562 | PERL_ARGS_ASSERT__TO_FOLD_LATIN1; | |
1563 | ||
1564 | if (c == MICRO_SIGN) { | |
1565 | converted = GREEK_SMALL_LETTER_MU; | |
1566 | } | |
1567 | else if (flags && c == LATIN_SMALL_LETTER_SHARP_S) { | |
1568 | *(p)++ = 's'; | |
1569 | *p = 's'; | |
1570 | *lenp = 2; | |
1571 | return 's'; | |
1572 | } | |
1573 | else { /* In this range the fold of all other characters is their lower | |
1574 | case */ | |
1575 | converted = toLOWER_LATIN1(c); | |
1576 | } | |
1577 | ||
1578 | if (UNI_IS_INVARIANT(converted)) { | |
1579 | *p = (U8) converted; | |
1580 | *lenp = 1; | |
1581 | } | |
1582 | else { | |
1583 | *(p)++ = UTF8_TWO_BYTE_HI(converted); | |
1584 | *p = UTF8_TWO_BYTE_LO(converted); | |
1585 | *lenp = 2; | |
1586 | } | |
1587 | ||
1588 | return converted; | |
1589 | } | |
1590 | ||
1591 | UV | |
f673fad4 | 1592 | Perl__to_uni_fold_flags(pTHX_ UV c, U8* p, STRLEN *lenp, const bool flags) |
84afefe6 | 1593 | { |
4b593389 KW |
1594 | |
1595 | /* Not currently externally documented, and subject to change, <flags> is | |
f673fad4 | 1596 | * TRUE iff full folding is to be used */ |
4b593389 | 1597 | |
36bb2ab6 | 1598 | PERL_ARGS_ASSERT__TO_UNI_FOLD_FLAGS; |
7918f24d | 1599 | |
a1dde8de KW |
1600 | if (c < 256) { |
1601 | return _to_fold_latin1((U8) c, p, lenp, flags); | |
1602 | } | |
1603 | ||
0ebc6274 | 1604 | uvchr_to_utf8(p, c); |
a1dde8de | 1605 | return CALL_FOLD_CASE(p, p, lenp, flags); |
84afefe6 JH |
1606 | } |
1607 | ||
ea317ccb KW |
1608 | /* for now these all assume no locale info available for Unicode > 255; and |
1609 | * the corresponding macros in handy.h (like isALNUM_LC_uvchr) should have been | |
1610 | * called instead, so that these don't get called for < 255 */ | |
a0ed51b3 LW |
1611 | |
1612 | bool | |
84afefe6 | 1613 | Perl_is_uni_alnum_lc(pTHX_ UV c) |
a0ed51b3 LW |
1614 | { |
1615 | return is_uni_alnum(c); /* XXX no locale support yet */ | |
1616 | } | |
1617 | ||
1618 | bool | |
84afefe6 | 1619 | Perl_is_uni_idfirst_lc(pTHX_ UV c) |
a0ed51b3 LW |
1620 | { |
1621 | return is_uni_idfirst(c); /* XXX no locale support yet */ | |
1622 | } | |
1623 | ||
1624 | bool | |
84afefe6 | 1625 | Perl_is_uni_alpha_lc(pTHX_ UV c) |
a0ed51b3 LW |
1626 | { |
1627 | return is_uni_alpha(c); /* XXX no locale support yet */ | |
1628 | } | |
1629 | ||
1630 | bool | |
84afefe6 | 1631 | Perl_is_uni_ascii_lc(pTHX_ UV c) |
4d61ec05 GS |
1632 | { |
1633 | return is_uni_ascii(c); /* XXX no locale support yet */ | |
1634 | } | |
1635 | ||
1636 | bool | |
84afefe6 | 1637 | Perl_is_uni_space_lc(pTHX_ UV c) |
a0ed51b3 LW |
1638 | { |
1639 | return is_uni_space(c); /* XXX no locale support yet */ | |
1640 | } | |
1641 | ||
1642 | bool | |
84afefe6 | 1643 | Perl_is_uni_digit_lc(pTHX_ UV c) |
a0ed51b3 LW |
1644 | { |
1645 | return is_uni_digit(c); /* XXX no locale support yet */ | |
1646 | } | |
1647 | ||
1648 | bool | |
84afefe6 | 1649 | Perl_is_uni_upper_lc(pTHX_ UV c) |
a0ed51b3 LW |
1650 | { |
1651 | return is_uni_upper(c); /* XXX no locale support yet */ | |
1652 | } | |
1653 | ||
1654 | bool | |
84afefe6 | 1655 | Perl_is_uni_lower_lc(pTHX_ UV c) |
a0ed51b3 LW |
1656 | { |
1657 | return is_uni_lower(c); /* XXX no locale support yet */ | |
1658 | } | |
1659 | ||
1660 | bool | |
84afefe6 | 1661 | Perl_is_uni_cntrl_lc(pTHX_ UV c) |
b8c5462f JH |
1662 | { |
1663 | return is_uni_cntrl(c); /* XXX no locale support yet */ | |
1664 | } | |
1665 | ||
1666 | bool | |
84afefe6 | 1667 | Perl_is_uni_graph_lc(pTHX_ UV c) |
b8c5462f JH |
1668 | { |
1669 | return is_uni_graph(c); /* XXX no locale support yet */ | |
1670 | } | |
1671 | ||
1672 | bool | |
84afefe6 | 1673 | Perl_is_uni_print_lc(pTHX_ UV c) |
a0ed51b3 LW |
1674 | { |
1675 | return is_uni_print(c); /* XXX no locale support yet */ | |
1676 | } | |
1677 | ||
b8c5462f | 1678 | bool |
84afefe6 | 1679 | Perl_is_uni_punct_lc(pTHX_ UV c) |
b8c5462f JH |
1680 | { |
1681 | return is_uni_punct(c); /* XXX no locale support yet */ | |
1682 | } | |
1683 | ||
4d61ec05 | 1684 | bool |
84afefe6 | 1685 | Perl_is_uni_xdigit_lc(pTHX_ UV c) |
4d61ec05 GS |
1686 | { |
1687 | return is_uni_xdigit(c); /* XXX no locale support yet */ | |
1688 | } | |
1689 | ||
b7ac61fa JH |
1690 | U32 |
1691 | Perl_to_uni_upper_lc(pTHX_ U32 c) | |
1692 | { | |
ee099d14 JH |
1693 | /* XXX returns only the first character -- do not use XXX */ |
1694 | /* XXX no locale support yet */ | |
1695 | STRLEN len; | |
89ebb4a3 | 1696 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
ee099d14 | 1697 | return (U32)to_uni_upper(c, tmpbuf, &len); |
b7ac61fa JH |
1698 | } |
1699 | ||
1700 | U32 | |
1701 | Perl_to_uni_title_lc(pTHX_ U32 c) | |
1702 | { | |
ee099d14 JH |
1703 | /* XXX returns only the first character XXX -- do not use XXX */ |
1704 | /* XXX no locale support yet */ | |
1705 | STRLEN len; | |
89ebb4a3 | 1706 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
ee099d14 | 1707 | return (U32)to_uni_title(c, tmpbuf, &len); |
b7ac61fa JH |
1708 | } |
1709 | ||
1710 | U32 | |
1711 | Perl_to_uni_lower_lc(pTHX_ U32 c) | |
1712 | { | |
ee099d14 JH |
1713 | /* XXX returns only the first character -- do not use XXX */ |
1714 | /* XXX no locale support yet */ | |
1715 | STRLEN len; | |
89ebb4a3 | 1716 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
ee099d14 | 1717 | return (U32)to_uni_lower(c, tmpbuf, &len); |
b7ac61fa JH |
1718 | } |
1719 | ||
7452cf6a | 1720 | static bool |
5141f98e | 1721 | S_is_utf8_common(pTHX_ const U8 *const p, SV **swash, |
bde6a22d NC |
1722 | const char *const swashname) |
1723 | { | |
ea317ccb KW |
1724 | /* returns a boolean giving whether or not the UTF8-encoded character that |
1725 | * starts at <p> is in the swash indicated by <swashname>. <swash> | |
1726 | * contains a pointer to where the swash indicated by <swashname> | |
1727 | * is to be stored; which this routine will do, so that future calls will | |
1728 | * look at <*swash> and only generate a swash if it is not null | |
1729 | * | |
1730 | * Note that it is assumed that the buffer length of <p> is enough to | |
1731 | * contain all the bytes that comprise the character. Thus, <*p> should | |
1732 | * have been checked before this call for mal-formedness enough to assure | |
1733 | * that. */ | |
1734 | ||
97aff369 | 1735 | dVAR; |
7918f24d NC |
1736 | |
1737 | PERL_ARGS_ASSERT_IS_UTF8_COMMON; | |
1738 | ||
492a624f KW |
1739 | /* The API should have included a length for the UTF-8 character in <p>, |
1740 | * but it doesn't. We therefor assume that p has been validated at least | |
1741 | * as far as there being enough bytes available in it to accommodate the | |
1742 | * character without reading beyond the end, and pass that number on to the | |
1743 | * validating routine */ | |
1744 | if (!is_utf8_char_buf(p, p + UTF8SKIP(p))) | |
bde6a22d NC |
1745 | return FALSE; |
1746 | if (!*swash) | |
711a919c | 1747 | *swash = swash_init("utf8", swashname, &PL_sv_undef, 1, 0); |
bde6a22d NC |
1748 | return swash_fetch(*swash, p, TRUE) != 0; |
1749 | } | |
1750 | ||
1751 | bool | |
7fc63493 | 1752 | Perl_is_utf8_alnum(pTHX_ const U8 *p) |
a0ed51b3 | 1753 | { |
97aff369 | 1754 | dVAR; |
7918f24d NC |
1755 | |
1756 | PERL_ARGS_ASSERT_IS_UTF8_ALNUM; | |
1757 | ||
671c33bf NC |
1758 | /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true |
1759 | * descendant of isalnum(3), in other words, it doesn't | |
1760 | * contain the '_'. --jhi */ | |
d4c19fe8 | 1761 | return is_utf8_common(p, &PL_utf8_alnum, "IsWord"); |
a0ed51b3 LW |
1762 | } |
1763 | ||
1764 | bool | |
7fc63493 | 1765 | Perl_is_utf8_idfirst(pTHX_ const U8 *p) /* The naming is historical. */ |
a0ed51b3 | 1766 | { |
97aff369 | 1767 | dVAR; |
7918f24d NC |
1768 | |
1769 | PERL_ARGS_ASSERT_IS_UTF8_IDFIRST; | |
1770 | ||
82686b01 JH |
1771 | if (*p == '_') |
1772 | return TRUE; | |
bde6a22d | 1773 | /* is_utf8_idstart would be more logical. */ |
d4c19fe8 | 1774 | return is_utf8_common(p, &PL_utf8_idstart, "IdStart"); |
82686b01 JH |
1775 | } |
1776 | ||
1777 | bool | |
c11ff943 KW |
1778 | Perl_is_utf8_xidfirst(pTHX_ const U8 *p) /* The naming is historical. */ |
1779 | { | |
1780 | dVAR; | |
1781 | ||
1782 | PERL_ARGS_ASSERT_IS_UTF8_XIDFIRST; | |
1783 | ||
1784 | if (*p == '_') | |
1785 | return TRUE; | |
1786 | /* is_utf8_idstart would be more logical. */ | |
1787 | return is_utf8_common(p, &PL_utf8_xidstart, "XIdStart"); | |
1788 | } | |
1789 | ||
1790 | bool | |
b6912c02 KW |
1791 | Perl__is_utf8__perl_idstart(pTHX_ const U8 *p) |
1792 | { | |
1793 | dVAR; | |
1794 | ||
1795 | PERL_ARGS_ASSERT__IS_UTF8__PERL_IDSTART; | |
1796 | ||
1797 | return is_utf8_common(p, &PL_utf8_perl_idstart, "_Perl_IDStart"); | |
1798 | } | |
1799 | ||
1800 | bool | |
7fc63493 | 1801 | Perl_is_utf8_idcont(pTHX_ const U8 *p) |
82686b01 | 1802 | { |
97aff369 | 1803 | dVAR; |
7918f24d NC |
1804 | |
1805 | PERL_ARGS_ASSERT_IS_UTF8_IDCONT; | |
1806 | ||
d4c19fe8 | 1807 | return is_utf8_common(p, &PL_utf8_idcont, "IdContinue"); |
a0ed51b3 LW |
1808 | } |
1809 | ||
1810 | bool | |
c11ff943 KW |
1811 | Perl_is_utf8_xidcont(pTHX_ const U8 *p) |
1812 | { | |
1813 | dVAR; | |
1814 | ||
1815 | PERL_ARGS_ASSERT_IS_UTF8_XIDCONT; | |
1816 | ||
c11ff943 KW |
1817 | return is_utf8_common(p, &PL_utf8_idcont, "XIdContinue"); |
1818 | } | |
1819 | ||
1820 | bool | |
7fc63493 | 1821 | Perl_is_utf8_alpha(pTHX_ const U8 *p) |
a0ed51b3 | 1822 | { |
97aff369 | 1823 | dVAR; |
7918f24d NC |
1824 | |
1825 | PERL_ARGS_ASSERT_IS_UTF8_ALPHA; | |
1826 | ||
d4c19fe8 | 1827 | return is_utf8_common(p, &PL_utf8_alpha, "IsAlpha"); |
a0ed51b3 LW |
1828 | } |
1829 | ||
1830 | bool | |
7fc63493 | 1831 | Perl_is_utf8_ascii(pTHX_ const U8 *p) |
b8c5462f | 1832 | { |
97aff369 | 1833 | dVAR; |
7918f24d NC |
1834 | |
1835 | PERL_ARGS_ASSERT_IS_UTF8_ASCII; | |
1836 | ||
bc39fe24 KW |
1837 | /* ASCII characters are the same whether in utf8 or not. So the macro |
1838 | * works on both utf8 and non-utf8 representations. */ | |
1839 | return isASCII(*p); | |
b8c5462f JH |
1840 | } |
1841 | ||
1842 | bool | |
7fc63493 | 1843 | Perl_is_utf8_space(pTHX_ const U8 *p) |
a0ed51b3 | 1844 | { |
97aff369 | 1845 | dVAR; |
7918f24d NC |
1846 | |
1847 | PERL_ARGS_ASSERT_IS_UTF8_SPACE; | |
1848 | ||
a34094a9 | 1849 | return is_utf8_common(p, &PL_utf8_space, "IsXPerlSpace"); |
a0ed51b3 LW |
1850 | } |
1851 | ||
1852 | bool | |
d1eb3177 YO |
1853 | Perl_is_utf8_perl_space(pTHX_ const U8 *p) |
1854 | { | |
1855 | dVAR; | |
1856 | ||
1857 | PERL_ARGS_ASSERT_IS_UTF8_PERL_SPACE; | |
1858 | ||
c4428693 KW |
1859 | /* Only true if is an ASCII space-like character, and ASCII is invariant |
1860 | * under utf8, so can just use the macro */ | |
1861 | return isSPACE_A(*p); | |
d1eb3177 YO |
1862 | } |
1863 | ||
1864 | bool | |
1865 | Perl_is_utf8_perl_word(pTHX_ const U8 *p) | |
1866 | { | |
1867 | dVAR; | |
1868 | ||
1869 | PERL_ARGS_ASSERT_IS_UTF8_PERL_WORD; | |
1870 | ||
c4428693 KW |
1871 | /* Only true if is an ASCII word character, and ASCII is invariant |
1872 | * under utf8, so can just use the macro */ | |
1873 | return isWORDCHAR_A(*p); | |
d1eb3177 YO |
1874 | } |
1875 | ||
1876 | bool | |
7fc63493 | 1877 | Perl_is_utf8_digit(pTHX_ const U8 *p) |
a0ed51b3 | 1878 | { |
97aff369 | 1879 | dVAR; |
7918f24d NC |
1880 | |
1881 | PERL_ARGS_ASSERT_IS_UTF8_DIGIT; | |
1882 | ||
d4c19fe8 | 1883 | return is_utf8_common(p, &PL_utf8_digit, "IsDigit"); |
a0ed51b3 LW |
1884 | } |
1885 | ||
1886 | bool | |
d1eb3177 YO |
1887 | Perl_is_utf8_posix_digit(pTHX_ const U8 *p) |
1888 | { | |
1889 | dVAR; | |
1890 | ||
1891 | PERL_ARGS_ASSERT_IS_UTF8_POSIX_DIGIT; | |
1892 | ||
c4428693 KW |
1893 | /* Only true if is an ASCII digit character, and ASCII is invariant |
1894 | * under utf8, so can just use the macro */ | |
1895 | return isDIGIT_A(*p); | |
d1eb3177 YO |
1896 | } |
1897 | ||
1898 | bool | |
7fc63493 | 1899 | Perl_is_utf8_upper(pTHX_ const U8 *p) |
a0ed51b3 | 1900 | { |
97aff369 | 1901 | dVAR; |
7918f24d NC |
1902 | |
1903 | PERL_ARGS_ASSERT_IS_UTF8_UPPER; | |
1904 | ||
d4c19fe8 | 1905 | return is_utf8_common(p, &PL_utf8_upper, "IsUppercase"); |
a0ed51b3 LW |
1906 | } |
1907 | ||
1908 | bool | |
7fc63493 | 1909 | Perl_is_utf8_lower(pTHX_ const U8 *p) |
a0ed51b3 | 1910 | { |
97aff369 | 1911 | dVAR; |
7918f24d NC |
1912 | |
1913 | PERL_ARGS_ASSERT_IS_UTF8_LOWER; | |
1914 | ||
d4c19fe8 | 1915 | return is_utf8_common(p, &PL_utf8_lower, "IsLowercase"); |
a0ed51b3 LW |
1916 | } |
1917 | ||
1918 | bool | |
7fc63493 | 1919 | Perl_is_utf8_cntrl(pTHX_ const U8 *p) |
b8c5462f | 1920 | { |
97aff369 | 1921 | dVAR; |
7918f24d NC |
1922 | |
1923 | PERL_ARGS_ASSERT_IS_UTF8_CNTRL; | |
1924 | ||
7b952154 KW |
1925 | if (isASCII(*p)) { |
1926 | return isCNTRL_A(*p); | |
1927 | } | |
1928 | ||
1929 | /* All controls are in Latin1 */ | |
1930 | if (! UTF8_IS_DOWNGRADEABLE_START(*p)) { | |
1931 | return 0; | |
1932 | } | |
1933 | return isCNTRL_L1(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1))); | |
b8c5462f JH |
1934 | } |
1935 | ||
1936 | bool | |
7fc63493 | 1937 | Perl_is_utf8_graph(pTHX_ const U8 *p) |
b8c5462f | 1938 | { |
97aff369 | 1939 | dVAR; |
7918f24d NC |
1940 | |
1941 | PERL_ARGS_ASSERT_IS_UTF8_GRAPH; | |
1942 | ||
d4c19fe8 | 1943 | return is_utf8_common(p, &PL_utf8_graph, "IsGraph"); |
b8c5462f JH |
1944 | } |
1945 | ||
1946 | bool | |
7fc63493 | 1947 | Perl_is_utf8_print(pTHX_ const U8 *p) |
a0ed51b3 | 1948 | { |
97aff369 | 1949 | dVAR; |
7918f24d NC |
1950 | |
1951 | PERL_ARGS_ASSERT_IS_UTF8_PRINT; | |
1952 | ||
d4c19fe8 | 1953 | return is_utf8_common(p, &PL_utf8_print, "IsPrint"); |
a0ed51b3 LW |
1954 | } |
1955 | ||
1956 | bool | |
7fc63493 | 1957 | Perl_is_utf8_punct(pTHX_ const U8 *p) |
b8c5462f | 1958 | { |
97aff369 | 1959 | dVAR; |
7918f24d NC |
1960 | |
1961 | PERL_ARGS_ASSERT_IS_UTF8_PUNCT; | |
1962 | ||
d4c19fe8 | 1963 | return is_utf8_common(p, &PL_utf8_punct, "IsPunct"); |
b8c5462f JH |
1964 | } |
1965 | ||
1966 | bool | |
7fc63493 | 1967 | Perl_is_utf8_xdigit(pTHX_ const U8 *p) |
b8c5462f | 1968 | { |
97aff369 | 1969 | dVAR; |
7918f24d NC |
1970 | |
1971 | PERL_ARGS_ASSERT_IS_UTF8_XDIGIT; | |
1972 | ||
d1eb3177 | 1973 | return is_utf8_common(p, &PL_utf8_xdigit, "IsXDigit"); |
b8c5462f JH |
1974 | } |
1975 | ||
1976 | bool | |
7fc63493 | 1977 | Perl_is_utf8_mark(pTHX_ const U8 *p) |
a0ed51b3 | 1978 | { |
97aff369 | 1979 | dVAR; |
7918f24d NC |
1980 | |
1981 | PERL_ARGS_ASSERT_IS_UTF8_MARK; | |
1982 | ||
d4c19fe8 | 1983 | return is_utf8_common(p, &PL_utf8_mark, "IsM"); |
a0ed51b3 LW |
1984 | } |
1985 | ||
37e2e78e KW |
1986 | bool |
1987 | Perl_is_utf8_X_begin(pTHX_ const U8 *p) | |
1988 | { | |
1989 | dVAR; | |
1990 | ||
1991 | PERL_ARGS_ASSERT_IS_UTF8_X_BEGIN; | |
1992 | ||
1993 | return is_utf8_common(p, &PL_utf8_X_begin, "_X_Begin"); | |
1994 | } | |
1995 | ||
1996 | bool | |
1997 | Perl_is_utf8_X_extend(pTHX_ const U8 *p) | |
1998 | { | |
1999 | dVAR; | |
2000 | ||
2001 | PERL_ARGS_ASSERT_IS_UTF8_X_EXTEND; | |
2002 | ||
2003 | return is_utf8_common(p, &PL_utf8_X_extend, "_X_Extend"); | |
2004 | } | |
2005 | ||
2006 | bool | |
2007 | Perl_is_utf8_X_prepend(pTHX_ const U8 *p) | |
2008 | { | |
2009 | dVAR; | |
2010 | ||
2011 | PERL_ARGS_ASSERT_IS_UTF8_X_PREPEND; | |
2012 | ||
2013 | return is_utf8_common(p, &PL_utf8_X_prepend, "GCB=Prepend"); | |
2014 | } | |
2015 | ||
2016 | bool | |
2017 | Perl_is_utf8_X_non_hangul(pTHX_ const U8 *p) | |
2018 | { | |
2019 | dVAR; | |
2020 | ||
2021 | PERL_ARGS_ASSERT_IS_UTF8_X_NON_HANGUL; | |
2022 | ||
2023 | return is_utf8_common(p, &PL_utf8_X_non_hangul, "HST=Not_Applicable"); | |
2024 | } | |
2025 | ||
2026 | bool | |
2027 | Perl_is_utf8_X_L(pTHX_ const U8 *p) | |
2028 | { | |
2029 | dVAR; | |
2030 | ||
2031 | PERL_ARGS_ASSERT_IS_UTF8_X_L; | |
2032 | ||
2033 | return is_utf8_common(p, &PL_utf8_X_L, "GCB=L"); | |
2034 | } | |
2035 | ||
2036 | bool | |
2037 | Perl_is_utf8_X_LV(pTHX_ const U8 *p) | |
2038 | { | |
2039 | dVAR; | |
2040 | ||
2041 | PERL_ARGS_ASSERT_IS_UTF8_X_LV; | |
2042 | ||
2043 | return is_utf8_common(p, &PL_utf8_X_LV, "GCB=LV"); | |
2044 | } | |
2045 | ||
2046 | bool | |
2047 | Perl_is_utf8_X_LVT(pTHX_ const U8 *p) | |
2048 | { | |
2049 | dVAR; | |
2050 | ||
2051 | PERL_ARGS_ASSERT_IS_UTF8_X_LVT; | |
2052 | ||
2053 | return is_utf8_common(p, &PL_utf8_X_LVT, "GCB=LVT"); | |
2054 | } | |
2055 | ||
2056 | bool | |
2057 | Perl_is_utf8_X_T(pTHX_ const U8 *p) | |
2058 | { | |
2059 | dVAR; | |
2060 | ||
2061 | PERL_ARGS_ASSERT_IS_UTF8_X_T; | |
2062 | ||
2063 | return is_utf8_common(p, &PL_utf8_X_T, "GCB=T"); | |
2064 | } | |
2065 | ||
2066 | bool | |
2067 | Perl_is_utf8_X_V(pTHX_ const U8 *p) | |
2068 | { | |
2069 | dVAR; | |
2070 | ||
2071 | PERL_ARGS_ASSERT_IS_UTF8_X_V; | |
2072 | ||
2073 | return is_utf8_common(p, &PL_utf8_X_V, "GCB=V"); | |
2074 | } | |
2075 | ||
2076 | bool | |
2077 | Perl_is_utf8_X_LV_LVT_V(pTHX_ const U8 *p) | |
2078 | { | |
2079 | dVAR; | |
2080 | ||
2081 | PERL_ARGS_ASSERT_IS_UTF8_X_LV_LVT_V; | |
2082 | ||
2083 | return is_utf8_common(p, &PL_utf8_X_LV_LVT_V, "_X_LV_LVT_V"); | |
2084 | } | |
2085 | ||
2e2b2571 KW |
2086 | bool |
2087 | Perl__is_utf8_quotemeta(pTHX_ const U8 *p) | |
2088 | { | |
2089 | /* For exclusive use of pp_quotemeta() */ | |
2090 | ||
2091 | dVAR; | |
2092 | ||
2093 | PERL_ARGS_ASSERT__IS_UTF8_QUOTEMETA; | |
2094 | ||
2095 | return is_utf8_common(p, &PL_utf8_quotemeta, "_Perl_Quotemeta"); | |
2096 | } | |
2097 | ||
6b5c0936 | 2098 | /* |
87cea99e | 2099 | =for apidoc to_utf8_case |
6b5c0936 | 2100 | |
a1433954 KW |
2101 | The C<p> contains the pointer to the UTF-8 string encoding |
2102 | the character that is being converted. This routine assumes that the character | |
2103 | at C<p> is well-formed. | |
6b5c0936 | 2104 | |
a1433954 KW |
2105 | The C<ustrp> is a pointer to the character buffer to put the |
2106 | conversion result to. The C<lenp> is a pointer to the length | |
6b5c0936 JH |
2107 | of the result. |
2108 | ||
a1433954 | 2109 | The C<swashp> is a pointer to the swash to use. |
6b5c0936 | 2110 | |
a1433954 KW |
2111 | Both the special and normal mappings are stored in F<lib/unicore/To/Foo.pl>, |
2112 | and loaded by SWASHNEW, using F<lib/utf8_heavy.pl>. The C<special> (usually, | |
0134edef | 2113 | but not always, a multicharacter mapping), is tried first. |
6b5c0936 | 2114 | |
a1433954 | 2115 | The C<special> is a string like "utf8::ToSpecLower", which means the |
0134edef JH |
2116 | hash %utf8::ToSpecLower. The access to the hash is through |
2117 | Perl_to_utf8_case(). | |
6b5c0936 | 2118 | |
a1433954 | 2119 | The C<normal> is a string like "ToLower" which means the swash |
0134edef JH |
2120 | %utf8::ToLower. |
2121 | ||
2122 | =cut */ | |
6b5c0936 | 2123 | |
2104c8d9 | 2124 | UV |
9a957fbc AL |
2125 | Perl_to_utf8_case(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, |
2126 | SV **swashp, const char *normal, const char *special) | |
a0ed51b3 | 2127 | { |
97aff369 | 2128 | dVAR; |
89ebb4a3 | 2129 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
0134edef | 2130 | STRLEN len = 0; |
aec46f14 | 2131 | const UV uv0 = utf8_to_uvchr(p, NULL); |
1feea2c7 JH |
2132 | /* The NATIVE_TO_UNI() and UNI_TO_NATIVE() mappings |
2133 | * are necessary in EBCDIC, they are redundant no-ops | |
2134 | * in ASCII-ish platforms, and hopefully optimized away. */ | |
f54cb97a | 2135 | const UV uv1 = NATIVE_TO_UNI(uv0); |
7918f24d NC |
2136 | |
2137 | PERL_ARGS_ASSERT_TO_UTF8_CASE; | |
2138 | ||
9ae3ac1a KW |
2139 | /* Note that swash_fetch() doesn't output warnings for these because it |
2140 | * assumes we will */ | |
8457b38f | 2141 | if (uv1 >= UNICODE_SURROGATE_FIRST) { |
9ae3ac1a | 2142 | if (uv1 <= UNICODE_SURROGATE_LAST) { |
8457b38f KW |
2143 | if (ckWARN_d(WARN_SURROGATE)) { |
2144 | const char* desc = (PL_op) ? OP_DESC(PL_op) : normal; | |
2145 | Perl_warner(aTHX_ packWARN(WARN_SURROGATE), | |
2146 | "Operation \"%s\" returns its argument for UTF-16 surrogate U+%04"UVXf"", desc, uv1); | |
2147 | } | |
9ae3ac1a KW |
2148 | } |
2149 | else if (UNICODE_IS_SUPER(uv1)) { | |
8457b38f KW |
2150 | if (ckWARN_d(WARN_NON_UNICODE)) { |
2151 | const char* desc = (PL_op) ? OP_DESC(PL_op) : normal; | |
2152 | Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE), | |
2153 | "Operation \"%s\" returns its argument for non-Unicode code point 0x%04"UVXf"", desc, uv1); | |
2154 | } | |
9ae3ac1a KW |
2155 | } |
2156 | ||
2157 | /* Note that non-characters are perfectly legal, so no warning should | |
2158 | * be given */ | |
2159 | } | |
2160 | ||
1feea2c7 | 2161 | uvuni_to_utf8(tmpbuf, uv1); |
0134edef JH |
2162 | |
2163 | if (!*swashp) /* load on-demand */ | |
2164 | *swashp = swash_init("utf8", normal, &PL_sv_undef, 4, 0); | |
2165 | ||
a6f87d8c | 2166 | if (special) { |
0134edef | 2167 | /* It might be "special" (sometimes, but not always, |
2a37f04d | 2168 | * a multicharacter mapping) */ |
6673a63c | 2169 | HV * const hv = get_hv(special, 0); |
b08cf34e JH |
2170 | SV **svp; |
2171 | ||
35da51f7 | 2172 | if (hv && |
b08cf34e JH |
2173 | (svp = hv_fetch(hv, (const char*)tmpbuf, UNISKIP(uv1), FALSE)) && |
2174 | (*svp)) { | |
cfd0369c | 2175 | const char *s; |
47654450 | 2176 | |
cfd0369c | 2177 | s = SvPV_const(*svp, len); |
47654450 JH |
2178 | if (len == 1) |
2179 | len = uvuni_to_utf8(ustrp, NATIVE_TO_UNI(*(U8*)s)) - ustrp; | |
2a37f04d | 2180 | else { |
2f9475ad JH |
2181 | #ifdef EBCDIC |
2182 | /* If we have EBCDIC we need to remap the characters | |
2183 | * since any characters in the low 256 are Unicode | |
2184 | * code points, not EBCDIC. */ | |
7cda7a3d | 2185 | U8 *t = (U8*)s, *tend = t + len, *d; |
2f9475ad JH |
2186 | |
2187 | d = tmpbuf; | |
b08cf34e | 2188 | if (SvUTF8(*svp)) { |
2f9475ad JH |
2189 | STRLEN tlen = 0; |
2190 | ||
2191 | while (t < tend) { | |
d4c19fe8 | 2192 | const UV c = utf8_to_uvchr(t, &tlen); |
2f9475ad JH |
2193 | if (tlen > 0) { |
2194 | d = uvchr_to_utf8(d, UNI_TO_NATIVE(c)); | |
2195 | t += tlen; | |
2196 | } | |
2197 | else | |
2198 | break; | |
2199 | } | |
2200 | } | |
2201 | else { | |
36fec512 JH |
2202 | while (t < tend) { |
2203 | d = uvchr_to_utf8(d, UNI_TO_NATIVE(*t)); | |
2204 | t++; | |
2205 | } | |
2f9475ad JH |
2206 | } |
2207 | len = d - tmpbuf; | |
2208 | Copy(tmpbuf, ustrp, len, U8); | |
2209 | #else | |
d2dcd0fb | 2210 | Copy(s, ustrp, len, U8); |
2f9475ad | 2211 | #endif |
29e98929 | 2212 | } |
983ffd37 | 2213 | } |
0134edef JH |
2214 | } |
2215 | ||
2216 | if (!len && *swashp) { | |
d4c19fe8 AL |
2217 | const UV uv2 = swash_fetch(*swashp, tmpbuf, TRUE); |
2218 | ||
0134edef JH |
2219 | if (uv2) { |
2220 | /* It was "normal" (a single character mapping). */ | |
d4c19fe8 | 2221 | const UV uv3 = UNI_TO_NATIVE(uv2); |
e9101d72 | 2222 | len = uvchr_to_utf8(ustrp, uv3) - ustrp; |
2a37f04d JH |
2223 | } |
2224 | } | |
1feea2c7 | 2225 | |
37e2e78e KW |
2226 | if (!len) /* Neither: just copy. In other words, there was no mapping |
2227 | defined, which means that the code point maps to itself */ | |
0134edef JH |
2228 | len = uvchr_to_utf8(ustrp, uv0) - ustrp; |
2229 | ||
2a37f04d JH |
2230 | if (lenp) |
2231 | *lenp = len; | |
2232 | ||
0134edef | 2233 | return len ? utf8_to_uvchr(ustrp, 0) : 0; |
a0ed51b3 LW |
2234 | } |
2235 | ||
051a06d4 KW |
2236 | STATIC UV |
2237 | S_check_locale_boundary_crossing(pTHX_ const U8* const p, const UV result, U8* const ustrp, STRLEN *lenp) | |
2238 | { | |
2239 | /* This is called when changing the case of a utf8-encoded character above | |
2240 | * the Latin1 range, and the operation is in locale. If the result | |
2241 | * contains a character that crosses the 255/256 boundary, disallow the | |
2242 | * change, and return the original code point. See L<perlfunc/lc> for why; | |
2243 | * | |
a1433954 KW |
2244 | * p points to the original string whose case was changed; assumed |
2245 | * by this routine to be well-formed | |
051a06d4 KW |
2246 | * result the code point of the first character in the changed-case string |
2247 | * ustrp points to the changed-case string (<result> represents its first char) | |
2248 | * lenp points to the length of <ustrp> */ | |
2249 | ||
2250 | UV original; /* To store the first code point of <p> */ | |
2251 | ||
2252 | PERL_ARGS_ASSERT_CHECK_LOCALE_BOUNDARY_CROSSING; | |
2253 | ||
2254 | assert(! UTF8_IS_INVARIANT(*p) && ! UTF8_IS_DOWNGRADEABLE_START(*p)); | |
2255 | ||
2256 | /* We know immediately if the first character in the string crosses the | |
2257 | * boundary, so can skip */ | |
2258 | if (result > 255) { | |
2259 | ||
2260 | /* Look at every character in the result; if any cross the | |
2261 | * boundary, the whole thing is disallowed */ | |
2262 | U8* s = ustrp + UTF8SKIP(ustrp); | |
2263 | U8* e = ustrp + *lenp; | |
2264 | while (s < e) { | |
2265 | if (UTF8_IS_INVARIANT(*s) || UTF8_IS_DOWNGRADEABLE_START(*s)) | |
2266 | { | |
2267 | goto bad_crossing; | |
2268 | } | |
2269 | s += UTF8SKIP(s); | |
2270 | } | |
2271 | ||
2272 | /* Here, no characters crossed, result is ok as-is */ | |
2273 | return result; | |
2274 | } | |
2275 | ||
2276 | bad_crossing: | |
2277 | ||
2278 | /* Failed, have to return the original */ | |
2279 | original = utf8_to_uvchr(p, lenp); | |
2280 | Copy(p, ustrp, *lenp, char); | |
2281 | return original; | |
2282 | } | |
2283 | ||
d3e79532 | 2284 | /* |
87cea99e | 2285 | =for apidoc to_utf8_upper |
d3e79532 | 2286 | |
a1433954 KW |
2287 | Convert the UTF-8 encoded character at C<p> to its uppercase version and |
2288 | store that in UTF-8 in C<ustrp> and its length in bytes in C<lenp>. Note | |
89ebb4a3 JH |
2289 | that the ustrp needs to be at least UTF8_MAXBYTES_CASE+1 bytes since |
2290 | the uppercase version may be longer than the original character. | |
d3e79532 JH |
2291 | |
2292 | The first character of the uppercased version is returned | |
2293 | (but note, as explained above, that there may be more.) | |
2294 | ||
a1433954 KW |
2295 | The character at C<p> is assumed by this routine to be well-formed. |
2296 | ||
d3e79532 JH |
2297 | =cut */ |
2298 | ||
051a06d4 KW |
2299 | /* Not currently externally documented, and subject to change: |
2300 | * <flags> is set iff locale semantics are to be used for code points < 256 | |
2301 | * <tainted_ptr> if non-null, *tainted_ptr will be set TRUE iff locale rules | |
2302 | * were used in the calculation; otherwise unchanged. */ | |
2303 | ||
2104c8d9 | 2304 | UV |
051a06d4 | 2305 | Perl__to_utf8_upper_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, const bool flags, bool* tainted_ptr) |
a0ed51b3 | 2306 | { |
97aff369 | 2307 | dVAR; |
7918f24d | 2308 | |
051a06d4 KW |
2309 | UV result; |
2310 | ||
2311 | PERL_ARGS_ASSERT__TO_UTF8_UPPER_FLAGS; | |
7918f24d | 2312 | |
3a4c58c9 | 2313 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 KW |
2314 | if (flags) { |
2315 | result = toUPPER_LC(*p); | |
2316 | } | |
2317 | else { | |
81c6c7ce | 2318 | return _to_upper_title_latin1(*p, ustrp, lenp, 'S'); |
051a06d4 | 2319 | } |
3a4c58c9 KW |
2320 | } |
2321 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 KW |
2322 | if (flags) { |
2323 | result = toUPPER_LC(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1))); | |
2324 | } | |
2325 | else { | |
81c6c7ce KW |
2326 | return _to_upper_title_latin1(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1)), |
2327 | ustrp, lenp, 'S'); | |
051a06d4 KW |
2328 | } |
2329 | } | |
2330 | else { /* utf8, ord above 255 */ | |
2331 | result = CALL_UPPER_CASE(p, ustrp, lenp); | |
2332 | ||
2333 | if (flags) { | |
2334 | result = check_locale_boundary_crossing(p, result, ustrp, lenp); | |
2335 | } | |
2336 | return result; | |
2337 | } | |
2338 | ||
2339 | /* Here, used locale rules. Convert back to utf8 */ | |
2340 | if (UTF8_IS_INVARIANT(result)) { | |
2341 | *ustrp = (U8) result; | |
2342 | *lenp = 1; | |
2343 | } | |
2344 | else { | |
2345 | *ustrp = UTF8_EIGHT_BIT_HI(result); | |
2346 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO(result); | |
2347 | *lenp = 2; | |
3a4c58c9 KW |
2348 | } |
2349 | ||
051a06d4 KW |
2350 | if (tainted_ptr) { |
2351 | *tainted_ptr = TRUE; | |
2352 | } | |
2353 | return result; | |
983ffd37 | 2354 | } |
a0ed51b3 | 2355 | |
d3e79532 | 2356 | /* |
87cea99e | 2357 | =for apidoc to_utf8_title |
d3e79532 | 2358 | |
a1433954 KW |
2359 | Convert the UTF-8 encoded character at C<p> to its titlecase version and |
2360 | store that in UTF-8 in C<ustrp> and its length in bytes in C<lenp>. Note | |
2361 | that the C<ustrp> needs to be at least UTF8_MAXBYTES_CASE+1 bytes since the | |
89ebb4a3 | 2362 | titlecase version may be longer than the original character. |
d3e79532 JH |
2363 | |
2364 | The first character of the titlecased version is returned | |
2365 | (but note, as explained above, that there may be more.) | |
2366 | ||
a1433954 KW |
2367 | The character at C<p> is assumed by this routine to be well-formed. |
2368 | ||
d3e79532 JH |
2369 | =cut */ |
2370 | ||
051a06d4 KW |
2371 | /* Not currently externally documented, and subject to change: |
2372 | * <flags> is set iff locale semantics are to be used for code points < 256 | |
2373 | * Since titlecase is not defined in POSIX, uppercase is used instead | |
2374 | * for these/ | |
2375 | * <tainted_ptr> if non-null, *tainted_ptr will be set TRUE iff locale rules | |
2376 | * were used in the calculation; otherwise unchanged. */ | |
2377 | ||
983ffd37 | 2378 | UV |
051a06d4 | 2379 | Perl__to_utf8_title_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, const bool flags, bool* tainted_ptr) |
983ffd37 | 2380 | { |
97aff369 | 2381 | dVAR; |
7918f24d | 2382 | |
051a06d4 KW |
2383 | UV result; |
2384 | ||
2385 | PERL_ARGS_ASSERT__TO_UTF8_TITLE_FLAGS; | |
7918f24d | 2386 | |
3a4c58c9 | 2387 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 KW |
2388 | if (flags) { |
2389 | result = toUPPER_LC(*p); | |
2390 | } | |
2391 | else { | |
81c6c7ce | 2392 | return _to_upper_title_latin1(*p, ustrp, lenp, 's'); |
051a06d4 | 2393 | } |
3a4c58c9 KW |
2394 | } |
2395 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 KW |
2396 | if (flags) { |
2397 | result = toUPPER_LC(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1))); | |
2398 | } | |
2399 | else { | |
81c6c7ce KW |
2400 | return _to_upper_title_latin1(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1)), |
2401 | ustrp, lenp, 's'); | |
051a06d4 KW |
2402 | } |
2403 | } | |
2404 | else { /* utf8, ord above 255 */ | |
2405 | result = CALL_TITLE_CASE(p, ustrp, lenp); | |
2406 | ||
2407 | if (flags) { | |
2408 | result = check_locale_boundary_crossing(p, result, ustrp, lenp); | |
2409 | } | |
2410 | return result; | |
2411 | } | |
2412 | ||
2413 | /* Here, used locale rules. Convert back to utf8 */ | |
2414 | if (UTF8_IS_INVARIANT(result)) { | |
2415 | *ustrp = (U8) result; | |
2416 | *lenp = 1; | |
2417 | } | |
2418 | else { | |
2419 | *ustrp = UTF8_EIGHT_BIT_HI(result); | |
2420 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO(result); | |
2421 | *lenp = 2; | |
3a4c58c9 KW |
2422 | } |
2423 | ||
051a06d4 KW |
2424 | if (tainted_ptr) { |
2425 | *tainted_ptr = TRUE; | |
2426 | } | |
2427 | return result; | |
a0ed51b3 LW |
2428 | } |
2429 | ||
d3e79532 | 2430 | /* |
87cea99e | 2431 | =for apidoc to_utf8_lower |
d3e79532 | 2432 | |
a1433954 KW |
2433 | Convert the UTF-8 encoded character at C<p> to its lowercase version and |
2434 | store that in UTF-8 in ustrp and its length in bytes in C<lenp>. Note | |
2435 | that the C<ustrp> needs to be at least UTF8_MAXBYTES_CASE+1 bytes since the | |
89ebb4a3 | 2436 | lowercase version may be longer than the original character. |
d3e79532 JH |
2437 | |
2438 | The first character of the lowercased version is returned | |
2439 | (but note, as explained above, that there may be more.) | |
2440 | ||
a1433954 KW |
2441 | The character at C<p> is assumed by this routine to be well-formed. |
2442 | ||
d3e79532 JH |
2443 | =cut */ |
2444 | ||
051a06d4 KW |
2445 | /* Not currently externally documented, and subject to change: |
2446 | * <flags> is set iff locale semantics are to be used for code points < 256 | |
2447 | * <tainted_ptr> if non-null, *tainted_ptr will be set TRUE iff locale rules | |
2448 | * were used in the calculation; otherwise unchanged. */ | |
2449 | ||
2104c8d9 | 2450 | UV |
051a06d4 | 2451 | Perl__to_utf8_lower_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, const bool flags, bool* tainted_ptr) |
a0ed51b3 | 2452 | { |
051a06d4 KW |
2453 | UV result; |
2454 | ||
97aff369 | 2455 | dVAR; |
7918f24d | 2456 | |
051a06d4 | 2457 | PERL_ARGS_ASSERT__TO_UTF8_LOWER_FLAGS; |
7918f24d | 2458 | |
968c5e6a | 2459 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 KW |
2460 | if (flags) { |
2461 | result = toLOWER_LC(*p); | |
2462 | } | |
2463 | else { | |
81c6c7ce | 2464 | return to_lower_latin1(*p, ustrp, lenp); |
051a06d4 | 2465 | } |
968c5e6a KW |
2466 | } |
2467 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 KW |
2468 | if (flags) { |
2469 | result = toLOWER_LC(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1))); | |
2470 | } | |
2471 | else { | |
81c6c7ce KW |
2472 | return to_lower_latin1(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1)), |
2473 | ustrp, lenp); | |
051a06d4 | 2474 | } |
968c5e6a | 2475 | } |
051a06d4 KW |
2476 | else { /* utf8, ord above 255 */ |
2477 | result = CALL_LOWER_CASE(p, ustrp, lenp); | |
2478 | ||
2479 | if (flags) { | |
2480 | result = check_locale_boundary_crossing(p, result, ustrp, lenp); | |
2481 | } | |
968c5e6a | 2482 | |
051a06d4 KW |
2483 | return result; |
2484 | } | |
2485 | ||
2486 | /* Here, used locale rules. Convert back to utf8 */ | |
2487 | if (UTF8_IS_INVARIANT(result)) { | |
2488 | *ustrp = (U8) result; | |
2489 | *lenp = 1; | |
2490 | } | |
2491 | else { | |
2492 | *ustrp = UTF8_EIGHT_BIT_HI(result); | |
2493 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO(result); | |
2494 | *lenp = 2; | |
2495 | } | |
2496 | ||
2497 | if (tainted_ptr) { | |
2498 | *tainted_ptr = TRUE; | |
2499 | } | |
2500 | return result; | |
b4e400f9 JH |
2501 | } |
2502 | ||
d3e79532 | 2503 | /* |
87cea99e | 2504 | =for apidoc to_utf8_fold |
d3e79532 | 2505 | |
a1433954 KW |
2506 | Convert the UTF-8 encoded character at C<p> to its foldcase version and |
2507 | store that in UTF-8 in C<ustrp> and its length in bytes in C<lenp>. Note | |
2508 | that the C<ustrp> needs to be at least UTF8_MAXBYTES_CASE+1 bytes since the | |
d3e79532 JH |
2509 | foldcase version may be longer than the original character (up to |
2510 | three characters). | |
2511 | ||
2512 | The first character of the foldcased version is returned | |
2513 | (but note, as explained above, that there may be more.) | |
2514 | ||
a1433954 KW |
2515 | The character at C<p> is assumed by this routine to be well-formed. |
2516 | ||
d3e79532 JH |
2517 | =cut */ |
2518 | ||
051a06d4 KW |
2519 | /* Not currently externally documented, and subject to change, |
2520 | * in <flags> | |
2521 | * bit FOLD_FLAGS_LOCALE is set iff locale semantics are to be used for code | |
2522 | * points < 256. Since foldcase is not defined in | |
2523 | * POSIX, lowercase is used instead | |
2524 | * bit FOLD_FLAGS_FULL is set iff full case folds are to be used; | |
2525 | * otherwise simple folds | |
2526 | * <tainted_ptr> if non-null, *tainted_ptr will be set TRUE iff locale rules | |
2527 | * were used in the calculation; otherwise unchanged. */ | |
36bb2ab6 | 2528 | |
b4e400f9 | 2529 | UV |
051a06d4 | 2530 | Perl__to_utf8_fold_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, U8 flags, bool* tainted_ptr) |
b4e400f9 | 2531 | { |
97aff369 | 2532 | dVAR; |
7918f24d | 2533 | |
051a06d4 KW |
2534 | UV result; |
2535 | ||
36bb2ab6 | 2536 | PERL_ARGS_ASSERT__TO_UTF8_FOLD_FLAGS; |
7918f24d | 2537 | |
a1dde8de | 2538 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 KW |
2539 | if (flags & FOLD_FLAGS_LOCALE) { |
2540 | result = toLOWER_LC(*p); | |
2541 | } | |
2542 | else { | |
81c6c7ce KW |
2543 | return _to_fold_latin1(*p, ustrp, lenp, |
2544 | cBOOL(flags & FOLD_FLAGS_FULL)); | |
051a06d4 | 2545 | } |
a1dde8de KW |
2546 | } |
2547 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 KW |
2548 | if (flags & FOLD_FLAGS_LOCALE) { |
2549 | result = toLOWER_LC(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1))); | |
2550 | } | |
2551 | else { | |
81c6c7ce | 2552 | return _to_fold_latin1(TWO_BYTE_UTF8_TO_UNI(*p, *(p+1)), |
051a06d4 KW |
2553 | ustrp, lenp, cBOOL(flags & FOLD_FLAGS_FULL)); |
2554 | } | |
a1dde8de | 2555 | } |
051a06d4 KW |
2556 | else { /* utf8, ord above 255 */ |
2557 | result = CALL_FOLD_CASE(p, ustrp, lenp, flags); | |
a1dde8de | 2558 | |
051a06d4 KW |
2559 | if ((flags & FOLD_FLAGS_LOCALE)) { |
2560 | result = check_locale_boundary_crossing(p, result, ustrp, lenp); | |
2561 | } | |
2562 | ||
2563 | return result; | |
2564 | } | |
2565 | ||
2566 | /* Here, used locale rules. Convert back to utf8 */ | |
2567 | if (UTF8_IS_INVARIANT(result)) { | |
2568 | *ustrp = (U8) result; | |
2569 | *lenp = 1; | |
2570 | } | |
2571 | else { | |
2572 | *ustrp = UTF8_EIGHT_BIT_HI(result); | |
2573 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO(result); | |
2574 | *lenp = 2; | |
2575 | } | |
2576 | ||
2577 | if (tainted_ptr) { | |
2578 | *tainted_ptr = TRUE; | |
2579 | } | |
2580 | return result; | |
a0ed51b3 LW |
2581 | } |
2582 | ||
711a919c | 2583 | /* Note: |
f90a9a02 | 2584 | * Returns a "swash" which is a hash described in utf8.c:Perl_swash_fetch(). |
711a919c TS |
2585 | * C<pkg> is a pointer to a package name for SWASHNEW, should be "utf8". |
2586 | * For other parameters, see utf8::SWASHNEW in lib/utf8_heavy.pl. | |
2587 | */ | |
c4a5db0c | 2588 | |
a0ed51b3 | 2589 | SV* |
7fc63493 | 2590 | Perl_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits, I32 none) |
a0ed51b3 | 2591 | { |
c4a5db0c KW |
2592 | PERL_ARGS_ASSERT_SWASH_INIT; |
2593 | ||
2594 | /* Returns a copy of a swash initiated by the called function. This is the | |
2595 | * public interface, and returning a copy prevents others from doing | |
2596 | * mischief on the original */ | |
2597 | ||
9a53f6cf | 2598 | return newSVsv(_core_swash_init(pkg, name, listsv, minbits, none, FALSE, NULL, FALSE)); |
c4a5db0c KW |
2599 | } |
2600 | ||
2601 | SV* | |
9a53f6cf | 2602 | Perl__core_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits, I32 none, bool return_if_undef, SV* invlist, bool passed_in_invlist_has_user_defined_property) |
c4a5db0c KW |
2603 | { |
2604 | /* Initialize and return a swash, creating it if necessary. It does this | |
9a53f6cf | 2605 | * by calling utf8_heavy.pl in the general case. |
c4a5db0c KW |
2606 | * |
2607 | * This interface should only be used by functions that won't destroy or | |
2608 | * adversely change the swash, as doing so affects all other uses of the | |
2609 | * swash in the program; the general public should use 'Perl_swash_init' | |
2610 | * instead. | |
2611 | * | |
2612 | * pkg is the name of the package that <name> should be in. | |
2613 | * name is the name of the swash to find. Typically it is a Unicode | |
2614 | * property name, including user-defined ones | |
2615 | * listsv is a string to initialize the swash with. It must be of the form | |
2616 | * documented as the subroutine return value in | |
2617 | * L<perlunicode/User-Defined Character Properties> | |
2618 | * minbits is the number of bits required to represent each data element. | |
2619 | * It is '1' for binary properties. | |
2620 | * none I (khw) do not understand this one, but it is used only in tr///. | |
9a53f6cf KW |
2621 | * return_if_undef is TRUE if the routine shouldn't croak if it can't find |
2622 | * the requested property | |
2623 | * invlist is an inversion list to initialize the swash with (or NULL) | |
2624 | * has_user_defined_property is TRUE if <invlist> has some component that | |
2625 | * came from a user-defined property | |
2626 | * | |
2627 | * Thus there are three possible inputs to find the swash: <name>, | |
2628 | * <listsv>, and <invlist>. At least one must be specified. The result | |
2629 | * will be the union of the specified ones, although <listsv>'s various | |
2630 | * actions can intersect, etc. what <name> gives. | |
2631 | * | |
2632 | * <invlist> is only valid for binary properties */ | |
c4a5db0c | 2633 | |
27da23d5 | 2634 | dVAR; |
c4a5db0c | 2635 | SV* retval = &PL_sv_undef; |
9a53f6cf KW |
2636 | |
2637 | assert(listsv != &PL_sv_undef || strNE(name, "") || invlist); | |
2638 | assert(! invlist || minbits == 1); | |
2639 | ||
2640 | /* If data was passed in to go out to utf8_heavy to find the swash of, do | |
2641 | * so */ | |
2642 | if (listsv != &PL_sv_undef || strNE(name, "")) { | |
69794297 KW |
2643 | dSP; |
2644 | const size_t pkg_len = strlen(pkg); | |
2645 | const size_t name_len = strlen(name); | |
2646 | HV * const stash = gv_stashpvn(pkg, pkg_len, 0); | |
2647 | SV* errsv_save; | |
2648 | GV *method; | |
2649 | ||
2650 | PERL_ARGS_ASSERT__CORE_SWASH_INIT; | |
2651 | ||
2652 | PUSHSTACKi(PERLSI_MAGIC); | |
ce3b816e | 2653 | ENTER; |
69794297 KW |
2654 | SAVEHINTS(); |
2655 | save_re_context(); | |
2656 | if (PL_parser && PL_parser->error_count) | |
2657 | SAVEI8(PL_parser->error_count), PL_parser->error_count = 0; | |
2658 | method = gv_fetchmeth(stash, "SWASHNEW", 8, -1); | |
2659 | if (!method) { /* demand load utf8 */ | |
2660 | ENTER; | |
2661 | errsv_save = newSVsv(ERRSV); | |
2662 | /* It is assumed that callers of this routine are not passing in | |
2663 | * any user derived data. */ | |
2664 | /* Need to do this after save_re_context() as it will set | |
2665 | * PL_tainted to 1 while saving $1 etc (see the code after getrx: | |
2666 | * in Perl_magic_get). Even line to create errsv_save can turn on | |
2667 | * PL_tainted. */ | |
2668 | SAVEBOOL(PL_tainted); | |
2669 | PL_tainted = 0; | |
2670 | Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvn(pkg,pkg_len), | |
2671 | NULL); | |
2672 | if (!SvTRUE(ERRSV)) | |
2673 | sv_setsv(ERRSV, errsv_save); | |
2674 | SvREFCNT_dec(errsv_save); | |
2675 | LEAVE; | |
2676 | } | |
2677 | SPAGAIN; | |
2678 | PUSHMARK(SP); | |
2679 | EXTEND(SP,5); | |
2680 | mPUSHp(pkg, pkg_len); | |
2681 | mPUSHp(name, name_len); | |
2682 | PUSHs(listsv); | |
2683 | mPUSHi(minbits); | |
2684 | mPUSHi(none); | |
2685 | PUTBACK; | |
f8be5cf0 | 2686 | errsv_save = newSVsv(ERRSV); |
69794297 KW |
2687 | /* If we already have a pointer to the method, no need to use |
2688 | * call_method() to repeat the lookup. */ | |
2689 | if (method ? call_sv(MUTABLE_SV(method), G_SCALAR) | |
2690 | : call_sv(newSVpvs_flags("SWASHNEW", SVs_TEMP), G_SCALAR | G_METHOD)) | |
2691 | { | |
2692 | retval = *PL_stack_sp--; | |
2693 | SvREFCNT_inc(retval); | |
2694 | } | |
f8be5cf0 JH |
2695 | if (!SvTRUE(ERRSV)) |
2696 | sv_setsv(ERRSV, errsv_save); | |
2697 | SvREFCNT_dec(errsv_save); | |
ce3b816e | 2698 | LEAVE; |
69794297 KW |
2699 | POPSTACK; |
2700 | if (IN_PERL_COMPILETIME) { | |
2701 | CopHINTS_set(PL_curcop, PL_hints); | |
2702 | } | |
2703 | if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV) { | |
2704 | if (SvPOK(retval)) | |
2705 | ||
2706 | /* If caller wants to handle missing properties, let them */ | |
2707 | if (return_if_undef) { | |
2708 | return NULL; | |
2709 | } | |
2710 | Perl_croak(aTHX_ | |
2711 | "Can't find Unicode property definition \"%"SVf"\"", | |
2712 | SVfARG(retval)); | |
2713 | Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref"); | |
2714 | } | |
9a53f6cf | 2715 | } /* End of calling the module to find the swash */ |
36eb48b4 KW |
2716 | |
2717 | /* Make sure there is an inversion list for binary properties */ | |
2718 | if (minbits == 1) { | |
2719 | SV** swash_invlistsvp = NULL; | |
2720 | SV* swash_invlist = NULL; | |
9a53f6cf | 2721 | bool invlist_in_swash_is_valid = FALSE; |
88d45d28 | 2722 | HV* swash_hv = NULL; |
36eb48b4 | 2723 | |
9a53f6cf KW |
2724 | /* If this operation fetched a swash, get its already existing |
2725 | * inversion list or create one for it */ | |
2726 | if (retval != &PL_sv_undef) { | |
36eb48b4 KW |
2727 | swash_hv = MUTABLE_HV(SvRV(retval)); |
2728 | ||
2729 | swash_invlistsvp = hv_fetchs(swash_hv, "INVLIST", FALSE); | |
9a53f6cf KW |
2730 | if (swash_invlistsvp) { |
2731 | swash_invlist = *swash_invlistsvp; | |
2732 | invlist_in_swash_is_valid = TRUE; | |
2733 | } | |
2734 | else { | |
36eb48b4 | 2735 | swash_invlist = _swash_to_invlist(retval); |
9a53f6cf KW |
2736 | } |
2737 | } | |
2738 | ||
2739 | /* If an inversion list was passed in, have to include it */ | |
2740 | if (invlist) { | |
2741 | ||
2742 | /* Any fetched swash will by now have an inversion list in it; | |
2743 | * otherwise <swash_invlist> will be NULL, indicating that we | |
2744 | * didn't fetch a swash */ | |
2745 | if (swash_invlist) { | |
2746 | ||
2747 | /* Add the passed-in inversion list, which invalidates the one | |
2748 | * already stored in the swash */ | |
2749 | invlist_in_swash_is_valid = FALSE; | |
2750 | _invlist_union(invlist, swash_invlist, &swash_invlist); | |
2751 | } | |
2752 | else { | |
2753 | ||
2754 | /* Here, there is no swash already. Set up a minimal one */ | |
2755 | swash_hv = newHV(); | |
2756 | retval = newRV_inc(MUTABLE_SV(swash_hv)); | |
2757 | swash_invlist = invlist; | |
2758 | } | |
2759 | ||
2760 | if (passed_in_invlist_has_user_defined_property) { | |
2761 | if (! hv_stores(swash_hv, "USER_DEFINED", newSVuv(1))) { | |
2762 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
2763 | } | |
2764 | } | |
2765 | } | |
2766 | ||
2767 | /* Here, we have computed the union of all the passed-in data. It may | |
2768 | * be that there was an inversion list in the swash which didn't get | |
2769 | * touched; otherwise save the one computed one */ | |
2770 | if (! invlist_in_swash_is_valid) { | |
69794297 KW |
2771 | if (! hv_stores(MUTABLE_HV(SvRV(retval)), "INVLIST", swash_invlist)) |
2772 | { | |
2773 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
2774 | } | |
9a53f6cf | 2775 | } |
36eb48b4 KW |
2776 | } |
2777 | ||
a0ed51b3 LW |
2778 | return retval; |
2779 | } | |
2780 | ||
035d37be JH |
2781 | |
2782 | /* This API is wrong for special case conversions since we may need to | |
2783 | * return several Unicode characters for a single Unicode character | |
2784 | * (see lib/unicore/SpecCase.txt) The SWASHGET in lib/utf8_heavy.pl is | |
2785 | * the lower-level routine, and it is similarly broken for returning | |
38684baa KW |
2786 | * multiple values. --jhi |
2787 | * For those, you should use to_utf8_case() instead */ | |
b0e3252e | 2788 | /* Now SWASHGET is recasted into S_swatch_get in this file. */ |
680c470c TS |
2789 | |
2790 | /* Note: | |
2791 | * Returns the value of property/mapping C<swash> for the first character | |
2792 | * of the string C<ptr>. If C<do_utf8> is true, the string C<ptr> is | |
2793 | * assumed to be in utf8. If C<do_utf8> is false, the string C<ptr> is | |
2794 | * assumed to be in native 8-bit encoding. Caches the swatch in C<swash>. | |
af2af982 KW |
2795 | * |
2796 | * A "swash" is a hash which contains initially the keys/values set up by | |
2797 | * SWASHNEW. The purpose is to be able to completely represent a Unicode | |
2798 | * property for all possible code points. Things are stored in a compact form | |
2799 | * (see utf8_heavy.pl) so that calculation is required to find the actual | |
2800 | * property value for a given code point. As code points are looked up, new | |
2801 | * key/value pairs are added to the hash, so that the calculation doesn't have | |
2802 | * to ever be re-done. Further, each calculation is done, not just for the | |
2803 | * desired one, but for a whole block of code points adjacent to that one. | |
2804 | * For binary properties on ASCII machines, the block is usually for 64 code | |
2805 | * points, starting with a code point evenly divisible by 64. Thus if the | |
2806 | * property value for code point 257 is requested, the code goes out and | |
2807 | * calculates the property values for all 64 code points between 256 and 319, | |
2808 | * and stores these as a single 64-bit long bit vector, called a "swatch", | |
2809 | * under the key for code point 256. The key is the UTF-8 encoding for code | |
2810 | * point 256, minus the final byte. Thus, if the length of the UTF-8 encoding | |
2811 | * for a code point is 13 bytes, the key will be 12 bytes long. If the value | |
2812 | * for code point 258 is then requested, this code realizes that it would be | |
2813 | * stored under the key for 256, and would find that value and extract the | |
2814 | * relevant bit, offset from 256. | |
2815 | * | |
2816 | * Non-binary properties are stored in as many bits as necessary to represent | |
2817 | * their values (32 currently, though the code is more general than that), not | |
2818 | * as single bits, but the principal is the same: the value for each key is a | |
2819 | * vector that encompasses the property values for all code points whose UTF-8 | |
2820 | * representations are represented by the key. That is, for all code points | |
2821 | * whose UTF-8 representations are length N bytes, and the key is the first N-1 | |
2822 | * bytes of that. | |
680c470c | 2823 | */ |
a0ed51b3 | 2824 | UV |
680c470c | 2825 | Perl_swash_fetch(pTHX_ SV *swash, const U8 *ptr, bool do_utf8) |
a0ed51b3 | 2826 | { |
27da23d5 | 2827 | dVAR; |
ef8f7699 | 2828 | HV *const hv = MUTABLE_HV(SvRV(swash)); |
3568d838 JH |
2829 | U32 klen; |
2830 | U32 off; | |
a0ed51b3 | 2831 | STRLEN slen; |
7d85a32c | 2832 | STRLEN needents; |
cfd0369c | 2833 | const U8 *tmps = NULL; |
a0ed51b3 | 2834 | U32 bit; |
979f2922 | 2835 | SV *swatch; |
3568d838 | 2836 | U8 tmputf8[2]; |
35da51f7 | 2837 | const UV c = NATIVE_TO_ASCII(*ptr); |
3568d838 | 2838 | |
7918f24d NC |
2839 | PERL_ARGS_ASSERT_SWASH_FETCH; |
2840 | ||
dbe7a391 | 2841 | /* Convert to utf8 if not already */ |
3568d838 | 2842 | if (!do_utf8 && !UNI_IS_INVARIANT(c)) { |
979f2922 TS |
2843 | tmputf8[0] = (U8)UTF8_EIGHT_BIT_HI(c); |
2844 | tmputf8[1] = (U8)UTF8_EIGHT_BIT_LO(c); | |
2845 | ptr = tmputf8; | |
3568d838 JH |
2846 | } |
2847 | /* Given a UTF-X encoded char 0xAA..0xYY,0xZZ | |
37e2e78e | 2848 | * then the "swatch" is a vec() for all the chars which start |
3568d838 JH |
2849 | * with 0xAA..0xYY |
2850 | * So the key in the hash (klen) is length of encoded char -1 | |
2851 | */ | |
2852 | klen = UTF8SKIP(ptr) - 1; | |
2853 | off = ptr[klen]; | |
a0ed51b3 | 2854 | |
979f2922 | 2855 | if (klen == 0) { |
37e2e78e | 2856 | /* If char is invariant then swatch is for all the invariant chars |
1e54db1a | 2857 | * In both UTF-8 and UTF-8-MOD that happens to be UTF_CONTINUATION_MARK |
7d85a32c | 2858 | */ |
979f2922 TS |
2859 | needents = UTF_CONTINUATION_MARK; |
2860 | off = NATIVE_TO_UTF(ptr[klen]); | |
2861 | } | |
2862 | else { | |
7d85a32c | 2863 | /* If char is encoded then swatch is for the prefix */ |
979f2922 TS |
2864 | needents = (1 << UTF_ACCUMULATION_SHIFT); |
2865 | off = NATIVE_TO_UTF(ptr[klen]) & UTF_CONTINUATION_MASK; | |
2866 | } | |
7d85a32c | 2867 | |
a0ed51b3 LW |
2868 | /* |
2869 | * This single-entry cache saves about 1/3 of the utf8 overhead in test | |
2870 | * suite. (That is, only 7-8% overall over just a hash cache. Still, | |
2871 | * it's nothing to sniff at.) Pity we usually come through at least | |
2872 | * two function calls to get here... | |
2873 | * | |
2874 | * NB: this code assumes that swatches are never modified, once generated! | |
2875 | */ | |
2876 | ||
3568d838 | 2877 | if (hv == PL_last_swash_hv && |
a0ed51b3 | 2878 | klen == PL_last_swash_klen && |
27da23d5 | 2879 | (!klen || memEQ((char *)ptr, (char *)PL_last_swash_key, klen)) ) |
a0ed51b3 LW |
2880 | { |
2881 | tmps = PL_last_swash_tmps; | |
2882 | slen = PL_last_swash_slen; | |
2883 | } | |
2884 | else { | |
2885 | /* Try our second-level swatch cache, kept in a hash. */ | |
e1ec3a88 | 2886 | SV** svp = hv_fetch(hv, (const char*)ptr, klen, FALSE); |
a0ed51b3 | 2887 | |
b0e3252e | 2888 | /* If not cached, generate it via swatch_get */ |
979f2922 TS |
2889 | if (!svp || !SvPOK(*svp) |
2890 | || !(tmps = (const U8*)SvPV_const(*svp, slen))) { | |
2b9d42f0 NIS |
2891 | /* We use utf8n_to_uvuni() as we want an index into |
2892 | Unicode tables, not a native character number. | |
2893 | */ | |
aec46f14 | 2894 | const UV code_point = utf8n_to_uvuni(ptr, UTF8_MAXBYTES, 0, |
872c91ae JH |
2895 | ckWARN(WARN_UTF8) ? |
2896 | 0 : UTF8_ALLOW_ANY); | |
b0e3252e | 2897 | swatch = swatch_get(swash, |
979f2922 | 2898 | /* On EBCDIC & ~(0xA0-1) isn't a useful thing to do */ |
361ee0fe | 2899 | (klen) ? (code_point & ~((UV)needents - 1)) : 0, |
979f2922 TS |
2900 | needents); |
2901 | ||
923e4eb5 | 2902 | if (IN_PERL_COMPILETIME) |
623e6609 | 2903 | CopHINTS_set(PL_curcop, PL_hints); |
a0ed51b3 | 2904 | |
979f2922 | 2905 | svp = hv_store(hv, (const char *)ptr, klen, swatch, 0); |
a0ed51b3 | 2906 | |
979f2922 TS |
2907 | if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) |
2908 | || (slen << 3) < needents) | |
5637ef5b NC |
2909 | Perl_croak(aTHX_ "panic: swash_fetch got improper swatch, " |
2910 | "svp=%p, tmps=%p, slen=%"UVuf", needents=%"UVuf, | |
2911 | svp, tmps, (UV)slen, (UV)needents); | |
a0ed51b3 LW |
2912 | } |
2913 | ||
2914 | PL_last_swash_hv = hv; | |
16d8f38a | 2915 | assert(klen <= sizeof(PL_last_swash_key)); |
eac04b2e | 2916 | PL_last_swash_klen = (U8)klen; |
cfd0369c NC |
2917 | /* FIXME change interpvar.h? */ |
2918 | PL_last_swash_tmps = (U8 *) tmps; | |
a0ed51b3 LW |
2919 | PL_last_swash_slen = slen; |
2920 | if (klen) | |
2921 | Copy(ptr, PL_last_swash_key, klen, U8); | |
2922 | } | |
2923 | ||
a410ec23 KW |
2924 | if (UTF8_IS_SUPER(ptr) && ckWARN_d(WARN_NON_UNICODE)) { |
2925 | SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE); | |
2926 | ||
2927 | /* This outputs warnings for binary properties only, assuming that | |
2928 | * to_utf8_case() will output any for non-binary. Also, surrogates | |
2929 | * aren't checked for, as that would warn on things like /\p{Gc=Cs}/ */ | |
2930 | ||
9a53f6cf | 2931 | if (! bitssvp || SvUV(*bitssvp) == 1) { |
a410ec23 KW |
2932 | /* User-defined properties can silently match above-Unicode */ |
2933 | SV** const user_defined_svp = hv_fetchs(hv, "USER_DEFINED", FALSE); | |
2934 | if (! user_defined_svp || ! SvUV(*user_defined_svp)) { | |
2935 | const UV code_point = utf8n_to_uvuni(ptr, UTF8_MAXBYTES, 0, 0); | |
2936 | Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE), | |
2937 | "Code point 0x%04"UVXf" is not Unicode, all \\p{} matches fail; all \\P{} matches succeed", code_point); | |
2938 | } | |
2939 | } | |
2940 | } | |
2941 | ||
9faf8d75 | 2942 | switch ((int)((slen << 3) / needents)) { |
a0ed51b3 LW |
2943 | case 1: |
2944 | bit = 1 << (off & 7); | |
2945 | off >>= 3; | |
2946 | return (tmps[off] & bit) != 0; | |
2947 | case 8: | |
2948 | return tmps[off]; | |
2949 | case 16: | |
2950 | off <<= 1; | |
2951 | return (tmps[off] << 8) + tmps[off + 1] ; | |
2952 | case 32: | |
2953 | off <<= 2; | |
2954 | return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ; | |
2955 | } | |
5637ef5b NC |
2956 | Perl_croak(aTHX_ "panic: swash_fetch got swatch of unexpected bit width, " |
2957 | "slen=%"UVuf", needents=%"UVuf, (UV)slen, (UV)needents); | |
670f1322 | 2958 | NORETURN_FUNCTION_END; |
a0ed51b3 | 2959 | } |
2b9d42f0 | 2960 | |
319009ee KW |
2961 | /* Read a single line of the main body of the swash input text. These are of |
2962 | * the form: | |
2963 | * 0053 0056 0073 | |
2964 | * where each number is hex. The first two numbers form the minimum and | |
2965 | * maximum of a range, and the third is the value associated with the range. | |
2966 | * Not all swashes should have a third number | |
2967 | * | |
2968 | * On input: l points to the beginning of the line to be examined; it points | |
2969 | * to somewhere in the string of the whole input text, and is | |
2970 | * terminated by a \n or the null string terminator. | |
2971 | * lend points to the null terminator of that string | |
2972 | * wants_value is non-zero if the swash expects a third number | |
2973 | * typestr is the name of the swash's mapping, like 'ToLower' | |
2974 | * On output: *min, *max, and *val are set to the values read from the line. | |
2975 | * returns a pointer just beyond the line examined. If there was no | |
2976 | * valid min number on the line, returns lend+1 | |
2977 | */ | |
2978 | ||
2979 | STATIC U8* | |
2980 | S_swash_scan_list_line(pTHX_ U8* l, U8* const lend, UV* min, UV* max, UV* val, | |
2981 | const bool wants_value, const U8* const typestr) | |
2982 | { | |
2983 | const int typeto = typestr[0] == 'T' && typestr[1] == 'o'; | |
2984 | STRLEN numlen; /* Length of the number */ | |
02470786 KW |
2985 | I32 flags = PERL_SCAN_SILENT_ILLDIGIT |
2986 | | PERL_SCAN_DISALLOW_PREFIX | |
2987 | | PERL_SCAN_SILENT_NON_PORTABLE; | |
319009ee KW |
2988 | |
2989 | /* nl points to the next \n in the scan */ | |
2990 | U8* const nl = (U8*)memchr(l, '\n', lend - l); | |
2991 | ||
2992 | /* Get the first number on the line: the range minimum */ | |
2993 | numlen = lend - l; | |
2994 | *min = grok_hex((char *)l, &numlen, &flags, NULL); | |
2995 | if (numlen) /* If found a hex number, position past it */ | |
2996 | l += numlen; | |
2997 | else if (nl) { /* Else, go handle next line, if any */ | |
2998 | return nl + 1; /* 1 is length of "\n" */ | |
2999 | } | |
3000 | else { /* Else, no next line */ | |
3001 | return lend + 1; /* to LIST's end at which \n is not found */ | |
3002 | } | |
3003 | ||
3004 | /* The max range value follows, separated by a BLANK */ | |
3005 | if (isBLANK(*l)) { | |
3006 | ++l; | |
02470786 KW |
3007 | flags = PERL_SCAN_SILENT_ILLDIGIT |
3008 | | PERL_SCAN_DISALLOW_PREFIX | |
3009 | | PERL_SCAN_SILENT_NON_PORTABLE; | |
319009ee KW |
3010 | numlen = lend - l; |
3011 | *max = grok_hex((char *)l, &numlen, &flags, NULL); | |
3012 | if (numlen) | |
3013 | l += numlen; | |
3014 | else /* If no value here, it is a single element range */ | |
3015 | *max = *min; | |
3016 | ||
3017 | /* Non-binary tables have a third entry: what the first element of the | |
3018 | * range maps to */ | |
3019 | if (wants_value) { | |
3020 | if (isBLANK(*l)) { | |
3021 | ++l; | |
f90a9a02 KW |
3022 | |
3023 | /* The ToLc, etc table mappings are not in hex, and must be | |
3024 | * corrected by adding the code point to them */ | |
3025 | if (typeto) { | |
3026 | char *after_strtol = (char *) lend; | |
3027 | *val = Strtol((char *)l, &after_strtol, 10); | |
3028 | l = (U8 *) after_strtol; | |
f90a9a02 KW |
3029 | } |
3030 | else { /* Other tables are in hex, and are the correct result | |
3031 | without tweaking */ | |
a9d188b3 KW |
3032 | flags = PERL_SCAN_SILENT_ILLDIGIT |
3033 | | PERL_SCAN_DISALLOW_PREFIX | |
3034 | | PERL_SCAN_SILENT_NON_PORTABLE; | |
3035 | numlen = lend - l; | |
3036 | *val = grok_hex((char *)l, &numlen, &flags, NULL); | |
3037 | if (numlen) | |
3038 | l += numlen; | |
3039 | else | |
3040 | *val = 0; | |
f90a9a02 | 3041 | } |
319009ee KW |
3042 | } |
3043 | else { | |
3044 | *val = 0; | |
3045 | if (typeto) { | |
dcbac5bb | 3046 | /* diag_listed_as: To%s: illegal mapping '%s' */ |
319009ee KW |
3047 | Perl_croak(aTHX_ "%s: illegal mapping '%s'", |
3048 | typestr, l); | |
3049 | } | |
3050 | } | |
3051 | } | |
3052 | else | |
3053 | *val = 0; /* bits == 1, then any val should be ignored */ | |
3054 | } | |
3055 | else { /* Nothing following range min, should be single element with no | |
3056 | mapping expected */ | |
3057 | *max = *min; | |
3058 | if (wants_value) { | |
3059 | *val = 0; | |
3060 | if (typeto) { | |
dcbac5bb | 3061 | /* diag_listed_as: To%s: illegal mapping '%s' */ |
319009ee KW |
3062 | Perl_croak(aTHX_ "%s: illegal mapping '%s'", typestr, l); |
3063 | } | |
3064 | } | |
3065 | else | |
3066 | *val = 0; /* bits == 1, then val should be ignored */ | |
3067 | } | |
3068 | ||
3069 | /* Position to next line if any, or EOF */ | |
3070 | if (nl) | |
3071 | l = nl + 1; | |
3072 | else | |
3073 | l = lend; | |
3074 | ||
3075 | return l; | |
3076 | } | |
3077 | ||
979f2922 TS |
3078 | /* Note: |
3079 | * Returns a swatch (a bit vector string) for a code point sequence | |
3080 | * that starts from the value C<start> and comprises the number C<span>. | |
3081 | * A C<swash> must be an object created by SWASHNEW (see lib/utf8_heavy.pl). | |
3082 | * Should be used via swash_fetch, which will cache the swatch in C<swash>. | |
3083 | */ | |
3084 | STATIC SV* | |
b0e3252e | 3085 | S_swatch_get(pTHX_ SV* swash, UV start, UV span) |
979f2922 TS |
3086 | { |
3087 | SV *swatch; | |
77f9f126 | 3088 | U8 *l, *lend, *x, *xend, *s, *send; |
979f2922 | 3089 | STRLEN lcur, xcur, scur; |
ef8f7699 | 3090 | HV *const hv = MUTABLE_HV(SvRV(swash)); |
36eb48b4 KW |
3091 | SV** const invlistsvp = hv_fetchs(hv, "INVLIST", FALSE); |
3092 | ||
88d45d28 KW |
3093 | SV** listsvp = NULL; /* The string containing the main body of the table */ |
3094 | SV** extssvp = NULL; | |
3095 | SV** invert_it_svp = NULL; | |
3096 | U8* typestr = NULL; | |
786861f5 KW |
3097 | STRLEN bits; |
3098 | STRLEN octets; /* if bits == 1, then octets == 0 */ | |
3099 | UV none; | |
3100 | UV end = start + span; | |
972dd592 | 3101 | |
36eb48b4 | 3102 | if (invlistsvp == NULL) { |
786861f5 KW |
3103 | SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE); |
3104 | SV** const nonesvp = hv_fetchs(hv, "NONE", FALSE); | |
3105 | SV** const typesvp = hv_fetchs(hv, "TYPE", FALSE); | |
3106 | extssvp = hv_fetchs(hv, "EXTRAS", FALSE); | |
3107 | listsvp = hv_fetchs(hv, "LIST", FALSE); | |
3108 | invert_it_svp = hv_fetchs(hv, "INVERT_IT", FALSE); | |
3109 | ||
3110 | bits = SvUV(*bitssvp); | |
3111 | none = SvUV(*nonesvp); | |
3112 | typestr = (U8*)SvPV_nolen(*typesvp); | |
3113 | } | |
36eb48b4 KW |
3114 | else { |
3115 | bits = 1; | |
3116 | none = 0; | |
3117 | } | |
786861f5 | 3118 | octets = bits >> 3; /* if bits == 1, then octets == 0 */ |
979f2922 | 3119 | |
b0e3252e | 3120 | PERL_ARGS_ASSERT_SWATCH_GET; |
7918f24d | 3121 | |
979f2922 | 3122 | if (bits != 1 && bits != 8 && bits != 16 && bits != 32) { |
b0e3252e | 3123 | Perl_croak(aTHX_ "panic: swatch_get doesn't expect bits %"UVuf, |
660a4616 | 3124 | (UV)bits); |
979f2922 TS |
3125 | } |
3126 | ||
84ea5ef6 KW |
3127 | /* If overflowed, use the max possible */ |
3128 | if (end < start) { | |
3129 | end = UV_MAX; | |
3130 | span = end - start; | |
3131 | } | |
3132 | ||
979f2922 | 3133 | /* create and initialize $swatch */ |
979f2922 | 3134 | scur = octets ? (span * octets) : (span + 7) / 8; |
e524fe40 NC |
3135 | swatch = newSV(scur); |
3136 | SvPOK_on(swatch); | |
979f2922 TS |
3137 | s = (U8*)SvPVX(swatch); |
3138 | if (octets && none) { | |
0bd48802 | 3139 | const U8* const e = s + scur; |
979f2922 TS |
3140 | while (s < e) { |
3141 | if (bits == 8) | |
3142 | *s++ = (U8)(none & 0xff); | |
3143 | else if (bits == 16) { | |
3144 | *s++ = (U8)((none >> 8) & 0xff); | |
3145 | *s++ = (U8)( none & 0xff); | |
3146 | } | |
3147 | else if (bits == 32) { | |
3148 | *s++ = (U8)((none >> 24) & 0xff); | |
3149 | *s++ = (U8)((none >> 16) & 0xff); | |
3150 | *s++ = (U8)((none >> 8) & 0xff); | |
3151 | *s++ = (U8)( none & 0xff); | |
3152 | } | |
3153 | } | |
3154 | *s = '\0'; | |
3155 | } | |
3156 | else { | |
3157 | (void)memzero((U8*)s, scur + 1); | |
3158 | } | |
3159 | SvCUR_set(swatch, scur); | |
3160 | s = (U8*)SvPVX(swatch); | |
3161 | ||
36eb48b4 KW |
3162 | if (invlistsvp) { /* If has an inversion list set up use that */ |
3163 | _invlist_populate_swatch(*invlistsvp, start, end, s); | |
3164 | return swatch; | |
3165 | } | |
3166 | ||
3167 | /* read $swash->{LIST} */ | |
979f2922 TS |
3168 | l = (U8*)SvPV(*listsvp, lcur); |
3169 | lend = l + lcur; | |
3170 | while (l < lend) { | |
8ed25d53 | 3171 | UV min, max, val, upper; |
319009ee KW |
3172 | l = S_swash_scan_list_line(aTHX_ l, lend, &min, &max, &val, |
3173 | cBOOL(octets), typestr); | |
3174 | if (l > lend) { | |
979f2922 TS |
3175 | break; |
3176 | } | |
3177 | ||
972dd592 | 3178 | /* If looking for something beyond this range, go try the next one */ |
979f2922 TS |
3179 | if (max < start) |
3180 | continue; | |
3181 | ||
8ed25d53 KW |
3182 | /* <end> is generally 1 beyond where we want to set things, but at the |
3183 | * platform's infinity, where we can't go any higher, we want to | |
3184 | * include the code point at <end> */ | |
3185 | upper = (max < end) | |
3186 | ? max | |
3187 | : (max != UV_MAX || end != UV_MAX) | |
3188 | ? end - 1 | |
3189 | : end; | |
3190 | ||
979f2922 | 3191 | if (octets) { |
35da51f7 | 3192 | UV key; |
979f2922 TS |
3193 | if (min < start) { |
3194 | if (!none || val < none) { | |
3195 | val += start - min; | |
3196 | } | |
3197 | min = start; | |
3198 | } | |
8ed25d53 | 3199 | for (key = min; key <= upper; key++) { |
979f2922 | 3200 | STRLEN offset; |
979f2922 TS |
3201 | /* offset must be non-negative (start <= min <= key < end) */ |
3202 | offset = octets * (key - start); | |
3203 | if (bits == 8) | |
3204 | s[offset] = (U8)(val & 0xff); | |
3205 | else if (bits == 16) { | |
3206 | s[offset ] = (U8)((val >> 8) & 0xff); | |
3207 | s[offset + 1] = (U8)( val & 0xff); | |
3208 | } | |
3209 | else if (bits == 32) { | |
3210 | s[offset ] = (U8)((val >> 24) & 0xff); | |
3211 | s[offset + 1] = (U8)((val >> 16) & 0xff); | |
3212 | s[offset + 2] = (U8)((val >> 8) & 0xff); | |
3213 | s[offset + 3] = (U8)( val & 0xff); | |
3214 | } | |
3215 | ||
3216 | if (!none || val < none) | |
3217 | ++val; | |
3218 | } | |
3219 | } | |
711a919c | 3220 | else { /* bits == 1, then val should be ignored */ |
35da51f7 | 3221 | UV key; |
979f2922 TS |
3222 | if (min < start) |
3223 | min = start; | |
6cb05c12 | 3224 | |
8ed25d53 | 3225 | for (key = min; key <= upper; key++) { |
0bd48802 | 3226 | const STRLEN offset = (STRLEN)(key - start); |
979f2922 TS |
3227 | s[offset >> 3] |= 1 << (offset & 7); |
3228 | } | |
3229 | } | |
3230 | } /* while */ | |
979f2922 | 3231 | |
9479a769 | 3232 | /* Invert if the data says it should be. Assumes that bits == 1 */ |
77f9f126 | 3233 | if (invert_it_svp && SvUV(*invert_it_svp)) { |
0bda3001 KW |
3234 | |
3235 | /* Unicode properties should come with all bits above PERL_UNICODE_MAX | |
3236 | * be 0, and their inversion should also be 0, as we don't succeed any | |
3237 | * Unicode property matches for non-Unicode code points */ | |
3238 | if (start <= PERL_UNICODE_MAX) { | |
3239 | ||
3240 | /* The code below assumes that we never cross the | |
3241 | * Unicode/above-Unicode boundary in a range, as otherwise we would | |
3242 | * have to figure out where to stop flipping the bits. Since this | |
3243 | * boundary is divisible by a large power of 2, and swatches comes | |
3244 | * in small powers of 2, this should be a valid assumption */ | |
3245 | assert(start + span - 1 <= PERL_UNICODE_MAX); | |
3246 | ||
507a8485 KW |
3247 | send = s + scur; |
3248 | while (s < send) { | |
3249 | *s = ~(*s); | |
3250 | s++; | |
3251 | } | |
0bda3001 | 3252 | } |
77f9f126 KW |
3253 | } |
3254 | ||
d73c39c5 KW |
3255 | /* read $swash->{EXTRAS} |
3256 | * This code also copied to swash_to_invlist() below */ | |
979f2922 TS |
3257 | x = (U8*)SvPV(*extssvp, xcur); |
3258 | xend = x + xcur; | |
3259 | while (x < xend) { | |
3260 | STRLEN namelen; | |
3261 | U8 *namestr; | |
3262 | SV** othersvp; | |
3263 | HV* otherhv; | |
3264 | STRLEN otherbits; | |
3265 | SV **otherbitssvp, *other; | |
711a919c | 3266 | U8 *s, *o, *nl; |
979f2922 TS |
3267 | STRLEN slen, olen; |
3268 | ||
35da51f7 | 3269 | const U8 opc = *x++; |
979f2922 TS |
3270 | if (opc == '\n') |
3271 | continue; | |
3272 | ||
3273 | nl = (U8*)memchr(x, '\n', xend - x); | |
3274 | ||
3275 | if (opc != '-' && opc != '+' && opc != '!' && opc != '&') { | |
3276 | if (nl) { | |
3277 | x = nl + 1; /* 1 is length of "\n" */ | |
3278 | continue; | |
3279 | } | |
3280 | else { | |
3281 | x = xend; /* to EXTRAS' end at which \n is not found */ | |
3282 | break; | |
3283 | } | |
3284 | } | |
3285 | ||
3286 | namestr = x; | |
3287 | if (nl) { | |
3288 | namelen = nl - namestr; | |
3289 | x = nl + 1; | |
3290 | } | |
3291 | else { | |
3292 | namelen = xend - namestr; | |
3293 | x = xend; | |
3294 | } | |
3295 | ||
3296 | othersvp = hv_fetch(hv, (char *)namestr, namelen, FALSE); | |
ef8f7699 | 3297 | otherhv = MUTABLE_HV(SvRV(*othersvp)); |
017a3ce5 | 3298 | otherbitssvp = hv_fetchs(otherhv, "BITS", FALSE); |
979f2922 TS |
3299 | otherbits = (STRLEN)SvUV(*otherbitssvp); |
3300 | if (bits < otherbits) | |
5637ef5b NC |
3301 | Perl_croak(aTHX_ "panic: swatch_get found swatch size mismatch, " |
3302 | "bits=%"UVuf", otherbits=%"UVuf, (UV)bits, (UV)otherbits); | |
979f2922 TS |
3303 | |
3304 | /* The "other" swatch must be destroyed after. */ | |
b0e3252e | 3305 | other = swatch_get(*othersvp, start, span); |
979f2922 TS |
3306 | o = (U8*)SvPV(other, olen); |
3307 | ||
3308 | if (!olen) | |
b0e3252e | 3309 | Perl_croak(aTHX_ "panic: swatch_get got improper swatch"); |
979f2922 TS |
3310 | |
3311 | s = (U8*)SvPV(swatch, slen); | |
3312 | if (bits == 1 && otherbits == 1) { | |
3313 | if (slen != olen) | |
5637ef5b NC |
3314 | Perl_croak(aTHX_ "panic: swatch_get found swatch length " |
3315 | "mismatch, slen=%"UVuf", olen=%"UVuf, | |
3316 | (UV)slen, (UV)olen); | |
979f2922 TS |
3317 | |
3318 | switch (opc) { | |
3319 | case '+': | |
3320 | while (slen--) | |
3321 | *s++ |= *o++; | |
3322 | break; | |
3323 | case '!': | |
3324 | while (slen--) | |
3325 | *s++ |= ~*o++; | |
3326 | break; | |
3327 | case '-': | |
3328 | while (slen--) | |
3329 | *s++ &= ~*o++; | |
3330 | break; | |
3331 | case '&': | |
3332 | while (slen--) | |
3333 | *s++ &= *o++; | |
3334 | break; | |
3335 | default: | |
3336 | break; | |
3337 | } | |
3338 | } | |
711a919c | 3339 | else { |
979f2922 TS |
3340 | STRLEN otheroctets = otherbits >> 3; |
3341 | STRLEN offset = 0; | |
35da51f7 | 3342 | U8* const send = s + slen; |
979f2922 TS |
3343 | |
3344 | while (s < send) { | |
3345 | UV otherval = 0; | |
3346 | ||
3347 | if (otherbits == 1) { | |
3348 | otherval = (o[offset >> 3] >> (offset & 7)) & 1; | |
3349 | ++offset; | |
3350 | } | |
3351 | else { | |
3352 | STRLEN vlen = otheroctets; | |
3353 | otherval = *o++; | |
3354 | while (--vlen) { | |
3355 | otherval <<= 8; | |
3356 | otherval |= *o++; | |
3357 | } | |
3358 | } | |
3359 | ||
711a919c | 3360 | if (opc == '+' && otherval) |
6f207bd3 | 3361 | NOOP; /* replace with otherval */ |
979f2922 TS |
3362 | else if (opc == '!' && !otherval) |
3363 | otherval = 1; | |
3364 | else if (opc == '-' && otherval) | |
3365 | otherval = 0; | |
3366 | else if (opc == '&' && !otherval) | |
3367 | otherval = 0; | |
3368 | else { | |
711a919c | 3369 | s += octets; /* no replacement */ |
979f2922 TS |
3370 | continue; |
3371 | } | |
3372 | ||
3373 | if (bits == 8) | |
3374 | *s++ = (U8)( otherval & 0xff); | |
3375 | else if (bits == 16) { | |
3376 | *s++ = (U8)((otherval >> 8) & 0xff); | |
3377 | *s++ = (U8)( otherval & 0xff); | |
3378 | } | |
3379 | else if (bits == 32) { | |
3380 | *s++ = (U8)((otherval >> 24) & 0xff); | |
3381 | *s++ = (U8)((otherval >> 16) & 0xff); | |
3382 | *s++ = (U8)((otherval >> 8) & 0xff); | |
3383 | *s++ = (U8)( otherval & 0xff); | |
3384 | } | |
3385 | } | |
3386 | } | |
3387 | sv_free(other); /* through with it! */ | |
3388 | } /* while */ | |
3389 | return swatch; | |
3390 | } | |
3391 | ||
064c021d | 3392 | HV* |
4c2e1131 | 3393 | Perl__swash_inversion_hash(pTHX_ SV* const swash) |
064c021d KW |
3394 | { |
3395 | ||
5662e334 KW |
3396 | /* Subject to change or removal. For use only in one place in regcomp.c. |
3397 | * Can't be used on a property that is subject to user override, as it | |
3398 | * relies on the value of SPECIALS in the swash which would be set by | |
3399 | * utf8_heavy.pl to the hash in the non-overriden file, and hence is not set | |
3400 | * for overridden properties | |
064c021d KW |
3401 | * |
3402 | * Returns a hash which is the inversion and closure of a swash mapping. | |
3403 | * For example, consider the input lines: | |
3404 | * 004B 006B | |
3405 | * 004C 006C | |
3406 | * 212A 006B | |
3407 | * | |
3408 | * The returned hash would have two keys, the utf8 for 006B and the utf8 for | |
3409 | * 006C. The value for each key is an array. For 006C, the array would | |
3410 | * have a two elements, the utf8 for itself, and for 004C. For 006B, there | |
3411 | * would be three elements in its array, the utf8 for 006B, 004B and 212A. | |
3412 | * | |
3413 | * Essentially, for any code point, it gives all the code points that map to | |
3414 | * it, or the list of 'froms' for that point. | |
3415 | * | |
5662e334 KW |
3416 | * Currently it ignores any additions or deletions from other swashes, |
3417 | * looking at just the main body of the swash, and if there are SPECIALS | |
3418 | * in the swash, at that hash | |
3419 | * | |
3420 | * The specials hash can be extra code points, and most likely consists of | |
3421 | * maps from single code points to multiple ones (each expressed as a string | |
3422 | * of utf8 characters). This function currently returns only 1-1 mappings. | |
3423 | * However consider this possible input in the specials hash: | |
3424 | * "\xEF\xAC\x85" => "\x{0073}\x{0074}", # U+FB05 => 0073 0074 | |
3425 | * "\xEF\xAC\x86" => "\x{0073}\x{0074}", # U+FB06 => 0073 0074 | |
3426 | * | |
3427 | * Both FB05 and FB06 map to the same multi-char sequence, which we don't | |
3428 | * currently handle. But it also means that FB05 and FB06 are equivalent in | |
3429 | * a 1-1 mapping which we should handle, and this relationship may not be in | |
3430 | * the main table. Therefore this function examines all the multi-char | |
3431 | * sequences and adds the 1-1 mappings that come out of that. */ | |
064c021d KW |
3432 | |
3433 | U8 *l, *lend; | |
3434 | STRLEN lcur; | |
3435 | HV *const hv = MUTABLE_HV(SvRV(swash)); | |
3436 | ||
3437 | /* The string containing the main body of the table */ | |
3438 | SV** const listsvp = hv_fetchs(hv, "LIST", FALSE); | |
3439 | ||
3440 | SV** const typesvp = hv_fetchs(hv, "TYPE", FALSE); | |
3441 | SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE); | |
3442 | SV** const nonesvp = hv_fetchs(hv, "NONE", FALSE); | |
3443 | /*SV** const extssvp = hv_fetchs(hv, "EXTRAS", FALSE);*/ | |
3444 | const U8* const typestr = (U8*)SvPV_nolen(*typesvp); | |
3445 | const STRLEN bits = SvUV(*bitssvp); | |
3446 | const STRLEN octets = bits >> 3; /* if bits == 1, then octets == 0 */ | |
3447 | const UV none = SvUV(*nonesvp); | |
5662e334 | 3448 | SV **specials_p = hv_fetchs(hv, "SPECIALS", 0); |
064c021d KW |
3449 | |
3450 | HV* ret = newHV(); | |
3451 | ||
3452 | PERL_ARGS_ASSERT__SWASH_INVERSION_HASH; | |
3453 | ||
3454 | /* Must have at least 8 bits to get the mappings */ | |
3455 | if (bits != 8 && bits != 16 && bits != 32) { | |
3456 | Perl_croak(aTHX_ "panic: swash_inversion_hash doesn't expect bits %"UVuf, | |
3457 | (UV)bits); | |
3458 | } | |
3459 | ||
5662e334 KW |
3460 | if (specials_p) { /* It might be "special" (sometimes, but not always, a |
3461 | mapping to more than one character */ | |
3462 | ||
3463 | /* Construct an inverse mapping hash for the specials */ | |
3464 | HV * const specials_hv = MUTABLE_HV(SvRV(*specials_p)); | |
3465 | HV * specials_inverse = newHV(); | |
3466 | char *char_from; /* the lhs of the map */ | |
3467 | I32 from_len; /* its byte length */ | |
3468 | char *char_to; /* the rhs of the map */ | |
3469 | I32 to_len; /* its byte length */ | |
3470 | SV *sv_to; /* and in a sv */ | |
3471 | AV* from_list; /* list of things that map to each 'to' */ | |
3472 | ||
3473 | hv_iterinit(specials_hv); | |
3474 | ||
3475 | /* The keys are the characters (in utf8) that map to the corresponding | |
3476 | * utf8 string value. Iterate through the list creating the inverse | |
3477 | * list. */ | |
3478 | while ((sv_to = hv_iternextsv(specials_hv, &char_from, &from_len))) { | |
3479 | SV** listp; | |
3480 | if (! SvPOK(sv_to)) { | |
5637ef5b NC |
3481 | Perl_croak(aTHX_ "panic: value returned from hv_iternextsv() " |
3482 | "unexpectedly is not a string, flags=%lu", | |
3483 | (unsigned long)SvFLAGS(sv_to)); | |
5662e334 KW |
3484 | } |
3485 | /*DEBUG_U(PerlIO_printf(Perl_debug_log, "Found mapping from %"UVXf", First char of to is %"UVXf"\n", utf8_to_uvchr((U8*) char_from, 0), utf8_to_uvchr((U8*) SvPVX(sv_to), 0)));*/ | |
3486 | ||
3487 | /* Each key in the inverse list is a mapped-to value, and the key's | |
3488 | * hash value is a list of the strings (each in utf8) that map to | |
3489 | * it. Those strings are all one character long */ | |
3490 | if ((listp = hv_fetch(specials_inverse, | |
3491 | SvPVX(sv_to), | |
3492 | SvCUR(sv_to), 0))) | |
3493 | { | |
3494 | from_list = (AV*) *listp; | |
3495 | } | |
3496 | else { /* No entry yet for it: create one */ | |
3497 | from_list = newAV(); | |
3498 | if (! hv_store(specials_inverse, | |
3499 | SvPVX(sv_to), | |
3500 | SvCUR(sv_to), | |
3501 | (SV*) from_list, 0)) | |
3502 | { | |
3503 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
3504 | } | |
3505 | } | |
3506 | ||
3507 | /* Here have the list associated with this 'to' (perhaps newly | |
3508 | * created and empty). Just add to it. Note that we ASSUME that | |
3509 | * the input is guaranteed to not have duplications, so we don't | |
3510 | * check for that. Duplications just slow down execution time. */ | |
3511 | av_push(from_list, newSVpvn_utf8(char_from, from_len, TRUE)); | |
3512 | } | |
3513 | ||
3514 | /* Here, 'specials_inverse' contains the inverse mapping. Go through | |
3515 | * it looking for cases like the FB05/FB06 examples above. There would | |
3516 | * be an entry in the hash like | |
3517 | * 'st' => [ FB05, FB06 ] | |
3518 | * In this example we will create two lists that get stored in the | |
3519 | * returned hash, 'ret': | |
3520 | * FB05 => [ FB05, FB06 ] | |
3521 | * FB06 => [ FB05, FB06 ] | |
3522 | * | |
3523 | * Note that there is nothing to do if the array only has one element. | |
3524 | * (In the normal 1-1 case handled below, we don't have to worry about | |
3525 | * two lists, as everything gets tied to the single list that is | |
3526 | * generated for the single character 'to'. But here, we are omitting | |
3527 | * that list, ('st' in the example), so must have multiple lists.) */ | |
3528 | while ((from_list = (AV *) hv_iternextsv(specials_inverse, | |
3529 | &char_to, &to_len))) | |
3530 | { | |
3531 | if (av_len(from_list) > 0) { | |
3532 | int i; | |
3533 | ||
3534 | /* We iterate over all combinations of i,j to place each code | |
3535 | * point on each list */ | |
3536 | for (i = 0; i <= av_len(from_list); i++) { | |
3537 | int j; | |
3538 | AV* i_list = newAV(); | |
3539 | SV** entryp = av_fetch(from_list, i, FALSE); | |
3540 | if (entryp == NULL) { | |
3541 | Perl_croak(aTHX_ "panic: av_fetch() unexpectedly failed"); | |
3542 | } | |
3543 | if (hv_fetch(ret, SvPVX(*entryp), SvCUR(*entryp), FALSE)) { | |
3544 | Perl_croak(aTHX_ "panic: unexpected entry for %s", SvPVX(*entryp)); | |
3545 | } | |
3546 | if (! hv_store(ret, SvPVX(*entryp), SvCUR(*entryp), | |
3547 | (SV*) i_list, FALSE)) | |
3548 | { | |
3549 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
3550 | } | |
3551 | ||
3552 | /* For debugging: UV u = utf8_to_uvchr((U8*) SvPVX(*entryp), 0);*/ | |
3553 | for (j = 0; j <= av_len(from_list); j++) { | |
3554 | entryp = av_fetch(from_list, j, FALSE); | |
3555 | if (entryp == NULL) { | |
3556 | Perl_croak(aTHX_ "panic: av_fetch() unexpectedly failed"); | |
3557 | } | |
3558 | ||
3559 | /* When i==j this adds itself to the list */ | |
3560 | av_push(i_list, newSVuv(utf8_to_uvchr( | |
3561 | (U8*) SvPVX(*entryp), 0))); | |
3562 | /*DEBUG_U(PerlIO_printf(Perl_debug_log, "Adding %"UVXf" to list for %"UVXf"\n", utf8_to_uvchr((U8*) SvPVX(*entryp), 0), u));*/ | |
3563 | } | |
3564 | } | |
3565 | } | |
3566 | } | |
3567 | SvREFCNT_dec(specials_inverse); /* done with it */ | |
3568 | } /* End of specials */ | |
3569 | ||
064c021d KW |
3570 | /* read $swash->{LIST} */ |
3571 | l = (U8*)SvPV(*listsvp, lcur); | |
3572 | lend = l + lcur; | |
3573 | ||
3574 | /* Go through each input line */ | |
3575 | while (l < lend) { | |
3576 | UV min, max, val; | |
3577 | UV inverse; | |
3578 | l = S_swash_scan_list_line(aTHX_ l, lend, &min, &max, &val, | |
3579 | cBOOL(octets), typestr); | |
3580 | if (l > lend) { | |
3581 | break; | |
3582 | } | |
3583 | ||
3584 | /* Each element in the range is to be inverted */ | |
3585 | for (inverse = min; inverse <= max; inverse++) { | |
3586 | AV* list; | |
064c021d KW |
3587 | SV** listp; |
3588 | IV i; | |
3589 | bool found_key = FALSE; | |
5662e334 | 3590 | bool found_inverse = FALSE; |
064c021d KW |
3591 | |
3592 | /* The key is the inverse mapping */ | |
3593 | char key[UTF8_MAXBYTES+1]; | |
3594 | char* key_end = (char *) uvuni_to_utf8((U8*) key, val); | |
3595 | STRLEN key_len = key_end - key; | |
3596 | ||
064c021d KW |
3597 | /* Get the list for the map */ |
3598 | if ((listp = hv_fetch(ret, key, key_len, FALSE))) { | |
3599 | list = (AV*) *listp; | |
3600 | } | |
3601 | else { /* No entry yet for it: create one */ | |
3602 | list = newAV(); | |
3603 | if (! hv_store(ret, key, key_len, (SV*) list, FALSE)) { | |
3604 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
3605 | } | |
3606 | } | |
3607 | ||
5662e334 KW |
3608 | /* Look through list to see if this inverse mapping already is |
3609 | * listed, or if there is a mapping to itself already */ | |
508f7cfa | 3610 | for (i = 0; i <= av_len(list); i++) { |
064c021d KW |
3611 | SV** entryp = av_fetch(list, i, FALSE); |
3612 | SV* entry; | |
3613 | if (entryp == NULL) { | |
3614 | Perl_croak(aTHX_ "panic: av_fetch() unexpectedly failed"); | |
3615 | } | |
3616 | entry = *entryp; | |
5662e334 | 3617 | /*DEBUG_U(PerlIO_printf(Perl_debug_log, "list for %"UVXf" contains %"UVXf"\n", val, SvUV(entry)));*/ |
56ca34ca | 3618 | if (SvUV(entry) == val) { |
064c021d | 3619 | found_key = TRUE; |
5662e334 KW |
3620 | } |
3621 | if (SvUV(entry) == inverse) { | |
3622 | found_inverse = TRUE; | |
3623 | } | |
3624 | ||
3625 | /* No need to continue searching if found everything we are | |
3626 | * looking for */ | |
3627 | if (found_key && found_inverse) { | |
064c021d KW |
3628 | break; |
3629 | } | |
3630 | } | |
56ca34ca KW |
3631 | |
3632 | /* Make sure there is a mapping to itself on the list */ | |
064c021d | 3633 | if (! found_key) { |
d397ff6a | 3634 | av_push(list, newSVuv(val)); |
5662e334 | 3635 | /*DEBUG_U(PerlIO_printf(Perl_debug_log, "Adding %"UVXf" to list for %"UVXf"\n", val, val));*/ |
064c021d KW |
3636 | } |
3637 | ||
3638 | ||
3639 | /* Simply add the value to the list */ | |
5662e334 KW |
3640 | if (! found_inverse) { |
3641 | av_push(list, newSVuv(inverse)); | |
3642 | /*DEBUG_U(PerlIO_printf(Perl_debug_log, "Adding %"UVXf" to list for %"UVXf"\n", inverse, val));*/ | |
3643 | } | |
064c021d | 3644 | |
b0e3252e | 3645 | /* swatch_get() increments the value of val for each element in the |
064c021d KW |
3646 | * range. That makes more compact tables possible. You can |
3647 | * express the capitalization, for example, of all consecutive | |
3648 | * letters with a single line: 0061\t007A\t0041 This maps 0061 to | |
3649 | * 0041, 0062 to 0042, etc. I (khw) have never understood 'none', | |
bd3f2f94 | 3650 | * and it's not documented; it appears to be used only in |
b0e3252e | 3651 | * implementing tr//; I copied the semantics from swatch_get(), just |
bd3f2f94 | 3652 | * in case */ |
064c021d KW |
3653 | if (!none || val < none) { |
3654 | ++val; | |
3655 | } | |
3656 | } | |
3657 | } | |
3658 | ||
3659 | return ret; | |
3660 | } | |
3661 | ||
a25abddc | 3662 | SV* |
d764b54e KW |
3663 | Perl__swash_to_invlist(pTHX_ SV* const swash) |
3664 | { | |
3665 | ||
3666 | /* Subject to change or removal. For use only in one place in regcomp.c */ | |
3667 | ||
3668 | U8 *l, *lend; | |
3669 | char *loc; | |
3670 | STRLEN lcur; | |
3671 | HV *const hv = MUTABLE_HV(SvRV(swash)); | |
3672 | UV elements = 0; /* Number of elements in the inversion list */ | |
b443038a | 3673 | U8 empty[] = ""; |
d764b54e KW |
3674 | |
3675 | /* The string containing the main body of the table */ | |
3676 | SV** const listsvp = hv_fetchs(hv, "LIST", FALSE); | |
3677 | SV** const typesvp = hv_fetchs(hv, "TYPE", FALSE); | |
3678 | SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE); | |
d73c39c5 | 3679 | SV** const extssvp = hv_fetchs(hv, "EXTRAS", FALSE); |
77f9f126 | 3680 | SV** const invert_it_svp = hv_fetchs(hv, "INVERT_IT", FALSE); |
d764b54e KW |
3681 | |
3682 | const U8* const typestr = (U8*)SvPV_nolen(*typesvp); | |
3683 | const STRLEN bits = SvUV(*bitssvp); | |
3684 | const STRLEN octets = bits >> 3; /* if bits == 1, then octets == 0 */ | |
d73c39c5 KW |
3685 | U8 *x, *xend; |
3686 | STRLEN xcur; | |
d764b54e | 3687 | |
a25abddc | 3688 | SV* invlist; |
d764b54e KW |
3689 | |
3690 | PERL_ARGS_ASSERT__SWASH_TO_INVLIST; | |
3691 | ||
3692 | /* read $swash->{LIST} */ | |
b443038a KW |
3693 | if (SvPOK(*listsvp)) { |
3694 | l = (U8*)SvPV(*listsvp, lcur); | |
3695 | } | |
3696 | else { | |
3697 | /* LIST legitimately doesn't contain a string during compilation phases | |
3698 | * of Perl itself, before the Unicode tables are generated. In this | |
3699 | * case, just fake things up by creating an empty list */ | |
3700 | l = empty; | |
3701 | lcur = 0; | |
3702 | } | |
d764b54e KW |
3703 | loc = (char *) l; |
3704 | lend = l + lcur; | |
3705 | ||
3706 | /* Scan the input to count the number of lines to preallocate array size | |
3707 | * based on worst possible case, which is each line in the input creates 2 | |
3708 | * elements in the inversion list: 1) the beginning of a range in the list; | |
3709 | * 2) the beginning of a range not in the list. */ | |
3710 | while ((loc = (strchr(loc, '\n'))) != NULL) { | |
3711 | elements += 2; | |
3712 | loc++; | |
3713 | } | |
3714 | ||
3715 | /* If the ending is somehow corrupt and isn't a new line, add another | |
3716 | * element for the final range that isn't in the inversion list */ | |
fd05e003 KW |
3717 | if (! (*lend == '\n' |
3718 | || (*lend == '\0' && (lcur == 0 || *(lend - 1) == '\n')))) | |
3719 | { | |
d764b54e KW |
3720 | elements++; |
3721 | } | |
3722 | ||
3723 | invlist = _new_invlist(elements); | |
3724 | ||
3725 | /* Now go through the input again, adding each range to the list */ | |
3726 | while (l < lend) { | |
3727 | UV start, end; | |
3728 | UV val; /* Not used by this function */ | |
3729 | ||
3730 | l = S_swash_scan_list_line(aTHX_ l, lend, &start, &end, &val, | |
3731 | cBOOL(octets), typestr); | |
3732 | ||
3733 | if (l > lend) { | |
3734 | break; | |
3735 | } | |
3736 | ||
3737 | _append_range_to_invlist(invlist, start, end); | |
3738 | } | |
3739 | ||
77f9f126 KW |
3740 | /* Invert if the data says it should be */ |
3741 | if (invert_it_svp && SvUV(*invert_it_svp)) { | |
45bb2768 | 3742 | _invlist_invert_prop(invlist); |
77f9f126 KW |
3743 | } |
3744 | ||
b0e3252e | 3745 | /* This code is copied from swatch_get() |
d73c39c5 KW |
3746 | * read $swash->{EXTRAS} */ |
3747 | x = (U8*)SvPV(*extssvp, xcur); | |
3748 | xend = x + xcur; | |
3749 | while (x < xend) { | |
3750 | STRLEN namelen; | |
3751 | U8 *namestr; | |
3752 | SV** othersvp; | |
3753 | HV* otherhv; | |
3754 | STRLEN otherbits; | |
3755 | SV **otherbitssvp, *other; | |
3756 | U8 *nl; | |
3757 | ||
3758 | const U8 opc = *x++; | |
3759 | if (opc == '\n') | |
3760 | continue; | |
3761 | ||
3762 | nl = (U8*)memchr(x, '\n', xend - x); | |
3763 | ||
3764 | if (opc != '-' && opc != '+' && opc != '!' && opc != '&') { | |
3765 | if (nl) { | |
3766 | x = nl + 1; /* 1 is length of "\n" */ | |
3767 | continue; | |
3768 | } | |
3769 | else { | |
3770 | x = xend; /* to EXTRAS' end at which \n is not found */ | |
3771 | break; | |
3772 | } | |
3773 | } | |
3774 | ||
3775 | namestr = x; | |
3776 | if (nl) { | |
3777 | namelen = nl - namestr; | |
3778 | x = nl + 1; | |
3779 | } | |
3780 | else { | |
3781 | namelen = xend - namestr; | |
3782 | x = xend; | |
3783 | } | |
3784 | ||
3785 | othersvp = hv_fetch(hv, (char *)namestr, namelen, FALSE); | |
3786 | otherhv = MUTABLE_HV(SvRV(*othersvp)); | |
3787 | otherbitssvp = hv_fetchs(otherhv, "BITS", FALSE); | |
3788 | otherbits = (STRLEN)SvUV(*otherbitssvp); | |
3789 | ||
3790 | if (bits != otherbits || bits != 1) { | |
5637ef5b NC |
3791 | Perl_croak(aTHX_ "panic: _swash_to_invlist only operates on boolean " |
3792 | "properties, bits=%"UVuf", otherbits=%"UVuf, | |
3793 | (UV)bits, (UV)otherbits); | |
d73c39c5 KW |
3794 | } |
3795 | ||
3796 | /* The "other" swatch must be destroyed after. */ | |
3797 | other = _swash_to_invlist((SV *)*othersvp); | |
3798 | ||
b0e3252e | 3799 | /* End of code copied from swatch_get() */ |
d73c39c5 KW |
3800 | switch (opc) { |
3801 | case '+': | |
3802 | _invlist_union(invlist, other, &invlist); | |
3803 | break; | |
3804 | case '!': | |
3805 | _invlist_invert(other); | |
3806 | _invlist_union(invlist, other, &invlist); | |
3807 | break; | |
3808 | case '-': | |
3809 | _invlist_subtract(invlist, other, &invlist); | |
3810 | break; | |
3811 | case '&': | |
3812 | _invlist_intersection(invlist, other, &invlist); | |
3813 | break; | |
3814 | <