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