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