PCRECALLOUT(3)

okay so this is pcrecallout(3) from the Library Functions Manual, and bestie we are about to get into callouts, which are genuinely one of the most underrated little features in PCRE. buckle up πŸ’–


πŸŽ€ NAME πŸŽ€

PCRE β€” Perl-compatible regular expressions. that's the whole name section, short and iconic, no notes πŸ’…


πŸ“œ SYNOPSIS πŸ“œ

#include <pcre.h>

int (*pcre_callout)(pcre_callout_block *);

int (*pcre16_callout)(pcre16_callout_block *);

int (*pcre32_callout)(pcre32_callout_block *);

πŸ’– DESCRIPTION πŸ’–

so PCRE has this feature called a "callout", and what it does is let you temporarily hand control back to whoever called PCRE β€” right in the middle of pattern matching. like, mid-match. it's a whole moment. ✨

The way you set it up: the caller of PCRE provides an external function, and you put its entry point in the global variable pcre_callout (that's pcre16_callout for the 16-bit library, and pcre32_callout for the 32-bit library). By default this variable contains NULL, which disables all calling out. no function, no callouts, simple as that πŸ¦‹

Inside a regular expression, (?C) marks the exact points where the external function gets called. Wanna tell your callout points apart? put a number less than 256 after the letter C. the default value is zero. for example, this pattern has two callout points:

(?C1)abc(?C2)def

Now, if the PCRE_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE automatically inserts callouts β€” all of them with number 255 β€” before each item in the pattern. for example, if PCRE_AUTO_CALLOUT is used with the pattern

A(\d{2}|--)

it gets processed as if it were

(?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)

notice there's a callout before and after each parenthesis and alternation bar. the attention to detail? unmatched. πŸ’…

And if the pattern contains a conditional group whose condition is an assertion, an automatic callout gets inserted immediately before the condition. you can also insert one of those explicitly, like:

(?(?C9)(?=a)ab|de)

This applies only to assertion conditions (because they are themselves independent groups). 🧚

Automatic callouts are lowkey perfect for tracking the progress of pattern matching. the pcretest program has a pattern qualifier (/C) that sets automatic callouts; when it's used, the output shows you how the pattern is being matched. which is genuinely useful info when you're trying to optimize the performance of a particular pattern. obsessed. πŸ”₯


😭 MISSING CALLOUTS 😭

real talk bestie β€” you should be aware that, because of optimizations in the way PCRE compiles and matches patterns, callouts sometimes do not happen exactly as you might expect. not a bug, just the optimizer doing its thing.

At compile time, PCRE "auto-possessifies" repeated items when it knows that what follows cannot be part of the repeat. for example, a+[bc] is compiled as if it were a++[bc]. the pcretest output when this pattern is anchored and then applied with automatic callouts to the string "aaaa" is:

  --->aaaa
   +0 ^        ^
   +1 ^        a+
   +3 ^   ^    [bc]
  No match

This indicates that when matching [bc] fails, there is no backtracking into a+ and therefore the callouts that would be taken for the backtracks do not occur. you can disable the auto-possessify feature by passing PCRE_NO_AUTO_POSSESS to pcre_compile(), or by starting the pattern with (*NO_AUTO_POSSESS). if this is done in pcretest (using the /O qualifier), the output changes to this:

  --->aaaa
   +0 ^        ^
   +1 ^        a+
   +3 ^   ^    [bc]
   +3 ^  ^     [bc]
   +3 ^ ^      [bc]
   +3 ^^       [bc]
  No match

This time, when matching [bc] fails, the matcher backtracks into a+ and tries again, repeatedly, until a+ itself fails. look at all those callouts finally showing up. they were always in there, just waiting 🫢

Other optimizations that provide fast "no match" results also affect callouts. for example, if the pattern is

ab(?C4)cd

PCRE knows that any matching string must contain the letter "d". if the subject string is "abyz", the lack of "d" means that matching doesn't ever start, and the callout is never reached. however, with "abyd", though the result is still no match, the callout is obeyed. πŸ¦‹

If the pattern is studied, PCRE knows the minimum length of a matching string, and will immediately give a "no match" return without actually running a match if the subject is not long enough, or, for unanchored patterns, if it has been scanned far enough.

You can disable these optimizations by passing the PCRE_NO_START_OPTIMIZE option to the matching function, or by starting the pattern with (*NO_START_OPT). this slows down the matching process, but it does ensure that callouts such as the example above are obeyed. trade-offs, babes. ✨


πŸ‘‘ THE CALLOUT INTERFACE πŸ‘‘

During matching, when PCRE reaches a callout point, the external function defined by pcre_callout or pcre[16|32]_callout is called (if it is set). this applies to both normal and DFA matching. the only argument to the callout function is a pointer to a pcre_callout or pcre[16|32]_callout block. these structures contains the following fields:

  int           version;
  int           callout_number;
  int          *offset_vector;
  const char   *subject;           (8-bit version)
  PCRE_SPTR16   subject;           (16-bit version)
  PCRE_SPTR32   subject;           (32-bit version)
  int           subject_length;
  int           start_match;
  int           current_position;
  int           capture_top;
  int           capture_last;
  void         *callout_data;
  int           pattern_position;
  int           next_item_length;
  const unsigned char *mark;       (8-bit version)
  const PCRE_UCHAR16  *mark;       (16-bit version)
  const PCRE_UCHAR32  *mark;       (32-bit version)

okay now let's go field by field, because each one is doing something and you deserve to know what πŸ’…

  • The version field is an integer containing the version number of the block format. the initial version was 0; the current version is 2. the version number will change again in future if additional fields are added, but the intention is never to remove any of the existing fields. they add, they don't subtract. growth mindset fr fr 🌱
  • The callout_number field contains the number of the callout, as compiled into the pattern (that is, the number after ?C for manual callouts, and 255 for automatically generated callouts).
  • The offset_vector field is a pointer to the vector of offsets that was passed by the caller to the matching function. when pcre_exec() or pcre[16|32]_exec() is used, the contents can be inspected, in order to extract substrings that have been matched so far, in the same way as for extracting substrings after a match has completed. for the DFA matching functions, this field is not useful. 🫠
  • The subject and subject_length fields contain copies of the values that were passed to the matching function.
  • The start_match field normally contains the offset within the subject at which the current match attempt started. however, if the escape sequence \K has been encountered, this value is changed to reflect the modified starting point. if the pattern is not anchored, the callout function may be called several times from the same point in the pattern for different starting points in the subject. yes, several times, that's normal, don't panic πŸ¦‹
  • The current_position field contains the offset within the subject of the current match pointer.
  • When the pcre_exec() or pcre[16|32]_exec() is used, the capture_top field contains one more than the number of the highest numbered captured substring so far. if no substrings have been captured, the value of capture_top is one. this is always the case when the DFA functions are used, because they do not support captured substrings.
  • The capture_last field contains the number of the most recently captured substring. however, when a recursion exits, the value reverts to what it was outside the recursion, as do the values of all captured substrings. if no substrings have been captured, the value of capture_last is -1. this is always the case for the DFA matching functions.
  • The callout_data field contains a value that is passed to a matching function specifically so that it can be passed back in callouts. it is passed in the callout_data field of a pcre_extra or pcre[16|32]_extra data structure. if no such data was passed, the value of callout_data in a callout block is NULL. there is a description of the pcre_extra structure in the pcreapi documentation. πŸ‘‘
  • The pattern_position field is present from version 1 of the callout structure. it contains the offset to the next item to be matched in the pattern string.
  • The next_item_length field is present from version 1 of the callout structure. it contains the length of the next item to be matched in the pattern string. when the callout immediately precedes an alternation bar, a closing parenthesis, or the end of the pattern, the length is zero. when the callout precedes an opening parenthesis, the length is that of the entire subpattern. ✨
  • The pattern_position and next_item_length fields are intended to help in distinguishing between different automatic callouts, which all have the same callout number. however, they are set for all callouts.
  • The mark field is present from version 2 of the callout structure. in callouts from pcre_exec() or pcre[16|32]_exec() it contains a pointer to the zero-terminated name of the most recently passed (*MARK), (*PRUNE), or (*THEN) item in the match, or NULL if no such items have been passed. instances of (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). in callouts from the DFA matching functions this field always contains NULL. DFA said no marks today, sorry πŸ’€

πŸ”₯ RETURN VALUES πŸ”₯

The external callout function returns an integer to PCRE. here's the vibe check on what those integers mean:

  1. if the value is zero, matching proceeds as normal. πŸŽ€
  2. if the value is greater than zero, matching fails at the current point, but the testing of other matching possibilities goes ahead, just as if a lookahead assertion had failed. πŸ’…
  3. if the value is less than zero, the match is abandoned, and the matching function returns the negative value. full stop, no more matching, we're done here. 😭

Negative values should normally be chosen from the set of PCRE_ERROR_xxx values. in particular, PCRE_ERROR_NOMATCH forces a standard "no match" failure. the error number PCRE_ERROR_CALLOUT is reserved for use by callout functions; it will never be used by PCRE itself. it's yours, bestie, reserved just for you 🫢


🌸 AUTHOR 🌸

Philip Hazel University Computing Service Cambridge CB2 3QH, England.

shoutout to the icon who built all of this πŸ’–


🌈 REVISION 🌈

Last updated: 12 November 2013 Copyright (c) 1997-2013 University of Cambridge.


and that's the whole page, no cap. this was PCRECALLOUT(3) from PCRE 8.34, dated 12 November 2013 β€” go forth and call out responsibly, bestie. mwah πŸ’…πŸ’–βœ¨