Home | History | Annotate | Download | only in html
      1 <html>
      2 <head>
      3 <title>pcre2unicode specification</title>
      4 </head>
      5 <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
      6 <h1>pcre2unicode man page</h1>
      7 <p>
      8 Return to the <a href="index.html">PCRE2 index page</a>.
      9 </p>
     10 <p>
     11 This page is part of the PCRE2 HTML documentation. It was generated
     12 automatically from the original man page. If there is any nonsense in it,
     13 please consult the man page, in case the conversion went wrong.
     14 <br>
     15 <br><b>
     16 UNICODE AND UTF SUPPORT
     17 </b><br>
     18 <P>
     19 When PCRE2 is built with Unicode support (which is the default), it has
     20 knowledge of Unicode character properties and can process text strings in
     21 UTF-8, UTF-16, or UTF-32 format (depending on the code unit width). However, by
     22 default, PCRE2 assumes that one code unit is one character. To process a
     23 pattern as a UTF string, where a character may require more than one code unit,
     24 you must call
     25 <a href="pcre2_compile.html"><b>pcre2_compile()</b></a>
     26 with the PCRE2_UTF option flag, or the pattern must start with the sequence
     27 (*UTF). When either of these is the case, both the pattern and any subject
     28 strings that are matched against it are treated as UTF strings instead of
     29 strings of individual one-code-unit characters. There are also some other
     30 changes to the way characters are handled, as documented below.
     31 </P>
     32 <P>
     33 If you do not need Unicode support you can build PCRE2 without it, in which
     34 case the library will be smaller.
     35 </P>
     36 <br><b>
     37 UNICODE PROPERTY SUPPORT
     38 </b><br>
     39 <P>
     40 When PCRE2 is built with Unicode support, the escape sequences \p{..},
     41 \P{..}, and \X can be used. The Unicode properties that can be tested are
     42 limited to the general category properties such as Lu for an upper case letter
     43 or Nd for a decimal number, the Unicode script names such as Arabic or Han, and
     44 the derived properties Any and L&. Full lists are given in the
     45 <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
     46 and
     47 <a href="pcre2syntax.html"><b>pcre2syntax</b></a>
     48 documentation. Only the short names for properties are supported. For example,
     49 \p{L} matches a letter. Its Perl synonym, \p{Letter}, is not supported.
     50 Furthermore, in Perl, many properties may optionally be prefixed by "Is", for
     51 compatibility with Perl 5.6. PCRE2 does not support this.
     52 </P>
     53 <br><b>
     54 WIDE CHARACTERS AND UTF MODES
     55 </b><br>
     56 <P>
     57 Code points less than 256 can be specified in patterns by either braced or
     58 unbraced hexadecimal escape sequences (for example, \x{b3} or \xb3). Larger
     59 values have to use braced sequences. Unbraced octal code points up to \777 are
     60 also recognized; larger ones can be coded using \o{...}.
     61 </P>
     62 <P>
     63 The escape sequence \N{U+&#60;hex digits&#62;} is recognized as another way of
     64 specifying a Unicode character by code point in a UTF mode. It is not allowed
     65 in non-UTF modes.
     66 </P>
     67 <P>
     68 In UTF modes, repeat quantifiers apply to complete UTF characters, not to
     69 individual code units.
     70 </P>
     71 <P>
     72 In UTF modes, the dot metacharacter matches one UTF character instead of a
     73 single code unit.
     74 </P>
     75 <P>
     76 The escape sequence \C can be used to match a single code unit in a UTF mode,
     77 but its use can lead to some strange effects because it breaks up multi-unit
     78 characters (see the description of \C in the
     79 <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
     80 documentation).
     81 </P>
     82 <P>
     83 The use of \C is not supported by the alternative matching function
     84 <b>pcre2_dfa_match()</b> when in UTF-8 or UTF-16 mode, that is, when a character
     85 may consist of more than one code unit. The use of \C in these modes provokes
     86 a match-time error. Also, the JIT optimization does not support \C in these
     87 modes. If JIT optimization is requested for a UTF-8 or UTF-16 pattern that
     88 contains \C, it will not succeed, and so when <b>pcre2_match()</b> is called,
     89 the matching will be carried out by the normal interpretive function.
     90 </P>
     91 <P>
     92 The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly test
     93 characters of any code value, but, by default, the characters that PCRE2
     94 recognizes as digits, spaces, or word characters remain the same set as in
     95 non-UTF mode, all with code points less than 256. This remains true even when
     96 PCRE2 is built to include Unicode support, because to do otherwise would slow
     97 down matching in many common cases. Note that this also applies to \b
     98 and \B, because they are defined in terms of \w and \W. If you want
     99 to test for a wider sense of, say, "digit", you can use explicit Unicode
    100 property tests such as \p{Nd}. Alternatively, if you set the PCRE2_UCP option,
    101 the way that the character escapes work is changed so that Unicode properties
    102 are used to determine which characters match. There are more details in the
    103 section on
    104 <a href="pcre2pattern.html#genericchartypes">generic character types</a>
    105 in the
    106 <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
    107 documentation.
    108 </P>
    109 <P>
    110 Similarly, characters that match the POSIX named character classes are all
    111 low-valued characters, unless the PCRE2_UCP option is set.
    112 </P>
    113 <P>
    114 However, the special horizontal and vertical white space matching escapes (\h,
    115 \H, \v, and \V) do match all the appropriate Unicode characters, whether or
    116 not PCRE2_UCP is set.
    117 </P>
    118 <br><b>
    119 CASE-EQUIVALENCE IN UTF MODES
    120 </b><br>
    121 <P>
    122 Case-insensitive matching in a UTF mode makes use of Unicode properties except
    123 for characters whose code points are less than 128 and that have at most two
    124 case-equivalent values. For these, a direct table lookup is used for speed. A
    125 few Unicode characters such as Greek sigma have more than two code points that
    126 are case-equivalent, and these are treated as such.
    127 </P>
    128 <br><b>
    129 VALIDITY OF UTF STRINGS
    130 </b><br>
    131 <P>
    132 When the PCRE2_UTF option is set, the strings passed as patterns and subjects
    133 are (by default) checked for validity on entry to the relevant functions.
    134 If an invalid UTF string is passed, an negative error code is returned. The
    135 code unit offset to the offending character can be extracted from the match
    136 data block by calling <b>pcre2_get_startchar()</b>, which is used for this
    137 purpose after a UTF error.
    138 </P>
    139 <P>
    140 UTF-16 and UTF-32 strings can indicate their endianness by special code knows
    141 as a byte-order mark (BOM). The PCRE2 functions do not handle this, expecting
    142 strings to be in host byte order.
    143 </P>
    144 <P>
    145 A UTF string is checked before any other processing takes place. In the case of
    146 <b>pcre2_match()</b> and <b>pcre2_dfa_match()</b> calls with a non-zero starting
    147 offset, the check is applied only to that part of the subject that could be
    148 inspected during matching, and there is a check that the starting offset points
    149 to the first code unit of a character or to the end of the subject. If there
    150 are no lookbehind assertions in the pattern, the check starts at the starting
    151 offset. Otherwise, it starts at the length of the longest lookbehind before the
    152 starting offset, or at the start of the subject if there are not that many
    153 characters before the starting offset. Note that the sequences \b and \B are
    154 one-character lookbehinds.
    155 </P>
    156 <P>
    157 In addition to checking the format of the string, there is a check to ensure
    158 that all code points lie in the range U+0 to U+10FFFF, excluding the surrogate
    159 area. The so-called "non-character" code points are not excluded because
    160 Unicode corrigendum #9 makes it clear that they should not be.
    161 </P>
    162 <P>
    163 Characters in the "Surrogate Area" of Unicode are reserved for use by UTF-16,
    164 where they are used in pairs to encode code points with values greater than
    165 0xFFFF. The code points that are encoded by UTF-16 pairs are available
    166 independently in the UTF-8 and UTF-32 encodings. (In other words, the whole
    167 surrogate thing is a fudge for UTF-16 which unfortunately messes up UTF-8 and
    168 UTF-32.)
    169 </P>
    170 <P>
    171 In some situations, you may already know that your strings are valid, and
    172 therefore want to skip these checks in order to improve performance, for
    173 example in the case of a long subject string that is being scanned repeatedly.
    174 If you set the PCRE2_NO_UTF_CHECK option at compile time or at match time,
    175 PCRE2 assumes that the pattern or subject it is given (respectively) contains
    176 only valid UTF code unit sequences.
    177 </P>
    178 <P>
    179 Passing PCRE2_NO_UTF_CHECK to <b>pcre2_compile()</b> just disables the check for
    180 the pattern; it does not also apply to subject strings. If you want to disable
    181 the check for a subject string you must pass this option to <b>pcre2_match()</b>
    182 or <b>pcre2_dfa_match()</b>.
    183 </P>
    184 <P>
    185 If you pass an invalid UTF string when PCRE2_NO_UTF_CHECK is set, the result
    186 is undefined and your program may crash or loop indefinitely.
    187 </P>
    188 <P>
    189 Note that setting PCRE2_NO_UTF_CHECK at compile time does not disable the error
    190 that is given if an escape sequence for an invalid Unicode code point is
    191 encountered in the pattern. If you want to allow escape sequences such as
    192 \x{d800} (a surrogate code point) you can set the
    193 PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES extra option. However, this is possible
    194 only in UTF-8 and UTF-32 modes, because these values are not representable in
    195 UTF-16.
    196 <a name="utf8strings"></a></P>
    197 <br><b>
    198 Errors in UTF-8 strings
    199 </b><br>
    200 <P>
    201 The following negative error codes are given for invalid UTF-8 strings:
    202 <pre>
    203   PCRE2_ERROR_UTF8_ERR1
    204   PCRE2_ERROR_UTF8_ERR2
    205   PCRE2_ERROR_UTF8_ERR3
    206   PCRE2_ERROR_UTF8_ERR4
    207   PCRE2_ERROR_UTF8_ERR5
    208 </pre>
    209 The string ends with a truncated UTF-8 character; the code specifies how many
    210 bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8 characters to be
    211 no longer than 4 bytes, the encoding scheme (originally defined by RFC 2279)
    212 allows for up to 6 bytes, and this is checked first; hence the possibility of
    213 4 or 5 missing bytes.
    214 <pre>
    215   PCRE2_ERROR_UTF8_ERR6
    216   PCRE2_ERROR_UTF8_ERR7
    217   PCRE2_ERROR_UTF8_ERR8
    218   PCRE2_ERROR_UTF8_ERR9
    219   PCRE2_ERROR_UTF8_ERR10
    220 </pre>
    221 The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of the
    222 character do not have the binary value 0b10 (that is, either the most
    223 significant bit is 0, or the next bit is 1).
    224 <pre>
    225   PCRE2_ERROR_UTF8_ERR11
    226   PCRE2_ERROR_UTF8_ERR12
    227 </pre>
    228 A character that is valid by the RFC 2279 rules is either 5 or 6 bytes long;
    229 these code points are excluded by RFC 3629.
    230 <pre>
    231   PCRE2_ERROR_UTF8_ERR13
    232 </pre>
    233 A 4-byte character has a value greater than 0x10fff; these code points are
    234 excluded by RFC 3629.
    235 <pre>
    236   PCRE2_ERROR_UTF8_ERR14
    237 </pre>
    238 A 3-byte character has a value in the range 0xd800 to 0xdfff; this range of
    239 code points are reserved by RFC 3629 for use with UTF-16, and so are excluded
    240 from UTF-8.
    241 <pre>
    242   PCRE2_ERROR_UTF8_ERR15
    243   PCRE2_ERROR_UTF8_ERR16
    244   PCRE2_ERROR_UTF8_ERR17
    245   PCRE2_ERROR_UTF8_ERR18
    246   PCRE2_ERROR_UTF8_ERR19
    247 </pre>
    248 A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes for a
    249 value that can be represented by fewer bytes, which is invalid. For example,
    250 the two bytes 0xc0, 0xae give the value 0x2e, whose correct coding uses just
    251 one byte.
    252 <pre>
    253   PCRE2_ERROR_UTF8_ERR20
    254 </pre>
    255 The two most significant bits of the first byte of a character have the binary
    256 value 0b10 (that is, the most significant bit is 1 and the second is 0). Such a
    257 byte can only validly occur as the second or subsequent byte of a multi-byte
    258 character.
    259 <pre>
    260   PCRE2_ERROR_UTF8_ERR21
    261 </pre>
    262 The first byte of a character has the value 0xfe or 0xff. These values can
    263 never occur in a valid UTF-8 string.
    264 <a name="utf16strings"></a></P>
    265 <br><b>
    266 Errors in UTF-16 strings
    267 </b><br>
    268 <P>
    269 The following negative error codes are given for invalid UTF-16 strings:
    270 <pre>
    271   PCRE2_ERROR_UTF16_ERR1  Missing low surrogate at end of string
    272   PCRE2_ERROR_UTF16_ERR2  Invalid low surrogate follows high surrogate
    273   PCRE2_ERROR_UTF16_ERR3  Isolated low surrogate
    274 
    275 <a name="utf32strings"></a></PRE>
    276 </P>
    277 <br><b>
    278 Errors in UTF-32 strings
    279 </b><br>
    280 <P>
    281 The following negative error codes are given for invalid UTF-32 strings:
    282 <pre>
    283   PCRE2_ERROR_UTF32_ERR1  Surrogate character (0xd800 to 0xdfff)
    284   PCRE2_ERROR_UTF32_ERR2  Code point is greater than 0x10ffff
    285 
    286 </PRE>
    287 </P>
    288 <br><b>
    289 AUTHOR
    290 </b><br>
    291 <P>
    292 Philip Hazel
    293 <br>
    294 University Computing Service
    295 <br>
    296 Cambridge, England.
    297 <br>
    298 </P>
    299 <br><b>
    300 REVISION
    301 </b><br>
    302 <P>
    303 Last updated: 02 September 2018
    304 <br>
    305 Copyright &copy; 1997-2018 University of Cambridge.
    306 <br>
    307 <p>
    308 Return to the <a href="index.html">PCRE2 index page</a>.
    309 </p>
    310