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 | 33 | #include "perl.h" |
b992490d | 34 | #include "invlist_inline.h" |
a0ed51b3 | 35 | |
27da23d5 JH |
36 | static const char unees[] = |
37 | "Malformed UTF-8 character (unexpected end of string)"; | |
760c7c2f | 38 | static const char cp_above_legal_max[] = |
2d212e86 | 39 | "Use of code point 0x%"UVXf" is deprecated; the permissible max is 0x%"UVXf""; |
760c7c2f | 40 | |
114d9c4d | 41 | #define MAX_NON_DEPRECATED_CP ((UV) (IV_MAX)) |
901b21bf | 42 | |
48ef279e | 43 | /* |
ccfc67b7 | 44 | =head1 Unicode Support |
7fefc6c1 | 45 | These are various utility functions for manipulating UTF8-encoded |
72d33970 | 46 | strings. For the uninitiated, this is a method of representing arbitrary |
61296642 | 47 | Unicode characters as a variable number of bytes, in such a way that |
56da48f7 DM |
48 | characters in the ASCII range are unmodified, and a zero byte never appears |
49 | within non-zero characters. | |
166f8a29 | 50 | |
eaf7a4d2 CS |
51 | =cut |
52 | */ | |
53 | ||
54 | /* | |
378516de | 55 | =for apidoc uvoffuni_to_utf8_flags |
eebe1485 | 56 | |
a27992cc | 57 | THIS FUNCTION SHOULD BE USED IN ONLY VERY SPECIALIZED CIRCUMSTANCES. |
de69f3af KW |
58 | Instead, B<Almost all code should use L</uvchr_to_utf8> or |
59 | L</uvchr_to_utf8_flags>>. | |
a27992cc | 60 | |
de69f3af KW |
61 | This function is like them, but the input is a strict Unicode |
62 | (as opposed to native) code point. Only in very rare circumstances should code | |
63 | not be using the native code point. | |
949cf498 | 64 | |
efa9cd84 | 65 | For details, see the description for L</uvchr_to_utf8_flags>. |
949cf498 | 66 | |
eebe1485 SC |
67 | =cut |
68 | */ | |
69 | ||
8ee1cdcb KW |
70 | #define HANDLE_UNICODE_SURROGATE(uv, flags) \ |
71 | STMT_START { \ | |
72 | if (flags & UNICODE_WARN_SURROGATE) { \ | |
73 | Perl_ck_warner_d(aTHX_ packWARN(WARN_SURROGATE), \ | |
74 | "UTF-16 surrogate U+%04"UVXf, uv); \ | |
75 | } \ | |
76 | if (flags & UNICODE_DISALLOW_SURROGATE) { \ | |
77 | return NULL; \ | |
78 | } \ | |
79 | } STMT_END; | |
80 | ||
81 | #define HANDLE_UNICODE_NONCHAR(uv, flags) \ | |
82 | STMT_START { \ | |
83 | if (flags & UNICODE_WARN_NONCHAR) { \ | |
84 | Perl_ck_warner_d(aTHX_ packWARN(WARN_NONCHAR), \ | |
85 | "Unicode non-character U+%04"UVXf" is not " \ | |
86 | "recommended for open interchange", uv); \ | |
87 | } \ | |
88 | if (flags & UNICODE_DISALLOW_NONCHAR) { \ | |
89 | return NULL; \ | |
90 | } \ | |
91 | } STMT_END; | |
92 | ||
ba6ed43c KW |
93 | /* Use shorter names internally in this file */ |
94 | #define SHIFT UTF_ACCUMULATION_SHIFT | |
95 | #undef MARK | |
96 | #define MARK UTF_CONTINUATION_MARK | |
97 | #define MASK UTF_CONTINUATION_MASK | |
98 | ||
dfe13c55 | 99 | U8 * |
378516de | 100 | Perl_uvoffuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags) |
a0ed51b3 | 101 | { |
378516de | 102 | PERL_ARGS_ASSERT_UVOFFUNI_TO_UTF8_FLAGS; |
7918f24d | 103 | |
2d1545e5 | 104 | if (OFFUNI_IS_INVARIANT(uv)) { |
4c8cd605 | 105 | *d++ = LATIN1_TO_NATIVE(uv); |
d9432125 KW |
106 | return d; |
107 | } | |
facc1dc2 | 108 | |
3ea68d71 | 109 | if (uv <= MAX_UTF8_TWO_BYTE) { |
facc1dc2 KW |
110 | *d++ = I8_TO_NATIVE_UTF8(( uv >> SHIFT) | UTF_START_MARK(2)); |
111 | *d++ = I8_TO_NATIVE_UTF8(( uv & MASK) | MARK); | |
3ea68d71 KW |
112 | return d; |
113 | } | |
d9432125 | 114 | |
ba6ed43c KW |
115 | /* Not 2-byte; test for and handle 3-byte result. In the test immediately |
116 | * below, the 16 is for start bytes E0-EF (which are all the possible ones | |
117 | * for 3 byte characters). The 2 is for 2 continuation bytes; these each | |
118 | * contribute SHIFT bits. This yields 0x4000 on EBCDIC platforms, 0x1_0000 | |
119 | * on ASCII; so 3 bytes covers the range 0x400-0x3FFF on EBCDIC; | |
120 | * 0x800-0xFFFF on ASCII */ | |
121 | if (uv < (16 * (1U << (2 * SHIFT)))) { | |
122 | *d++ = I8_TO_NATIVE_UTF8(( uv >> ((3 - 1) * SHIFT)) | UTF_START_MARK(3)); | |
123 | *d++ = I8_TO_NATIVE_UTF8(((uv >> ((2 - 1) * SHIFT)) & MASK) | MARK); | |
124 | *d++ = I8_TO_NATIVE_UTF8(( uv /* (1 - 1) */ & MASK) | MARK); | |
125 | ||
126 | #ifndef EBCDIC /* These problematic code points are 4 bytes on EBCDIC, so | |
127 | aren't tested here */ | |
128 | /* The most likely code points in this range are below the surrogates. | |
129 | * Do an extra test to quickly exclude those. */ | |
130 | if (UNLIKELY(uv >= UNICODE_SURROGATE_FIRST)) { | |
131 | if (UNLIKELY( UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv) | |
132 | || UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv))) | |
133 | { | |
8ee1cdcb KW |
134 | HANDLE_UNICODE_NONCHAR(uv, flags); |
135 | } | |
136 | else if (UNLIKELY(UNICODE_IS_SURROGATE(uv))) { | |
137 | HANDLE_UNICODE_SURROGATE(uv, flags); | |
760c7c2f | 138 | } |
ba6ed43c KW |
139 | } |
140 | #endif | |
141 | return d; | |
142 | } | |
143 | ||
144 | /* Not 3-byte; that means the code point is at least 0x1_0000 on ASCII | |
145 | * platforms, and 0x4000 on EBCDIC. There are problematic cases that can | |
146 | * happen starting with 4-byte characters on ASCII platforms. We unify the | |
147 | * code for these with EBCDIC, even though some of them require 5-bytes on | |
148 | * those, because khw believes the code saving is worth the very slight | |
149 | * performance hit on these high EBCDIC code points. */ | |
150 | ||
151 | if (UNLIKELY(UNICODE_IS_SUPER(uv))) { | |
a5bf80e0 KW |
152 | if ( UNLIKELY(uv > MAX_NON_DEPRECATED_CP) |
153 | && ckWARN_d(WARN_DEPRECATED)) | |
154 | { | |
155 | Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), | |
156 | cp_above_legal_max, uv, MAX_NON_DEPRECATED_CP); | |
157 | } | |
158 | if ( (flags & UNICODE_WARN_SUPER) | |
159 | || ( UNICODE_IS_ABOVE_31_BIT(uv) | |
160 | && (flags & UNICODE_WARN_ABOVE_31_BIT))) | |
161 | { | |
162 | Perl_ck_warner_d(aTHX_ packWARN(WARN_NON_UNICODE), | |
163 | ||
164 | /* Choose the more dire applicable warning */ | |
165 | (UNICODE_IS_ABOVE_31_BIT(uv)) | |
166 | ? "Code point 0x%"UVXf" is not Unicode, and not portable" | |
167 | : "Code point 0x%"UVXf" is not Unicode, may not be portable", | |
168 | uv); | |
169 | } | |
170 | if (flags & UNICODE_DISALLOW_SUPER | |
171 | || ( UNICODE_IS_ABOVE_31_BIT(uv) | |
172 | && (flags & UNICODE_DISALLOW_ABOVE_31_BIT))) | |
173 | { | |
174 | return NULL; | |
175 | } | |
176 | } | |
ba6ed43c KW |
177 | else if (UNLIKELY(UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv))) { |
178 | HANDLE_UNICODE_NONCHAR(uv, flags); | |
507b9800 | 179 | } |
d9432125 | 180 | |
ba6ed43c KW |
181 | /* Test for and handle 4-byte result. In the test immediately below, the |
182 | * 8 is for start bytes F0-F7 (which are all the possible ones for 4 byte | |
183 | * characters). The 3 is for 3 continuation bytes; these each contribute | |
184 | * SHIFT bits. This yields 0x4_0000 on EBCDIC platforms, 0x20_0000 on | |
185 | * ASCII, so 4 bytes covers the range 0x4000-0x3_FFFF on EBCDIC; | |
186 | * 0x1_0000-0x1F_FFFF on ASCII */ | |
187 | if (uv < (8 * (1U << (3 * SHIFT)))) { | |
188 | *d++ = I8_TO_NATIVE_UTF8(( uv >> ((4 - 1) * SHIFT)) | UTF_START_MARK(4)); | |
189 | *d++ = I8_TO_NATIVE_UTF8(((uv >> ((3 - 1) * SHIFT)) & MASK) | MARK); | |
190 | *d++ = I8_TO_NATIVE_UTF8(((uv >> ((2 - 1) * SHIFT)) & MASK) | MARK); | |
191 | *d++ = I8_TO_NATIVE_UTF8(( uv /* (1 - 1) */ & MASK) | MARK); | |
192 | ||
193 | #ifdef EBCDIC /* These were handled on ASCII platforms in the code for 3-byte | |
194 | characters. The end-plane non-characters for EBCDIC were | |
195 | handled just above */ | |
196 | if (UNLIKELY(UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv))) { | |
197 | HANDLE_UNICODE_NONCHAR(uv, flags); | |
d528804a | 198 | } |
ba6ed43c KW |
199 | else if (UNLIKELY(UNICODE_IS_SURROGATE(uv))) { |
200 | HANDLE_UNICODE_SURROGATE(uv, flags); | |
201 | } | |
202 | #endif | |
203 | ||
204 | return d; | |
205 | } | |
206 | ||
207 | /* Not 4-byte; that means the code point is at least 0x20_0000 on ASCII | |
208 | * platforms, and 0x4000 on EBCDIC. At this point we switch to a loop | |
209 | * format. The unrolled version above turns out to not save all that much | |
210 | * time, and at these high code points (well above the legal Unicode range | |
211 | * on ASCII platforms, and well above anything in common use in EBCDIC), | |
212 | * khw believes that less code outweighs slight performance gains. */ | |
213 | ||
d9432125 | 214 | { |
5aaebcb3 | 215 | STRLEN len = OFFUNISKIP(uv); |
1d72bdf6 NIS |
216 | U8 *p = d+len-1; |
217 | while (p > d) { | |
4c8cd605 | 218 | *p-- = I8_TO_NATIVE_UTF8((uv & UTF_CONTINUATION_MASK) | UTF_CONTINUATION_MARK); |
1d72bdf6 NIS |
219 | uv >>= UTF_ACCUMULATION_SHIFT; |
220 | } | |
4c8cd605 | 221 | *p = I8_TO_NATIVE_UTF8((uv & UTF_START_MASK(len)) | UTF_START_MARK(len)); |
1d72bdf6 NIS |
222 | return d+len; |
223 | } | |
a0ed51b3 | 224 | } |
a5bf80e0 | 225 | |
646ca15d | 226 | /* |
07693fe6 KW |
227 | =for apidoc uvchr_to_utf8 |
228 | ||
bcb1a2d4 | 229 | Adds the UTF-8 representation of the native code point C<uv> to the end |
f2fc1b45 | 230 | of the string C<d>; C<d> should have at least C<UVCHR_SKIP(uv)+1> (up to |
c749c9fd KW |
231 | C<UTF8_MAXBYTES+1>) free bytes available. The return value is the pointer to |
232 | the byte after the end of the new character. In other words, | |
07693fe6 KW |
233 | |
234 | d = uvchr_to_utf8(d, uv); | |
235 | ||
236 | is the recommended wide native character-aware way of saying | |
237 | ||
238 | *(d++) = uv; | |
239 | ||
760c7c2f KW |
240 | This function accepts any UV as input, but very high code points (above |
241 | C<IV_MAX> on the platform) will raise a deprecation warning. This is | |
242 | typically 0x7FFF_FFFF in a 32-bit word. | |
243 | ||
244 | It is possible to forbid or warn on non-Unicode code points, or those that may | |
245 | be problematic by using L</uvchr_to_utf8_flags>. | |
de69f3af | 246 | |
07693fe6 KW |
247 | =cut |
248 | */ | |
249 | ||
de69f3af KW |
250 | /* This is also a macro */ |
251 | PERL_CALLCONV U8* Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv); | |
252 | ||
07693fe6 KW |
253 | U8 * |
254 | Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv) | |
255 | { | |
de69f3af | 256 | return uvchr_to_utf8(d, uv); |
07693fe6 KW |
257 | } |
258 | ||
de69f3af KW |
259 | /* |
260 | =for apidoc uvchr_to_utf8_flags | |
261 | ||
262 | Adds the UTF-8 representation of the native code point C<uv> to the end | |
f2fc1b45 | 263 | of the string C<d>; C<d> should have at least C<UVCHR_SKIP(uv)+1> (up to |
c749c9fd KW |
264 | C<UTF8_MAXBYTES+1>) free bytes available. The return value is the pointer to |
265 | the byte after the end of the new character. In other words, | |
de69f3af KW |
266 | |
267 | d = uvchr_to_utf8_flags(d, uv, flags); | |
268 | ||
269 | or, in most cases, | |
270 | ||
271 | d = uvchr_to_utf8_flags(d, uv, 0); | |
272 | ||
273 | This is the Unicode-aware way of saying | |
274 | ||
275 | *(d++) = uv; | |
276 | ||
760c7c2f KW |
277 | If C<flags> is 0, this function accepts any UV as input, but very high code |
278 | points (above C<IV_MAX> for the platform) will raise a deprecation warning. | |
279 | This is typically 0x7FFF_FFFF in a 32-bit word. | |
280 | ||
281 | Specifying C<flags> can further restrict what is allowed and not warned on, as | |
282 | follows: | |
de69f3af | 283 | |
796b6530 | 284 | If C<uv> is a Unicode surrogate code point and C<UNICODE_WARN_SURROGATE> is set, |
7ee537e6 KW |
285 | the function will raise a warning, provided UTF8 warnings are enabled. If |
286 | instead C<UNICODE_DISALLOW_SURROGATE> is set, the function will fail and return | |
287 | NULL. If both flags are set, the function will both warn and return NULL. | |
de69f3af | 288 | |
760c7c2f KW |
289 | Similarly, the C<UNICODE_WARN_NONCHAR> and C<UNICODE_DISALLOW_NONCHAR> flags |
290 | affect how the function handles a Unicode non-character. | |
93e6dbd6 | 291 | |
760c7c2f KW |
292 | And likewise, the C<UNICODE_WARN_SUPER> and C<UNICODE_DISALLOW_SUPER> flags |
293 | affect the handling of code points that are above the Unicode maximum of | |
294 | 0x10FFFF. Languages other than Perl may not be able to accept files that | |
295 | contain these. | |
93e6dbd6 KW |
296 | |
297 | The flag C<UNICODE_WARN_ILLEGAL_INTERCHANGE> selects all three of | |
298 | the above WARN flags; and C<UNICODE_DISALLOW_ILLEGAL_INTERCHANGE> selects all | |
299 | three DISALLOW flags. | |
300 | ||
ab8e6d41 KW |
301 | Code points above 0x7FFF_FFFF (2**31 - 1) were never specified in any standard, |
302 | so using them is more problematic than other above-Unicode code points. Perl | |
303 | invented an extension to UTF-8 to represent the ones above 2**36-1, so it is | |
304 | likely that non-Perl languages will not be able to read files that contain | |
305 | these that written by the perl interpreter; nor would Perl understand files | |
306 | written by something that uses a different extension. For these reasons, there | |
307 | is a separate set of flags that can warn and/or disallow these extremely high | |
308 | code points, even if other above-Unicode ones are accepted. These are the | |
760c7c2f KW |
309 | C<UNICODE_WARN_ABOVE_31_BIT> and C<UNICODE_DISALLOW_ABOVE_31_BIT> flags. These |
310 | are entirely independent from the deprecation warning for code points above | |
311 | C<IV_MAX>. On 32-bit machines, it will eventually be forbidden to have any | |
312 | code point that needs more than 31 bits to represent. When that happens, | |
313 | effectively the C<UNICODE_DISALLOW_ABOVE_31_BIT> flag will always be set on | |
314 | 32-bit machines. (Of course C<UNICODE_DISALLOW_SUPER> will treat all | |
ab8e6d41 KW |
315 | above-Unicode code points, including these, as malformations; and |
316 | C<UNICODE_WARN_SUPER> warns on these.) | |
317 | ||
318 | On EBCDIC platforms starting in Perl v5.24, the Perl extension for representing | |
319 | extremely high code points kicks in at 0x3FFF_FFFF (2**30 -1), which is lower | |
320 | than on ASCII. Prior to that, code points 2**31 and higher were simply | |
321 | unrepresentable, and a different, incompatible method was used to represent | |
322 | code points between 2**30 and 2**31 - 1. The flags C<UNICODE_WARN_ABOVE_31_BIT> | |
323 | and C<UNICODE_DISALLOW_ABOVE_31_BIT> have the same function as on ASCII | |
324 | platforms, warning and disallowing 2**31 and higher. | |
de69f3af | 325 | |
de69f3af KW |
326 | =cut |
327 | */ | |
328 | ||
329 | /* This is also a macro */ | |
330 | PERL_CALLCONV U8* Perl_uvchr_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags); | |
331 | ||
07693fe6 KW |
332 | U8 * |
333 | Perl_uvchr_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags) | |
334 | { | |
de69f3af | 335 | return uvchr_to_utf8_flags(d, uv, flags); |
07693fe6 KW |
336 | } |
337 | ||
338 | /* | |
87cea99e | 339 | =for apidoc is_utf8_string |
6662521e | 340 | |
a1433954 | 341 | Returns true if the first C<len> bytes of string C<s> form a valid |
9f7e3d64 | 342 | UTF-8 string, false otherwise. If C<len> is 0, it will be calculated |
75200dff KW |
343 | using C<strlen(s)> (which means if you use this option, that C<s> can't have |
344 | embedded C<NUL> characters and has to have a terminating C<NUL> byte). Note | |
345 | that all characters being ASCII constitute 'a valid UTF-8 string'. | |
6662521e | 346 | |
8871a094 KW |
347 | See also L</is_utf8_invariant_string>(), L</is_utf8_string_loclen>(), and |
348 | L</is_utf8_string_loc>(). | |
768c67ee | 349 | |
6662521e GS |
350 | =cut |
351 | */ | |
352 | ||
8e84507e | 353 | bool |
668b6d8d | 354 | Perl_is_utf8_string(const U8 *s, STRLEN len) |
6662521e | 355 | { |
35da51f7 | 356 | const U8* const send = s + (len ? len : strlen((const char *)s)); |
7fc63493 | 357 | const U8* x = s; |
067a85ef | 358 | |
7918f24d | 359 | PERL_ARGS_ASSERT_IS_UTF8_STRING; |
1aa99e6b | 360 | |
6662521e | 361 | while (x < send) { |
6302f837 KW |
362 | STRLEN len = isUTF8_CHAR(x, send); |
363 | if (UNLIKELY(! len)) { | |
364 | return FALSE; | |
365 | } | |
366 | x += len; | |
6662521e | 367 | } |
768c67ee | 368 | |
067a85ef | 369 | return TRUE; |
6662521e GS |
370 | } |
371 | ||
67e989fb | 372 | /* |
814fafa7 NC |
373 | Implemented as a macro in utf8.h |
374 | ||
87cea99e | 375 | =for apidoc is_utf8_string_loc |
814fafa7 | 376 | |
a1433954 KW |
377 | Like L</is_utf8_string> but stores the location of the failure (in the |
378 | case of "utf8ness failure") or the location C<s>+C<len> (in the case of | |
814fafa7 NC |
379 | "utf8ness success") in the C<ep>. |
380 | ||
a1433954 | 381 | See also L</is_utf8_string_loclen>() and L</is_utf8_string>(). |
814fafa7 | 382 | |
87cea99e | 383 | =for apidoc is_utf8_string_loclen |
81cd54e3 | 384 | |
a1433954 KW |
385 | Like L</is_utf8_string>() but stores the location of the failure (in the |
386 | case of "utf8ness failure") or the location C<s>+C<len> (in the case of | |
768c67ee JH |
387 | "utf8ness success") in the C<ep>, and the number of UTF-8 |
388 | encoded characters in the C<el>. | |
389 | ||
a1433954 | 390 | See also L</is_utf8_string_loc>() and L</is_utf8_string>(). |
81cd54e3 JH |
391 | |
392 | =cut | |
393 | */ | |
394 | ||
395 | bool | |
668b6d8d | 396 | Perl_is_utf8_string_loclen(const U8 *s, STRLEN len, const U8 **ep, STRLEN *el) |
81cd54e3 | 397 | { |
35da51f7 | 398 | const U8* const send = s + (len ? len : strlen((const char *)s)); |
7fc63493 | 399 | const U8* x = s; |
3ebfea28 | 400 | STRLEN outlen = 0; |
7918f24d NC |
401 | |
402 | PERL_ARGS_ASSERT_IS_UTF8_STRING_LOCLEN; | |
81cd54e3 | 403 | |
81cd54e3 | 404 | while (x < send) { |
6302f837 KW |
405 | STRLEN len = isUTF8_CHAR(x, send); |
406 | if (UNLIKELY(! len)) { | |
9f590ad4 | 407 | break; |
6302f837 KW |
408 | } |
409 | x += len; | |
410 | outlen++; | |
81cd54e3 | 411 | } |
768c67ee | 412 | |
3ebfea28 AL |
413 | if (el) |
414 | *el = outlen; | |
415 | ||
768c67ee JH |
416 | if (ep) |
417 | *ep = x; | |
3ebfea28 | 418 | return (x == send); |
81cd54e3 JH |
419 | } |
420 | ||
421 | /* | |
768c67ee | 422 | |
de69f3af | 423 | =for apidoc utf8n_to_uvchr |
378516de KW |
424 | |
425 | THIS FUNCTION SHOULD BE USED IN ONLY VERY SPECIALIZED CIRCUMSTANCES. | |
de69f3af | 426 | Most code should use L</utf8_to_uvchr_buf>() rather than call this directly. |
67e989fb | 427 | |
9041c2e3 | 428 | Bottom level UTF-8 decode routine. |
de69f3af | 429 | Returns the native code point value of the first character in the string C<s>, |
746afd53 KW |
430 | which is assumed to be in UTF-8 (or UTF-EBCDIC) encoding, and no longer than |
431 | C<curlen> bytes; C<*retlen> (if C<retlen> isn't NULL) will be set to | |
432 | the length, in bytes, of that character. | |
949cf498 KW |
433 | |
434 | The value of C<flags> determines the behavior when C<s> does not point to a | |
435 | well-formed UTF-8 character. If C<flags> is 0, when a malformation is found, | |
524080c4 KW |
436 | zero is returned and C<*retlen> is set so that (S<C<s> + C<*retlen>>) is the |
437 | next possible position in C<s> that could begin a non-malformed character. | |
438 | Also, if UTF-8 warnings haven't been lexically disabled, a warning is raised. | |
949cf498 KW |
439 | |
440 | Various ALLOW flags can be set in C<flags> to allow (and not warn on) | |
441 | individual types of malformations, such as the sequence being overlong (that | |
442 | is, when there is a shorter sequence that can express the same code point; | |
443 | overlong sequences are expressly forbidden in the UTF-8 standard due to | |
444 | potential security issues). Another malformation example is the first byte of | |
445 | a character not being a legal first byte. See F<utf8.h> for the list of such | |
524080c4 KW |
446 | flags. For allowed 0 length strings, this function returns 0; for allowed |
447 | overlong sequences, the computed code point is returned; for all other allowed | |
448 | malformations, the Unicode REPLACEMENT CHARACTER is returned, as these have no | |
449 | determinable reasonable value. | |
949cf498 | 450 | |
796b6530 | 451 | The C<UTF8_CHECK_ONLY> flag overrides the behavior when a non-allowed (by other |
949cf498 KW |
452 | flags) malformation is found. If this flag is set, the routine assumes that |
453 | the caller will raise a warning, and this function will silently just set | |
d088425d KW |
454 | C<retlen> to C<-1> (cast to C<STRLEN>) and return zero. |
455 | ||
75200dff | 456 | Note that this API requires disambiguation between successful decoding a C<NUL> |
796b6530 | 457 | character, and an error return (unless the C<UTF8_CHECK_ONLY> flag is set), as |
d088425d | 458 | in both cases, 0 is returned. To disambiguate, upon a zero return, see if the |
75200dff KW |
459 | first byte of C<s> is 0 as well. If so, the input was a C<NUL>; if not, the |
460 | input had an error. | |
949cf498 KW |
461 | |
462 | Certain code points are considered problematic. These are Unicode surrogates, | |
746afd53 | 463 | Unicode non-characters, and code points above the Unicode maximum of 0x10FFFF. |
949cf498 | 464 | By default these are considered regular code points, but certain situations |
5eafe189 | 465 | warrant special handling for them. If C<flags> contains |
796b6530 KW |
466 | C<UTF8_DISALLOW_ILLEGAL_INTERCHANGE>, all three classes are treated as |
467 | malformations and handled as such. The flags C<UTF8_DISALLOW_SURROGATE>, | |
468 | C<UTF8_DISALLOW_NONCHAR>, and C<UTF8_DISALLOW_SUPER> (meaning above the legal | |
469 | Unicode maximum) can be set to disallow these categories individually. | |
470 | ||
471 | The flags C<UTF8_WARN_ILLEGAL_INTERCHANGE>, C<UTF8_WARN_SURROGATE>, | |
472 | C<UTF8_WARN_NONCHAR>, and C<UTF8_WARN_SUPER> will cause warning messages to be | |
473 | raised for their respective categories, but otherwise the code points are | |
474 | considered valid (not malformations). To get a category to both be treated as | |
475 | a malformation and raise a warning, specify both the WARN and DISALLOW flags. | |
949cf498 | 476 | (But note that warnings are not raised if lexically disabled nor if |
796b6530 | 477 | C<UTF8_CHECK_ONLY> is also specified.) |
949cf498 | 478 | |
760c7c2f KW |
479 | It is now deprecated to have very high code points (above C<IV_MAX> on the |
480 | platforms) and this function will raise a deprecation warning for these (unless | |
481 | such warnings are turned off). This value, is typically 0x7FFF_FFFF (2**31 -1) | |
482 | in a 32-bit word. | |
ab8e6d41 KW |
483 | |
484 | Code points above 0x7FFF_FFFF (2**31 - 1) were never specified in any standard, | |
485 | so using them is more problematic than other above-Unicode code points. Perl | |
486 | invented an extension to UTF-8 to represent the ones above 2**36-1, so it is | |
487 | likely that non-Perl languages will not be able to read files that contain | |
488 | these that written by the perl interpreter; nor would Perl understand files | |
489 | written by something that uses a different extension. For these reasons, there | |
490 | is a separate set of flags that can warn and/or disallow these extremely high | |
491 | code points, even if other above-Unicode ones are accepted. These are the | |
760c7c2f KW |
492 | C<UTF8_WARN_ABOVE_31_BIT> and C<UTF8_DISALLOW_ABOVE_31_BIT> flags. These |
493 | are entirely independent from the deprecation warning for code points above | |
494 | C<IV_MAX>. On 32-bit machines, it will eventually be forbidden to have any | |
495 | code point that needs more than 31 bits to represent. When that happens, | |
496 | effectively the C<UTF8_DISALLOW_ABOVE_31_BIT> flag will always be set on | |
497 | 32-bit machines. (Of course C<UTF8_DISALLOW_SUPER> will treat all | |
ab8e6d41 KW |
498 | above-Unicode code points, including these, as malformations; and |
499 | C<UTF8_WARN_SUPER> warns on these.) | |
500 | ||
501 | On EBCDIC platforms starting in Perl v5.24, the Perl extension for representing | |
502 | extremely high code points kicks in at 0x3FFF_FFFF (2**30 -1), which is lower | |
503 | than on ASCII. Prior to that, code points 2**31 and higher were simply | |
504 | unrepresentable, and a different, incompatible method was used to represent | |
505 | code points between 2**30 and 2**31 - 1. The flags C<UTF8_WARN_ABOVE_31_BIT> | |
506 | and C<UTF8_DISALLOW_ABOVE_31_BIT> have the same function as on ASCII | |
507 | platforms, warning and disallowing 2**31 and higher. | |
949cf498 KW |
508 | |
509 | All other code points corresponding to Unicode characters, including private | |
510 | use and those yet to be assigned, are never considered malformed and never | |
511 | warn. | |
67e989fb | 512 | |
37607a96 PK |
513 | =cut |
514 | */ | |
67e989fb | 515 | |
a0ed51b3 | 516 | UV |
de69f3af | 517 | Perl_utf8n_to_uvchr(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags) |
a0ed51b3 | 518 | { |
d4c19fe8 | 519 | const U8 * const s0 = s; |
eb83ed87 | 520 | U8 overflow_byte = '\0'; /* Save byte in case of overflow */ |
0b8d30e8 | 521 | U8 * send; |
eb83ed87 KW |
522 | UV uv = *s; |
523 | STRLEN expectlen; | |
949cf498 | 524 | SV* sv = NULL; |
eb83ed87 KW |
525 | UV outlier_ret = 0; /* return value when input is in error or problematic |
526 | */ | |
527 | UV pack_warn = 0; /* Save result of packWARN() for later */ | |
528 | bool unexpected_non_continuation = FALSE; | |
529 | bool overflowed = FALSE; | |
2f8f112e | 530 | bool do_overlong_test = TRUE; /* May have to skip this test */ |
a0dbb045 | 531 | |
eb83ed87 | 532 | const char* const malformed_text = "Malformed UTF-8 character"; |
7918f24d | 533 | |
de69f3af | 534 | PERL_ARGS_ASSERT_UTF8N_TO_UVCHR; |
a0dbb045 | 535 | |
eb83ed87 KW |
536 | /* The order of malformation tests here is important. We should consume as |
537 | * few bytes as possible in order to not skip any valid character. This is | |
538 | * required by the Unicode Standard (section 3.9 of Unicode 6.0); see also | |
539 | * http://unicode.org/reports/tr36 for more discussion as to why. For | |
540 | * example, once we've done a UTF8SKIP, we can tell the expected number of | |
541 | * bytes, and could fail right off the bat if the input parameters indicate | |
542 | * that there are too few available. But it could be that just that first | |
543 | * byte is garbled, and the intended character occupies fewer bytes. If we | |
544 | * blindly assumed that the first byte is correct, and skipped based on | |
545 | * that number, we could skip over a valid input character. So instead, we | |
546 | * always examine the sequence byte-by-byte. | |
547 | * | |
548 | * We also should not consume too few bytes, otherwise someone could inject | |
549 | * things. For example, an input could be deliberately designed to | |
550 | * overflow, and if this code bailed out immediately upon discovering that, | |
e2660c54 | 551 | * returning to the caller C<*retlen> pointing to the very next byte (one |
eb83ed87 KW |
552 | * which is actually part of of the overflowing sequence), that could look |
553 | * legitimate to the caller, which could discard the initial partial | |
554 | * sequence and process the rest, inappropriately */ | |
555 | ||
556 | /* Zero length strings, if allowed, of necessity are zero */ | |
b5b9af04 | 557 | if (UNLIKELY(curlen == 0)) { |
eb83ed87 KW |
558 | if (retlen) { |
559 | *retlen = 0; | |
560 | } | |
a0dbb045 | 561 | |
eb83ed87 KW |
562 | if (flags & UTF8_ALLOW_EMPTY) { |
563 | return 0; | |
564 | } | |
565 | if (! (flags & UTF8_CHECK_ONLY)) { | |
566 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "%s (empty string)", malformed_text)); | |
567 | } | |
0c443dc2 JH |
568 | goto malformed; |
569 | } | |
570 | ||
eb83ed87 KW |
571 | expectlen = UTF8SKIP(s); |
572 | ||
573 | /* A well-formed UTF-8 character, as the vast majority of calls to this | |
574 | * function will be for, has this expected length. For efficiency, set | |
575 | * things up here to return it. It will be overriden only in those rare | |
576 | * cases where a malformation is found */ | |
577 | if (retlen) { | |
578 | *retlen = expectlen; | |
579 | } | |
580 | ||
581 | /* An invariant is trivially well-formed */ | |
1d72bdf6 | 582 | if (UTF8_IS_INVARIANT(uv)) { |
de69f3af | 583 | return uv; |
a0ed51b3 | 584 | } |
67e989fb | 585 | |
eb83ed87 | 586 | /* A continuation character can't start a valid sequence */ |
b5b9af04 | 587 | if (UNLIKELY(UTF8_IS_CONTINUATION(uv))) { |
eb83ed87 KW |
588 | if (flags & UTF8_ALLOW_CONTINUATION) { |
589 | if (retlen) { | |
590 | *retlen = 1; | |
591 | } | |
592 | return UNICODE_REPLACEMENT; | |
593 | } | |
ba210ebe | 594 | |
eb83ed87 KW |
595 | if (! (flags & UTF8_CHECK_ONLY)) { |
596 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "%s (unexpected continuation byte 0x%02x, with no preceding start byte)", malformed_text, *s0)); | |
597 | } | |
598 | curlen = 1; | |
ba210ebe JH |
599 | goto malformed; |
600 | } | |
9041c2e3 | 601 | |
dcd27b3c KW |
602 | /* Here is not a continuation byte, nor an invariant. The only thing left |
603 | * is a start byte (possibly for an overlong) */ | |
604 | ||
534752c1 KW |
605 | /* Convert to I8 on EBCDIC (no-op on ASCII), then remove the leading bits |
606 | * that indicate the number of bytes in the character's whole UTF-8 | |
607 | * sequence, leaving just the bits that are part of the value. */ | |
608 | uv = NATIVE_UTF8_TO_I8(uv) & UTF_START_MASK(expectlen); | |
ba210ebe | 609 | |
eb83ed87 KW |
610 | /* Now, loop through the remaining bytes in the character's sequence, |
611 | * accumulating each into the working value as we go. Be sure to not look | |
612 | * past the end of the input string */ | |
0b8d30e8 KW |
613 | send = (U8*) s0 + ((expectlen <= curlen) ? expectlen : curlen); |
614 | ||
eb83ed87 | 615 | for (s = s0 + 1; s < send; s++) { |
b5b9af04 | 616 | if (LIKELY(UTF8_IS_CONTINUATION(*s))) { |
eb83ed87 KW |
617 | if (uv & UTF_ACCUMULATION_OVERFLOW_MASK) { |
618 | ||
619 | /* The original implementors viewed this malformation as more | |
620 | * serious than the others (though I, khw, don't understand | |
621 | * why, since other malformations also give very very wrong | |
622 | * results), so there is no way to turn off checking for it. | |
623 | * Set a flag, but keep going in the loop, so that we absorb | |
624 | * the rest of the bytes that comprise the character. */ | |
625 | overflowed = TRUE; | |
626 | overflow_byte = *s; /* Save for warning message's use */ | |
627 | } | |
8850bf83 | 628 | uv = UTF8_ACCUMULATE(uv, *s); |
eb83ed87 KW |
629 | } |
630 | else { | |
631 | /* Here, found a non-continuation before processing all expected | |
632 | * bytes. This byte begins a new character, so quit, even if | |
633 | * allowing this malformation. */ | |
634 | unexpected_non_continuation = TRUE; | |
635 | break; | |
636 | } | |
637 | } /* End of loop through the character's bytes */ | |
638 | ||
639 | /* Save how many bytes were actually in the character */ | |
640 | curlen = s - s0; | |
641 | ||
642 | /* The loop above finds two types of malformations: non-continuation and/or | |
643 | * overflow. The non-continuation malformation is really a too-short | |
644 | * malformation, as it means that the current character ended before it was | |
645 | * expected to (being terminated prematurely by the beginning of the next | |
646 | * character, whereas in the too-short malformation there just are too few | |
647 | * bytes available to hold the character. In both cases, the check below | |
648 | * that we have found the expected number of bytes would fail if executed.) | |
649 | * Thus the non-continuation malformation is really unnecessary, being a | |
650 | * subset of the too-short malformation. But there may be existing | |
651 | * applications that are expecting the non-continuation type, so we retain | |
652 | * it, and return it in preference to the too-short malformation. (If this | |
653 | * code were being written from scratch, the two types might be collapsed | |
654 | * into one.) I, khw, am also giving priority to returning the | |
655 | * non-continuation and too-short malformations over overflow when multiple | |
656 | * ones are present. I don't know of any real reason to prefer one over | |
657 | * the other, except that it seems to me that multiple-byte errors trumps | |
658 | * errors from a single byte */ | |
b5b9af04 | 659 | if (UNLIKELY(unexpected_non_continuation)) { |
eb83ed87 KW |
660 | if (!(flags & UTF8_ALLOW_NON_CONTINUATION)) { |
661 | if (! (flags & UTF8_CHECK_ONLY)) { | |
662 | if (curlen == 1) { | |
663 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "%s (unexpected non-continuation byte 0x%02x, immediately after start byte 0x%02x)", malformed_text, *s, *s0)); | |
664 | } | |
665 | else { | |
666 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "%s (unexpected non-continuation byte 0x%02x, %d bytes after start byte 0x%02x, expected %d bytes)", malformed_text, *s, (int) curlen, *s0, (int)expectlen)); | |
a0dbb045 JH |
667 | } |
668 | } | |
eb83ed87 KW |
669 | goto malformed; |
670 | } | |
671 | uv = UNICODE_REPLACEMENT; | |
2f8f112e KW |
672 | |
673 | /* Skip testing for overlongs, as the REPLACEMENT may not be the same | |
674 | * as what the original expectations were. */ | |
675 | do_overlong_test = FALSE; | |
eb83ed87 KW |
676 | if (retlen) { |
677 | *retlen = curlen; | |
678 | } | |
679 | } | |
b5b9af04 | 680 | else if (UNLIKELY(curlen < expectlen)) { |
eb83ed87 KW |
681 | if (! (flags & UTF8_ALLOW_SHORT)) { |
682 | if (! (flags & UTF8_CHECK_ONLY)) { | |
683 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "%s (%d byte%s, need %d, after start byte 0x%02x)", malformed_text, (int)curlen, curlen == 1 ? "" : "s", (int)expectlen, *s0)); | |
a0dbb045 | 684 | } |
eb83ed87 KW |
685 | goto malformed; |
686 | } | |
687 | uv = UNICODE_REPLACEMENT; | |
2f8f112e | 688 | do_overlong_test = FALSE; |
eb83ed87 KW |
689 | if (retlen) { |
690 | *retlen = curlen; | |
691 | } | |
692 | } | |
693 | ||
b5b9af04 | 694 | if (UNLIKELY(overflowed)) { |
eb83ed87 | 695 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "%s (overflow at byte 0x%02x, after start byte 0x%02x)", malformed_text, overflow_byte, *s0)); |
ba210ebe | 696 | goto malformed; |
eb83ed87 | 697 | } |
eb83ed87 | 698 | |
2f8f112e | 699 | if (do_overlong_test |
5aaebcb3 | 700 | && expectlen > (STRLEN) OFFUNISKIP(uv) |
2f8f112e KW |
701 | && ! (flags & UTF8_ALLOW_LONG)) |
702 | { | |
eb83ed87 KW |
703 | /* The overlong malformation has lower precedence than the others. |
704 | * Note that if this malformation is allowed, we return the actual | |
705 | * value, instead of the replacement character. This is because this | |
706 | * value is actually well-defined. */ | |
707 | if (! (flags & UTF8_CHECK_ONLY)) { | |
5aaebcb3 | 708 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "%s (%d byte%s, need %d, after start byte 0x%02x)", malformed_text, (int)expectlen, expectlen == 1 ? "": "s", OFFUNISKIP(uv), *s0)); |
eb83ed87 KW |
709 | } |
710 | goto malformed; | |
711 | } | |
712 | ||
1a89bb6c | 713 | /* Here, the input is considered to be well-formed, but it still could be a |
eb83ed87 KW |
714 | * problematic code point that is not allowed by the input parameters. */ |
715 | if (uv >= UNICODE_SURROGATE_FIRST /* isn't problematic if < this */ | |
760c7c2f KW |
716 | && ((flags & ( UTF8_DISALLOW_NONCHAR |
717 | |UTF8_DISALLOW_SURROGATE | |
718 | |UTF8_DISALLOW_SUPER | |
719 | |UTF8_DISALLOW_ABOVE_31_BIT | |
720 | |UTF8_WARN_NONCHAR | |
721 | |UTF8_WARN_SURROGATE | |
722 | |UTF8_WARN_SUPER | |
723 | |UTF8_WARN_ABOVE_31_BIT)) | |
724 | || ( UNLIKELY(uv > MAX_NON_DEPRECATED_CP) | |
725 | && ckWARN_d(WARN_DEPRECATED)))) | |
eb83ed87 | 726 | { |
949cf498 | 727 | if (UNICODE_IS_SURROGATE(uv)) { |
ea5ced44 KW |
728 | |
729 | /* By adding UTF8_CHECK_ONLY to the test, we avoid unnecessary | |
730 | * generation of the sv, since no warnings are raised under CHECK */ | |
eb83ed87 | 731 | if ((flags & (UTF8_WARN_SURROGATE|UTF8_CHECK_ONLY)) == UTF8_WARN_SURROGATE |
54f4afef | 732 | && ckWARN_d(WARN_SURROGATE)) |
eb83ed87 | 733 | { |
111d382d | 734 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "UTF-16 surrogate U+%04"UVXf"", uv)); |
54f4afef | 735 | pack_warn = packWARN(WARN_SURROGATE); |
949cf498 KW |
736 | } |
737 | if (flags & UTF8_DISALLOW_SURROGATE) { | |
738 | goto disallowed; | |
739 | } | |
740 | } | |
949cf498 | 741 | else if ((uv > PERL_UNICODE_MAX)) { |
eb83ed87 | 742 | if ((flags & (UTF8_WARN_SUPER|UTF8_CHECK_ONLY)) == UTF8_WARN_SUPER |
ea5ced44 | 743 | && ckWARN_d(WARN_NON_UNICODE)) |
eb83ed87 | 744 | { |
0bcdd8f6 KW |
745 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ |
746 | "Code point 0x%04"UVXf" is not Unicode, may not be portable", | |
747 | uv)); | |
54f4afef | 748 | pack_warn = packWARN(WARN_NON_UNICODE); |
949cf498 | 749 | } |
c0236afe KW |
750 | |
751 | /* The maximum code point ever specified by a standard was | |
752 | * 2**31 - 1. Anything larger than that is a Perl extension that | |
753 | * very well may not be understood by other applications (including | |
754 | * earlier perl versions on EBCDIC platforms). On ASCII platforms, | |
755 | * these code points are indicated by the first UTF-8 byte being | |
d35f2ca5 KW |
756 | * 0xFE or 0xFF. We test for these after the regular SUPER ones, |
757 | * and before possibly bailing out, so that the slightly more dire | |
c0236afe KW |
758 | * warning will override the regular one. */ |
759 | if ( | |
760 | #ifndef EBCDIC | |
761 | (*s0 & 0xFE) == 0xFE /* matches both FE, FF */ | |
762 | #else | |
763 | /* The I8 for 2**31 (U+80000000) is | |
764 | * \xFF\xA0\xA0\xA0\xA0\xA0\xA0\xA2\xA0\xA0\xA0\xA0\xA0\xA0 | |
765 | * and it turns out that on all EBCDIC pages recognized that | |
766 | * the UTF-EBCDIC for that code point is | |
767 | * \xFE\x41\x41\x41\x41\x41\x41\x43\x41\x41\x41\x41\x41\x41 | |
768 | * For the next lower code point, the 1047 UTF-EBCDIC is | |
769 | * \xFE\x41\x41\x41\x41\x41\x41\x42\x73\x73\x73\x73\x73\x73 | |
770 | * The other code pages differ only in the bytes following | |
771 | * \x42. Thus the following works (the minimum continuation | |
772 | * byte is \x41). */ | |
773 | *s0 == 0xFE && send - s0 > 7 && ( s0[1] > 0x41 | |
774 | || s0[2] > 0x41 | |
775 | || s0[3] > 0x41 | |
776 | || s0[4] > 0x41 | |
777 | || s0[5] > 0x41 | |
778 | || s0[6] > 0x41 | |
779 | || s0[7] > 0x42) | |
780 | #endif | |
d35f2ca5 KW |
781 | && (flags & (UTF8_WARN_ABOVE_31_BIT|UTF8_WARN_SUPER |
782 | |UTF8_DISALLOW_ABOVE_31_BIT))) | |
ea5ced44 | 783 | { |
0bcdd8f6 | 784 | if ( ! (flags & UTF8_CHECK_ONLY) |
d35f2ca5 | 785 | && (flags & (UTF8_WARN_ABOVE_31_BIT|UTF8_WARN_SUPER)) |
0bcdd8f6 | 786 | && ckWARN_d(WARN_UTF8)) |
ea5ced44 | 787 | { |
0bcdd8f6 KW |
788 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ |
789 | "Code point 0x%"UVXf" is not Unicode, and not portable", | |
790 | uv)); | |
ea5ced44 KW |
791 | pack_warn = packWARN(WARN_UTF8); |
792 | } | |
d35f2ca5 | 793 | if (flags & UTF8_DISALLOW_ABOVE_31_BIT) { |
ea5ced44 KW |
794 | goto disallowed; |
795 | } | |
796 | } | |
c0236afe | 797 | |
949cf498 KW |
798 | if (flags & UTF8_DISALLOW_SUPER) { |
799 | goto disallowed; | |
800 | } | |
760c7c2f KW |
801 | |
802 | /* The deprecated warning overrides any non-deprecated one */ | |
803 | if (UNLIKELY(uv > MAX_NON_DEPRECATED_CP) && ckWARN_d(WARN_DEPRECATED)) | |
804 | { | |
805 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ cp_above_legal_max, | |
806 | uv, MAX_NON_DEPRECATED_CP)); | |
807 | pack_warn = packWARN(WARN_DEPRECATED); | |
808 | } | |
949cf498 | 809 | } |
4190d317 KW |
810 | else if (UNICODE_IS_NONCHAR(uv)) { |
811 | if ((flags & (UTF8_WARN_NONCHAR|UTF8_CHECK_ONLY)) == UTF8_WARN_NONCHAR | |
54f4afef | 812 | && ckWARN_d(WARN_NONCHAR)) |
4190d317 | 813 | { |
ba707cdc | 814 | sv = sv_2mortal(Perl_newSVpvf(aTHX_ "Unicode non-character U+%04"UVXf" is not recommended for open interchange", uv)); |
54f4afef | 815 | pack_warn = packWARN(WARN_NONCHAR); |
4190d317 KW |
816 | } |
817 | if (flags & UTF8_DISALLOW_NONCHAR) { | |
818 | goto disallowed; | |
819 | } | |
820 | } | |
949cf498 | 821 | |
eb83ed87 | 822 | if (sv) { |
de69f3af KW |
823 | outlier_ret = uv; /* Note we don't bother to convert to native, |
824 | as all the outlier code points are the same | |
825 | in both ASCII and EBCDIC */ | |
eb83ed87 KW |
826 | goto do_warn; |
827 | } | |
828 | ||
949cf498 KW |
829 | /* Here, this is not considered a malformed character, so drop through |
830 | * to return it */ | |
a0ed51b3 | 831 | } |
ba210ebe | 832 | |
de69f3af | 833 | return UNI_TO_NATIVE(uv); |
ba210ebe | 834 | |
eb83ed87 KW |
835 | /* There are three cases which get to beyond this point. In all 3 cases: |
836 | * <sv> if not null points to a string to print as a warning. | |
837 | * <curlen> is what <*retlen> should be set to if UTF8_CHECK_ONLY isn't | |
838 | * set. | |
839 | * <outlier_ret> is what return value to use if UTF8_CHECK_ONLY isn't set. | |
840 | * This is done by initializing it to 0, and changing it only | |
841 | * for case 1). | |
842 | * The 3 cases are: | |
843 | * 1) The input is valid but problematic, and to be warned about. The | |
844 | * return value is the resultant code point; <*retlen> is set to | |
845 | * <curlen>, the number of bytes that comprise the code point. | |
846 | * <pack_warn> contains the result of packWARN() for the warning | |
847 | * types. The entry point for this case is the label <do_warn>; | |
848 | * 2) The input is a valid code point but disallowed by the parameters to | |
849 | * this function. The return value is 0. If UTF8_CHECK_ONLY is set, | |
850 | * <*relen> is -1; otherwise it is <curlen>, the number of bytes that | |
851 | * comprise the code point. <pack_warn> contains the result of | |
852 | * packWARN() for the warning types. The entry point for this case is | |
853 | * the label <disallowed>. | |
854 | * 3) The input is malformed. The return value is 0. If UTF8_CHECK_ONLY | |
855 | * is set, <*relen> is -1; otherwise it is <curlen>, the number of | |
856 | * bytes that comprise the malformation. All such malformations are | |
857 | * assumed to be warning type <utf8>. The entry point for this case | |
858 | * is the label <malformed>. | |
859 | */ | |
949cf498 | 860 | |
7b52d656 | 861 | malformed: |
ba210ebe | 862 | |
eb83ed87 KW |
863 | if (sv && ckWARN_d(WARN_UTF8)) { |
864 | pack_warn = packWARN(WARN_UTF8); | |
865 | } | |
866 | ||
7b52d656 | 867 | disallowed: |
eb83ed87 | 868 | |
fcc8fcf6 | 869 | if (flags & UTF8_CHECK_ONLY) { |
ba210ebe | 870 | if (retlen) |
10edeb5d | 871 | *retlen = ((STRLEN) -1); |
ba210ebe JH |
872 | return 0; |
873 | } | |
874 | ||
7b52d656 | 875 | do_warn: |
5b311467 | 876 | |
eb83ed87 KW |
877 | if (pack_warn) { /* <pack_warn> was initialized to 0, and changed only |
878 | if warnings are to be raised. */ | |
f555bc63 | 879 | const char * const string = SvPVX_const(sv); |
a0dbb045 | 880 | |
f555bc63 KW |
881 | if (PL_op) |
882 | Perl_warner(aTHX_ pack_warn, "%s in %s", string, OP_DESC(PL_op)); | |
883 | else | |
884 | Perl_warner(aTHX_ pack_warn, "%s", string); | |
a0dbb045 JH |
885 | } |
886 | ||
eb83ed87 KW |
887 | if (retlen) { |
888 | *retlen = curlen; | |
889 | } | |
ba210ebe | 890 | |
eb83ed87 | 891 | return outlier_ret; |
a0ed51b3 LW |
892 | } |
893 | ||
8e84507e | 894 | /* |
ec5f19d0 KW |
895 | =for apidoc utf8_to_uvchr_buf |
896 | ||
897 | Returns the native code point of the first character in the string C<s> which | |
898 | is assumed to be in UTF-8 encoding; C<send> points to 1 beyond the end of C<s>. | |
524080c4 | 899 | C<*retlen> will be set to the length, in bytes, of that character. |
ec5f19d0 | 900 | |
524080c4 KW |
901 | If C<s> does not point to a well-formed UTF-8 character and UTF8 warnings are |
902 | enabled, zero is returned and C<*retlen> is set (if C<retlen> isn't | |
796b6530 | 903 | C<NULL>) to -1. If those warnings are off, the computed value, if well-defined |
173db420 | 904 | (or the Unicode REPLACEMENT CHARACTER if not), is silently returned, and |
796b6530 | 905 | C<*retlen> is set (if C<retlen> isn't C<NULL>) so that (S<C<s> + C<*retlen>>) is |
173db420 | 906 | the next possible position in C<s> that could begin a non-malformed character. |
de69f3af | 907 | See L</utf8n_to_uvchr> for details on when the REPLACEMENT CHARACTER is |
173db420 | 908 | returned. |
ec5f19d0 | 909 | |
760c7c2f KW |
910 | Code points above the platform's C<IV_MAX> will raise a deprecation warning, |
911 | unless those are turned off. | |
912 | ||
ec5f19d0 KW |
913 | =cut |
914 | */ | |
915 | ||
916 | ||
917 | UV | |
918 | Perl_utf8_to_uvchr_buf(pTHX_ const U8 *s, const U8 *send, STRLEN *retlen) | |
919 | { | |
ec5f19d0 KW |
920 | assert(s < send); |
921 | ||
922 | return utf8n_to_uvchr(s, send - s, retlen, | |
923 | ckWARN_d(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY); | |
924 | } | |
925 | ||
926 | /* | |
ec5f19d0 KW |
927 | =for apidoc utf8_to_uvuni_buf |
928 | ||
de69f3af KW |
929 | Only in very rare circumstances should code need to be dealing in Unicode |
930 | (as opposed to native) code points. In those few cases, use | |
931 | C<L<NATIVE_TO_UNI(utf8_to_uvchr_buf(...))|/utf8_to_uvchr_buf>> instead. | |
4f83cdcd KW |
932 | |
933 | Returns the Unicode (not-native) code point of the first character in the | |
934 | string C<s> which | |
ec5f19d0 KW |
935 | is assumed to be in UTF-8 encoding; C<send> points to 1 beyond the end of C<s>. |
936 | C<retlen> will be set to the length, in bytes, of that character. | |
937 | ||
524080c4 KW |
938 | If C<s> does not point to a well-formed UTF-8 character and UTF8 warnings are |
939 | enabled, zero is returned and C<*retlen> is set (if C<retlen> isn't | |
940 | NULL) to -1. If those warnings are off, the computed value if well-defined (or | |
941 | the Unicode REPLACEMENT CHARACTER, if not) is silently returned, and C<*retlen> | |
942 | is set (if C<retlen> isn't NULL) so that (S<C<s> + C<*retlen>>) is the | |
943 | next possible position in C<s> that could begin a non-malformed character. | |
de69f3af | 944 | See L</utf8n_to_uvchr> for details on when the REPLACEMENT CHARACTER is returned. |
ec5f19d0 | 945 | |
760c7c2f KW |
946 | Code points above the platform's C<IV_MAX> will raise a deprecation warning, |
947 | unless those are turned off. | |
948 | ||
ec5f19d0 KW |
949 | =cut |
950 | */ | |
951 | ||
952 | UV | |
953 | Perl_utf8_to_uvuni_buf(pTHX_ const U8 *s, const U8 *send, STRLEN *retlen) | |
954 | { | |
955 | PERL_ARGS_ASSERT_UTF8_TO_UVUNI_BUF; | |
956 | ||
957 | assert(send > s); | |
958 | ||
959 | /* Call the low level routine asking for checks */ | |
de69f3af KW |
960 | return NATIVE_TO_UNI(Perl_utf8n_to_uvchr(aTHX_ s, send -s, retlen, |
961 | ckWARN_d(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY)); | |
ec5f19d0 KW |
962 | } |
963 | ||
b76347f2 | 964 | /* |
87cea99e | 965 | =for apidoc utf8_length |
b76347f2 JH |
966 | |
967 | Return the length of the UTF-8 char encoded string C<s> in characters. | |
02eb7b47 JH |
968 | Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end |
969 | up past C<e>, croaks. | |
b76347f2 JH |
970 | |
971 | =cut | |
972 | */ | |
973 | ||
974 | STRLEN | |
35a4481c | 975 | Perl_utf8_length(pTHX_ const U8 *s, const U8 *e) |
b76347f2 JH |
976 | { |
977 | STRLEN len = 0; | |
978 | ||
7918f24d NC |
979 | PERL_ARGS_ASSERT_UTF8_LENGTH; |
980 | ||
8850bf83 JH |
981 | /* Note: cannot use UTF8_IS_...() too eagerly here since e.g. |
982 | * the bitops (especially ~) can create illegal UTF-8. | |
983 | * In other words: in Perl UTF-8 is not just for Unicode. */ | |
984 | ||
a3b680e6 AL |
985 | if (e < s) |
986 | goto warn_and_return; | |
b76347f2 | 987 | while (s < e) { |
4cbf4130 | 988 | s += UTF8SKIP(s); |
8e91ec7f AV |
989 | len++; |
990 | } | |
991 | ||
992 | if (e != s) { | |
993 | len--; | |
994 | warn_and_return: | |
9b387841 NC |
995 | if (PL_op) |
996 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), | |
997 | "%s in %s", unees, OP_DESC(PL_op)); | |
998 | else | |
61a12c31 | 999 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), "%s", unees); |
b76347f2 JH |
1000 | } |
1001 | ||
1002 | return len; | |
1003 | } | |
1004 | ||
b06226ff | 1005 | /* |
87cea99e | 1006 | =for apidoc utf8_distance |
b06226ff | 1007 | |
1e54db1a | 1008 | Returns the number of UTF-8 characters between the UTF-8 pointers C<a> |
b06226ff JH |
1009 | and C<b>. |
1010 | ||
1011 | WARNING: use only if you *know* that the pointers point inside the | |
1012 | same UTF-8 buffer. | |
1013 | ||
37607a96 PK |
1014 | =cut |
1015 | */ | |
a0ed51b3 | 1016 | |
02eb7b47 | 1017 | IV |
35a4481c | 1018 | Perl_utf8_distance(pTHX_ const U8 *a, const U8 *b) |
a0ed51b3 | 1019 | { |
7918f24d NC |
1020 | PERL_ARGS_ASSERT_UTF8_DISTANCE; |
1021 | ||
bf1665bc | 1022 | return (a < b) ? -1 * (IV) utf8_length(a, b) : (IV) utf8_length(b, a); |
a0ed51b3 LW |
1023 | } |
1024 | ||
b06226ff | 1025 | /* |
87cea99e | 1026 | =for apidoc utf8_hop |
b06226ff | 1027 | |
8850bf83 JH |
1028 | Return the UTF-8 pointer C<s> displaced by C<off> characters, either |
1029 | forward or backward. | |
b06226ff JH |
1030 | |
1031 | WARNING: do not use the following unless you *know* C<off> is within | |
8850bf83 JH |
1032 | the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned |
1033 | on the first byte of character or just after the last byte of a character. | |
b06226ff | 1034 | |
37607a96 PK |
1035 | =cut |
1036 | */ | |
a0ed51b3 LW |
1037 | |
1038 | U8 * | |
4caf7d8c | 1039 | Perl_utf8_hop(const U8 *s, SSize_t off) |
a0ed51b3 | 1040 | { |
7918f24d NC |
1041 | PERL_ARGS_ASSERT_UTF8_HOP; |
1042 | ||
8850bf83 JH |
1043 | /* Note: cannot use UTF8_IS_...() too eagerly here since e.g |
1044 | * the bitops (especially ~) can create illegal UTF-8. | |
1045 | * In other words: in Perl UTF-8 is not just for Unicode. */ | |
1046 | ||
a0ed51b3 LW |
1047 | if (off >= 0) { |
1048 | while (off--) | |
1049 | s += UTF8SKIP(s); | |
1050 | } | |
1051 | else { | |
1052 | while (off++) { | |
1053 | s--; | |
8850bf83 JH |
1054 | while (UTF8_IS_CONTINUATION(*s)) |
1055 | s--; | |
a0ed51b3 LW |
1056 | } |
1057 | } | |
4373e329 | 1058 | return (U8 *)s; |
a0ed51b3 LW |
1059 | } |
1060 | ||
6940069f | 1061 | /* |
fed3ba5d NC |
1062 | =for apidoc bytes_cmp_utf8 |
1063 | ||
a1433954 | 1064 | Compares the sequence of characters (stored as octets) in C<b>, C<blen> with the |
72d33970 FC |
1065 | sequence of characters (stored as UTF-8) |
1066 | in C<u>, C<ulen>. Returns 0 if they are | |
fed3ba5d NC |
1067 | equal, -1 or -2 if the first string is less than the second string, +1 or +2 |
1068 | if the first string is greater than the second string. | |
1069 | ||
1070 | -1 or +1 is returned if the shorter string was identical to the start of the | |
72d33970 FC |
1071 | longer string. -2 or +2 is returned if |
1072 | there was a difference between characters | |
fed3ba5d NC |
1073 | within the strings. |
1074 | ||
1075 | =cut | |
1076 | */ | |
1077 | ||
1078 | int | |
1079 | Perl_bytes_cmp_utf8(pTHX_ const U8 *b, STRLEN blen, const U8 *u, STRLEN ulen) | |
1080 | { | |
1081 | const U8 *const bend = b + blen; | |
1082 | const U8 *const uend = u + ulen; | |
1083 | ||
1084 | PERL_ARGS_ASSERT_BYTES_CMP_UTF8; | |
fed3ba5d NC |
1085 | |
1086 | while (b < bend && u < uend) { | |
1087 | U8 c = *u++; | |
1088 | if (!UTF8_IS_INVARIANT(c)) { | |
1089 | if (UTF8_IS_DOWNGRADEABLE_START(c)) { | |
1090 | if (u < uend) { | |
1091 | U8 c1 = *u++; | |
1092 | if (UTF8_IS_CONTINUATION(c1)) { | |
a62b247b | 1093 | c = EIGHT_BIT_UTF8_TO_NATIVE(c, c1); |
fed3ba5d NC |
1094 | } else { |
1095 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), | |
1096 | "Malformed UTF-8 character " | |
1097 | "(unexpected non-continuation byte 0x%02x" | |
1098 | ", immediately after start byte 0x%02x)" | |
1099 | /* Dear diag.t, it's in the pod. */ | |
1100 | "%s%s", c1, c, | |
1101 | PL_op ? " in " : "", | |
1102 | PL_op ? OP_DESC(PL_op) : ""); | |
1103 | return -2; | |
1104 | } | |
1105 | } else { | |
1106 | if (PL_op) | |
1107 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), | |
1108 | "%s in %s", unees, OP_DESC(PL_op)); | |
1109 | else | |
61a12c31 | 1110 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), "%s", unees); |
fed3ba5d NC |
1111 | return -2; /* Really want to return undef :-) */ |
1112 | } | |
1113 | } else { | |
1114 | return -2; | |
1115 | } | |
1116 | } | |
1117 | if (*b != c) { | |
1118 | return *b < c ? -2 : +2; | |
1119 | } | |
1120 | ++b; | |
1121 | } | |
1122 | ||
1123 | if (b == bend && u == uend) | |
1124 | return 0; | |
1125 | ||
1126 | return b < bend ? +1 : -1; | |
1127 | } | |
1128 | ||
1129 | /* | |
87cea99e | 1130 | =for apidoc utf8_to_bytes |
6940069f | 1131 | |
2bbc8d55 | 1132 | Converts a string C<s> of length C<len> from UTF-8 into native byte encoding. |
a1433954 KW |
1133 | Unlike L</bytes_to_utf8>, this over-writes the original string, and |
1134 | updates C<len> to contain the new length. | |
67e989fb | 1135 | Returns zero on failure, setting C<len> to -1. |
6940069f | 1136 | |
a1433954 | 1137 | If you need a copy of the string, see L</bytes_from_utf8>. |
95be277c | 1138 | |
6940069f GS |
1139 | =cut |
1140 | */ | |
1141 | ||
1142 | U8 * | |
37607a96 | 1143 | Perl_utf8_to_bytes(pTHX_ U8 *s, STRLEN *len) |
6940069f | 1144 | { |
d4c19fe8 AL |
1145 | U8 * const save = s; |
1146 | U8 * const send = s + *len; | |
6940069f | 1147 | U8 *d; |
246fae53 | 1148 | |
7918f24d | 1149 | PERL_ARGS_ASSERT_UTF8_TO_BYTES; |
81611534 | 1150 | PERL_UNUSED_CONTEXT; |
7918f24d | 1151 | |
1e54db1a | 1152 | /* ensure valid UTF-8 and chars < 256 before updating string */ |
d4c19fe8 | 1153 | while (s < send) { |
d59937ca KW |
1154 | if (! UTF8_IS_INVARIANT(*s)) { |
1155 | if (! UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, send)) { | |
1156 | *len = ((STRLEN) -1); | |
1157 | return 0; | |
1158 | } | |
1159 | s++; | |
dcad2880 | 1160 | } |
d59937ca | 1161 | s++; |
246fae53 | 1162 | } |
dcad2880 JH |
1163 | |
1164 | d = s = save; | |
6940069f | 1165 | while (s < send) { |
80e0b38f KW |
1166 | U8 c = *s++; |
1167 | if (! UTF8_IS_INVARIANT(c)) { | |
1168 | /* Then it is two-byte encoded */ | |
a62b247b | 1169 | c = EIGHT_BIT_UTF8_TO_NATIVE(c, *s); |
80e0b38f KW |
1170 | s++; |
1171 | } | |
1172 | *d++ = c; | |
6940069f GS |
1173 | } |
1174 | *d = '\0'; | |
246fae53 | 1175 | *len = d - save; |
6940069f GS |
1176 | return save; |
1177 | } | |
1178 | ||
1179 | /* | |
87cea99e | 1180 | =for apidoc bytes_from_utf8 |
f9a63242 | 1181 | |
2bbc8d55 | 1182 | Converts a string C<s> of length C<len> from UTF-8 into native byte encoding. |
a1433954 | 1183 | Unlike L</utf8_to_bytes> but like L</bytes_to_utf8>, returns a pointer to |
ef9edfd0 JH |
1184 | the newly-created string, and updates C<len> to contain the new |
1185 | length. Returns the original string if no conversion occurs, C<len> | |
72d33970 | 1186 | is unchanged. Do nothing if C<is_utf8> points to 0. Sets C<is_utf8> to |
2bbc8d55 | 1187 | 0 if C<s> is converted or consisted entirely of characters that are invariant |
4a4088c4 | 1188 | in UTF-8 (i.e., US-ASCII on non-EBCDIC machines). |
f9a63242 | 1189 | |
37607a96 PK |
1190 | =cut |
1191 | */ | |
f9a63242 JH |
1192 | |
1193 | U8 * | |
e1ec3a88 | 1194 | Perl_bytes_from_utf8(pTHX_ const U8 *s, STRLEN *len, bool *is_utf8) |
f9a63242 | 1195 | { |
f9a63242 | 1196 | U8 *d; |
e1ec3a88 AL |
1197 | const U8 *start = s; |
1198 | const U8 *send; | |
f9a63242 JH |
1199 | I32 count = 0; |
1200 | ||
7918f24d | 1201 | PERL_ARGS_ASSERT_BYTES_FROM_UTF8; |
96a5add6 | 1202 | PERL_UNUSED_CONTEXT; |
f9a63242 | 1203 | if (!*is_utf8) |
73d840c0 | 1204 | return (U8 *)start; |
f9a63242 | 1205 | |
1e54db1a | 1206 | /* ensure valid UTF-8 and chars < 256 before converting string */ |
f9a63242 | 1207 | for (send = s + *len; s < send;) { |
d59937ca KW |
1208 | if (! UTF8_IS_INVARIANT(*s)) { |
1209 | if (! UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, send)) { | |
73d840c0 | 1210 | return (U8 *)start; |
d59937ca KW |
1211 | } |
1212 | count++; | |
1213 | s++; | |
db42d148 | 1214 | } |
d59937ca | 1215 | s++; |
f9a63242 JH |
1216 | } |
1217 | ||
35da51f7 | 1218 | *is_utf8 = FALSE; |
f9a63242 | 1219 | |
212542aa | 1220 | Newx(d, (*len) - count + 1, U8); |
ef9edfd0 | 1221 | s = start; start = d; |
f9a63242 JH |
1222 | while (s < send) { |
1223 | U8 c = *s++; | |
1a91c45d | 1224 | if (! UTF8_IS_INVARIANT(c)) { |
c4d5f83a | 1225 | /* Then it is two-byte encoded */ |
a62b247b | 1226 | c = EIGHT_BIT_UTF8_TO_NATIVE(c, *s); |
1a91c45d | 1227 | s++; |
c4d5f83a NIS |
1228 | } |
1229 | *d++ = c; | |
f9a63242 JH |
1230 | } |
1231 | *d = '\0'; | |
1232 | *len = d - start; | |
73d840c0 | 1233 | return (U8 *)start; |
f9a63242 JH |
1234 | } |
1235 | ||
1236 | /* | |
87cea99e | 1237 | =for apidoc bytes_to_utf8 |
6940069f | 1238 | |
ff97e5cf KW |
1239 | Converts a string C<s> of length C<len> bytes from the native encoding into |
1240 | UTF-8. | |
6662521e | 1241 | Returns a pointer to the newly-created string, and sets C<len> to |
ff97e5cf | 1242 | reflect the new length in bytes. |
6940069f | 1243 | |
75200dff | 1244 | A C<NUL> character will be written after the end of the string. |
2bbc8d55 SP |
1245 | |
1246 | If you want to convert to UTF-8 from encodings other than | |
1247 | the native (Latin1 or EBCDIC), | |
a1433954 | 1248 | see L</sv_recode_to_utf8>(). |
c9ada85f | 1249 | |
497711e7 | 1250 | =cut |
6940069f GS |
1251 | */ |
1252 | ||
c682ebef FC |
1253 | /* This logic is duplicated in sv_catpvn_flags, so any bug fixes will |
1254 | likewise need duplication. */ | |
1255 | ||
6940069f | 1256 | U8* |
35a4481c | 1257 | Perl_bytes_to_utf8(pTHX_ const U8 *s, STRLEN *len) |
6940069f | 1258 | { |
35a4481c | 1259 | const U8 * const send = s + (*len); |
6940069f GS |
1260 | U8 *d; |
1261 | U8 *dst; | |
7918f24d NC |
1262 | |
1263 | PERL_ARGS_ASSERT_BYTES_TO_UTF8; | |
96a5add6 | 1264 | PERL_UNUSED_CONTEXT; |
6940069f | 1265 | |
212542aa | 1266 | Newx(d, (*len) * 2 + 1, U8); |
6940069f GS |
1267 | dst = d; |
1268 | ||
1269 | while (s < send) { | |
55d09dc8 KW |
1270 | append_utf8_from_native_byte(*s, &d); |
1271 | s++; | |
6940069f GS |
1272 | } |
1273 | *d = '\0'; | |
6662521e | 1274 | *len = d-dst; |
6940069f GS |
1275 | return dst; |
1276 | } | |
1277 | ||
a0ed51b3 | 1278 | /* |
dea0fc0b | 1279 | * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8. |
a0ed51b3 LW |
1280 | * |
1281 | * Destination must be pre-extended to 3/2 source. Do not use in-place. | |
1282 | * We optimize for native, for obvious reasons. */ | |
1283 | ||
1284 | U8* | |
dea0fc0b | 1285 | Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen) |
a0ed51b3 | 1286 | { |
dea0fc0b JH |
1287 | U8* pend; |
1288 | U8* dstart = d; | |
1289 | ||
7918f24d NC |
1290 | PERL_ARGS_ASSERT_UTF16_TO_UTF8; |
1291 | ||
dea0fc0b | 1292 | if (bytelen & 1) |
f5992bc4 | 1293 | Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen %"UVuf, (UV)bytelen); |
dea0fc0b JH |
1294 | |
1295 | pend = p + bytelen; | |
1296 | ||
a0ed51b3 | 1297 | while (p < pend) { |
dea0fc0b JH |
1298 | UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */ |
1299 | p += 2; | |
2d1545e5 | 1300 | if (OFFUNI_IS_INVARIANT(uv)) { |
56d37426 | 1301 | *d++ = LATIN1_TO_NATIVE((U8) uv); |
a0ed51b3 LW |
1302 | continue; |
1303 | } | |
56d37426 KW |
1304 | if (uv <= MAX_UTF8_TWO_BYTE) { |
1305 | *d++ = UTF8_TWO_BYTE_HI(UNI_TO_NATIVE(uv)); | |
1306 | *d++ = UTF8_TWO_BYTE_LO(UNI_TO_NATIVE(uv)); | |
a0ed51b3 LW |
1307 | continue; |
1308 | } | |
46956fad KW |
1309 | #define FIRST_HIGH_SURROGATE UNICODE_SURROGATE_FIRST |
1310 | #define LAST_HIGH_SURROGATE 0xDBFF | |
1311 | #define FIRST_LOW_SURROGATE 0xDC00 | |
1312 | #define LAST_LOW_SURROGATE UNICODE_SURROGATE_LAST | |
e23c50db KW |
1313 | |
1314 | /* This assumes that most uses will be in the first Unicode plane, not | |
1315 | * needing surrogates */ | |
1316 | if (UNLIKELY(uv >= UNICODE_SURROGATE_FIRST | |
1317 | && uv <= UNICODE_SURROGATE_LAST)) | |
1318 | { | |
1319 | if (UNLIKELY(p >= pend) || UNLIKELY(uv > LAST_HIGH_SURROGATE)) { | |
1320 | Perl_croak(aTHX_ "Malformed UTF-16 surrogate"); | |
1321 | } | |
1322 | else { | |
01ea242b | 1323 | UV low = (p[0] << 8) + p[1]; |
e23c50db KW |
1324 | if ( UNLIKELY(low < FIRST_LOW_SURROGATE) |
1325 | || UNLIKELY(low > LAST_LOW_SURROGATE)) | |
1326 | { | |
01ea242b | 1327 | Perl_croak(aTHX_ "Malformed UTF-16 surrogate"); |
e23c50db KW |
1328 | } |
1329 | p += 2; | |
46956fad KW |
1330 | uv = ((uv - FIRST_HIGH_SURROGATE) << 10) |
1331 | + (low - FIRST_LOW_SURROGATE) + 0x10000; | |
01ea242b | 1332 | } |
a0ed51b3 | 1333 | } |
56d37426 KW |
1334 | #ifdef EBCDIC |
1335 | d = uvoffuni_to_utf8_flags(d, uv, 0); | |
1336 | #else | |
a0ed51b3 | 1337 | if (uv < 0x10000) { |
eb160463 GS |
1338 | *d++ = (U8)(( uv >> 12) | 0xe0); |
1339 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
1340 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
1341 | continue; |
1342 | } | |
1343 | else { | |
eb160463 GS |
1344 | *d++ = (U8)(( uv >> 18) | 0xf0); |
1345 | *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80); | |
1346 | *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80); | |
1347 | *d++ = (U8)(( uv & 0x3f) | 0x80); | |
a0ed51b3 LW |
1348 | continue; |
1349 | } | |
56d37426 | 1350 | #endif |
a0ed51b3 | 1351 | } |
dea0fc0b | 1352 | *newlen = d - dstart; |
a0ed51b3 LW |
1353 | return d; |
1354 | } | |
1355 | ||
1356 | /* Note: this one is slightly destructive of the source. */ | |
1357 | ||
1358 | U8* | |
dea0fc0b | 1359 | Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen) |
a0ed51b3 LW |
1360 | { |
1361 | U8* s = (U8*)p; | |
d4c19fe8 | 1362 | U8* const send = s + bytelen; |
7918f24d NC |
1363 | |
1364 | PERL_ARGS_ASSERT_UTF16_TO_UTF8_REVERSED; | |
1365 | ||
e0ea5e2d NC |
1366 | if (bytelen & 1) |
1367 | Perl_croak(aTHX_ "panic: utf16_to_utf8_reversed: odd bytelen %"UVuf, | |
1368 | (UV)bytelen); | |
1369 | ||
a0ed51b3 | 1370 | while (s < send) { |
d4c19fe8 | 1371 | const U8 tmp = s[0]; |
a0ed51b3 LW |
1372 | s[0] = s[1]; |
1373 | s[1] = tmp; | |
1374 | s += 2; | |
1375 | } | |
dea0fc0b | 1376 | return utf16_to_utf8(p, d, bytelen, newlen); |
a0ed51b3 LW |
1377 | } |
1378 | ||
922e8cb4 KW |
1379 | bool |
1380 | Perl__is_uni_FOO(pTHX_ const U8 classnum, const UV c) | |
1381 | { | |
1382 | U8 tmpbuf[UTF8_MAXBYTES+1]; | |
1383 | uvchr_to_utf8(tmpbuf, c); | |
1384 | return _is_utf8_FOO(classnum, tmpbuf); | |
1385 | } | |
1386 | ||
f9ae8fb6 JD |
1387 | /* Internal function so we can deprecate the external one, and call |
1388 | this one from other deprecated functions in this file */ | |
1389 | ||
f2645549 KW |
1390 | bool |
1391 | Perl__is_utf8_idstart(pTHX_ const U8 *p) | |
61b19385 | 1392 | { |
f2645549 | 1393 | PERL_ARGS_ASSERT__IS_UTF8_IDSTART; |
61b19385 KW |
1394 | |
1395 | if (*p == '_') | |
1396 | return TRUE; | |
f25ce844 | 1397 | return is_utf8_common(p, &PL_utf8_idstart, "IdStart", NULL); |
61b19385 KW |
1398 | } |
1399 | ||
5092f92a | 1400 | bool |
eba68aa0 KW |
1401 | Perl__is_uni_perl_idcont(pTHX_ UV c) |
1402 | { | |
1403 | U8 tmpbuf[UTF8_MAXBYTES+1]; | |
1404 | uvchr_to_utf8(tmpbuf, c); | |
1405 | return _is_utf8_perl_idcont(tmpbuf); | |
1406 | } | |
1407 | ||
1408 | bool | |
f91dcd13 KW |
1409 | Perl__is_uni_perl_idstart(pTHX_ UV c) |
1410 | { | |
1411 | U8 tmpbuf[UTF8_MAXBYTES+1]; | |
1412 | uvchr_to_utf8(tmpbuf, c); | |
1413 | return _is_utf8_perl_idstart(tmpbuf); | |
1414 | } | |
1415 | ||
3a4c58c9 KW |
1416 | UV |
1417 | Perl__to_upper_title_latin1(pTHX_ const U8 c, U8* p, STRLEN *lenp, const char S_or_s) | |
1418 | { | |
1419 | /* We have the latin1-range values compiled into the core, so just use | |
4a4088c4 | 1420 | * those, converting the result to UTF-8. The only difference between upper |
3a4c58c9 KW |
1421 | * and title case in this range is that LATIN_SMALL_LETTER_SHARP_S is |
1422 | * either "SS" or "Ss". Which one to use is passed into the routine in | |
1423 | * 'S_or_s' to avoid a test */ | |
1424 | ||
1425 | UV converted = toUPPER_LATIN1_MOD(c); | |
1426 | ||
1427 | PERL_ARGS_ASSERT__TO_UPPER_TITLE_LATIN1; | |
1428 | ||
1429 | assert(S_or_s == 'S' || S_or_s == 's'); | |
1430 | ||
6f2d5cbc | 1431 | if (UVCHR_IS_INVARIANT(converted)) { /* No difference between the two for |
f4cd282c | 1432 | characters in this range */ |
3a4c58c9 KW |
1433 | *p = (U8) converted; |
1434 | *lenp = 1; | |
1435 | return converted; | |
1436 | } | |
1437 | ||
1438 | /* toUPPER_LATIN1_MOD gives the correct results except for three outliers, | |
1439 | * which it maps to one of them, so as to only have to have one check for | |
1440 | * it in the main case */ | |
1441 | if (UNLIKELY(converted == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) { | |
1442 | switch (c) { | |
1443 | case LATIN_SMALL_LETTER_Y_WITH_DIAERESIS: | |
1444 | converted = LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS; | |
1445 | break; | |
1446 | case MICRO_SIGN: | |
1447 | converted = GREEK_CAPITAL_LETTER_MU; | |
1448 | break; | |
79e064b9 KW |
1449 | #if UNICODE_MAJOR_VERSION > 2 \ |
1450 | || (UNICODE_MAJOR_VERSION == 2 && UNICODE_DOT_VERSION >= 1 \ | |
1451 | && UNICODE_DOT_DOT_VERSION >= 8) | |
3a4c58c9 KW |
1452 | case LATIN_SMALL_LETTER_SHARP_S: |
1453 | *(p)++ = 'S'; | |
1454 | *p = S_or_s; | |
1455 | *lenp = 2; | |
1456 | return 'S'; | |
79e064b9 | 1457 | #endif |
3a4c58c9 KW |
1458 | default: |
1459 | Perl_croak(aTHX_ "panic: to_upper_title_latin1 did not expect '%c' to map to '%c'", c, LATIN_SMALL_LETTER_Y_WITH_DIAERESIS); | |
e5964223 | 1460 | NOT_REACHED; /* NOTREACHED */ |
3a4c58c9 KW |
1461 | } |
1462 | } | |
1463 | ||
1464 | *(p)++ = UTF8_TWO_BYTE_HI(converted); | |
1465 | *p = UTF8_TWO_BYTE_LO(converted); | |
1466 | *lenp = 2; | |
1467 | ||
1468 | return converted; | |
1469 | } | |
1470 | ||
50bda2c3 KW |
1471 | /* Call the function to convert a UTF-8 encoded character to the specified case. |
1472 | * Note that there may be more than one character in the result. | |
1473 | * INP is a pointer to the first byte of the input character | |
1474 | * OUTP will be set to the first byte of the string of changed characters. It | |
1475 | * needs to have space for UTF8_MAXBYTES_CASE+1 bytes | |
1476 | * LENP will be set to the length in bytes of the string of changed characters | |
1477 | * | |
1478 | * The functions return the ordinal of the first character in the string of OUTP */ | |
b9992569 KW |
1479 | #define CALL_UPPER_CASE(uv, s, d, lenp) _to_utf8_case(uv, s, d, lenp, &PL_utf8_toupper, "ToUc", "") |
1480 | #define CALL_TITLE_CASE(uv, s, d, lenp) _to_utf8_case(uv, s, d, lenp, &PL_utf8_totitle, "ToTc", "") | |
1481 | #define CALL_LOWER_CASE(uv, s, d, lenp) _to_utf8_case(uv, s, d, lenp, &PL_utf8_tolower, "ToLc", "") | |
50bda2c3 | 1482 | |
b9992569 KW |
1483 | /* This additionally has the input parameter 'specials', which if non-zero will |
1484 | * cause this to use the specials hash for folding (meaning get full case | |
50bda2c3 | 1485 | * folding); otherwise, when zero, this implies a simple case fold */ |
b9992569 | 1486 | #define CALL_FOLD_CASE(uv, s, d, lenp, specials) _to_utf8_case(uv, s, d, lenp, &PL_utf8_tofold, "ToCf", (specials) ? "" : NULL) |
c3fd2246 | 1487 | |
84afefe6 JH |
1488 | UV |
1489 | Perl_to_uni_upper(pTHX_ UV c, U8* p, STRLEN *lenp) | |
a0ed51b3 | 1490 | { |
a1433954 KW |
1491 | /* Convert the Unicode character whose ordinal is <c> to its uppercase |
1492 | * version and store that in UTF-8 in <p> and its length in bytes in <lenp>. | |
1493 | * Note that the <p> needs to be at least UTF8_MAXBYTES_CASE+1 bytes since | |
c3fd2246 KW |
1494 | * the changed version may be longer than the original character. |
1495 | * | |
1496 | * The ordinal of the first character of the changed version is returned | |
1497 | * (but note, as explained above, that there may be more.) */ | |
1498 | ||
7918f24d NC |
1499 | PERL_ARGS_ASSERT_TO_UNI_UPPER; |
1500 | ||
3a4c58c9 KW |
1501 | if (c < 256) { |
1502 | return _to_upper_title_latin1((U8) c, p, lenp, 'S'); | |
1503 | } | |
1504 | ||
0ebc6274 | 1505 | uvchr_to_utf8(p, c); |
b9992569 | 1506 | return CALL_UPPER_CASE(c, p, p, lenp); |
a0ed51b3 LW |
1507 | } |
1508 | ||
84afefe6 JH |
1509 | UV |
1510 | Perl_to_uni_title(pTHX_ UV c, U8* p, STRLEN *lenp) | |
a0ed51b3 | 1511 | { |
7918f24d NC |
1512 | PERL_ARGS_ASSERT_TO_UNI_TITLE; |
1513 | ||
3a4c58c9 KW |
1514 | if (c < 256) { |
1515 | return _to_upper_title_latin1((U8) c, p, lenp, 's'); | |
1516 | } | |
1517 | ||
0ebc6274 | 1518 | uvchr_to_utf8(p, c); |
b9992569 | 1519 | return CALL_TITLE_CASE(c, p, p, lenp); |
a0ed51b3 LW |
1520 | } |
1521 | ||
afc16117 | 1522 | STATIC U8 |
81611534 | 1523 | S_to_lower_latin1(const U8 c, U8* p, STRLEN *lenp) |
afc16117 KW |
1524 | { |
1525 | /* We have the latin1-range values compiled into the core, so just use | |
4a4088c4 | 1526 | * those, converting the result to UTF-8. Since the result is always just |
a1433954 | 1527 | * one character, we allow <p> to be NULL */ |
afc16117 KW |
1528 | |
1529 | U8 converted = toLOWER_LATIN1(c); | |
1530 | ||
1531 | if (p != NULL) { | |
6f2d5cbc | 1532 | if (NATIVE_BYTE_IS_INVARIANT(converted)) { |
afc16117 KW |
1533 | *p = converted; |
1534 | *lenp = 1; | |
1535 | } | |
1536 | else { | |
430c9760 KW |
1537 | /* Result is known to always be < 256, so can use the EIGHT_BIT |
1538 | * macros */ | |
1539 | *p = UTF8_EIGHT_BIT_HI(converted); | |
1540 | *(p+1) = UTF8_EIGHT_BIT_LO(converted); | |
afc16117 KW |
1541 | *lenp = 2; |
1542 | } | |
1543 | } | |
1544 | return converted; | |
1545 | } | |
1546 | ||
84afefe6 JH |
1547 | UV |
1548 | Perl_to_uni_lower(pTHX_ UV c, U8* p, STRLEN *lenp) | |
a0ed51b3 | 1549 | { |
7918f24d NC |
1550 | PERL_ARGS_ASSERT_TO_UNI_LOWER; |
1551 | ||
afc16117 KW |
1552 | if (c < 256) { |
1553 | return to_lower_latin1((U8) c, p, lenp); | |
bca00c02 KW |
1554 | } |
1555 | ||
afc16117 | 1556 | uvchr_to_utf8(p, c); |
b9992569 | 1557 | return CALL_LOWER_CASE(c, p, p, lenp); |
a0ed51b3 LW |
1558 | } |
1559 | ||
84afefe6 | 1560 | UV |
51910141 | 1561 | Perl__to_fold_latin1(pTHX_ const U8 c, U8* p, STRLEN *lenp, const unsigned int flags) |
a1dde8de | 1562 | { |
51910141 | 1563 | /* Corresponds to to_lower_latin1(); <flags> bits meanings: |
1ca267a5 | 1564 | * FOLD_FLAGS_NOMIX_ASCII iff non-ASCII to ASCII folds are prohibited |
51910141 | 1565 | * FOLD_FLAGS_FULL iff full folding is to be used; |
1ca267a5 KW |
1566 | * |
1567 | * Not to be used for locale folds | |
51910141 | 1568 | */ |
f673fad4 | 1569 | |
a1dde8de KW |
1570 | UV converted; |
1571 | ||
1572 | PERL_ARGS_ASSERT__TO_FOLD_LATIN1; | |
81611534 | 1573 | PERL_UNUSED_CONTEXT; |
a1dde8de | 1574 | |
1ca267a5 KW |
1575 | assert (! (flags & FOLD_FLAGS_LOCALE)); |
1576 | ||
659a7c2d | 1577 | if (UNLIKELY(c == MICRO_SIGN)) { |
a1dde8de KW |
1578 | converted = GREEK_SMALL_LETTER_MU; |
1579 | } | |
9b63e895 KW |
1580 | #if UNICODE_MAJOR_VERSION > 3 /* no multifolds in early Unicode */ \ |
1581 | || (UNICODE_MAJOR_VERSION == 3 && ( UNICODE_DOT_VERSION > 0) \ | |
1582 | || UNICODE_DOT_DOT_VERSION > 0) | |
659a7c2d KW |
1583 | else if ( (flags & FOLD_FLAGS_FULL) |
1584 | && UNLIKELY(c == LATIN_SMALL_LETTER_SHARP_S)) | |
1585 | { | |
1ca267a5 KW |
1586 | /* If can't cross 127/128 boundary, can't return "ss"; instead return |
1587 | * two U+017F characters, as fc("\df") should eq fc("\x{17f}\x{17f}") | |
1588 | * under those circumstances. */ | |
1589 | if (flags & FOLD_FLAGS_NOMIX_ASCII) { | |
1590 | *lenp = 2 * sizeof(LATIN_SMALL_LETTER_LONG_S_UTF8) - 2; | |
1591 | Copy(LATIN_SMALL_LETTER_LONG_S_UTF8 LATIN_SMALL_LETTER_LONG_S_UTF8, | |
1592 | p, *lenp, U8); | |
1593 | return LATIN_SMALL_LETTER_LONG_S; | |
1594 | } | |
1595 | else { | |
4f489194 KW |
1596 | *(p)++ = 's'; |
1597 | *p = 's'; | |
1598 | *lenp = 2; | |
1599 | return 's'; | |
1ca267a5 | 1600 | } |
a1dde8de | 1601 | } |
9b63e895 | 1602 | #endif |
a1dde8de KW |
1603 | else { /* In this range the fold of all other characters is their lower |
1604 | case */ | |
1605 | converted = toLOWER_LATIN1(c); | |
1606 | } | |
1607 | ||
6f2d5cbc | 1608 | if (UVCHR_IS_INVARIANT(converted)) { |
a1dde8de KW |
1609 | *p = (U8) converted; |
1610 | *lenp = 1; | |
1611 | } | |
1612 | else { | |
1613 | *(p)++ = UTF8_TWO_BYTE_HI(converted); | |
1614 | *p = UTF8_TWO_BYTE_LO(converted); | |
1615 | *lenp = 2; | |
1616 | } | |
1617 | ||
1618 | return converted; | |
1619 | } | |
1620 | ||
1621 | UV | |
31f05a37 | 1622 | Perl__to_uni_fold_flags(pTHX_ UV c, U8* p, STRLEN *lenp, U8 flags) |
84afefe6 | 1623 | { |
4b593389 | 1624 | |
a0270393 KW |
1625 | /* Not currently externally documented, and subject to change |
1626 | * <flags> bits meanings: | |
1627 | * FOLD_FLAGS_FULL iff full folding is to be used; | |
31f05a37 KW |
1628 | * FOLD_FLAGS_LOCALE is set iff the rules from the current underlying |
1629 | * locale are to be used. | |
a0270393 KW |
1630 | * FOLD_FLAGS_NOMIX_ASCII iff non-ASCII to ASCII folds are prohibited |
1631 | */ | |
4b593389 | 1632 | |
36bb2ab6 | 1633 | PERL_ARGS_ASSERT__TO_UNI_FOLD_FLAGS; |
7918f24d | 1634 | |
780fcc9f KW |
1635 | if (flags & FOLD_FLAGS_LOCALE) { |
1636 | /* Treat a UTF-8 locale as not being in locale at all */ | |
1637 | if (IN_UTF8_CTYPE_LOCALE) { | |
1638 | flags &= ~FOLD_FLAGS_LOCALE; | |
1639 | } | |
1640 | else { | |
1641 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; | |
e7b7ac46 | 1642 | goto needs_full_generality; |
780fcc9f | 1643 | } |
31f05a37 KW |
1644 | } |
1645 | ||
a1dde8de | 1646 | if (c < 256) { |
e7b7ac46 | 1647 | return _to_fold_latin1((U8) c, p, lenp, |
31f05a37 | 1648 | flags & (FOLD_FLAGS_FULL | FOLD_FLAGS_NOMIX_ASCII)); |
a1dde8de KW |
1649 | } |
1650 | ||
2f306ab9 | 1651 | /* Here, above 255. If no special needs, just use the macro */ |
a0270393 KW |
1652 | if ( ! (flags & (FOLD_FLAGS_LOCALE|FOLD_FLAGS_NOMIX_ASCII))) { |
1653 | uvchr_to_utf8(p, c); | |
b9992569 | 1654 | return CALL_FOLD_CASE(c, p, p, lenp, flags & FOLD_FLAGS_FULL); |
a0270393 KW |
1655 | } |
1656 | else { /* Otherwise, _to_utf8_fold_flags has the intelligence to deal with | |
1657 | the special flags. */ | |
1658 | U8 utf8_c[UTF8_MAXBYTES + 1]; | |
e7b7ac46 KW |
1659 | |
1660 | needs_full_generality: | |
a0270393 | 1661 | uvchr_to_utf8(utf8_c, c); |
445bf929 | 1662 | return _to_utf8_fold_flags(utf8_c, p, lenp, flags); |
a0270393 | 1663 | } |
84afefe6 JH |
1664 | } |
1665 | ||
26483009 | 1666 | PERL_STATIC_INLINE bool |
5141f98e | 1667 | S_is_utf8_common(pTHX_ const U8 *const p, SV **swash, |
f25ce844 | 1668 | const char *const swashname, SV* const invlist) |
bde6a22d | 1669 | { |
ea317ccb KW |
1670 | /* returns a boolean giving whether or not the UTF8-encoded character that |
1671 | * starts at <p> is in the swash indicated by <swashname>. <swash> | |
1672 | * contains a pointer to where the swash indicated by <swashname> | |
1673 | * is to be stored; which this routine will do, so that future calls will | |
f25ce844 KW |
1674 | * look at <*swash> and only generate a swash if it is not null. <invlist> |
1675 | * is NULL or an inversion list that defines the swash. If not null, it | |
1676 | * saves time during initialization of the swash. | |
ea317ccb KW |
1677 | * |
1678 | * Note that it is assumed that the buffer length of <p> is enough to | |
1679 | * contain all the bytes that comprise the character. Thus, <*p> should | |
1680 | * have been checked before this call for mal-formedness enough to assure | |
1681 | * that. */ | |
1682 | ||
7918f24d NC |
1683 | PERL_ARGS_ASSERT_IS_UTF8_COMMON; |
1684 | ||
492a624f | 1685 | /* The API should have included a length for the UTF-8 character in <p>, |
28123549 | 1686 | * but it doesn't. We therefore assume that p has been validated at least |
492a624f KW |
1687 | * as far as there being enough bytes available in it to accommodate the |
1688 | * character without reading beyond the end, and pass that number on to the | |
1689 | * validating routine */ | |
6302f837 | 1690 | if (! isUTF8_CHAR(p, p + UTF8SKIP(p))) { |
28123549 KW |
1691 | if (ckWARN_d(WARN_UTF8)) { |
1692 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED,WARN_UTF8), | |
9816f121 | 1693 | "Passing malformed UTF-8 to \"%s\" is deprecated", swashname); |
28123549 KW |
1694 | if (ckWARN(WARN_UTF8)) { /* This will output details as to the |
1695 | what the malformation is */ | |
1696 | utf8_to_uvchr_buf(p, p + UTF8SKIP(p), NULL); | |
1697 | } | |
1698 | } | |
1699 | return FALSE; | |
1700 | } | |
87367d5f KW |
1701 | if (!*swash) { |
1702 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
f25ce844 KW |
1703 | *swash = _core_swash_init("utf8", |
1704 | ||
1705 | /* Only use the name if there is no inversion | |
1706 | * list; otherwise will go out to disk */ | |
1707 | (invlist) ? "" : swashname, | |
1708 | ||
1709 | &PL_sv_undef, 1, 0, invlist, &flags); | |
87367d5f | 1710 | } |
28123549 | 1711 | |
bde6a22d NC |
1712 | return swash_fetch(*swash, p, TRUE) != 0; |
1713 | } | |
1714 | ||
1715 | bool | |
922e8cb4 KW |
1716 | Perl__is_utf8_FOO(pTHX_ const U8 classnum, const U8 *p) |
1717 | { | |
922e8cb4 KW |
1718 | PERL_ARGS_ASSERT__IS_UTF8_FOO; |
1719 | ||
1720 | assert(classnum < _FIRST_NON_SWASH_CC); | |
1721 | ||
f25ce844 KW |
1722 | return is_utf8_common(p, |
1723 | &PL_utf8_swash_ptrs[classnum], | |
1724 | swash_property_names[classnum], | |
1725 | PL_XPosix_ptrs[classnum]); | |
922e8cb4 KW |
1726 | } |
1727 | ||
1728 | bool | |
f2645549 | 1729 | Perl__is_utf8_perl_idstart(pTHX_ const U8 *p) |
a0ed51b3 | 1730 | { |
f2645549 | 1731 | SV* invlist = NULL; |
7918f24d | 1732 | |
f2645549 | 1733 | PERL_ARGS_ASSERT__IS_UTF8_PERL_IDSTART; |
7918f24d | 1734 | |
f2645549 KW |
1735 | if (! PL_utf8_perl_idstart) { |
1736 | invlist = _new_invlist_C_array(_Perl_IDStart_invlist); | |
1737 | } | |
60071a22 | 1738 | return is_utf8_common(p, &PL_utf8_perl_idstart, "_Perl_IDStart", invlist); |
82686b01 JH |
1739 | } |
1740 | ||
1741 | bool | |
f2645549 | 1742 | Perl__is_utf8_xidstart(pTHX_ const U8 *p) |
c11ff943 | 1743 | { |
f2645549 | 1744 | PERL_ARGS_ASSERT__IS_UTF8_XIDSTART; |
c11ff943 KW |
1745 | |
1746 | if (*p == '_') | |
1747 | return TRUE; | |
f25ce844 | 1748 | return is_utf8_common(p, &PL_utf8_xidstart, "XIdStart", NULL); |
c11ff943 KW |
1749 | } |
1750 | ||
1751 | bool | |
eba68aa0 KW |
1752 | Perl__is_utf8_perl_idcont(pTHX_ const U8 *p) |
1753 | { | |
b24b43f7 | 1754 | SV* invlist = NULL; |
eba68aa0 KW |
1755 | |
1756 | PERL_ARGS_ASSERT__IS_UTF8_PERL_IDCONT; | |
1757 | ||
b24b43f7 KW |
1758 | if (! PL_utf8_perl_idcont) { |
1759 | invlist = _new_invlist_C_array(_Perl_IDCont_invlist); | |
1760 | } | |
60071a22 | 1761 | return is_utf8_common(p, &PL_utf8_perl_idcont, "_Perl_IDCont", invlist); |
eba68aa0 KW |
1762 | } |
1763 | ||
eba68aa0 | 1764 | bool |
f2645549 | 1765 | Perl__is_utf8_idcont(pTHX_ const U8 *p) |
82686b01 | 1766 | { |
f2645549 | 1767 | PERL_ARGS_ASSERT__IS_UTF8_IDCONT; |
7918f24d | 1768 | |
f25ce844 | 1769 | return is_utf8_common(p, &PL_utf8_idcont, "IdContinue", NULL); |
a0ed51b3 LW |
1770 | } |
1771 | ||
1772 | bool | |
f2645549 | 1773 | Perl__is_utf8_xidcont(pTHX_ const U8 *p) |
c11ff943 | 1774 | { |
f2645549 | 1775 | PERL_ARGS_ASSERT__IS_UTF8_XIDCONT; |
c11ff943 | 1776 | |
f25ce844 | 1777 | return is_utf8_common(p, &PL_utf8_idcont, "XIdContinue", NULL); |
c11ff943 KW |
1778 | } |
1779 | ||
1780 | bool | |
7dbf68d2 KW |
1781 | Perl__is_utf8_mark(pTHX_ const U8 *p) |
1782 | { | |
7dbf68d2 KW |
1783 | PERL_ARGS_ASSERT__IS_UTF8_MARK; |
1784 | ||
f25ce844 | 1785 | return is_utf8_common(p, &PL_utf8_mark, "IsM", NULL); |
7dbf68d2 KW |
1786 | } |
1787 | ||
6b5c0936 | 1788 | /* |
87cea99e | 1789 | =for apidoc to_utf8_case |
6b5c0936 | 1790 | |
9da1e7cb KW |
1791 | Instead use the appropriate one of L</toUPPER_utf8>, |
1792 | L</toTITLE_utf8>, | |
1793 | L</toLOWER_utf8>, | |
1794 | or L</toFOLD_utf8>. | |
1795 | ||
6fae5207 | 1796 | C<p> contains the pointer to the UTF-8 string encoding |
a1433954 KW |
1797 | the character that is being converted. This routine assumes that the character |
1798 | at C<p> is well-formed. | |
6b5c0936 | 1799 | |
6fae5207 KW |
1800 | C<ustrp> is a pointer to the character buffer to put the |
1801 | conversion result to. C<lenp> is a pointer to the length | |
6b5c0936 JH |
1802 | of the result. |
1803 | ||
6fae5207 | 1804 | C<swashp> is a pointer to the swash to use. |
6b5c0936 | 1805 | |
a1433954 | 1806 | Both the special and normal mappings are stored in F<lib/unicore/To/Foo.pl>, |
796b6530 | 1807 | and loaded by C<SWASHNEW>, using F<lib/utf8_heavy.pl>. C<special> (usually, |
0134edef | 1808 | but not always, a multicharacter mapping), is tried first. |
6b5c0936 | 1809 | |
4a8240a3 KW |
1810 | C<special> is a string, normally C<NULL> or C<"">. C<NULL> means to not use |
1811 | any special mappings; C<""> means to use the special mappings. Values other | |
1812 | than these two are treated as the name of the hash containing the special | |
1813 | mappings, like C<"utf8::ToSpecLower">. | |
6b5c0936 | 1814 | |
796b6530 KW |
1815 | C<normal> is a string like C<"ToLower"> which means the swash |
1816 | C<%utf8::ToLower>. | |
0134edef | 1817 | |
760c7c2f KW |
1818 | Code points above the platform's C<IV_MAX> will raise a deprecation warning, |
1819 | unless those are turned off. | |
1820 | ||
0134edef | 1821 | =cut */ |
6b5c0936 | 1822 | |
2104c8d9 | 1823 | UV |
9a957fbc AL |
1824 | Perl_to_utf8_case(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, |
1825 | SV **swashp, const char *normal, const char *special) | |
a0ed51b3 | 1826 | { |
b9992569 KW |
1827 | PERL_ARGS_ASSERT_TO_UTF8_CASE; |
1828 | ||
1829 | return _to_utf8_case(valid_utf8_to_uvchr(p, NULL), p, ustrp, lenp, swashp, normal, special); | |
1830 | } | |
1831 | ||
1832 | /* change namve uv1 to 'from' */ | |
6a4a25f4 | 1833 | STATIC UV |
b9992569 KW |
1834 | S__to_utf8_case(pTHX_ const UV uv1, const U8 *p, U8* ustrp, STRLEN *lenp, |
1835 | SV **swashp, const char *normal, const char *special) | |
1836 | { | |
0134edef | 1837 | STRLEN len = 0; |
7918f24d | 1838 | |
b9992569 | 1839 | PERL_ARGS_ASSERT__TO_UTF8_CASE; |
7918f24d | 1840 | |
36eaa811 KW |
1841 | /* For code points that don't change case, we already know that the output |
1842 | * of this function is the unchanged input, so we can skip doing look-ups | |
1843 | * for them. Unfortunately the case-changing code points are scattered | |
1844 | * around. But there are some long consecutive ranges where there are no | |
1845 | * case changing code points. By adding tests, we can eliminate the lookup | |
1846 | * for all the ones in such ranges. This is currently done here only for | |
1847 | * just a few cases where the scripts are in common use in modern commerce | |
1848 | * (and scripts adjacent to those which can be included without additional | |
1849 | * tests). */ | |
1850 | ||
1851 | if (uv1 >= 0x0590) { | |
1852 | /* This keeps from needing further processing the code points most | |
1853 | * likely to be used in the following non-cased scripts: Hebrew, | |
1854 | * Arabic, Syriac, Thaana, NKo, Samaritan, Mandaic, Devanagari, | |
1855 | * Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada, | |
1856 | * Malayalam, Sinhala, Thai, Lao, Tibetan, Myanmar */ | |
1857 | if (uv1 < 0x10A0) { | |
1858 | goto cases_to_self; | |
1859 | } | |
1860 | ||
1861 | /* The following largish code point ranges also don't have case | |
1862 | * changes, but khw didn't think they warranted extra tests to speed | |
1863 | * them up (which would slightly slow down everything else above them): | |
1864 | * 1100..139F Hangul Jamo, Ethiopic | |
1865 | * 1400..1CFF Unified Canadian Aboriginal Syllabics, Ogham, Runic, | |
1866 | * Tagalog, Hanunoo, Buhid, Tagbanwa, Khmer, Mongolian, | |
1867 | * Limbu, Tai Le, New Tai Lue, Buginese, Tai Tham, | |
1868 | * Combining Diacritical Marks Extended, Balinese, | |
1869 | * Sundanese, Batak, Lepcha, Ol Chiki | |
1870 | * 2000..206F General Punctuation | |
1871 | */ | |
1872 | ||
1873 | if (uv1 >= 0x2D30) { | |
1874 | ||
1875 | /* This keeps the from needing further processing the code points | |
1876 | * most likely to be used in the following non-cased major scripts: | |
1877 | * CJK, Katakana, Hiragana, plus some less-likely scripts. | |
1878 | * | |
1879 | * (0x2D30 above might have to be changed to 2F00 in the unlikely | |
1880 | * event that Unicode eventually allocates the unused block as of | |
1881 | * v8.0 2FE0..2FEF to code points that are cased. khw has verified | |
1882 | * that the test suite will start having failures to alert you | |
1883 | * should that happen) */ | |
1884 | if (uv1 < 0xA640) { | |
1885 | goto cases_to_self; | |
1886 | } | |
1887 | ||
1888 | if (uv1 >= 0xAC00) { | |
1889 | if (UNLIKELY(UNICODE_IS_SURROGATE(uv1))) { | |
5af9bc97 KW |
1890 | if (ckWARN_d(WARN_SURROGATE)) { |
1891 | const char* desc = (PL_op) ? OP_DESC(PL_op) : normal; | |
1892 | Perl_warner(aTHX_ packWARN(WARN_SURROGATE), | |
1893 | "Operation \"%s\" returns its argument for UTF-16 surrogate U+%04"UVXf"", desc, uv1); | |
1894 | } | |
1895 | goto cases_to_self; | |
1896 | } | |
36eaa811 KW |
1897 | |
1898 | /* AC00..FAFF Catches Hangul syllables and private use, plus | |
1899 | * some others */ | |
1900 | if (uv1 < 0xFB00) { | |
1901 | goto cases_to_self; | |
1902 | ||
1903 | } | |
1904 | ||
5af9bc97 KW |
1905 | if (UNLIKELY(UNICODE_IS_SUPER(uv1))) { |
1906 | if ( UNLIKELY(uv1 > MAX_NON_DEPRECATED_CP) | |
1907 | && ckWARN_d(WARN_DEPRECATED)) | |
1908 | { | |
1909 | Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), | |
1910 | cp_above_legal_max, uv1, MAX_NON_DEPRECATED_CP); | |
1911 | } | |
1912 | if (ckWARN_d(WARN_NON_UNICODE)) { | |
1913 | const char* desc = (PL_op) ? OP_DESC(PL_op) : normal; | |
1914 | Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE), | |
1915 | "Operation \"%s\" returns its argument for non-Unicode code point 0x%04"UVXf"", desc, uv1); | |
1916 | } | |
1917 | goto cases_to_self; | |
1918 | } | |
3bfc1e70 KW |
1919 | #ifdef HIGHEST_CASE_CHANGING_CP_FOR_USE_ONLY_BY_UTF8_DOT_C |
1920 | if (UNLIKELY(uv1 | |
1921 | > HIGHEST_CASE_CHANGING_CP_FOR_USE_ONLY_BY_UTF8_DOT_C)) | |
1922 | { | |
1923 | ||
1924 | /* As of this writing, this means we avoid swash creation | |
1925 | * for anything beyond low Plane 1 */ | |
1926 | goto cases_to_self; | |
1927 | } | |
1928 | #endif | |
36eaa811 KW |
1929 | } |
1930 | } | |
9ae3ac1a | 1931 | |
36eaa811 KW |
1932 | /* Note that non-characters are perfectly legal, so no warning should |
1933 | * be given. There are so few of them, that it isn't worth the extra | |
1934 | * tests to avoid swash creation */ | |
9ae3ac1a KW |
1935 | } |
1936 | ||
0134edef | 1937 | if (!*swashp) /* load on-demand */ |
5ab9d2ef | 1938 | *swashp = _core_swash_init("utf8", normal, &PL_sv_undef, 4, 0, NULL, NULL); |
0134edef | 1939 | |
a6f87d8c | 1940 | if (special) { |
0134edef | 1941 | /* It might be "special" (sometimes, but not always, |
2a37f04d | 1942 | * a multicharacter mapping) */ |
4a8240a3 | 1943 | HV *hv = NULL; |
b08cf34e JH |
1944 | SV **svp; |
1945 | ||
4a8240a3 KW |
1946 | /* If passed in the specials name, use that; otherwise use any |
1947 | * given in the swash */ | |
1948 | if (*special != '\0') { | |
1949 | hv = get_hv(special, 0); | |
1950 | } | |
1951 | else { | |
1952 | svp = hv_fetchs(MUTABLE_HV(SvRV(*swashp)), "SPECIALS", 0); | |
1953 | if (svp) { | |
1954 | hv = MUTABLE_HV(SvRV(*svp)); | |
1955 | } | |
1956 | } | |
1957 | ||
176fe009 | 1958 | if (hv |
5f560d8a | 1959 | && (svp = hv_fetch(hv, (const char*)p, UVCHR_SKIP(uv1), FALSE)) |
176fe009 KW |
1960 | && (*svp)) |
1961 | { | |
cfd0369c | 1962 | const char *s; |
47654450 | 1963 | |
cfd0369c | 1964 | s = SvPV_const(*svp, len); |
47654450 | 1965 | if (len == 1) |
f4cd282c | 1966 | /* EIGHTBIT */ |
c80e42f3 | 1967 | len = uvchr_to_utf8(ustrp, *(U8*)s) - ustrp; |
2a37f04d | 1968 | else { |
d2dcd0fb | 1969 | Copy(s, ustrp, len, U8); |
29e98929 | 1970 | } |
983ffd37 | 1971 | } |
0134edef JH |
1972 | } |
1973 | ||
1974 | if (!len && *swashp) { | |
4a4088c4 | 1975 | const UV uv2 = swash_fetch(*swashp, p, TRUE /* => is UTF-8 */); |
d4c19fe8 | 1976 | |
0134edef JH |
1977 | if (uv2) { |
1978 | /* It was "normal" (a single character mapping). */ | |
f4cd282c | 1979 | len = uvchr_to_utf8(ustrp, uv2) - ustrp; |
2a37f04d JH |
1980 | } |
1981 | } | |
1feea2c7 | 1982 | |
cbe07460 KW |
1983 | if (len) { |
1984 | if (lenp) { | |
1985 | *lenp = len; | |
1986 | } | |
1987 | return valid_utf8_to_uvchr(ustrp, 0); | |
1988 | } | |
1989 | ||
1990 | /* Here, there was no mapping defined, which means that the code point maps | |
1991 | * to itself. Return the inputs */ | |
e24dfe9c | 1992 | cases_to_self: |
bfdf22ec | 1993 | len = UTF8SKIP(p); |
ca9fab46 KW |
1994 | if (p != ustrp) { /* Don't copy onto itself */ |
1995 | Copy(p, ustrp, len, U8); | |
1996 | } | |
0134edef | 1997 | |
2a37f04d JH |
1998 | if (lenp) |
1999 | *lenp = len; | |
2000 | ||
f4cd282c | 2001 | return uv1; |
cbe07460 | 2002 | |
a0ed51b3 LW |
2003 | } |
2004 | ||
051a06d4 | 2005 | STATIC UV |
357aadde | 2006 | S_check_locale_boundary_crossing(pTHX_ const U8* const p, const UV result, U8* const ustrp, STRLEN *lenp) |
051a06d4 | 2007 | { |
4a4088c4 | 2008 | /* This is called when changing the case of a UTF-8-encoded character above |
31f05a37 KW |
2009 | * the Latin1 range, and the operation is in a non-UTF-8 locale. If the |
2010 | * result contains a character that crosses the 255/256 boundary, disallow | |
2011 | * the change, and return the original code point. See L<perlfunc/lc> for | |
2012 | * why; | |
051a06d4 | 2013 | * |
a1433954 KW |
2014 | * p points to the original string whose case was changed; assumed |
2015 | * by this routine to be well-formed | |
051a06d4 KW |
2016 | * result the code point of the first character in the changed-case string |
2017 | * ustrp points to the changed-case string (<result> represents its first char) | |
2018 | * lenp points to the length of <ustrp> */ | |
2019 | ||
2020 | UV original; /* To store the first code point of <p> */ | |
2021 | ||
2022 | PERL_ARGS_ASSERT_CHECK_LOCALE_BOUNDARY_CROSSING; | |
2023 | ||
a4f12ed7 | 2024 | assert(UTF8_IS_ABOVE_LATIN1(*p)); |
051a06d4 KW |
2025 | |
2026 | /* We know immediately if the first character in the string crosses the | |
2027 | * boundary, so can skip */ | |
2028 | if (result > 255) { | |
2029 | ||
2030 | /* Look at every character in the result; if any cross the | |
2031 | * boundary, the whole thing is disallowed */ | |
2032 | U8* s = ustrp + UTF8SKIP(ustrp); | |
2033 | U8* e = ustrp + *lenp; | |
2034 | while (s < e) { | |
a4f12ed7 | 2035 | if (! UTF8_IS_ABOVE_LATIN1(*s)) { |
051a06d4 KW |
2036 | goto bad_crossing; |
2037 | } | |
2038 | s += UTF8SKIP(s); | |
2039 | } | |
2040 | ||
613abc6d KW |
2041 | /* Here, no characters crossed, result is ok as-is, but we warn. */ |
2042 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(p, p + UTF8SKIP(p)); | |
051a06d4 KW |
2043 | return result; |
2044 | } | |
2045 | ||
7b52d656 | 2046 | bad_crossing: |
051a06d4 KW |
2047 | |
2048 | /* Failed, have to return the original */ | |
4b88fb76 | 2049 | original = valid_utf8_to_uvchr(p, lenp); |
ab0b796c KW |
2050 | |
2051 | /* diag_listed_as: Can't do %s("%s") on non-UTF-8 locale; resolved to "%s". */ | |
2052 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
2053 | "Can't do %s(\"\\x{%"UVXf"}\") on non-UTF-8 locale; " | |
2054 | "resolved to \"\\x{%"UVXf"}\".", | |
357aadde | 2055 | OP_DESC(PL_op), |
ab0b796c KW |
2056 | original, |
2057 | original); | |
051a06d4 KW |
2058 | Copy(p, ustrp, *lenp, char); |
2059 | return original; | |
2060 | } | |
2061 | ||
d3e79532 | 2062 | /* |
87cea99e | 2063 | =for apidoc to_utf8_upper |
d3e79532 | 2064 | |
1f607577 | 2065 | Instead use L</toUPPER_utf8>. |
a1433954 | 2066 | |
d3e79532 JH |
2067 | =cut */ |
2068 | ||
051a06d4 | 2069 | /* Not currently externally documented, and subject to change: |
31f05a37 KW |
2070 | * <flags> is set iff iff the rules from the current underlying locale are to |
2071 | * be used. */ | |
051a06d4 | 2072 | |
2104c8d9 | 2073 | UV |
31f05a37 | 2074 | Perl__to_utf8_upper_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, bool flags) |
a0ed51b3 | 2075 | { |
051a06d4 KW |
2076 | UV result; |
2077 | ||
2078 | PERL_ARGS_ASSERT__TO_UTF8_UPPER_FLAGS; | |
7918f24d | 2079 | |
780fcc9f KW |
2080 | if (flags) { |
2081 | /* Treat a UTF-8 locale as not being in locale at all */ | |
2082 | if (IN_UTF8_CTYPE_LOCALE) { | |
2083 | flags = FALSE; | |
2084 | } | |
2085 | else { | |
2086 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; | |
2087 | } | |
31f05a37 KW |
2088 | } |
2089 | ||
3a4c58c9 | 2090 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 KW |
2091 | if (flags) { |
2092 | result = toUPPER_LC(*p); | |
2093 | } | |
2094 | else { | |
81c6c7ce | 2095 | return _to_upper_title_latin1(*p, ustrp, lenp, 'S'); |
051a06d4 | 2096 | } |
3a4c58c9 KW |
2097 | } |
2098 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 | 2099 | if (flags) { |
a62b247b | 2100 | U8 c = EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)); |
68067e4e | 2101 | result = toUPPER_LC(c); |
051a06d4 KW |
2102 | } |
2103 | else { | |
a62b247b | 2104 | return _to_upper_title_latin1(EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)), |
81c6c7ce | 2105 | ustrp, lenp, 'S'); |
051a06d4 KW |
2106 | } |
2107 | } | |
4a4088c4 | 2108 | else { /* UTF-8, ord above 255 */ |
b9992569 | 2109 | result = CALL_UPPER_CASE(valid_utf8_to_uvchr(p, NULL), p, ustrp, lenp); |
051a06d4 KW |
2110 | |
2111 | if (flags) { | |
357aadde | 2112 | result = check_locale_boundary_crossing(p, result, ustrp, lenp); |
051a06d4 KW |
2113 | } |
2114 | return result; | |
2115 | } | |
2116 | ||
4a4088c4 | 2117 | /* Here, used locale rules. Convert back to UTF-8 */ |
051a06d4 KW |
2118 | if (UTF8_IS_INVARIANT(result)) { |
2119 | *ustrp = (U8) result; | |
2120 | *lenp = 1; | |
2121 | } | |
2122 | else { | |
62cb07ea KW |
2123 | *ustrp = UTF8_EIGHT_BIT_HI((U8) result); |
2124 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO((U8) result); | |
051a06d4 | 2125 | *lenp = 2; |
3a4c58c9 | 2126 | } |
baa60164 | 2127 | |
051a06d4 | 2128 | return result; |
983ffd37 | 2129 | } |
a0ed51b3 | 2130 | |
d3e79532 | 2131 | /* |
87cea99e | 2132 | =for apidoc to_utf8_title |
d3e79532 | 2133 | |
1f607577 | 2134 | Instead use L</toTITLE_utf8>. |
a1433954 | 2135 | |
d3e79532 JH |
2136 | =cut */ |
2137 | ||
051a06d4 | 2138 | /* Not currently externally documented, and subject to change: |
31f05a37 KW |
2139 | * <flags> is set iff the rules from the current underlying locale are to be |
2140 | * used. Since titlecase is not defined in POSIX, for other than a | |
2141 | * UTF-8 locale, uppercase is used instead for code points < 256. | |
445bf929 | 2142 | */ |
051a06d4 | 2143 | |
983ffd37 | 2144 | UV |
31f05a37 | 2145 | Perl__to_utf8_title_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, bool flags) |
983ffd37 | 2146 | { |
051a06d4 KW |
2147 | UV result; |
2148 | ||
2149 | PERL_ARGS_ASSERT__TO_UTF8_TITLE_FLAGS; | |
7918f24d | 2150 | |
780fcc9f KW |
2151 | if (flags) { |
2152 | /* Treat a UTF-8 locale as not being in locale at all */ | |
2153 | if (IN_UTF8_CTYPE_LOCALE) { | |
2154 | flags = FALSE; | |
2155 | } | |
2156 | else { | |
2157 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; | |
2158 | } | |
31f05a37 KW |
2159 | } |
2160 | ||
3a4c58c9 | 2161 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 KW |
2162 | if (flags) { |
2163 | result = toUPPER_LC(*p); | |
2164 | } | |
2165 | else { | |
81c6c7ce | 2166 | return _to_upper_title_latin1(*p, ustrp, lenp, 's'); |
051a06d4 | 2167 | } |
3a4c58c9 KW |
2168 | } |
2169 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 | 2170 | if (flags) { |
a62b247b | 2171 | U8 c = EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)); |
68067e4e | 2172 | result = toUPPER_LC(c); |
051a06d4 KW |
2173 | } |
2174 | else { | |
a62b247b | 2175 | return _to_upper_title_latin1(EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)), |
81c6c7ce | 2176 | ustrp, lenp, 's'); |
051a06d4 KW |
2177 | } |
2178 | } | |
4a4088c4 | 2179 | else { /* UTF-8, ord above 255 */ |
b9992569 | 2180 | result = CALL_TITLE_CASE(valid_utf8_to_uvchr(p, NULL), p, ustrp, lenp); |
051a06d4 KW |
2181 | |
2182 | if (flags) { | |
357aadde | 2183 | result = check_locale_boundary_crossing(p, result, ustrp, lenp); |
051a06d4 KW |
2184 | } |
2185 | return result; | |
2186 | } | |
2187 | ||
4a4088c4 | 2188 | /* Here, used locale rules. Convert back to UTF-8 */ |
051a06d4 KW |
2189 | if (UTF8_IS_INVARIANT(result)) { |
2190 | *ustrp = (U8) result; | |
2191 | *lenp = 1; | |
2192 | } | |
2193 | else { | |
62cb07ea KW |
2194 | *ustrp = UTF8_EIGHT_BIT_HI((U8) result); |
2195 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO((U8) result); | |
051a06d4 | 2196 | *lenp = 2; |
3a4c58c9 KW |
2197 | } |
2198 | ||
051a06d4 | 2199 | return result; |
a0ed51b3 LW |
2200 | } |
2201 | ||
d3e79532 | 2202 | /* |
87cea99e | 2203 | =for apidoc to_utf8_lower |
d3e79532 | 2204 | |
1f607577 | 2205 | Instead use L</toLOWER_utf8>. |
a1433954 | 2206 | |
d3e79532 JH |
2207 | =cut */ |
2208 | ||
051a06d4 | 2209 | /* Not currently externally documented, and subject to change: |
31f05a37 KW |
2210 | * <flags> is set iff iff the rules from the current underlying locale are to |
2211 | * be used. | |
2212 | */ | |
051a06d4 | 2213 | |
2104c8d9 | 2214 | UV |
31f05a37 | 2215 | Perl__to_utf8_lower_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, bool flags) |
a0ed51b3 | 2216 | { |
051a06d4 KW |
2217 | UV result; |
2218 | ||
051a06d4 | 2219 | PERL_ARGS_ASSERT__TO_UTF8_LOWER_FLAGS; |
7918f24d | 2220 | |
780fcc9f KW |
2221 | if (flags) { |
2222 | /* Treat a UTF-8 locale as not being in locale at all */ | |
2223 | if (IN_UTF8_CTYPE_LOCALE) { | |
2224 | flags = FALSE; | |
2225 | } | |
2226 | else { | |
2227 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; | |
2228 | } | |
31f05a37 KW |
2229 | } |
2230 | ||
968c5e6a | 2231 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 KW |
2232 | if (flags) { |
2233 | result = toLOWER_LC(*p); | |
2234 | } | |
2235 | else { | |
81c6c7ce | 2236 | return to_lower_latin1(*p, ustrp, lenp); |
051a06d4 | 2237 | } |
968c5e6a KW |
2238 | } |
2239 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 | 2240 | if (flags) { |
a62b247b | 2241 | U8 c = EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)); |
68067e4e | 2242 | result = toLOWER_LC(c); |
051a06d4 KW |
2243 | } |
2244 | else { | |
a62b247b | 2245 | return to_lower_latin1(EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)), |
81c6c7ce | 2246 | ustrp, lenp); |
051a06d4 | 2247 | } |
968c5e6a | 2248 | } |
4a4088c4 | 2249 | else { /* UTF-8, ord above 255 */ |
b9992569 | 2250 | result = CALL_LOWER_CASE(valid_utf8_to_uvchr(p, NULL), p, ustrp, lenp); |
051a06d4 KW |
2251 | |
2252 | if (flags) { | |
357aadde | 2253 | result = check_locale_boundary_crossing(p, result, ustrp, lenp); |
051a06d4 | 2254 | } |
968c5e6a | 2255 | |
051a06d4 KW |
2256 | return result; |
2257 | } | |
2258 | ||
4a4088c4 | 2259 | /* Here, used locale rules. Convert back to UTF-8 */ |
051a06d4 KW |
2260 | if (UTF8_IS_INVARIANT(result)) { |
2261 | *ustrp = (U8) result; | |
2262 | *lenp = 1; | |
2263 | } | |
2264 | else { | |
62cb07ea KW |
2265 | *ustrp = UTF8_EIGHT_BIT_HI((U8) result); |
2266 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO((U8) result); | |
051a06d4 KW |
2267 | *lenp = 2; |
2268 | } | |
2269 | ||
051a06d4 | 2270 | return result; |
b4e400f9 JH |
2271 | } |
2272 | ||
d3e79532 | 2273 | /* |
87cea99e | 2274 | =for apidoc to_utf8_fold |
d3e79532 | 2275 | |
1f607577 | 2276 | Instead use L</toFOLD_utf8>. |
a1433954 | 2277 | |
d3e79532 JH |
2278 | =cut */ |
2279 | ||
051a06d4 KW |
2280 | /* Not currently externally documented, and subject to change, |
2281 | * in <flags> | |
31f05a37 KW |
2282 | * bit FOLD_FLAGS_LOCALE is set iff the rules from the current underlying |
2283 | * locale are to be used. | |
051a06d4 KW |
2284 | * bit FOLD_FLAGS_FULL is set iff full case folds are to be used; |
2285 | * otherwise simple folds | |
a0270393 KW |
2286 | * bit FOLD_FLAGS_NOMIX_ASCII is set iff folds of non-ASCII to ASCII are |
2287 | * prohibited | |
445bf929 | 2288 | */ |
36bb2ab6 | 2289 | |
b4e400f9 | 2290 | UV |
445bf929 | 2291 | Perl__to_utf8_fold_flags(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp, U8 flags) |
b4e400f9 | 2292 | { |
051a06d4 KW |
2293 | UV result; |
2294 | ||
36bb2ab6 | 2295 | PERL_ARGS_ASSERT__TO_UTF8_FOLD_FLAGS; |
7918f24d | 2296 | |
a0270393 KW |
2297 | /* These are mutually exclusive */ |
2298 | assert (! ((flags & FOLD_FLAGS_LOCALE) && (flags & FOLD_FLAGS_NOMIX_ASCII))); | |
2299 | ||
50ba90ff KW |
2300 | assert(p != ustrp); /* Otherwise overwrites */ |
2301 | ||
780fcc9f KW |
2302 | if (flags & FOLD_FLAGS_LOCALE) { |
2303 | /* Treat a UTF-8 locale as not being in locale at all */ | |
2304 | if (IN_UTF8_CTYPE_LOCALE) { | |
2305 | flags &= ~FOLD_FLAGS_LOCALE; | |
2306 | } | |
2307 | else { | |
2308 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; | |
2309 | } | |
31f05a37 KW |
2310 | } |
2311 | ||
a1dde8de | 2312 | if (UTF8_IS_INVARIANT(*p)) { |
051a06d4 | 2313 | if (flags & FOLD_FLAGS_LOCALE) { |
d22b930b | 2314 | result = toFOLD_LC(*p); |
051a06d4 KW |
2315 | } |
2316 | else { | |
81c6c7ce | 2317 | return _to_fold_latin1(*p, ustrp, lenp, |
1ca267a5 | 2318 | flags & (FOLD_FLAGS_FULL | FOLD_FLAGS_NOMIX_ASCII)); |
051a06d4 | 2319 | } |
a1dde8de KW |
2320 | } |
2321 | else if UTF8_IS_DOWNGRADEABLE_START(*p) { | |
051a06d4 | 2322 | if (flags & FOLD_FLAGS_LOCALE) { |
a62b247b | 2323 | U8 c = EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)); |
68067e4e | 2324 | result = toFOLD_LC(c); |
051a06d4 KW |
2325 | } |
2326 | else { | |
a62b247b | 2327 | return _to_fold_latin1(EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1)), |
51910141 | 2328 | ustrp, lenp, |
1ca267a5 | 2329 | flags & (FOLD_FLAGS_FULL | FOLD_FLAGS_NOMIX_ASCII)); |
051a06d4 | 2330 | } |
a1dde8de | 2331 | } |
4a4088c4 | 2332 | else { /* UTF-8, ord above 255 */ |
b9992569 | 2333 | result = CALL_FOLD_CASE(valid_utf8_to_uvchr(p, NULL), p, ustrp, lenp, flags & FOLD_FLAGS_FULL); |
a1dde8de | 2334 | |
1ca267a5 KW |
2335 | if (flags & FOLD_FLAGS_LOCALE) { |
2336 | ||
76f2ffcd | 2337 | # define LONG_S_T LATIN_SMALL_LIGATURE_LONG_S_T_UTF8 |
0766489e KW |
2338 | const unsigned int long_s_t_len = sizeof(LONG_S_T) - 1; |
2339 | ||
2340 | # ifdef LATIN_CAPITAL_LETTER_SHARP_S_UTF8 | |
2341 | # define CAP_SHARP_S LATIN_CAPITAL_LETTER_SHARP_S_UTF8 | |
76f2ffcd KW |
2342 | |
2343 | const unsigned int cap_sharp_s_len = sizeof(CAP_SHARP_S) - 1; | |
76f2ffcd | 2344 | |
538e84ed KW |
2345 | /* Special case these two characters, as what normally gets |
2346 | * returned under locale doesn't work */ | |
76f2ffcd KW |
2347 | if (UTF8SKIP(p) == cap_sharp_s_len |
2348 | && memEQ((char *) p, CAP_SHARP_S, cap_sharp_s_len)) | |
1ca267a5 | 2349 | { |
ab0b796c KW |
2350 | /* diag_listed_as: Can't do %s("%s") on non-UTF-8 locale; resolved to "%s". */ |
2351 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
2352 | "Can't do fc(\"\\x{1E9E}\") on non-UTF-8 locale; " | |
2353 | "resolved to \"\\x{17F}\\x{17F}\"."); | |
1ca267a5 KW |
2354 | goto return_long_s; |
2355 | } | |
0766489e KW |
2356 | else |
2357 | #endif | |
2358 | if (UTF8SKIP(p) == long_s_t_len | |
76f2ffcd | 2359 | && memEQ((char *) p, LONG_S_T, long_s_t_len)) |
9fc2026f | 2360 | { |
ab0b796c KW |
2361 | /* diag_listed_as: Can't do %s("%s") on non-UTF-8 locale; resolved to "%s". */ |
2362 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
2363 | "Can't do fc(\"\\x{FB05}\") on non-UTF-8 locale; " | |
2364 | "resolved to \"\\x{FB06}\"."); | |
9fc2026f KW |
2365 | goto return_ligature_st; |
2366 | } | |
74894415 KW |
2367 | |
2368 | #if UNICODE_MAJOR_VERSION == 3 \ | |
2369 | && UNICODE_DOT_VERSION == 0 \ | |
2370 | && UNICODE_DOT_DOT_VERSION == 1 | |
2371 | # define DOTTED_I LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE_UTF8 | |
2372 | ||
2373 | /* And special case this on this Unicode version only, for the same | |
2374 | * reaons the other two are special cased. They would cross the | |
2375 | * 255/256 boundary which is forbidden under /l, and so the code | |
2376 | * wouldn't catch that they are equivalent (which they are only in | |
2377 | * this release) */ | |
2378 | else if (UTF8SKIP(p) == sizeof(DOTTED_I) - 1 | |
2379 | && memEQ((char *) p, DOTTED_I, sizeof(DOTTED_I) - 1)) | |
2380 | { | |
2381 | /* diag_listed_as: Can't do %s("%s") on non-UTF-8 locale; resolved to "%s". */ | |
2382 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
2383 | "Can't do fc(\"\\x{0130}\") on non-UTF-8 locale; " | |
2384 | "resolved to \"\\x{0131}\"."); | |
2385 | goto return_dotless_i; | |
2386 | } | |
2387 | #endif | |
2388 | ||
357aadde | 2389 | return check_locale_boundary_crossing(p, result, ustrp, lenp); |
051a06d4 | 2390 | } |
a0270393 KW |
2391 | else if (! (flags & FOLD_FLAGS_NOMIX_ASCII)) { |
2392 | return result; | |
2393 | } | |
2394 | else { | |
4a4088c4 | 2395 | /* This is called when changing the case of a UTF-8-encoded |
9fc2026f KW |
2396 | * character above the ASCII range, and the result should not |
2397 | * contain an ASCII character. */ | |
a0270393 KW |
2398 | |
2399 | UV original; /* To store the first code point of <p> */ | |
2400 | ||
2401 | /* Look at every character in the result; if any cross the | |
2402 | * boundary, the whole thing is disallowed */ | |
2403 | U8* s = ustrp; | |
2404 | U8* e = ustrp + *lenp; | |
2405 | while (s < e) { | |
2406 | if (isASCII(*s)) { | |
2407 | /* Crossed, have to return the original */ | |
2408 | original = valid_utf8_to_uvchr(p, lenp); | |
1ca267a5 | 2409 | |
9fc2026f | 2410 | /* But in these instances, there is an alternative we can |
1ca267a5 | 2411 | * return that is valid */ |
0766489e KW |
2412 | if (original == LATIN_SMALL_LETTER_SHARP_S |
2413 | #ifdef LATIN_CAPITAL_LETTER_SHARP_S /* not defined in early Unicode releases */ | |
2414 | || original == LATIN_CAPITAL_LETTER_SHARP_S | |
2415 | #endif | |
2416 | ) { | |
1ca267a5 KW |
2417 | goto return_long_s; |
2418 | } | |
9fc2026f KW |
2419 | else if (original == LATIN_SMALL_LIGATURE_LONG_S_T) { |
2420 | goto return_ligature_st; | |
2421 | } | |
74894415 KW |
2422 | #if UNICODE_MAJOR_VERSION == 3 \ |
2423 | && UNICODE_DOT_VERSION == 0 \ | |
2424 | && UNICODE_DOT_DOT_VERSION == 1 | |
2425 | ||
2426 | else if (original == LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE) { | |
2427 | goto return_dotless_i; | |
2428 | } | |
2429 | #endif | |
a0270393 KW |
2430 | Copy(p, ustrp, *lenp, char); |
2431 | return original; | |
2432 | } | |
2433 | s += UTF8SKIP(s); | |
2434 | } | |
051a06d4 | 2435 | |
a0270393 KW |
2436 | /* Here, no characters crossed, result is ok as-is */ |
2437 | return result; | |
2438 | } | |
051a06d4 KW |
2439 | } |
2440 | ||
4a4088c4 | 2441 | /* Here, used locale rules. Convert back to UTF-8 */ |
051a06d4 KW |
2442 | if (UTF8_IS_INVARIANT(result)) { |
2443 | *ustrp = (U8) result; | |
2444 | *lenp = 1; | |
2445 | } | |
2446 | else { | |
62cb07ea KW |
2447 | *ustrp = UTF8_EIGHT_BIT_HI((U8) result); |
2448 | *(ustrp + 1) = UTF8_EIGHT_BIT_LO((U8) result); | |
051a06d4 KW |
2449 | *lenp = 2; |
2450 | } | |
2451 | ||
051a06d4 | 2452 | return result; |
1ca267a5 KW |
2453 | |
2454 | return_long_s: | |
2455 | /* Certain folds to 'ss' are prohibited by the options, but they do allow | |
2456 | * folds to a string of two of these characters. By returning this | |
2457 | * instead, then, e.g., | |
2458 | * fc("\x{1E9E}") eq fc("\x{17F}\x{17F}") | |
2459 | * works. */ | |
2460 | ||
2461 | *lenp = 2 * sizeof(LATIN_SMALL_LETTER_LONG_S_UTF8) - 2; | |
2462 | Copy(LATIN_SMALL_LETTER_LONG_S_UTF8 LATIN_SMALL_LETTER_LONG_S_UTF8, | |
2463 | ustrp, *lenp, U8); | |
2464 | return LATIN_SMALL_LETTER_LONG_S; | |
9fc2026f KW |
2465 | |
2466 | return_ligature_st: | |
2467 | /* Two folds to 'st' are prohibited by the options; instead we pick one and | |
2468 | * have the other one fold to it */ | |
2469 | ||
2470 | *lenp = sizeof(LATIN_SMALL_LIGATURE_ST_UTF8) - 1; | |
2471 | Copy(LATIN_SMALL_LIGATURE_ST_UTF8, ustrp, *lenp, U8); | |
2472 | return LATIN_SMALL_LIGATURE_ST; | |
74894415 KW |
2473 | |
2474 | #if UNICODE_MAJOR_VERSION == 3 \ | |
2475 | && UNICODE_DOT_VERSION == 0 \ | |
2476 | && UNICODE_DOT_DOT_VERSION == 1 | |
2477 | ||
2478 | return_dotless_i: | |
2479 | *lenp = sizeof(LATIN_SMALL_LETTER_DOTLESS_I_UTF8) - 1; | |
2480 | Copy(LATIN_SMALL_LETTER_DOTLESS_I_UTF8, ustrp, *lenp, U8); | |
2481 | return LATIN_SMALL_LETTER_DOTLESS_I; | |
2482 | ||
2483 | #endif | |
2484 | ||
a0ed51b3 LW |
2485 | } |
2486 | ||
711a919c | 2487 | /* Note: |
f90a9a02 | 2488 | * Returns a "swash" which is a hash described in utf8.c:Perl_swash_fetch(). |
711a919c ST |
2489 | * C<pkg> is a pointer to a package name for SWASHNEW, should be "utf8". |
2490 | * For other parameters, see utf8::SWASHNEW in lib/utf8_heavy.pl. | |
2491 | */ | |
c4a5db0c | 2492 | |
a0ed51b3 | 2493 | SV* |
7fc63493 | 2494 | Perl_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits, I32 none) |
a0ed51b3 | 2495 | { |
c4a5db0c KW |
2496 | PERL_ARGS_ASSERT_SWASH_INIT; |
2497 | ||
2498 | /* Returns a copy of a swash initiated by the called function. This is the | |
2499 | * public interface, and returning a copy prevents others from doing | |
2500 | * mischief on the original */ | |
2501 | ||
5d3d13d1 | 2502 | return newSVsv(_core_swash_init(pkg, name, listsv, minbits, none, NULL, NULL)); |
c4a5db0c KW |
2503 | } |
2504 | ||
2505 | SV* | |
5d3d13d1 | 2506 | Perl__core_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits, I32 none, SV* invlist, U8* const flags_p) |
c4a5db0c | 2507 | { |
2c1f00b9 YO |
2508 | |
2509 | /*NOTE NOTE NOTE - If you want to use "return" in this routine you MUST | |
2510 | * use the following define */ | |
2511 | ||
2512 | #define CORE_SWASH_INIT_RETURN(x) \ | |
2513 | PL_curpm= old_PL_curpm; \ | |
2514 | return x | |
2515 | ||
c4a5db0c | 2516 | /* Initialize and return a swash, creating it if necessary. It does this |
87367d5f KW |
2517 | * by calling utf8_heavy.pl in the general case. The returned value may be |
2518 | * the swash's inversion list instead if the input parameters allow it. | |
2519 | * Which is returned should be immaterial to callers, as the only | |
923b6d4e KW |
2520 | * operations permitted on a swash, swash_fetch(), _get_swash_invlist(), |
2521 | * and swash_to_invlist() handle both these transparently. | |
c4a5db0c KW |
2522 | * |
2523 | * This interface should only be used by functions that won't destroy or | |
2524 | * adversely change the swash, as doing so affects all other uses of the | |
2525 | * swash in the program; the general public should use 'Perl_swash_init' | |
2526 | * instead. | |
2527 | * | |
2528 | * pkg is the name of the package that <name> should be in. | |
2529 | * name is the name of the swash to find. Typically it is a Unicode | |
2530 | * property name, including user-defined ones | |
2531 | * listsv is a string to initialize the swash with. It must be of the form | |
2532 | * documented as the subroutine return value in | |
2533 | * L<perlunicode/User-Defined Character Properties> | |
2534 | * minbits is the number of bits required to represent each data element. | |
2535 | * It is '1' for binary properties. | |
2536 | * none I (khw) do not understand this one, but it is used only in tr///. | |
9a53f6cf | 2537 | * invlist is an inversion list to initialize the swash with (or NULL) |
83199d38 KW |
2538 | * flags_p if non-NULL is the address of various input and output flag bits |
2539 | * to the routine, as follows: ('I' means is input to the routine; | |
2540 | * 'O' means output from the routine. Only flags marked O are | |
2541 | * meaningful on return.) | |
2542 | * _CORE_SWASH_INIT_USER_DEFINED_PROPERTY indicates if the swash | |
2543 | * came from a user-defined property. (I O) | |
5d3d13d1 KW |
2544 | * _CORE_SWASH_INIT_RETURN_IF_UNDEF indicates that instead of croaking |
2545 | * when the swash cannot be located, to simply return NULL. (I) | |
87367d5f KW |
2546 | * _CORE_SWASH_INIT_ACCEPT_INVLIST indicates that the caller will accept a |
2547 | * return of an inversion list instead of a swash hash if this routine | |
2548 | * thinks that would result in faster execution of swash_fetch() later | |
2549 | * on. (I) | |
9a53f6cf KW |
2550 | * |
2551 | * Thus there are three possible inputs to find the swash: <name>, | |
2552 | * <listsv>, and <invlist>. At least one must be specified. The result | |
2553 | * will be the union of the specified ones, although <listsv>'s various | |
aabbdbda KW |
2554 | * actions can intersect, etc. what <name> gives. To avoid going out to |
2555 | * disk at all, <invlist> should specify completely what the swash should | |
2556 | * have, and <listsv> should be &PL_sv_undef and <name> should be "". | |
9a53f6cf KW |
2557 | * |
2558 | * <invlist> is only valid for binary properties */ | |
c4a5db0c | 2559 | |
2c1f00b9 YO |
2560 | PMOP *old_PL_curpm= PL_curpm; /* save away the old PL_curpm */ |
2561 | ||
c4a5db0c | 2562 | SV* retval = &PL_sv_undef; |
83199d38 | 2563 | HV* swash_hv = NULL; |
87367d5f KW |
2564 | const int invlist_swash_boundary = |
2565 | (flags_p && *flags_p & _CORE_SWASH_INIT_ACCEPT_INVLIST) | |
2566 | ? 512 /* Based on some benchmarking, but not extensive, see commit | |
2567 | message */ | |
2568 | : -1; /* Never return just an inversion list */ | |
9a53f6cf KW |
2569 | |
2570 | assert(listsv != &PL_sv_undef || strNE(name, "") || invlist); | |
2571 | assert(! invlist || minbits == 1); | |
2572 | ||
2c1f00b9 YO |
2573 | PL_curpm= NULL; /* reset PL_curpm so that we dont get confused between the regex |
2574 | that triggered the swash init and the swash init perl logic itself. | |
2575 | See perl #122747 */ | |
2576 | ||
9a53f6cf KW |
2577 | /* If data was passed in to go out to utf8_heavy to find the swash of, do |
2578 | * so */ | |
2579 | if (listsv != &PL_sv_undef || strNE(name, "")) { | |
69794297 KW |
2580 | dSP; |
2581 | const size_t pkg_len = strlen(pkg); | |
2582 | const size_t name_len = strlen(name); | |
2583 | HV * const stash = gv_stashpvn(pkg, pkg_len, 0); | |
2584 | SV* errsv_save; | |
2585 | GV *method; | |
2586 | ||
2587 | PERL_ARGS_ASSERT__CORE_SWASH_INIT; | |
2588 | ||
2589 | PUSHSTACKi(PERLSI_MAGIC); | |
ce3b816e | 2590 | ENTER; |
69794297 | 2591 | SAVEHINTS(); |
2782061f | 2592 | save_re_context(); |
650f067c JL |
2593 | /* We might get here via a subroutine signature which uses a utf8 |
2594 | * parameter name, at which point PL_subname will have been set | |
2595 | * but not yet used. */ | |
2596 | save_item(PL_subname); | |
69794297 KW |
2597 | if (PL_parser && PL_parser->error_count) |
2598 | SAVEI8(PL_parser->error_count), PL_parser->error_count = 0; | |
2599 | method = gv_fetchmeth(stash, "SWASHNEW", 8, -1); | |
4a4088c4 | 2600 | if (!method) { /* demand load UTF-8 */ |
69794297 | 2601 | ENTER; |
db2c6cb3 FC |
2602 | if ((errsv_save = GvSV(PL_errgv))) SAVEFREESV(errsv_save); |
2603 | GvSV(PL_errgv) = NULL; | |
1a419e6b | 2604 | #ifndef NO_TAINT_SUPPORT |
69794297 KW |
2605 | /* It is assumed that callers of this routine are not passing in |
2606 | * any user derived data. */ | |
2782061f DM |
2607 | /* Need to do this after save_re_context() as it will set |
2608 | * PL_tainted to 1 while saving $1 etc (see the code after getrx: | |
2609 | * in Perl_magic_get). Even line to create errsv_save can turn on | |
2610 | * PL_tainted. */ | |
284167a5 SM |
2611 | SAVEBOOL(TAINT_get); |
2612 | TAINT_NOT; | |
2613 | #endif | |
69794297 KW |
2614 | Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvn(pkg,pkg_len), |
2615 | NULL); | |
eed484f9 | 2616 | { |
db2c6cb3 FC |
2617 | /* Not ERRSV, as there is no need to vivify a scalar we are |
2618 | about to discard. */ | |
2619 | SV * const errsv = GvSV(PL_errgv); | |
2620 | if (!SvTRUE(errsv)) { | |
2621 | GvSV(PL_errgv) = SvREFCNT_inc_simple(errsv_save); | |
2622 | SvREFCNT_dec(errsv); | |
2623 | } | |
eed484f9 | 2624 | } |
69794297 KW |
2625 | LEAVE; |
2626 | } | |
2627 | SPAGAIN; | |
2628 | PUSHMARK(SP); | |
2629 | EXTEND(SP,5); | |
2630 | mPUSHp(pkg, pkg_len); | |
2631 | mPUSHp(name, name_len); | |
2632 | PUSHs(listsv); | |
2633 | mPUSHi(minbits); | |
2634 | mPUSHi(none); | |
2635 | PUTBACK; | |
db2c6cb3 FC |
2636 | if ((errsv_save = GvSV(PL_errgv))) SAVEFREESV(errsv_save); |
2637 | GvSV(PL_errgv) = NULL; | |
69794297 KW |
2638 | /* If we already have a pointer to the method, no need to use |
2639 | * call_method() to repeat the lookup. */ | |
c41800a8 KW |
2640 | if (method |
2641 | ? call_sv(MUTABLE_SV(method), G_SCALAR) | |
69794297 KW |
2642 | : call_sv(newSVpvs_flags("SWASHNEW", SVs_TEMP), G_SCALAR | G_METHOD)) |
2643 | { | |
2644 | retval = *PL_stack_sp--; | |
2645 | SvREFCNT_inc(retval); | |
2646 | } | |
eed484f9 | 2647 | { |
db2c6cb3 FC |
2648 | /* Not ERRSV. See above. */ |
2649 | SV * const errsv = GvSV(PL_errgv); | |
2650 | if (!SvTRUE(errsv)) { | |
2651 | GvSV(PL_errgv) = SvREFCNT_inc_simple(errsv_save); | |
2652 | SvREFCNT_dec(errsv); | |
2653 | } | |
eed484f9 | 2654 | } |
ce3b816e | 2655 | LEAVE; |
69794297 KW |
2656 | POPSTACK; |
2657 | if (IN_PERL_COMPILETIME) { | |
2658 | CopHINTS_set(PL_curcop, PL_hints); | |
2659 | } | |
2660 | if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV) { | |
d95e4a00 | 2661 | if (SvPOK(retval)) { |
69794297 KW |
2662 | |
2663 | /* If caller wants to handle missing properties, let them */ | |
5d3d13d1 | 2664 | if (flags_p && *flags_p & _CORE_SWASH_INIT_RETURN_IF_UNDEF) { |
2c1f00b9 | 2665 | CORE_SWASH_INIT_RETURN(NULL); |
69794297 KW |
2666 | } |
2667 | Perl_croak(aTHX_ | |
2668 | "Can't find Unicode property definition \"%"SVf"\"", | |
2669 | SVfARG(retval)); | |
a25b5927 | 2670 | NOT_REACHED; /* NOTREACHED */ |
d95e4a00 | 2671 | } |
69794297 | 2672 | } |
9a53f6cf | 2673 | } /* End of calling the module to find the swash */ |
36eb48b4 | 2674 | |
83199d38 KW |
2675 | /* If this operation fetched a swash, and we will need it later, get it */ |
2676 | if (retval != &PL_sv_undef | |
2677 | && (minbits == 1 || (flags_p | |
2678 | && ! (*flags_p | |
2679 | & _CORE_SWASH_INIT_USER_DEFINED_PROPERTY)))) | |
2680 | { | |
2681 | swash_hv = MUTABLE_HV(SvRV(retval)); | |
2682 | ||
2683 | /* If we don't already know that there is a user-defined component to | |
2684 | * this swash, and the user has indicated they wish to know if there is | |
2685 | * one (by passing <flags_p>), find out */ | |
2686 | if (flags_p && ! (*flags_p & _CORE_SWASH_INIT_USER_DEFINED_PROPERTY)) { | |
2687 | SV** user_defined = hv_fetchs(swash_hv, "USER_DEFINED", FALSE); | |
2688 | if (user_defined && SvUV(*user_defined)) { | |
2689 | *flags_p |= _CORE_SWASH_INIT_USER_DEFINED_PROPERTY; | |
2690 | } | |
2691 | } | |
2692 | } | |
2693 | ||
36eb48b4 KW |
2694 | /* Make sure there is an inversion list for binary properties */ |
2695 | if (minbits == 1) { | |
2696 | SV** swash_invlistsvp = NULL; | |
2697 | SV* swash_invlist = NULL; | |
9a53f6cf | 2698 | bool invlist_in_swash_is_valid = FALSE; |
02c85471 FC |
2699 | bool swash_invlist_unclaimed = FALSE; /* whether swash_invlist has |
2700 | an unclaimed reference count */ | |
36eb48b4 | 2701 | |
9a53f6cf | 2702 | /* If this operation fetched a swash, get its already existing |
83199d38 | 2703 | * inversion list, or create one for it */ |
36eb48b4 | 2704 | |
83199d38 | 2705 | if (swash_hv) { |
5c9f4bd2 | 2706 | swash_invlistsvp = hv_fetchs(swash_hv, "V", FALSE); |
9a53f6cf KW |
2707 | if (swash_invlistsvp) { |
2708 | swash_invlist = *swash_invlistsvp; | |
2709 | invlist_in_swash_is_valid = TRUE; | |
2710 | } | |
2711 | else { | |
36eb48b4 | 2712 | swash_invlist = _swash_to_invlist(retval); |
02c85471 | 2713 | swash_invlist_unclaimed = TRUE; |
9a53f6cf KW |
2714 | } |
2715 | } | |
2716 | ||
2717 | /* If an inversion list was passed in, have to include it */ | |
2718 | if (invlist) { | |
2719 | ||
2720 | /* Any fetched swash will by now have an inversion list in it; | |
2721 | * otherwise <swash_invlist> will be NULL, indicating that we | |
2722 | * didn't fetch a swash */ | |
2723 | if (swash_invlist) { | |
2724 | ||
2725 | /* Add the passed-in inversion list, which invalidates the one | |
2726 | * already stored in the swash */ | |
2727 | invlist_in_swash_is_valid = FALSE; | |
2728 | _invlist_union(invlist, swash_invlist, &swash_invlist); | |
2729 | } | |
2730 | else { | |
2731 | ||
87367d5f KW |
2732 | /* Here, there is no swash already. Set up a minimal one, if |
2733 | * we are going to return a swash */ | |
2734 | if ((int) _invlist_len(invlist) > invlist_swash_boundary) { | |
971d486f | 2735 | swash_hv = newHV(); |
4aca0fe6 | 2736 | retval = newRV_noinc(MUTABLE_SV(swash_hv)); |
87367d5f | 2737 | } |
9a53f6cf KW |
2738 | swash_invlist = invlist; |
2739 | } | |
9a53f6cf KW |
2740 | } |
2741 | ||
2742 | /* Here, we have computed the union of all the passed-in data. It may | |
2743 | * be that there was an inversion list in the swash which didn't get | |
538e84ed | 2744 | * touched; otherwise save the computed one */ |
87367d5f KW |
2745 | if (! invlist_in_swash_is_valid |
2746 | && (int) _invlist_len(swash_invlist) > invlist_swash_boundary) | |
2747 | { | |
5c9f4bd2 | 2748 | if (! hv_stores(MUTABLE_HV(SvRV(retval)), "V", swash_invlist)) |
69794297 KW |
2749 | { |
2750 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
2751 | } | |
cc34d8c5 FC |
2752 | /* We just stole a reference count. */ |
2753 | if (swash_invlist_unclaimed) swash_invlist_unclaimed = FALSE; | |
2754 | else SvREFCNT_inc_simple_void_NN(swash_invlist); | |
9a53f6cf | 2755 | } |
87367d5f | 2756 | |
dbfdbd26 KW |
2757 | SvREADONLY_on(swash_invlist); |
2758 | ||
c41800a8 | 2759 | /* Use the inversion list stand-alone if small enough */ |
87367d5f KW |
2760 | if ((int) _invlist_len(swash_invlist) <= invlist_swash_boundary) { |
2761 | SvREFCNT_dec(retval); | |
02c85471 FC |
2762 | if (!swash_invlist_unclaimed) |
2763 | SvREFCNT_inc_simple_void_NN(swash_invlist); | |
2764 | retval = newRV_noinc(swash_invlist); | |
87367d5f | 2765 | } |
36eb48b4 KW |
2766 | } |
2767 | ||
2c1f00b9 YO |
2768 | CORE_SWASH_INIT_RETURN(retval); |
2769 | #undef CORE_SWASH_INIT_RETURN | |
a0ed51b3 LW |
2770 | } |
2771 | ||
035d37be JH |
2772 | |
2773 | /* This API is wrong for special case conversions since we may need to | |
2774 | * return several Unicode characters for a single Unicode character | |
2775 | * (see lib/unicore/SpecCase.txt) The SWASHGET in lib/utf8_heavy.pl is | |
2776 | * the lower-level routine, and it is similarly broken for returning | |
38684baa | 2777 | * multiple values. --jhi |
b9992569 | 2778 | * For those, you should use S__to_utf8_case() instead */ |
b0e3252e | 2779 | /* Now SWASHGET is recasted into S_swatch_get in this file. */ |
680c470c ST |
2780 | |
2781 | /* Note: | |
2782 | * Returns the value of property/mapping C<swash> for the first character | |
2783 | * of the string C<ptr>. If C<do_utf8> is true, the string C<ptr> is | |
4a4088c4 | 2784 | * assumed to be in well-formed UTF-8. If C<do_utf8> is false, the string C<ptr> |
3d0f8846 | 2785 | * is assumed to be in native 8-bit encoding. Caches the swatch in C<swash>. |
af2af982 KW |
2786 | * |
2787 | * A "swash" is a hash which contains initially the keys/values set up by | |
2788 | * SWASHNEW. The purpose is to be able to completely represent a Unicode | |
2789 | * property for all possible code points. Things are stored in a compact form | |
2790 | * (see utf8_heavy.pl) so that calculation is required to find the actual | |
2791 | * property value for a given code point. As code points are looked up, new | |
2792 | * key/value pairs are added to the hash, so that the calculation doesn't have | |
2793 | * to ever be re-done. Further, each calculation is done, not just for the | |
2794 | * desired one, but for a whole block of code points adjacent to that one. | |
2795 | * For binary properties on ASCII machines, the block is usually for 64 code | |
2796 | * points, starting with a code point evenly divisible by 64. Thus if the | |
2797 | * property value for code point 257 is requested, the code goes out and | |
2798 | * calculates the property values for all 64 code points between 256 and 319, | |
2799 | * and stores these as a single 64-bit long bit vector, called a "swatch", | |
2800 | * under the key for code point 256. The key is the UTF-8 encoding for code | |
2801 | * point 256, minus the final byte. Thus, if the length of the UTF-8 encoding | |
2802 | * for a code point is 13 bytes, the key will be 12 bytes long. If the value | |
2803 | * for code point 258 is then requested, this code realizes that it would be | |
2804 | * stored under the key for 256, and would find that value and extract the | |
2805 | * relevant bit, offset from 256. | |
2806 | * | |
2807 | * Non-binary properties are stored in as many bits as necessary to represent | |
2808 | * their values (32 currently, though the code is more general than that), not | |
fc273927 | 2809 | * as single bits, but the principle is the same: the value for each key is a |
af2af982 KW |
2810 | * vector that encompasses the property values for all code points whose UTF-8 |
2811 | * representations are represented by the key. That is, for all code points | |
2812 | * whose UTF-8 representations are length N bytes, and the key is the first N-1 | |
2813 | * bytes of that. | |
680c470c | 2814 | */ |
a0ed51b3 | 2815 | UV |
680c470c | 2816 | Perl_swash_fetch(pTHX_ SV *swash, const U8 *ptr, bool do_utf8) |
a0ed51b3 | 2817 | { |
ef8f7699 | 2818 | HV *const hv = MUTABLE_HV(SvRV(swash)); |
3568d838 JH |
2819 | U32 klen; |
2820 | U32 off; | |
9b56a019 | 2821 | STRLEN slen = 0; |
7d85a32c | 2822 | STRLEN needents; |
cfd0369c | 2823 | const U8 *tmps = NULL; |
979f2922 | 2824 | SV *swatch; |
08fb1ac5 | 2825 | const U8 c = *ptr; |
3568d838 | 2826 | |
7918f24d NC |
2827 | PERL_ARGS_ASSERT_SWASH_FETCH; |
2828 | ||
87367d5f KW |
2829 | /* If it really isn't a hash, it isn't really swash; must be an inversion |
2830 | * list */ | |
2831 | if (SvTYPE(hv) != SVt_PVHV) { | |
2832 | return _invlist_contains_cp((SV*)hv, | |
2833 | (do_utf8) | |
2834 | ? valid_utf8_to_uvchr(ptr, NULL) | |
2835 | : c); | |
2836 | } | |
2837 | ||
08fb1ac5 KW |
2838 | /* We store the values in a "swatch" which is a vec() value in a swash |
2839 | * hash. Code points 0-255 are a single vec() stored with key length | |
2840 | * (klen) 0. All other code points have a UTF-8 representation | |
2841 | * 0xAA..0xYY,0xZZ. A vec() is constructed containing all of them which | |
2842 | * share 0xAA..0xYY, which is the key in the hash to that vec. So the key | |
2843 | * length for them is the length of the encoded char - 1. ptr[klen] is the | |
2844 | * final byte in the sequence representing the character */ | |
2845 | if (!do_utf8 || UTF8_IS_INVARIANT(c)) { | |
2846 | klen = 0; | |
2847 | needents = 256; | |
2848 | off = c; | |
3568d838 | 2849 | } |
08fb1ac5 KW |
2850 | else if (UTF8_IS_DOWNGRADEABLE_START(c)) { |
2851 | klen = 0; | |
2852 | needents = 256; | |
a62b247b | 2853 | off = EIGHT_BIT_UTF8_TO_NATIVE(c, *(ptr + 1)); |
979f2922 ST |
2854 | } |
2855 | else { | |
08fb1ac5 KW |
2856 | klen = UTF8SKIP(ptr) - 1; |
2857 | ||
2858 | /* Each vec() stores 2**UTF_ACCUMULATION_SHIFT values. The offset into | |
2859 | * the vec is the final byte in the sequence. (In EBCDIC this is | |
2860 | * converted to I8 to get consecutive values.) To help you visualize | |
2861 | * all this: | |
2862 | * Straight 1047 After final byte | |
2863 | * UTF-8 UTF-EBCDIC I8 transform | |
2864 | * U+0400: \xD0\x80 \xB8\x41\x41 \xB8\x41\xA0 | |
2865 | * U+0401: \xD0\x81 \xB8\x41\x42 \xB8\x41\xA1 | |
2866 | * ... | |
2867 | * U+0409: \xD0\x89 \xB8\x41\x4A \xB8\x41\xA9 | |
2868 | * U+040A: \xD0\x8A \xB8\x41\x51 \xB8\x41\xAA | |
2869 | * ... | |
2870 | * U+0412: \xD0\x92 \xB8\x41\x59 \xB8\x41\xB2 | |
2871 | * U+0413: \xD0\x93 \xB8\x41\x62 \xB8\x41\xB3 | |
2872 | * ... | |
2873 | * U+041B: \xD0\x9B \xB8\x41\x6A \xB8\x41\xBB | |
2874 | * U+041C: \xD0\x9C \xB8\x41\x70 \xB8\x41\xBC | |
2875 | * ... | |
2876 | * U+041F: \xD0\x9F \xB8\x41\x73 \xB8\x41\xBF | |
2877 | * U+0420: \xD0\xA0 \xB8\x42\x41 \xB8\x42\x41 | |
2878 | * | |
2879 | * (There are no discontinuities in the elided (...) entries.) | |
2880 | * The UTF-8 key for these 33 code points is '\xD0' (which also is the | |
2881 | * key for the next 31, up through U+043F, whose UTF-8 final byte is | |
2882 | * \xBF). Thus in UTF-8, each key is for a vec() for 64 code points. | |
2883 | * The final UTF-8 byte, which ranges between \x80 and \xBF, is an | |
2884 | * index into the vec() swatch (after subtracting 0x80, which we | |
2885 | * actually do with an '&'). | |
2886 | * In UTF-EBCDIC, each key is for a 32 code point vec(). The first 32 | |
2887 | * code points above have key '\xB8\x41'. The final UTF-EBCDIC byte has | |
2888 | * dicontinuities which go away by transforming it into I8, and we | |
2889 | * effectively subtract 0xA0 to get the index. */ | |
979f2922 | 2890 | needents = (1 << UTF_ACCUMULATION_SHIFT); |
bc3632a8 | 2891 | off = NATIVE_UTF8_TO_I8(ptr[klen]) & UTF_CONTINUATION_MASK; |
979f2922 | 2892 | } |
7d85a32c | 2893 | |
a0ed51b3 | 2894 | /* |
4a4088c4 | 2895 | * This single-entry cache saves about 1/3 of the UTF-8 overhead in test |
a0ed51b3 LW |
2896 | * suite. (That is, only 7-8% overall over just a hash cache. Still, |
2897 | * it's nothing to sniff at.) Pity we usually come through at least | |
2898 | * two function calls to get here... | |
2899 | * | |
2900 | * NB: this code assumes that swatches are never modified, once generated! | |
2901 | */ | |
2902 | ||
3568d838 | 2903 | if (hv == PL_last_swash_hv && |
a0ed51b3 | 2904 | klen == PL_last_swash_klen && |
27da23d5 | 2905 | (!klen || memEQ((char *)ptr, (char *)PL_last_swash_key, klen)) ) |
a0ed51b3 LW |
2906 | { |
2907 | tmps = PL_last_swash_tmps; | |
2908 | slen = PL_last_swash_slen; | |
2909 | } | |
2910 | else { | |
2911 | /* Try our second-level swatch cache, kept in a hash. */ | |
e1ec3a88 | 2912 | SV** svp = hv_fetch(hv, (const char*)ptr, klen, FALSE); |
a0ed51b3 | 2913 | |
b0e3252e | 2914 | /* If not cached, generate it via swatch_get */ |
979f2922 | 2915 | if (!svp || !SvPOK(*svp) |
08fb1ac5 KW |
2916 | || !(tmps = (const U8*)SvPV_const(*svp, slen))) |
2917 | { | |
2918 | if (klen) { | |
2919 | const UV code_point = valid_utf8_to_uvchr(ptr, NULL); | |
2920 | swatch = swatch_get(swash, | |
2921 | code_point & ~((UV)needents - 1), | |
2922 | needents); | |
2923 | } | |
2924 | else { /* For the first 256 code points, the swatch has a key of | |
2925 | length 0 */ | |
2926 | swatch = swatch_get(swash, 0, needents); | |
2927 | } | |
979f2922 | 2928 | |
923e4eb5 | 2929 | if (IN_PERL_COMPILETIME) |
623e6609 | 2930 | CopHINTS_set(PL_curcop, PL_hints); |
a0ed51b3 | 2931 | |
979f2922 | 2932 | svp = hv_store(hv, (const char *)ptr, klen, swatch, 0); |
a0ed51b3 | 2933 | |
979f2922 ST |
2934 | if (!svp || !(tmps = (U8*)SvPV(*svp, slen)) |
2935 | || (slen << 3) < needents) | |
5637ef5b NC |
2936 | Perl_croak(aTHX_ "panic: swash_fetch got improper swatch, " |
2937 | "svp=%p, tmps=%p, slen=%"UVuf", needents=%"UVuf, | |
2938 | svp, tmps, (UV)slen, (UV)needents); | |
a0ed51b3 LW |
2939 | } |
2940 | ||
2941 | PL_last_swash_hv = hv; | |
16d8f38a | 2942 | assert(klen <= sizeof(PL_last_swash_key)); |
eac04b2e | 2943 | PL_last_swash_klen = (U8)klen; |
cfd0369c NC |
2944 | /* FIXME change interpvar.h? */ |
2945 | PL_last_swash_tmps = (U8 *) tmps; | |
a0ed51b3 LW |
2946 | PL_last_swash_slen = slen; |
2947 | if (klen) | |
2948 | Copy(ptr, PL_last_swash_key, klen, U8); | |
2949 | } | |
2950 | ||
9faf8d75 | 2951 | switch ((int)((slen << 3) / needents)) { |
a0ed51b3 | 2952 | case 1: |
e7aca353 | 2953 | return ((UV) tmps[off >> 3] & (1 << (off & 7))) != 0; |
a0ed51b3 | 2954 | case 8: |
e7aca353 | 2955 | return ((UV) tmps[off]); |
a0ed51b3 LW |
2956 | case 16: |
2957 | off <<= 1; | |
e7aca353 JH |
2958 | return |
2959 | ((UV) tmps[off ] << 8) + | |
2960 | ((UV) tmps[off + 1]); | |
a0ed51b3 LW |
2961 | case 32: |
2962 | off <<= 2; | |
e7aca353 JH |
2963 | return |
2964 | ((UV) tmps[off ] << 24) + | |
2965 | ((UV) tmps[off + 1] << 16) + | |
2966 | ((UV) tmps[off + 2] << 8) + | |
2967 | ((UV) tmps[off + 3]); | |
a0ed51b3 | 2968 | } |
5637ef5b NC |
2969 | Perl_croak(aTHX_ "panic: swash_fetch got swatch of unexpected bit width, " |
2970 | "slen=%"UVuf", needents=%"UVuf, (UV)slen, (UV)needents); | |
670f1322 | 2971 | NORETURN_FUNCTION_END; |
a0ed51b3 | 2972 | } |
2b9d42f0 | 2973 | |
319009ee KW |
2974 | /* Read a single line of the main body of the swash input text. These are of |
2975 | * the form: | |
2976 | * 0053 0056 0073 | |
2977 | * where each number is hex. The first two numbers form the minimum and | |
2978 | * maximum of a range, and the third is the value associated with the range. | |
2979 | * Not all swashes should have a third number | |
2980 | * | |
2981 | * On input: l points to the beginning of the line to be examined; it points | |
2982 | * to somewhere in the string of the whole input text, and is | |
2983 | * terminated by a \n or the null string terminator. | |
2984 | * lend points to the null terminator of that string | |
2985 | * wants_value is non-zero if the swash expects a third number | |
2986 | * typestr is the name of the swash's mapping, like 'ToLower' | |
2987 | * On output: *min, *max, and *val are set to the values read from the line. | |
2988 | * returns a pointer just beyond the line examined. If there was no | |
2989 | * valid min number on the line, returns lend+1 | |
2990 | */ | |
2991 | ||
2992 | STATIC U8* | |
2993 | S_swash_scan_list_line(pTHX_ U8* l, U8* const lend, UV* min, UV* max, UV* val, | |
2994 | const bool wants_value, const U8* const typestr) | |
2995 | { | |
2996 | const int typeto = typestr[0] == 'T' && typestr[1] == 'o'; | |
2997 | STRLEN numlen; /* Length of the number */ | |
02470786 KW |
2998 | I32 flags = PERL_SCAN_SILENT_ILLDIGIT |
2999 | | PERL_SCAN_DISALLOW_PREFIX | |
3000 | | PERL_SCAN_SILENT_NON_PORTABLE; | |
319009ee KW |
3001 | |
3002 | /* nl points to the next \n in the scan */ | |
3003 | U8* const nl = (U8*)memchr(l, '\n', lend - l); | |
3004 | ||
95543e92 KW |
3005 | PERL_ARGS_ASSERT_SWASH_SCAN_LIST_LINE; |
3006 | ||
319009ee KW |
3007 | /* Get the first number on the line: the range minimum */ |
3008 | numlen = lend - l; | |
3009 | *min = grok_hex((char *)l, &numlen, &flags, NULL); | |
c88850db | 3010 | *max = *min; /* So can never return without setting max */ |
319009ee KW |
3011 | if (numlen) /* If found a hex number, position past it */ |
3012 | l += numlen; | |
3013 | else if (nl) { /* Else, go handle next line, if any */ | |
3014 | return nl + 1; /* 1 is length of "\n" */ | |
3015 | } | |
3016 | else { /* Else, no next line */ | |
3017 | return lend + 1; /* to LIST's end at which \n is not found */ | |
3018 | } | |
3019 | ||
3020 | /* The max range value follows, separated by a BLANK */ | |
3021 | if (isBLANK(*l)) { | |
3022 | ++l; | |
02470786 KW |
3023 | flags = PERL_SCAN_SILENT_ILLDIGIT |
3024 | | PERL_SCAN_DISALLOW_PREFIX | |
3025 | | PERL_SCAN_SILENT_NON_PORTABLE; | |
319009ee KW |
3026 | numlen = lend - l; |
3027 | *max = grok_hex((char *)l, &numlen, &flags, NULL); | |
3028 | if (numlen) | |
3029 | l += numlen; | |
3030 | else /* If no value here, it is a single element range */ | |
3031 | *max = *min; | |
3032 | ||
3033 | /* Non-binary tables have a third entry: what the first element of the | |
24303724 | 3034 | * range maps to. The map for those currently read here is in hex */ |
319009ee KW |
3035 | if (wants_value) { |
3036 | if (isBLANK(*l)) { | |
3037 | ++l; | |
f2a7d0fc KW |
3038 | flags = PERL_SCAN_SILENT_ILLDIGIT |
3039 | | PERL_SCAN_DISALLOW_PREFIX | |
3040 | | PERL_SCAN_SILENT_NON_PORTABLE; | |
3041 | numlen = lend - l; | |
3042 | *val = grok_hex((char *)l, &numlen, &flags, NULL); | |
3043 | if (numlen) | |
3044 | l += numlen; | |
3045 | else | |
3046 | *val = 0; | |
319009ee KW |
3047 | } |
3048 | else { | |
3049 | *val = 0; | |
3050 | if (typeto) { | |
dcbac5bb | 3051 | /* diag_listed_as: To%s: illegal mapping '%s' */ |
319009ee KW |
3052 | Perl_croak(aTHX_ "%s: illegal mapping '%s'", |
3053 | typestr, l); | |
3054 | } | |
3055 | } | |
3056 | } | |
3057 | else | |
3058 | *val = 0; /* bits == 1, then any val should be ignored */ | |
3059 | } | |
3060 | else { /* Nothing following range min, should be single element with no | |
3061 | mapping expected */ | |
319009ee KW |
3062 | if (wants_value) { |
3063 | *val = 0; | |
3064 | if (typeto) { | |
dcbac5bb | 3065 | /* diag_listed_as: To%s: illegal mapping '%s' */ |
319009ee KW |
3066 | Perl_croak(aTHX_ "%s: illegal mapping '%s'", typestr, l); |
3067 | } | |
3068 | } | |
3069 | else | |
3070 | *val = 0; /* bits == 1, then val should be ignored */ | |
3071 | } | |
3072 | ||
3073 | /* Position to next line if any, or EOF */ | |
3074 | if (nl) | |
3075 | l = nl + 1; | |
3076 | else | |
3077 | l = lend; | |
3078 | ||
3079 | return l; | |
3080 | } | |
3081 | ||
979f2922 ST |
3082 | /* Note: |
3083 | * Returns a swatch (a bit vector string) for a code point sequence | |
3084 | * that starts from the value C<start> and comprises the number C<span>. | |
3085 | * A C<swash> must be an object created by SWASHNEW (see lib/utf8_heavy.pl). | |
3086 | * Should be used via swash_fetch, which will cache the swatch in C<swash>. | |
3087 | */ | |
3088 | STATIC SV* | |
b0e3252e | 3089 | S_swatch_get(pTHX_ SV* swash, UV start, UV span) |
979f2922 ST |
3090 | { |
3091 | SV *swatch; | |
77f9f126 | 3092 | U8 *l, *lend, *x, *xend, *s, *send; |
979f2922 | 3093 | STRLEN lcur, xcur, scur; |
ef8f7699 | 3094 | HV *const hv = MUTABLE_HV(SvRV(swash)); |
5c9f4bd2 | 3095 | SV** const invlistsvp = hv_fetchs(hv, "V", FALSE); |
36eb48b4 | 3096 | |
88d45d28 KW |
3097 | SV** listsvp = NULL; /* The string containing the main body of the table */ |
3098 | SV** extssvp = NULL; | |
3099 | SV** invert_it_svp = NULL; | |
3100 | U8* typestr = NULL; | |
786861f5 KW |
3101 | STRLEN bits; |
3102 | STRLEN octets; /* if bits == 1, then octets == 0 */ | |
3103 | UV none; | |
3104 | UV end = start + span; | |
972dd592 | 3105 | |
36eb48b4 | 3106 | if (invlistsvp == NULL) { |
786861f5 KW |
3107 | SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE); |
3108 | SV** const nonesvp = hv_fetchs(hv, "NONE", FALSE); | |
3109 | SV** const typesvp = hv_fetchs(hv, "TYPE", FALSE); | |
3110 | extssvp = hv_fetchs(hv, "EXTRAS", FALSE); | |
3111 | listsvp = hv_fetchs(hv, "LIST", FALSE); | |
3112 | invert_it_svp = hv_fetchs(hv, "INVERT_IT", FALSE); | |
3113 | ||
3114 | bits = SvUV(*bitssvp); | |
3115 | none = SvUV(*nonesvp); | |
3116 | typestr = (U8*)SvPV_nolen(*typesvp); | |
3117 | } | |
36eb48b4 KW |
3118 | else { |
3119 | bits = 1; | |
3120 | none = 0; | |
3121 | } | |
786861f5 | 3122 | octets = bits >> 3; /* if bits == 1, then octets == 0 */ |
979f2922 | 3123 | |
b0e3252e | 3124 | PERL_ARGS_ASSERT_SWATCH_GET; |
7918f24d | 3125 | |
979f2922 | 3126 | if (bits != 1 && bits != 8 && bits != 16 && bits != 32) { |
b0e3252e | 3127 | Perl_croak(aTHX_ "panic: swatch_get doesn't expect bits %"UVuf, |
660a4616 | 3128 | (UV)bits); |
979f2922 ST |
3129 | } |
3130 | ||
84ea5ef6 KW |
3131 | /* If overflowed, use the max possible */ |
3132 | if (end < start) { | |
3133 | end = UV_MAX; | |
3134 | span = end - start; | |
3135 | } | |
3136 | ||
979f2922 | 3137 | /* create and initialize $swatch */ |
979f2922 | 3138 | scur = octets ? (span * octets) : (span + 7) / 8; |
e524fe40 NC |
3139 | swatch = newSV(scur); |
3140 | SvPOK_on(swatch); | |
979f2922 ST |
3141 | s = (U8*)SvPVX(swatch); |
3142 | if (octets && none) { | |
0bd48802 | 3143 | const U8* const e = s + scur; |
979f2922 ST |
3144 | while (s < e) { |
3145 | if (bits == 8) | |
3146 | *s++ = (U8)(none & 0xff); | |
3147 | else if (bits == 16) { | |
3148 | *s++ = (U8)((none >> 8) & 0xff); | |
3149 | *s++ = (U8)( none & 0xff); | |
3150 | } | |
3151 | else if (bits == 32) { | |
3152 | *s++ = (U8)((none >> 24) & 0xff); | |
3153 | *s++ = (U8)((none >> 16) & 0xff); | |
3154 | *s++ = (U8)((none >> 8) & 0xff); | |
3155 | *s++ = (U8)( none & 0xff); | |
3156 | } | |
3157 | } | |
3158 | *s = '\0'; | |
3159 | } | |
3160 | else { | |
3161 | (void)memzero((U8*)s, scur + 1); | |
3162 | } | |
3163 | SvCUR_set(swatch, scur); | |
3164 | s = (U8*)SvPVX(swatch); | |
3165 | ||
36eb48b4 KW |
3166 | if (invlistsvp) { /* If has an inversion list set up use that */ |
3167 | _invlist_populate_swatch(*invlistsvp, start, end, s); | |
3168 | return swatch; | |
3169 | } | |
3170 | ||
3171 | /* read $swash->{LIST} */ | |
979f2922 ST |
3172 | l = (U8*)SvPV(*listsvp, lcur); |
3173 | lend = l + lcur; | |
3174 | while (l < lend) { | |
8ed25d53 | 3175 | UV min, max, val, upper; |
95543e92 KW |
3176 | l = swash_scan_list_line(l, lend, &min, &max, &val, |
3177 | cBOOL(octets), typestr); | |
319009ee | 3178 | if (l > lend) { |
979f2922 ST |
3179 | break; |
3180 | } | |
3181 | ||
972dd592 | 3182 | /* If looking for something beyond this range, go try the next one */ |
979f2922 ST |
3183 | if (max < start) |
3184 | continue; | |
3185 | ||
8ed25d53 KW |
3186 | /* <end> is generally 1 beyond where we want to set things, but at the |
3187 | * platform's infinity, where we can't go any higher, we want to | |
3188 | * include the code point at <end> */ | |
3189 | upper = (max < end) | |
3190 | ? max | |
3191 | : (max != UV_MAX || end != UV_MAX) | |
3192 | ? end - 1 | |
3193 | : end; | |
3194 | ||
979f2922 | 3195 | if (octets) { |
35da51f7 | 3196 | UV key; |
979f2922 ST |
3197 | if (min < start) { |
3198 | if (!none || val < none) { | |
3199 | val += start - min; | |
3200 | } | |
3201 | min = start; | |
3202 | } | |
8ed25d53 | 3203 | for (key = min; key <= upper; key++) { |
979f2922 | 3204 | STRLEN offset; |
979f2922 ST |
3205 | /* offset must be non-negative (start <= min <= key < end) */ |
3206 | offset = octets * (key - start); | |
3207 | if (bits == 8) | |
3208 | s[offset] = (U8)(val & 0xff); | |
3209 | else if (bits == 16) { | |
3210 | s[offset ] = (U8)((val >> 8) & 0xff); | |
3211 | s[offset + 1] = (U8)( val & 0xff); | |
3212 | } | |
3213 | else if (bits == 32) { | |
3214 | s[offset ] = (U8)((val >> 24) & 0xff); | |
3215 | s[offset + 1] = (U8)((val >> 16) & 0xff); | |
3216 | s[offset + 2] = (U8)((val >> 8) & 0xff); | |
3217 | s[offset + 3] = (U8)( val & 0xff); | |
3218 | } | |
3219 | ||
3220 | if (!none || val < none) | |
3221 | ++val; | |
3222 | } | |
3223 | } | |
711a919c | 3224 | else { /* bits == 1, then val should be ignored */ |
35da51f7 | 3225 | UV key; |
979f2922 ST |
3226 | if (min < start) |
3227 | min = start; | |
6cb05c12 | 3228 | |
8ed25d53 | 3229 | for (key = min; key <= upper; key++) { |
0bd48802 | 3230 | const STRLEN offset = (STRLEN)(key - start); |
979f2922 ST |
3231 | s[offset >> 3] |= 1 << (offset & 7); |
3232 | } | |
3233 | } | |
3234 | } /* while */ | |
979f2922 | 3235 | |
9479a769 | 3236 | /* Invert if the data says it should be. Assumes that bits == 1 */ |
77f9f126 | 3237 | if (invert_it_svp && SvUV(*invert_it_svp)) { |
0bda3001 KW |
3238 | |
3239 | /* Unicode properties should come with all bits above PERL_UNICODE_MAX | |
3240 | * be 0, and their inversion should also be 0, as we don't succeed any | |
3241 | * Unicode property matches for non-Unicode code points */ | |
3242 | if (start <= PERL_UNICODE_MAX) { | |
3243 | ||
3244 | /* The code below assumes that we never cross the | |
3245 | * Unicode/above-Unicode boundary in a range, as otherwise we would | |
3246 | * have to figure out where to stop flipping the bits. Since this | |
3247 | * boundary is divisible by a large power of 2, and swatches comes | |
3248 | * in small powers of 2, this should be a valid assumption */ | |
3249 | assert(start + span - 1 <= PERL_UNICODE_MAX); | |
3250 | ||
507a8485 KW |
3251 | send = s + scur; |
3252 | while (s < send) { | |
3253 | *s = ~(*s); | |
3254 | s++; | |
3255 | } | |
0bda3001 | 3256 | } |
77f9f126 KW |
3257 | } |
3258 | ||
d73c39c5 KW |
3259 | /* read $swash->{EXTRAS} |
3260 | * This code also copied to swash_to_invlist() below */ | |
979f2922 ST |
3261 | x = (U8*)SvPV(*extssvp, xcur); |
3262 | xend = x + xcur; | |
3263 | while (x < xend) { | |
3264 | STRLEN namelen; | |
3265 | U8 *namestr; | |
3266 | SV** othersvp; | |
3267 | HV* otherhv; | |
3268 | STRLEN otherbits; | |
3269 | SV **otherbitssvp, *other; | |
711a919c | 3270 | U8 *s, *o, *nl; |
979f2922 ST |
3271 | STRLEN slen, olen; |
3272 | ||
35da51f7 | 3273 | const U8 opc = *x++; |
979f2922 ST |
3274 | if (opc == '\n') |
3275 | continue; | |
3276 | ||
3277 | nl = (U8*)memchr(x, '\n', xend - x); | |
3278 | ||
3279 | if (opc != '-' && opc != '+' && opc != '!' && opc != '&') { | |
3280 | if (nl) { | |
3281 | x = nl + 1; /* 1 is length of "\n" */ | |
3282 | continue; | |
3283 | } | |
3284 | else { | |
3285 | x = xend; /* to EXTRAS' end at which \n is not found */ | |
3286 | break; | |
3287 | } | |
3288 | } | |
3289 | ||
3290 | namestr = x; | |
3291 | if (nl) { | |
3292 | namelen = nl - namestr; | |
3293 | x = nl + 1; | |
3294 | } | |
3295 | else { | |
3296 | namelen = xend - namestr; | |
3297 | x = xend; | |
3298 | } | |
3299 | ||
3300 | othersvp = hv_fetch(hv, (char *)namestr, namelen, FALSE); | |
ef8f7699 | 3301 | otherhv = MUTABLE_HV(SvRV(*othersvp)); |
017a3ce5 | 3302 | otherbitssvp = hv_fetchs(otherhv, "BITS", FALSE); |
979f2922 ST |
3303 | otherbits = (STRLEN)SvUV(*otherbitssvp); |
3304 | if (bits < otherbits) | |
5637ef5b NC |
3305 | Perl_croak(aTHX_ "panic: swatch_get found swatch size mismatch, " |
3306 | "bits=%"UVuf", otherbits=%"UVuf, (UV)bits, (UV)otherbits); | |
979f2922 ST |
3307 | |
3308 | /* The "other" swatch must be destroyed after. */ | |
b0e3252e | 3309 | other = swatch_get(*othersvp, start, span); |
979f2922 ST |
3310 | o = (U8*)SvPV(other, olen); |
3311 | ||
3312 | if (!olen) | |
b0e3252e | 3313 | Perl_croak(aTHX_ "panic: swatch_get got improper swatch"); |
979f2922 ST |
3314 | |
3315 | s = (U8*)SvPV(swatch, slen); | |
3316 | if (bits == 1 && otherbits == 1) { | |
3317 | if (slen != olen) | |
5637ef5b NC |
3318 | Perl_croak(aTHX_ "panic: swatch_get found swatch length " |
3319 | "mismatch, slen=%"UVuf", olen=%"UVuf, | |
3320 | (UV)slen, (UV)olen); | |
979f2922 ST |
3321 | |
3322 | switch (opc) { | |
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 | case '&': | |
3336 | while (slen--) | |
3337 | *s++ &= *o++; | |
3338 | break; | |
3339 | default: | |
3340 | break; | |
3341 | } | |
3342 | } | |
711a919c | 3343 | else { |
979f2922 ST |
3344 | STRLEN otheroctets = otherbits >> 3; |
3345 | STRLEN offset = 0; | |
35da51f7 | 3346 | U8* const send = s + slen; |
979f2922 ST |
3347 | |
3348 | while (s < send) { | |
3349 | UV otherval = 0; | |
3350 | ||
3351 | if (otherbits == 1) { | |
3352 | otherval = (o[offset >> 3] >> (offset & 7)) & 1; | |
3353 | ++offset; | |
3354 | } | |
3355 | else { | |
3356 | STRLEN vlen = otheroctets; | |
3357 | otherval = *o++; | |
3358 | while (--vlen) { | |
3359 | otherval <<= 8; | |
3360 | otherval |= *o++; | |
3361 | } | |
3362 | } | |
3363 | ||
711a919c | 3364 | if (opc == '+' && otherval) |
6f207bd3 | 3365 | NOOP; /* replace with otherval */ |
979f2922 ST |
3366 | else if (opc == '!' && !otherval) |
3367 | otherval = 1; | |
3368 | else if (opc == '-' && otherval) | |
3369 | otherval = 0; | |
3370 | else if (opc == '&' && !otherval) | |
3371 | otherval = 0; | |
3372 | else { | |
711a919c | 3373 | s += octets; /* no replacement */ |
979f2922 ST |
3374 | continue; |
3375 | } | |
3376 | ||
3377 | if (bits == 8) | |
3378 | *s++ = (U8)( otherval & 0xff); | |
3379 | else if (bits == 16) { | |
3380 | *s++ = (U8)((otherval >> 8) & 0xff); | |
3381 | *s++ = (U8)( otherval & 0xff); | |
3382 | } | |
3383 | else if (bits == 32) { | |
3384 | *s++ = (U8)((otherval >> 24) & 0xff); | |
3385 | *s++ = (U8)((otherval >> 16) & 0xff); | |
3386 | *s++ = (U8)((otherval >> 8) & 0xff); | |
3387 | *s++ = (U8)( otherval & 0xff); | |
3388 | } | |
3389 | } | |
3390 | } | |
3391 | sv_free(other); /* through with it! */ | |
3392 | } /* while */ | |
3393 | return swatch; | |
3394 | } | |
3395 | ||
064c021d | 3396 | HV* |
4c2e1131 | 3397 | Perl__swash_inversion_hash(pTHX_ SV* const swash) |
064c021d KW |
3398 | { |
3399 | ||
79a2a0e8 | 3400 | /* Subject to change or removal. For use only in regcomp.c and regexec.c |
5662e334 KW |
3401 | * Can't be used on a property that is subject to user override, as it |
3402 | * relies on the value of SPECIALS in the swash which would be set by | |
3403 | * utf8_heavy.pl to the hash in the non-overriden file, and hence is not set | |
3404 | * for overridden properties | |
064c021d KW |
3405 | * |
3406 | * Returns a hash which is the inversion and closure of a swash mapping. | |
3407 | * For example, consider the input lines: | |
3408 | * 004B 006B | |
3409 | * 004C 006C | |
3410 | * 212A 006B | |
3411 | * | |
4a4088c4 | 3412 | * The returned hash would have two keys, the UTF-8 for 006B and the UTF-8 for |
064c021d | 3413 | * 006C. The value for each key is an array. For 006C, the array would |
4a4088c4 KW |
3414 | * have two elements, the UTF-8 for itself, and for 004C. For 006B, there |
3415 | * would be three elements in its array, the UTF-8 for 006B, 004B and 212A. | |
064c021d | 3416 | * |
538e84ed KW |
3417 | * Note that there are no elements in the hash for 004B, 004C, 212A. The |
3418 | * keys are only code points that are folded-to, so it isn't a full closure. | |
3419 | * | |
064c021d KW |
3420 | * Essentially, for any code point, it gives all the code points that map to |
3421 | * it, or the list of 'froms' for that point. | |
3422 | * | |
5662e334 KW |
3423 | * Currently it ignores any additions or deletions from other swashes, |
3424 | * looking at just the main body of the swash, and if there are SPECIALS | |
3425 | * in the swash, at that hash | |
3426 | * | |
3427 | * The specials hash can be extra code points, and most likely consists of | |
3428 | * maps from single code points to multiple ones (each expressed as a string | |
4a4088c4 | 3429 | * of UTF-8 characters). This function currently returns only 1-1 mappings. |
5662e334 KW |
3430 | * However consider this possible input in the specials hash: |
3431 | * "\xEF\xAC\x85" => "\x{0073}\x{0074}", # U+FB05 => 0073 0074 | |
3432 | * "\xEF\xAC\x86" => "\x{0073}\x{0074}", # U+FB06 => 0073 0074 | |
3433 | * | |
3434 | * Both FB05 and FB06 map to the same multi-char sequence, which we don't | |
3435 | * currently handle. But it also means that FB05 and FB06 are equivalent in | |
3436 | * a 1-1 mapping which we should handle, and this relationship may not be in | |
3437 | * the main table. Therefore this function examines all the multi-char | |
74894415 KW |
3438 | * sequences and adds the 1-1 mappings that come out of that. |
3439 | * | |
3440 | * XXX This function was originally intended to be multipurpose, but its | |
3441 | * only use is quite likely to remain for constructing the inversion of | |
3442 | * the CaseFolding (//i) property. If it were more general purpose for | |
3443 | * regex patterns, it would have to do the FB05/FB06 game for simple folds, | |
3444 | * because certain folds are prohibited under /iaa and /il. As an example, | |
3445 | * in Unicode 3.0.1 both U+0130 and U+0131 fold to 'i', and hence are both | |
3446 | * equivalent under /i. But under /iaa and /il, the folds to 'i' are | |
3447 | * prohibited, so we would not figure out that they fold to each other. | |
3448 | * Code could be written to automatically figure this out, similar to the | |
3449 | * code that does this for multi-character folds, but this is the only case | |
3450 | * where something like this is ever likely to happen, as all the single | |
7ee537e6 | 3451 | * char folds to the 0-255 range are now quite settled. Instead there is a |
74894415 KW |
3452 | * little special code that is compiled only for this Unicode version. This |
3453 | * is smaller and didn't require much coding time to do. But this makes | |
3454 | * this routine strongly tied to being used just for CaseFolding. If ever | |
3455 | * it should be generalized, this would have to be fixed */ | |
064c021d KW |
3456 | |
3457 | U8 *l, *lend; | |
3458 | STRLEN lcur; | |
3459 | HV *const hv = MUTABLE_HV(SvRV(swash)); | |
3460 | ||
923b6d4e KW |
3461 | /* The string containing the main body of the table. This will have its |
3462 | * assertion fail if the swash has been converted to its inversion list */ | |
064c021d KW |
3463 | SV** const listsvp = hv_fetchs(hv, "LIST", FALSE); |
3464 | ||
3465 | SV** const typesvp = hv_fetchs(hv, "TYPE", FALSE); | |
3466 | SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE); | |
3467 | SV** const nonesvp = hv_fetchs(hv, "NONE", FALSE); | |
3468 | /*SV** const extssvp = hv_fetchs(hv, "EXTRAS", FALSE);*/ | |
3469 | const U8* const typestr = (U8*)SvPV_nolen(*typesvp); | |
3470 | const STRLEN bits = SvUV(*bitssvp); | |
3471 | const STRLEN octets = bits >> 3; /* if bits == 1, then octets == 0 */ | |
3472 | const UV none = SvUV(*nonesvp); | |
5662e334 | 3473 | SV **specials_p = hv_fetchs(hv, "SPECIALS", 0); |
064c021d KW |
3474 | |
3475 | HV* ret = newHV(); | |
3476 | ||
3477 | PERL_ARGS_ASSERT__SWASH_INVERSION_HASH; | |
3478 | ||
3479 | /* Must have at least 8 bits to get the mappings */ | |
3480 | if (bits != 8 && bits != 16 && bits != 32) { | |
3481 | Perl_croak(aTHX_ "panic: swash_inversion_hash doesn't expect bits %"UVuf, | |
3482 | (UV)bits); | |
3483 | } | |
3484 | ||
5662e334 KW |
3485 | if (specials_p) { /* It might be "special" (sometimes, but not always, a |
3486 | mapping to more than one character */ | |
3487 | ||
3488 | /* Construct an inverse mapping hash for the specials */ | |
3489 | HV * const specials_hv = MUTABLE_HV(SvRV(*specials_p)); | |
3490 | HV * specials_inverse = newHV(); | |
3491 | char *char_from; /* the lhs of the map */ | |
3492 | I32 from_len; /* its byte length */ | |
3493 | char *char_to; /* the rhs of the map */ | |
3494 | I32 to_len; /* its byte length */ | |
3495 | SV *sv_to; /* and in a sv */ | |
3496 | AV* from_list; /* list of things that map to each 'to' */ | |
3497 | ||
3498 | hv_iterinit(specials_hv); | |
3499 | ||
4a4088c4 KW |
3500 | /* The keys are the characters (in UTF-8) that map to the corresponding |
3501 | * UTF-8 string value. Iterate through the list creating the inverse | |
5662e334 KW |
3502 | * list. */ |
3503 | while ((sv_to = hv_iternextsv(specials_hv, &char_from, &from_len))) { | |
3504 | SV** listp; | |
3505 | if (! SvPOK(sv_to)) { | |
5637ef5b NC |
3506 | Perl_croak(aTHX_ "panic: value returned from hv_iternextsv() " |
3507 | "unexpectedly is not a string, flags=%lu", | |
3508 | (unsigned long)SvFLAGS(sv_to)); | |
5662e334 | 3509 | } |
4b88fb76 | 3510 | /*DEBUG_U(PerlIO_printf(Perl_debug_log, "Found mapping from %"UVXf", First char of to is %"UVXf"\n", valid_utf8_to_uvchr((U8*) char_from, 0), valid_utf8_to_uvchr((U8*) SvPVX(sv_to), 0)));*/ |
5662e334 KW |
3511 | |
3512 | /* Each key in the inverse list is a mapped-to value, and the key's | |
4a4088c4 | 3513 | * hash value is a list of the strings (each in UTF-8) that map to |
5662e334 KW |
3514 | * it. Those strings are all one character long */ |
3515 | if ((listp = hv_fetch(specials_inverse, | |
3516 | SvPVX(sv_to), | |
3517 | SvCUR(sv_to), 0))) | |
3518 | { | |
3519 | from_list = (AV*) *listp; | |
3520 | } | |
3521 | else { /* No entry yet for it: create one */ | |
3522 | from_list = newAV(); | |
3523 | if (! hv_store(specials_inverse, | |
3524 | SvPVX(sv_to), | |
3525 | SvCUR(sv_to), | |
3526 | (SV*) from_list, 0)) | |
3527 | { | |
3528 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
3529 | } | |
3530 | } | |
3531 | ||
3532 | /* Here have the list associated with this 'to' (perhaps newly | |
3533 | * created and empty). Just add to it. Note that we ASSUME that | |
3534 | * the input is guaranteed to not have duplications, so we don't | |
3535 | * check for that. Duplications just slow down execution time. */ | |
3536 | av_push(from_list, newSVpvn_utf8(char_from, from_len, TRUE)); | |
3537 | } | |
3538 | ||
3539 | /* Here, 'specials_inverse' contains the inverse mapping. Go through | |
3540 | * it looking for cases like the FB05/FB06 examples above. There would | |
3541 | * be an entry in the hash like | |
3542 | * 'st' => [ FB05, FB06 ] | |
3543 | * In this example we will create two lists that get stored in the | |
3544 | * returned hash, 'ret': | |
3545 | * FB05 => [ FB05, FB06 ] | |
3546 | * FB06 => [ FB05, FB06 ] | |
3547 | * | |
3548 | * Note that there is nothing to do if the array only has one element. | |
3549 | * (In the normal 1-1 case handled below, we don't have to worry about | |
3550 | * two lists, as everything gets tied to the single list that is | |
3551 | * generated for the single character 'to'. But here, we are omitting | |
3552 | * that list, ('st' in the example), so must have multiple lists.) */ | |
3553 | while ((from_list = (AV *) hv_iternextsv(specials_inverse, | |
3554 | &char_to, &to_len))) | |
3555 | { | |
7e13d567 | 3556 | if (av_tindex_nomg(from_list) > 0) { |
c70927a6 | 3557 | SSize_t i; |
5662e334 KW |
3558 | |
3559 | /* We iterate over all combinations of i,j to place each code | |
3560 | * point on each list */ | |
7e13d567 | 3561 | for (i = 0; i <= av_tindex_nomg(from_list); i++) { |
c70927a6 | 3562 | SSize_t j; |
5662e334 KW |
3563 | AV* i_list = newAV(); |
3564 | SV** entryp = av_fetch(from_list, i, FALSE); | |
3565 | if (entryp == NULL) { | |
3566 | Perl_croak(aTHX_ "panic: av_fetch() unexpectedly failed"); | |
3567 | } | |
3568 | if (hv_fetch(ret, SvPVX(*entryp), SvCUR(*entryp), FALSE)) { | |
3569 | Perl_croak(aTHX_ "panic: unexpected entry for %s", SvPVX(*entryp)); | |
3570 | } | |
3571 | if (! hv_store(ret, SvPVX(*entryp), SvCUR(*entryp), | |
3572 | (SV*) i_list, FALSE)) | |
3573 | { | |
3574 | Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed"); | |
3575 | } | |
3576 | ||
538e84ed | 3577 | /* For DEBUG_U: UV u = valid_utf8_to_uvchr((U8*) SvPVX(*entryp), 0);*/ |
7e13d567 | 3578 | for (j = 0; j <= av_tindex_nomg(from_list); j++) { |
5662e334 KW |
3579 | entryp = av_fetch(from_list, j, FALSE); |
3580 | if (entryp == NULL) { | |
3581 | Perl_croak(aTHX_ "panic: av_fetch() unexpectedly failed"); | |
3582 | } | |
3583 | ||
3584 | /* When i==j this adds itself to the list */ | |
4b88fb76 KW |
3585 | av_push(i_list, newSVuv(utf8_to_uvchr_buf( |
3586 | (U8*) SvPVX(*entryp), | |
3587 | (U8*) SvPVX(*entryp) + SvCUR(*entryp), | |
3588 | 0))); | |
4637d003 | 3589 | /*DEBUG_U(PerlIO_printf(Perl_debug_log, "%s: %d: Adding %"UVXf" to list for %"UVXf"\n", __FILE__, __LINE__, valid_utf8_to_uvchr((U8*) SvPVX(*entryp), 0), u));*/ |
5662e334 KW |
3590 | } |
3591 | } | |
3592 | } | |
3593 | } | |
3594 | SvREFCNT_dec(specials_inverse); /* done with it */ | |
3595 | } /* End of specials */ | |
3596 | ||
064c021d | 3597 | /* read $swash->{LIST} */ |
74894415 KW |
3598 | |
3599 | #if UNICODE_MAJOR_VERSION == 3 \ | |
3600 | && UNICODE_DOT_VERSION == 0 \ | |
3601 | && UNICODE_DOT_DOT_VERSION == 1 | |
3602 | ||
3603 | /* For this version only U+130 and U+131 are equivalent under qr//i. Add a | |
3604 | * rule so that things work under /iaa and /il */ | |
3605 | ||
3606 | SV * mod_listsv = sv_mortalcopy(*listsvp); | |
3607 | sv_catpv(mod_listsv, "130\t130\t131\n"); | |
3608 | l = (U8*)SvPV(mod_listsv, lcur); | |
3609 | ||
3610 | #else | |
3611 | ||
064c021d | 3612 | l = (U8*)SvPV(*listsvp, lcur); |
74894415 KW |
3613 | |
3614 | #endif | |
3615 | ||
064c021d KW |
3616 | lend = l + lcur; |
3617 | ||
3618 | /* Go through each input line */ | |
3619 | while (l < lend) { | |
3620 | UV min, max, val; | |
3621 | UV inverse; | |
95543e92 KW |
3622 | l = swash_scan_list_line(l, lend, &min, &max, &val, |
3623 | cBOOL(octets), typestr); | |
064c021d KW |
3624 | if (l > lend) { |
3625 | break; | |
3626 | } | |
3627 | ||
3628 | /* Each element in the range is to be inverted */ | |
3629 | for (inverse = min; inverse <= max; inverse++) { | |
3630 | AV* list; | |
064c021d KW |
3631 | SV** listp; |
3632 | IV i; | |
3633 | bool found_key = FALSE; | |
5662e334 | 3634 | bool found_inverse = FALSE; |
064c021d KW |
3635 | |
3636 | /* The key is the inverse mapping */ | |
3637 | char key[UTF8_MAXBYTES+1]; | |
c80e42f3 | 3638 | char* key_end = (char *) uvchr_to_utf8((U8*) key, val); |
064c021d KW |
3639 | STRLEN key_len = key_end - key; |
3640 | ||
064c021d KW |