xref: /aosp_15_r20/external/pcre/doc/html/pcre2unicode.html (revision 22dc650d8ae982c6770746019a6f94af92b0f024)
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>
8Return to the <a href="index.html">PCRE2 index page</a>.
9</p>
10<p>
11This page is part of the PCRE2 HTML documentation. It was generated
12automatically from the original man page. If there is any nonsense in it,
13please consult the man page, in case the conversion went wrong.
14<br>
15<br><b>
16UNICODE AND UTF SUPPORT
17</b><br>
18<P>
19PCRE2 is normally built with Unicode support, though if you do not need it, you
20can build it without, in which case the library will be smaller. With Unicode
21support, PCRE2 has knowledge of Unicode character properties and can process
22strings of text in UTF-8, UTF-16, and UTF-32 format (depending on the code unit
23width), but this is not the default. Unless specifically requested, PCRE2
24treats each code unit in a string as one character.
25</P>
26<P>
27There are two ways of telling PCRE2 to switch to UTF mode, where characters may
28consist of more than one code unit and the range of values is constrained. The
29program can call
30<a href="pcre2_compile.html"><b>pcre2_compile()</b></a>
31with the PCRE2_UTF option, or the pattern may start with the sequence (*UTF).
32However, the latter facility can be locked out by the PCRE2_NEVER_UTF option.
33That is, the programmer can prevent the supplier of the pattern from switching
34to UTF mode.
35</P>
36<P>
37Note that the PCRE2_MATCH_INVALID_UTF option (see
38<a href="#matchinvalid">below)</a>
39forces PCRE2_UTF to be set.
40</P>
41<P>
42In UTF mode, both the pattern and any subject strings that are matched against
43it are treated as UTF strings instead of strings of individual one-code-unit
44characters. There are also some other changes to the way characters are
45handled, as documented below.
46</P>
47<br><b>
48UNICODE PROPERTY SUPPORT
49</b><br>
50<P>
51When PCRE2 is built with Unicode support, the escape sequences \p{..},
52\P{..}, and \X can be used. This is not dependent on the PCRE2_UTF setting.
53The Unicode properties that can be tested are a subset of those that Perl
54supports. Currently they are limited to the general category properties such as
55Lu for an upper case letter or Nd for a decimal number, the derived properties
56Any and LC (synonym L&), the Unicode script names such as Arabic or Han,
57Bidi_Class, Bidi_Control, and a few binary properties.
58</P>
59<P>
60The full lists are given in the
61<a href="pcre2pattern.html"><b>pcre2pattern</b></a>
62and
63<a href="pcre2syntax.html"><b>pcre2syntax</b></a>
64documentation. In general, only the short names for properties are supported.
65For example, \p{L} matches a letter. Its longer synonym, \p{Letter}, is not
66supported. Furthermore, in Perl, many properties may optionally be prefixed by
67"Is", for compatibility with Perl 5.6. PCRE2 does not support this.
68</P>
69<br><b>
70WIDE CHARACTERS AND UTF MODES
71</b><br>
72<P>
73Code points less than 256 can be specified in patterns by either braced or
74unbraced hexadecimal escape sequences (for example, \x{b3} or \xb3). Larger
75values have to use braced sequences. Unbraced octal code points up to \777 are
76also recognized; larger ones can be coded using \o{...}.
77</P>
78<P>
79The escape sequence \N{U+&#60;hex digits&#62;} is recognized as another way of
80specifying a Unicode character by code point in a UTF mode. It is not allowed
81in non-UTF mode.
82</P>
83<P>
84In UTF mode, repeat quantifiers apply to complete UTF characters, not to
85individual code units.
86</P>
87<P>
88In UTF mode, the dot metacharacter matches one UTF character instead of a
89single code unit.
90</P>
91<P>
92In UTF mode, capture group names are not restricted to ASCII, and may contain
93any Unicode letters and decimal digits, as well as underscore.
94</P>
95<P>
96The escape sequence \C can be used to match a single code unit in UTF mode,
97but its use can lead to some strange effects because it breaks up multi-unit
98characters (see the description of \C in the
99<a href="pcre2pattern.html"><b>pcre2pattern</b></a>
100documentation). For this reason, there is a build-time option that disables
101support for \C completely. There is also a less draconian compile-time option
102for locking out the use of \C when a pattern is compiled.
103</P>
104<P>
105The use of \C is not supported by the alternative matching function
106<b>pcre2_dfa_match()</b> when in UTF-8 or UTF-16 mode, that is, when a character
107may consist of more than one code unit. The use of \C in these modes provokes
108a match-time error. Also, the JIT optimization does not support \C in these
109modes. If JIT optimization is requested for a UTF-8 or UTF-16 pattern that
110contains \C, it will not succeed, and so when <b>pcre2_match()</b> is called,
111the matching will be carried out by the interpretive function.
112</P>
113<P>
114The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly test
115characters of any code value, but, by default, the characters that PCRE2
116recognizes as digits, spaces, or word characters remain the same set as in
117non-UTF mode, all with code points less than 256. This remains true even when
118PCRE2 is built to include Unicode support, because to do otherwise would slow
119down matching in many common cases. Note that this also applies to \b
120and \B, because they are defined in terms of \w and \W. If you want
121to test for a wider sense of, say, "digit", you can use explicit Unicode
122property tests such as \p{Nd}. Alternatively, if you set the PCRE2_UCP option,
123the way that the character escapes work is changed so that Unicode properties
124are used to determine which characters match, though there are some options
125that suppress this for individual escapes. For details see the section on
126<a href="pcre2pattern.html#genericchartypes">generic character types</a>
127in the
128<a href="pcre2pattern.html"><b>pcre2pattern</b></a>
129documentation.
130</P>
131<P>
132Like the escapes, characters that match the POSIX named character classes are
133all low-valued characters unless the PCRE2_UCP option is set, but there is an
134option to override this.
135</P>
136<P>
137In contrast to the character escapes and character classes, the special
138horizontal and vertical white space escapes (\h, \H, \v, and \V) do match
139all the appropriate Unicode characters, whether or not PCRE2_UCP is set.
140</P>
141<br><b>
142UNICODE CASE-EQUIVALENCE
143</b><br>
144<P>
145If either PCRE2_UTF or PCRE2_UCP is set, upper/lower case processing makes use
146of Unicode properties except for characters whose code points are less than 128
147and that have at most two case-equivalent values. For these, a direct table
148lookup is used for speed. A few Unicode characters such as Greek sigma have
149more than two code points that are case-equivalent, and these are treated
150specially. Setting PCRE2_UCP without PCRE2_UTF allows Unicode-style case
151processing for non-UTF character encodings such as UCS-2.
152</P>
153<P>
154There are two ASCII characters (S and K) that, in addition to their ASCII lower
155case equivalents, have a non-ASCII one as well (long S and Kelvin sign).
156Recognition of these non-ASCII characters as case-equivalent to their ASCII
157counterparts can be disabled by setting the PCRE2_EXTRA_CASELESS_RESTRICT
158option. When this is set, all characters in a case equivalence must either be
159ASCII or non-ASCII; there can be no mixing.
160<a name="scriptruns"></a></P>
161<br><b>
162SCRIPT RUNS
163</b><br>
164<P>
165The pattern constructs (*script_run:...) and (*atomic_script_run:...), with
166synonyms (*sr:...) and (*asr:...), verify that the string matched within the
167parentheses is a script run. In concept, a script run is a sequence of
168characters that are all from the same Unicode script. However, because some
169scripts are commonly used together, and because some diacritical and other
170marks are used with multiple scripts, it is not that simple.
171</P>
172<P>
173Every Unicode character has a Script property, mostly with a value
174corresponding to the name of a script, such as Latin, Greek, or Cyrillic. There
175are also three special values:
176</P>
177<P>
178"Unknown" is used for code points that have not been assigned, and also for the
179surrogate code points. In the PCRE2 32-bit library, characters whose code
180points are greater than the Unicode maximum (U+10FFFF), which are accessible
181only in non-UTF mode, are assigned the Unknown script.
182</P>
183<P>
184"Common" is used for characters that are used with many scripts. These include
185punctuation, emoji, mathematical, musical, and currency symbols, and the ASCII
186digits 0 to 9.
187</P>
188<P>
189"Inherited" is used for characters such as diacritical marks that modify a
190previous character. These are considered to take on the script of the character
191that they modify.
192</P>
193<P>
194Some Inherited characters are used with many scripts, but many of them are only
195normally used with a small number of scripts. For example, U+102E0 (Coptic
196Epact thousands mark) is used only with Arabic and Coptic. In order to make it
197possible to check this, a Unicode property called Script Extension exists. Its
198value is a list of scripts that apply to the character. For the majority of
199characters, the list contains just one script, the same one as the Script
200property. However, for characters such as U+102E0 more than one Script is
201listed. There are also some Common characters that have a single, non-Common
202script in their Script Extension list.
203</P>
204<P>
205The next section describes the basic rules for deciding whether a given string
206of characters is a script run. Note, however, that there are some special cases
207involving the Chinese Han script, and an additional constraint for decimal
208digits. These are covered in subsequent sections.
209</P>
210<br><b>
211Basic script run rules
212</b><br>
213<P>
214A string that is less than two characters long is a script run. This is the
215only case in which an Unknown character can be part of a script run. Longer
216strings are checked using only the Script Extensions property, not the basic
217Script property.
218</P>
219<P>
220If a character's Script Extension property is the single value "Inherited", it
221is always accepted as part of a script run. This is also true for the property
222"Common", subject to the checking of decimal digits described below. All the
223remaining characters in a script run must have at least one script in common in
224their Script Extension lists. In set-theoretic terminology, the intersection of
225all the sets of scripts must not be empty.
226</P>
227<P>
228A simple example is an Internet name such as "google.com". The letters are all
229in the Latin script, and the dot is Common, so this string is a script run.
230However, the Cyrillic letter "o" looks exactly the same as the Latin "o"; a
231string that looks the same, but with Cyrillic "o"s is not a script run.
232</P>
233<P>
234More interesting examples involve characters with more than one script in their
235Script Extension. Consider the following characters:
236<pre>
237  U+060C  Arabic comma
238  U+06D4  Arabic full stop
239</pre>
240The first has the Script Extension list Arabic, Hanifi Rohingya, Syriac, and
241Thaana; the second has just Arabic and Hanifi Rohingya. Both of them could
242appear in script runs of either Arabic or Hanifi Rohingya. The first could also
243appear in Syriac or Thaana script runs, but the second could not.
244</P>
245<br><b>
246The Chinese Han script
247</b><br>
248<P>
249The Chinese Han script is commonly used in conjunction with other scripts for
250writing certain languages. Japanese uses the Hiragana and Katakana scripts
251together with Han; Korean uses Hangul and Han; Taiwanese Mandarin uses Bopomofo
252and Han. These three combinations are treated as special cases when checking
253script runs and are, in effect, "virtual scripts". Thus, a script run may
254contain a mixture of Hiragana, Katakana, and Han, or a mixture of Hangul and
255Han, or a mixture of Bopomofo and Han, but not, for example, a mixture of
256Hangul and Bopomofo and Han. PCRE2 (like Perl) follows Unicode's Technical
257Standard 39 ("Unicode Security Mechanisms", http://unicode.org/reports/tr39/)
258in allowing such mixtures.
259</P>
260<br><b>
261Decimal digits
262</b><br>
263<P>
264Unicode contains many sets of 10 decimal digits in different scripts, and some
265scripts (including the Common script) contain more than one set. Some of these
266decimal digits them are visually indistinguishable from the common ASCII
267digits. In addition to the script checking described above, if a script run
268contains any decimal digits, they must all come from the same set of 10
269adjacent characters.
270</P>
271<br><b>
272VALIDITY OF UTF STRINGS
273</b><br>
274<P>
275When the PCRE2_UTF option is set, the strings passed as patterns and subjects
276are (by default) checked for validity on entry to the relevant functions. If an
277invalid UTF string is passed, a negative error code is returned. The code unit
278offset to the offending character can be extracted from the match data block by
279calling <b>pcre2_get_startchar()</b>, which is used for this purpose after a UTF
280error.
281</P>
282<P>
283In some situations, you may already know that your strings are valid, and
284therefore want to skip these checks in order to improve performance, for
285example in the case of a long subject string that is being scanned repeatedly.
286If you set the PCRE2_NO_UTF_CHECK option at compile time or at match time,
287PCRE2 assumes that the pattern or subject it is given (respectively) contains
288only valid UTF code unit sequences.
289</P>
290<P>
291If you pass an invalid UTF string when PCRE2_NO_UTF_CHECK is set, the result
292is undefined and your program may crash or loop indefinitely or give incorrect
293results. There is, however, one mode of matching that can handle invalid UTF
294subject strings. This is enabled by passing PCRE2_MATCH_INVALID_UTF to
295<b>pcre2_compile()</b> and is discussed below in the next section. The rest of
296this section covers the case when PCRE2_MATCH_INVALID_UTF is not set.
297</P>
298<P>
299Passing PCRE2_NO_UTF_CHECK to <b>pcre2_compile()</b> just disables the UTF check
300for the pattern; it does not also apply to subject strings. If you want to
301disable the check for a subject string you must pass this same option to
302<b>pcre2_match()</b> or <b>pcre2_dfa_match()</b>.
303</P>
304<P>
305UTF-16 and UTF-32 strings can indicate their endianness by special code knows
306as a byte-order mark (BOM). The PCRE2 functions do not handle this, expecting
307strings to be in host byte order.
308</P>
309<P>
310Unless PCRE2_NO_UTF_CHECK is set, a UTF string is checked before any other
311processing takes place. In the case of <b>pcre2_match()</b> and
312<b>pcre2_dfa_match()</b> calls with a non-zero starting offset, the check is
313applied only to that part of the subject that could be inspected during
314matching, and there is a check that the starting offset points to the first
315code unit of a character or to the end of the subject. If there are no
316lookbehind assertions in the pattern, the check starts at the starting offset.
317Otherwise, it starts at the length of the longest lookbehind before the
318starting offset, or at the start of the subject if there are not that many
319characters before the starting offset. Note that the sequences \b and \B are
320one-character lookbehinds.
321</P>
322<P>
323In addition to checking the format of the string, there is a check to ensure
324that all code points lie in the range U+0 to U+10FFFF, excluding the surrogate
325area. The so-called "non-character" code points are not excluded because
326Unicode corrigendum #9 makes it clear that they should not be.
327</P>
328<P>
329Characters in the "Surrogate Area" of Unicode are reserved for use by UTF-16,
330where they are used in pairs to encode code points with values greater than
3310xFFFF. The code points that are encoded by UTF-16 pairs are available
332independently in the UTF-8 and UTF-32 encodings. (In other words, the whole
333surrogate thing is a fudge for UTF-16 which unfortunately messes up UTF-8 and
334UTF-32.)
335</P>
336<P>
337Setting PCRE2_NO_UTF_CHECK at compile time does not disable the error that is
338given if an escape sequence for an invalid Unicode code point is encountered in
339the pattern. If you want to allow escape sequences such as \x{d800} (a
340surrogate code point) you can set the PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES extra
341option. However, this is possible only in UTF-8 and UTF-32 modes, because these
342values are not representable in UTF-16.
343<a name="utf8strings"></a></P>
344<br><b>
345Errors in UTF-8 strings
346</b><br>
347<P>
348The following negative error codes are given for invalid UTF-8 strings:
349<pre>
350  PCRE2_ERROR_UTF8_ERR1
351  PCRE2_ERROR_UTF8_ERR2
352  PCRE2_ERROR_UTF8_ERR3
353  PCRE2_ERROR_UTF8_ERR4
354  PCRE2_ERROR_UTF8_ERR5
355</pre>
356The string ends with a truncated UTF-8 character; the code specifies how many
357bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8 characters to be
358no longer than 4 bytes, the encoding scheme (originally defined by RFC 2279)
359allows for up to 6 bytes, and this is checked first; hence the possibility of
3604 or 5 missing bytes.
361<pre>
362  PCRE2_ERROR_UTF8_ERR6
363  PCRE2_ERROR_UTF8_ERR7
364  PCRE2_ERROR_UTF8_ERR8
365  PCRE2_ERROR_UTF8_ERR9
366  PCRE2_ERROR_UTF8_ERR10
367</pre>
368The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of the
369character do not have the binary value 0b10 (that is, either the most
370significant bit is 0, or the next bit is 1).
371<pre>
372  PCRE2_ERROR_UTF8_ERR11
373  PCRE2_ERROR_UTF8_ERR12
374</pre>
375A character that is valid by the RFC 2279 rules is either 5 or 6 bytes long;
376these code points are excluded by RFC 3629.
377<pre>
378  PCRE2_ERROR_UTF8_ERR13
379</pre>
380A 4-byte character has a value greater than 0x10ffff; these code points are
381excluded by RFC 3629.
382<pre>
383  PCRE2_ERROR_UTF8_ERR14
384</pre>
385A 3-byte character has a value in the range 0xd800 to 0xdfff; this range of
386code points are reserved by RFC 3629 for use with UTF-16, and so are excluded
387from UTF-8.
388<pre>
389  PCRE2_ERROR_UTF8_ERR15
390  PCRE2_ERROR_UTF8_ERR16
391  PCRE2_ERROR_UTF8_ERR17
392  PCRE2_ERROR_UTF8_ERR18
393  PCRE2_ERROR_UTF8_ERR19
394</pre>
395A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes for a
396value that can be represented by fewer bytes, which is invalid. For example,
397the two bytes 0xc0, 0xae give the value 0x2e, whose correct coding uses just
398one byte.
399<pre>
400  PCRE2_ERROR_UTF8_ERR20
401</pre>
402The two most significant bits of the first byte of a character have the binary
403value 0b10 (that is, the most significant bit is 1 and the second is 0). Such a
404byte can only validly occur as the second or subsequent byte of a multi-byte
405character.
406<pre>
407  PCRE2_ERROR_UTF8_ERR21
408</pre>
409The first byte of a character has the value 0xfe or 0xff. These values can
410never occur in a valid UTF-8 string.
411<a name="utf16strings"></a></P>
412<br><b>
413Errors in UTF-16 strings
414</b><br>
415<P>
416The following negative error codes are given for invalid UTF-16 strings:
417<pre>
418  PCRE2_ERROR_UTF16_ERR1  Missing low surrogate at end of string
419  PCRE2_ERROR_UTF16_ERR2  Invalid low surrogate follows high surrogate
420  PCRE2_ERROR_UTF16_ERR3  Isolated low surrogate
421
422<a name="utf32strings"></a></PRE>
423</P>
424<br><b>
425Errors in UTF-32 strings
426</b><br>
427<P>
428The following negative error codes are given for invalid UTF-32 strings:
429<pre>
430  PCRE2_ERROR_UTF32_ERR1  Surrogate character (0xd800 to 0xdfff)
431  PCRE2_ERROR_UTF32_ERR2  Code point is greater than 0x10ffff
432
433<a name="matchinvalid"></a></PRE>
434</P>
435<br><b>
436MATCHING IN INVALID UTF STRINGS
437</b><br>
438<P>
439You can run pattern matches on subject strings that may contain invalid UTF
440sequences if you call <b>pcre2_compile()</b> with the PCRE2_MATCH_INVALID_UTF
441option. This is supported by <b>pcre2_match()</b>, including JIT matching, but
442not by <b>pcre2_dfa_match()</b>. When PCRE2_MATCH_INVALID_UTF is set, it forces
443PCRE2_UTF to be set as well. Note, however, that the pattern itself must be a
444valid UTF string.
445</P>
446<P>
447If you do not set PCRE2_MATCH_INVALID_UTF when calling <b>pcre2_compile</b>, and
448you are not certain that your subject strings are valid UTF sequences, you
449should not make use of the JIT "fast path" function <b>pcre2_jit_match()</b>
450because it bypasses sanity checks, including the one for UTF validity. An
451invalid string may cause undefined behaviour, including looping, crashing, or
452giving the wrong answer.
453</P>
454<P>
455Setting PCRE2_MATCH_INVALID_UTF does not affect what <b>pcre2_compile()</b>
456generates, but if <b>pcre2_jit_compile()</b> is subsequently called, it does
457generate different code. If JIT is not used, the option affects the behaviour
458of the interpretive code in <b>pcre2_match()</b>. When PCRE2_MATCH_INVALID_UTF
459is set at compile time, PCRE2_NO_UTF_CHECK is ignored at match time.
460</P>
461<P>
462In this mode, an invalid code unit sequence in the subject never matches any
463pattern item. It does not match dot, it does not match \p{Any}, it does not
464even match negative items such as [^X]. A lookbehind assertion fails if it
465encounters an invalid sequence while moving the current point backwards. In
466other words, an invalid UTF code unit sequence acts as a barrier which no match
467can cross.
468</P>
469<P>
470You can also think of this as the subject being split up into fragments of
471valid UTF, delimited internally by invalid code unit sequences. The pattern is
472matched fragment by fragment. The result of a successful match, however, is
473given as code unit offsets in the entire subject string in the usual way. There
474are a few points to consider:
475</P>
476<P>
477The internal boundaries are not interpreted as the beginnings or ends of lines
478and so do not match circumflex or dollar characters in the pattern.
479</P>
480<P>
481If <b>pcre2_match()</b> is called with an offset that points to an invalid
482UTF-sequence, that sequence is skipped, and the match starts at the next valid
483UTF character, or the end of the subject.
484</P>
485<P>
486At internal fragment boundaries, \b and \B behave in the same way as at the
487beginning and end of the subject. For example, a sequence such as \bWORD\b
488would match an instance of WORD that is surrounded by invalid UTF code units.
489</P>
490<P>
491Using PCRE2_MATCH_INVALID_UTF, an application can run matches on arbitrary
492data, knowing that any matched strings that are returned are valid UTF. This
493can be useful when searching for UTF text in executable or other binary files.
494</P>
495<P>
496Note, however, that the 16-bit and 32-bit PCRE2 libraries process strings as
497sequences of uint16_t or uint32_t code points. They cannot find valid UTF
498sequences within an arbitrary string of bytes unless such sequences are
499suitably aligned.
500</P>
501<br><b>
502AUTHOR
503</b><br>
504<P>
505Philip Hazel
506<br>
507Retired from University Computing Service
508<br>
509Cambridge, England.
510<br>
511</P>
512<br><b>
513REVISION
514</b><br>
515<P>
516Last updated: 12 October 2023
517<br>
518Copyright &copy; 1997-2023 University of Cambridge.
519<br>
520<p>
521Return to the <a href="index.html">PCRE2 index page</a>.
522</p>
523