This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
IO::getline(): use CALLRUNOPS
[perl5.git] / utf8.h
CommitLineData
a0ed51b3
LW
1/* utf8.h
2 *
f3cb6f94
KW
3 * This file contains definitions for use with the UTF-8 encoding. It
4 * actually also works with the variant UTF-8 encoding called UTF-EBCDIC, and
5 * hides almost all of the differences between these from the caller. In other
6 * words, someone should #include this file, and if the code is being compiled
7 * on an EBCDIC platform, things should mostly just work.
8 *
2eee27d7
SS
9 * Copyright (C) 2000, 2001, 2002, 2005, 2006, 2007, 2009,
10 * 2010, 2011 by Larry Wall and others
a0ed51b3
LW
11 *
12 * You may distribute under the terms of either the GNU General Public
13 * License or the Artistic License, as specified in the README file.
14 *
42b360b2
KW
15 * A note on nomenclature: The term UTF-8 is used loosely and inconsistently
16 * in Perl documentation. For one, perl uses an extension of UTF-8 to
17 * represent code points that Unicode considers illegal. For another, ASCII
18 * platform UTF-8 is usually conflated with EBCDIC platform UTF-EBCDIC, because
19 * outside some of the macros in this this file, the differences are hopefully
20 * invisible at the semantic level.
21 *
22 * UTF-EBCDIC has an isomorphic translation named I8 (for "Intermediate eight")
23 * which differs from UTF-8 only in a few details. It is often useful to
24 * translate UTF-EBCDIC into this form for processing. In general, macros and
25 * functions that are expecting their inputs to be either in I8 or UTF-8 are
26 * named UTF_foo (without an '8'), to indicate this.
27 *
28 * Unfortunately there are inconsistencies.
29 *
a0ed51b3
LW
30 */
31
6a5bc5ac
KW
32#ifndef PERL_UTF8_H_ /* Guard against recursive inclusion */
33#define PERL_UTF8_H_ 1
57f0e7e2 34
1cdf84d2
KW
35/*
36=for apidoc Ay||utf8ness_t
37
38This typedef is used by several core functions that return PV strings, to
39indicate the UTF-8ness of those strings.
40
41(If you write a new function, you probably should instead return the PV in an
42SV with the UTF-8 flag of the SV properly set, rather than use this mechanism.)
43
44The possible values this can be are:
45
46=over
47
48=item C<UTF8NESS_YES>
49
50This means the string definitely should be treated as a sequence of
51UTF-8-encoded characters.
52
53Most code that needs to handle this typedef should be of the form:
54
55 if (utf8ness_flag == UTF8NESS_YES) {
56 treat as utf8; // like turning on an SV UTF-8 flag
57 }
58
59=item C<UTF8NESS_NO>
60
61This means the string definitely should be treated as a sequence of bytes, not
62encoded as UTF-8.
63
64=item C<UTF8NESS_IMMATERIAL>
65
66This means it is equally valid to treat the string as bytes, or as UTF-8
67characters; use whichever way you want. This happens when the string consists
68entirely of characters which have the same representation whether encoded in
69UTF-8 or not.
70
71=item C<UTF8NESS_UNKNOWN>
72
73This means it is unknown how the string should be treated. No core function
74will ever return this value to a non-core caller. Instead, it is used by the
75caller to initialize a variable to a non-legal value. A typical call will look like:
76
77 utf8ness_t string_is_utf8 = UTF8NESS_UNKNOWN
78 const char * string = foo(arg1, arg2, ..., &string_is_utf8);
79 if (string_is_utf8 == UTF8NESS_YES) {
80 do something for UTF-8;
81 }
82
83=back
84
85The following relationships hold between the enum values:
86
87=over
88
89=item S<C<0 E<lt>= I<enum value> E<lt>= UTF8NESS_IMMATERIAL>>
90
91the string may be treated in code as non-UTF8
92
93=item S<C<UTF8NESS_IMMATERIAL E<lt>= <I<enum value>>>
94
95the string may be treated in code as encoded in UTF-8
96
97=back
98
99=cut
100*/
101
102typedef enum {
103 UTF8NESS_NO = 0, /* Definitely not UTF-8 */
104 UTF8NESS_IMMATERIAL = 1, /* Representation is the same in UTF-8 as
105 not, so the UTF8ness doesn't actually
106 matter */
107 UTF8NESS_YES = 2, /* Defintely is UTF-8, wideness
108 unspecified */
8a6c2cd5 109 UTF8NESS_UNKNOWN = -1, /* Undetermined so far */
1cdf84d2
KW
110} utf8ness_t;
111
39e02b42 112/* Use UTF-8 as the default script encoding?
1e54db1a 113 * Turning this on will break scripts having non-UTF-8 binary
39e02b42
JH
114 * data (such as Latin-1) in string literals. */
115#ifdef USE_UTF8_SCRIPTS
116# define USE_UTF8_IN_NAMES (!IN_BYTES)
117#else
118# define USE_UTF8_IN_NAMES (PL_hints & HINT_UTF8)
119#endif
120
3cd96634
KW
121#include "regcharclass.h"
122#include "unicode_constants.h"
123
051a06d4 124/* For to_utf8_fold_flags, q.v. */
e4f4ef45
KW
125#define FOLD_FLAGS_LOCALE 0x1
126#define FOLD_FLAGS_FULL 0x2
127#define FOLD_FLAGS_NOMIX_ASCII 0x4
051a06d4 128
7bbfa158 129/*
7bbfa158
KW
130=for apidoc is_ascii_string
131
8871a094 132This is a misleadingly-named synonym for L</is_utf8_invariant_string>.
7bbfa158
KW
133On ASCII-ish platforms, the name isn't misleading: the ASCII-range characters
134are exactly the UTF-8 invariants. But EBCDIC machines have more invariants
8871a094
KW
135than just the ASCII characters, so C<is_utf8_invariant_string> is preferred.
136
137=for apidoc is_invariant_string
138
139This is a somewhat misleadingly-named synonym for L</is_utf8_invariant_string>.
140C<is_utf8_invariant_string> is preferred, as it indicates under what conditions
141the string is invariant.
7bbfa158
KW
142
143=cut
144*/
8871a094
KW
145#define is_ascii_string(s, len) is_utf8_invariant_string(s, len)
146#define is_invariant_string(s, len) is_utf8_invariant_string(s, len)
7bbfa158 147
33f38593
KW
148#define uvoffuni_to_utf8_flags(d,uv,flags) \
149 uvoffuni_to_utf8_flags_msgs(d, uv, flags, 0)
de69f3af
KW
150#define uvchr_to_utf8(a,b) uvchr_to_utf8_flags(a,b,0)
151#define uvchr_to_utf8_flags(d,uv,flags) \
33f38593
KW
152 uvchr_to_utf8_flags_msgs(d,uv,flags, 0)
153#define uvchr_to_utf8_flags_msgs(d,uv,flags,msgs) \
154 uvoffuni_to_utf8_flags_msgs(d,NATIVE_TO_UNI(uv),flags, msgs)
de69f3af 155#define utf8_to_uvchr_buf(s, e, lenp) \
9a9a6c98 156 utf8_to_uvchr_buf_helper((const U8 *) (s), (const U8 *) e, lenp)
f9380377
KW
157#define utf8n_to_uvchr(s, len, lenp, flags) \
158 utf8n_to_uvchr_error(s, len, lenp, flags, 0)
37657a5b
KW
159#define utf8n_to_uvchr_error(s, len, lenp, flags, errors) \
160 utf8n_to_uvchr_msgs(s, len, lenp, flags, errors, 0)
de69f3af 161
5fd26678
KW
162#define utf16_to_utf8(p, d, bytelen, newlen) \
163 utf16_to_utf8_base(p, d, bytelen, newlen, 0, 1)
164#define utf16_to_utf8_reversed(p, d, bytelen, newlen) \
165 utf16_to_utf8_base(p, d, bytelen, newlen, 1, 0)
6af810c4
KW
166#define utf8_to_utf16(p, d, bytelen, newlen) \
167 utf8_to_utf16_base(p, d, bytelen, newlen, 0, 1)
168#define utf8_to_utf16_reversed(p, d, bytelen, newlen) \
169 utf8_to_utf16_base(p, d, bytelen, newlen, 1, 0)
5fd26678 170
a0270393 171#define to_uni_fold(c, p, lenp) _to_uni_fold_flags(c, p, lenp, FOLD_FLAGS_FULL)
a239b1e2 172
eda9cac1 173#define foldEQ_utf8(s1, pe1, l1, u1, s2, pe2, l2, u2) \
1604cfb0 174 foldEQ_utf8_flags(s1, pe1, l1, u1, s2, pe2, l2, u2, 0)
baa60164 175#define FOLDEQ_UTF8_NOMIX_ASCII (1 << 0)
cea315b6 176#define FOLDEQ_LOCALE (1 << 1)
18f762c3
KW
177#define FOLDEQ_S1_ALREADY_FOLDED (1 << 2)
178#define FOLDEQ_S2_ALREADY_FOLDED (1 << 3)
d635b710
KW
179#define FOLDEQ_S1_FOLDS_SANE (1 << 4)
180#define FOLDEQ_S2_FOLDS_SANE (1 << 5)
a33c29bc 181
fcd03d92
KW
182/* This will be described more fully below, but it turns out that the
183 * fundamental difference between UTF-8 and UTF-EBCDIC is that the former has
184 * the upper 2 bits of a continuation byte be '10', and the latter has the
185 * upper 3 bits be '101', leaving 6 and 5 significant bits respectively.
186 *
187 * It is helpful to know the EBCDIC value on ASCII platforms, mainly to avoid
188 * some #ifdef's */
189#define UTF_EBCDIC_CONTINUATION_BYTE_INFO_BITS 5
190
e77f0df2
KW
191/* See explanation below at 'UTF8_MAXBYTES' */
192#define ASCII_PLATFORM_UTF8_MAXBYTES 13
f4d83e55 193
6736ce80 194#ifdef EBCDIC
f4d83e55 195
6736ce80
KW
196/* The equivalent of the next few macros but implementing UTF-EBCDIC are in the
197 * following header file: */
198# include "utfebcdic.h"
f4d83e55 199
030c8206 200# else /* ! EBCDIC */
f4d83e55 201
6736ce80 202START_EXTERN_C
111e8ed9 203
6736ce80
KW
204# ifndef DOINIT
205EXTCONST unsigned char PL_utf8skip[];
206# else
6f06b55f 207EXTCONST unsigned char PL_utf8skip[] = {
b2635aa8
KW
208/* 0x00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
209/* 0x10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
210/* 0x20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
211/* 0x30 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
212/* 0x40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
213/* 0x50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
214/* 0x60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
215/* 0x70 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
216/* 0x80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
217/* 0x90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
218/* 0xA0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
219/* 0xB0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
220/* 0xC0 */ 2,2, /* overlong */
1ff3baa2 221/* 0xC2 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* U+0080 to U+03FF */
b2635aa8
KW
222/* 0xD0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* U+0400 to U+07FF */
223/* 0xE0 */ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, /* U+0800 to U+FFFF */
224/* 0xF0 */ 4,4,4,4,4,4,4,4,5,5,5,5,6,6, /* above BMP to 2**31 - 1 */
6937f885
KW
225 /* Perl extended (never was official UTF-8). Up to 36 bit */
226/* 0xFE */ 7,
227 /* More extended, Up to 72 bits (64-bit + reserved) */
e77f0df2 228/* 0xFF */ ASCII_PLATFORM_UTF8_MAXBYTES
a0ed51b3 229};
6736ce80 230# endif
a0ed51b3 231
73c4f7a1 232END_EXTERN_C
7e2040f0 233
ef2a4c8f
KW
234/*
235
236=for apidoc Am|U8|NATIVE_TO_LATIN1|U8 ch
237
238Returns the Latin-1 (including ASCII and control characters) equivalent of the
239input native code point given by C<ch>. Thus, C<NATIVE_TO_LATIN1(193)> on
240EBCDIC platforms returns 65. These each represent the character C<"A"> on
241their respective platforms. On ASCII platforms no conversion is needed, so
242this macro expands to just its input, adding no time nor space requirements to
243the implementation.
244
245For conversion of code points potentially larger than will fit in a character,
246use L</NATIVE_TO_UNI>.
247
248=for apidoc Am|U8|LATIN1_TO_NATIVE|U8 ch
249
250Returns the native equivalent of the input Latin-1 code point (including ASCII
251and control characters) given by C<ch>. Thus, C<LATIN1_TO_NATIVE(66)> on
252EBCDIC platforms returns 194. These each represent the character C<"B"> on
253their respective platforms. On ASCII platforms no conversion is needed, so
254this macro expands to just its input, adding no time nor space requirements to
255the implementation.
256
257For conversion of code points potentially larger than will fit in a character,
258use L</UNI_TO_NATIVE>.
259
260=for apidoc Am|UV|NATIVE_TO_UNI|UV ch
261
262Returns the Unicode equivalent of the input native code point given by C<ch>.
263Thus, C<NATIVE_TO_UNI(195)> on EBCDIC platforms returns 67. These each
264represent the character C<"C"> on their respective platforms. On ASCII
265platforms no conversion is needed, so this macro expands to just its input,
266adding no time nor space requirements to the implementation.
267
268=for apidoc Am|UV|UNI_TO_NATIVE|UV ch
269
270Returns the native equivalent of the input Unicode code point given by C<ch>.
271Thus, C<UNI_TO_NATIVE(68)> on EBCDIC platforms returns 196. These each
272represent the character C<"D"> on their respective platforms. On ASCII
273platforms no conversion is needed, so this macro expands to just its input,
274adding no time nor space requirements to the implementation.
275
276=cut
277*/
278
e7285277
KW
279#define NATIVE_TO_LATIN1(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
280#define LATIN1_TO_NATIVE(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
59a449d5
KW
281
282/* I8 is an intermediate version of UTF-8 used only in UTF-EBCDIC. We thus
283 * consider it to be identical to UTF-8 on ASCII platforms. Strictly speaking
284 * UTF-8 and UTF-EBCDIC are two different things, but we often conflate them
285 * because they are 8-bit encodings that serve the same purpose in Perl, and
286 * rarely do we need to distinguish them. The term "NATIVE_UTF8" applies to
287 * whichever one is applicable on the current platform */
e7285277
KW
288#define NATIVE_UTF8_TO_I8(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
289#define I8_TO_NATIVE_UTF8(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
59a449d5 290
296969d3
KW
291#define UNI_TO_NATIVE(ch) ((UV) ASSERT_NOT_PTR(ch))
292#define NATIVE_TO_UNI(ch) ((UV) ASSERT_NOT_PTR(ch))
d7578b48 293
877d9f0d 294/*
9041c2e3 295
a14e0a36
KW
296 The following table is from Unicode 3.2, plus the Perl extensions for above
297 U+10FFFF
877d9f0d 298
a14e0a36 299 Code Points 1st Byte 2nd Byte 3rd 4th 5th 6th 7th 8th-13th
877d9f0d 300
375122d7 301 U+0000..U+007F 00..7F
e1b711da 302 U+0080..U+07FF * C2..DF 80..BF
a14e0a36
KW
303 U+0800..U+0FFF E0 * A0..BF 80..BF
304 U+1000..U+CFFF E1..EC 80..BF 80..BF
305 U+D000..U+D7FF ED 80..9F 80..BF
306 U+D800..U+DFFF ED A0..BF 80..BF (surrogates)
307 U+E000..U+FFFF EE..EF 80..BF 80..BF
308 U+10000..U+3FFFF F0 * 90..BF 80..BF 80..BF
309 U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
310 U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
311 Below are above-Unicode code points
312 U+110000..U+13FFFF F4 90..BF 80..BF 80..BF
313 U+110000..U+1FFFFF F5..F7 80..BF 80..BF 80..BF
314 U+200000..U+FFFFFF F8 * 88..BF 80..BF 80..BF 80..BF
315U+1000000..U+3FFFFFF F9..FB 80..BF 80..BF 80..BF 80..BF
316U+4000000..U+3FFFFFFF FC * 84..BF 80..BF 80..BF 80..BF 80..BF
317U+40000000..U+7FFFFFFF FD 80..BF 80..BF 80..BF 80..BF 80..BF
318U+80000000..U+FFFFFFFFF FE * 82..BF 80..BF 80..BF 80..BF 80..BF 80..BF
319U+1000000000.. FF 80..BF 80..BF 80..BF 80..BF 80..BF * 81..BF 80..BF
877d9f0d 320
e1b711da 321Note the gaps before several of the byte entries above marked by '*'. These are
37e2e78e
KW
322caused by legal UTF-8 avoiding non-shortest encodings: it is technically
323possible to UTF-8-encode a single code point in different ways, but that is
324explicitly forbidden, and the shortest possible encoding should always be used
15824458 325(and that is what Perl does). The non-shortest ones are called 'overlongs'.
8c007b5a 326
42b360b2 327Another way to look at it, as bits:
8c007b5a 328
b2635aa8 329 Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte
8c007b5a 330
b2635aa8
KW
331 0aaa aaaa 0aaa aaaa
332 0000 0bbb bbaa aaaa 110b bbbb 10aa aaaa
333 cccc bbbb bbaa aaaa 1110 cccc 10bb bbbb 10aa aaaa
334 00 000d ddcc cccc bbbb bbaa aaaa 1111 0ddd 10cc cccc 10bb bbbb 10aa aaaa
8c007b5a
JH
335
336As you can see, the continuation bytes all begin with C<10>, and the
e1b711da 337leading bits of the start byte tell how many bytes there are in the
8c007b5a
JH
338encoded character.
339
df863e43
KW
340Perl's extended UTF-8 means we can have start bytes up through FF, though any
341beginning with FF yields a code point that is too large for 32-bit ASCII
342platforms. FF signals to use 13 bytes for the encoded character. This breaks
343the paradigm that the number of leading bits gives how many total bytes there
ab2e28c2 344are in the character. */
38953e5a 345
15824458
KW
346/* This is the number of low-order bits a continuation byte in a UTF-8 encoded
347 * sequence contributes to the specification of the code point. In the bit
348 * maps above, you see that the first 2 bits are a constant '10', leaving 6 of
349 * real information */
36da1e17 350# define UTF_CONTINUATION_BYTE_INFO_BITS 6
b2635aa8 351
fed423a5
KW
352/* ^? is defined to be DEL on ASCII systems. See the definition of toCTRL()
353 * for more */
030c8206 354# define QUESTION_MARK_CTRL DEL_NATIVE
fed423a5 355
fed423a5
KW
356#endif /* EBCDIC vs ASCII */
357
28ca3ab5
KW
358/* It turns out that in a number of cases, that handling ASCII vs EBCDIC is a
359 * matter of being off-by-one. So this is a convenience macro, used to avoid
360 * some #ifdefs. */
361#define ONE_IF_EBCDIC_ZERO_IF_NOT \
362 (UTF_CONTINUATION_BYTE_INFO_BITS == UTF_EBCDIC_CONTINUATION_BYTE_INFO_BITS)
363
36da1e17
KW
364/* Since the significant bits in a continuation byte are stored in the
365 * least-significant positions, we often find ourselves shifting by that
366 * amount. This is a clearer name in such situations */
367#define UTF_ACCUMULATION_SHIFT UTF_CONTINUATION_BYTE_INFO_BITS
368
42b360b2
KW
369/* 2**info_bits - 1. This masks out all but the bits that carry real
370 * information in a continuation byte. This turns out to be 0x3F in UTF-8,
371 * 0x1F in UTF-EBCDIC. */
36da1e17
KW
372#define UTF_CONTINUATION_MASK \
373 ((U8) nBIT_MASK(UTF_CONTINUATION_BYTE_INFO_BITS))
fed423a5 374
9f3cfb7a
KW
375/* For use in UTF8_IS_CONTINUATION(). This turns out to be 0xC0 in UTF-8,
376 * E0 in UTF-EBCDIC */
2c96f462
TC
377#define UTF_IS_CONTINUATION_MASK \
378 ((U8) ((0xFF << UTF_ACCUMULATION_SHIFT) & 0xFF))
9f3cfb7a 379
38f458ff
KW
380/* This defines the bits that are to be in the continuation bytes of a
381 * multi-byte UTF-8 encoded character that mark it is a continuation byte.
382 * This turns out to be 0x80 in UTF-8, 0xA0 in UTF-EBCDIC. (khw doesn't know
fcd03d92
KW
383 * the underlying reason that B0 works here, except it just happens to work.
384 * One could solve for two linear equations and come up with it.) */
38f458ff
KW
385#define UTF_CONTINUATION_MARK (UTF_IS_CONTINUATION_MASK & 0xB0)
386
4a2c769e
KW
387/* This value is clearer in some contexts */
388#define UTF_MIN_CONTINUATION_BYTE UTF_CONTINUATION_MARK
389
f4225fa0
KW
390/* Is the byte 'c' part of a multi-byte UTF8-8 encoded sequence, and not the
391 * first byte thereof? */
392#define UTF8_IS_CONTINUATION(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
393 (((NATIVE_UTF8_TO_I8(c) & UTF_IS_CONTINUATION_MASK) \
394 == UTF_CONTINUATION_MARK)))
395
2dc97505
KW
396/* Is the representation of the Unicode code point 'cp' the same regardless of
397 * being encoded in UTF-8 or not? This is a fundamental property of
398 * UTF-8,EBCDIC */
4a2c769e
KW
399#define OFFUNI_IS_INVARIANT(c) \
400 (((WIDEST_UTYPE)(c)) < UTF_MIN_CONTINUATION_BYTE)
2dc97505 401
ab2e28c2
KW
402/*
403=for apidoc Am|bool|UVCHR_IS_INVARIANT|UV cp
404
405Evaluates to 1 if the representation of code point C<cp> is the same whether or
406not it is encoded in UTF-8; otherwise evaluates to 0. UTF-8 invariant
407characters can be copied as-is when converting to/from UTF-8, saving time.
408C<cp> is Unicode if above 255; otherwise is platform-native.
409
410=cut
411 */
412#define UVCHR_IS_INVARIANT(cp) (OFFUNI_IS_INVARIANT(NATIVE_TO_UNI(cp)))
413
c26e6896
KW
414/* This defines the 1-bits that are to be in the first byte of a multi-byte
415 * UTF-8 encoded character that mark it as a start byte and give the number of
416 * bytes that comprise the character. 'len' is that number.
417 *
418 * To illustrate: len = 2 => ((U8) ~ 0b0011_1111) or 1100_0000
419 * 7 => ((U8) ~ 0b0000_0001) or 1111_1110
420 * > 7 => 0xFF
421 *
422 * This is not to be used on a single-byte character. As in many places in
423 * perl, U8 must be 8 bits
424 */
425#define UTF_START_MARK(len) ((U8) ~(0xFF >> (len)))
426
0176a72c
KW
427/* Masks out the initial one bits in a start byte, leaving the following 0 bit
428 * and the real data bits. 'len' is the number of bytes in the multi-byte
429 * sequence that comprises the character.
430 *
431 * To illustrate: len = 2 => 0b0011_1111 works on start byte 110xxxxx
432 * 6 => 0b0000_0011 works on start byte 1111110x
433 * >= 7 => There are no data bits in the start byte
434 * Note that on ASCII platforms, this can be passed a len=1 byte; and all the
435 * real data bits will be returned:
436 len = 1 => 0b0111_1111
437 * This isn't true on EBCDIC platforms, where some len=1 bytes are of the form
438 * 0b101x_xxxx, so this can't be used there on single-byte characters. */
439#define UTF_START_MASK(len) (0xFF >> (len))
440
e77f0df2
KW
441/*
442
443=for apidoc AmnU|STRLEN|UTF8_MAXBYTES
444
445The maximum width of a single UTF-8 encoded character, in bytes.
446
447NOTE: Strictly speaking Perl's UTF-8 should not be called UTF-8 since UTF-8
448is an encoding of Unicode, and Unicode's upper limit, 0x10FFFF, can be
449expressed with 4 bytes. However, Perl thinks of UTF-8 as a way to encode
450non-negative integers in a binary format, even those above Unicode.
451
452=cut
453
454The start byte 0xFE, never used in any ASCII platform UTF-8 specification, has
455an obvious meaning, namely it has its upper 7 bits set, so it should start a
456sequence of 7 bytes. And in fact, this is exactly what standard UTF-EBCDIC
457does.
458
459The start byte FF, on the other hand could have several different plausible
460meanings:
461 1) The meaning in standard UTF-EBCDIC, namely as an FE start byte, with the
462 bottom bit that should be a fixed '0' to form FE, instead acting as an
463 info bit, 0 or 1.
464 2) That the sequence should have exactly 8 bytes.
465 3) That the next byte is to be treated as a sort of extended start byte,
466 which in combination with this one gives the total length of the sequence.
467 There are published UTF-8 extensions that do this, some string together
468 multiple initial FF start bytes to achieve arbitrary precision.
469 4) That the sequence has exactly n bytes, where n is what the implementation
470 chooses.
471
472Perl has chosen 4).
473The goal is to be able to represent 64-bit values in UTF-8 or UTF-EBCDIC. That
474rules out items 1) and 2). Item 3) has the deal-breaking disadvantage of
475requiring one to read more than one byte to determine the total length of the
476sequence. So in Perl, a start byte of FF indicates a UTF-8 string consisting
477of the start byte, plus enough continuation bytes to encode a 64 bit value.
478This turns out to be 13 total bytes in UTF-8 and 14 in UTF-EBCDIC. This is
479because we get zero info bits from the start byte, plus
480 12 * 6 bits of info per continuation byte (could encode 72-bit numbers) on
481 UTF-8 (khw knows not why 11, which would encode 66 bits wasn't
482 chosen instead); and
483 13 * 5 bits of info per byte (could encode 65-bit numbers) on UTF-EBCDIC
484
485The disadvantages of this method are:
486 1) There's potentially a lot of wasted bytes for all but the largest values.
487 For example, something that could be represented by 7 continuation bytes,
488 instead requires the full 12 or 13.
489 2) There would be problems should larger values, 128-bit say, ever need to be
490 represented.
491
492WARNING: This number must be in sync with the value in
493regen/charset_translations.pl. */
494#define UTF8_MAXBYTES \
495 (ASCII_PLATFORM_UTF8_MAXBYTES + ONE_IF_EBCDIC_ZERO_IF_NOT)
496
7bf011a1
KW
497/* Calculate how many bytes are necessary to represent a value whose most
498 * significant 1 bit is in bit position 'pos' of the word. For 0x1, 'pos would
499 * be 0; and for 0x400, 'pos' would be 10, and the result would be:
500 * EBCDIC floor((-1 + (10 + 5 - 1 - 1)) / (5 - 1))
501 * = floor((-1 + (13)) / 4)
502 * = floor(12 / 4)
503 * = 3
504 * ASCII floor(( 0 + (10 + 6 - 1 - 1)) / (6 - 1))
505 * = floor(14 / 5)
506 * = 2
507 * The reason this works is because the number of bits needed to represent a
508 * value is proportional to (UTF_CONTINUATION_BYTE_INFO_BITS - 1). The -1 is
509 * because each new continuation byte removes one bit of information from the
510 * start byte.
511 *
512 * This is a step function (we need to allocate a full extra byte if we
513 * overflow by just a single bit)
514 *
515 * The caller is responsible for making sure 'pos' is at least 8 (occupies 9
516 * bits), as it breaks down at the lower edge. At the high end, if it returns
517 * 8 or more, Perl instead anomalously uses MAX_BYTES, so this would be wrong.
518 * */
519#define UNISKIP_BY_MSB_(pos) \
520 ( ( -ONE_IF_EBCDIC_ZERO_IF_NOT /* platform break pos's are off-by-one */ \
521 + (pos) + ((UTF_CONTINUATION_BYTE_INFO_BITS - 1) - 1)) /* Step fcn */ \
522 / (UTF_CONTINUATION_BYTE_INFO_BITS - 1)) /* take floor of */
523
787e8384
KW
524/* Compute the number of UTF-8 bytes required for representing the input uv,
525 * which must be a Unicode, not native value.
7028aeba 526 *
787e8384
KW
527 * This uses msbit_pos() which doesn't work on NUL, and UNISKIP_BY_MSB_ breaks
528 * down for small code points. So first check if the input is invariant to get
529 * around that, and use a helper for high code points to accommodate the fact
530 * that above 7 btyes, the value is anomalous. The helper is empty on
531 * platforms that don't go that high */
532#define OFFUNISKIP(uv) \
533 ((OFFUNI_IS_INVARIANT(uv)) \
534 ? 1 \
535 : (OFFUNISKIP_helper_(uv) UNISKIP_BY_MSB_(msbit_pos(uv))))
536
537/* We need to go to MAX_BYTES when we can't represent 'uv' by the number of
538 * information bits in 6 continuation bytes (when we get to 6, the start byte
539 * has no information bits to add to the total). But on 32-bit ASCII
540 * platforms, that doesn't happen until 6*6 bits, so on those platforms, this
541 * will always be false */
542#if UVSIZE * CHARBITS > (6 * UTF_CONTINUATION_BYTE_INFO_BITS)
788cdc67 543# define HAS_EXTRA_LONG_UTF8
787e8384
KW
544# define OFFUNISKIP_helper_(uv) \
545 UNLIKELY(uv > nBIT_UMAX(6 * UTF_CONTINUATION_BYTE_INFO_BITS)) \
546 ? UTF8_MAXBYTES :
1d68d6cd 547#else
787e8384 548# define OFFUNISKIP_helper_(uv)
1d68d6cd
SC
549#endif
550
5352a763
KW
551/*
552
553=for apidoc Am|STRLEN|UVCHR_SKIP|UV cp
554returns the number of bytes required to represent the code point C<cp> when
555encoded as UTF-8. C<cp> is a native (ASCII or EBCDIC) code point if less than
556255; a Unicode code point otherwise.
557
558=cut
559 */
787e8384 560#define UVCHR_SKIP(uv) OFFUNISKIP(NATIVE_TO_UNI(uv))
5352a763 561
222d7630
KW
562#define NATIVE_SKIP(uv) UVCHR_SKIP(uv) /* Old terminology */
563
564/* Most code which says UNISKIP is really thinking in terms of native code
565 * points (0-255) plus all those beyond. This is an imprecise term, but having
566 * it means existing code continues to work. For precision, use UVCHR_SKIP,
567 * NATIVE_SKIP, or OFFUNISKIP */
568#define UNISKIP(uv) UVCHR_SKIP(uv)
569
bdcc1e93
KW
570/* Compute the start byte for a given code point. This requires the log2 of
571 * the code point, which is hard to compute at compile time, which this macro
572 * wants to be. (Perhaps deBruijn sequences could be used.) So a parameter
573 * for the number of bits the value occupies is passed in, which the programmer
574 * has had to figure out to get compile-time effect. And asserts are used to
575 * make sure the value is correct.
576 *
577 * Since we are interested only in the start byte, we ignore the lower bits
578 * accounted for by the continuation bytes. Each continuation byte eats up
579 * UTF_CONTINUATION_BYTE_INFO_BITS bits, so the number of continuation bytes
580 * needed is floor(bits / UTF_CONTINUATION_BYTE_INFO_BITS). That number is fed
581 * to UTF_START_MARK() to get the upper part of the start byte. The left over
582 * bits form the lower part which is OR'd with the mark
583 *
584 * Note that on EBCDIC platforms, this is actually the I8 */
585#define UTF_START_BYTE(uv, bits) \
586 (__ASSERT_((uv) >> ((bits) - 1)) /* At least 'bits' */ \
587 __ASSERT_(((uv) & ~nBIT_MASK(bits)) == 0) /* No extra bits */ \
588 UTF_START_MARK(UNISKIP_BY_MSB_((bits) - 1)) \
589 | ((uv) >> (((bits) / UTF_CONTINUATION_BYTE_INFO_BITS) \
590 * UTF_CONTINUATION_BYTE_INFO_BITS)))
591
592/* Compute the first continuation byte for a given code point. This is mostly
593 * for compile-time, so how many bits it occupies is also passed in).
594 *
595 * We are interested in the first continuation byte, so we ignore the lower
596 * bits accounted for by the rest of the continuation bytes by right shifting
597 * out their info bit, and mask out the higher bits that will go into the start
598 * byte.
599 *
600 * Note that on EBCDIC platforms, this is actually the I8 */
601#define UTF_FIRST_CONT_BYTE(uv, bits) \
602 (__ASSERT_((uv) >> ((bits) - 1)) /* At least 'bits' */ \
603 __ASSERT_(((uv) & ~nBIT_MASK(bits)) == 0) /* No extra bits */ \
604 UTF_CONTINUATION_MARK \
605 | ( UTF_CONTINUATION_MASK \
606 & ((uv) >> ((((bits) / UTF_CONTINUATION_BYTE_INFO_BITS) - 1) \
607 * UTF_CONTINUATION_BYTE_INFO_BITS))))
608
609#define UTF_MIN_START_BYTE UTF_START_BYTE(UTF_MIN_CONTINUATION_BYTE, 8)
4bab39bc
KW
610
611/* Is the byte 'c' the first byte of a multi-byte UTF8-8 encoded sequence?
59645eb1 612 * This excludes invariants (they are single-byte). It also excludes the
4bab39bc 613 * illegal overlong sequences that begin with C0 and C1 on ASCII platforms, and
59645eb1
KW
614 * C0-C4 I8 start bytes on EBCDIC ones. On EBCDIC E0 can't start a
615 * non-overlong sequence, so we define a base macro and for those platforms,
616 * extend it to also exclude E0 */
617#define UTF8_IS_START_base(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
4bab39bc 618 (NATIVE_UTF8_TO_I8(c) >= UTF_MIN_START_BYTE))
59645eb1
KW
619#ifdef EBCDIC
620# define UTF8_IS_START(c) \
621 (UTF8_IS_START_base(c) && (c) != I8_TO_NATIVE_UTF8(0xE0))
622#else
623# define UTF8_IS_START(c) UTF8_IS_START_base(c)
624#endif
4bab39bc 625
bdcc1e93 626#define UTF_MIN_ABOVE_LATIN1_BYTE UTF_START_BYTE(0x100, 9)
1df63428
KW
627
628/* Is the UTF8-encoded byte 'c' the first byte of a sequence of bytes that
629 * represent a code point > 255? */
630#define UTF8_IS_ABOVE_LATIN1(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
631 (NATIVE_UTF8_TO_I8(c) >= UTF_MIN_ABOVE_LATIN1_BYTE))
632
7c88d61e
KW
633/* Is the UTF8-encoded byte 'c' the first byte of a two byte sequence? Use
634 * UTF8_IS_NEXT_CHAR_DOWNGRADEABLE() instead if the input isn't known to
635 * be well-formed. */
636#define UTF8_IS_DOWNGRADEABLE_START(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
03dc0b1b 637 inRANGE_helper_(U8, NATIVE_UTF8_TO_I8(c), \
7c88d61e
KW
638 UTF_MIN_START_BYTE, UTF_MIN_ABOVE_LATIN1_BYTE - 1))
639
b651802e 640/* The largest code point representable by two UTF-8 bytes on this platform.
03a8ddc0
KW
641 * The binary for that code point is:
642 * 1101_1111 10xx_xxxx in UTF-8, and
643 * 1101_1111 101y_yyyy in UTF-EBCDIC I8.
644 * where both x and y are 1, and shown this way to indicate there is one more x
645 * than there is y. The number of x and y bits are their platform's respective
646 * UTF_CONTINUATION_BYTE_INFO_BITS. Squeezing out the bits that don't
647 * contribute to the value, these evaluate to:
648 * 1_1111 xx_xxxx in UTF-8, and
649 * 1_1111 y_yyyy in UTF-EBCDIC I8.
650 * or, the maximum value of an unsigned with (5 + info_bit_count) bits */
651#define MAX_UTF8_TWO_BYTE nBIT_UMAX(5 + UTF_CONTINUATION_BYTE_INFO_BITS)
aa206fb7 652
b651802e 653/* The largest code point representable by two UTF-8 bytes on any platform that
fcd03d92
KW
654 * Perl runs on. */
655#define MAX_PORTABLE_UTF8_TWO_BYTE \
656 nBIT_UMAX(5 + MIN( UTF_CONTINUATION_BYTE_INFO_BITS, \
657 UTF_EBCDIC_CONTINUATION_BYTE_INFO_BITS))
aa206fb7 658
f2c50040
KW
659/*
660
661=for apidoc AmnU|STRLEN|UTF8_MAXBYTES_CASE
662
663The maximum number of UTF-8 bytes a single Unicode character can
664uppercase/lowercase/titlecase/fold into.
665
666=cut
667
668 * Unicode guarantees that the maximum expansion is UTF8_MAX_FOLD_CHAR_EXPAND
669 * characters, but any above-Unicode code point will fold to itself, so we only
670 * have to look at the expansion of the maximum Unicode code point. But this
671 * number may be less than the space occupied by a very large code point under
672 * Perl's extended UTF-8. We have to make it large enough to fit any single
673 * character. (It turns out that ASCII and EBCDIC differ in which is larger)
674 *
675=cut
676*/
7bf011a1
KW
677#define UTF8_MAXBYTES_CASE \
678 MAX(UTF8_MAXBYTES, UTF8_MAX_FOLD_CHAR_EXPAND * UNISKIP_BY_MSB_(20))
c03c0950 679
d06134e5
KW
680/* Rest of these are attributes of Unicode and perl's internals rather than the
681 * encoding, or happen to be the same in both ASCII and EBCDIC (at least at
682 * this level; the macros that some of these call may have different
683 * definitions in the two encodings */
684
59a449d5
KW
685/* In domain restricted to ASCII, these may make more sense to the reader than
686 * the ones with Latin1 in the name */
687#define NATIVE_TO_ASCII(ch) NATIVE_TO_LATIN1(ch)
688#define ASCII_TO_NATIVE(ch) LATIN1_TO_NATIVE(ch)
689
690/* More or less misleadingly-named defines, retained for back compat */
691#define NATIVE_TO_UTF(ch) NATIVE_UTF8_TO_I8(ch)
692#define NATIVE_TO_I8(ch) NATIVE_UTF8_TO_I8(ch)
693#define UTF_TO_NATIVE(ch) I8_TO_NATIVE_UTF8(ch)
694#define I8_TO_NATIVE(ch) I8_TO_NATIVE_UTF8(ch)
695#define NATIVE8_TO_UNI(ch) NATIVE_TO_LATIN1(ch)
d06134e5 696
537124e4
KW
697/* Adds a UTF8 continuation byte 'new' of information to a running total code
698 * point 'old' of all the continuation bytes so far. This is designed to be
155d2738
KW
699 * used in a loop to convert from UTF-8 to the code point represented. Note
700 * that this is asymmetric on EBCDIC platforms, in that the 'new' parameter is
701 * the UTF-EBCDIC byte, whereas the 'old' parameter is a Unicode (not EBCDIC)
702 * code point in process of being generated */
a6951642
KW
703#define UTF8_ACCUMULATE(old, new) (__ASSERT_(FITS_IN_8_BITS(new)) \
704 ((old) << UTF_ACCUMULATION_SHIFT) \
009097b1 705 | ((NATIVE_UTF8_TO_I8(new)) \
155d2738 706 & UTF_CONTINUATION_MASK))
d06134e5 707
4ab10950 708/* This works in the face of malformed UTF-8. */
4e1ed312 709#define UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, e) \
0b47b392
KW
710 ( ( (e) - (s) > 1) \
711 && UTF8_IS_DOWNGRADEABLE_START(*(s)) \
4e1ed312 712 && UTF8_IS_CONTINUATION(*((s)+1)))
4ab10950 713
3c0792e4
KW
714/* Longer, but more accurate name */
715#define UTF8_IS_ABOVE_LATIN1_START(c) UTF8_IS_ABOVE_LATIN1(c)
716
a62b247b
KW
717/* Convert a UTF-8 variant Latin1 character to a native code point value.
718 * Needs just one iteration of accumulate. Should be used only if it is known
719 * that the code point is < 256, and is not UTF-8 invariant. Use the slower
720 * but more general TWO_BYTE_UTF8_TO_NATIVE() which handles any code point
721 * representable by two bytes (which turns out to be up through
722 * MAX_PORTABLE_UTF8_TWO_BYTE). The two parameters are:
723 * HI: a downgradable start byte;
724 * LO: continuation.
725 * */
726#define EIGHT_BIT_UTF8_TO_NATIVE(HI, LO) \
727 ( __ASSERT_(UTF8_IS_DOWNGRADEABLE_START(HI)) \
728 __ASSERT_(UTF8_IS_CONTINUATION(LO)) \
729 LATIN1_TO_NATIVE(UTF8_ACCUMULATE(( \
730 NATIVE_UTF8_TO_I8(HI) & UTF_START_MASK(2)), (LO))))
731
94bb8c36 732/* Convert a two (not one) byte utf8 character to a native code point value.
2950f2a7
KW
733 * Needs just one iteration of accumulate. Should not be used unless it is
734 * known that the two bytes are legal: 1) two-byte start, and 2) continuation.
735 * Note that the result can be larger than 255 if the input character is not
736 * downgradable */
94bb8c36 737#define TWO_BYTE_UTF8_TO_NATIVE(HI, LO) \
a6951642
KW
738 (__ASSERT_(FITS_IN_8_BITS(HI)) \
739 __ASSERT_(FITS_IN_8_BITS(LO)) \
46b0a3a2 740 __ASSERT_(PL_utf8skip[(U8) HI] == 2) \
a6951642 741 __ASSERT_(UTF8_IS_CONTINUATION(LO)) \
94bb8c36 742 UNI_TO_NATIVE(UTF8_ACCUMULATE((NATIVE_UTF8_TO_I8(HI) & UTF_START_MASK(2)), \
635e76f5 743 (LO))))
94bb8c36
KW
744
745/* Should never be used, and be deprecated */
746#define TWO_BYTE_UTF8_TO_UNI(HI, LO) NATIVE_TO_UNI(TWO_BYTE_UTF8_TO_NATIVE(HI, LO))
2950f2a7 747
bd18bd40
KW
748/*
749
750=for apidoc Am|STRLEN|UTF8SKIP|char* s
ee0ff0f5
KW
751returns the number of bytes a non-malformed UTF-8 encoded character whose first
752(perhaps only) byte is pointed to by C<s>.
753
754If there is a possibility of malformed input, use instead:
755
756=over
757
eb992c6f 758=item C<L</UTF8_SAFE_SKIP>> if you know the maximum ending pointer in the
ee0ff0f5
KW
759buffer pointed to by C<s>; or
760
eb992c6f 761=item C<L</UTF8_CHK_SKIP>> if you don't know it.
ee0ff0f5
KW
762
763=back
764
765It is better to restructure your code so the end pointer is passed down so that
766you know what it actually is at the point of this call, but if that isn't
eb992c6f 767possible, C<L</UTF8_CHK_SKIP>> can minimize the chance of accessing beyond the end
ee0ff0f5 768of the input buffer.
bd18bd40
KW
769
770=cut
771 */
08e4e7bf 772#define UTF8SKIP(s) PL_utf8skip[*(const U8*)(ASSERT_IS_PTR(s))]
a281f16c
KW
773
774/*
775=for apidoc Am|STRLEN|UTF8_SKIP|char* s
eb992c6f 776This is a synonym for C<L</UTF8SKIP>>
a281f16c
KW
777
778=cut
779*/
780
2a70536e 781#define UTF8_SKIP(s) UTF8SKIP(s)
d06134e5 782
85fcc8f2 783/*
ee0ff0f5
KW
784=for apidoc Am|STRLEN|UTF8_CHK_SKIP|char* s
785
eb992c6f
KW
786This is a safer version of C<L</UTF8SKIP>>, but still not as safe as
787C<L</UTF8_SAFE_SKIP>>. This version doesn't blindly assume that the input
ee0ff0f5
KW
788string pointed to by C<s> is well-formed, but verifies that there isn't a NUL
789terminating character before the expected end of the next character in C<s>.
790The length C<UTF8_CHK_SKIP> returns stops just before any such NUL.
791
792Perl tends to add NULs, as an insurance policy, after the end of strings in
793SV's, so it is likely that using this macro will prevent inadvertent reading
794beyond the end of the input buffer, even if it is malformed UTF-8.
795
796This macro is intended to be used by XS modules where the inputs could be
797malformed, and it isn't feasible to restructure to use the safer
eb992c6f 798C<L</UTF8_SAFE_SKIP>>, for example when interfacing with a C library.
ee0ff0f5
KW
799
800=cut
801*/
802
803#define UTF8_CHK_SKIP(s) \
8974941d 804 (UNLIKELY(s[0] == '\0') ? 1 : MIN(UTF8SKIP(s), \
f87d8789 805 my_strnlen((char *) (s), UTF8SKIP(s))))
ee0ff0f5 806/*
85fcc8f2
KW
807
808=for apidoc Am|STRLEN|UTF8_SAFE_SKIP|char* s|char* e
45671da2
KW
809returns 0 if S<C<s E<gt>= e>>; otherwise returns the number of bytes in the
810UTF-8 encoded character whose first byte is pointed to by C<s>. But it never
811returns beyond C<e>. On DEBUGGING builds, it asserts that S<C<s E<lt>= e>>.
85fcc8f2
KW
812
813=cut
814 */
45671da2 815#define UTF8_SAFE_SKIP(s, e) (__ASSERT_((e) >= (s)) \
8974941d 816 UNLIKELY(((e) - (s)) <= 0) \
45671da2
KW
817 ? 0 \
818 : MIN(((e) - (s)), UTF8_SKIP(s)))
85fcc8f2 819
2d1545e5
KW
820/* Most code that says 'UNI_' really means the native value for code points up
821 * through 255 */
822#define UNI_IS_INVARIANT(cp) UVCHR_IS_INVARIANT(cp)
823
c2b32798
KW
824/*
825=for apidoc Am|bool|UTF8_IS_INVARIANT|char c
826
827Evaluates to 1 if the byte C<c> represents the same character when encoded in
828UTF-8 as when not; otherwise evaluates to 0. UTF-8 invariant characters can be
829copied as-is when converting to/from UTF-8, saving time.
830
831In spite of the name, this macro gives the correct result if the input string
832from which C<c> comes is not encoded in UTF-8.
833
834See C<L</UVCHR_IS_INVARIANT>> for checking if a UV is invariant.
835
836=cut
837
838The reason it works on both UTF-8 encoded strings and non-UTF-8 encoded, is
839that it returns TRUE in each for the exact same set of bit patterns. It is
840valid on a subset of what UVCHR_IS_INVARIANT is valid on, so can just use that;
841and the compiler should optimize out anything extraneous given the
296969d3
KW
842implementation of the latter. */
843#define UTF8_IS_INVARIANT(c) UVCHR_IS_INVARIANT(ASSERT_NOT_PTR(c))
5fc230f1
KW
844
845/* Like the above, but its name implies a non-UTF8 input, which as the comments
846 * above show, doesn't matter as to its implementation */
38953e5a 847#define NATIVE_BYTE_IS_INVARIANT(c) UVCHR_IS_INVARIANT(c)
d06134e5 848
2c03e801
KW
849/* Misleadingly named: is the UTF8-encoded byte 'c' part of a variant sequence
850 * in UTF-8? This is the inverse of UTF8_IS_INVARIANT. */
851#define UTF8_IS_CONTINUED(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
852 (! UTF8_IS_INVARIANT(c)))
853
48ccf5e1
KW
854/* The macros in the next 4 sets are used to generate the two utf8 or utfebcdic
855 * bytes from an ordinal that is known to fit into exactly two (not one) bytes;
856 * it must be less than 0x3FF to work across both encodings. */
857
858/* These two are helper macros for the other three sets, and should not be used
859 * directly anywhere else. 'translate_function' is either NATIVE_TO_LATIN1
1ff3baa2
KW
860 * (which works for code points up through 0xFF) or NATIVE_TO_UNI which works
861 * for any code point */
48ccf5e1 862#define __BASE_TWO_BYTE_HI(c, translate_function) \
2863dafa 863 (__ASSERT_(! UVCHR_IS_INVARIANT(c)) \
48ccf5e1 864 I8_TO_NATIVE_UTF8((translate_function(c) >> UTF_ACCUMULATION_SHIFT) \
2863dafa 865 | UTF_START_MARK(2)))
48ccf5e1 866#define __BASE_TWO_BYTE_LO(c, translate_function) \
2863dafa 867 (__ASSERT_(! UVCHR_IS_INVARIANT(c)) \
48ccf5e1 868 I8_TO_NATIVE_UTF8((translate_function(c) & UTF_CONTINUATION_MASK) \
2863dafa 869 | UTF_CONTINUATION_MARK))
48ccf5e1 870
48ccf5e1
KW
871/* The next two macros should not be used. They were designed to be usable as
872 * the case label of a switch statement, but this doesn't work for EBCDIC. Use
9d0d3a03 873 * regen/unicode_constants.pl instead */
48ccf5e1
KW
874#define UTF8_TWO_BYTE_HI_nocast(c) __BASE_TWO_BYTE_HI(c, NATIVE_TO_UNI)
875#define UTF8_TWO_BYTE_LO_nocast(c) __BASE_TWO_BYTE_LO(c, NATIVE_TO_UNI)
876
877/* The next two macros are used when the source should be a single byte
878 * character; checked for under DEBUGGING */
879#define UTF8_EIGHT_BIT_HI(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
4c8cd605 880 ( __BASE_TWO_BYTE_HI(c, NATIVE_TO_LATIN1)))
48ccf5e1 881#define UTF8_EIGHT_BIT_LO(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
4c8cd605 882 (__BASE_TWO_BYTE_LO(c, NATIVE_TO_LATIN1)))
48ccf5e1
KW
883
884/* These final two macros in the series are used when the source can be any
885 * code point whose UTF-8 is known to occupy 2 bytes; they are less efficient
886 * than the EIGHT_BIT versions on EBCDIC platforms. We use the logical '~'
887 * operator instead of "<=" to avoid getting compiler warnings.
d52b8576 888 * MAX_UTF8_TWO_BYTE should be exactly all one bits in the lower few
48ccf5e1
KW
889 * places, so the ~ works */
890#define UTF8_TWO_BYTE_HI(c) \
891 (__ASSERT_((sizeof(c) == 1) \
d52b8576 892 || !(((WIDEST_UTYPE)(c)) & ~MAX_UTF8_TWO_BYTE)) \
4c8cd605 893 (__BASE_TWO_BYTE_HI(c, NATIVE_TO_UNI)))
48ccf5e1
KW
894#define UTF8_TWO_BYTE_LO(c) \
895 (__ASSERT_((sizeof(c) == 1) \
d52b8576 896 || !(((WIDEST_UTYPE)(c)) & ~MAX_UTF8_TWO_BYTE)) \
4c8cd605 897 (__BASE_TWO_BYTE_LO(c, NATIVE_TO_UNI)))
d06134e5 898
e7214ce8
KW
899/* This is illegal in any well-formed UTF-8 in both EBCDIC and ASCII
900 * as it is only in overlongs. */
901#define ILLEGAL_UTF8_BYTE I8_TO_NATIVE_UTF8(0xC1)
902
7e2040f0 903/*
e3036cf4 904 * 'UTF' is whether or not p is encoded in UTF8. The names 'foo_lazy_if' stem
20df05f4
KW
905 * from an earlier version of these macros in which they didn't call the
906 * foo_utf8() macros (i.e. were 'lazy') unless they decided that *p is the
907 * beginning of a utf8 character. Now that foo_utf8() determines that itself,
908 * no need to do it again here
7e2040f0 909 */
da8c1a98
KW
910#define isIDFIRST_lazy_if_safe(p, e, UTF) \
911 ((IN_BYTES || !UTF) \
912 ? isIDFIRST(*(p)) \
913 : isIDFIRST_utf8_safe(p, e))
da8c1a98
KW
914#define isWORDCHAR_lazy_if_safe(p, e, UTF) \
915 ((IN_BYTES || !UTF) \
916 ? isWORDCHAR(*(p)) \
917 : isWORDCHAR_utf8_safe((U8 *) p, (U8 *) e))
4c1d9526 918#define isALNUM_lazy_if_safe(p, e, UTF) isWORDCHAR_lazy_if_safe(p, e, UTF)
da8c1a98 919
030c8206 920#define UTF8_MAXLEN UTF8_MAXBYTES
89ebb4a3 921
8cb75cc8 922/* A Unicode character can fold to up to 3 characters */
030c8206 923#define UTF8_MAX_FOLD_CHAR_EXPAND 3
8cb75cc8 924
030c8206 925#define IN_BYTES UNLIKELY(CopHINTS_get(PL_curcop) & HINT_BYTES)
bd18bd40
KW
926
927/*
928
929=for apidoc Am|bool|DO_UTF8|SV* sv
930Returns a bool giving whether or not the PV in C<sv> is to be treated as being
931encoded in UTF-8.
932
933You should use this I<after> a call to C<SvPV()> or one of its variants, in
934case any call to string overloading updates the internal UTF-8 encoding flag.
935
936=cut
937*/
0064a8a9 938#define DO_UTF8(sv) (SvUTF8(sv) && !IN_BYTES)
1ff3baa2
KW
939
940/* Should all strings be treated as Unicode, and not just UTF-8 encoded ones?
941 * Is so within 'feature unicode_strings' or 'locale :not_characters', and not
2d34d1ba
KW
942 * within 'use bytes'. UTF-8 locales are not tested for here, because it gets
943 * complicated by the probability of having categories in different locales. */
70844984 944#define IN_UNI_8_BIT \
1604cfb0 945 (( ( (CopHINTS_get(PL_curcop) & HINT_UNI_8_BIT)) \
70844984
KW
946 || ( CopHINTS_get(PL_curcop) & HINT_LOCALE_PARTIAL \
947 /* -1 below is for :not_characters */ \
948 && _is_in_locale_category(FALSE, -1))) \
949 && (! IN_BYTES))
b36bf33f 950
6110285c
KW
951#define UNICODE_SURROGATE_FIRST 0xD800
952#define UNICODE_SURROGATE_LAST 0xDFFF
953
954/*
39fafb79
KW
955=for apidoc Am|bool|UNICODE_IS_SURROGATE|const UV uv
956
957Returns a boolean as to whether or not C<uv> is one of the Unicode surrogate
958code points
959
6110285c
KW
960=for apidoc Am|bool|UTF8_IS_SURROGATE|const U8 *s|const U8 *e
961
962Evaluates to non-zero if the first few bytes of the string starting at C<s> and
963looking no further than S<C<e - 1>> are well-formed UTF-8 that represents one
964of the Unicode surrogate code points; otherwise it evaluates to 0. If
965non-zero, the value gives how many bytes starting at C<s> comprise the code
966point's representation.
967
968=cut
969 */
970
6110285c
KW
971#define UNICODE_IS_SURROGATE(uv) UNLIKELY(inRANGE(uv, UNICODE_SURROGATE_FIRST, \
972 UNICODE_SURROGATE_LAST))
973#define UTF8_IS_SURROGATE(s, e) is_SURROGATE_utf8_safe(s, e)
974
975/*
976
977=for apidoc AmnU|UV|UNICODE_REPLACEMENT
978
979Evaluates to 0xFFFD, the code point of the Unicode REPLACEMENT CHARACTER
980
39fafb79
KW
981=for apidoc Am|bool|UNICODE_IS_REPLACEMENT|const UV uv
982
983Returns a boolean as to whether or not C<uv> is the Unicode REPLACEMENT
984CHARACTER
985
986=for apidoc Am|bool|UTF8_IS_REPLACEMENT|const U8 *s|const U8 *e
987
988Evaluates to non-zero if the first few bytes of the string starting at C<s> and
989looking no further than S<C<e - 1>> are well-formed UTF-8 that represents the
990Unicode REPLACEMENT CHARACTER; otherwise it evaluates to 0. If non-zero, the
991value gives how many bytes starting at C<s> comprise the code point's
992representation.
993
6110285c
KW
994=cut
995 */
996#define UNICODE_REPLACEMENT 0xFFFD
997#define UNICODE_IS_REPLACEMENT(uv) UNLIKELY((UV) (uv) == UNICODE_REPLACEMENT)
3c9bbd85
YO
998#define UTF8_IS_REPLACEMENT(s, send) \
999 UNLIKELY( \
1000 ((send) - (s)) >= ((SSize_t)(sizeof(REPLACEMENT_CHARACTER_UTF8) - 1))\
1001 && memEQ((s), REPLACEMENT_CHARACTER_UTF8, \
ae005663 1002 sizeof(REPLACEMENT_CHARACTER_UTF8) - 1))
6110285c 1003
42b360b2 1004/* Max legal code point according to Unicode */
6110285c
KW
1005#define PERL_UNICODE_MAX 0x10FFFF
1006
39fafb79
KW
1007/*
1008
1009=for apidoc Am|bool|UNICODE_IS_SUPER|const UV uv
1010
1011Returns a boolean as to whether or not C<uv> is above the maximum legal Unicode
1012code point of U+10FFFF.
1013
1014=cut
1015*/
1016
030c8206 1017#define UNICODE_IS_SUPER(uv) UNLIKELY((UV) (uv) > PERL_UNICODE_MAX)
6110285c
KW
1018
1019/*
1020=for apidoc Am|bool|UTF8_IS_SUPER|const U8 *s|const U8 *e
1021
1022Recall that Perl recognizes an extension to UTF-8 that can encode code
1023points larger than the ones defined by Unicode, which are 0..0x10FFFF.
1024
1025This macro evaluates to non-zero if the first few bytes of the string starting
1026at C<s> and looking no further than S<C<e - 1>> are from this UTF-8 extension;
7ce5b055 1027otherwise it evaluates to 0. If non-zero, the return is how many bytes
6110285c
KW
1028starting at C<s> comprise the code point's representation.
1029
10300 is returned if the bytes are not well-formed extended UTF-8, or if they
1031represent a code point that cannot fit in a UV on the current platform. Hence
1032this macro can give different results when run on a 64-bit word machine than on
1033one with a 32-bit word size.
1034
7ce5b055
KW
1035Note that it is illegal in Perl to have code points that are larger than what can
1036fit in an IV on the current machine; and illegal in Unicode to have any that
1037this macro matches
6110285c
KW
1038
1039=cut
1040
1041 * ASCII EBCDIC I8
1042 * U+10FFFF: \xF4\x8F\xBF\xBF \xF9\xA1\xBF\xBF\xBF max legal Unicode
1043 * U+110000: \xF4\x90\x80\x80 \xF9\xA2\xA0\xA0\xA0
1044 * U+110001: \xF4\x90\x80\x81 \xF9\xA2\xA0\xA0\xA1
1045 */
7ce5b055
KW
1046#define UTF_START_BYTE_110000_ UTF_START_BYTE(PERL_UNICODE_MAX + 1, 21)
1047#define UTF_FIRST_CONT_BYTE_110000_ \
1048 UTF_FIRST_CONT_BYTE(PERL_UNICODE_MAX + 1, 21)
1049#define UTF8_IS_SUPER(s, e) \
1050 ( ((e) - (s)) >= UNISKIP_BY_MSB_(20) \
1051 && ( NATIVE_UTF8_TO_I8(s[0]) >= UTF_START_BYTE_110000_ \
1052 && ( NATIVE_UTF8_TO_I8(s[0]) > UTF_START_BYTE_110000_ \
1053 || NATIVE_UTF8_TO_I8(s[1]) >= UTF_FIRST_CONT_BYTE_110000_))) \
1054 ? isUTF8_CHAR(s, e) \
1055 : 0
6110285c 1056
39fafb79
KW
1057/*
1058=for apidoc Am|bool|UNICODE_IS_NONCHAR|const UV uv
1059
1060Returns a boolean as to whether or not C<uv> is one of the Unicode
1061non-character code points
1062
1063=cut
1064*/
1065
6110285c
KW
1066/* Is 'uv' one of the 32 contiguous-range noncharacters? */
1067#define UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv) \
1068 UNLIKELY(inRANGE(uv, 0xFDD0, 0xFDEF))
1069
1070/* Is 'uv' one of the 34 plane-ending noncharacters 0xFFFE, 0xFFFF, 0x1FFFE,
1071 * 0x1FFFF, ... 0x10FFFE, 0x10FFFF, given that we know that 'uv' is not above
1072 * the Unicode legal max */
030c8206
KW
1073#define UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv) \
1074 UNLIKELY(((UV) (uv) & 0xFFFE) == 0xFFFE)
6110285c 1075
030c8206
KW
1076#define UNICODE_IS_NONCHAR(uv) \
1077 ( UNLIKELY(UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv)) \
1078 || ( UNLIKELY(UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv)) \
6110285c
KW
1079 && LIKELY(! UNICODE_IS_SUPER(uv))))
1080
6110285c
KW
1081/*
1082=for apidoc Am|bool|UTF8_IS_NONCHAR|const U8 *s|const U8 *e
1083
1084Evaluates to non-zero if the first few bytes of the string starting at C<s> and
1085looking no further than S<C<e - 1>> are well-formed UTF-8 that represents one
1086of the Unicode non-character code points; otherwise it evaluates to 0. If
1087non-zero, the value gives how many bytes starting at C<s> comprise the code
1088point's representation.
1089
1090=cut
1091*/
65e4aa05
KW
1092#define UTF8_IS_NONCHAR(s, e) is_NONCHAR_utf8_safe(s,e)
1093
1094/* This is now machine generated, and the 'given' clause is no longer
1095 * applicable */
1096#define UTF8_IS_NONCHAR_GIVEN_THAT_NON_SUPER_AND_GE_PROBLEMATIC(s, e) \
1097 UTF8_IS_NONCHAR(s, e)
6110285c 1098
6f8b1f93
KW
1099/* Surrogates, non-character code points and above-Unicode code points are
1100 * problematic in some contexts. These macros allow code that needs to check
1101 * for those to quickly exclude the vast majority of code points it will
1102 * encounter.
1103 *
1104 * The lowest such code point is the smallest surrogate, U+D800. We calculate
1105 * the start byte of that. 0xD800 occupies 16 bits. */
1106#define isUNICODE_POSSIBLY_PROBLEMATIC(uv) ((uv) >= UNICODE_SURROGATE_FIRST)
1107#define isUTF8_POSSIBLY_PROBLEMATIC(c) \
1108 (NATIVE_UTF8_TO_I8(c) >= UTF_START_BYTE(UNICODE_SURROGATE_FIRST, 16))
1d72bdf6 1109
99904f65
KW
1110/* Perl extends Unicode so that it is possible to encode (as extended UTF-8 or
1111 * UTF-EBCDIC) any 64-bit value. No standard known to khw ever encoded higher
1112 * than a 31 bit value. On ASCII platforms this just meant arbitrarily saying
1113 * nothing could be higher than this. On these the start byte FD gets you to
1114 * 31 bits, and FE and FF are forbidden as start bytes. On EBCDIC platforms,
1115 * FD gets you only to 26 bits; adding FE to mean 7 total bytes gets you to 30
1116 * bits. To get to 31 bits, they treated an initial FF byte idiosyncratically.
1117 * It was considered to be the start byte FE meaning it had 7 total bytes, and
1118 * the final 1 was treated as an information bit, getting you to 31 bits.
1119 *
1120 * Perl used to accept this idiosyncratic interpretation of FF, but now rejects
1121 * it in order to get to being able to encode 64 bits. The bottom line is that
1122 * it is a Perl extension to use the start bytes FE and FF on ASCII platforms,
1123 * and the start byte FF on EBCDIC ones. That translates into that it is a
1124 * Perl extension to represent anything occupying more than 31 bits on ASCII
1125 * platforms; 30 bits on EBCDIC. */
1126#define UNICODE_IS_PERL_EXTENDED(uv) \
1127 UNLIKELY((UV) (uv) > nBIT_UMAX(31 - ONE_IF_EBCDIC_ZERO_IF_NOT))
43732c4f
KW
1128#define UTF8_IS_PERL_EXTENDED(s) \
1129 (UTF8SKIP(s) > 6 + ONE_IF_EBCDIC_ZERO_IF_NOT)
99904f65 1130
42b360b2 1131/* Largest code point we accept from external sources */
6110285c
KW
1132#define MAX_LEGAL_CP ((UV)IV_MAX)
1133
c76687c5 1134#define UTF8_ALLOW_EMPTY 0x0001 /* Allow a zero length string */
2b5e7bc2 1135#define UTF8_GOT_EMPTY UTF8_ALLOW_EMPTY
c76687c5
KW
1136
1137/* Allow first byte to be a continuation byte */
1d72bdf6 1138#define UTF8_ALLOW_CONTINUATION 0x0002
2b5e7bc2 1139#define UTF8_GOT_CONTINUATION UTF8_ALLOW_CONTINUATION
c76687c5 1140
cd01d3b1 1141/* Unexpected non-continuation byte */
1d72bdf6 1142#define UTF8_ALLOW_NON_CONTINUATION 0x0004
2b5e7bc2 1143#define UTF8_GOT_NON_CONTINUATION UTF8_ALLOW_NON_CONTINUATION
949cf498
KW
1144
1145/* expecting more bytes than were available in the string */
1146#define UTF8_ALLOW_SHORT 0x0008
2b5e7bc2 1147#define UTF8_GOT_SHORT UTF8_ALLOW_SHORT
949cf498 1148
94953955
KW
1149/* Overlong sequence; i.e., the code point can be specified in fewer bytes.
1150 * First one will convert the overlong to the REPLACEMENT CHARACTER; second
1151 * will return what the overlong evaluates to */
949cf498 1152#define UTF8_ALLOW_LONG 0x0010
94953955 1153#define UTF8_ALLOW_LONG_AND_ITS_VALUE (UTF8_ALLOW_LONG|0x0020)
2b5e7bc2
KW
1154#define UTF8_GOT_LONG UTF8_ALLOW_LONG
1155
d60baaa7
KW
1156#define UTF8_ALLOW_OVERFLOW 0x0080
1157#define UTF8_GOT_OVERFLOW UTF8_ALLOW_OVERFLOW
949cf498 1158
f180b292 1159#define UTF8_DISALLOW_SURROGATE 0x0100 /* Unicode surrogates */
2b5e7bc2 1160#define UTF8_GOT_SURROGATE UTF8_DISALLOW_SURROGATE
f180b292 1161#define UTF8_WARN_SURROGATE 0x0200
949cf498 1162
c4e96019
KW
1163/* Unicode non-character code points */
1164#define UTF8_DISALLOW_NONCHAR 0x0400
2b5e7bc2 1165#define UTF8_GOT_NONCHAR UTF8_DISALLOW_NONCHAR
c4e96019 1166#define UTF8_WARN_NONCHAR 0x0800
949cf498 1167
c4e96019
KW
1168/* Super-set of Unicode: code points above the legal max */
1169#define UTF8_DISALLOW_SUPER 0x1000
2b5e7bc2 1170#define UTF8_GOT_SUPER UTF8_DISALLOW_SUPER
c4e96019
KW
1171#define UTF8_WARN_SUPER 0x2000
1172
1173/* The original UTF-8 standard did not define UTF-8 with start bytes of 0xFE or
1174 * 0xFF, though UTF-EBCDIC did. This allowed both versions to represent code
1175 * points up to 2 ** 31 - 1. Perl extends UTF-8 so that 0xFE and 0xFF are
1176 * usable on ASCII platforms, and 0xFF means something different than
1177 * UTF-EBCDIC defines. These changes allow code points of 64 bits (actually
1178 * somewhat more) to be represented on both platforms. But these are Perl
1179 * extensions, and not likely to be interchangeable with other languages. Note
1180 * that on ASCII platforms, FE overflows a signed 32-bit word, and FF an
1181 * unsigned one. */
d044b7a7
KW
1182#define UTF8_DISALLOW_PERL_EXTENDED 0x4000
1183#define UTF8_GOT_PERL_EXTENDED UTF8_DISALLOW_PERL_EXTENDED
1184#define UTF8_WARN_PERL_EXTENDED 0x8000
d35f2ca5 1185
57ff5f59
KW
1186/* For back compat, these old names are misleading for overlongs and
1187 * UTF_EBCDIC. */
d044b7a7
KW
1188#define UTF8_DISALLOW_ABOVE_31_BIT UTF8_DISALLOW_PERL_EXTENDED
1189#define UTF8_GOT_ABOVE_31_BIT UTF8_GOT_PERL_EXTENDED
1190#define UTF8_WARN_ABOVE_31_BIT UTF8_WARN_PERL_EXTENDED
1191#define UTF8_DISALLOW_FE_FF UTF8_DISALLOW_PERL_EXTENDED
1192#define UTF8_WARN_FE_FF UTF8_WARN_PERL_EXTENDED
949cf498 1193
f180b292 1194#define UTF8_CHECK_ONLY 0x10000
99a765e9 1195#define _UTF8_NO_CONFIDENCE_IN_CURLEN 0x20000 /* Internal core use only */
949cf498
KW
1196
1197/* For backwards source compatibility. They do nothing, as the default now
1198 * includes what they used to mean. The first one's meaning was to allow the
1199 * just the single non-character 0xFFFF */
1200#define UTF8_ALLOW_FFFF 0
c825ef8c 1201#define UTF8_ALLOW_FE_FF 0
949cf498
KW
1202#define UTF8_ALLOW_SURROGATE 0
1203
ecc1615f
KW
1204/* C9 refers to Unicode Corrigendum #9: allows but discourages non-chars */
1205#define UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE \
1206 (UTF8_DISALLOW_SUPER|UTF8_DISALLOW_SURROGATE)
1207#define UTF8_WARN_ILLEGAL_C9_INTERCHANGE (UTF8_WARN_SUPER|UTF8_WARN_SURROGATE)
1208
d35f2ca5 1209#define UTF8_DISALLOW_ILLEGAL_INTERCHANGE \
ecc1615f 1210 (UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE|UTF8_DISALLOW_NONCHAR)
949cf498 1211#define UTF8_WARN_ILLEGAL_INTERCHANGE \
ecc1615f
KW
1212 (UTF8_WARN_ILLEGAL_C9_INTERCHANGE|UTF8_WARN_NONCHAR)
1213
0eb3d6a0
KW
1214/* This is typically used for code that processes UTF-8 input and doesn't want
1215 * to have to deal with any malformations that might be present. All such will
1216 * be safely replaced by the REPLACEMENT CHARACTER, unless other flags
1217 * overriding this are also present. */
2d532c27
KW
1218#define UTF8_ALLOW_ANY ( UTF8_ALLOW_CONTINUATION \
1219 |UTF8_ALLOW_NON_CONTINUATION \
1220 |UTF8_ALLOW_SHORT \
d60baaa7
KW
1221 |UTF8_ALLOW_LONG \
1222 |UTF8_ALLOW_OVERFLOW)
2d532c27
KW
1223
1224/* Accept any Perl-extended UTF-8 that evaluates to any UV on the platform, but
cd01d3b1 1225 * not any malformed. This is the default. */
2d532c27
KW
1226#define UTF8_ALLOW_ANYUV 0
1227#define UTF8_ALLOW_DEFAULT UTF8_ALLOW_ANYUV
1d72bdf6 1228
d044b7a7
KW
1229#define UNICODE_WARN_SURROGATE 0x0001 /* UTF-16 surrogates */
1230#define UNICODE_WARN_NONCHAR 0x0002 /* Non-char code points */
1231#define UNICODE_WARN_SUPER 0x0004 /* Above 0x10FFFF */
1232#define UNICODE_WARN_PERL_EXTENDED 0x0008 /* Above 0x7FFF_FFFF */
1233#define UNICODE_WARN_ABOVE_31_BIT UNICODE_WARN_PERL_EXTENDED
1234#define UNICODE_DISALLOW_SURROGATE 0x0010
1235#define UNICODE_DISALLOW_NONCHAR 0x0020
1236#define UNICODE_DISALLOW_SUPER 0x0040
1237#define UNICODE_DISALLOW_PERL_EXTENDED 0x0080
24b4c303
KW
1238
1239#ifdef PERL_CORE
1240# define UNICODE_ALLOW_ABOVE_IV_MAX 0x0100
1241#endif
d044b7a7 1242#define UNICODE_DISALLOW_ABOVE_31_BIT UNICODE_DISALLOW_PERL_EXTENDED
33f38593
KW
1243
1244#define UNICODE_GOT_SURROGATE UNICODE_DISALLOW_SURROGATE
1245#define UNICODE_GOT_NONCHAR UNICODE_DISALLOW_NONCHAR
1246#define UNICODE_GOT_SUPER UNICODE_DISALLOW_SUPER
1247#define UNICODE_GOT_PERL_EXTENDED UNICODE_DISALLOW_PERL_EXTENDED
1248
ecc1615f
KW
1249#define UNICODE_WARN_ILLEGAL_C9_INTERCHANGE \
1250 (UNICODE_WARN_SURROGATE|UNICODE_WARN_SUPER)
bb88be5f 1251#define UNICODE_WARN_ILLEGAL_INTERCHANGE \
ecc1615f
KW
1252 (UNICODE_WARN_ILLEGAL_C9_INTERCHANGE|UNICODE_WARN_NONCHAR)
1253#define UNICODE_DISALLOW_ILLEGAL_C9_INTERCHANGE \
1254 (UNICODE_DISALLOW_SURROGATE|UNICODE_DISALLOW_SUPER)
bb88be5f 1255#define UNICODE_DISALLOW_ILLEGAL_INTERCHANGE \
ecc1615f 1256 (UNICODE_DISALLOW_ILLEGAL_C9_INTERCHANGE|UNICODE_DISALLOW_NONCHAR)
949cf498
KW
1257
1258/* For backward source compatibility, as are now the default */
1259#define UNICODE_ALLOW_SURROGATE 0
1260#define UNICODE_ALLOW_SUPER 0
1261#define UNICODE_ALLOW_ANY 0
b851fbc1 1262
6110285c 1263#define UNICODE_BYTE_ORDER_MARK 0xFEFF
030c8206
KW
1264#define UNICODE_IS_BYTE_ORDER_MARK(uv) UNLIKELY((UV) (uv) \
1265 == UNICODE_BYTE_ORDER_MARK)
c149ab20 1266
ec34087a
KW
1267#define LATIN_SMALL_LETTER_SHARP_S LATIN_SMALL_LETTER_SHARP_S_NATIVE
1268#define LATIN_SMALL_LETTER_Y_WITH_DIAERESIS \
1269 LATIN_SMALL_LETTER_Y_WITH_DIAERESIS_NATIVE
1270#define MICRO_SIGN MICRO_SIGN_NATIVE
1271#define LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVE \
1272 LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVE_NATIVE
1273#define LATIN_SMALL_LETTER_A_WITH_RING_ABOVE \
1274 LATIN_SMALL_LETTER_A_WITH_RING_ABOVE_NATIVE
09091399
JH
1275#define UNICODE_GREEK_CAPITAL_LETTER_SIGMA 0x03A3
1276#define UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA 0x03C2
1277#define UNICODE_GREEK_SMALL_LETTER_SIGMA 0x03C3
9dcbe121 1278#define GREEK_SMALL_LETTER_MU 0x03BC
9e682c18
KW
1279#define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case
1280 of MICRON */
1281#define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
0766489e
KW
1282#ifdef LATIN_CAPITAL_LETTER_SHARP_S_UTF8
1283# define LATIN_CAPITAL_LETTER_SHARP_S 0x1E9E
1284#endif
74894415
KW
1285#define LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE 0x130
1286#define LATIN_SMALL_LETTER_DOTLESS_I 0x131
9e682c18 1287#define LATIN_SMALL_LETTER_LONG_S 0x017F
a9f50d33
KW
1288#define LATIN_SMALL_LIGATURE_LONG_S_T 0xFB05
1289#define LATIN_SMALL_LIGATURE_ST 0xFB06
9e682c18
KW
1290#define KELVIN_SIGN 0x212A
1291#define ANGSTROM_SIGN 0x212B
09091399 1292
9e55ce06 1293#define UNI_DISPLAY_ISPRINT 0x0001
c728cb41 1294#define UNI_DISPLAY_BACKSLASH 0x0002
daf6caf1
KW
1295#define UNI_DISPLAY_BACKSPACE 0x0004 /* Allow \b when also
1296 UNI_DISPLAY_BACKSLASH */
1297#define UNI_DISPLAY_QQ (UNI_DISPLAY_ISPRINT \
1298 |UNI_DISPLAY_BACKSLASH \
1299 |UNI_DISPLAY_BACKSPACE)
1300
1301/* Character classes could also allow \b, but not patterns in general */
c728cb41 1302#define UNI_DISPLAY_REGEX (UNI_DISPLAY_ISPRINT|UNI_DISPLAY_BACKSLASH)
9e55ce06 1303
e0ffa6d6 1304/* Should be removed; maybe deprecated, but not used in CPAN */
ebc501f0 1305#define SHARP_S_SKIP 2
3b0fc154 1306
3cedd9d9 1307#define is_utf8_char_buf(buf, buf_end) isUTF8_CHAR(buf, buf_end)
976c1b08
KW
1308#define bytes_from_utf8(s, lenp, is_utf8p) \
1309 bytes_from_utf8_loc(s, lenp, is_utf8p, 0)
3cedd9d9 1310
6302f837
KW
1311/* Do not use; should be deprecated. Use isUTF8_CHAR() instead; this is
1312 * retained solely for backwards compatibility */
1313#define IS_UTF8_CHAR(p, n) (isUTF8_CHAR(p, (p) + (n)) == n)
e9a8c099 1314
6a5bc5ac 1315#endif /* PERL_UTF8_H_ */
57f0e7e2 1316
e9a8c099 1317/*
14d04a33 1318 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 1319 */