xref: /aosp_15_r20/external/pcre/doc/html/pcre2callout.html (revision 22dc650d8ae982c6770746019a6f94af92b0f024)
1*22dc650dSSadaf Ebrahimi<html>
2*22dc650dSSadaf Ebrahimi<head>
3*22dc650dSSadaf Ebrahimi<title>pcre2callout specification</title>
4*22dc650dSSadaf Ebrahimi</head>
5*22dc650dSSadaf Ebrahimi<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6*22dc650dSSadaf Ebrahimi<h1>pcre2callout man page</h1>
7*22dc650dSSadaf Ebrahimi<p>
8*22dc650dSSadaf EbrahimiReturn to the <a href="index.html">PCRE2 index page</a>.
9*22dc650dSSadaf Ebrahimi</p>
10*22dc650dSSadaf Ebrahimi<p>
11*22dc650dSSadaf EbrahimiThis page is part of the PCRE2 HTML documentation. It was generated
12*22dc650dSSadaf Ebrahimiautomatically from the original man page. If there is any nonsense in it,
13*22dc650dSSadaf Ebrahimiplease consult the man page, in case the conversion went wrong.
14*22dc650dSSadaf Ebrahimi<br>
15*22dc650dSSadaf Ebrahimi<ul>
16*22dc650dSSadaf Ebrahimi<li><a name="TOC1" href="#SEC1">SYNOPSIS</a>
17*22dc650dSSadaf Ebrahimi<li><a name="TOC2" href="#SEC2">DESCRIPTION</a>
18*22dc650dSSadaf Ebrahimi<li><a name="TOC3" href="#SEC3">MISSING CALLOUTS</a>
19*22dc650dSSadaf Ebrahimi<li><a name="TOC4" href="#SEC4">THE CALLOUT INTERFACE</a>
20*22dc650dSSadaf Ebrahimi<li><a name="TOC5" href="#SEC5">RETURN VALUES FROM CALLOUTS</a>
21*22dc650dSSadaf Ebrahimi<li><a name="TOC6" href="#SEC6">CALLOUT ENUMERATION</a>
22*22dc650dSSadaf Ebrahimi<li><a name="TOC7" href="#SEC7">AUTHOR</a>
23*22dc650dSSadaf Ebrahimi<li><a name="TOC8" href="#SEC8">REVISION</a>
24*22dc650dSSadaf Ebrahimi</ul>
25*22dc650dSSadaf Ebrahimi<br><a name="SEC1" href="#TOC1">SYNOPSIS</a><br>
26*22dc650dSSadaf Ebrahimi<P>
27*22dc650dSSadaf Ebrahimi<b>#include &#60;pcre2.h&#62;</b>
28*22dc650dSSadaf Ebrahimi</P>
29*22dc650dSSadaf Ebrahimi<P>
30*22dc650dSSadaf Ebrahimi<b>int (*pcre2_callout)(pcre2_callout_block *, void *);</b>
31*22dc650dSSadaf Ebrahimi<br>
32*22dc650dSSadaf Ebrahimi<br>
33*22dc650dSSadaf Ebrahimi<b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
34*22dc650dSSadaf Ebrahimi<b>  int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
35*22dc650dSSadaf Ebrahimi<b>  void *<i>user_data</i>);</b>
36*22dc650dSSadaf Ebrahimi</P>
37*22dc650dSSadaf Ebrahimi<br><a name="SEC2" href="#TOC1">DESCRIPTION</a><br>
38*22dc650dSSadaf Ebrahimi<P>
39*22dc650dSSadaf EbrahimiPCRE2 provides a feature called "callout", which is a means of temporarily
40*22dc650dSSadaf Ebrahimipassing control to the caller of PCRE2 in the middle of pattern matching. The
41*22dc650dSSadaf Ebrahimicaller of PCRE2 provides an external function by putting its entry point in
42*22dc650dSSadaf Ebrahimia match context (see <b>pcre2_set_callout()</b> in the
43*22dc650dSSadaf Ebrahimi<a href="pcre2api.html"><b>pcre2api</b></a>
44*22dc650dSSadaf Ebrahimidocumentation).
45*22dc650dSSadaf Ebrahimi</P>
46*22dc650dSSadaf Ebrahimi<P>
47*22dc650dSSadaf EbrahimiWhen using the <b>pcre2_substitute()</b> function, an additional callout feature
48*22dc650dSSadaf Ebrahimiis available. This does a callout after each change to the subject string and
49*22dc650dSSadaf Ebrahimiis described in the
50*22dc650dSSadaf Ebrahimi<a href="pcre2api.html"><b>pcre2api</b></a>
51*22dc650dSSadaf Ebrahimidocumentation; the rest of this document is concerned with callouts during
52*22dc650dSSadaf Ebrahimipattern matching.
53*22dc650dSSadaf Ebrahimi</P>
54*22dc650dSSadaf Ebrahimi<P>
55*22dc650dSSadaf EbrahimiWithin a regular expression, (?C&#60;arg&#62;) indicates a point at which the external
56*22dc650dSSadaf Ebrahimifunction is to be called. Different callout points can be identified by putting
57*22dc650dSSadaf Ebrahimia number less than 256 after the letter C. The default value is zero.
58*22dc650dSSadaf EbrahimiAlternatively, the argument may be a delimited string. The starting delimiter
59*22dc650dSSadaf Ebrahimimust be one of ` ' " ^ % # $ { and the ending delimiter is the same as the
60*22dc650dSSadaf Ebrahimistart, except for {, where the ending delimiter is }. If the ending delimiter
61*22dc650dSSadaf Ebrahimiis needed within the string, it must be doubled. For example, this pattern has
62*22dc650dSSadaf Ebrahimitwo callout points:
63*22dc650dSSadaf Ebrahimi<pre>
64*22dc650dSSadaf Ebrahimi  (?C1)abc(?C"some ""arbitrary"" text")def
65*22dc650dSSadaf Ebrahimi</pre>
66*22dc650dSSadaf EbrahimiIf the PCRE2_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE2
67*22dc650dSSadaf Ebrahimiautomatically inserts callouts, all with number 255, before each item in the
68*22dc650dSSadaf Ebrahimipattern except for immediately before or after an explicit callout. For
69*22dc650dSSadaf Ebrahimiexample, if PCRE2_AUTO_CALLOUT is used with the pattern
70*22dc650dSSadaf Ebrahimi<pre>
71*22dc650dSSadaf Ebrahimi  A(?C3)B
72*22dc650dSSadaf Ebrahimi</pre>
73*22dc650dSSadaf Ebrahimiit is processed as if it were
74*22dc650dSSadaf Ebrahimi<pre>
75*22dc650dSSadaf Ebrahimi  (?C255)A(?C3)B(?C255)
76*22dc650dSSadaf Ebrahimi</pre>
77*22dc650dSSadaf EbrahimiHere is a more complicated example:
78*22dc650dSSadaf Ebrahimi<pre>
79*22dc650dSSadaf Ebrahimi  A(\d{2}|--)
80*22dc650dSSadaf Ebrahimi</pre>
81*22dc650dSSadaf EbrahimiWith PCRE2_AUTO_CALLOUT, this pattern is processed as if it were
82*22dc650dSSadaf Ebrahimi<pre>
83*22dc650dSSadaf Ebrahimi  (?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
84*22dc650dSSadaf Ebrahimi</pre>
85*22dc650dSSadaf EbrahimiNotice that there is a callout before and after each parenthesis and
86*22dc650dSSadaf Ebrahimialternation bar. If the pattern contains a conditional group whose condition is
87*22dc650dSSadaf Ebrahimian assertion, an automatic callout is inserted immediately before the
88*22dc650dSSadaf Ebrahimicondition. Such a callout may also be inserted explicitly, for example:
89*22dc650dSSadaf Ebrahimi<pre>
90*22dc650dSSadaf Ebrahimi  (?(?C9)(?=a)ab|de)  (?(?C%text%)(?!=d)ab|de)
91*22dc650dSSadaf Ebrahimi</pre>
92*22dc650dSSadaf EbrahimiThis applies only to assertion conditions (because they are themselves
93*22dc650dSSadaf Ebrahimiindependent groups).
94*22dc650dSSadaf Ebrahimi</P>
95*22dc650dSSadaf Ebrahimi<P>
96*22dc650dSSadaf EbrahimiCallouts can be useful for tracking the progress of pattern matching. The
97*22dc650dSSadaf Ebrahimi<a href="pcre2test.html"><b>pcre2test</b></a>
98*22dc650dSSadaf Ebrahimiprogram has a pattern qualifier (/auto_callout) that sets automatic callouts.
99*22dc650dSSadaf EbrahimiWhen any callouts are present, the output from <b>pcre2test</b> indicates how
100*22dc650dSSadaf Ebrahimithe pattern is being matched. This is useful information when you are trying to
101*22dc650dSSadaf Ebrahimioptimize the performance of a particular pattern.
102*22dc650dSSadaf Ebrahimi</P>
103*22dc650dSSadaf Ebrahimi<br><a name="SEC3" href="#TOC1">MISSING CALLOUTS</a><br>
104*22dc650dSSadaf Ebrahimi<P>
105*22dc650dSSadaf EbrahimiYou should be aware that, because of optimizations in the way PCRE2 compiles
106*22dc650dSSadaf Ebrahimiand matches patterns, callouts sometimes do not happen exactly as you might
107*22dc650dSSadaf Ebrahimiexpect.
108*22dc650dSSadaf Ebrahimi</P>
109*22dc650dSSadaf Ebrahimi<br><b>
110*22dc650dSSadaf EbrahimiAuto-possessification
111*22dc650dSSadaf Ebrahimi</b><br>
112*22dc650dSSadaf Ebrahimi<P>
113*22dc650dSSadaf EbrahimiAt compile time, PCRE2 "auto-possessifies" repeated items when it knows that
114*22dc650dSSadaf Ebrahimiwhat follows cannot be part of the repeat. For example, a+[bc] is compiled as
115*22dc650dSSadaf Ebrahimiif it were a++[bc]. The <b>pcre2test</b> output when this pattern is compiled
116*22dc650dSSadaf Ebrahimiwith PCRE2_ANCHORED and PCRE2_AUTO_CALLOUT and then applied to the string
117*22dc650dSSadaf Ebrahimi"aaaa" is:
118*22dc650dSSadaf Ebrahimi<pre>
119*22dc650dSSadaf Ebrahimi  ---&#62;aaaa
120*22dc650dSSadaf Ebrahimi   +0 ^        a+
121*22dc650dSSadaf Ebrahimi   +2 ^   ^    [bc]
122*22dc650dSSadaf Ebrahimi  No match
123*22dc650dSSadaf Ebrahimi</pre>
124*22dc650dSSadaf EbrahimiThis indicates that when matching [bc] fails, there is no backtracking into a+
125*22dc650dSSadaf Ebrahimi(because it is being treated as a++) and therefore the callouts that would be
126*22dc650dSSadaf Ebrahimitaken for the backtracks do not occur. You can disable the auto-possessify
127*22dc650dSSadaf Ebrahimifeature by passing PCRE2_NO_AUTO_POSSESS to <b>pcre2_compile()</b>, or starting
128*22dc650dSSadaf Ebrahimithe pattern with (*NO_AUTO_POSSESS). In this case, the output changes to this:
129*22dc650dSSadaf Ebrahimi<pre>
130*22dc650dSSadaf Ebrahimi  ---&#62;aaaa
131*22dc650dSSadaf Ebrahimi   +0 ^        a+
132*22dc650dSSadaf Ebrahimi   +2 ^   ^    [bc]
133*22dc650dSSadaf Ebrahimi   +2 ^  ^     [bc]
134*22dc650dSSadaf Ebrahimi   +2 ^ ^      [bc]
135*22dc650dSSadaf Ebrahimi   +2 ^^       [bc]
136*22dc650dSSadaf Ebrahimi  No match
137*22dc650dSSadaf Ebrahimi</pre>
138*22dc650dSSadaf EbrahimiThis time, when matching [bc] fails, the matcher backtracks into a+ and tries
139*22dc650dSSadaf Ebrahimiagain, repeatedly, until a+ itself fails.
140*22dc650dSSadaf Ebrahimi</P>
141*22dc650dSSadaf Ebrahimi<br><b>
142*22dc650dSSadaf EbrahimiAutomatic .* anchoring
143*22dc650dSSadaf Ebrahimi</b><br>
144*22dc650dSSadaf Ebrahimi<P>
145*22dc650dSSadaf EbrahimiBy default, an optimization is applied when .* is the first significant item in
146*22dc650dSSadaf Ebrahimia pattern. If PCRE2_DOTALL is set, so that the dot can match any character, the
147*22dc650dSSadaf Ebrahimipattern is automatically anchored. If PCRE2_DOTALL is not set, a match can
148*22dc650dSSadaf Ebrahimistart only after an internal newline or at the beginning of the subject, and
149*22dc650dSSadaf Ebrahimi<b>pcre2_compile()</b> remembers this. If a pattern has more than one top-level
150*22dc650dSSadaf Ebrahimibranch, automatic anchoring occurs if all branches are anchorable.
151*22dc650dSSadaf Ebrahimi</P>
152*22dc650dSSadaf Ebrahimi<P>
153*22dc650dSSadaf EbrahimiThis optimization is disabled, however, if .* is in an atomic group or if there
154*22dc650dSSadaf Ebrahimiis a backreference to the capture group in which it appears. It is also
155*22dc650dSSadaf Ebrahimidisabled if the pattern contains (*PRUNE) or (*SKIP). However, the presence of
156*22dc650dSSadaf Ebrahimicallouts does not affect it.
157*22dc650dSSadaf Ebrahimi</P>
158*22dc650dSSadaf Ebrahimi<P>
159*22dc650dSSadaf EbrahimiFor example, if the pattern .*\d is compiled with PCRE2_AUTO_CALLOUT and
160*22dc650dSSadaf Ebrahimiapplied to the string "aa", the <b>pcre2test</b> output is:
161*22dc650dSSadaf Ebrahimi<pre>
162*22dc650dSSadaf Ebrahimi  ---&#62;aa
163*22dc650dSSadaf Ebrahimi   +0 ^      .*
164*22dc650dSSadaf Ebrahimi   +2 ^ ^    \d
165*22dc650dSSadaf Ebrahimi   +2 ^^     \d
166*22dc650dSSadaf Ebrahimi   +2 ^      \d
167*22dc650dSSadaf Ebrahimi  No match
168*22dc650dSSadaf Ebrahimi</pre>
169*22dc650dSSadaf EbrahimiThis shows that all match attempts start at the beginning of the subject. In
170*22dc650dSSadaf Ebrahimiother words, the pattern is anchored. You can disable this optimization by
171*22dc650dSSadaf Ebrahimipassing PCRE2_NO_DOTSTAR_ANCHOR to <b>pcre2_compile()</b>, or starting the
172*22dc650dSSadaf Ebrahimipattern with (*NO_DOTSTAR_ANCHOR). In this case, the output changes to:
173*22dc650dSSadaf Ebrahimi<pre>
174*22dc650dSSadaf Ebrahimi  ---&#62;aa
175*22dc650dSSadaf Ebrahimi   +0 ^      .*
176*22dc650dSSadaf Ebrahimi   +2 ^ ^    \d
177*22dc650dSSadaf Ebrahimi   +2 ^^     \d
178*22dc650dSSadaf Ebrahimi   +2 ^      \d
179*22dc650dSSadaf Ebrahimi   +0  ^     .*
180*22dc650dSSadaf Ebrahimi   +2  ^^    \d
181*22dc650dSSadaf Ebrahimi   +2  ^     \d
182*22dc650dSSadaf Ebrahimi  No match
183*22dc650dSSadaf Ebrahimi</pre>
184*22dc650dSSadaf EbrahimiThis shows more match attempts, starting at the second subject character.
185*22dc650dSSadaf EbrahimiAnother optimization, described in the next section, means that there is no
186*22dc650dSSadaf Ebrahimisubsequent attempt to match with an empty subject.
187*22dc650dSSadaf Ebrahimi</P>
188*22dc650dSSadaf Ebrahimi<br><b>
189*22dc650dSSadaf EbrahimiOther optimizations
190*22dc650dSSadaf Ebrahimi</b><br>
191*22dc650dSSadaf Ebrahimi<P>
192*22dc650dSSadaf EbrahimiOther optimizations that provide fast "no match" results also affect callouts.
193*22dc650dSSadaf EbrahimiFor example, if the pattern is
194*22dc650dSSadaf Ebrahimi<pre>
195*22dc650dSSadaf Ebrahimi  ab(?C4)cd
196*22dc650dSSadaf Ebrahimi</pre>
197*22dc650dSSadaf EbrahimiPCRE2 knows that any matching string must contain the letter "d". If the
198*22dc650dSSadaf Ebrahimisubject string is "abyz", the lack of "d" means that matching doesn't ever
199*22dc650dSSadaf Ebrahimistart, and the callout is never reached. However, with "abyd", though the
200*22dc650dSSadaf Ebrahimiresult is still no match, the callout is obeyed.
201*22dc650dSSadaf Ebrahimi</P>
202*22dc650dSSadaf Ebrahimi<P>
203*22dc650dSSadaf EbrahimiFor most patterns PCRE2 also knows the minimum length of a matching string, and
204*22dc650dSSadaf Ebrahimiwill immediately give a "no match" return without actually running a match if
205*22dc650dSSadaf Ebrahimithe subject is not long enough, or, for unanchored patterns, if it has been
206*22dc650dSSadaf Ebrahimiscanned far enough.
207*22dc650dSSadaf Ebrahimi</P>
208*22dc650dSSadaf Ebrahimi<P>
209*22dc650dSSadaf EbrahimiYou can disable these optimizations by passing the PCRE2_NO_START_OPTIMIZE
210*22dc650dSSadaf Ebrahimioption to <b>pcre2_compile()</b>, or by starting the pattern with
211*22dc650dSSadaf Ebrahimi(*NO_START_OPT). This slows down the matching process, but does ensure that
212*22dc650dSSadaf Ebrahimicallouts such as the example above are obeyed.
213*22dc650dSSadaf Ebrahimi<a name="calloutinterface"></a></P>
214*22dc650dSSadaf Ebrahimi<br><a name="SEC4" href="#TOC1">THE CALLOUT INTERFACE</a><br>
215*22dc650dSSadaf Ebrahimi<P>
216*22dc650dSSadaf EbrahimiDuring matching, when PCRE2 reaches a callout point, if an external function is
217*22dc650dSSadaf Ebrahimiprovided in the match context, it is called. This applies to both normal,
218*22dc650dSSadaf EbrahimiDFA, and JIT matching. The first argument to the callout function is a pointer
219*22dc650dSSadaf Ebrahimito a <b>pcre2_callout</b> block. The second argument is the void * callout data
220*22dc650dSSadaf Ebrahimithat was supplied when the callout was set up by calling
221*22dc650dSSadaf Ebrahimi<b>pcre2_set_callout()</b> (see the
222*22dc650dSSadaf Ebrahimi<a href="pcre2api.html"><b>pcre2api</b></a>
223*22dc650dSSadaf Ebrahimidocumentation). The callout block structure contains the following fields, not
224*22dc650dSSadaf Ebrahiminecessarily in this order:
225*22dc650dSSadaf Ebrahimi<pre>
226*22dc650dSSadaf Ebrahimi  uint32_t      <i>version</i>;
227*22dc650dSSadaf Ebrahimi  uint32_t      <i>callout_number</i>;
228*22dc650dSSadaf Ebrahimi  uint32_t      <i>capture_top</i>;
229*22dc650dSSadaf Ebrahimi  uint32_t      <i>capture_last</i>;
230*22dc650dSSadaf Ebrahimi  uint32_t      <i>callout_flags</i>;
231*22dc650dSSadaf Ebrahimi  PCRE2_SIZE   *<i>offset_vector</i>;
232*22dc650dSSadaf Ebrahimi  PCRE2_SPTR    <i>mark</i>;
233*22dc650dSSadaf Ebrahimi  PCRE2_SPTR    <i>subject</i>;
234*22dc650dSSadaf Ebrahimi  PCRE2_SIZE    <i>subject_length</i>;
235*22dc650dSSadaf Ebrahimi  PCRE2_SIZE    <i>start_match</i>;
236*22dc650dSSadaf Ebrahimi  PCRE2_SIZE    <i>current_position</i>;
237*22dc650dSSadaf Ebrahimi  PCRE2_SIZE    <i>pattern_position</i>;
238*22dc650dSSadaf Ebrahimi  PCRE2_SIZE    <i>next_item_length</i>;
239*22dc650dSSadaf Ebrahimi  PCRE2_SIZE    <i>callout_string_offset</i>;
240*22dc650dSSadaf Ebrahimi  PCRE2_SIZE    <i>callout_string_length</i>;
241*22dc650dSSadaf Ebrahimi  PCRE2_SPTR    <i>callout_string</i>;
242*22dc650dSSadaf Ebrahimi</pre>
243*22dc650dSSadaf EbrahimiThe <i>version</i> field contains the version number of the block format. The
244*22dc650dSSadaf Ebrahimicurrent version is 2; the three callout string fields were added for version 1,
245*22dc650dSSadaf Ebrahimiand the <i>callout_flags</i> field for version 2. If you are writing an
246*22dc650dSSadaf Ebrahimiapplication that might use an earlier release of PCRE2, you should check the
247*22dc650dSSadaf Ebrahimiversion number before accessing any of these fields. The version number will
248*22dc650dSSadaf Ebrahimiincrease in future if more fields are added, but the intention is never to
249*22dc650dSSadaf Ebrahimiremove any of the existing fields.
250*22dc650dSSadaf Ebrahimi</P>
251*22dc650dSSadaf Ebrahimi<br><b>
252*22dc650dSSadaf EbrahimiFields for numerical callouts
253*22dc650dSSadaf Ebrahimi</b><br>
254*22dc650dSSadaf Ebrahimi<P>
255*22dc650dSSadaf EbrahimiFor a numerical callout, <i>callout_string</i> is NULL, and <i>callout_number</i>
256*22dc650dSSadaf Ebrahimicontains the number of the callout, in the range 0-255. This is the number
257*22dc650dSSadaf Ebrahimithat follows (?C for callouts that part of the pattern; it is 255 for
258*22dc650dSSadaf Ebrahimiautomatically generated callouts.
259*22dc650dSSadaf Ebrahimi</P>
260*22dc650dSSadaf Ebrahimi<br><b>
261*22dc650dSSadaf EbrahimiFields for string callouts
262*22dc650dSSadaf Ebrahimi</b><br>
263*22dc650dSSadaf Ebrahimi<P>
264*22dc650dSSadaf EbrahimiFor callouts with string arguments, <i>callout_number</i> is always zero, and
265*22dc650dSSadaf Ebrahimi<i>callout_string</i> points to the string that is contained within the compiled
266*22dc650dSSadaf Ebrahimipattern. Its length is given by <i>callout_string_length</i>. Duplicated ending
267*22dc650dSSadaf Ebrahimidelimiters that were present in the original pattern string have been turned
268*22dc650dSSadaf Ebrahimiinto single characters, but there is no other processing of the callout string
269*22dc650dSSadaf Ebrahimiargument. An additional code unit containing binary zero is present after the
270*22dc650dSSadaf Ebrahimistring, but is not included in the length. The delimiter that was used to start
271*22dc650dSSadaf Ebrahimithe string is also stored within the pattern, immediately before the string
272*22dc650dSSadaf Ebrahimiitself. You can access this delimiter as <i>callout_string</i>[-1] if you need
273*22dc650dSSadaf Ebrahimiit.
274*22dc650dSSadaf Ebrahimi</P>
275*22dc650dSSadaf Ebrahimi<P>
276*22dc650dSSadaf EbrahimiThe <i>callout_string_offset</i> field is the code unit offset to the start of
277*22dc650dSSadaf Ebrahimithe callout argument string within the original pattern string. This is
278*22dc650dSSadaf Ebrahimiprovided for the benefit of applications such as script languages that might
279*22dc650dSSadaf Ebrahimineed to report errors in the callout string within the pattern.
280*22dc650dSSadaf Ebrahimi</P>
281*22dc650dSSadaf Ebrahimi<br><b>
282*22dc650dSSadaf EbrahimiFields for all callouts
283*22dc650dSSadaf Ebrahimi</b><br>
284*22dc650dSSadaf Ebrahimi<P>
285*22dc650dSSadaf EbrahimiThe remaining fields in the callout block are the same for both kinds of
286*22dc650dSSadaf Ebrahimicallout.
287*22dc650dSSadaf Ebrahimi</P>
288*22dc650dSSadaf Ebrahimi<P>
289*22dc650dSSadaf EbrahimiThe <i>offset_vector</i> field is a pointer to a vector of capturing offsets
290*22dc650dSSadaf Ebrahimi(the "ovector"). You may read the elements in this vector, but you must not
291*22dc650dSSadaf Ebrahimichange any of them.
292*22dc650dSSadaf Ebrahimi</P>
293*22dc650dSSadaf Ebrahimi<P>
294*22dc650dSSadaf EbrahimiFor calls to <b>pcre2_match()</b>, the <i>offset_vector</i> field is not (since
295*22dc650dSSadaf Ebrahimirelease 10.30) a pointer to the actual ovector that was passed to the matching
296*22dc650dSSadaf Ebrahimifunction in the match data block. Instead it points to an internal ovector of a
297*22dc650dSSadaf Ebrahimisize large enough to hold all possible captured substrings in the pattern. Note
298*22dc650dSSadaf Ebrahimithat whenever a recursion or subroutine call within a pattern completes, the
299*22dc650dSSadaf Ebrahimicapturing state is reset to what it was before.
300*22dc650dSSadaf Ebrahimi</P>
301*22dc650dSSadaf Ebrahimi<P>
302*22dc650dSSadaf EbrahimiThe <i>capture_last</i> field contains the number of the most recently captured
303*22dc650dSSadaf Ebrahimisubstring, and the <i>capture_top</i> field contains one more than the number of
304*22dc650dSSadaf Ebrahimithe highest numbered captured substring so far. If no substrings have yet been
305*22dc650dSSadaf Ebrahimicaptured, the value of <i>capture_last</i> is 0 and the value of
306*22dc650dSSadaf Ebrahimi<i>capture_top</i> is 1. The values of these fields do not always differ by one;
307*22dc650dSSadaf Ebrahimifor example, when the callout in the pattern ((a)(b))(?C2) is taken,
308*22dc650dSSadaf Ebrahimi<i>capture_last</i> is 1 but <i>capture_top</i> is 4.
309*22dc650dSSadaf Ebrahimi</P>
310*22dc650dSSadaf Ebrahimi<P>
311*22dc650dSSadaf EbrahimiThe contents of ovector[2] to ovector[&#60;capture_top&#62;*2-1] can be inspected in
312*22dc650dSSadaf Ebrahimiorder to extract substrings that have been matched so far, in the same way as
313*22dc650dSSadaf Ebrahimiextracting substrings after a match has completed. The values in ovector[0] and
314*22dc650dSSadaf Ebrahimiovector[1] are always PCRE2_UNSET because the match is by definition not
315*22dc650dSSadaf Ebrahimicomplete. Substrings that have not been captured but whose numbers are less
316*22dc650dSSadaf Ebrahimithan <i>capture_top</i> also have both of their ovector slots set to
317*22dc650dSSadaf EbrahimiPCRE2_UNSET.
318*22dc650dSSadaf Ebrahimi</P>
319*22dc650dSSadaf Ebrahimi<P>
320*22dc650dSSadaf EbrahimiFor DFA matching, the <i>offset_vector</i> field points to the ovector that was
321*22dc650dSSadaf Ebrahimipassed to the matching function in the match data block for callouts at the top
322*22dc650dSSadaf Ebrahimilevel, but to an internal ovector during the processing of pattern recursions,
323*22dc650dSSadaf Ebrahimilookarounds, and atomic groups. However, these ovectors hold no useful
324*22dc650dSSadaf Ebrahimiinformation because <b>pcre2_dfa_match()</b> does not support substring
325*22dc650dSSadaf Ebrahimicapturing. The value of <i>capture_top</i> is always 1 and the value of
326*22dc650dSSadaf Ebrahimi<i>capture_last</i> is always 0 for DFA matching.
327*22dc650dSSadaf Ebrahimi</P>
328*22dc650dSSadaf Ebrahimi<P>
329*22dc650dSSadaf EbrahimiThe <i>subject</i> and <i>subject_length</i> fields contain copies of the values
330*22dc650dSSadaf Ebrahimithat were passed to the matching function.
331*22dc650dSSadaf Ebrahimi</P>
332*22dc650dSSadaf Ebrahimi<P>
333*22dc650dSSadaf EbrahimiThe <i>start_match</i> field normally contains the offset within the subject at
334*22dc650dSSadaf Ebrahimiwhich the current match attempt started. However, if the escape sequence \K
335*22dc650dSSadaf Ebrahimihas been encountered, this value is changed to reflect the modified starting
336*22dc650dSSadaf Ebrahimipoint. If the pattern is not anchored, the callout function may be called
337*22dc650dSSadaf Ebrahimiseveral times from the same point in the pattern for different starting points
338*22dc650dSSadaf Ebrahimiin the subject.
339*22dc650dSSadaf Ebrahimi</P>
340*22dc650dSSadaf Ebrahimi<P>
341*22dc650dSSadaf EbrahimiThe <i>current_position</i> field contains the offset within the subject of the
342*22dc650dSSadaf Ebrahimicurrent match pointer.
343*22dc650dSSadaf Ebrahimi</P>
344*22dc650dSSadaf Ebrahimi<P>
345*22dc650dSSadaf EbrahimiThe <i>pattern_position</i> field contains the offset in the pattern string to
346*22dc650dSSadaf Ebrahimithe next item to be matched.
347*22dc650dSSadaf Ebrahimi</P>
348*22dc650dSSadaf Ebrahimi<P>
349*22dc650dSSadaf EbrahimiThe <i>next_item_length</i> field contains the length of the next item to be
350*22dc650dSSadaf Ebrahimiprocessed in the pattern string. When the callout is at the end of the pattern,
351*22dc650dSSadaf Ebrahimithe length is zero. When the callout precedes an opening parenthesis, the
352*22dc650dSSadaf Ebrahimilength includes meta characters that follow the parenthesis. For example, in a
353*22dc650dSSadaf Ebrahimicallout before an assertion such as (?=ab) the length is 3. For an alternation
354*22dc650dSSadaf Ebrahimibar or a closing parenthesis, the length is one, unless a closing parenthesis
355*22dc650dSSadaf Ebrahimiis followed by a quantifier, in which case its length is included. (This
356*22dc650dSSadaf Ebrahimichanged in release 10.23. In earlier releases, before an opening parenthesis
357*22dc650dSSadaf Ebrahimithe length was that of the entire group, and before an alternation bar or a
358*22dc650dSSadaf Ebrahimiclosing parenthesis the length was zero.)
359*22dc650dSSadaf Ebrahimi</P>
360*22dc650dSSadaf Ebrahimi<P>
361*22dc650dSSadaf EbrahimiThe <i>pattern_position</i> and <i>next_item_length</i> fields are intended to
362*22dc650dSSadaf Ebrahimihelp in distinguishing between different automatic callouts, which all have the
363*22dc650dSSadaf Ebrahimisame callout number. However, they are set for all callouts, and are used by
364*22dc650dSSadaf Ebrahimi<b>pcre2test</b> to show the next item to be matched when displaying callout
365*22dc650dSSadaf Ebrahimiinformation.
366*22dc650dSSadaf Ebrahimi</P>
367*22dc650dSSadaf Ebrahimi<P>
368*22dc650dSSadaf EbrahimiIn callouts from <b>pcre2_match()</b> the <i>mark</i> field contains a pointer to
369*22dc650dSSadaf Ebrahimithe zero-terminated name of the most recently passed (*MARK), (*PRUNE), or
370*22dc650dSSadaf Ebrahimi(*THEN) item in the match, or NULL if no such items have been passed. Instances
371*22dc650dSSadaf Ebrahimiof (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). In
372*22dc650dSSadaf Ebrahimicallouts from the DFA matching function this field always contains NULL.
373*22dc650dSSadaf Ebrahimi</P>
374*22dc650dSSadaf Ebrahimi<P>
375*22dc650dSSadaf EbrahimiThe <i>callout_flags</i> field is always zero in callouts from
376*22dc650dSSadaf Ebrahimi<b>pcre2_dfa_match()</b> or when JIT is being used. When <b>pcre2_match()</b>
377*22dc650dSSadaf Ebrahimiwithout JIT is used, the following bits may be set:
378*22dc650dSSadaf Ebrahimi<pre>
379*22dc650dSSadaf Ebrahimi  PCRE2_CALLOUT_STARTMATCH
380*22dc650dSSadaf Ebrahimi</pre>
381*22dc650dSSadaf EbrahimiThis is set for the first callout after the start of matching for each new
382*22dc650dSSadaf Ebrahimistarting position in the subject.
383*22dc650dSSadaf Ebrahimi<pre>
384*22dc650dSSadaf Ebrahimi  PCRE2_CALLOUT_BACKTRACK
385*22dc650dSSadaf Ebrahimi</pre>
386*22dc650dSSadaf EbrahimiThis is set if there has been a matching backtrack since the previous callout,
387*22dc650dSSadaf Ebrahimior since the start of matching if this is the first callout from a
388*22dc650dSSadaf Ebrahimi<b>pcre2_match()</b> run.
389*22dc650dSSadaf Ebrahimi</P>
390*22dc650dSSadaf Ebrahimi<P>
391*22dc650dSSadaf EbrahimiBoth bits are set when a backtrack has caused a "bumpalong" to a new starting
392*22dc650dSSadaf Ebrahimiposition in the subject. Output from <b>pcre2test</b> does not indicate the
393*22dc650dSSadaf Ebrahimipresence of these bits unless the <b>callout_extra</b> modifier is set.
394*22dc650dSSadaf Ebrahimi</P>
395*22dc650dSSadaf Ebrahimi<P>
396*22dc650dSSadaf EbrahimiThe information in the <b>callout_flags</b> field is provided so that
397*22dc650dSSadaf Ebrahimiapplications can track and tell their users how matching with backtracking is
398*22dc650dSSadaf Ebrahimidone. This can be useful when trying to optimize patterns, or just to
399*22dc650dSSadaf Ebrahimiunderstand how PCRE2 works. There is no support in <b>pcre2_dfa_match()</b>
400*22dc650dSSadaf Ebrahimibecause there is no backtracking in DFA matching, and there is no support in
401*22dc650dSSadaf EbrahimiJIT because JIT is all about maximimizing matching performance. In both these
402*22dc650dSSadaf Ebrahimicases the <b>callout_flags</b> field is always zero.
403*22dc650dSSadaf Ebrahimi</P>
404*22dc650dSSadaf Ebrahimi<br><a name="SEC5" href="#TOC1">RETURN VALUES FROM CALLOUTS</a><br>
405*22dc650dSSadaf Ebrahimi<P>
406*22dc650dSSadaf EbrahimiThe external callout function returns an integer to PCRE2. If the value is
407*22dc650dSSadaf Ebrahimizero, matching proceeds as normal. If the value is greater than zero, matching
408*22dc650dSSadaf Ebrahimifails at the current point, but the testing of other matching possibilities
409*22dc650dSSadaf Ebrahimigoes ahead, just as if a lookahead assertion had failed. If the value is less
410*22dc650dSSadaf Ebrahimithan zero, the match is abandoned, and the matching function returns the
411*22dc650dSSadaf Ebrahiminegative value.
412*22dc650dSSadaf Ebrahimi</P>
413*22dc650dSSadaf Ebrahimi<P>
414*22dc650dSSadaf EbrahimiNegative values should normally be chosen from the set of PCRE2_ERROR_xxx
415*22dc650dSSadaf Ebrahimivalues. In particular, PCRE2_ERROR_NOMATCH forces a standard "no match"
416*22dc650dSSadaf Ebrahimifailure. The error number PCRE2_ERROR_CALLOUT is reserved for use by callout
417*22dc650dSSadaf Ebrahimifunctions; it will never be used by PCRE2 itself.
418*22dc650dSSadaf Ebrahimi</P>
419*22dc650dSSadaf Ebrahimi<br><a name="SEC6" href="#TOC1">CALLOUT ENUMERATION</a><br>
420*22dc650dSSadaf Ebrahimi<P>
421*22dc650dSSadaf Ebrahimi<b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
422*22dc650dSSadaf Ebrahimi<b>  int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
423*22dc650dSSadaf Ebrahimi<b>  void *<i>user_data</i>);</b>
424*22dc650dSSadaf Ebrahimi<br>
425*22dc650dSSadaf Ebrahimi<br>
426*22dc650dSSadaf EbrahimiA script language that supports the use of string arguments in callouts might
427*22dc650dSSadaf Ebrahimilike to scan all the callouts in a pattern before running the match. This can
428*22dc650dSSadaf Ebrahimibe done by calling <b>pcre2_callout_enumerate()</b>. The first argument is a
429*22dc650dSSadaf Ebrahimipointer to a compiled pattern, the second points to a callback function, and
430*22dc650dSSadaf Ebrahimithe third is arbitrary user data. The callback function is called for every
431*22dc650dSSadaf Ebrahimicallout in the pattern in the order in which they appear. Its first argument is
432*22dc650dSSadaf Ebrahimia pointer to a callout enumeration block, and its second argument is the
433*22dc650dSSadaf Ebrahimi<i>user_data</i> value that was passed to <b>pcre2_callout_enumerate()</b>. The
434*22dc650dSSadaf Ebrahimidata block contains the following fields:
435*22dc650dSSadaf Ebrahimi<pre>
436*22dc650dSSadaf Ebrahimi  <i>version</i>                Block version number
437*22dc650dSSadaf Ebrahimi  <i>pattern_position</i>       Offset to next item in pattern
438*22dc650dSSadaf Ebrahimi  <i>next_item_length</i>       Length of next item in pattern
439*22dc650dSSadaf Ebrahimi  <i>callout_number</i>         Number for numbered callouts
440*22dc650dSSadaf Ebrahimi  <i>callout_string_offset</i>  Offset to string within pattern
441*22dc650dSSadaf Ebrahimi  <i>callout_string_length</i>  Length of callout string
442*22dc650dSSadaf Ebrahimi  <i>callout_string</i>         Points to callout string or is NULL
443*22dc650dSSadaf Ebrahimi</pre>
444*22dc650dSSadaf EbrahimiThe version number is currently 0. It will increase if new fields are ever
445*22dc650dSSadaf Ebrahimiadded to the block. The remaining fields are the same as their namesakes in the
446*22dc650dSSadaf Ebrahimi<b>pcre2_callout</b> block that is used for callouts during matching, as
447*22dc650dSSadaf Ebrahimidescribed
448*22dc650dSSadaf Ebrahimi<a href="#calloutinterface">above.</a>
449*22dc650dSSadaf Ebrahimi</P>
450*22dc650dSSadaf Ebrahimi<P>
451*22dc650dSSadaf EbrahimiNote that the value of <i>pattern_position</i> is unique for each callout.
452*22dc650dSSadaf EbrahimiHowever, if a callout occurs inside a group that is quantified with a non-zero
453*22dc650dSSadaf Ebrahimiminimum or a fixed maximum, the group is replicated inside the compiled
454*22dc650dSSadaf Ebrahimipattern. For example, a pattern such as /(a){2}/ is compiled as if it were
455*22dc650dSSadaf Ebrahimi/(a)(a)/. This means that the callout will be enumerated more than once, but
456*22dc650dSSadaf Ebrahimiwith the same value for <i>pattern_position</i> in each case.
457*22dc650dSSadaf Ebrahimi</P>
458*22dc650dSSadaf Ebrahimi<P>
459*22dc650dSSadaf EbrahimiThe callback function should normally return zero. If it returns a non-zero
460*22dc650dSSadaf Ebrahimivalue, scanning the pattern stops, and that value is returned from
461*22dc650dSSadaf Ebrahimi<b>pcre2_callout_enumerate()</b>.
462*22dc650dSSadaf Ebrahimi</P>
463*22dc650dSSadaf Ebrahimi<br><a name="SEC7" href="#TOC1">AUTHOR</a><br>
464*22dc650dSSadaf Ebrahimi<P>
465*22dc650dSSadaf EbrahimiPhilip Hazel
466*22dc650dSSadaf Ebrahimi<br>
467*22dc650dSSadaf EbrahimiRetired from University Computing Service
468*22dc650dSSadaf Ebrahimi<br>
469*22dc650dSSadaf EbrahimiCambridge, England.
470*22dc650dSSadaf Ebrahimi<br>
471*22dc650dSSadaf Ebrahimi</P>
472*22dc650dSSadaf Ebrahimi<br><a name="SEC8" href="#TOC1">REVISION</a><br>
473*22dc650dSSadaf Ebrahimi<P>
474*22dc650dSSadaf EbrahimiLast updated: 19 January 2024
475*22dc650dSSadaf Ebrahimi<br>
476*22dc650dSSadaf EbrahimiCopyright &copy; 1997-2024 University of Cambridge.
477*22dc650dSSadaf Ebrahimi<br>
478*22dc650dSSadaf Ebrahimi<p>
479*22dc650dSSadaf EbrahimiReturn to the <a href="index.html">PCRE2 index page</a>.
480*22dc650dSSadaf Ebrahimi</p>
481