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.
9 * Copyright (C) 2000, 2001, 2002, 2005, 2006, 2007, 2009,
10 * 2010, 2011 by Larry Wall and others
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.
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.
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. khw thinks it would be
27 * clearer if these were renamed to be I8, because UTF is the prefix for UTF16,
28 * U32, etc., and we use it only for 8 bit quantities; though on ASCII machines
29 * these are final, not intermediate, values. U8 would be more accurate, but
30 * bears too much resemblence to the ubiquitous U8 declaration. 'I8' stands
31 * out as very different from 'UTFn'.
33 * Unfortunately there are inconsistencies.
37 #ifndef PERL_UTF8_H_ /* Guard against recursive inclusion */
38 #define PERL_UTF8_H_ 1
41 =for apidoc Ay||utf8ness_t
43 This typedef is used by several core functions that return PV strings, to
44 indicate the UTF-8ness of those strings.
46 (If you write a new function, you probably should instead return the PV in an
47 SV with the UTF-8 flag of the SV properly set, rather than use this mechanism.)
49 The possible values this can be are:
55 This means the string definitely should be treated as a sequence of
56 UTF-8-encoded characters.
58 Most code that needs to handle this typedef should be of the form:
60 if (utf8ness_flag == UTF8NESS_YES) {
61 treat as utf8; // like turning on an SV UTF-8 flag
66 This means the string definitely should be treated as a sequence of bytes, not
69 =item C<UTF8NESS_IMMATERIAL>
71 This means it is equally valid to treat the string as bytes, or as UTF-8
72 characters; use whichever way you want. This happens when the string consists
73 entirely of characters which have the same representation whether encoded in
76 =item C<UTF8NESS_UNKNOWN>
78 This means it is unknown how the string should be treated. No core function
79 will ever return this value to a non-core caller. Instead, it is used by the
80 caller to initialize a variable to a non-legal value. A typical call will look like:
82 utf8ness_t string_is_utf8 = UTF8NESS_UNKNOWN
83 const char * string = foo(arg1, arg2, ..., &string_is_utf8);
84 if (string_is_utf8 == UTF8NESS_YES) {
85 do something for UTF-8;
90 The following relationships hold between the enum values:
94 =item S<C<0 E<lt>= I<enum value> E<lt>= UTF8NESS_IMMATERIAL>>
96 the string may be treated in code as non-UTF8
98 =item S<C<UTF8NESS_IMMATERIAL E<lt>= <I<enum value>>>
100 the string may be treated in code as encoded in UTF-8
108 UTF8NESS_NO = 0, /* Definitely not UTF-8 */
109 UTF8NESS_IMMATERIAL = 1, /* Representation is the same in UTF-8 as
110 not, so the UTF8ness doesn't actually
112 UTF8NESS_YES = 2, /* Defintely is UTF-8, wideness
114 UTF8NESS_UNKNOWN = -1, /* Undetermined so far */
117 /* Use UTF-8 as the default script encoding?
118 * Turning this on will break scripts having non-UTF-8 binary
119 * data (such as Latin-1) in string literals. */
120 #ifdef USE_UTF8_SCRIPTS
121 # define USE_UTF8_IN_NAMES (!IN_BYTES)
123 # define USE_UTF8_IN_NAMES (PL_hints & HINT_UTF8)
126 #include "regcharclass.h"
127 #include "unicode_constants.h"
129 /* For to_utf8_fold_flags, q.v. */
130 #define FOLD_FLAGS_LOCALE 0x1
131 #define FOLD_FLAGS_FULL 0x2
132 #define FOLD_FLAGS_NOMIX_ASCII 0x4
135 =for apidoc_defn APRTdm|bool|is_ascii_string|NN const U8 * const s|STRLEN len
136 =for apidoc_defn APRTdm|bool|is_invariant_string|NN const U8 * const s|STRLEN len
139 #define is_ascii_string(s, len) is_utf8_invariant_string(s, len)
140 #define is_invariant_string(s, len) is_utf8_invariant_string(s, len)
142 #define uvoffuni_to_utf8_flags(d,uv,flags) \
143 uvoffuni_to_utf8_flags_msgs(d, uv, flags, 0)
145 #define uvchr_to_utf8 uv_to_utf8
146 #define uvchr_to_utf8_flags uv_to_utf8_flags
147 #define uvchr_to_utf8_flags_msgs uv_to_utf8_msgs
148 #define Perl_uvchr_to_utf8 Perl_uv_to_utf8
149 #define Perl_uvchr_to_utf8_flags Perl_uv_to_utf8_flags
150 #define Perl_uvchr_to_utf8_flags_msgs Perl_uv_to_utf8_msgs
152 /* This is needed to cast the parameters for all those calls that had them
153 * improperly as chars */
154 #define utf8_to_uvchr_buf(s, e, lenp) \
155 Perl_utf8_to_uvchr_buf(aTHX_ (const U8 *) (s), (const U8 *) e, lenp)
157 #define Perl_utf8n_to_uvchr(s, len, lenp, flags) \
158 Perl_utf8n_to_uvchr_error(s, len, lenp, flags, 0)
159 #define Perl_utf8n_to_uvchr_error(s, len, lenp, flags, errors) \
160 Perl_utf8n_to_uvchr_msgs(s, len, lenp, flags, errors, 0)
162 #define Perl_utf8_to_uv( s, e, cp_p, advance_p) \
163 Perl_utf8_to_uv_flags( s, e, cp_p, advance_p, 0)
164 #define Perl_utf8_to_uv_flags( s, e, cp_p, advance_p, flags) \
165 Perl_utf8_to_uv_errors( s, e, cp_p, advance_p, flags, 0)
166 #define Perl_utf8_to_uv_errors( s, e, cp_p, advance_p, flags, errors) \
167 Perl_utf8_to_uv_msgs( s, e, cp_p, advance_p, flags, errors, 0)
168 #define Perl_extended_utf8_to_uv(s, e, cp_p, advance_p) \
169 Perl_utf8_to_uv(s, e, cp_p, advance_p)
170 #define Perl_strict_utf8_to_uv( s, e, cp_p, advance_p) \
171 Perl_utf8_to_uv_flags( s, e, cp_p, advance_p, \
172 UTF8_DISALLOW_ILLEGAL_INTERCHANGE)
173 #define Perl_c9strict_utf8_to_uv(s, e, cp_p, advance_p) \
174 Perl_utf8_to_uv_flags( s, e, cp_p, advance_p, \
175 UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE)
177 #define utf16_to_utf8(p, d, bytelen, newlen) \
178 utf16_to_utf8_base(p, d, bytelen, newlen, 0, 1)
179 #define utf16_to_utf8_reversed(p, d, bytelen, newlen) \
180 utf16_to_utf8_base(p, d, bytelen, newlen, 1, 0)
181 #define utf8_to_utf16(p, d, bytelen, newlen) \
182 utf8_to_utf16_base(p, d, bytelen, newlen, 0, 1)
183 #define utf8_to_utf16_reversed(p, d, bytelen, newlen) \
184 utf8_to_utf16_base(p, d, bytelen, newlen, 1, 0)
186 #define to_uni_fold(c, p, lenp) _to_uni_fold_flags(c, p, lenp, FOLD_FLAGS_FULL)
188 #define foldEQ_utf8(s1, pe1, l1, u1, s2, pe2, l2, u2) \
189 foldEQ_utf8_flags(s1, pe1, l1, u1, s2, pe2, l2, u2, 0)
190 #define FOLDEQ_UTF8_NOMIX_ASCII (1 << 0)
191 #define FOLDEQ_LOCALE (1 << 1)
192 #define FOLDEQ_S1_ALREADY_FOLDED (1 << 2)
193 #define FOLDEQ_S2_ALREADY_FOLDED (1 << 3)
194 #define FOLDEQ_S1_FOLDS_SANE (1 << 4)
195 #define FOLDEQ_S2_FOLDS_SANE (1 << 5)
197 /* This will be described more fully below, but it turns out that the
198 * fundamental difference between UTF-8 and UTF-EBCDIC is that the former has
199 * the upper 2 bits of a continuation byte be '10', and the latter has the
200 * upper 3 bits be '101', leaving 6 and 5 significant bits respectively.
202 * It is helpful to know the EBCDIC value on ASCII platforms, mainly to avoid
204 #define UTF_EBCDIC_CONTINUATION_BYTE_INFO_BITS 5
206 /* See explanation below at 'UTF8_MAXBYTES' */
207 #define ASCII_PLATFORM_UTF8_MAXBYTES 13
211 /* The equivalent of the next few macros but implementing UTF-EBCDIC are in the
212 * following header file: */
213 # include "utfebcdic.h"
215 # else /* ! EBCDIC */
220 EXTCONST unsigned char PL_utf8skip[];
222 EXTCONST unsigned char PL_utf8skip[] = {
223 /* 0x00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
224 /* 0x10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
225 /* 0x20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
226 /* 0x30 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
227 /* 0x40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
228 /* 0x50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
229 /* 0x60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
230 /* 0x70 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ascii */
231 /* 0x80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
232 /* 0x90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
233 /* 0xA0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
234 /* 0xB0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bogus: continuation byte */
235 /* 0xC0 */ 2,2, /* overlong */
236 /* 0xC2 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* U+0080 to U+03FF */
237 /* 0xD0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* U+0400 to U+07FF */
238 /* 0xE0 */ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, /* U+0800 to U+FFFF */
239 /* 0xF0 */ 4,4,4,4,4,4,4,4,5,5,5,5,6,6, /* above BMP to 2**31 - 1 */
240 /* Perl extended (never was official UTF-8). Up to 36 bit */
242 /* More extended, Up to 72 bits (64-bit + reserved) */
243 /* 0xFF */ ASCII_PLATFORM_UTF8_MAXBYTES
251 =for apidoc Am|U8|NATIVE_TO_LATIN1|U8 ch
253 Returns the Latin-1 (including ASCII and control characters) equivalent of the
254 input native code point given by C<ch>. Thus, C<NATIVE_TO_LATIN1(193)> on
255 EBCDIC platforms returns 65. These each represent the character C<"A"> on
256 their respective platforms. On ASCII platforms no conversion is needed, so
257 this macro expands to just its input, adding no time nor space requirements to
260 For conversion of code points potentially larger than will fit in a character,
261 use L</NATIVE_TO_UNI>.
263 =for apidoc Am|U8|LATIN1_TO_NATIVE|U8 ch
265 Returns the native equivalent of the input Latin-1 code point (including ASCII
266 and control characters) given by C<ch>. Thus, C<LATIN1_TO_NATIVE(66)> on
267 EBCDIC platforms returns 194. These each represent the character C<"B"> on
268 their respective platforms. On ASCII platforms no conversion is needed, so
269 this macro expands to just its input, adding no time nor space requirements to
272 For conversion of code points potentially larger than will fit in a character,
273 use L</UNI_TO_NATIVE>.
275 =for apidoc Am|UV|NATIVE_TO_UNI|UV ch
277 Returns the Unicode equivalent of the input native code point given by C<ch>.
278 Thus, C<NATIVE_TO_UNI(195)> on EBCDIC platforms returns 67. These each
279 represent the character C<"C"> on their respective platforms. On ASCII
280 platforms no conversion is needed, so this macro expands to just its input,
281 adding no time nor space requirements to the implementation.
283 =for apidoc Am|UV|UNI_TO_NATIVE|UV ch
285 Returns the native equivalent of the input Unicode code point given by C<ch>.
286 Thus, C<UNI_TO_NATIVE(68)> on EBCDIC platforms returns 196. These each
287 represent the character C<"D"> on their respective platforms. On ASCII
288 platforms no conversion is needed, so this macro expands to just its input,
289 adding no time nor space requirements to the implementation.
294 #define NATIVE_TO_LATIN1(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
295 #define LATIN1_TO_NATIVE(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
297 /* I8 is an intermediate version of UTF-8 used only in UTF-EBCDIC. We thus
298 * consider it to be identical to UTF-8 on ASCII platforms. Strictly speaking
299 * UTF-8 and UTF-EBCDIC are two different things, but we often conflate them
300 * because they are 8-bit encodings that serve the same purpose in Perl, and
301 * rarely do we need to distinguish them. The term "NATIVE_UTF8" applies to
302 * whichever one is applicable on the current platform */
303 #define NATIVE_UTF8_TO_I8(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
304 #define I8_TO_NATIVE_UTF8(ch) (__ASSERT_(FITS_IN_8_BITS(ch)) ((U8) (ch)))
306 #define UNI_TO_NATIVE(ch) ((UV) ASSERT_NOT_PTR(ch))
307 #define NATIVE_TO_UNI(ch) ((UV) ASSERT_NOT_PTR(ch))
311 The following table is from Unicode 3.2, plus the Perl extensions for above
314 Code Points 1st Byte 2nd Byte 3rd 4th 5th 6th 7th 8th-13th
316 U+0000..U+007F 00..7F
317 U+0080..U+07FF * C2..DF 80..BF
318 U+0800..U+0FFF E0 * A0..BF 80..BF
319 U+1000..U+CFFF E1..EC 80..BF 80..BF
320 U+D000..U+D7FF ED 80..9F 80..BF
321 U+D800..U+DFFF ED A0..BF 80..BF (surrogates)
322 U+E000..U+FFFF EE..EF 80..BF 80..BF
323 U+10000..U+3FFFF F0 * 90..BF 80..BF 80..BF
324 U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
325 U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
326 Below are above-Unicode code points
327 U+110000..U+13FFFF F4 90..BF 80..BF 80..BF
328 U+110000..U+1FFFFF F5..F7 80..BF 80..BF 80..BF
329 U+200000..U+FFFFFF F8 * 88..BF 80..BF 80..BF 80..BF
330 U+1000000..U+3FFFFFF F9..FB 80..BF 80..BF 80..BF 80..BF
331 U+4000000..U+3FFFFFFF FC * 84..BF 80..BF 80..BF 80..BF 80..BF
332 U+40000000..U+7FFFFFFF FD 80..BF 80..BF 80..BF 80..BF 80..BF
333 U+80000000..U+FFFFFFFFF FE * 82..BF 80..BF 80..BF 80..BF 80..BF 80..BF
334 U+1000000000.. FF 80..BF 80..BF 80..BF 80..BF 80..BF * 81..BF 80..BF
336 Note the gaps before several of the byte entries above marked by '*'. These are
337 caused by legal UTF-8 avoiding non-shortest encodings: it is technically
338 possible to UTF-8-encode a single code point in different ways, but that is
339 explicitly forbidden, and the shortest possible encoding should always be used
340 (and that is what Perl does). The non-shortest ones are called 'overlongs'.
342 Another way to look at it, as bits:
344 Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte
347 0000 0bbb bbaa aaaa 110b bbbb 10aa aaaa
348 cccc bbbb bbaa aaaa 1110 cccc 10bb bbbb 10aa aaaa
349 00 000d ddcc cccc bbbb bbaa aaaa 1111 0ddd 10cc cccc 10bb bbbb 10aa aaaa
351 As you can see, the continuation bytes all begin with C<10>, and the
352 leading bits of the start byte tell how many bytes there are in the
355 Perl's extended UTF-8 means we can have start bytes up through FF, though any
356 beginning with FF yields a code point that is too large for 32-bit ASCII
357 platforms. FF signals to use 13 bytes for the encoded character. This breaks
358 the paradigm that the number of leading bits gives how many total bytes there
359 are in the character. */
361 /* A continuation byte in a UTF-8 encoded sequence contributes this number of
362 * low-order bits to the specification of the code point. In the bit
363 * maps above, you see that the first 2 bits are a constant '10', leaving 6 of
364 * real information. (If you're really curious, the only two numbers that work
365 * out for this on an 8-bit byte are 5 and 6. Since the first two bits are
366 * already taken, a maximum of 6 are available for anything else. If 6 is
367 * used, there are 64 possible continuations 80-BF. With 5, there are 32,
368 * A0-BF. And with 4 there would be 0 continuations possible; an
369 * impossibility. So 5 is the minimum. UTF-EBCDIC I8 (Intermediate 8) is just
370 * setting this to 5. We could have a UTF-8 encoding that is based on ASCII,
371 * but uses just 5 bits of payload per continuation byte. The reason someone
372 * might want to do this is to extend the set of characters that occupy a
373 * single byte when encoded in this hypothetical UTF-8 to additionally include
374 * the C1 controls.) */
375 # define UTF_CONTINUATION_BYTE_INFO_BITS 6
377 /* ^? is defined to be DEL on ASCII systems. See the definition of toCTRL()
379 # define QUESTION_MARK_CTRL DEL_NATIVE
381 #endif /* EBCDIC vs ASCII */
383 /* It turns out that in a number of cases, that handling ASCII vs EBCDIC is a
384 * matter of being off-by-one. So this is a convenience macro, used to avoid
386 #define ONE_IF_EBCDIC_ZERO_IF_NOT \
387 (UTF_CONTINUATION_BYTE_INFO_BITS == UTF_EBCDIC_CONTINUATION_BYTE_INFO_BITS)
389 /* Since the significant bits in a continuation byte are stored in the
390 * least-significant positions, we often find ourselves shifting by that
391 * amount. This is a clearer name in such situations */
392 #define UTF_ACCUMULATION_SHIFT UTF_CONTINUATION_BYTE_INFO_BITS
394 /* 2**info_bits - 1. This masks out all but the bits that carry real
395 * information in a continuation byte. This turns out to be 0x3F in UTF-8,
396 * 0x1F in UTF-EBCDIC. */
397 #define UTF_CONTINUATION_MASK \
398 ((U8) nBIT_MASK(UTF_CONTINUATION_BYTE_INFO_BITS))
400 /* For use in UTF8_IS_CONTINUATION(). This turns out to be 0xC0 in UTF-8,
401 * E0 in UTF-EBCDIC */
402 #define UTF_IS_CONTINUATION_MASK \
403 ((U8) ((0xFF << UTF_ACCUMULATION_SHIFT) & 0xFF))
405 /* This defines the bits that mark a byte in a multi-byte UTF-8 encoded
406 * character as being a continuation byte. A MASK clears the bits you don't
407 * want, using a binary '&'; and a MARK sets the ones you do want, using a
408 * binary '|'. As stated earlier, the fundamental difference between UTF-8 and
409 * UTF-EBCDIC is that the former has the upper 2 bits of a continuation byte be
410 * '10', and the latter has the upper 3 bits be '101', leaving 6 and 5 bits
411 * respectively in which to store information. This is equivalent to "All bits
412 * are 1 except those that store information (which vary) plus the bit that is
413 * required to be 0". This yields 1000 0000 (0x80) for ASCII, and 1010 0000
414 * (0xA0) for UTF-EBCDIC. */
415 #define UTF_CONTINUATION_MARK (~(0x40 | UTF_CONTINUATION_MASK) & 0xff)
417 /* These values are clearer in some contexts; still apply to UTF, not UTF-8 */
418 #define UTF_MIN_CONTINUATION_BYTE UTF_CONTINUATION_MARK
419 #define MIN_OFFUNI_VARIANT_CP UTF_MIN_CONTINUATION_BYTE
421 /* This is the name to use if you are working in UTF-8, as opposed to plain
422 * 'UTF', which is I8 on EBCDIC platforms */
423 #define UTF8_MIN_CONTINUATION_BYTE I8_TO_NATIVE_UTF8(UTF_MIN_CONTINUATION_BYTE)
425 /* Is the byte 'c' part of a multi-byte UTF8-8 encoded sequence, and not the
426 * first byte thereof? */
427 #define UTF8_IS_CONTINUATION(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
428 (((NATIVE_UTF8_TO_I8(c) & UTF_IS_CONTINUATION_MASK) \
429 == UTF_CONTINUATION_MARK)))
431 /* Is the representation of the Unicode code point 'cp' the same regardless of
432 * being encoded in UTF-8 or not? This is a fundamental property of
434 #define OFFUNI_IS_INVARIANT(c) (((WIDEST_UTYPE)(c)) < MIN_OFFUNI_VARIANT_CP)
437 =for apidoc Am|bool|UVCHR_IS_INVARIANT|UV cp
439 Evaluates to 1 if the representation of code point C<cp> is the same whether or
440 not it is encoded in UTF-8; otherwise evaluates to 0. UTF-8 invariant
441 characters can be copied as-is when converting to/from UTF-8, saving time.
442 C<cp> is Unicode if above 255; otherwise is platform-native.
446 #define UVCHR_IS_INVARIANT(cp) (OFFUNI_IS_INVARIANT(NATIVE_TO_UNI(cp)))
448 /* This defines the 1-bits that are to be in the first byte of a multi-byte
449 * UTF-8 encoded character that mark it as a start byte and give the number of
450 * bytes that comprise the character. 'len' is that number.
452 * To illustrate: len = 2 => ((U8) ~ 0b0011_1111) or 1100_0000
453 * 7 => ((U8) ~ 0b0000_0001) or 1111_1110
456 * This is not to be used on a single-byte character. As in many places in
457 * perl, U8 must be 8 bits
459 #define UTF_START_MARK(len) ((U8) ~(0xFF >> (len)))
461 /* Masks out the initial one bits in a start byte, leaving the following 0 bit
462 * and the real data bits. 'len' is the number of bytes in the multi-byte
463 * sequence that comprises the character.
465 * To illustrate: len = 2 => 0b0011_1111 works on start byte 110xxxxx
466 * 6 => 0b0000_0011 works on start byte 1111110x
467 * >= 7 => There are no data bits in the start byte
468 * Note that on ASCII platforms, this can be passed a len=1 byte; and all the
469 * real data bits will be returned:
470 len = 1 => 0b0111_1111
471 * This isn't true on EBCDIC platforms, where some len=1 bytes are of the form
472 * 0b101x_xxxx, so this can't be used there on single-byte characters. */
473 #define UTF_START_MASK(len) (0xFF >> (len))
477 =for apidoc AmnU|STRLEN|UTF8_MAXBYTES
479 The maximum width of a single UTF-8 encoded character, in bytes.
481 NOTE: Strictly speaking Perl's UTF-8 should not be called UTF-8 since UTF-8
482 is an encoding of Unicode, and Unicode's upper limit, 0x10FFFF, can be
483 expressed with 4 bytes. However, Perl thinks of UTF-8 as a way to encode
484 non-negative integers in a binary format, even those above Unicode.
488 The start byte 0xFE, never used in any ASCII platform UTF-8 specification, has
489 an obvious meaning, namely it has its upper 7 bits set, so it should start a
490 sequence of 7 bytes. And in fact, this is exactly what standard UTF-EBCDIC
493 The start byte FF, on the other hand could have several different plausible
495 1) The meaning in standard UTF-EBCDIC, namely as an FE start byte, with the
496 bottom bit that should be a fixed '0' to form FE, instead acting as an
498 2) That the sequence should have exactly 8 bytes.
499 3) That the next byte is to be treated as a sort of extended start byte,
500 which in combination with this one gives the total length of the sequence.
501 There are published UTF-8 extensions that do this, some string together
502 multiple initial FF start bytes to achieve arbitrary precision.
503 4) That the sequence has exactly n bytes, where n is what the implementation
507 The goal is to be able to represent 64-bit values in UTF-8 or UTF-EBCDIC. That
508 rules out items 1) and 2). Item 3) has the deal-breaking disadvantage of
509 requiring one to read more than one byte to determine the total length of the
510 sequence. So in Perl, a start byte of FF indicates a UTF-8 string consisting
511 of the start byte, plus enough continuation bytes to encode a 64 bit value.
512 This turns out to be 13 total bytes in UTF-8 and 14 in UTF-EBCDIC. This is
513 because we get zero info bits from the start byte, plus
514 12 * 6 bits of info per continuation byte (could encode 72-bit numbers) on
515 UTF-8 (khw knows not why 11, which would encode 66 bits wasn't
517 13 * 5 bits of info per byte (could encode 65-bit numbers) on UTF-EBCDIC
519 The disadvantages of this method are:
520 1) There's potentially a lot of wasted bytes for all but the largest values.
521 For example, something that could be represented by 7 continuation bytes,
522 instead requires the full 12 or 13.
523 2) There would be problems should larger values, 128-bit say, ever need to be
526 WARNING: This number must be in sync with the value in
527 regen/charset_translations.pl. */
528 #define UTF8_MAXBYTES \
529 (ASCII_PLATFORM_UTF8_MAXBYTES + ONE_IF_EBCDIC_ZERO_IF_NOT)
531 /* Calculate how many bytes are necessary to represent a value whose most
532 * significant 1 bit is in bit position 'pos' of the word. For 0x1, 'pos would
533 * be 0; and for 0x400, 'pos' would be 10, and the result would be:
534 * EBCDIC floor((-1 + (10 + 5 - 1 - 1)) / (5 - 1))
535 * = floor((-1 + (13)) / 4)
538 * ASCII floor(( 0 + (10 + 6 - 1 - 1)) / (6 - 1))
541 * The reason this works is because the number of bits needed to represent a
542 * value is proportional to (UTF_CONTINUATION_BYTE_INFO_BITS - 1). The -1 is
543 * because each new continuation byte removes one bit of information from the
546 * This is a step function (we need to allocate a full extra byte if we
547 * overflow by just a single bit)
549 * The caller is responsible for making sure 'pos' is at least 8 (occupies 9
550 * bits), as it breaks down at the lower edge. At the high end, if it returns
551 * 8 or more, Perl instead anomalously uses MAX_BYTES, so this would be wrong.
553 #define UNISKIP_BY_MSB_(pos) \
554 ( ( -ONE_IF_EBCDIC_ZERO_IF_NOT /* platform break pos's are off-by-one */ \
555 + (pos) + ((UTF_CONTINUATION_BYTE_INFO_BITS - 1) - 1)) /* Step fcn */ \
556 / (UTF_CONTINUATION_BYTE_INFO_BITS - 1)) /* take floor of */
558 /* Compute the number of UTF-8 bytes required for representing the input uv,
559 * which must be a Unicode, not native value.
561 * This uses msbit_pos() which doesn't work on NUL, and UNISKIP_BY_MSB_ breaks
562 * down for small code points. So first check if the input is invariant to get
563 * around that, and use a helper for high code points to accommodate the fact
564 * that above 7 btyes, the value is anomalous. The helper is empty on
565 * platforms that don't go that high */
566 #define OFFUNISKIP(uv) \
567 ((OFFUNI_IS_INVARIANT(uv)) \
569 : (OFFUNISKIP_helper_(uv) UNISKIP_BY_MSB_(msbit_pos(uv))))
571 /* We need to go to MAX_BYTES when we can't represent 'uv' by the number of
572 * information bits in 6 continuation bytes (when we get to 6, the start byte
573 * has no information bits to add to the total). But on 32-bit ASCII
574 * platforms, that doesn't happen until 6*6 bits, so on those platforms, this
575 * will always be false */
576 #if UVSIZE * CHARBITS > (6 * UTF_CONTINUATION_BYTE_INFO_BITS)
577 # define HAS_EXTRA_LONG_UTF8
578 # define OFFUNISKIP_helper_(uv) \
579 UNLIKELY(uv > nBIT_UMAX(6 * UTF_CONTINUATION_BYTE_INFO_BITS)) \
582 # define OFFUNISKIP_helper_(uv)
587 =for apidoc Am|STRLEN|UVCHR_SKIP|UV cp
588 returns the number of bytes required to represent the code point C<cp> when
589 encoded as UTF-8. C<cp> is a native (ASCII or EBCDIC) code point if less than
590 255; a Unicode code point otherwise.
594 #define UVCHR_SKIP(uv) OFFUNISKIP(NATIVE_TO_UNI(uv))
596 #define NATIVE_SKIP(uv) UVCHR_SKIP(uv) /* Old terminology */
598 /* Most code which says UNISKIP is really thinking in terms of native code
599 * points (0-255) plus all those beyond. This is an imprecise term, but having
600 * it means existing code continues to work. For precision, use UVCHR_SKIP,
601 * NATIVE_SKIP, or OFFUNISKIP */
602 #define UNISKIP(uv) UVCHR_SKIP(uv)
604 /* Compute the start byte for a given code point. This requires the log2 of
605 * the code point, which is hard to compute at compile time, which this macro
606 * wants to be. (Perhaps deBruijn sequences could be used.) So a parameter
607 * for the number of bits the value occupies is passed in, which the programmer
608 * has had to figure out to get compile-time effect. And asserts are used to
609 * make sure the value is correct.
611 * Since we are interested only in the start byte, we ignore the lower bits
612 * accounted for by the continuation bytes. Each continuation byte eats up
613 * UTF_CONTINUATION_BYTE_INFO_BITS bits, so the number of continuation bytes
614 * needed is floor(bits / UTF_CONTINUATION_BYTE_INFO_BITS). That number is fed
615 * to UTF_START_MARK() to get the upper part of the start byte. The left over
616 * bits form the lower part which is OR'd with the mark
618 * Note that on EBCDIC platforms, this is actually the I8 */
619 #define UTF_START_BYTE(uv, bits) \
620 (__ASSERT_((uv) >> ((bits) - 1)) /* At least 'bits' */ \
621 __ASSERT_(((uv) & ~nBIT_MASK(bits)) == 0) /* No extra bits */ \
622 UTF_START_MARK(UNISKIP_BY_MSB_((bits) - 1)) \
623 | ((uv) >> (((bits) / UTF_CONTINUATION_BYTE_INFO_BITS) \
624 * UTF_CONTINUATION_BYTE_INFO_BITS)))
626 /* Compute the first continuation byte for a given code point. This is mostly
627 * for compile-time, so how many bits it occupies is also passed in).
629 * We are interested in the first continuation byte, so we ignore the lower
630 * bits accounted for by the rest of the continuation bytes by right shifting
631 * out their info bit, and mask out the higher bits that will go into the start
634 * Note that on EBCDIC platforms, this is actually the I8 */
635 #define UTF_FIRST_CONT_BYTE(uv, bits) \
636 (__ASSERT_((uv) >> ((bits) - 1)) /* At least 'bits' */ \
637 __ASSERT_(((uv) & ~nBIT_MASK(bits)) == 0) /* No extra bits */ \
638 UTF_CONTINUATION_MARK \
639 | ( UTF_CONTINUATION_MASK \
640 & ((uv) >> ((((bits) / UTF_CONTINUATION_BYTE_INFO_BITS) - 1) \
641 * UTF_CONTINUATION_BYTE_INFO_BITS))))
643 #define UTF_MIN_START_BYTE UTF_START_BYTE(UTF_MIN_CONTINUATION_BYTE, 8)
645 /* Is the byte 'c' the first byte of a multi-byte UTF8-8 encoded sequence?
646 * This excludes invariants (they are single-byte). It also excludes the
647 * illegal overlong sequences that begin with C0 and C1 on ASCII platforms, and
648 * C0-C4 I8 start bytes on EBCDIC ones. On EBCDIC E0 can't start a
649 * non-overlong sequence, so we define a base macro and for those platforms,
650 * extend it to also exclude E0 */
651 #define UTF8_IS_START_base(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
652 (NATIVE_UTF8_TO_I8(c) >= UTF_MIN_START_BYTE))
654 # define UTF8_IS_START(c) \
655 (UTF8_IS_START_base(c) && (c) != I8_TO_NATIVE_UTF8(0xE0))
657 # define UTF8_IS_START(c) UTF8_IS_START_base(c)
660 #define UTF_MIN_ABOVE_LATIN1_BYTE UTF_START_BYTE(0x100, 9)
662 /* Is the UTF8-encoded byte 'c' the first byte of a sequence of bytes that
663 * represent a code point > 255? */
664 #define UTF8_IS_ABOVE_LATIN1(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
665 (NATIVE_UTF8_TO_I8(c) >= UTF_MIN_ABOVE_LATIN1_BYTE))
667 /* Is the UTF8-encoded byte 'c' the first byte of a two byte sequence? Use
668 * UTF8_IS_NEXT_CHAR_DOWNGRADEABLE() instead if the input isn't known to
670 #define UTF8_IS_DOWNGRADEABLE_START(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
671 inRANGE_helper_(U8, NATIVE_UTF8_TO_I8(c), \
672 UTF_MIN_START_BYTE, UTF_MIN_ABOVE_LATIN1_BYTE - 1))
674 /* The largest code point representable by two UTF-8 bytes on this platform.
675 * The binary for that code point is:
676 * 1101_1111 10xx_xxxx in UTF-8, and
677 * 1101_1111 101y_yyyy in UTF-EBCDIC I8.
678 * where both x and y are 1, and shown this way to indicate there is one more x
679 * than there is y. The number of x and y bits are their platform's respective
680 * UTF_CONTINUATION_BYTE_INFO_BITS. Squeezing out the bits that don't
681 * contribute to the value, these evaluate to:
682 * 1_1111 xx_xxxx in UTF-8, and
683 * 1_1111 y_yyyy in UTF-EBCDIC I8.
684 * or, the maximum value of an unsigned with (5 + info_bit_count) bits */
685 #define MAX_UTF8_TWO_BYTE nBIT_UMAX(5 + UTF_CONTINUATION_BYTE_INFO_BITS)
687 /* The largest code point representable by two UTF-8 bytes on any platform that
689 #define MAX_PORTABLE_UTF8_TWO_BYTE \
690 nBIT_UMAX(5 + MIN( UTF_CONTINUATION_BYTE_INFO_BITS, \
691 UTF_EBCDIC_CONTINUATION_BYTE_INFO_BITS))
695 =for apidoc AmnU|STRLEN|UTF8_MAXBYTES_CASE
697 The maximum number of UTF-8 bytes a single Unicode character can
698 uppercase/lowercase/titlecase/fold into.
702 * Unicode guarantees that the maximum expansion is UTF8_MAX_FOLD_CHAR_EXPAND
703 * characters, but any above-Unicode code point will fold to itself, so we only
704 * have to look at the expansion of the maximum Unicode code point. But this
705 * number may be less than the space occupied by a very large code point under
706 * Perl's extended UTF-8. We have to make it large enough to fit any single
707 * character. (It turns out that ASCII and EBCDIC differ in which is larger)
711 #define UTF8_MAXBYTES_CASE \
712 MAX(UTF8_MAXBYTES, UTF8_MAX_FOLD_CHAR_EXPAND * UNISKIP_BY_MSB_(20))
714 /* Rest of these are attributes of Unicode and perl's internals rather than the
715 * encoding, or happen to be the same in both ASCII and EBCDIC (at least at
716 * this level; the macros that some of these call may have different
717 * definitions in the two encodings */
719 /* In domain restricted to ASCII, these may make more sense to the reader than
720 * the ones with Latin1 in the name */
721 #define NATIVE_TO_ASCII(ch) NATIVE_TO_LATIN1(ch)
722 #define ASCII_TO_NATIVE(ch) LATIN1_TO_NATIVE(ch)
724 /* More or less misleadingly-named defines, retained for back compat */
725 #define NATIVE_TO_UTF(ch) NATIVE_UTF8_TO_I8(ch)
726 #define NATIVE_TO_I8(ch) NATIVE_UTF8_TO_I8(ch)
727 #define UTF_TO_NATIVE(ch) I8_TO_NATIVE_UTF8(ch)
728 #define I8_TO_NATIVE(ch) I8_TO_NATIVE_UTF8(ch)
729 #define NATIVE8_TO_UNI(ch) NATIVE_TO_LATIN1(ch)
731 /* Adds a UTF8 continuation byte 'new' of information to a running total code
732 * point 'old' of all the continuation bytes so far. This is designed to be
733 * used in a loop to convert from UTF-8 to the code point represented. Note
734 * that this is asymmetric on EBCDIC platforms, in that the 'new' parameter is
735 * the UTF-EBCDIC byte, whereas the 'old' parameter is a Unicode (not EBCDIC)
736 * code point in process of being generated */
737 #define UTF8_ACCUMULATE(old, new) (__ASSERT_(FITS_IN_8_BITS(new)) \
738 ((old) << UTF_ACCUMULATION_SHIFT) \
739 | ((NATIVE_UTF8_TO_I8(new)) \
740 & UTF_CONTINUATION_MASK))
742 /* This works in the face of malformed UTF-8. */
743 #define UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, e) \
745 && UTF8_IS_DOWNGRADEABLE_START(*(s)) \
746 && UTF8_IS_CONTINUATION(*((s)+1)))
748 /* Longer, but more accurate name */
749 #define UTF8_IS_ABOVE_LATIN1_START(c) UTF8_IS_ABOVE_LATIN1(c)
751 /* Convert a UTF-8 variant Latin1 character to a native code point value.
752 * Needs just one iteration of accumulate. Should be used only if it is known
753 * that the code point is < 256, and is not UTF-8 invariant. Use the slower
754 * but more general TWO_BYTE_UTF8_TO_NATIVE() which handles any code point
755 * representable by two bytes (which turns out to be up through
756 * MAX_PORTABLE_UTF8_TWO_BYTE). The two parameters are:
757 * HI: a downgradable start byte;
760 #define EIGHT_BIT_UTF8_TO_NATIVE(HI, LO) \
761 ( __ASSERT_(UTF8_IS_DOWNGRADEABLE_START(HI)) \
762 __ASSERT_(UTF8_IS_CONTINUATION(LO)) \
763 LATIN1_TO_NATIVE(UTF8_ACCUMULATE(( \
764 NATIVE_UTF8_TO_I8(HI) & UTF_START_MASK(2)), (LO))))
766 /* Convert a two (not one) byte utf8 character to a native code point value.
767 * Needs just one iteration of accumulate. Should not be used unless it is
768 * known that the two bytes are legal: 1) two-byte start, and 2) continuation.
769 * Note that the result can be larger than 255 if the input character is not
771 #define TWO_BYTE_UTF8_TO_NATIVE(HI, LO) \
772 (__ASSERT_(FITS_IN_8_BITS(HI)) \
773 __ASSERT_(FITS_IN_8_BITS(LO)) \
774 __ASSERT_(PL_utf8skip[(U8) HI] == 2) \
775 __ASSERT_(UTF8_IS_CONTINUATION(LO)) \
776 UNI_TO_NATIVE(UTF8_ACCUMULATE((NATIVE_UTF8_TO_I8(HI) & UTF_START_MASK(2)), \
779 /* Should never be used, and be deprecated */
780 #define TWO_BYTE_UTF8_TO_UNI(HI, LO) NATIVE_TO_UNI(TWO_BYTE_UTF8_TO_NATIVE(HI, LO))
784 =for apidoc Am|STRLEN|UTF8SKIP|const char* s
785 =for apidoc_item |STRLEN|UTF8_SKIP|const char* s
786 =for apidoc_item |STRLEN|UTF8_CHK_SKIP|const char* s
787 =for apidoc_item |STRLEN|UTF8_SAFE_SKIP|const char* s|const char* e
789 Each of these returns the number of bytes in the UTF-8 encoded character
790 whose first (perhaps only) byte is pointed to by C<s>.
792 C<UTF8SKIP> and C<UTF8_SKIP> are synonyms. Use them when there is no
793 possibility that the character pointed to by C<s> is malformed.
795 If there is a possibility of malformed input, use instead:
799 =item C<L</UTF8_SAFE_SKIP>> if you know the maximum ending pointer in the buffer pointed to by C<s>
801 If the buffer has enough bytes to hold the character, it returns the
802 same value as C<UTF8SKIP> and C<UTF8_SKIP> would. If the buffer has
803 fewer bytes than can fit, it returns the number of bytes available in
804 the buffer, which could be 0 if S<C<s E<gt>= e>>. On DEBUGGING builds,
805 it asserts that S<C<s E<lt>= e>>.
807 =item C<L</UTF8_CHK_SKIP>> if you don't know the maximum ending pointer
809 This version doesn't blindly assume that the input string pointed to by
810 C<s> is well-formed, but verifies that there isn't a NUL terminating
811 character before the expected end of the next character in C<s>. The
812 length C<UTF8_CHK_SKIP> returns stops just before any such NUL.
814 Perl tends to add NULs, as an insurance policy, after the end of strings in
815 SV's, so it is likely that using this macro on an SV string will prevent
816 inadvertent reading beyond the end of the input buffer, even if it is
819 This macro is intended to be used by XS modules where the inputs could be
820 malformed, and it isn't feasible to restructure to use the safer
821 C<L</UTF8_SAFE_SKIP>>, for example when interfacing with a C library.
827 #define UTF8SKIP(s) PL_utf8skip[*(const U8*)(ASSERT_IS_PTR(s))]
828 #define UTF8_SKIP(s) UTF8SKIP(s)
829 #define UTF8_CHK_SKIP(s) \
830 (UNLIKELY(s[0] == '\0') ? 1 : my_strnlen((const char *) (s), UTF8SKIP(s)))
831 #define UTF8_SAFE_SKIP(s, e) (__ASSERT_((e) >= (s)) \
832 UNLIKELY(((e) - (s)) <= 0) \
834 : MIN(((e) - (s)), UTF8_SKIP(s)))
836 /* Most code that says 'UNI_' really means the native value for code points up
838 #define UNI_IS_INVARIANT(cp) UVCHR_IS_INVARIANT(cp)
841 =for apidoc Am|bool|UTF8_IS_INVARIANT|char c
843 Evaluates to 1 if the byte C<c> represents the same character when encoded in
844 UTF-8 as when not; otherwise evaluates to 0. UTF-8 invariant characters can be
845 copied as-is when converting to/from UTF-8, saving time.
847 In spite of the name, this macro gives the correct result if the input string
848 from which C<c> comes is not encoded in UTF-8.
850 See C<L</UVCHR_IS_INVARIANT>> for checking if a UV is invariant.
854 The reason it works on both UTF-8 encoded strings and non-UTF-8 encoded, is
855 that it returns TRUE in each for the exact same set of bit patterns. It is
856 valid on a subset of what UVCHR_IS_INVARIANT is valid on, so can just use that;
857 and the compiler should optimize out anything extraneous given the
858 implementation of the latter. */
859 #define UTF8_IS_INVARIANT(c) UVCHR_IS_INVARIANT(ASSERT_NOT_PTR(c))
861 /* Like the above, but its name implies a non-UTF8 input, which as the comments
862 * above show, doesn't matter as to its implementation */
863 #define NATIVE_BYTE_IS_INVARIANT(c) UVCHR_IS_INVARIANT(c)
865 /* Misleadingly named: is the UTF8-encoded byte 'c' part of a variant sequence
866 * in UTF-8? This is the inverse of UTF8_IS_INVARIANT. */
867 #define UTF8_IS_CONTINUED(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
868 (! UTF8_IS_INVARIANT(c)))
870 /* The macros in the next 4 sets are used to generate the two utf8 or utfebcdic
871 * bytes from an ordinal that is known to fit into exactly two (not one) bytes;
872 * it must be less than 0x3FF to work across both encodings. */
874 /* These two are helper macros for the other three sets, and should not be used
875 * directly anywhere else. 'translate_function' is either NATIVE_TO_LATIN1
876 * (which works for code points up through 0xFF) or NATIVE_TO_UNI which works
877 * for any code point */
878 #define __BASE_TWO_BYTE_HI(c, translate_function) \
879 (__ASSERT_(! UVCHR_IS_INVARIANT(c)) \
880 I8_TO_NATIVE_UTF8((translate_function(c) >> UTF_ACCUMULATION_SHIFT) \
881 | UTF_START_MARK(2)))
882 #define __BASE_TWO_BYTE_LO(c, translate_function) \
883 (__ASSERT_(! UVCHR_IS_INVARIANT(c)) \
884 I8_TO_NATIVE_UTF8((translate_function(c) & UTF_CONTINUATION_MASK) \
885 | UTF_CONTINUATION_MARK))
887 /* The next two macros should not be used. They were designed to be usable as
888 * the case label of a switch statement, but this doesn't work for EBCDIC. Use
889 * regen/unicode_constants.pl instead */
890 #define UTF8_TWO_BYTE_HI_nocast(c) __BASE_TWO_BYTE_HI(c, NATIVE_TO_UNI)
891 #define UTF8_TWO_BYTE_LO_nocast(c) __BASE_TWO_BYTE_LO(c, NATIVE_TO_UNI)
893 /* The next two macros are used when the source should be a single byte
894 * character; checked for under DEBUGGING */
895 #define UTF8_EIGHT_BIT_HI(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
896 ( __BASE_TWO_BYTE_HI(c, NATIVE_TO_LATIN1)))
897 #define UTF8_EIGHT_BIT_LO(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
898 (__BASE_TWO_BYTE_LO(c, NATIVE_TO_LATIN1)))
900 /* These final two macros in the series are used when the source can be any
901 * code point whose UTF-8 is known to occupy 2 bytes; they are less efficient
902 * than the EIGHT_BIT versions on EBCDIC platforms. We use the logical '~'
903 * operator instead of "<=" to avoid getting compiler warnings.
904 * MAX_UTF8_TWO_BYTE should be exactly all one bits in the lower few
905 * places, so the ~ works */
906 #define UTF8_TWO_BYTE_HI(c) \
907 (__ASSERT_((sizeof(c) == 1) \
908 || !(((WIDEST_UTYPE)(c)) & ~MAX_UTF8_TWO_BYTE)) \
909 (__BASE_TWO_BYTE_HI(c, NATIVE_TO_UNI)))
910 #define UTF8_TWO_BYTE_LO(c) \
911 (__ASSERT_((sizeof(c) == 1) \
912 || !(((WIDEST_UTYPE)(c)) & ~MAX_UTF8_TWO_BYTE)) \
913 (__BASE_TWO_BYTE_LO(c, NATIVE_TO_UNI)))
915 /* This is illegal in any well-formed UTF-8 in both EBCDIC and ASCII
916 * as it is only in overlongs. */
917 #define ILLEGAL_UTF8_BYTE I8_TO_NATIVE_UTF8(0xC1)
920 * 'UTF' is whether or not p is encoded in UTF8. The names 'foo_lazy_if' stem
921 * from an earlier version of these macros in which they didn't call the
922 * foo_utf8() macros (i.e. were 'lazy') unless they decided that *p is the
923 * beginning of a utf8 character. Now that foo_utf8() determines that itself,
924 * no need to do it again here
926 #define isIDFIRST_lazy_if_safe(p, e, UTF) \
927 ((IN_BYTES || !UTF) \
929 : isIDFIRST_utf8_safe(p, e))
930 #define isWORDCHAR_lazy_if_safe(p, e, UTF) \
931 ((IN_BYTES || !UTF) \
933 : isWORDCHAR_utf8_safe((U8 *) p, (U8 *) e))
934 #define isALNUM_lazy_if_safe(p, e, UTF) isWORDCHAR_lazy_if_safe(p, e, UTF)
936 #define UTF8_MAXLEN UTF8_MAXBYTES
938 /* A Unicode character can fold to up to 3 characters */
939 #define UTF8_MAX_FOLD_CHAR_EXPAND 3
941 #define IN_BYTES UNLIKELY(CopHINTS_get(PL_curcop) & HINT_BYTES)
945 =for apidoc Am|bool|DO_UTF8|SV* sv
946 Returns a bool giving whether or not the PV in C<sv> is to be treated as being
949 You should use this I<after> a call to C<SvPV()> or one of its variants, in
950 case any call to string overloading updates the internal UTF-8 encoding flag.
954 #define DO_UTF8(sv) (SvUTF8(sv) && !IN_BYTES)
956 /* Should all strings be treated as Unicode, and not just UTF-8 encoded ones?
957 * Is so within 'feature unicode_strings' or 'locale :not_characters', and not
958 * within 'use bytes'. UTF-8 locales are not tested for here, because it gets
959 * complicated by the probability of having categories in different locales. */
960 #define IN_UNI_8_BIT \
961 (( ( (CopHINTS_get(PL_curcop) & HINT_UNI_8_BIT)) \
962 || ( CopHINTS_get(PL_curcop) & HINT_LOCALE \
963 /* -1 below is for :not_characters */ \
964 && is_in_locale_category_(FALSE, -1))) \
967 #define UNICODE_SURROGATE_FIRST 0xD800
968 #define UNICODE_SURROGATE_LAST 0xDFFF
971 =for apidoc Am|bool|UNICODE_IS_SURROGATE|const UV uv
973 Returns a boolean as to whether or not C<uv> is one of the Unicode surrogate
976 =for apidoc Am|bool|UTF8_IS_SURROGATE|const U8 *s|const U8 *e
978 Evaluates to non-zero if the first few bytes of the string starting at C<s> and
979 looking no further than S<C<e - 1>> are well-formed UTF-8 that represents one
980 of the Unicode surrogate code points; otherwise it evaluates to 0. If
981 non-zero, the value gives how many bytes starting at C<s> comprise the code
982 point's representation.
987 #define UNICODE_IS_SURROGATE(uv) UNLIKELY(inRANGE(uv, UNICODE_SURROGATE_FIRST, \
988 UNICODE_SURROGATE_LAST))
989 #define UTF8_IS_SURROGATE(s, e) is_SURROGATE_utf8_safe(s, e)
993 =for apidoc AmnU|UV|UNICODE_REPLACEMENT
995 Evaluates to 0xFFFD, the code point of the Unicode REPLACEMENT CHARACTER
997 =for apidoc Am|bool|UNICODE_IS_REPLACEMENT|const UV uv
999 Returns a boolean as to whether or not C<uv> is the Unicode REPLACEMENT
1002 =for apidoc Am|bool|UTF8_IS_REPLACEMENT|const U8 *s|const U8 *e
1004 Evaluates to non-zero if the first few bytes of the string starting at C<s> and
1005 looking no further than S<C<e - 1>> are well-formed UTF-8 that represents the
1006 Unicode REPLACEMENT CHARACTER; otherwise it evaluates to 0. If non-zero, the
1007 value gives how many bytes starting at C<s> comprise the code point's
1012 #define UNICODE_REPLACEMENT 0xFFFD
1013 #define UNICODE_IS_REPLACEMENT(uv) UNLIKELY((UV) (uv) == UNICODE_REPLACEMENT)
1014 #define UTF8_IS_REPLACEMENT(s, send) \
1016 ((send) - (s)) >= ((SSize_t)(sizeof(REPLACEMENT_CHARACTER_UTF8) - 1))\
1017 && memEQ((s), REPLACEMENT_CHARACTER_UTF8, \
1018 sizeof(REPLACEMENT_CHARACTER_UTF8) - 1))
1020 /* Max legal code point according to Unicode */
1021 #define PERL_UNICODE_MAX 0x10FFFF
1025 =for apidoc Am|bool|UNICODE_IS_SUPER|const UV uv
1027 Returns a boolean as to whether or not C<uv> is above the maximum legal Unicode
1028 code point of U+10FFFF.
1033 #define UNICODE_IS_SUPER(uv) UNLIKELY((UV) (uv) > PERL_UNICODE_MAX)
1036 =for apidoc Am|bool|UTF8_IS_SUPER|const U8 *s|const U8 *e
1038 Recall that Perl recognizes an extension to UTF-8 that can encode code
1039 points larger than the ones defined by Unicode, which are 0..0x10FFFF.
1041 This macro evaluates to non-zero if the first few bytes of the string starting
1042 at C<s> and looking no further than S<C<e - 1>> are from this UTF-8 extension;
1043 otherwise it evaluates to 0. If non-zero, the return is how many bytes
1044 starting at C<s> comprise the code point's representation.
1046 0 is returned if the bytes are not well-formed extended UTF-8, or if they
1047 represent a code point that cannot fit in a UV on the current platform. Hence
1048 this macro can give different results when run on a 64-bit word machine than on
1049 one with a 32-bit word size.
1051 Note that it is illegal in Perl to have code points that are larger than what can
1052 fit in an IV on the current machine; and illegal in Unicode to have any that
1058 * U+10FFFF: \xF4\x8F\xBF\xBF \xF9\xA1\xBF\xBF\xBF max legal Unicode
1059 * U+110000: \xF4\x90\x80\x80 \xF9\xA2\xA0\xA0\xA0
1060 * U+110001: \xF4\x90\x80\x81 \xF9\xA2\xA0\xA0\xA1
1062 #define UTF_START_BYTE_110000_ UTF_START_BYTE(PERL_UNICODE_MAX + 1, 21)
1063 #define UTF_FIRST_CONT_BYTE_110000_ \
1064 UTF_FIRST_CONT_BYTE(PERL_UNICODE_MAX + 1, 21)
1066 /* Internal macro when we don't care about it being well-formed, and know we
1067 * have two bytes available to read */
1068 #define UTF8_IS_SUPER_NO_CHECK_(s) \
1069 ( NATIVE_UTF8_TO_I8(s[0]) >= UTF_START_BYTE_110000_ \
1070 && ( NATIVE_UTF8_TO_I8(s[0]) > UTF_START_BYTE_110000_ \
1071 || NATIVE_UTF8_TO_I8(s[1]) >= UTF_FIRST_CONT_BYTE_110000_))
1073 #define UTF8_IS_SUPER(s, e) \
1074 ((((e) - (s)) >= UNISKIP_BY_MSB_(20) && UTF8_IS_SUPER_NO_CHECK_(s)) \
1075 ? isUTF8_CHAR(s, e) \
1079 =for apidoc Am|bool|UNICODE_IS_NONCHAR|const UV uv
1081 Returns a boolean as to whether or not C<uv> is one of the Unicode
1082 non-character code points
1087 /* Is 'uv' one of the 32 contiguous-range noncharacters? */
1088 #define UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv) \
1089 UNLIKELY(inRANGE(uv, 0xFDD0, 0xFDEF))
1091 /* Is 'uv' one of the 34 plane-ending noncharacters 0xFFFE, 0xFFFF, 0x1FFFE,
1092 * 0x1FFFF, ... 0x10FFFE, 0x10FFFF, given that we know that 'uv' is not above
1093 * the Unicode legal max */
1094 #define UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv) \
1095 UNLIKELY(((UV) (uv) & 0xFFFE) == 0xFFFE)
1097 #define UNICODE_IS_NONCHAR(uv) \
1098 ( UNLIKELY(UNICODE_IS_32_CONTIGUOUS_NONCHARS(uv)) \
1099 || ( UNLIKELY(UNICODE_IS_END_PLANE_NONCHAR_GIVEN_NOT_SUPER(uv)) \
1100 && LIKELY(! UNICODE_IS_SUPER(uv))))
1103 =for apidoc Am|bool|UTF8_IS_NONCHAR|const U8 *s|const U8 *e
1105 Evaluates to non-zero if the first few bytes of the string starting at C<s> and
1106 looking no further than S<C<e - 1>> are well-formed UTF-8 that represents one
1107 of the Unicode non-character code points; otherwise it evaluates to 0. If
1108 non-zero, the value gives how many bytes starting at C<s> comprise the code
1109 point's representation.
1113 #define UTF8_IS_NONCHAR(s, e) is_NONCHAR_utf8_safe(s,e)
1115 /* This is now machine generated, and the 'given' clause is no longer
1117 #define UTF8_IS_NONCHAR_GIVEN_THAT_NON_SUPER_AND_GE_PROBLEMATIC(s, e) \
1118 UTF8_IS_NONCHAR(s, e)
1120 /* Surrogates, non-character code points and above-Unicode code points are
1121 * problematic in some contexts. These macros allow code that needs to check
1122 * for those to quickly exclude the vast majority of code points it will
1125 * The lowest such code point is the smallest surrogate, U+D800. We calculate
1126 * the start byte of that. 0xD800 occupies 16 bits. */
1127 #define isUNICODE_POSSIBLY_PROBLEMATIC(uv) ((uv) >= UNICODE_SURROGATE_FIRST)
1128 #define isUTF8_POSSIBLY_PROBLEMATIC(c) \
1129 (NATIVE_UTF8_TO_I8(c) >= UTF_START_BYTE(UNICODE_SURROGATE_FIRST, 16))
1131 /* Perl extends Unicode so that it is possible to encode (as extended UTF-8 or
1132 * UTF-EBCDIC) any 64-bit value. No standard known to khw ever encoded higher
1133 * than a 31 bit value. On ASCII platforms this just meant arbitrarily saying
1134 * nothing could be higher than this. On these the start byte FD gets you to
1135 * 31 bits, and FE and FF are forbidden as start bytes. On EBCDIC platforms,
1136 * FD gets you only to 26 bits; adding FE to mean 7 total bytes gets you to 30
1137 * bits. To get to 31 bits, they treated an initial FF byte idiosyncratically.
1138 * It was considered to be the start byte FE meaning it had 7 total bytes, and
1139 * the final 1 was treated as an information bit, getting you to 31 bits.
1141 * Perl used to accept this idiosyncratic interpretation of FF, but now rejects
1142 * it in order to get to being able to encode 64 bits. The bottom line is that
1143 * it is a Perl extension to use the start bytes FE and FF on ASCII platforms,
1144 * and the start byte FF on EBCDIC ones. That translates into that it is a
1145 * Perl extension to represent anything occupying more than 31 bits on ASCII
1146 * platforms; 30 bits on EBCDIC. */
1147 #define UNICODE_IS_PERL_EXTENDED(uv) \
1148 UNLIKELY((UV) (uv) > nBIT_UMAX(31 - ONE_IF_EBCDIC_ZERO_IF_NOT))
1149 #define UTF8_IS_PERL_EXTENDED(s) \
1150 (UTF8SKIP(s) > 6 + ONE_IF_EBCDIC_ZERO_IF_NOT)
1152 /* Largest code point we accept from external sources */
1153 #define MAX_LEGAL_CP ((UV)IV_MAX)
1155 /* The ordering of these bits is important to a switch() statement in utf8.c
1156 * for handling problems in converting UTF-8 to a UV */
1157 #define UTF8_ALLOW_OVERFLOW 0x0001
1158 #define UTF8_GOT_OVERFLOW UTF8_ALLOW_OVERFLOW
1160 #define UTF8_ALLOW_EMPTY 0x0002 /* Allow a zero length string */
1161 #define UTF8_GOT_EMPTY UTF8_ALLOW_EMPTY
1163 /* Allow first byte to be a continuation byte */
1164 #define UTF8_ALLOW_CONTINUATION 0x0004
1165 #define UTF8_GOT_CONTINUATION UTF8_ALLOW_CONTINUATION
1167 /* expecting more bytes than were available in the string */
1168 #define UTF8_ALLOW_SHORT 0x0008
1169 #define UTF8_GOT_SHORT UTF8_ALLOW_SHORT
1171 /* Unexpected non-continuation byte */
1172 #define UTF8_ALLOW_NON_CONTINUATION 0x0010
1173 #define UTF8_GOT_NON_CONTINUATION UTF8_ALLOW_NON_CONTINUATION
1175 #define UTF8_DISALLOW_SURROGATE 0x0020 /* Unicode surrogates */
1176 #define UTF8_GOT_SURROGATE UTF8_DISALLOW_SURROGATE
1177 #define UTF8_WARN_SURROGATE 0x0040
1179 /* The original UTF-8 standard did not define UTF-8 with start bytes of 0xFE or
1180 * 0xFF, though UTF-EBCDIC did. This allowed both versions to represent code
1181 * points up to 2 ** 31 - 1. Perl extends UTF-8 so that 0xFE and 0xFF are
1182 * usable on ASCII platforms, and 0xFF means something different than
1183 * UTF-EBCDIC defines. These changes allow code points of 64 bits (actually
1184 * somewhat more) to be represented on both platforms. But these are Perl
1185 * extensions, and not likely to be interchangeable with other languages. Note
1186 * that on ASCII platforms, FE overflows a signed 32-bit word, and FF an
1188 #define UTF8_DISALLOW_PERL_EXTENDED 0x0080
1189 #define UTF8_GOT_PERL_EXTENDED UTF8_DISALLOW_PERL_EXTENDED
1190 #define UTF8_WARN_PERL_EXTENDED 0x0100
1192 /* Super-set of Unicode: code points above the legal max */
1193 #define UTF8_DISALLOW_SUPER 0x0200
1194 #define UTF8_GOT_SUPER UTF8_DISALLOW_SUPER
1195 #define UTF8_WARN_SUPER 0x0400
1197 /* Unicode non-character code points */
1198 #define UTF8_DISALLOW_NONCHAR 0x0800
1199 #define UTF8_GOT_NONCHAR UTF8_DISALLOW_NONCHAR
1200 #define UTF8_WARN_NONCHAR 0x1000
1202 /* Overlong sequence; i.e., the code point can be specified in fewer bytes.
1203 * First one will convert the overlong to the REPLACEMENT CHARACTER; second
1204 * will return what the overlong evaluates to */
1205 #define UTF8_ALLOW_LONG 0x2000
1206 #define UTF8_GOT_LONG UTF8_ALLOW_LONG
1207 #define UTF8_ALLOW_LONG_AND_ITS_VALUE 0x4000
1208 #define UTF8_GOT_LONG_WITH_VALUE UTF8_ALLOW_LONG_AND_ITS_VALUE
1210 /* For back compat, these old names are misleading for overlongs and
1212 #define UTF8_DISALLOW_ABOVE_31_BIT UTF8_DISALLOW_PERL_EXTENDED
1213 #define UTF8_GOT_ABOVE_31_BIT UTF8_GOT_PERL_EXTENDED
1214 #define UTF8_WARN_ABOVE_31_BIT UTF8_WARN_PERL_EXTENDED
1215 #define UTF8_DISALLOW_FE_FF UTF8_DISALLOW_PERL_EXTENDED
1216 #define UTF8_WARN_FE_FF UTF8_WARN_PERL_EXTENDED
1218 #define UTF8_CHECK_ONLY 0x8000
1219 #define UTF8_NO_CONFIDENCE_IN_CURLEN_ 0x10000 /* Internal core use only */
1220 #define UTF8_DIE_IF_MALFORMED 0x20000
1221 #define UTF8_FORCE_WARN_IF_MALFORMED 0x40000
1223 /* For backwards source compatibility. They do nothing, as the default now
1224 * includes what they used to mean. The first one's meaning was to allow the
1225 * just the single non-character 0xFFFF */
1226 #define UTF8_ALLOW_FFFF 0
1227 #define UTF8_ALLOW_FE_FF 0
1228 #define UTF8_ALLOW_SURROGATE 0
1230 /* C9 refers to Unicode Corrigendum #9: allows but discourages non-chars */
1231 #define UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE \
1232 (UTF8_DISALLOW_SUPER|UTF8_DISALLOW_PERL_EXTENDED|UTF8_DISALLOW_SURROGATE)
1233 #define UTF8_WARN_ILLEGAL_C9_INTERCHANGE \
1234 (UTF8_WARN_SUPER|UTF8_WARN_PERL_EXTENDED|UTF8_WARN_SURROGATE)
1236 #define UTF8_DISALLOW_ILLEGAL_INTERCHANGE \
1237 (UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE|UTF8_DISALLOW_NONCHAR)
1238 #define UTF8_WARN_ILLEGAL_INTERCHANGE \
1239 (UTF8_WARN_ILLEGAL_C9_INTERCHANGE|UTF8_WARN_NONCHAR)
1241 /* This is typically used for code that processes UTF-8 input and doesn't want
1242 * to have to deal with any malformations that might be present. All such will
1243 * be safely replaced by the REPLACEMENT CHARACTER, unless other flags
1244 * overriding this are also present. */
1245 #define UTF8_ALLOW_ANY ( UTF8_ALLOW_CONTINUATION \
1246 |UTF8_ALLOW_NON_CONTINUATION \
1249 |UTF8_ALLOW_OVERFLOW)
1251 /* Accept any Perl-extended UTF-8 that evaluates to any UV on the platform, but
1252 * not any malformed. This is the default. */
1253 #define UTF8_ALLOW_ANYUV 0
1254 #define UTF8_ALLOW_DEFAULT UTF8_ALLOW_ANYUV
1256 #define UNICODE_WARN_SURROGATE 0x0001 /* UTF-16 surrogates */
1257 #define UNICODE_WARN_NONCHAR 0x0002 /* Non-char code points */
1258 #define UNICODE_WARN_SUPER 0x0004 /* Above 0x10FFFF */
1259 #define UNICODE_WARN_PERL_EXTENDED 0x0008 /* Above 0x7FFF_FFFF */
1260 #define UNICODE_WARN_ABOVE_31_BIT UNICODE_WARN_PERL_EXTENDED
1261 #define UNICODE_DISALLOW_SURROGATE 0x0010
1262 #define UNICODE_DISALLOW_NONCHAR 0x0020
1263 #define UNICODE_DISALLOW_SUPER 0x0040
1264 #define UNICODE_DISALLOW_PERL_EXTENDED 0x0080
1267 # define UNICODE_ALLOW_ABOVE_IV_MAX 0x0100
1269 #define UNICODE_DISALLOW_ABOVE_31_BIT UNICODE_DISALLOW_PERL_EXTENDED
1271 #define UNICODE_GOT_SURROGATE UNICODE_DISALLOW_SURROGATE
1272 #define UNICODE_GOT_NONCHAR UNICODE_DISALLOW_NONCHAR
1273 #define UNICODE_GOT_SUPER UNICODE_DISALLOW_SUPER
1274 #define UNICODE_GOT_PERL_EXTENDED UNICODE_DISALLOW_PERL_EXTENDED
1276 #define UNICODE_WARN_ILLEGAL_C9_INTERCHANGE \
1277 (UNICODE_WARN_SURROGATE|UNICODE_WARN_SUPER)
1278 #define UNICODE_WARN_ILLEGAL_INTERCHANGE \
1279 (UNICODE_WARN_ILLEGAL_C9_INTERCHANGE|UNICODE_WARN_NONCHAR)
1280 #define UNICODE_DISALLOW_ILLEGAL_C9_INTERCHANGE \
1281 (UNICODE_DISALLOW_SURROGATE|UNICODE_DISALLOW_SUPER)
1282 #define UNICODE_DISALLOW_ILLEGAL_INTERCHANGE \
1283 (UNICODE_DISALLOW_ILLEGAL_C9_INTERCHANGE|UNICODE_DISALLOW_NONCHAR)
1285 /* For backward source compatibility, as are now the default */
1286 #define UNICODE_ALLOW_SURROGATE 0
1287 #define UNICODE_ALLOW_SUPER 0
1288 #define UNICODE_ALLOW_ANY 0
1290 #define UNICODE_BYTE_ORDER_MARK 0xFEFF
1291 #define UNICODE_IS_BYTE_ORDER_MARK(uv) UNLIKELY((UV) (uv) \
1292 == UNICODE_BYTE_ORDER_MARK)
1294 #define LATIN_SMALL_LETTER_SHARP_S LATIN_SMALL_LETTER_SHARP_S_NATIVE
1295 #define LATIN_SMALL_LETTER_Y_WITH_DIAERESIS \
1296 LATIN_SMALL_LETTER_Y_WITH_DIAERESIS_NATIVE
1297 #define MICRO_SIGN MICRO_SIGN_NATIVE
1298 #define LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVE \
1299 LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVE_NATIVE
1300 #define LATIN_SMALL_LETTER_A_WITH_RING_ABOVE \
1301 LATIN_SMALL_LETTER_A_WITH_RING_ABOVE_NATIVE
1302 #define UNICODE_GREEK_CAPITAL_LETTER_SIGMA 0x03A3
1303 #define UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA 0x03C2
1304 #define UNICODE_GREEK_SMALL_LETTER_SIGMA 0x03C3
1305 #define GREEK_SMALL_LETTER_MU 0x03BC
1306 #define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case
1308 #define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
1309 #ifdef LATIN_CAPITAL_LETTER_SHARP_S_UTF8
1310 # define LATIN_CAPITAL_LETTER_SHARP_S 0x1E9E
1312 #define LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE 0x130
1313 #define LATIN_SMALL_LETTER_DOTLESS_I 0x131
1314 #define LATIN_SMALL_LETTER_LONG_S 0x017F
1315 #define LATIN_SMALL_LIGATURE_LONG_S_T 0xFB05
1316 #define LATIN_SMALL_LIGATURE_ST 0xFB06
1317 #define KELVIN_SIGN 0x212A
1318 #define ANGSTROM_SIGN 0x212B
1320 #define UNI_DISPLAY_ISPRINT 0x0001
1321 #define UNI_DISPLAY_BACKSLASH 0x0002
1322 #define UNI_DISPLAY_BACKSPACE 0x0004 /* Allow \b when also
1323 UNI_DISPLAY_BACKSLASH */
1324 #define UNI_DISPLAY_QQ (UNI_DISPLAY_ISPRINT \
1325 |UNI_DISPLAY_BACKSLASH \
1326 |UNI_DISPLAY_BACKSPACE)
1328 /* Character classes could also allow \b, but not patterns in general */
1329 #define UNI_DISPLAY_REGEX (UNI_DISPLAY_ISPRINT|UNI_DISPLAY_BACKSLASH)
1331 /* Should be removed; maybe deprecated, but not used in CPAN */
1332 #define SHARP_S_SKIP 2
1334 #define is_utf8_char_buf(buf, buf_end) isUTF8_CHAR(buf, buf_end)
1337 PL_utf8_to_bytes_overwrite = 0,
1338 PL_utf8_to_bytes_new_memory,
1339 PL_utf8_to_bytes_use_temporary,
1340 } Perl_utf8_to_bytes_arg;
1342 /* Do not use; should be deprecated. Use isUTF8_CHAR() instead; this is
1343 * retained solely for backwards compatibility */
1344 #define IS_UTF8_CHAR(p, n) (isUTF8_CHAR(p, (p) + (n)) == n)
1346 #define MALFORMED_UTF8_DIE TRUE
1347 #define MALFORMED_UTF8_WARN FALSE
1349 #endif /* PERL_UTF8_H_ */
1352 * ex: set ts=8 sts=4 sw=4 et: