github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
21 KiB

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