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