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