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