This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
op.c: Remove unnecessary flag
[perl5.git] / utf8.c
CommitLineData
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 *
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
TC
20 * as is the custom in the West, if you wish to be answered?'
21 * --Gandalf, addressing Théoden's door wardens
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. */
38PERL_CALLCONV UV Perl_utf8n_to_uvchr(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags);
39PERL_CALLCONV U8* Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv);
40#endif
41
27da23d5
JH
42static 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
48This file contains various utility functions for manipulating UTF8-encoded
49strings. For the uninitiated, this is a method of representing arbitrary
61296642 50Unicode characters as a variable number of bytes, in such a way that
56da48f7
DM
51characters in the ASCII range are unmodified, and a zero byte never appears
52within non-zero characters.
166f8a29 53
eaf7a4d2
CS
54=cut
55*/
56
57/*
58=for apidoc is_ascii_string
59
970ea3cb
KW
60Returns true if the first C<len> bytes of the given string are the same whether
61or not the string is encoded in UTF-8 (or UTF-EBCDIC on EBCDIC machines). That
62is, if they are invariant. On ASCII-ish machines, only ASCII characters
63fit this definition, hence the function's name.
eaf7a4d2 64
9f7e3d64
MH
65If C<len> is 0, it will be calculated using C<strlen(s)>.
66
eaf7a4d2
CS
67See also is_utf8_string(), is_utf8_string_loclen(), and is_utf8_string_loc().
68
69=cut
70*/
71
72bool
668b6d8d 73Perl_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
91Adds the UTF-8 representation of the code point C<uv> to the end
92of the string C<d>; C<d> should have at least C<UTF8_MAXBYTES+1> free
eebe1485 93bytes available. The return value is the pointer to the byte after the
9041c2e3 94end of the new character. In other words,
eebe1485 95
b851fbc1
JH
96 d = uvuni_to_utf8_flags(d, uv, flags);
97
98or, 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 106This is the recommended Unicode-aware way of saying
eebe1485
SC
107
108 *(d++) = uv;
109
949cf498
KW
110This function will convert to UTF-8 (and not warn) even code points that aren't
111legal Unicode or are problematic, unless C<flags> contains one or more of the
112following flags.
113If C<uv> is a Unicode surrogate code point and UNICODE_WARN_SURROGATE is set,
114the function will raise a warning, provided UTF8 warnings are enabled. If instead
115UNICODE_DISALLOW_SURROGATE is set, the function will fail and return NULL.
116If both flags are set, the function will both warn and return NULL.
117
118The UNICODE_WARN_NONCHAR and UNICODE_DISALLOW_NONCHAR flags correspondingly
119affect how the function handles a Unicode non-character. And, likewise for the
120UNICODE_WARN_SUPER and UNICODE_DISALLOW_SUPER flags, and code points that are
121above the Unicode maximum of 0x10FFFF. Code points above 0x7FFF_FFFF (which are
122even less portable) can be warned and/or disallowed even if other above-Unicode
123code points are accepted by the UNICODE_WARN_FE_FF and UNICODE_DISALLOW_FE_FF
124flags.
125
126And finally, the flag UNICODE_WARN_ILLEGAL_INTERCHANGE selects all four of the
127above WARN flags; and UNICODE_DISALLOW_ILLEGAL_INTERCHANGE selects all four
128DISALLOW flags.
129
130
eebe1485
SC
131=cut
132*/
133
dfe13c55 134U8 *
b851fbc1 135Perl_uvuni_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
a0ed51b3 136{
7918f24d
NC
137 PERL_ARGS_ASSERT_UVUNI_TO_UTF8_FLAGS;
138
949cf498
KW
139 if (ckWARN_d(WARN_UTF8)) {
140 if (UNICODE_IS_SURROGATE(uv)) {
141 if (flags & UNICODE_WARN_SURROGATE) {
142 Perl_warner(aTHX_ packWARN(WARN_UTF8),
143 "UTF-16 surrogate U+%04"UVXf, uv);
144 }
145 if (flags & UNICODE_DISALLOW_SURROGATE) {
146 return NULL;
147 }
148 }
149 else if (UNICODE_IS_SUPER(uv)) {
150 if (flags & UNICODE_WARN_SUPER
151 || (UNICODE_IS_FE_FF(uv) && (flags & UNICODE_WARN_FE_FF)))
152 {
153 Perl_warner(aTHX_ packWARN(WARN_UTF8),
154 "Code point 0x%04"UVXf" is not Unicode, may not be portable", uv);
155 }
156 if (flags & UNICODE_DISALLOW_SUPER
157 || (UNICODE_IS_FE_FF(uv) && (flags & UNICODE_DISALLOW_FE_FF)))
158 {
159 return NULL;
160 }
161 }
162 else if (UNICODE_IS_NONCHAR(uv)) {
163 if (flags & UNICODE_WARN_NONCHAR) {
164 Perl_warner(aTHX_ packWARN(WARN_UTF8),
165 "Unicode non-character U+%04"UVXf" is illegal for open interchange",
166 uv);
167 }
168 if (flags & UNICODE_DISALLOW_NONCHAR) {
169 return NULL;
170 }
171 }
507b9800 172 }
c4d5f83a 173 if (UNI_IS_INVARIANT(uv)) {
eb160463 174 *d++ = (U8)UTF_TO_NATIVE(uv);
a0ed51b3
LW
175 return d;
176 }
2d331972 177#if defined(EBCDIC)
1d72bdf6
NIS
178 else {
179 STRLEN len = UNISKIP(uv);
180 U8 *p = d+len-1;
181 while (p > d) {
eb160463 182 *p-- = (U8)UTF_TO_NATIVE((uv & UTF_CONTINUATION_MASK) | UTF_CONTINUATION_MARK);
1d72bdf6
NIS
183 uv >>= UTF_ACCUMULATION_SHIFT;
184 }
eb160463 185 *p = (U8)UTF_TO_NATIVE((uv & UTF_START_MASK(len)) | UTF_START_MARK(len));
1d72bdf6
NIS
186 return d+len;
187 }
188#else /* Non loop style */
a0ed51b3 189 if (uv < 0x800) {
eb160463
GS
190 *d++ = (U8)(( uv >> 6) | 0xc0);
191 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
192 return d;
193 }
194 if (uv < 0x10000) {
eb160463
GS
195 *d++ = (U8)(( uv >> 12) | 0xe0);
196 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
197 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
198 return d;
199 }
200 if (uv < 0x200000) {
eb160463
GS
201 *d++ = (U8)(( uv >> 18) | 0xf0);
202 *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
203 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
204 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
205 return d;
206 }
207 if (uv < 0x4000000) {
eb160463
GS
208 *d++ = (U8)(( uv >> 24) | 0xf8);
209 *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
210 *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
211 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
212 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
213 return d;
214 }
215 if (uv < 0x80000000) {
eb160463
GS
216 *d++ = (U8)(( uv >> 30) | 0xfc);
217 *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
218 *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
219 *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
220 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
221 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
222 return d;
223 }
6b8eaf93 224#ifdef HAS_QUAD
d7578b48 225 if (uv < UTF8_QUAD_MAX)
a0ed51b3
LW
226#endif
227 {
eb160463
GS
228 *d++ = 0xfe; /* Can't match U+FEFF! */
229 *d++ = (U8)(((uv >> 30) & 0x3f) | 0x80);
230 *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
231 *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
232 *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
233 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
234 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
235 return d;
236 }
6b8eaf93 237#ifdef HAS_QUAD
a0ed51b3 238 {
eb160463
GS
239 *d++ = 0xff; /* Can't match U+FFFE! */
240 *d++ = 0x80; /* 6 Reserved bits */
241 *d++ = (U8)(((uv >> 60) & 0x0f) | 0x80); /* 2 Reserved bits */
242 *d++ = (U8)(((uv >> 54) & 0x3f) | 0x80);
243 *d++ = (U8)(((uv >> 48) & 0x3f) | 0x80);
244 *d++ = (U8)(((uv >> 42) & 0x3f) | 0x80);
245 *d++ = (U8)(((uv >> 36) & 0x3f) | 0x80);
246 *d++ = (U8)(((uv >> 30) & 0x3f) | 0x80);
247 *d++ = (U8)(((uv >> 24) & 0x3f) | 0x80);
248 *d++ = (U8)(((uv >> 18) & 0x3f) | 0x80);
249 *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
250 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
251 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
252 return d;
253 }
254#endif
1d72bdf6 255#endif /* Loop style */
a0ed51b3 256}
9041c2e3 257
646ca15d
JH
258/*
259
260Tests if some arbitrary number of bytes begins in a valid UTF-8
261character. Note that an INVARIANT (i.e. ASCII) character is a valid
262UTF-8 character. The actual number of bytes in the UTF-8 character
263will be returned if it is valid, otherwise 0.
264
265This is the "slow" version as opposed to the "fast" version which is
266the "unrolled" IS_UTF8_CHAR(). E.g. for t/uni/class.t the speed
267difference is a factor of 2 to 3. For lengths (UTF8SKIP(s)) of four
268or less you should use the IS_UTF8_CHAR(), for lengths of five or more
269you should use the _slow(). In practice this means that the _slow()
270will be used very rarely, since the maximum Unicode code point (as of
271Unicode 4.1) is U+10FFFF, which encodes in UTF-8 to four bytes. Only
272the "Perl extended UTF-8" (the infamous 'v-strings') will encode into
273five bytes or more.
274
275=cut */
c053b435 276STATIC STRLEN
5f66b61c 277S_is_utf8_char_slow(const U8 *s, const STRLEN len)
646ca15d
JH
278{
279 U8 u = *s;
280 STRLEN slen;
281 UV uv, ouv;
282
7918f24d
NC
283 PERL_ARGS_ASSERT_IS_UTF8_CHAR_SLOW;
284
646ca15d
JH
285 if (UTF8_IS_INVARIANT(u))
286 return 1;
287
288 if (!UTF8_IS_START(u))
289 return 0;
290
291 if (len < 2 || !UTF8_IS_CONTINUATION(s[1]))
292 return 0;
293
294 slen = len - 1;
295 s++;
77263263
ST
296#ifdef EBCDIC
297 u = NATIVE_TO_UTF(u);
298#endif
646ca15d
JH
299 u &= UTF_START_MASK(len);
300 uv = u;
301 ouv = uv;
302 while (slen--) {
303 if (!UTF8_IS_CONTINUATION(*s))
304 return 0;
305 uv = UTF8_ACCUMULATE(uv, *s);
48ef279e 306 if (uv < ouv)
646ca15d
JH
307 return 0;
308 ouv = uv;
309 s++;
310 }
311
312 if ((STRLEN)UNISKIP(uv) < len)
313 return 0;
314
315 return len;
316}
9041c2e3
NIS
317
318/*
87cea99e 319=for apidoc is_utf8_char
eebe1485 320
5da9da9e 321Tests if some arbitrary number of bytes begins in a valid UTF-8
2bbc8d55
SP
322character. Note that an INVARIANT (i.e. ASCII on non-EBCDIC machines)
323character is a valid UTF-8 character. The actual number of bytes in the UTF-8
324character will be returned if it is valid, otherwise 0.
9041c2e3 325
82686b01 326=cut */
067a85ef 327STRLEN
668b6d8d 328Perl_is_utf8_char(const U8 *s)
386d01d6 329{
44f8325f 330 const STRLEN len = UTF8SKIP(s);
7918f24d
NC
331
332 PERL_ARGS_ASSERT_IS_UTF8_CHAR;
3b0fc154 333#ifdef IS_UTF8_CHAR
768c67ee 334 if (IS_UTF8_CHAR_FAST(len))
3b0fc154
JH
335 return IS_UTF8_CHAR(s, len) ? len : 0;
336#endif /* #ifdef IS_UTF8_CHAR */
2c0c5f92 337 return is_utf8_char_slow(s, len);
386d01d6
GS
338}
339
eaf7a4d2 340
6662521e 341/*
87cea99e 342=for apidoc is_utf8_string
6662521e 343
c9ada85f 344Returns true if first C<len> bytes of the given string form a valid
9f7e3d64
MH
345UTF-8 string, false otherwise. If C<len> is 0, it will be calculated
346using C<strlen(s)>. Note that 'a valid UTF-8 string' does not mean 'a
347string that contains code points above 0x7F encoded in UTF-8' because a
348valid ASCII string is a valid UTF-8 string.
6662521e 349
eaf7a4d2 350See also is_ascii_string(), is_utf8_string_loclen(), and is_utf8_string_loc().
768c67ee 351
6662521e
GS
352=cut
353*/
354
8e84507e 355bool
668b6d8d 356Perl_is_utf8_string(const U8 *s, STRLEN len)
6662521e 357{
35da51f7 358 const U8* const send = s + (len ? len : strlen((const char *)s));
7fc63493 359 const U8* x = s;
067a85ef 360
7918f24d 361 PERL_ARGS_ASSERT_IS_UTF8_STRING;
1aa99e6b 362
6662521e 363 while (x < send) {
a3b680e6 364 STRLEN c;
1acdb0da
JH
365 /* Inline the easy bits of is_utf8_char() here for speed... */
366 if (UTF8_IS_INVARIANT(*x))
367 c = 1;
368 else if (!UTF8_IS_START(*x))
768c67ee 369 goto out;
1acdb0da
JH
370 else {
371 /* ... and call is_utf8_char() only if really needed. */
646ca15d
JH
372#ifdef IS_UTF8_CHAR
373 c = UTF8SKIP(x);
768c67ee
JH
374 if (IS_UTF8_CHAR_FAST(c)) {
375 if (!IS_UTF8_CHAR(x, c))
3c614e38
AD
376 c = 0;
377 }
378 else
379 c = is_utf8_char_slow(x, c);
646ca15d
JH
380#else
381 c = is_utf8_char(x);
382#endif /* #ifdef IS_UTF8_CHAR */
1acdb0da 383 if (!c)
768c67ee 384 goto out;
1acdb0da 385 }
6662521e 386 x += c;
6662521e 387 }
768c67ee
JH
388
389 out:
60006e79
JH
390 if (x != send)
391 return FALSE;
067a85ef
A
392
393 return TRUE;
6662521e
GS
394}
395
67e989fb 396/*
814fafa7
NC
397Implemented as a macro in utf8.h
398
87cea99e 399=for apidoc is_utf8_string_loc
814fafa7
NC
400
401Like is_utf8_string() but stores the location of the failure (in the
402case of "utf8ness failure") or the location s+len (in the case of
403"utf8ness success") in the C<ep>.
404
405See also is_utf8_string_loclen() and is_utf8_string().
406
87cea99e 407=for apidoc is_utf8_string_loclen
81cd54e3 408
e3e4599f 409Like is_utf8_string() but stores the location of the failure (in the
768c67ee
JH
410case of "utf8ness failure") or the location s+len (in the case of
411"utf8ness success") in the C<ep>, and the number of UTF-8
412encoded characters in the C<el>.
413
414See also is_utf8_string_loc() and is_utf8_string().
81cd54e3
JH
415
416=cut
417*/
418
419bool
668b6d8d 420Perl_is_utf8_string_loclen(const U8 *s, STRLEN len, const U8 **ep, STRLEN *el)
81cd54e3 421{
35da51f7 422 const U8* const send = s + (len ? len : strlen((const char *)s));
7fc63493 423 const U8* x = s;
81cd54e3 424 STRLEN c;
3ebfea28 425 STRLEN outlen = 0;
7918f24d
NC
426
427 PERL_ARGS_ASSERT_IS_UTF8_STRING_LOCLEN;
81cd54e3 428
81cd54e3
JH
429 while (x < send) {
430 /* Inline the easy bits of is_utf8_char() here for speed... */
431 if (UTF8_IS_INVARIANT(*x))
768c67ee
JH
432 c = 1;
433 else if (!UTF8_IS_START(*x))
434 goto out;
81cd54e3 435 else {
768c67ee
JH
436 /* ... and call is_utf8_char() only if really needed. */
437#ifdef IS_UTF8_CHAR
438 c = UTF8SKIP(x);
439 if (IS_UTF8_CHAR_FAST(c)) {
440 if (!IS_UTF8_CHAR(x, c))
441 c = 0;
442 } else
443 c = is_utf8_char_slow(x, c);
444#else
445 c = is_utf8_char(x);
446#endif /* #ifdef IS_UTF8_CHAR */
447 if (!c)
448 goto out;
81cd54e3 449 }
768c67ee 450 x += c;
3ebfea28 451 outlen++;
81cd54e3 452 }
768c67ee
JH
453
454 out:
3ebfea28
AL
455 if (el)
456 *el = outlen;
457
768c67ee
JH
458 if (ep)
459 *ep = x;
3ebfea28 460 return (x == send);
81cd54e3
JH
461}
462
463/*
768c67ee 464
87cea99e 465=for apidoc utf8n_to_uvuni
67e989fb 466
9041c2e3 467Bottom level UTF-8 decode routine.
949cf498
KW
468Returns the code point value of the first character in the string C<s>
469which is assumed to be in UTF-8 (or UTF-EBCDIC) encoding and no longer than
470C<curlen> bytes; C<retlen> will be set to the length, in bytes, of that
471character.
472
473The value of C<flags> determines the behavior when C<s> does not point to a
474well-formed UTF-8 character. If C<flags> is 0, when a malformation is found,
475C<retlen> is set to the expected length of the UTF-8 character in bytes, zero
476is returned, and if UTF-8 warnings haven't been lexically disabled, a warning
477is raised.
478
479Various ALLOW flags can be set in C<flags> to allow (and not warn on)
480individual types of malformations, such as the sequence being overlong (that
481is, when there is a shorter sequence that can express the same code point;
482overlong sequences are expressly forbidden in the UTF-8 standard due to
483potential security issues). Another malformation example is the first byte of
484a character not being a legal first byte. See F<utf8.h> for the list of such
485flags. Of course, the value returned by this function under such conditions is
486not reliable.
487
488The UTF8_CHECK_ONLY flag overrides the behavior when a non-allowed (by other
489flags) malformation is found. If this flag is set, the routine assumes that
490the caller will raise a warning, and this function will silently just set
491C<retlen> to C<-1> and return zero.
492
493Certain code points are considered problematic. These are Unicode surrogates,
494Unicode non-characters, and code points above the Unicode maximum of 0x10FFF.
495By default these are considered regular code points, but certain situations
496warrant special handling for them. if C<flags> contains
497UTF8_DISALLOW_ILLEGAL_INTERCHANGE, all three classes are treated as
498malformations and handled as such. The flags UTF8_DISALLOW_SURROGATE,
499UTF8_DISALLOW_NONCHAR, and UTF8_DISALLOW_SUPER (meaning above the legal Unicode
500maximum) can be set to disallow these categories individually.
501
502The flags UTF8_WARN_ILLEGAL_INTERCHANGE, UTF8_WARN_SURROGATE,
503UTF8_WARN_NONCHAR, and UTF8_WARN_SUPER will cause warning messages to be raised
504for their respective categories, but otherwise the code points are considered
505valid (not malformations). To get a category to both be treated as a
506malformation and raise a warning, specify both the WARN and DISALLOW flags.
507(But note that warnings are not raised if lexically disabled nor if
508UTF8_CHECK_ONLY is also specified.)
509
510Very large code points (above 0x7FFF_FFFF) are considered more problematic than
511the others that are above the Unicode legal maximum. There are several
512reasons, one of which is that the original UTF-8 specification never went above
513this number (the current 0x10FFF limit was imposed later). The UTF-8 encoding
514on ASCII platforms for these large code point begins with a byte containing
5150xFE or 0xFF. The UTF8_DISALLOW_FE_FF flag will cause them to be treated as
516malformations, while allowing smaller above-Unicode code points. (Of course
517UTF8_DISALLOW_SUPER will treat all above-Unicode code points, including these,
518as malformations.) Similarly, UTF8_WARN_FE_FF acts just like the other WARN
519flags, but applies just to these code points.
520
521All other code points corresponding to Unicode characters, including private
522use and those yet to be assigned, are never considered malformed and never
523warn.
67e989fb 524
9041c2e3
NIS
525Most code should use utf8_to_uvchr() rather than call this directly.
526
37607a96
PK
527=cut
528*/
67e989fb 529
a0ed51b3 530UV
7fc63493 531Perl_utf8n_to_uvuni(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen, U32 flags)
a0ed51b3 532{
97aff369 533 dVAR;
d4c19fe8 534 const U8 * const s0 = s;
9c5ffd7c 535 UV uv = *s, ouv = 0;
ba210ebe 536 STRLEN len = 1;
949cf498 537 bool dowarn = ckWARN_d(WARN_UTF8);
7fc63493 538 const UV startbyte = *s;
ba210ebe 539 STRLEN expectlen = 0;
a0dbb045 540 U32 warning = 0;
949cf498 541 SV* sv = NULL;
a0dbb045 542
7918f24d
NC
543 PERL_ARGS_ASSERT_UTF8N_TO_UVUNI;
544
949cf498 545/* This list is a superset of the UTF8_ALLOW_XXX. */
a0dbb045
JH
546
547#define UTF8_WARN_EMPTY 1
548#define UTF8_WARN_CONTINUATION 2
549#define UTF8_WARN_NON_CONTINUATION 3
a0dbb045
JH
550#define UTF8_WARN_SHORT 5
551#define UTF8_WARN_OVERFLOW 6
c867b360 552#define UTF8_WARN_LONG 8
a0dbb045
JH
553
554 if (curlen == 0 &&
555 !(flags & UTF8_ALLOW_EMPTY)) {
556 warning = UTF8_WARN_EMPTY;
0c443dc2
JH
557 goto malformed;
558 }
559
1d72bdf6 560 if (UTF8_IS_INVARIANT(uv)) {
a0ed51b3
LW
561 if (retlen)
562 *retlen = 1;
c4d5f83a 563 return (UV) (NATIVE_TO_UTF(*s));
a0ed51b3 564 }
67e989fb 565
421a8bf2 566 if (UTF8_IS_CONTINUATION(uv) &&
fcc8fcf6 567 !(flags & UTF8_ALLOW_CONTINUATION)) {
a0dbb045 568 warning = UTF8_WARN_CONTINUATION;
ba210ebe
JH
569 goto malformed;
570 }
571
421a8bf2 572 if (UTF8_IS_START(uv) && curlen > 1 && !UTF8_IS_CONTINUATION(s[1]) &&
fcc8fcf6 573 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
a0dbb045 574 warning = UTF8_WARN_NON_CONTINUATION;
ba210ebe
JH
575 goto malformed;
576 }
9041c2e3 577
1d72bdf6 578#ifdef EBCDIC
75383841 579 uv = NATIVE_TO_UTF(uv);
1d72bdf6 580#else
949cf498
KW
581 if (uv == 0xfe || uv == 0xff) {
582 if (flags & (UTF8_WARN_SUPER|UTF8_WARN_FE_FF)) {
583 sv = sv_2mortal(newSVpvf_nocontext("Code point beginning with byte 0x%02"UVXf" is not Unicode, and not portable", uv));
584 flags &= ~UTF8_WARN_SUPER; /* Only warn once on this problem */
585 }
586 if (flags & (UTF8_DISALLOW_SUPER|UTF8_DISALLOW_FE_FF)) {
587 goto malformed;
588 }
a0ed51b3 589 }
1d72bdf6
NIS
590#endif
591
ba210ebe
JH
592 if (!(uv & 0x20)) { len = 2; uv &= 0x1f; }
593 else if (!(uv & 0x10)) { len = 3; uv &= 0x0f; }
594 else if (!(uv & 0x08)) { len = 4; uv &= 0x07; }
595 else if (!(uv & 0x04)) { len = 5; uv &= 0x03; }
1d72bdf6
NIS
596#ifdef EBCDIC
597 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
598 else { len = 7; uv &= 0x01; }
599#else
ba210ebe
JH
600 else if (!(uv & 0x02)) { len = 6; uv &= 0x01; }
601 else if (!(uv & 0x01)) { len = 7; uv = 0; }
1d72bdf6
NIS
602 else { len = 13; uv = 0; } /* whoa! */
603#endif
604
a0ed51b3
LW
605 if (retlen)
606 *retlen = len;
9041c2e3 607
ba210ebe
JH
608 expectlen = len;
609
fcc8fcf6
JH
610 if ((curlen < expectlen) &&
611 !(flags & UTF8_ALLOW_SHORT)) {
a0dbb045 612 warning = UTF8_WARN_SHORT;
ba210ebe
JH
613 goto malformed;
614 }
615
616 len--;
a0ed51b3 617 s++;
949cf498 618 ouv = uv; /* ouv is the value from the previous iteration */
ba210ebe 619
a0ed51b3 620 while (len--) {
421a8bf2
JH
621 if (!UTF8_IS_CONTINUATION(*s) &&
622 !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
a0dbb045
JH
623 s--;
624 warning = UTF8_WARN_NON_CONTINUATION;
ba210ebe 625 goto malformed;
a0ed51b3
LW
626 }
627 else
8850bf83 628 uv = UTF8_ACCUMULATE(uv, *s);
949cf498
KW
629 if (!(uv > ouv)) { /* If the value didn't grow from the previous
630 iteration, something is horribly wrong */
a0dbb045
JH
631 /* These cannot be allowed. */
632 if (uv == ouv) {
75dbc644 633 if (expectlen != 13 && !(flags & UTF8_ALLOW_LONG)) {
a0dbb045
JH
634 warning = UTF8_WARN_LONG;
635 goto malformed;
636 }
637 }
638 else { /* uv < ouv */
639 /* This cannot be allowed. */
640 warning = UTF8_WARN_OVERFLOW;
641 goto malformed;
642 }
ba210ebe
JH
643 }
644 s++;
645 ouv = uv;
646 }
647
949cf498 648 if ((expectlen > (STRLEN)UNISKIP(uv)) && !(flags & UTF8_ALLOW_LONG)) {
a0dbb045 649 warning = UTF8_WARN_LONG;
ba210ebe 650 goto malformed;
949cf498
KW
651 } else if (flags & (UTF8_DISALLOW_ILLEGAL_INTERCHANGE|UTF8_WARN_ILLEGAL_INTERCHANGE)) {
652 if (UNICODE_IS_SURROGATE(uv)) {
653 if ((flags & (UTF8_WARN_SURROGATE|UTF8_CHECK_ONLY)) == UTF8_WARN_SURROGATE) {
654 sv = sv_2mortal(newSVpvf_nocontext("UTF-16 surrogate U+%04"UVXf"", uv));
655 }
656 if (flags & UTF8_DISALLOW_SURROGATE) {
657 goto disallowed;
658 }
659 }
660 else if (UNICODE_IS_NONCHAR(uv)) {
661 if ((flags & (UTF8_WARN_NONCHAR|UTF8_CHECK_ONLY)) == UTF8_WARN_NONCHAR ) {
662 sv = sv_2mortal(newSVpvf_nocontext("Unicode non-character U+%04"UVXf" is illegal for open interchange", uv));
663 }
664 if (flags & UTF8_DISALLOW_NONCHAR) {
665 goto disallowed;
666 }
667 }
668 else if ((uv > PERL_UNICODE_MAX)) {
669 if ((flags & (UTF8_WARN_SUPER|UTF8_CHECK_ONLY)) == UTF8_WARN_SUPER) {
670 sv = sv_2mortal(newSVpvf_nocontext("Code point 0x%04"UVXf" is not Unicode, may not be portable", uv));
671 }
672 if (flags & UTF8_DISALLOW_SUPER) {
673 goto disallowed;
674 }
675 }
676
677 /* Here, this is not considered a malformed character, so drop through
678 * to return it */
a0ed51b3 679 }
ba210ebe 680
a0ed51b3 681 return uv;
ba210ebe 682
949cf498
KW
683disallowed: /* Is disallowed, but otherwise not malformed. 'sv' will have been
684 set if there is to be a warning. */
685 if (!sv) {
686 dowarn = 0;
687 }
688
ba210ebe
JH
689malformed:
690
fcc8fcf6 691 if (flags & UTF8_CHECK_ONLY) {
ba210ebe 692 if (retlen)
10edeb5d 693 *retlen = ((STRLEN) -1);
ba210ebe
JH
694 return 0;
695 }
696
a0dbb045 697 if (dowarn) {
949cf498 698 if (! sv) {
5b311467 699 sv = newSVpvs_flags("Malformed UTF-8 character ", SVs_TEMP);
949cf498 700 }
5b311467
KW
701
702 switch (warning) {
703 case 0: /* Intentionally empty. */ break;
704 case UTF8_WARN_EMPTY:
705 sv_catpvs(sv, "(empty string)");
706 break;
707 case UTF8_WARN_CONTINUATION:
708 Perl_sv_catpvf(aTHX_ sv, "(unexpected continuation byte 0x%02"UVxf", with no preceding start byte)", uv);
709 break;
710 case UTF8_WARN_NON_CONTINUATION:
711 if (s == s0)
712 Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf", immediately after start byte 0x%02"UVxf")",
713 (UV)s[1], startbyte);
714 else {
715 const int len = (int)(s-s0);
716 Perl_sv_catpvf(aTHX_ sv, "(unexpected non-continuation byte 0x%02"UVxf", %d byte%s after start byte 0x%02"UVxf", expected %d bytes)",
717 (UV)s[1], len, len > 1 ? "s" : "", startbyte, (int)expectlen);
718 }
719
720 break;
5b311467
KW
721 case UTF8_WARN_SHORT:
722 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d, after start byte 0x%02"UVxf")",
723 (int)curlen, curlen == 1 ? "" : "s", (int)expectlen, startbyte);
724 expectlen = curlen; /* distance for caller to skip */
725 break;
726 case UTF8_WARN_OVERFLOW:
727 Perl_sv_catpvf(aTHX_ sv, "(overflow at 0x%"UVxf", byte 0x%02x, after start byte 0x%02"UVxf")",
728 ouv, *s, startbyte);
729 break;
5b311467
KW
730 case UTF8_WARN_LONG:
731 Perl_sv_catpvf(aTHX_ sv, "(%d byte%s, need %d, after start byte 0x%02"UVxf")",
732 (int)expectlen, expectlen == 1 ? "": "s", UNISKIP(uv), startbyte);
733 break;
734 default:
735 sv_catpvs(sv, "(unknown reason)");
736 break;
551405c4 737 }
a0dbb045 738
949cf498 739 if (sv) {
44f8325f 740 const char * const s = SvPVX_const(sv);
a0dbb045
JH
741
742 if (PL_op)
9014280d 743 Perl_warner(aTHX_ packWARN(WARN_UTF8),
53e06cf0 744 "%s in %s", s, OP_DESC(PL_op));
a0dbb045 745 else
9014280d 746 Perl_warner(aTHX_ packWARN(WARN_UTF8), "%s", s);
a0dbb045
JH
747 }
748 }
749
ba210ebe 750 if (retlen)
28d3d195 751 *retlen = expectlen ? expectlen : len;
ba210ebe 752
28d3d195 753 return 0;
a0ed51b3
LW
754}
755
8e84507e 756/*
87cea99e 757=for apidoc utf8_to_uvchr
9041c2e3 758
6ee84de2 759Returns the native code point of the first character in the string C<s>
1e54db1a 760which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
9041c2e3
NIS
761length, in bytes, of that character.
762
1e54db1a 763If C<s> does not point to a well-formed UTF-8 character, zero is
9041c2e3
NIS
764returned and retlen is set, if possible, to -1.
765
766=cut
767*/
768
769UV
7fc63493 770Perl_utf8_to_uvchr(pTHX_ const U8 *s, STRLEN *retlen)
9041c2e3 771{
7918f24d
NC
772 PERL_ARGS_ASSERT_UTF8_TO_UVCHR;
773
1754c1a1
NC
774 return utf8n_to_uvchr(s, UTF8_MAXBYTES, retlen,
775 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
9041c2e3
NIS
776}
777
778/*
87cea99e 779=for apidoc utf8_to_uvuni
9041c2e3
NIS
780
781Returns the Unicode code point of the first character in the string C<s>
1e54db1a 782which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
9041c2e3
NIS
783length, in bytes, of that character.
784
2bbc8d55 785This function should only be used when the returned UV is considered
9041c2e3
NIS
786an index into the Unicode semantic tables (e.g. swashes).
787
1e54db1a 788If C<s> does not point to a well-formed UTF-8 character, zero is
ba210ebe 789returned and retlen is set, if possible, to -1.
8e84507e
NIS
790
791=cut
792*/
793
794UV
7fc63493 795Perl_utf8_to_uvuni(pTHX_ const U8 *s, STRLEN *retlen)
8e84507e 796{
7918f24d
NC
797 PERL_ARGS_ASSERT_UTF8_TO_UVUNI;
798
9041c2e3 799 /* Call the low level routine asking for checks */
89ebb4a3 800 return Perl_utf8n_to_uvuni(aTHX_ s, UTF8_MAXBYTES, retlen,
872c91ae 801 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
8e84507e
NIS
802}
803
b76347f2 804/*
87cea99e 805=for apidoc utf8_length
b76347f2
JH
806
807Return the length of the UTF-8 char encoded string C<s> in characters.
02eb7b47
JH
808Stops at C<e> (inclusive). If C<e E<lt> s> or if the scan would end
809up past C<e>, croaks.
b76347f2
JH
810
811=cut
812*/
813
814STRLEN
35a4481c 815Perl_utf8_length(pTHX_ const U8 *s, const U8 *e)
b76347f2 816{
97aff369 817 dVAR;
b76347f2
JH
818 STRLEN len = 0;
819
7918f24d
NC
820 PERL_ARGS_ASSERT_UTF8_LENGTH;
821
8850bf83
JH
822 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g.
823 * the bitops (especially ~) can create illegal UTF-8.
824 * In other words: in Perl UTF-8 is not just for Unicode. */
825
a3b680e6
AL
826 if (e < s)
827 goto warn_and_return;
b76347f2 828 while (s < e) {
8e91ec7f
AV
829 if (!UTF8_IS_INVARIANT(*s))
830 s += UTF8SKIP(s);
831 else
832 s++;
833 len++;
834 }
835
836 if (e != s) {
837 len--;
838 warn_and_return:
9b387841
NC
839 if (PL_op)
840 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
841 "%s in %s", unees, OP_DESC(PL_op));
842 else
843 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), unees);
b76347f2
JH
844 }
845
846 return len;
847}
848
b06226ff 849/*
87cea99e 850=for apidoc utf8_distance
b06226ff 851
1e54db1a 852Returns the number of UTF-8 characters between the UTF-8 pointers C<a>
b06226ff
JH
853and C<b>.
854
855WARNING: use only if you *know* that the pointers point inside the
856same UTF-8 buffer.
857
37607a96
PK
858=cut
859*/
a0ed51b3 860
02eb7b47 861IV
35a4481c 862Perl_utf8_distance(pTHX_ const U8 *a, const U8 *b)
a0ed51b3 863{
7918f24d
NC
864 PERL_ARGS_ASSERT_UTF8_DISTANCE;
865
bf1665bc 866 return (a < b) ? -1 * (IV) utf8_length(a, b) : (IV) utf8_length(b, a);
a0ed51b3
LW
867}
868
b06226ff 869/*
87cea99e 870=for apidoc utf8_hop
b06226ff 871
8850bf83
JH
872Return the UTF-8 pointer C<s> displaced by C<off> characters, either
873forward or backward.
b06226ff
JH
874
875WARNING: do not use the following unless you *know* C<off> is within
8850bf83
JH
876the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned
877on the first byte of character or just after the last byte of a character.
b06226ff 878
37607a96
PK
879=cut
880*/
a0ed51b3
LW
881
882U8 *
4373e329 883Perl_utf8_hop(pTHX_ const U8 *s, I32 off)
a0ed51b3 884{
7918f24d
NC
885 PERL_ARGS_ASSERT_UTF8_HOP;
886
96a5add6 887 PERL_UNUSED_CONTEXT;
8850bf83
JH
888 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
889 * the bitops (especially ~) can create illegal UTF-8.
890 * In other words: in Perl UTF-8 is not just for Unicode. */
891
a0ed51b3
LW
892 if (off >= 0) {
893 while (off--)
894 s += UTF8SKIP(s);
895 }
896 else {
897 while (off++) {
898 s--;
8850bf83
JH
899 while (UTF8_IS_CONTINUATION(*s))
900 s--;
a0ed51b3
LW
901 }
902 }
4373e329 903 return (U8 *)s;
a0ed51b3
LW
904}
905
6940069f 906/*
fed3ba5d
NC
907=for apidoc bytes_cmp_utf8
908
909Compares the sequence of characters (stored as octets) in b, blen with the
910sequence of characters (stored as UTF-8) in u, ulen. Returns 0 if they are
911equal, -1 or -2 if the first string is less than the second string, +1 or +2
912if the first string is greater than the second string.
913
914-1 or +1 is returned if the shorter string was identical to the start of the
915longer string. -2 or +2 is returned if the was a difference between characters
916within the strings.
917
918=cut
919*/
920
921int
922Perl_bytes_cmp_utf8(pTHX_ const U8 *b, STRLEN blen, const U8 *u, STRLEN ulen)
923{
924 const U8 *const bend = b + blen;
925 const U8 *const uend = u + ulen;
926
927 PERL_ARGS_ASSERT_BYTES_CMP_UTF8;
928
929 PERL_UNUSED_CONTEXT;
930
931 while (b < bend && u < uend) {
932 U8 c = *u++;
933 if (!UTF8_IS_INVARIANT(c)) {
934 if (UTF8_IS_DOWNGRADEABLE_START(c)) {
935 if (u < uend) {
936 U8 c1 = *u++;
937 if (UTF8_IS_CONTINUATION(c1)) {
356979f4 938 c = UNI_TO_NATIVE(TWO_BYTE_UTF8_TO_UNI(c, c1));
fed3ba5d
NC
939 } else {
940 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
941 "Malformed UTF-8 character "
942 "(unexpected non-continuation byte 0x%02x"
943 ", immediately after start byte 0x%02x)"
944 /* Dear diag.t, it's in the pod. */
945 "%s%s", c1, c,
946 PL_op ? " in " : "",
947 PL_op ? OP_DESC(PL_op) : "");
948 return -2;
949 }
950 } else {
951 if (PL_op)
952 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
953 "%s in %s", unees, OP_DESC(PL_op));
954 else
955 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), unees);
956 return -2; /* Really want to return undef :-) */
957 }
958 } else {
959 return -2;
960 }
961 }
962 if (*b != c) {
963 return *b < c ? -2 : +2;
964 }
965 ++b;
966 }
967
968 if (b == bend && u == uend)
969 return 0;
970
971 return b < bend ? +1 : -1;
972}
973
974/*
87cea99e 975=for apidoc utf8_to_bytes
6940069f 976
2bbc8d55 977Converts a string C<s> of length C<len> from UTF-8 into native byte encoding.
246fae53
MG
978Unlike C<bytes_to_utf8>, this over-writes the original string, and
979updates len to contain the new length.
67e989fb 980Returns zero on failure, setting C<len> to -1.
6940069f 981
95be277c
NC
982If you need a copy of the string, see C<bytes_from_utf8>.
983
6940069f
GS
984=cut
985*/
986
987U8 *
37607a96 988Perl_utf8_to_bytes(pTHX_ U8 *s, STRLEN *len)
6940069f 989{
d4c19fe8
AL
990 U8 * const save = s;
991 U8 * const send = s + *len;
6940069f 992 U8 *d;
246fae53 993
7918f24d
NC
994 PERL_ARGS_ASSERT_UTF8_TO_BYTES;
995
1e54db1a 996 /* ensure valid UTF-8 and chars < 256 before updating string */
d4c19fe8 997 while (s < send) {
dcad2880
JH
998 U8 c = *s++;
999
1d72bdf6
NIS
1000 if (!UTF8_IS_INVARIANT(c) &&
1001 (!UTF8_IS_DOWNGRADEABLE_START(c) || (s >= send)
1002 || !(c = *s++) || !UTF8_IS_CONTINUATION(c))) {
10edeb5d 1003 *len = ((STRLEN) -1);
dcad2880
JH
1004 return 0;
1005 }
246fae53 1006 }
dcad2880
JH
1007
1008 d = s = save;
6940069f 1009 while (s < send) {
ed646e6e 1010 STRLEN ulen;
9041c2e3 1011 *d++ = (U8)utf8_to_uvchr(s, &ulen);
ed646e6e 1012 s += ulen;
6940069f
GS
1013 }
1014 *d = '\0';
246fae53 1015 *len = d - save;
6940069f
GS
1016 return save;
1017}
1018
1019/*
87cea99e 1020=for apidoc bytes_from_utf8
f9a63242 1021
2bbc8d55 1022Converts a string C<s> of length C<len> from UTF-8 into native byte encoding.
35a4481c 1023Unlike C<utf8_to_bytes> but like C<bytes_to_utf8>, returns a pointer to
ef9edfd0
JH
1024the newly-created string, and updates C<len> to contain the new
1025length. Returns the original string if no conversion occurs, C<len>
1026is unchanged. Do nothing if C<is_utf8> points to 0. Sets C<is_utf8> to
2bbc8d55
SP
10270 if C<s> is converted or consisted entirely of characters that are invariant
1028in utf8 (i.e., US-ASCII on non-EBCDIC machines).
f9a63242 1029
37607a96
PK
1030=cut
1031*/
f9a63242
JH
1032
1033U8 *
e1ec3a88 1034Perl_bytes_from_utf8(pTHX_ const U8 *s, STRLEN *len, bool *is_utf8)
f9a63242 1035{
f9a63242 1036 U8 *d;
e1ec3a88
AL
1037 const U8 *start = s;
1038 const U8 *send;
f9a63242
JH
1039 I32 count = 0;
1040
7918f24d
NC
1041 PERL_ARGS_ASSERT_BYTES_FROM_UTF8;
1042
96a5add6 1043 PERL_UNUSED_CONTEXT;
f9a63242 1044 if (!*is_utf8)
73d840c0 1045 return (U8 *)start;
f9a63242 1046
1e54db1a 1047 /* ensure valid UTF-8 and chars < 256 before converting string */
f9a63242 1048 for (send = s + *len; s < send;) {
e1ec3a88 1049 U8 c = *s++;
1d72bdf6 1050 if (!UTF8_IS_INVARIANT(c)) {
db42d148
NIS
1051 if (UTF8_IS_DOWNGRADEABLE_START(c) && s < send &&
1052 (c = *s++) && UTF8_IS_CONTINUATION(c))
1053 count++;
1054 else
73d840c0 1055 return (U8 *)start;
db42d148 1056 }
f9a63242
JH
1057 }
1058
35da51f7 1059 *is_utf8 = FALSE;
f9a63242 1060
212542aa 1061 Newx(d, (*len) - count + 1, U8);
ef9edfd0 1062 s = start; start = d;
f9a63242
JH
1063 while (s < send) {
1064 U8 c = *s++;
c4d5f83a
NIS
1065 if (!UTF8_IS_INVARIANT(c)) {
1066 /* Then it is two-byte encoded */
356979f4 1067 c = UNI_TO_NATIVE(TWO_BYTE_UTF8_TO_UNI(c, *s++));
c4d5f83a
NIS
1068 }
1069 *d++ = c;
f9a63242
JH
1070 }
1071 *d = '\0';
1072 *len = d - start;
73d840c0 1073 return (U8 *)start;
f9a63242
JH
1074}
1075
1076/*
87cea99e 1077=for apidoc bytes_to_utf8
6940069f 1078
ff97e5cf
KW
1079Converts a string C<s> of length C<len> bytes from the native encoding into
1080UTF-8.
6662521e 1081Returns a pointer to the newly-created string, and sets C<len> to
ff97e5cf 1082reflect the new length in bytes.
6940069f 1083
2bbc8d55
SP
1084A NUL character will be written after the end of the string.
1085
1086If you want to convert to UTF-8 from encodings other than
1087the native (Latin1 or EBCDIC),
c9ada85f
JH
1088see sv_recode_to_utf8().
1089
497711e7 1090=cut
6940069f
GS
1091*/
1092
1093U8*
35a4481c 1094Perl_bytes_to_utf8(pTHX_ const U8 *s, STRLEN *len)
6940069f 1095{
35a4481c 1096 const U8 * const send = s + (*len);
6940069f
GS
1097 U8 *d;
1098 U8 *dst;
7918f24d
NC
1099
1100 PERL_ARGS_ASSERT_BYTES_TO_UTF8;
96a5add6 1101 PERL_UNUSED_CONTEXT;
6940069f 1102
212542aa 1103 Newx(d, (*len) * 2 + 1, U8);
6940069f
GS
1104 dst = d;
1105
1106 while (s < send) {
35a4481c 1107 const UV uv = NATIVE_TO_ASCII(*s++);
c4d5f83a 1108 if (UNI_IS_INVARIANT(uv))
eb160463 1109 *d++ = (U8)UTF_TO_NATIVE(uv);
6940069f 1110 else {
eb160463
GS
1111 *d++ = (U8)UTF8_EIGHT_BIT_HI(uv);
1112 *d++ = (U8)UTF8_EIGHT_BIT_LO(uv);
6940069f
GS
1113 }
1114 }
1115 *d = '\0';
6662521e 1116 *len = d-dst;
6940069f
GS
1117 return dst;
1118}
1119
a0ed51b3 1120/*
dea0fc0b 1121 * Convert native (big-endian) or reversed (little-endian) UTF-16 to UTF-8.
a0ed51b3
LW
1122 *
1123 * Destination must be pre-extended to 3/2 source. Do not use in-place.
1124 * We optimize for native, for obvious reasons. */
1125
1126U8*
dea0fc0b 1127Perl_utf16_to_utf8(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
a0ed51b3 1128{
dea0fc0b
JH
1129 U8* pend;
1130 U8* dstart = d;
1131
7918f24d
NC
1132 PERL_ARGS_ASSERT_UTF16_TO_UTF8;
1133
dea0fc0b 1134 if (bytelen & 1)
f5992bc4 1135 Perl_croak(aTHX_ "panic: utf16_to_utf8: odd bytelen %"UVuf, (UV)bytelen);
dea0fc0b
JH
1136
1137 pend = p + bytelen;
1138
a0ed51b3 1139 while (p < pend) {
dea0fc0b
JH
1140 UV uv = (p[0] << 8) + p[1]; /* UTF-16BE */
1141 p += 2;
a0ed51b3 1142 if (uv < 0x80) {
e294cc5d
JH
1143#ifdef EBCDIC
1144 *d++ = UNI_TO_NATIVE(uv);
1145#else
eb160463 1146 *d++ = (U8)uv;
e294cc5d 1147#endif
a0ed51b3
LW
1148 continue;
1149 }
1150 if (uv < 0x800) {
eb160463
GS
1151 *d++ = (U8)(( uv >> 6) | 0xc0);
1152 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
1153 continue;
1154 }
52b9aa85 1155 if (uv >= 0xd800 && uv <= 0xdbff) { /* surrogates */
01ea242b 1156 if (p >= pend) {
dea0fc0b 1157 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
01ea242b
NC
1158 } else {
1159 UV low = (p[0] << 8) + p[1];
1160 p += 2;
52b9aa85 1161 if (low < 0xdc00 || low > 0xdfff)
01ea242b
NC
1162 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
1163 uv = ((uv - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
1164 }
dbde1951
NC
1165 } else if (uv >= 0xdc00 && uv <= 0xdfff) {
1166 Perl_croak(aTHX_ "Malformed UTF-16 surrogate");
a0ed51b3
LW
1167 }
1168 if (uv < 0x10000) {
eb160463
GS
1169 *d++ = (U8)(( uv >> 12) | 0xe0);
1170 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
1171 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
1172 continue;
1173 }
1174 else {
eb160463
GS
1175 *d++ = (U8)(( uv >> 18) | 0xf0);
1176 *d++ = (U8)(((uv >> 12) & 0x3f) | 0x80);
1177 *d++ = (U8)(((uv >> 6) & 0x3f) | 0x80);
1178 *d++ = (U8)(( uv & 0x3f) | 0x80);
a0ed51b3
LW
1179 continue;
1180 }
1181 }
dea0fc0b 1182 *newlen = d - dstart;
a0ed51b3
LW
1183 return d;
1184}
1185
1186/* Note: this one is slightly destructive of the source. */
1187
1188U8*
dea0fc0b 1189Perl_utf16_to_utf8_reversed(pTHX_ U8* p, U8* d, I32 bytelen, I32 *newlen)
a0ed51b3
LW
1190{
1191 U8* s = (U8*)p;
d4c19fe8 1192 U8* const send = s + bytelen;
7918f24d
NC
1193
1194 PERL_ARGS_ASSERT_UTF16_TO_UTF8_REVERSED;
1195
e0ea5e2d
NC
1196 if (bytelen & 1)
1197 Perl_croak(aTHX_ "panic: utf16_to_utf8_reversed: odd bytelen %"UVuf,
1198 (UV)bytelen);
1199
a0ed51b3 1200 while (s < send) {
d4c19fe8 1201 const U8 tmp = s[0];
a0ed51b3
LW
1202 s[0] = s[1];
1203 s[1] = tmp;
1204 s += 2;
1205 }
dea0fc0b 1206 return utf16_to_utf8(p, d, bytelen, newlen);
a0ed51b3
LW
1207}
1208
1209/* for now these are all defined (inefficiently) in terms of the utf8 versions */
1210
1211bool
84afefe6 1212Perl_is_uni_alnum(pTHX_ UV c)
a0ed51b3 1213{
89ebb4a3 1214 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1215 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1216 return is_utf8_alnum(tmpbuf);
1217}
1218
1219bool
84afefe6 1220Perl_is_uni_idfirst(pTHX_ UV c)
a0ed51b3 1221{
89ebb4a3 1222 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1223 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1224 return is_utf8_idfirst(tmpbuf);
1225}
1226
1227bool
84afefe6 1228Perl_is_uni_alpha(pTHX_ UV c)
a0ed51b3 1229{
89ebb4a3 1230 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1231 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1232 return is_utf8_alpha(tmpbuf);
1233}
1234
1235bool
84afefe6 1236Perl_is_uni_ascii(pTHX_ UV c)
4d61ec05 1237{
89ebb4a3 1238 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1239 uvchr_to_utf8(tmpbuf, c);
4d61ec05
GS
1240 return is_utf8_ascii(tmpbuf);
1241}
1242
1243bool
84afefe6 1244Perl_is_uni_space(pTHX_ UV c)
a0ed51b3 1245{
89ebb4a3 1246 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1247 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1248 return is_utf8_space(tmpbuf);
1249}
1250
1251bool
84afefe6 1252Perl_is_uni_digit(pTHX_ UV c)
a0ed51b3 1253{
89ebb4a3 1254 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1255 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1256 return is_utf8_digit(tmpbuf);
1257}
1258
1259bool
84afefe6 1260Perl_is_uni_upper(pTHX_ UV c)
a0ed51b3 1261{
89ebb4a3 1262 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1263 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1264 return is_utf8_upper(tmpbuf);
1265}
1266
1267bool
84afefe6 1268Perl_is_uni_lower(pTHX_ UV c)
a0ed51b3 1269{
89ebb4a3 1270 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1271 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1272 return is_utf8_lower(tmpbuf);
1273}
1274
1275bool
84afefe6 1276Perl_is_uni_cntrl(pTHX_ UV c)
b8c5462f 1277{
89ebb4a3 1278 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1279 uvchr_to_utf8(tmpbuf, c);
b8c5462f
JH
1280 return is_utf8_cntrl(tmpbuf);
1281}
1282
1283bool
84afefe6 1284Perl_is_uni_graph(pTHX_ UV c)
b8c5462f 1285{
89ebb4a3 1286 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1287 uvchr_to_utf8(tmpbuf, c);
b8c5462f
JH
1288 return is_utf8_graph(tmpbuf);
1289}
1290
1291bool
84afefe6 1292Perl_is_uni_print(pTHX_ UV c)
a0ed51b3 1293{
89ebb4a3 1294 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1295 uvchr_to_utf8(tmpbuf, c);
a0ed51b3
LW
1296 return is_utf8_print(tmpbuf);
1297}
1298
b8c5462f 1299bool
84afefe6 1300Perl_is_uni_punct(pTHX_ UV c)
b8c5462f 1301{
89ebb4a3 1302 U8 tmpbuf[UTF8_MAXBYTES+1];
230880c1 1303 uvchr_to_utf8(tmpbuf, c);
b8c5462f
JH
1304 return is_utf8_punct(tmpbuf);
1305}
1306
4d61ec05 1307bool
84afefe6 1308Perl_is_uni_xdigit(pTHX_ UV c)
4d61ec05 1309{
89ebb4a3 1310 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
230880c1 1311 uvchr_to_utf8(tmpbuf, c);
4d61ec05
GS
1312 return is_utf8_xdigit(tmpbuf);
1313}
1314
84afefe6
JH
1315UV
1316Perl_to_uni_upper(pTHX_ UV c, U8* p, STRLEN *lenp)
a0ed51b3 1317{
7918f24d
NC
1318 PERL_ARGS_ASSERT_TO_UNI_UPPER;
1319
0ebc6274
JH
1320 uvchr_to_utf8(p, c);
1321 return to_utf8_upper(p, p, lenp);
a0ed51b3
LW
1322}
1323
84afefe6
JH
1324UV
1325Perl_to_uni_title(pTHX_ UV c, U8* p, STRLEN *lenp)
a0ed51b3 1326{
7918f24d
NC
1327 PERL_ARGS_ASSERT_TO_UNI_TITLE;
1328
0ebc6274
JH
1329 uvchr_to_utf8(p, c);
1330 return to_utf8_title(p, p, lenp);
a0ed51b3
LW
1331}
1332
84afefe6
JH
1333UV
1334Perl_to_uni_lower(pTHX_ UV c, U8* p, STRLEN *lenp)
a0ed51b3 1335{
7918f24d
NC
1336 PERL_ARGS_ASSERT_TO_UNI_LOWER;
1337
0ebc6274
JH
1338 uvchr_to_utf8(p, c);
1339 return to_utf8_lower(p, p, lenp);
a0ed51b3
LW
1340}
1341
84afefe6
JH
1342UV
1343Perl_to_uni_fold(pTHX_ UV c, U8* p, STRLEN *lenp)
1344{
7918f24d
NC
1345 PERL_ARGS_ASSERT_TO_UNI_FOLD;
1346
0ebc6274
JH
1347 uvchr_to_utf8(p, c);
1348 return to_utf8_fold(p, p, lenp);
84afefe6
JH
1349}
1350
a0ed51b3
LW
1351/* for now these all assume no locale info available for Unicode > 255 */
1352
1353bool
84afefe6 1354Perl_is_uni_alnum_lc(pTHX_ UV c)
a0ed51b3
LW
1355{
1356 return is_uni_alnum(c); /* XXX no locale support yet */
1357}
1358
1359bool
84afefe6 1360Perl_is_uni_idfirst_lc(pTHX_ UV c)
a0ed51b3
LW
1361{
1362 return is_uni_idfirst(c); /* XXX no locale support yet */
1363}
1364
1365bool
84afefe6 1366Perl_is_uni_alpha_lc(pTHX_ UV c)
a0ed51b3
LW
1367{
1368 return is_uni_alpha(c); /* XXX no locale support yet */
1369}
1370
1371bool
84afefe6 1372Perl_is_uni_ascii_lc(pTHX_ UV c)
4d61ec05
GS
1373{
1374 return is_uni_ascii(c); /* XXX no locale support yet */
1375}
1376
1377bool
84afefe6 1378Perl_is_uni_space_lc(pTHX_ UV c)
a0ed51b3
LW
1379{
1380 return is_uni_space(c); /* XXX no locale support yet */
1381}
1382
1383bool
84afefe6 1384Perl_is_uni_digit_lc(pTHX_ UV c)
a0ed51b3
LW
1385{
1386 return is_uni_digit(c); /* XXX no locale support yet */
1387}
1388
1389bool
84afefe6 1390Perl_is_uni_upper_lc(pTHX_ UV c)
a0ed51b3
LW
1391{
1392 return is_uni_upper(c); /* XXX no locale support yet */
1393}
1394
1395bool
84afefe6 1396Perl_is_uni_lower_lc(pTHX_ UV c)
a0ed51b3
LW
1397{
1398 return is_uni_lower(c); /* XXX no locale support yet */
1399}
1400
1401bool
84afefe6 1402Perl_is_uni_cntrl_lc(pTHX_ UV c)
b8c5462f
JH
1403{
1404 return is_uni_cntrl(c); /* XXX no locale support yet */
1405}
1406
1407bool
84afefe6 1408Perl_is_uni_graph_lc(pTHX_ UV c)
b8c5462f
JH
1409{
1410 return is_uni_graph(c); /* XXX no locale support yet */
1411}
1412
1413bool
84afefe6 1414Perl_is_uni_print_lc(pTHX_ UV c)
a0ed51b3
LW
1415{
1416 return is_uni_print(c); /* XXX no locale support yet */
1417}
1418
b8c5462f 1419bool
84afefe6 1420Perl_is_uni_punct_lc(pTHX_ UV c)
b8c5462f
JH
1421{
1422 return is_uni_punct(c); /* XXX no locale support yet */
1423}
1424
4d61ec05 1425bool
84afefe6 1426Perl_is_uni_xdigit_lc(pTHX_ UV c)
4d61ec05
GS
1427{
1428 return is_uni_xdigit(c); /* XXX no locale support yet */
1429}
1430
b7ac61fa
JH
1431U32
1432Perl_to_uni_upper_lc(pTHX_ U32 c)
1433{
ee099d14
JH
1434 /* XXX returns only the first character -- do not use XXX */
1435 /* XXX no locale support yet */
1436 STRLEN len;
89ebb4a3 1437 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
ee099d14 1438 return (U32)to_uni_upper(c, tmpbuf, &len);
b7ac61fa
JH
1439}
1440
1441U32
1442Perl_to_uni_title_lc(pTHX_ U32 c)
1443{
ee099d14
JH
1444 /* XXX returns only the first character XXX -- do not use XXX */
1445 /* XXX no locale support yet */
1446 STRLEN len;
89ebb4a3 1447 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
ee099d14 1448 return (U32)to_uni_title(c, tmpbuf, &len);
b7ac61fa
JH
1449}
1450
1451U32
1452Perl_to_uni_lower_lc(pTHX_ U32 c)
1453{
ee099d14
JH
1454 /* XXX returns only the first character -- do not use XXX */
1455 /* XXX no locale support yet */
1456 STRLEN len;
89ebb4a3 1457 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
ee099d14 1458 return (U32)to_uni_lower(c, tmpbuf, &len);
b7ac61fa
JH
1459}
1460
7452cf6a 1461static bool
5141f98e 1462S_is_utf8_common(pTHX_ const U8 *const p, SV **swash,
bde6a22d
NC
1463 const char *const swashname)
1464{
97aff369 1465 dVAR;
7918f24d
NC
1466
1467 PERL_ARGS_ASSERT_IS_UTF8_COMMON;
1468
bde6a22d
NC
1469 if (!is_utf8_char(p))
1470 return FALSE;
1471 if (!*swash)
711a919c 1472 *swash = swash_init("utf8", swashname, &PL_sv_undef, 1, 0);
bde6a22d
NC
1473 return swash_fetch(*swash, p, TRUE) != 0;
1474}
1475
1476bool
7fc63493 1477Perl_is_utf8_alnum(pTHX_ const U8 *p)
a0ed51b3 1478{
97aff369 1479 dVAR;
7918f24d
NC
1480
1481 PERL_ARGS_ASSERT_IS_UTF8_ALNUM;
1482
671c33bf
NC
1483 /* NOTE: "IsWord", not "IsAlnum", since Alnum is a true
1484 * descendant of isalnum(3), in other words, it doesn't
1485 * contain the '_'. --jhi */
d4c19fe8 1486 return is_utf8_common(p, &PL_utf8_alnum, "IsWord");
a0ed51b3
LW
1487}
1488
1489bool
7fc63493 1490Perl_is_utf8_idfirst(pTHX_ const U8 *p) /* The naming is historical. */
a0ed51b3 1491{
97aff369 1492 dVAR;
7918f24d
NC
1493
1494 PERL_ARGS_ASSERT_IS_UTF8_IDFIRST;
1495
82686b01
JH
1496 if (*p == '_')
1497 return TRUE;
bde6a22d 1498 /* is_utf8_idstart would be more logical. */
d4c19fe8 1499 return is_utf8_common(p, &PL_utf8_idstart, "IdStart");
82686b01
JH
1500}
1501
1502bool
7fc63493 1503Perl_is_utf8_idcont(pTHX_ const U8 *p)
82686b01 1504{
97aff369 1505 dVAR;
7918f24d
NC
1506
1507 PERL_ARGS_ASSERT_IS_UTF8_IDCONT;
1508
82686b01
JH
1509 if (*p == '_')
1510 return TRUE;
d4c19fe8 1511 return is_utf8_common(p, &PL_utf8_idcont, "IdContinue");
a0ed51b3
LW
1512}
1513
1514bool
7fc63493 1515Perl_is_utf8_alpha(pTHX_ const U8 *p)
a0ed51b3 1516{
97aff369 1517 dVAR;
7918f24d
NC
1518
1519 PERL_ARGS_ASSERT_IS_UTF8_ALPHA;
1520
d4c19fe8 1521 return is_utf8_common(p, &PL_utf8_alpha, "IsAlpha");
a0ed51b3
LW
1522}
1523
1524bool
7fc63493 1525Perl_is_utf8_ascii(pTHX_ const U8 *p)
b8c5462f 1526{
97aff369 1527 dVAR;
7918f24d
NC
1528
1529 PERL_ARGS_ASSERT_IS_UTF8_ASCII;
1530
d4c19fe8 1531 return is_utf8_common(p, &PL_utf8_ascii, "IsAscii");
b8c5462f
JH
1532}
1533
1534bool
7fc63493 1535Perl_is_utf8_space(pTHX_ const U8 *p)
a0ed51b3 1536{
97aff369 1537 dVAR;
7918f24d
NC
1538
1539 PERL_ARGS_ASSERT_IS_UTF8_SPACE;
1540
d4c19fe8 1541 return is_utf8_common(p, &PL_utf8_space, "IsSpacePerl");
a0ed51b3
LW
1542}
1543
1544bool
d1eb3177
YO
1545Perl_is_utf8_perl_space(pTHX_ const U8 *p)
1546{
1547 dVAR;
1548
1549 PERL_ARGS_ASSERT_IS_UTF8_PERL_SPACE;
1550
1551 return is_utf8_common(p, &PL_utf8_perl_space, "IsPerlSpace");
1552}
1553
1554bool
1555Perl_is_utf8_perl_word(pTHX_ const U8 *p)
1556{
1557 dVAR;
1558
1559 PERL_ARGS_ASSERT_IS_UTF8_PERL_WORD;
1560
1561 return is_utf8_common(p, &PL_utf8_perl_word, "IsPerlWord");
1562}
1563
1564bool
7fc63493 1565Perl_is_utf8_digit(pTHX_ const U8 *p)
a0ed51b3 1566{
97aff369 1567 dVAR;
7918f24d
NC
1568
1569 PERL_ARGS_ASSERT_IS_UTF8_DIGIT;
1570
d4c19fe8 1571 return is_utf8_common(p, &PL_utf8_digit, "IsDigit");
a0ed51b3
LW
1572}
1573
1574bool
d1eb3177
YO
1575Perl_is_utf8_posix_digit(pTHX_ const U8 *p)
1576{
1577 dVAR;
1578
1579 PERL_ARGS_ASSERT_IS_UTF8_POSIX_DIGIT;
1580
1581 return is_utf8_common(p, &PL_utf8_posix_digit, "IsPosixDigit");
1582}
1583
1584bool
7fc63493 1585Perl_is_utf8_upper(pTHX_ const U8 *p)
a0ed51b3 1586{
97aff369 1587 dVAR;
7918f24d
NC
1588
1589 PERL_ARGS_ASSERT_IS_UTF8_UPPER;
1590
d4c19fe8 1591 return is_utf8_common(p, &PL_utf8_upper, "IsUppercase");
a0ed51b3
LW
1592}
1593
1594bool
7fc63493 1595Perl_is_utf8_lower(pTHX_ const U8 *p)
a0ed51b3 1596{
97aff369 1597 dVAR;
7918f24d
NC
1598
1599 PERL_ARGS_ASSERT_IS_UTF8_LOWER;
1600
d4c19fe8 1601 return is_utf8_common(p, &PL_utf8_lower, "IsLowercase");
a0ed51b3
LW
1602}
1603
1604bool
7fc63493 1605Perl_is_utf8_cntrl(pTHX_ const U8 *p)
b8c5462f 1606{
97aff369 1607 dVAR;
7918f24d
NC
1608
1609 PERL_ARGS_ASSERT_IS_UTF8_CNTRL;
1610
d4c19fe8 1611 return is_utf8_common(p, &PL_utf8_cntrl, "IsCntrl");
b8c5462f
JH
1612}
1613
1614bool
7fc63493 1615Perl_is_utf8_graph(pTHX_ const U8 *p)
b8c5462f 1616{
97aff369 1617 dVAR;
7918f24d
NC
1618
1619 PERL_ARGS_ASSERT_IS_UTF8_GRAPH;
1620
d4c19fe8 1621 return is_utf8_common(p, &PL_utf8_graph, "IsGraph");
b8c5462f
JH
1622}
1623
1624bool
7fc63493 1625Perl_is_utf8_print(pTHX_ const U8 *p)
a0ed51b3 1626{
97aff369 1627 dVAR;
7918f24d
NC
1628
1629 PERL_ARGS_ASSERT_IS_UTF8_PRINT;
1630
d4c19fe8 1631 return is_utf8_common(p, &PL_utf8_print, "IsPrint");
a0ed51b3
LW
1632}
1633
1634bool
7fc63493 1635Perl_is_utf8_punct(pTHX_ const U8 *p)
b8c5462f 1636{
97aff369 1637 dVAR;
7918f24d
NC
1638
1639 PERL_ARGS_ASSERT_IS_UTF8_PUNCT;
1640
d4c19fe8 1641 return is_utf8_common(p, &PL_utf8_punct, "IsPunct");
b8c5462f
JH
1642}
1643
1644bool
7fc63493 1645Perl_is_utf8_xdigit(pTHX_ const U8 *p)
b8c5462f 1646{
97aff369 1647 dVAR;
7918f24d
NC
1648
1649 PERL_ARGS_ASSERT_IS_UTF8_XDIGIT;
1650
d1eb3177 1651 return is_utf8_common(p, &PL_utf8_xdigit, "IsXDigit");
b8c5462f
JH
1652}
1653
1654bool
7fc63493 1655Perl_is_utf8_mark(pTHX_ const U8 *p)
a0ed51b3 1656{
97aff369 1657 dVAR;
7918f24d
NC
1658
1659 PERL_ARGS_ASSERT_IS_UTF8_MARK;
1660
d4c19fe8 1661 return is_utf8_common(p, &PL_utf8_mark, "IsM");
a0ed51b3
LW
1662}
1663
37e2e78e
KW
1664bool
1665Perl_is_utf8_X_begin(pTHX_ const U8 *p)
1666{
1667 dVAR;
1668
1669 PERL_ARGS_ASSERT_IS_UTF8_X_BEGIN;
1670
1671 return is_utf8_common(p, &PL_utf8_X_begin, "_X_Begin");
1672}
1673
1674bool
1675Perl_is_utf8_X_extend(pTHX_ const U8 *p)
1676{
1677 dVAR;
1678
1679 PERL_ARGS_ASSERT_IS_UTF8_X_EXTEND;
1680
1681 return is_utf8_common(p, &PL_utf8_X_extend, "_X_Extend");
1682}
1683
1684bool
1685Perl_is_utf8_X_prepend(pTHX_ const U8 *p)
1686{
1687 dVAR;
1688
1689 PERL_ARGS_ASSERT_IS_UTF8_X_PREPEND;
1690
1691 return is_utf8_common(p, &PL_utf8_X_prepend, "GCB=Prepend");
1692}
1693
1694bool
1695Perl_is_utf8_X_non_hangul(pTHX_ const U8 *p)
1696{
1697 dVAR;
1698
1699 PERL_ARGS_ASSERT_IS_UTF8_X_NON_HANGUL;
1700
1701 return is_utf8_common(p, &PL_utf8_X_non_hangul, "HST=Not_Applicable");
1702}
1703
1704bool
1705Perl_is_utf8_X_L(pTHX_ const U8 *p)
1706{
1707 dVAR;
1708
1709 PERL_ARGS_ASSERT_IS_UTF8_X_L;
1710
1711 return is_utf8_common(p, &PL_utf8_X_L, "GCB=L");
1712}
1713
1714bool
1715Perl_is_utf8_X_LV(pTHX_ const U8 *p)
1716{
1717 dVAR;
1718
1719 PERL_ARGS_ASSERT_IS_UTF8_X_LV;
1720
1721 return is_utf8_common(p, &PL_utf8_X_LV, "GCB=LV");
1722}
1723
1724bool
1725Perl_is_utf8_X_LVT(pTHX_ const U8 *p)
1726{
1727 dVAR;
1728
1729 PERL_ARGS_ASSERT_IS_UTF8_X_LVT;
1730
1731 return is_utf8_common(p, &PL_utf8_X_LVT, "GCB=LVT");
1732}
1733
1734bool
1735Perl_is_utf8_X_T(pTHX_ const U8 *p)
1736{
1737 dVAR;
1738
1739 PERL_ARGS_ASSERT_IS_UTF8_X_T;
1740
1741 return is_utf8_common(p, &PL_utf8_X_T, "GCB=T");
1742}
1743
1744bool
1745Perl_is_utf8_X_V(pTHX_ const U8 *p)
1746{
1747 dVAR;
1748
1749 PERL_ARGS_ASSERT_IS_UTF8_X_V;
1750
1751 return is_utf8_common(p, &PL_utf8_X_V, "GCB=V");
1752}
1753
1754bool
1755Perl_is_utf8_X_LV_LVT_V(pTHX_ const U8 *p)
1756{
1757 dVAR;
1758
1759 PERL_ARGS_ASSERT_IS_UTF8_X_LV_LVT_V;
1760
1761 return is_utf8_common(p, &PL_utf8_X_LV_LVT_V, "_X_LV_LVT_V");
1762}
1763
6b5c0936 1764/*
87cea99e 1765=for apidoc to_utf8_case
6b5c0936
JH
1766
1767The "p" contains the pointer to the UTF-8 string encoding
1768the character that is being converted.
1769
1770The "ustrp" is a pointer to the character buffer to put the
1771conversion result to. The "lenp" is a pointer to the length
1772of the result.
1773
0134edef 1774The "swashp" is a pointer to the swash to use.
6b5c0936 1775
0134edef 1776Both the special and normal mappings are stored lib/unicore/To/Foo.pl,
8fe4d5b2 1777and loaded by SWASHNEW, using lib/utf8_heavy.pl. The special (usually,
0134edef 1778but not always, a multicharacter mapping), is tried first.
6b5c0936 1779
0134edef
JH
1780The "special" is a string like "utf8::ToSpecLower", which means the
1781hash %utf8::ToSpecLower. The access to the hash is through
1782Perl_to_utf8_case().
6b5c0936 1783
0134edef
JH
1784The "normal" is a string like "ToLower" which means the swash
1785%utf8::ToLower.
1786
1787=cut */
6b5c0936 1788
2104c8d9 1789UV
9a957fbc
AL
1790Perl_to_utf8_case(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp,
1791 SV **swashp, const char *normal, const char *special)
a0ed51b3 1792{
97aff369 1793 dVAR;
89ebb4a3 1794 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
0134edef 1795 STRLEN len = 0;
aec46f14 1796 const UV uv0 = utf8_to_uvchr(p, NULL);
1feea2c7
JH
1797 /* The NATIVE_TO_UNI() and UNI_TO_NATIVE() mappings
1798 * are necessary in EBCDIC, they are redundant no-ops
1799 * in ASCII-ish platforms, and hopefully optimized away. */
f54cb97a 1800 const UV uv1 = NATIVE_TO_UNI(uv0);
7918f24d
NC
1801
1802 PERL_ARGS_ASSERT_TO_UTF8_CASE;
1803
1feea2c7 1804 uvuni_to_utf8(tmpbuf, uv1);
0134edef
JH
1805
1806 if (!*swashp) /* load on-demand */
1807 *swashp = swash_init("utf8", normal, &PL_sv_undef, 4, 0);
37e2e78e
KW
1808 /* This is the beginnings of a skeleton of code to read the info section
1809 * that is in all the swashes in case we ever want to do that, so one can
1810 * read things whose maps aren't code points, and whose default if missing
1811 * is not to the code point itself. This was just to see if it actually
1812 * worked. Details on what the possibilities are are in perluniprops.pod
1813 HV * const hv = get_hv("utf8::SwashInfo", 0);
1814 if (hv) {
1815 SV **svp;
1816 svp = hv_fetch(hv, (const char*)normal, strlen(normal), FALSE);
1817 const char *s;
1818
1819 HV * const this_hash = SvRV(*svp);
1820 svp = hv_fetch(this_hash, "type", strlen("type"), FALSE);
1821 s = SvPV_const(*svp, len);
1822 }
1823 }*/
0134edef 1824
a6f87d8c 1825 if (special) {
0134edef 1826 /* It might be "special" (sometimes, but not always,
2a37f04d 1827 * a multicharacter mapping) */
6673a63c 1828 HV * const hv = get_hv(special, 0);
b08cf34e
JH
1829 SV **svp;
1830
35da51f7 1831 if (hv &&
b08cf34e
JH
1832 (svp = hv_fetch(hv, (const char*)tmpbuf, UNISKIP(uv1), FALSE)) &&
1833 (*svp)) {
cfd0369c 1834 const char *s;
47654450 1835
cfd0369c 1836 s = SvPV_const(*svp, len);
47654450
JH
1837 if (len == 1)
1838 len = uvuni_to_utf8(ustrp, NATIVE_TO_UNI(*(U8*)s)) - ustrp;
2a37f04d 1839 else {
2f9475ad
JH
1840#ifdef EBCDIC
1841 /* If we have EBCDIC we need to remap the characters
1842 * since any characters in the low 256 are Unicode
1843 * code points, not EBCDIC. */
7cda7a3d 1844 U8 *t = (U8*)s, *tend = t + len, *d;
2f9475ad
JH
1845
1846 d = tmpbuf;
b08cf34e 1847 if (SvUTF8(*svp)) {
2f9475ad
JH
1848 STRLEN tlen = 0;
1849
1850 while (t < tend) {
d4c19fe8 1851 const UV c = utf8_to_uvchr(t, &tlen);
2f9475ad
JH
1852 if (tlen > 0) {
1853 d = uvchr_to_utf8(d, UNI_TO_NATIVE(c));
1854 t += tlen;
1855 }
1856 else
1857 break;
1858 }
1859 }
1860 else {
36fec512
JH
1861 while (t < tend) {
1862 d = uvchr_to_utf8(d, UNI_TO_NATIVE(*t));
1863 t++;
1864 }
2f9475ad
JH
1865 }
1866 len = d - tmpbuf;
1867 Copy(tmpbuf, ustrp, len, U8);
1868#else
d2dcd0fb 1869 Copy(s, ustrp, len, U8);
2f9475ad 1870#endif
29e98929 1871 }
983ffd37 1872 }
0134edef
JH
1873 }
1874
1875 if (!len && *swashp) {
d4c19fe8
AL
1876 const UV uv2 = swash_fetch(*swashp, tmpbuf, TRUE);
1877
0134edef
JH
1878 if (uv2) {
1879 /* It was "normal" (a single character mapping). */
d4c19fe8 1880 const UV uv3 = UNI_TO_NATIVE(uv2);
e9101d72 1881 len = uvchr_to_utf8(ustrp, uv3) - ustrp;
2a37f04d
JH
1882 }
1883 }
1feea2c7 1884
37e2e78e
KW
1885 if (!len) /* Neither: just copy. In other words, there was no mapping
1886 defined, which means that the code point maps to itself */
0134edef
JH
1887 len = uvchr_to_utf8(ustrp, uv0) - ustrp;
1888
2a37f04d
JH
1889 if (lenp)
1890 *lenp = len;
1891
0134edef 1892 return len ? utf8_to_uvchr(ustrp, 0) : 0;
a0ed51b3
LW
1893}
1894
d3e79532 1895/*
87cea99e 1896=for apidoc to_utf8_upper
d3e79532
JH
1897
1898Convert the UTF-8 encoded character at p to its uppercase version and
1899store that in UTF-8 in ustrp and its length in bytes in lenp. Note
89ebb4a3
JH
1900that the ustrp needs to be at least UTF8_MAXBYTES_CASE+1 bytes since
1901the uppercase version may be longer than the original character.
d3e79532
JH
1902
1903The first character of the uppercased version is returned
1904(but note, as explained above, that there may be more.)
1905
1906=cut */
1907
2104c8d9 1908UV
7fc63493 1909Perl_to_utf8_upper(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp)
a0ed51b3 1910{
97aff369 1911 dVAR;
7918f24d
NC
1912
1913 PERL_ARGS_ASSERT_TO_UTF8_UPPER;
1914
983ffd37 1915 return Perl_to_utf8_case(aTHX_ p, ustrp, lenp,
b4e400f9 1916 &PL_utf8_toupper, "ToUpper", "utf8::ToSpecUpper");
983ffd37 1917}
a0ed51b3 1918
d3e79532 1919/*
87cea99e 1920=for apidoc to_utf8_title
d3e79532
JH
1921
1922Convert the UTF-8 encoded character at p to its titlecase version and
1923store that in UTF-8 in ustrp and its length in bytes in lenp. Note
89ebb4a3
JH
1924that the ustrp needs to be at least UTF8_MAXBYTES_CASE+1 bytes since the
1925titlecase version may be longer than the original character.
d3e79532
JH
1926
1927The first character of the titlecased version is returned
1928(but note, as explained above, that there may be more.)
1929
1930=cut */
1931
983ffd37 1932UV
7fc63493 1933Perl_to_utf8_title(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp)
983ffd37 1934{
97aff369 1935 dVAR;
7918f24d
NC
1936
1937 PERL_ARGS_ASSERT_TO_UTF8_TITLE;
1938
983ffd37 1939 return Perl_to_utf8_case(aTHX_ p, ustrp, lenp,
b4e400f9 1940 &PL_utf8_totitle, "ToTitle", "utf8::ToSpecTitle");
a0ed51b3
LW
1941}
1942
d3e79532 1943/*
87cea99e 1944=for apidoc to_utf8_lower
d3e79532
JH
1945
1946Convert the UTF-8 encoded character at p to its lowercase version and
1947store that in UTF-8 in ustrp and its length in bytes in lenp. Note
89ebb4a3
JH
1948that the ustrp needs to be at least UTF8_MAXBYTES_CASE+1 bytes since the
1949lowercase version may be longer than the original character.
d3e79532
JH
1950
1951The first character of the lowercased version is returned
1952(but note, as explained above, that there may be more.)
1953
1954=cut */
1955
2104c8d9 1956UV
7fc63493 1957Perl_to_utf8_lower(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp)
a0ed51b3 1958{
97aff369 1959 dVAR;
7918f24d
NC
1960
1961 PERL_ARGS_ASSERT_TO_UTF8_LOWER;
1962
983ffd37 1963 return Perl_to_utf8_case(aTHX_ p, ustrp, lenp,
b4e400f9
JH
1964 &PL_utf8_tolower, "ToLower", "utf8::ToSpecLower");
1965}
1966
d3e79532 1967/*
87cea99e 1968=for apidoc to_utf8_fold
d3e79532
JH
1969
1970Convert the UTF-8 encoded character at p to its foldcase version and
1971store that in UTF-8 in ustrp and its length in bytes in lenp. Note
89ebb4a3 1972that the ustrp needs to be at least UTF8_MAXBYTES_CASE+1 bytes since the
d3e79532
JH
1973foldcase version may be longer than the original character (up to
1974three characters).
1975
1976The first character of the foldcased version is returned
1977(but note, as explained above, that there may be more.)
1978
1979=cut */
1980
b4e400f9 1981UV
7fc63493 1982Perl_to_utf8_fold(pTHX_ const U8 *p, U8* ustrp, STRLEN *lenp)
b4e400f9 1983{
97aff369 1984 dVAR;
7918f24d
NC
1985
1986 PERL_ARGS_ASSERT_TO_UTF8_FOLD;
1987
b4e400f9
JH
1988 return Perl_to_utf8_case(aTHX_ p, ustrp, lenp,
1989 &PL_utf8_tofold, "ToFold", "utf8::ToSpecFold");
a0ed51b3
LW
1990}
1991
711a919c
ST
1992/* Note:
1993 * A "swash" is a swatch hash.
1994 * A "swatch" is a bit vector generated by utf8.c:S_swash_get().
1995 * C<pkg> is a pointer to a package name for SWASHNEW, should be "utf8".
1996 * For other parameters, see utf8::SWASHNEW in lib/utf8_heavy.pl.
1997 */
a0ed51b3 1998SV*
7fc63493 1999Perl_swash_init(pTHX_ const char* pkg, const char* name, SV *listsv, I32 minbits, I32 none)
a0ed51b3 2000{
27da23d5 2001 dVAR;
a0ed51b3 2002 SV* retval;
8e84507e 2003 dSP;
7fc63493
AL
2004 const size_t pkg_len = strlen(pkg);
2005 const size_t name_len = strlen(name);
da51bb9b 2006 HV * const stash = gv_stashpvn(pkg, pkg_len, 0);
f8be5cf0 2007 SV* errsv_save;
88c57d91 2008 GV *method;
ce3b816e 2009
7918f24d
NC
2010 PERL_ARGS_ASSERT_SWASH_INIT;
2011
96ca9f55
DM
2012 PUSHSTACKi(PERLSI_MAGIC);
2013 ENTER;
ec34a119 2014 SAVEHINTS();
96ca9f55 2015 save_re_context();
88c57d91
NC
2016 method = gv_fetchmeth(stash, "SWASHNEW", 8, -1);
2017 if (!method) { /* demand load utf8 */
ce3b816e 2018 ENTER;
f8be5cf0 2019 errsv_save = newSVsv(ERRSV);
dc0c6abb
NC
2020 /* It is assumed that callers of this routine are not passing in any
2021 user derived data. */
2022 /* Need to do this after save_re_context() as it will set PL_tainted to
2023 1 while saving $1 etc (see the code after getrx: in Perl_magic_get).
2024 Even line to create errsv_save can turn on PL_tainted. */
2025 SAVEBOOL(PL_tainted);
2026 PL_tainted = 0;
71bed85a 2027 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvn(pkg,pkg_len),
a0714e2c 2028 NULL);
f8be5cf0
JH
2029 if (!SvTRUE(ERRSV))
2030 sv_setsv(ERRSV, errsv_save);
2031 SvREFCNT_dec(errsv_save);
ce3b816e
GS
2032 LEAVE;
2033 }
2034 SPAGAIN;
a0ed51b3
LW
2035 PUSHMARK(SP);
2036 EXTEND(SP,5);
6e449a3a
MHM
2037 mPUSHp(pkg, pkg_len);
2038 mPUSHp(name, name_len);
a0ed51b3 2039 PUSHs(listsv);
6e449a3a
MHM
2040 mPUSHi(minbits);
2041 mPUSHi(none);
a0ed51b3 2042 PUTBACK;
f8be5cf0 2043 errsv_save = newSVsv(ERRSV);
88c57d91
NC
2044 /* If we already have a pointer to the method, no need to use call_method()
2045 to repeat the lookup. */
2046 if (method ? call_sv(MUTABLE_SV(method), G_SCALAR)
8b563eef 2047 : call_sv(newSVpvs_flags("SWASHNEW", SVs_TEMP), G_SCALAR | G_METHOD))
8e84507e 2048 retval = newSVsv(*PL_stack_sp--);
a0ed51b3 2049 else
e24b16f9 2050 retval = &PL_sv_undef;
f8be5cf0
JH
2051 if (!SvTRUE(ERRSV))
2052 sv_setsv(ERRSV, errsv_save);
2053 SvREFCNT_dec(errsv_save);
a0ed51b3
LW
2054 LEAVE;
2055 POPSTACK;
923e4eb5 2056 if (IN_PERL_COMPILETIME) {
623e6609 2057 CopHINTS_set(PL_curcop, PL_hints);
a0ed51b3 2058 }
bc45ce41
JH
2059 if (!SvROK(retval) || SvTYPE(SvRV(retval)) != SVt_PVHV) {
2060 if (SvPOK(retval))
35c1215d 2061 Perl_croak(aTHX_ "Can't find Unicode property definition \"%"SVf"\"",
be2597df 2062 SVfARG(retval));
cea2e8a9 2063 Perl_croak(aTHX_ "SWASHNEW didn't return an HV ref");
bc45ce41 2064 }
a0ed51b3
LW
2065 return retval;
2066}
2067
035d37be
JH
2068
2069/* This API is wrong for special case conversions since we may need to
2070 * return several Unicode characters for a single Unicode character
2071 * (see lib/unicore/SpecCase.txt) The SWASHGET in lib/utf8_heavy.pl is
2072 * the lower-level routine, and it is similarly broken for returning
38684baa
KW
2073 * multiple values. --jhi
2074 * For those, you should use to_utf8_case() instead */
979f2922 2075/* Now SWASHGET is recasted into S_swash_get in this file. */
680c470c
ST
2076
2077/* Note:
2078 * Returns the value of property/mapping C<swash> for the first character
2079 * of the string C<ptr>. If C<do_utf8> is true, the string C<ptr> is
2080 * assumed to be in utf8. If C<do_utf8> is false, the string C<ptr> is
2081 * assumed to be in native 8-bit encoding. Caches the swatch in C<swash>.
2082 */
a0ed51b3 2083UV
680c470c 2084Perl_swash_fetch(pTHX_ SV *swash, const U8 *ptr, bool do_utf8)
a0ed51b3 2085{
27da23d5 2086 dVAR;
ef8f7699 2087 HV *const hv = MUTABLE_HV(SvRV(swash));
3568d838
JH
2088 U32 klen;
2089 U32 off;
a0ed51b3 2090 STRLEN slen;
7d85a32c 2091 STRLEN needents;
cfd0369c 2092 const U8 *tmps = NULL;
a0ed51b3 2093 U32 bit;
979f2922 2094 SV *swatch;
3568d838 2095 U8 tmputf8[2];
35da51f7 2096 const UV c = NATIVE_TO_ASCII(*ptr);
3568d838 2097
7918f24d
NC
2098 PERL_ARGS_ASSERT_SWASH_FETCH;
2099
3568d838 2100 if (!do_utf8 && !UNI_IS_INVARIANT(c)) {
979f2922
ST
2101 tmputf8[0] = (U8)UTF8_EIGHT_BIT_HI(c);
2102 tmputf8[1] = (U8)UTF8_EIGHT_BIT_LO(c);
2103 ptr = tmputf8;
3568d838
JH
2104 }
2105 /* Given a UTF-X encoded char 0xAA..0xYY,0xZZ
37e2e78e 2106 * then the "swatch" is a vec() for all the chars which start
3568d838
JH
2107 * with 0xAA..0xYY
2108 * So the key in the hash (klen) is length of encoded char -1
2109 */
2110 klen = UTF8SKIP(ptr) - 1;
2111 off = ptr[klen];
a0ed51b3 2112
979f2922 2113 if (klen == 0) {
37e2e78e 2114 /* If char is invariant then swatch is for all the invariant chars
1e54db1a 2115 * In both UTF-8 and UTF-8-MOD that happens to be UTF_CONTINUATION_MARK
7d85a32c 2116 */
979f2922
ST
2117 needents = UTF_CONTINUATION_MARK;
2118 off = NATIVE_TO_UTF(ptr[klen]);
2119 }
2120 else {
7d85a32c 2121 /* If char is encoded then swatch is for the prefix */
979f2922
ST
2122 needents = (1 << UTF_ACCUMULATION_SHIFT);
2123 off = NATIVE_TO_UTF(ptr[klen]) & UTF_CONTINUATION_MASK;
2124 }
7d85a32c 2125
a0ed51b3
LW
2126 /*
2127 * This single-entry cache saves about 1/3 of the utf8 overhead in test
2128 * suite. (That is, only 7-8% overall over just a hash cache. Still,
2129 * it's nothing to sniff at.) Pity we usually come through at least
2130 * two function calls to get here...
2131 *
2132 * NB: this code assumes that swatches are never modified, once generated!
2133 */
2134
3568d838 2135 if (hv == PL_last_swash_hv &&
a0ed51b3 2136 klen == PL_last_swash_klen &&
27da23d5 2137 (!klen || memEQ((char *)ptr, (char *)PL_last_swash_key, klen)) )
a0ed51b3
LW
2138 {
2139 tmps = PL_last_swash_tmps;
2140 slen = PL_last_swash_slen;
2141 }
2142 else {
2143 /* Try our second-level swatch cache, kept in a hash. */
e1ec3a88 2144 SV** svp = hv_fetch(hv, (const char*)ptr, klen, FALSE);
a0ed51b3 2145
979f2922
ST
2146 /* If not cached, generate it via swash_get */
2147 if (!svp || !SvPOK(*svp)
2148 || !(tmps = (const U8*)SvPV_const(*svp, slen))) {
2b9d42f0
NIS
2149 /* We use utf8n_to_uvuni() as we want an index into
2150 Unicode tables, not a native character number.
2151 */
aec46f14 2152 const UV code_point = utf8n_to_uvuni(ptr, UTF8_MAXBYTES, 0,
872c91ae
JH
2153 ckWARN(WARN_UTF8) ?
2154 0 : UTF8_ALLOW_ANY);
680c470c 2155 swatch = swash_get(swash,
979f2922
ST
2156 /* On EBCDIC & ~(0xA0-1) isn't a useful thing to do */
2157 (klen) ? (code_point & ~(needents - 1)) : 0,
2158 needents);
2159
923e4eb5 2160 if (IN_PERL_COMPILETIME)
623e6609 2161 CopHINTS_set(PL_curcop, PL_hints);
a0ed51b3 2162
979f2922 2163 svp = hv_store(hv, (const char *)ptr, klen, swatch, 0);
a0ed51b3 2164
979f2922
ST
2165 if (!svp || !(tmps = (U8*)SvPV(*svp, slen))
2166 || (slen << 3) < needents)
660a4616 2167 Perl_croak(aTHX_ "panic: swash_fetch got improper swatch");
a0ed51b3
LW
2168 }
2169
2170 PL_last_swash_hv = hv;
16d8f38a 2171 assert(klen <= sizeof(PL_last_swash_key));
eac04b2e 2172 PL_last_swash_klen = (U8)klen;
cfd0369c
NC
2173 /* FIXME change interpvar.h? */
2174 PL_last_swash_tmps = (U8 *) tmps;
a0ed51b3
LW
2175 PL_last_swash_slen = slen;
2176 if (klen)
2177 Copy(ptr, PL_last_swash_key, klen, U8);
2178 }
2179
9faf8d75 2180 switch ((int)((slen << 3) / needents)) {
a0ed51b3
LW
2181 case 1:
2182 bit = 1 << (off & 7);
2183 off >>= 3;
2184 return (tmps[off] & bit) != 0;
2185 case 8:
2186 return tmps[off];
2187 case 16:
2188 off <<= 1;
2189 return (tmps[off] << 8) + tmps[off + 1] ;
2190 case 32:
2191 off <<= 2;
2192 return (tmps[off] << 24) + (tmps[off+1] << 16) + (tmps[off+2] << 8) + tmps[off + 3] ;
2193 }
660a4616 2194 Perl_croak(aTHX_ "panic: swash_fetch got swatch of unexpected bit width");
670f1322 2195 NORETURN_FUNCTION_END;
a0ed51b3 2196}
2b9d42f0 2197
319009ee
KW
2198/* Read a single line of the main body of the swash input text. These are of
2199 * the form:
2200 * 0053 0056 0073
2201 * where each number is hex. The first two numbers form the minimum and
2202 * maximum of a range, and the third is the value associated with the range.
2203 * Not all swashes should have a third number
2204 *
2205 * On input: l points to the beginning of the line to be examined; it points
2206 * to somewhere in the string of the whole input text, and is
2207 * terminated by a \n or the null string terminator.
2208 * lend points to the null terminator of that string
2209 * wants_value is non-zero if the swash expects a third number
2210 * typestr is the name of the swash's mapping, like 'ToLower'
2211 * On output: *min, *max, and *val are set to the values read from the line.
2212 * returns a pointer just beyond the line examined. If there was no
2213 * valid min number on the line, returns lend+1
2214 */
2215
2216STATIC U8*
2217S_swash_scan_list_line(pTHX_ U8* l, U8* const lend, UV* min, UV* max, UV* val,
2218 const bool wants_value, const U8* const typestr)
2219{
2220 const int typeto = typestr[0] == 'T' && typestr[1] == 'o';
2221 STRLEN numlen; /* Length of the number */
2222 I32 flags = PERL_SCAN_SILENT_ILLDIGIT | PERL_SCAN_DISALLOW_PREFIX;
2223
2224 /* nl points to the next \n in the scan */
2225 U8* const nl = (U8*)memchr(l, '\n', lend - l);
2226
2227 /* Get the first number on the line: the range minimum */
2228 numlen = lend - l;
2229 *min = grok_hex((char *)l, &numlen, &flags, NULL);
2230 if (numlen) /* If found a hex number, position past it */
2231 l += numlen;
2232 else if (nl) { /* Else, go handle next line, if any */
2233 return nl + 1; /* 1 is length of "\n" */
2234 }
2235 else { /* Else, no next line */
2236 return lend + 1; /* to LIST's end at which \n is not found */
2237 }
2238
2239 /* The max range value follows, separated by a BLANK */
2240 if (isBLANK(*l)) {
2241 ++l;
2242 flags = PERL_SCAN_SILENT_ILLDIGIT | PERL_SCAN_DISALLOW_PREFIX;
2243 numlen = lend - l;
2244 *max = grok_hex((char *)l, &numlen, &flags, NULL);
2245 if (numlen)
2246 l += numlen;
2247 else /* If no value here, it is a single element range */
2248 *max = *min;
2249
2250 /* Non-binary tables have a third entry: what the first element of the
2251 * range maps to */
2252 if (wants_value) {
2253 if (isBLANK(*l)) {
2254 ++l;
2255 flags = PERL_SCAN_SILENT_ILLDIGIT |
2256 PERL_SCAN_DISALLOW_PREFIX;
2257 numlen = lend - l;
2258 *val = grok_hex((char *)l, &numlen, &flags, NULL);
2259 if (numlen)
2260 l += numlen;
2261 else
2262 *val = 0;
2263 }
2264 else {
2265 *val = 0;
2266 if (typeto) {
2267 Perl_croak(aTHX_ "%s: illegal mapping '%s'",
2268 typestr, l);
2269 }
2270 }
2271 }
2272 else
2273 *val = 0; /* bits == 1, then any val should be ignored */
2274 }
2275 else { /* Nothing following range min, should be single element with no
2276 mapping expected */
2277 *max = *min;
2278 if (wants_value) {
2279 *val = 0;
2280 if (typeto) {
2281 Perl_croak(aTHX_ "%s: illegal mapping '%s'", typestr, l);
2282 }
2283 }
2284 else
2285 *val = 0; /* bits == 1, then val should be ignored */
2286 }
2287
2288 /* Position to next line if any, or EOF */
2289 if (nl)
2290 l = nl + 1;
2291 else
2292 l = lend;
2293
2294 return l;
2295}
2296
979f2922
ST
2297/* Note:
2298 * Returns a swatch (a bit vector string) for a code point sequence
2299 * that starts from the value C<start> and comprises the number C<span>.
2300 * A C<swash> must be an object created by SWASHNEW (see lib/utf8_heavy.pl).
2301 * Should be used via swash_fetch, which will cache the swatch in C<swash>.
2302 */
2303STATIC SV*
2304S_swash_get(pTHX_ SV* swash, UV start, UV span)
2305{
2306 SV *swatch;
711a919c 2307 U8 *l, *lend, *x, *xend, *s;
979f2922 2308 STRLEN lcur, xcur, scur;
ef8f7699 2309 HV *const hv = MUTABLE_HV(SvRV(swash));
972dd592
KW
2310
2311 /* The string containing the main body of the table */
017a3ce5 2312 SV** const listsvp = hv_fetchs(hv, "LIST", FALSE);
972dd592 2313
017a3ce5
GA
2314 SV** const typesvp = hv_fetchs(hv, "TYPE", FALSE);
2315 SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE);
2316 SV** const nonesvp = hv_fetchs(hv, "NONE", FALSE);
2317 SV** const extssvp = hv_fetchs(hv, "EXTRAS", FALSE);
0bd48802 2318 const U8* const typestr = (U8*)SvPV_nolen(*typesvp);
0bd48802
AL
2319 const STRLEN bits = SvUV(*bitssvp);
2320 const STRLEN octets = bits >> 3; /* if bits == 1, then octets == 0 */
2321 const UV none = SvUV(*nonesvp);
2322 const UV end = start + span;
979f2922 2323
7918f24d
NC
2324 PERL_ARGS_ASSERT_SWASH_GET;
2325
979f2922 2326 if (bits != 1 && bits != 8 && bits != 16 && bits != 32) {
660a4616
ST
2327 Perl_croak(aTHX_ "panic: swash_get doesn't expect bits %"UVuf,
2328 (UV)bits);
979f2922
ST
2329 }
2330
2331 /* create and initialize $swatch */
979f2922 2332 scur = octets ? (span * octets) : (span + 7) / 8;
e524fe40
NC
2333 swatch = newSV(scur);
2334 SvPOK_on(swatch);
979f2922
ST
2335 s = (U8*)SvPVX(swatch);
2336 if (octets && none) {
0bd48802 2337 const U8* const e = s + scur;
979f2922
ST
2338 while (s < e) {
2339 if (bits == 8)
2340 *s++ = (U8)(none & 0xff);
2341 else if (bits == 16) {
2342 *s++ = (U8)((none >> 8) & 0xff);
2343 *s++ = (U8)( none & 0xff);
2344 }
2345 else if (bits == 32) {
2346 *s++ = (U8)((none >> 24) & 0xff);
2347 *s++ = (U8)((none >> 16) & 0xff);
2348 *s++ = (U8)((none >> 8) & 0xff);
2349 *s++ = (U8)( none & 0xff);
2350 }
2351 }
2352 *s = '\0';
2353 }
2354 else {
2355 (void)memzero((U8*)s, scur + 1);
2356 }
2357 SvCUR_set(swatch, scur);
2358 s = (U8*)SvPVX(swatch);
2359
2360 /* read $swash->{LIST} */
2361 l = (U8*)SvPV(*listsvp, lcur);
2362 lend = l + lcur;
2363 while (l < lend) {
35da51f7 2364 UV min, max, val;
319009ee
KW
2365 l = S_swash_scan_list_line(aTHX_ l, lend, &min, &max, &val,
2366 cBOOL(octets), typestr);
2367 if (l > lend) {
979f2922
ST
2368 break;
2369 }
2370
972dd592 2371 /* If looking for something beyond this range, go try the next one */
979f2922
ST
2372 if (max < start)
2373 continue;
2374
2375 if (octets) {
35da51f7 2376 UV key;
979f2922
ST
2377 if (min < start) {
2378 if (!none || val < none) {
2379 val += start - min;
2380 }
2381 min = start;
2382 }
2383 for (key = min; key <= max; key++) {
2384 STRLEN offset;
2385 if (key >= end)
2386 goto go_out_list;
2387 /* offset must be non-negative (start <= min <= key < end) */
2388 offset = octets * (key - start);
2389 if (bits == 8)
2390 s[offset] = (U8)(val & 0xff);
2391 else if (bits == 16) {
2392 s[offset ] = (U8)((val >> 8) & 0xff);
2393 s[offset + 1] = (U8)( val & 0xff);
2394 }
2395 else if (bits == 32) {
2396 s[offset ] = (U8)((val >> 24) & 0xff);
2397 s[offset + 1] = (U8)((val >> 16) & 0xff);
2398 s[offset + 2] = (U8)((val >> 8) & 0xff);
2399 s[offset + 3] = (U8)( val & 0xff);
2400 }
2401
2402 if (!none || val < none)
2403 ++val;
2404 }
2405 }
711a919c 2406 else { /* bits == 1, then val should be ignored */
35da51f7 2407 UV key;
979f2922
ST
2408 if (min < start)
2409 min = start;
2410 for (key = min; key <= max; key++) {
0bd48802 2411 const STRLEN offset = (STRLEN)(key - start);
979f2922
ST
2412 if (key >= end)
2413 goto go_out_list;
2414 s[offset >> 3] |= 1 << (offset & 7);
2415 }
2416 }
2417 } /* while */
2418 go_out_list:
2419
2420 /* read $swash->{EXTRAS} */
2421 x = (U8*)SvPV(*extssvp, xcur);
2422 xend = x + xcur;
2423 while (x < xend) {
2424 STRLEN namelen;
2425 U8 *namestr;
2426 SV** othersvp;
2427 HV* otherhv;
2428 STRLEN otherbits;
2429 SV **otherbitssvp, *other;
711a919c 2430 U8 *s, *o, *nl;
979f2922
ST
2431 STRLEN slen, olen;
2432
35da51f7 2433 const U8 opc = *x++;
979f2922
ST
2434 if (opc == '\n')
2435 continue;
2436
2437 nl = (U8*)memchr(x, '\n', xend - x);
2438
2439 if (opc != '-' && opc != '+' && opc != '!' && opc != '&') {
2440 if (nl) {
2441 x = nl + 1; /* 1 is length of "\n" */
2442 continue;
2443 }
2444 else {
2445 x = xend; /* to EXTRAS' end at which \n is not found */
2446 break;
2447 }
2448 }
2449
2450 namestr = x;
2451 if (nl) {
2452 namelen = nl - namestr;
2453 x = nl + 1;
2454 }
2455 else {
2456 namelen = xend - namestr;
2457 x = xend;
2458 }
2459
2460 othersvp = hv_fetch(hv, (char *)namestr, namelen, FALSE);
ef8f7699 2461 otherhv = MUTABLE_HV(SvRV(*othersvp));
017a3ce5 2462 otherbitssvp = hv_fetchs(otherhv, "BITS", FALSE);
979f2922
ST
2463 otherbits = (STRLEN)SvUV(*otherbitssvp);
2464 if (bits < otherbits)
660a4616 2465 Perl_croak(aTHX_ "panic: swash_get found swatch size mismatch");
979f2922
ST
2466
2467 /* The "other" swatch must be destroyed after. */
2468 other = swash_get(*othersvp, start, span);
2469 o = (U8*)SvPV(other, olen);
2470
2471 if (!olen)
660a4616 2472 Perl_croak(aTHX_ "panic: swash_get got improper swatch");
979f2922
ST
2473
2474 s = (U8*)SvPV(swatch, slen);
2475 if (bits == 1 && otherbits == 1) {
2476 if (slen != olen)
660a4616 2477 Perl_croak(aTHX_ "panic: swash_get found swatch length mismatch");
979f2922
ST
2478
2479 switch (opc) {
2480 case '+':
2481 while (slen--)
2482 *s++ |= *o++;
2483 break;
2484 case '!':
2485 while (slen--)
2486 *s++ |= ~*o++;
2487 break;
2488 case '-':
2489 while (slen--)
2490 *s++ &= ~*o++;
2491 break;
2492 case '&':
2493 while (slen--)
2494 *s++ &= *o++;
2495 break;
2496 default:
2497 break;
2498 }
2499 }
711a919c 2500 else {
979f2922
ST
2501 STRLEN otheroctets = otherbits >> 3;
2502 STRLEN offset = 0;
35da51f7 2503 U8* const send = s + slen;
979f2922
ST
2504
2505 while (s < send) {
2506 UV otherval = 0;
2507
2508 if (otherbits == 1) {
2509 otherval = (o[offset >> 3] >> (offset & 7)) & 1;
2510 ++offset;
2511 }
2512 else {
2513 STRLEN vlen = otheroctets;
2514 otherval = *o++;
2515 while (--vlen) {
2516 otherval <<= 8;
2517 otherval |= *o++;
2518 }
2519 }
2520
711a919c 2521 if (opc == '+' && otherval)
6f207bd3 2522 NOOP; /* replace with otherval */
979f2922
ST
2523 else if (opc == '!' && !otherval)
2524 otherval = 1;
2525 else if (opc == '-' && otherval)
2526 otherval = 0;
2527 else if (opc == '&' && !otherval)
2528 otherval = 0;
2529 else {
711a919c 2530 s += octets; /* no replacement */
979f2922
ST
2531 continue;
2532 }
2533
2534 if (bits == 8)
2535 *s++ = (U8)( otherval & 0xff);
2536 else if (bits == 16) {
2537 *s++ = (U8)((otherval >> 8) & 0xff);
2538 *s++ = (U8)( otherval & 0xff);
2539 }
2540 else if (bits == 32) {
2541 *s++ = (U8)((otherval >> 24) & 0xff);
2542 *s++ = (U8)((otherval >> 16) & 0xff);
2543 *s++ = (U8)((otherval >> 8) & 0xff);
2544 *s++ = (U8)( otherval & 0xff);
2545 }
2546 }
2547 }
2548 sv_free(other); /* through with it! */
2549 } /* while */
2550 return swatch;
2551}
2552
064c021d
KW
2553HV*
2554Perl__swash_inversion_hash(pTHX_ SV* swash)
2555{
2556
2557 /* Subject to change or removal. For use only in one place in regexec.c
2558 *
2559 * Returns a hash which is the inversion and closure of a swash mapping.
2560 * For example, consider the input lines:
2561 * 004B 006B
2562 * 004C 006C
2563 * 212A 006B
2564 *
2565 * The returned hash would have two keys, the utf8 for 006B and the utf8 for
2566 * 006C. The value for each key is an array. For 006C, the array would
2567 * have a two elements, the utf8 for itself, and for 004C. For 006B, there
2568 * would be three elements in its array, the utf8 for 006B, 004B and 212A.
2569 *
2570 * Essentially, for any code point, it gives all the code points that map to
2571 * it, or the list of 'froms' for that point.
2572 *
2573 * Currently it only looks at the main body of the swash, and ignores any
2574 * additions or deletions from other swashes */
2575
2576 U8 *l, *lend;
2577 STRLEN lcur;
2578 HV *const hv = MUTABLE_HV(SvRV(swash));
2579
2580 /* The string containing the main body of the table */
2581 SV** const listsvp = hv_fetchs(hv, "LIST", FALSE);
2582
2583 SV** const typesvp = hv_fetchs(hv, "TYPE", FALSE);
2584 SV** const bitssvp = hv_fetchs(hv, "BITS", FALSE);
2585 SV** const nonesvp = hv_fetchs(hv, "NONE", FALSE);
2586 /*SV** const extssvp = hv_fetchs(hv, "EXTRAS", FALSE);*/
2587 const U8* const typestr = (U8*)SvPV_nolen(*typesvp);
2588 const STRLEN bits = SvUV(*bitssvp);
2589 const STRLEN octets = bits >> 3; /* if bits == 1, then octets == 0 */
2590 const UV none = SvUV(*nonesvp);
2591
2592 HV* ret = newHV();
2593
2594 PERL_ARGS_ASSERT__SWASH_INVERSION_HASH;
2595
2596 /* Must have at least 8 bits to get the mappings */
2597 if (bits != 8 && bits != 16 && bits != 32) {
2598 Perl_croak(aTHX_ "panic: swash_inversion_hash doesn't expect bits %"UVuf,
2599 (UV)bits);
2600 }
2601
2602 /* read $swash->{LIST} */
2603 l = (U8*)SvPV(*listsvp, lcur);
2604 lend = l + lcur;
2605
2606 /* Go through each input line */
2607 while (l < lend) {
2608 UV min, max, val;
2609 UV inverse;
2610 l = S_swash_scan_list_line(aTHX_ l, lend, &min, &max, &val,
2611 cBOOL(octets), typestr);
2612 if (l > lend) {
2613 break;
2614 }
2615
2616 /* Each element in the range is to be inverted */
2617 for (inverse = min; inverse <= max; inverse++) {
2618 AV* list;
2619 SV* element;
2620 SV** listp;
2621 IV i;
2622 bool found_key = FALSE;
2623
2624 /* The key is the inverse mapping */
2625 char key[UTF8_MAXBYTES+1];
2626 char* key_end = (char *) uvuni_to_utf8((U8*) key, val);
2627 STRLEN key_len = key_end - key;
2628
2629 /* And the value is what the forward mapping is from. */
2630 char utf8_inverse[UTF8_MAXBYTES+1];
2631 char *utf8_inverse_end = (char *) uvuni_to_utf8((U8*) utf8_inverse, inverse);
2632
2633 /* Get the list for the map */
2634 if ((listp = hv_fetch(ret, key, key_len, FALSE))) {
2635 list = (AV*) *listp;
2636 }
2637 else { /* No entry yet for it: create one */
2638 list = newAV();
2639 if (! hv_store(ret, key, key_len, (SV*) list, FALSE)) {
2640 Perl_croak(aTHX_ "panic: hv_store() unexpectedly failed");
2641 }
2642 }
2643
2644 for (i = 0; i < av_len(list); i++) {
2645 SV** entryp = av_fetch(list, i, FALSE);
2646 SV* entry;
2647 if (entryp == NULL) {
2648 Perl_croak(aTHX_ "panic: av_fetch() unexpectedly failed");
2649 }
2650 entry = *entryp;
2651 if (SvCUR(entry) != key_len) {
2652 continue;
2653 }
2654 if (memEQ(key, SvPVX(entry), key_len)) {
2655 found_key = TRUE;
2656 break;
2657 }
2658 }
2659 if (! found_key) {
2660 element = newSVpvn_flags(key, key_len, SVf_UTF8);
2661 av_push(list, element);
2662 }
2663
2664
2665 /* Simply add the value to the list */
2666 element = newSVpvn_flags(utf8_inverse, utf8_inverse_end - utf8_inverse, SVf_UTF8);
2667 av_push(list, element);
2668
2669 /* swash_get() increments the value of val for each element in the
2670 * range. That makes more compact tables possible. You can
2671 * express the capitalization, for example, of all consecutive
2672 * letters with a single line: 0061\t007A\t0041 This maps 0061 to
2673 * 0041, 0062 to 0042, etc. I (khw) have never understood 'none',
2674 * and it's not documented, and perhaps not even currently used,
2675 * but I copied the semantics from swash_get(), just in case */
2676 if (!none || val < none) {
2677 ++val;
2678 }
2679 }
2680 }
2681
2682 return ret;
2683}
2684
0f830e0b 2685/*
87cea99e 2686=for apidoc uvchr_to_utf8
0f830e0b 2687
6ee84de2 2688Adds the UTF-8 representation of the Native code point C<uv> to the end
0f830e0b
NC
2689of the string C<d>; C<d> should be have at least C<UTF8_MAXBYTES+1> free
2690bytes available. The return value is the pointer to the byte after the
2691end of the new character. In other words,
2692
2693 d = uvchr_to_utf8(d, uv);
2694
2695is the recommended wide native character-aware way of saying
2696
2697 *(d++) = uv;
2698
2699=cut
2700*/
2701
2702/* On ASCII machines this is normally a macro but we want a
2703 real function in case XS code wants it
2704*/
0f830e0b
NC
2705U8 *
2706Perl_uvchr_to_utf8(pTHX_ U8 *d, UV uv)
2707{
7918f24d
NC
2708 PERL_ARGS_ASSERT_UVCHR_TO_UTF8;
2709
0f830e0b
NC
2710 return Perl_uvuni_to_utf8_flags(aTHX_ d, NATIVE_TO_UNI(uv), 0);
2711}
2712
b851fbc1
JH
2713U8 *
2714Perl_uvchr_to_utf8_flags(pTHX_ U8 *d, UV uv, UV flags)
2715{
7918f24d
NC
2716 PERL_ARGS_ASSERT_UVCHR_TO_UTF8_FLAGS;
2717
b851fbc1
JH
2718 return Perl_uvuni_to_utf8_flags(aTHX_ d, NATIVE_TO_UNI(uv), flags);
2719}
2b9d42f0
NIS
2720
2721/*
87cea99e 2722=for apidoc utf8n_to_uvchr
0f830e0b 2723
48ef279e 2724Returns the native character value of the first character in the string
0f830e0b
NC
2725C<s>
2726which is assumed to be in UTF-8 encoding; C<retlen> will be set to the
2727length, in bytes, of that character.
2728
6ee84de2 2729length and flags are the same as utf8n_to_uvuni().
0f830e0b
NC
2730
2731=cut
2732*/
2733/* On ASCII machines this is normally a macro but we want
2734 a real function in case XS code wants it
2735*/
0f830e0b 2736UV
48ef279e 2737Perl_utf8n_to_uvchr(pTHX_ const U8 *s, STRLEN curlen, STRLEN *retlen,
0f830e0b
NC
2738U32 flags)
2739{
2740 const UV uv = Perl_utf8n_to_uvuni(aTHX_ s, curlen, retlen, flags);
7918f24d
NC
2741
2742 PERL_ARGS_ASSERT_UTF8N_TO_UVCHR;
2743
0f830e0b
NC
2744 return UNI_TO_NATIVE(uv);
2745}
2746
0876b9a0
KW
2747bool
2748Perl_check_utf8_print(pTHX_ register const U8* s, const STRLEN len)
2749{
2750 /* May change: warns if surrogates, non-character code points, or
2751 * non-Unicode code points are in s which has length len. Returns TRUE if
2752 * none found; FALSE otherwise. The only other validity check is to make
2753 * sure that this won't exceed the string's length */
2754
2755 const U8* const e = s + len;
2756 bool ok = TRUE;
2757
2758 PERL_ARGS_ASSERT_CHECK_UTF8_PRINT;
2759
2760 while (s < e) {
2761 if (UTF8SKIP(s) > len) {
2762 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
2763 "%s in %s", unees, PL_op ? OP_DESC(PL_op) : "print");
2764 return FALSE;
2765 }
2766 if (*s >= UTF8_FIRST_PROBLEMATIC_CODE_POINT_FIRST_BYTE) {
2767 STRLEN char_len;
2768 if (UTF8_IS_SUPER(s)) {
2769 UV uv = utf8_to_uvchr(s, &char_len);
2770 Perl_warner(aTHX_ packWARN(WARN_UTF8),
2771 "Code point 0x%04"UVXf" is not Unicode, may not be portable", uv);
2772 ok = FALSE;
2773 }
2774 else if (UTF8_IS_SURROGATE(s)) {
2775 UV uv = utf8_to_uvchr(s, &char_len);
2776 Perl_warner(aTHX_ packWARN(WARN_UTF8),
2777 "Unicode surrogate U+%04"UVXf" is illegal in UTF-8", uv);
2778 ok = FALSE;
2779 }
2780 else if
2781 (UTF8_IS_NONCHAR_GIVEN_THAT_NON_SUPER_AND_GE_PROBLEMATIC(s))
2782 {
2783 UV uv = utf8_to_uvchr(s, &char_len);
2784 Perl_warner(aTHX_ packWARN(WARN_UTF8),
2785 "Unicode non-character U+%04"UVXf" is illegal for open interchange", uv);
2786 ok = FALSE;
2787 }
2788 }
2789 s += UTF8SKIP(s);
2790 }
2791
2792 return ok;
2793}
2794
0f830e0b 2795/*
87cea99e 2796=for apidoc pv_uni_display
d2cc3551
JH
2797
2798Build to the scalar dsv a displayable version of the string spv,
2799length len, the displayable version being at most pvlim bytes long
2800(if longer, the rest is truncated and "..." will be appended).
0a2ef054 2801
9e55ce06 2802The flags argument can have UNI_DISPLAY_ISPRINT set to display
00e86452 2803isPRINT()able characters as themselves, UNI_DISPLAY_BACKSLASH
0a2ef054
JH
2804to display the \\[nrfta\\] as the backslashed versions (like '\n')
2805(UNI_DISPLAY_BACKSLASH is preferred over UNI_DISPLAY_ISPRINT for \\).
2806UNI_DISPLAY_QQ (and its alias UNI_DISPLAY_REGEX) have both
2807UNI_DISPLAY_BACKSLASH and UNI_DISPLAY_ISPRINT turned on.
2808
d2cc3551
JH
2809The pointer to the PV of the dsv is returned.
2810
2811=cut */
e6b2e755 2812char *
e1ec3a88 2813Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv, STRLEN len, STRLEN pvlim, UV flags)
e6b2e755
JH
2814{
2815 int truncated = 0;
e1ec3a88 2816 const char *s, *e;
e6b2e755 2817
7918f24d
NC
2818 PERL_ARGS_ASSERT_PV_UNI_DISPLAY;
2819
76f68e9b 2820 sv_setpvs(dsv, "");
7fddd944 2821 SvUTF8_off(dsv);
e1ec3a88 2822 for (s = (const char *)spv, e = s + len; s < e; s += UTF8SKIP(s)) {
e6b2e755 2823 UV u;
a49f32c6
NC
2824 /* This serves double duty as a flag and a character to print after
2825 a \ when flags & UNI_DISPLAY_BACKSLASH is true.
2826 */
2827 char ok = 0;
c728cb41 2828
e6b2e755
JH
2829 if (pvlim && SvCUR(dsv) >= pvlim) {
2830 truncated++;
2831 break;
2832 }
2833 u = utf8_to_uvchr((U8*)s, 0);
c728cb41 2834 if (u < 256) {
a3b680e6 2835 const unsigned char c = (unsigned char)u & 0xFF;
0bd48802 2836 if (flags & UNI_DISPLAY_BACKSLASH) {
a49f32c6 2837 switch (c) {
c728cb41 2838 case '\n':
a49f32c6 2839 ok = 'n'; break;
c728cb41 2840 case '\r':
a49f32c6 2841 ok = 'r'; break;
c728cb41 2842 case '\t':
a49f32c6 2843 ok = 't'; break;
c728cb41 2844 case '\f':
a49f32c6 2845 ok = 'f'; break;
c728cb41 2846 case '\a':
a49f32c6 2847 ok = 'a'; break;
c728cb41 2848 case '\\':
a49f32c6 2849 ok = '\\'; break;
c728cb41
JH
2850 default: break;
2851 }
a49f32c6 2852 if (ok) {
88c9ea1e 2853 const char string = ok;
76f68e9b 2854 sv_catpvs(dsv, "\\");
5e7aa789 2855 sv_catpvn(dsv, &string, 1);
a49f32c6 2856 }
c728cb41 2857 }
00e86452 2858 /* isPRINT() is the locale-blind version. */
a49f32c6 2859 if (!ok && (flags & UNI_DISPLAY_ISPRINT) && isPRINT(c)) {
88c9ea1e 2860 const char string = c;
5e7aa789 2861 sv_catpvn(dsv, &string, 1);
a49f32c6 2862 ok = 1;
0a2ef054 2863 }
c728cb41
JH
2864 }
2865 if (!ok)
9e55ce06 2866 Perl_sv_catpvf(aTHX_ dsv, "\\x{%"UVxf"}", u);
e6b2e755
JH
2867 }
2868 if (truncated)
396482e1 2869 sv_catpvs(dsv, "...");
48ef279e 2870
e6b2e755
JH
2871 return SvPVX(dsv);
2872}
2b9d42f0 2873
d2cc3551 2874/*
87cea99e 2875=for apidoc sv_uni_display
d2cc3551
JH
2876
2877Build to the scalar dsv a displayable version of the scalar sv,
0a2ef054 2878the displayable version being at most pvlim bytes long
d2cc3551 2879(if longer, the rest is truncated and "..." will be appended).
0a2ef054
JH
2880
2881The flags argument is as in pv_uni_display().
2882
d2cc3551
JH
2883The pointer to the PV of the dsv is returned.
2884
d4c19fe8
AL
2885=cut
2886*/
e6b2e755
JH
2887char *
2888Perl_sv_uni_display(pTHX_ SV *dsv, SV *ssv, STRLEN pvlim, UV flags)
2889{
7918f24d
NC
2890 PERL_ARGS_ASSERT_SV_UNI_DISPLAY;
2891
cfd0369c
NC
2892 return Perl_pv_uni_display(aTHX_ dsv, (const U8*)SvPVX_const(ssv),
2893 SvCUR(ssv), pvlim, flags);
701a277b
JH
2894}
2895
d2cc3551 2896/*
e6226b18 2897=for apidoc foldEQ_utf8
d2cc3551 2898
d51c1b21 2899Returns true if the leading portions of the strings s1 and s2 (either or both
e6226b18 2900of which may be in UTF-8) are the same case-insensitively; false otherwise.
d51c1b21 2901How far into the strings to compare is determined by other input parameters.
8b35872c
KW
2902
2903If u1 is true, the string s1 is assumed to be in UTF-8-encoded Unicode;
2904otherwise it is assumed to be in native 8-bit encoding. Correspondingly for u2
2905with respect to s2.
2906
d51c1b21
KW
2907If the byte length l1 is non-zero, it says how far into s1 to check for fold
2908equality. In other words, s1+l1 will be used as a goal to reach. The
8b35872c
KW
2909scan will not be considered to be a match unless the goal is reached, and
2910scanning won't continue past that goal. Correspondingly for l2 with respect to
2911s2.
2912
2913If pe1 is non-NULL and the pointer it points to is not NULL, that pointer is
2914considered an end pointer beyond which scanning of s1 will not continue under
2915any circumstances. This means that if both l1 and pe1 are specified, and pe1
2916is less than s1+l1, the match will never be successful because it can never
d51c1b21
KW
2917get as far as its goal (and in fact is asserted against). Correspondingly for
2918pe2 with respect to s2.
8b35872c 2919
d51c1b21
KW
2920At least one of s1 and s2 must have a goal (at least one of l1 and l2 must be
2921non-zero), and if both do, both have to be
8b35872c
KW
2922reached for a successful match. Also, if the fold of a character is multiple
2923characters, all of them must be matched (see tr21 reference below for
2924'folding').
2925
e6226b18 2926Upon a successful match, if pe1 is non-NULL,
8b35872c
KW
2927it will be set to point to the beginning of the I<next> character of s1 beyond
2928what was matched. Correspondingly for pe2 and s2.
d2cc3551
JH
2929
2930For case-insensitiveness, the "casefolding" of Unicode is used
2931instead of upper/lowercasing both the characters, see
2932http://www.unicode.org/unicode/reports/tr21/ (Case Mappings).
2933
2934=cut */
701a277b 2935I32
e6226b18 2936Perl_foldEQ_utf8(pTHX_ const char *s1, char **pe1, register UV l1, bool u1, const char *s2, char **pe2, register UV l2, bool u2)
332ddc25 2937{
8b35872c
KW
2938 dVAR;
2939 register const U8 *p1 = (const U8*)s1; /* Point to current char */
2940 register const U8 *p2 = (const U8*)s2;
48ef279e 2941 register const U8 *g1 = NULL; /* goal for s1 */
8b35872c 2942 register const U8 *g2 = NULL;
48ef279e
KW
2943 register const U8 *e1 = NULL; /* Don't scan s1 past this */
2944 register U8 *f1 = NULL; /* Point to current folded */
8b35872c
KW
2945 register const U8 *e2 = NULL;
2946 register U8 *f2 = NULL;
48ef279e 2947 STRLEN n1 = 0, n2 = 0; /* Number of bytes in current char */
8b35872c
KW
2948 U8 foldbuf1[UTF8_MAXBYTES_CASE+1];
2949 U8 foldbuf2[UTF8_MAXBYTES_CASE+1];
48ef279e
KW
2950 U8 natbuf[2]; /* Holds native 8-bit char converted to utf8;
2951 these always fit in 2 bytes */
8b35872c 2952
e6226b18 2953 PERL_ARGS_ASSERT_FOLDEQ_UTF8;
8b35872c
KW
2954
2955 if (pe1) {
48ef279e 2956 e1 = *(U8**)pe1;
8b35872c
KW
2957 }
2958
2959 if (l1) {
48ef279e 2960 g1 = (const U8*)s1 + l1;
8b35872c
KW
2961 }
2962
2963 if (pe2) {
48ef279e 2964 e2 = *(U8**)pe2;
8b35872c
KW
2965 }
2966
2967 if (l2) {
48ef279e 2968 g2 = (const U8*)s2 + l2;
8b35872c
KW
2969 }
2970
2971 /* Must have at least one goal */
2972 assert(g1 || g2);
2973
2974 if (g1) {
2975
48ef279e
KW
2976 /* Will never match if goal is out-of-bounds */
2977 assert(! e1 || e1 >= g1);
8b35872c 2978
48ef279e
KW
2979 /* Here, there isn't an end pointer, or it is beyond the goal. We
2980 * only go as far as the goal */
2981 e1 = g1;
8b35872c 2982 }
313b38e5
NC
2983 else {
2984 assert(e1); /* Must have an end for looking at s1 */
2985 }
8b35872c
KW
2986
2987 /* Same for goal for s2 */
2988 if (g2) {
48ef279e
KW
2989 assert(! e2 || e2 >= g2);
2990 e2 = g2;
8b35872c 2991 }
313b38e5
NC
2992 else {
2993 assert(e2);
2994 }
8b35872c
KW
2995
2996 /* Look through both strings, a character at a time */
2997 while (p1 < e1 && p2 < e2) {
2998
d51c1b21
KW
2999 /* If at the beginning of a new character in s1, get its fold to use
3000 * and the length of the fold */
48ef279e
KW
3001 if (n1 == 0) {
3002 if (u1) {
3003 to_utf8_fold(p1, foldbuf1, &n1);
3004 }
3005 else { /* Not utf8, convert to it first and then get fold */
3006 uvuni_to_utf8(natbuf, (UV) NATIVE_TO_UNI(((UV)*p1)));
3007 to_utf8_fold(natbuf, foldbuf1, &n1);
3008 }
3009 f1 = foldbuf1;
3010 }
8b35872c 3011
48ef279e
KW
3012 if (n2 == 0) { /* Same for s2 */
3013 if (u2) {
3014 to_utf8_fold(p2, foldbuf2, &n2);
3015 }
3016 else {
3017 uvuni_to_utf8(natbuf, (UV) NATIVE_TO_UNI(((UV)*p2)));
3018 to_utf8_fold(natbuf, foldbuf2, &n2);
3019 }
3020 f2 = foldbuf2;
3021 }
8b35872c 3022
48ef279e
KW
3023 /* While there is more to look for in both folds, see if they
3024 * continue to match */
3025 while (n1 && n2) {
3026 U8 fold_length = UTF8SKIP(f1);
3027 if (fold_length != UTF8SKIP(f2)
3028 || (fold_length == 1 && *f1 != *f2) /* Short circuit memNE
3029 function call for single
3030 character */
3031 || memNE((char*)f1, (char*)f2, fold_length))
3032 {
e6226b18 3033 return 0; /* mismatch */
48ef279e
KW
3034 }
3035
3036 /* Here, they matched, advance past them */
3037 n1 -= fold_length;
3038 f1 += fold_length;
3039 n2 -= fold_length;
3040 f2 += fold_length;
3041 }
8b35872c 3042
48ef279e
KW
3043 /* When reach the end of any fold, advance the input past it */
3044 if (n1 == 0) {
3045 p1 += u1 ? UTF8SKIP(p1) : 1;
3046 }
3047 if (n2 == 0) {
3048 p2 += u2 ? UTF8SKIP(p2) : 1;
3049 }
8b35872c
KW
3050 } /* End of loop through both strings */
3051
3052 /* A match is defined by each scan that specified an explicit length
3053 * reaching its final goal, and the other not having matched a partial
3054 * character (which can happen when the fold of a character is more than one
3055 * character). */
3056 if (! ((g1 == 0 || p1 == g1) && (g2 == 0 || p2 == g2)) || n1 || n2) {
e6226b18 3057 return 0;
8b35872c
KW
3058 }
3059
3060 /* Successful match. Set output pointers */
3061 if (pe1) {
48ef279e 3062 *pe1 = (char*)p1;
8b35872c
KW
3063 }
3064 if (pe2) {
48ef279e 3065 *pe2 = (char*)p2;
8b35872c 3066 }
e6226b18 3067 return 1;
e6b2e755 3068}
701a277b 3069
a49f32c6
NC
3070/*
3071 * Local variables:
3072 * c-indentation-style: bsd
3073 * c-basic-offset: 4
3074 * indent-tabs-mode: t
3075 * End:
3076 *
37442d52
RGS
3077 * ex: set ts=8 sts=4 sw=4 noet:
3078 */