Substitute code update and documentation.

This commit is contained in:
Philip.Hazel 2014-11-11 16:51:07 +00:00
parent bb34dede56
commit b3ac0ffb32
21 changed files with 978 additions and 551 deletions

View File

@ -63,6 +63,7 @@ dist_html_DATA = \
doc/html/pcre2_set_parens_nest_limit.html \
doc/html/pcre2_set_recursion_limit.html \
doc/html/pcre2_set_recursion_memory_management.html \
doc/html/pcre2_substitute.html \
doc/html/pcre2_substring_copy_byname.html \
doc/html/pcre2_substring_copy_bynumber.html \
doc/html/pcre2_substring_free.html \
@ -134,6 +135,7 @@ dist_man_MANS = \
doc/pcre2_set_parens_nest_limit.3 \
doc/pcre2_set_recursion_limit.3 \
doc/pcre2_set_recursion_memory_management.3 \
doc/pcre2_substitute.3 \
doc/pcre2_substring_copy_byname.3 \
doc/pcre2_substring_copy_bynumber.3 \
doc/pcre2_substring_free.3 \

View File

@ -204,6 +204,10 @@ in the library.
<tr><td><a href="pcre2_set_recursion_memory_management.html">pcre2_set_recursion_memory_management</a></td>
<td>&nbsp;&nbsp;Set match recursion memory management</td></tr>
<tr><td><a href="pcre2_substitute.html">pcre2_substitute</a></td>
<td>&nbsp;&nbsp;Match a compiled pattern to a subject string and do
substitutions</td></tr>
<tr><td><a href="pcre2_substring_copy_byname.html">pcre2_substring_copy_byname</a></td>
<td>&nbsp;&nbsp;Extract named substring into given buffer</td></tr>

View File

@ -70,8 +70,8 @@ available codes are:
</pre>
The function yields a non-negative value on success or the negative value
PCRE2_ERROR_BADOPTION otherwise. This is also the result for the
PCRE2_CONFIG_JITTARGET code if JIT support is not available. When a string
is returned the yield is the length of the string, in code units, excluding the
PCRE2_CONFIG_JITTARGET code if JIT support is not available. When a string is
requested, the function returns the number of code units used, including the
terminating zero.
</P>
<P>

View File

@ -22,7 +22,7 @@ SYNOPSIS
<b>int pcre2_jit_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
<b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
<b> pcre2_match_context *<i>mcontext</i>, pcre2_jit_stack *<i>jit_stack</i>);</b>
<b> pcre2_match_context *<i>mcontext</i>);</b>
</P>
<br><b>
DESCRIPTION
@ -33,11 +33,7 @@ processed by the JIT compiler against a given subject string, using a matching
algorithm that is similar to Perl's. It is a "fast path" interface to JIT, and
it bypasses some of the sanity checks that <b>pcre2_match()</b> applies.
Its arguments are exactly the same as for
<a href="pcre2_match.html"><b>pcre2_match()</b></a>
plus one additional argument that must either point to a JIT stack or be NULL.
In the latter case, if a callback function has been set up by
<b>pcre2_jit_stack_create()</b>, it is called. Otherwise the system stack is
used.
<a href="pcre2_match.html"><b>pcre2_match()</b>.</a>
</P>
<P>
The supported options are PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY,

View File

@ -19,18 +19,20 @@ SYNOPSIS
<b>#include &#60;pcre2.h&#62;</b>
</P>
<P>
<b>void pcre2_jit_stack_assign(const pcre2_code *<i>code</i>,</b>
<b>void pcre2_jit_stack_assign(pcre2_match_context *<i>mcontext</i>,</b>
<b> pcre2_jit_callback <i>callback_function</i>, void *<i>callback_data</i>);</b>
</P>
<br><b>
DESCRIPTION
</b><br>
<P>
This function provides control over the memory used as a stack at run-time by a
call to <b>pcre2_match()</b> or <b>pcre2_jit_match()</b> with a pattern that has
been successfully processed by the JIT compiler. The arguments are:
This function provides control over the memory used by JIT as a run-time stack
when <b>pcre2_match()</b> or <b>pcre2_jit_match()</b> is called with a pattern
that has been successfully processed by the JIT compiler. The information that
determines which stack is used is put into a match context that is subsequently
passed to a matching function. The arguments of this function are:
<pre>
code the pointer returned by <b>pcre2_compile()</b>
mcontext a pointer to a match context
callback a callback function
callback_data a JIT stack or a value to be passed to the callback
</PRE>
@ -51,7 +53,7 @@ result is NULL, the internal 32K stack is used; otherwise the return value must
be a valid JIT stack, the result of calling <b>pcre2_jit_stack_create()</b>.
</P>
<P>
You may safely assign the same JIT stack to multiple patterns, as long as they
You may safely use the same JIT stack for multiple patterns, as long as they
are all matched in the same thread. In a multithread application, each thread
must use its own JIT stack. For more details, see the
<a href="pcre2jit.html"><b>pcre2jit</b></a>

View File

@ -19,7 +19,7 @@ SYNOPSIS
<b>#include &#60;pcre2.h&#62;</b>
</P>
<P>
<b>pcre2_match_data_create_from_pattern(pcre2_code *<i>code</i>,</b>
<b>pcre2_match_data_create_from_pattern(const pcre2_code *<i>code</i>,</b>
<b> pcre2_general_context *<i>gcontext</i>);</b>
</P>
<br><b>

View File

@ -0,0 +1,85 @@
<html>
<head>
<title>pcre2_substitute specification</title>
</head>
<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
<h1>pcre2_substitute man page</h1>
<p>
Return to the <a href="index.html">PCRE2 index page</a>.
</p>
<p>
This page is part of the PCRE2 HTML documentation. It was generated
automatically from the original man page. If there is any nonsense in it,
please consult the man page, in case the conversion went wrong.
<br>
<br><b>
SYNOPSIS
</b><br>
<P>
<b>#include &#60;pcre2.h&#62;</b>
</P>
<P>
<b>int pcre2_substitute(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
<b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
<b> pcre2_match_context *<i>mcontext</i>, PCRE2_SPTR \fIreplacementzfP,</b>
<b> PCRE2_SIZE <i>rlength</i>, PCRE2_UCHAR *<i>outputbuffer</i>,</b>
<b> PCRE2_SIZE *<i>outlengthptr</i>);</b>
</P>
<br><b>
DESCRIPTION
</b><br>
<P>
This function matches a compiled regular expression against a given subject
string, using a matching algorithm that is similar to Perl's. It then makes a
copy of the subject, substituting a replacement string for what was matched.
Its arguments are:
<pre>
<i>code</i> Points to the compiled pattern
<i>subject</i> Points to the subject string
<i>length</i> Length of the subject string
<i>startoffset</i> Offset in the subject at which to start matching
<i>options</i> Option bits
<i>match_data</i> Points to a match data block, or is NULL
<i>mcontext</i> Points to a match context, or is NULL
<i>replacement</i> Points to the replacement string
<i>rlength</i> Length of the replacement string
<i>outputbuffer</i> Points to the output buffer
<i>outlengthptr</i> Points to the length of the output buffer
</pre>
A match context is needed only if you want to:
<pre>
Set up a callout function
Change the limit for calling the internal function <i>match()</i>
Change the limit for calling <i>match()</i> recursively
Set custom memory management when the heap is used for recursion
</pre>
The <i>length</i>, <i>startoffset</i> and <i>rlength</i> values are code
units, not characters, as is the contents of the variable pointed at by
<i>outlengthptr</i>, which is updated to the actual length of the new string.
The options are:
<pre>
PCRE2_ANCHORED Match only at the first position
PCRE2_NOTBOL Subject string is not the beginning of a line
PCRE2_NOTEOL Subject string is not the end of a line
PCRE2_NOTEMPTY An empty string is not a valid match
PCRE2_NOTEMPTY_ATSTART An empty string at the start of the subject
is not a valid match
PCRE2_NO_UTF_CHECK Do not check the subject or replacement for
UTF validity (only relevant if PCRE2_UTF
was set at compile time)
PCRE2_SUBSTITUTE_GLOBAL Replace all occurrences in the subject
</pre>
The function returns the number of substitutions, which may be zero if there
were no matches. The result can be greater than one only when
PCRE2_SUBSTITUTE_GLOBAL is set.
</P>
<P>
There is a complete description of the PCRE2 native API in the
<a href="pcre2api.html"><b>pcre2api</b></a>
page and a description of the POSIX API in the
<a href="pcre2posix.html"><b>pcre2posix</b></a>
page.
<p>
Return to the <a href="index.html">PCRE2 index page</a>.
</p>

View File

@ -19,32 +19,34 @@ please consult the man page, in case the conversion went wrong.
<li><a name="TOC4" href="#SEC4">PCRE2 NATIVE API COMPILE CONTEXT FUNCTIONS</a>
<li><a name="TOC5" href="#SEC5">PCRE2 NATIVE API MATCH CONTEXT FUNCTIONS</a>
<li><a name="TOC6" href="#SEC6">PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS</a>
<li><a name="TOC7" href="#SEC7">PCRE2 NATIVE API JIT FUNCTIONS</a>
<li><a name="TOC8" href="#SEC8">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a>
<li><a name="TOC9" href="#SEC9">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a>
<li><a name="TOC10" href="#SEC10">PCRE2 API OVERVIEW</a>
<li><a name="TOC11" href="#SEC11">NEWLINES</a>
<li><a name="TOC12" href="#SEC12">MULTITHREADING</a>
<li><a name="TOC13" href="#SEC13">PCRE2 CONTEXTS</a>
<li><a name="TOC14" href="#SEC14">CHECKING BUILD-TIME OPTIONS</a>
<li><a name="TOC15" href="#SEC15">COMPILING A PATTERN</a>
<li><a name="TOC16" href="#SEC16">COMPILATION ERROR CODES</a>
<li><a name="TOC17" href="#SEC17">JUST-IN-TIME (JIT) COMPILATION</a>
<li><a name="TOC18" href="#SEC18">LOCALE SUPPORT</a>
<li><a name="TOC19" href="#SEC19">INFORMATION ABOUT A COMPILED PATTERN</a>
<li><a name="TOC20" href="#SEC20">THE MATCH DATA BLOCK</a>
<li><a name="TOC21" href="#SEC21">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a>
<li><a name="TOC22" href="#SEC22">NEWLINE HANDLING WHEN MATCHING</a>
<li><a name="TOC23" href="#SEC23">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a>
<li><a name="TOC24" href="#SEC24">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a>
<li><a name="TOC25" href="#SEC25">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a>
<li><a name="TOC26" href="#SEC26">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a>
<li><a name="TOC27" href="#SEC27">DUPLICATE SUBPATTERN NAMES</a>
<li><a name="TOC28" href="#SEC28">FINDING ALL POSSIBLE MATCHES</a>
<li><a name="TOC29" href="#SEC29">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a>
<li><a name="TOC30" href="#SEC30">SEE ALSO</a>
<li><a name="TOC31" href="#SEC31">AUTHOR</a>
<li><a name="TOC32" href="#SEC32">REVISION</a>
<li><a name="TOC7" href="#SEC7">PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION</a>
<li><a name="TOC8" href="#SEC8">PCRE2 NATIVE API JIT FUNCTIONS</a>
<li><a name="TOC9" href="#SEC9">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a>
<li><a name="TOC10" href="#SEC10">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a>
<li><a name="TOC11" href="#SEC11">PCRE2 API OVERVIEW</a>
<li><a name="TOC12" href="#SEC12">NEWLINES</a>
<li><a name="TOC13" href="#SEC13">MULTITHREADING</a>
<li><a name="TOC14" href="#SEC14">PCRE2 CONTEXTS</a>
<li><a name="TOC15" href="#SEC15">CHECKING BUILD-TIME OPTIONS</a>
<li><a name="TOC16" href="#SEC16">COMPILING A PATTERN</a>
<li><a name="TOC17" href="#SEC17">COMPILATION ERROR CODES</a>
<li><a name="TOC18" href="#SEC18">JUST-IN-TIME (JIT) COMPILATION</a>
<li><a name="TOC19" href="#SEC19">LOCALE SUPPORT</a>
<li><a name="TOC20" href="#SEC20">INFORMATION ABOUT A COMPILED PATTERN</a>
<li><a name="TOC21" href="#SEC21">THE MATCH DATA BLOCK</a>
<li><a name="TOC22" href="#SEC22">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a>
<li><a name="TOC23" href="#SEC23">NEWLINE HANDLING WHEN MATCHING</a>
<li><a name="TOC24" href="#SEC24">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a>
<li><a name="TOC25" href="#SEC25">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a>
<li><a name="TOC26" href="#SEC26">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a>
<li><a name="TOC27" href="#SEC27">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a>
<li><a name="TOC28" href="#SEC28">CREATING A NEW STRING WITH SUBSTITUTIONS</a>
<li><a name="TOC29" href="#SEC29">DUPLICATE SUBPATTERN NAMES</a>
<li><a name="TOC30" href="#SEC30">FINDING ALL POSSIBLE MATCHES</a>
<li><a name="TOC31" href="#SEC31">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a>
<li><a name="TOC32" href="#SEC32">SEE ALSO</a>
<li><a name="TOC33" href="#SEC33">AUTHOR</a>
<li><a name="TOC34" href="#SEC34">REVISION</a>
</ul>
<P>
<b>#include &#60;pcre2.h&#62;</b>
@ -69,7 +71,7 @@ document for an overview of all the PCRE2 documentation.
<b> pcre2_general_context *<i>gcontext</i>);</b>
<br>
<br>
<b>pcre2_match_data_create_from_pattern(pcre2_code *<i>code</i>,</b>
<b>pcre2_match_data_create_from_pattern(const pcre2_code *<i>code</i>,</b>
<b> pcre2_general_context *<i>gcontext</i>);</b>
<br>
<br>
@ -222,7 +224,16 @@ document for an overview of all the PCRE2 documentation.
<b>int pcre2_substring_list_get(pcre2_match_data *<i>match_data</i>,</b>
<b>" PCRE2_UCHAR ***<i>listptr</i>, PCRE2_SIZE **<i>lengthsptr</i>);</b>
</P>
<br><a name="SEC7" href="#TOC1">PCRE2 NATIVE API JIT FUNCTIONS</a><br>
<br><a name="SEC7" href="#TOC1">PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION</a><br>
<P>
<b>int pcre2_substitute(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
<b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
<b> pcre2_match_context *<i>mcontext</i>, PCRE2_SPTR \fIreplacementzfP,</b>
<b> PCRE2_SIZE <i>rlength</i>, PCRE2_UCHAR *<i>outputbuffer</i>,</b>
<b> PCRE2_SIZE *<i>outlengthptr</i>);</b>
</P>
<br><a name="SEC8" href="#TOC1">PCRE2 NATIVE API JIT FUNCTIONS</a><br>
<P>
<b>int pcre2_jit_compile(pcre2_code *<i>code</i>, uint32_t <i>options</i>);</b>
<br>
@ -230,7 +241,7 @@ document for an overview of all the PCRE2 documentation.
<b>int pcre2_jit_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
<b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
<b> pcre2_match_context *<i>mcontext</i>, pcre2_jit_stack *<i>jit_stack</i>);</b>
<b> pcre2_match_context *<i>mcontext</i>);</b>
<br>
<br>
<b>void pcre2_jit_free_unused_memory(pcre2_general_context *<i>gcontext</i>);</b>
@ -240,13 +251,13 @@ document for an overview of all the PCRE2 documentation.
<b> PCRE2_SIZE <i>startsize</i>, PCRE2_SIZE <i>maxsize</i>);</b>
<br>
<br>
<b>void pcre2_jit_stack_assign(const pcre2_code *<i>code</i>,</b>
<b>void pcre2_jit_stack_assign(pcre2_match_context *<i>mcontext</i>,</b>
<b> pcre2_jit_callback <i>callback_function</i>, void *<i>callback_data</i>);</b>
<br>
<br>
<b>void pcre2_jit_stack_free(pcre2_jit_stack *<i>jit_stack</i>);</b>
</P>
<br><a name="SEC8" href="#TOC1">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a><br>
<br><a name="SEC9" href="#TOC1">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a><br>
<P>
<b>int pcre2_get_error_message(int <i>errorcode</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
<b> PCRE2_SIZE <i>bufflen</i>);</b>
@ -260,7 +271,7 @@ document for an overview of all the PCRE2 documentation.
<br>
<b>int pcre2_config(uint32_t <i>what</i>, void *<i>where</i>);</b>
</P>
<br><a name="SEC9" href="#TOC1">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a><br>
<br><a name="SEC10" href="#TOC1">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a><br>
<P>
There are three PCRE2 libraries, supporting 8-bit, 16-bit, and 32-bit code
units, respectively. However, there is just one header file, <b>pcre2.h</b>.
@ -321,7 +332,7 @@ In the function summaries above, and in the rest of this document and other
PCRE2 documents, functions and data types are described using their generic
names, without the 8, 16, or 32 suffix.
</P>
<br><a name="SEC10" href="#TOC1">PCRE2 API OVERVIEW</a><br>
<br><a name="SEC11" href="#TOC1">PCRE2 API OVERVIEW</a><br>
<P>
PCRE2 has its own native API, which is described in this document. There are
also some wrapper functions for the 8-bit library that correspond to the
@ -386,8 +397,8 @@ documentation. There is no JIT support for <b>pcre2_dfa_match()</b>.
</P>
<P>
In addition to the main compiling and matching functions, there are convenience
functions for extracting captured substrings from a subject string that is
matched by <b>pcre2_match()</b>. They are:
functions for extracting captured substrings from a subject string that has
been matched by <b>pcre2_match()</b>. They are:
<pre>
<b>pcre2_substring_copy_byname()</b>
<b>pcre2_substring_copy_bynumber()</b>
@ -403,11 +414,16 @@ matched by <b>pcre2_match()</b>. They are:
provided, to free the memory used for extracted strings.
</P>
<P>
There are functions for finding out information about a compiled pattern
(<b>pcre2_pattern_info()</b>) and about the configuration with which PCRE2 was
built (<b>pcre2_config()</b>).
The function <b>pcre2_substitute()</b> can be called to match a pattern and
return a copy of the subject string with substitutions for parts that were
matched.
</P>
<P>
Finally, there are functions for finding out information about a compiled
pattern (<b>pcre2_pattern_info()</b>) and about the configuration with which
PCRE2 was built (<b>pcre2_config()</b>).
<a name="newlines"></a></P>
<br><a name="SEC11" href="#TOC1">NEWLINES</a><br>
<br><a name="SEC12" href="#TOC1">NEWLINES</a><br>
<P>
PCRE2 supports five different conventions for indicating line breaks in
strings: a single CR (carriage return) character, a single LF (linefeed)
@ -446,7 +462,7 @@ The choice of newline convention does not affect the interpretation of
the \n or \r escape sequences, nor does it affect what \R matches, which has
its own separate control.
</P>
<br><a name="SEC12" href="#TOC1">MULTITHREADING</a><br>
<br><a name="SEC13" href="#TOC1">MULTITHREADING</a><br>
<P>
In a multithreaded application it is important to keep thread-specific data
separate from data that can be shared between threads. The PCRE2 library code
@ -491,7 +507,7 @@ storing the results of a match. This includes details of what was matched, as
well as additional information such as the name of a (*MARK) setting. Each
thread must provide its own version of this memory.
</P>
<br><a name="SEC13" href="#TOC1">PCRE2 CONTEXTS</a><br>
<br><a name="SEC14" href="#TOC1">PCRE2 CONTEXTS</a><br>
<P>
Some PCRE2 functions have a lot of parameters, many of which are used only by
specialist applications, for example, those that use custom memory management
@ -765,7 +781,7 @@ so that they can be re-used when possible during the match. In the absence of
these functions, the normal custom memory management functions are used, if
supplied, otherwise the system functions.
</P>
<br><a name="SEC14" href="#TOC1">CHECKING BUILD-TIME OPTIONS</a><br>
<br><a name="SEC15" href="#TOC1">CHECKING BUILD-TIME OPTIONS</a><br>
<P>
<b>int pcre2_config(uint32_t <i>what</i>, void *<i>where</i>);</b>
</P>
@ -809,8 +825,9 @@ units long. (The exact length needed can be found by calling
<b>pcre2_config()</b> with <b>where</b> set to NULL.) The buffer is filled with a
string that contains the name of the architecture for which the JIT compiler is
configured, for example "x86 32bit (little endian + unaligned)". If JIT support
is not available, PCRE2_ERROR_BADOPTION is returned, otherwise the length of
the string, in code units, is returned.
is not available, PCRE2_ERROR_BADOPTION is returned, otherwise the number of
code units used is returned. This is the length of the string, plus one unit
for the terminating zero.
<pre>
PCRE2_CONFIG_LINKSIZE
</pre>
@ -878,8 +895,8 @@ units long. (The exact length needed can be found by calling
<b>pcre2_config()</b> with <b>where</b> set to NULL.) If PCRE2 has been compiled
without Unicode support, the buffer is filled with the text "Unicode not
supported". Otherwise, the Unicode version string (for example, "7.0.0") is
inserted. The string is zero-terminated. The function returns the length of the
string in code units.
inserted. The number of code units used is returned. This is the length of the
string plus one unit for the terminating zero.
<pre>
PCRE2_CONFIG_UNICODE
</pre>
@ -891,10 +908,11 @@ otherwise it is set to zero. Unicode support implies UTF support.
The <i>where</i> argument should point to a buffer that is at least 12 code
units long. (The exact length needed can be found by calling
<b>pcre2_config()</b> with <b>where</b> set to NULL.) The buffer is filled with
the PCRE2 version string, zero-terminated. The length of the string (in code
units) is returned.
the PCRE2 version string, zero-terminated. The number of code units used is
returned. This is the length of the string plus one unit for the terminating
zero.
<a name="compiling"></a></P>
<br><a name="SEC15" href="#TOC1">COMPILING A PATTERN</a><br>
<br><a name="SEC16" href="#TOC1">COMPILING A PATTERN</a><br>
<P>
<b>pcre2_code *pcre2_compile(PCRE2_SPTR <i>pattern</i>, PCRE2_SIZE <i>length</i>,</b>
<b> uint32_t <i>options</i>, int *<i>errorcode</i>, PCRE2_SIZE *<i>erroroffset,</i></b>
@ -1248,7 +1266,7 @@ of how this option changes the behaviour of PCRE2 are given in the
<a href="pcre2unicode.html"><b>pcre2unicode</b></a>
page.
</P>
<br><a name="SEC16" href="#TOC1">COMPILATION ERROR CODES</a><br>
<br><a name="SEC17" href="#TOC1">COMPILATION ERROR CODES</a><br>
<P>
There are over 80 positive error codes that <b>pcre2_compile()</b> may return if
it finds an error in the pattern. There are also some negative error codes that
@ -1258,7 +1276,7 @@ are used for invalid UTF strings. These are the same as given by
page. The <b>pcre2_get_error_message()</b> function can be called to obtain a
textual error message from any error code.
</P>
<br><a name="SEC17" href="#TOC1">JUST-IN-TIME (JIT) COMPILATION</a><br>
<br><a name="SEC18" href="#TOC1">JUST-IN-TIME (JIT) COMPILATION</a><br>
<P>
<b>int pcre2_jit_compile(pcre2_code *<i>code</i>, uint32_t <i>options</i>);</b>
<br>
@ -1266,7 +1284,7 @@ textual error message from any error code.
<b>int pcre2_jit_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
<b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
<b> pcre2_match_context *<i>mcontext</i>, pcre2_jit_stack *<i>jit_stack</i>);</b>
<b> pcre2_match_context *<i>mcontext</i>);</b>
<br>
<br>
<b>void pcre2_jit_free_unused_memory(pcre2_general_context *<i>gcontext</i>);</b>
@ -1276,7 +1294,7 @@ textual error message from any error code.
<b> PCRE2_SIZE <i>startsize</i>, PCRE2_SIZE <i>maxsize</i>);</b>
<br>
<br>
<b>void pcre2_jit_stack_assign(const pcre2_code *<i>code</i>,</b>
<b>void pcre2_jit_stack_assign(pcre2_match_context *<i>mcontext</i>,</b>
<b> pcre2_jit_callback <i>callback_function</i>, void *<i>callback_data</i>);</b>
<br>
<br>
@ -1296,7 +1314,7 @@ patterns to be analyzed, and for one-off matches and simple patterns the
benefit of faster execution might be offset by a much slower compilation time.
Most, but not all patterns can be optimized by the JIT compiler.
<a name="localesupport"></a></P>
<br><a name="SEC18" href="#TOC1">LOCALE SUPPORT</a><br>
<br><a name="SEC19" href="#TOC1">LOCALE SUPPORT</a><br>
<P>
PCRE2 handles caseless matching, and determines whether characters are letters,
digits, or whatever, by reference to a set of tables, indexed by character code
@ -1353,7 +1371,7 @@ is saved with the compiled pattern, and the same tables are used by
compilation, and matching all happen in the same locale, but different patterns
can be processed in different locales.
<a name="infoaboutpattern"></a></P>
<br><a name="SEC19" href="#TOC1">INFORMATION ABOUT A COMPILED PATTERN</a><br>
<br><a name="SEC20" href="#TOC1">INFORMATION ABOUT A COMPILED PATTERN</a><br>
<P>
<b>int pcre2_pattern_info(const pcre2 *<i>code</i>, uint32_t <i>what</i>, void *<i>where</i>);</b>
</P>
@ -1640,13 +1658,13 @@ getting memory in which to place the compiled data is the value returned by
this option plus the size of the <b>pcre2_code</b> structure. Processing a
pattern with the JIT compiler does not alter the value returned by this option.
<a name="matchdatablock"></a></P>
<br><a name="SEC20" href="#TOC1">THE MATCH DATA BLOCK</a><br>
<br><a name="SEC21" href="#TOC1">THE MATCH DATA BLOCK</a><br>
<P>
<b>pcre2_match_data_create(uint32_t <i>ovecsize</i>,</b>
<b> pcre2_general_context *<i>gcontext</i>);</b>
<br>
<br>
<b>pcre2_match_data_create_from_pattern(pcre2_code *<i>code</i>,</b>
<b>pcre2_match_data_create_from_pattern(const pcre2_code *<i>code</i>,</b>
<b> pcre2_general_context *<i>gcontext</i>);</b>
<br>
<br>
@ -1690,7 +1708,7 @@ and
<a href="#matchotherdata">other match data</a>
below.
</P>
<br><a name="SEC21" href="#TOC1">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a><br>
<br><a name="SEC22" href="#TOC1">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a><br>
<P>
<b>int pcre2_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
@ -1902,7 +1920,7 @@ examples, in the
<a href="pcre2partial.html"><b>pcre2partial</b></a>
documentation.
</P>
<br><a name="SEC22" href="#TOC1">NEWLINE HANDLING WHEN MATCHING</a><br>
<br><a name="SEC23" href="#TOC1">NEWLINE HANDLING WHEN MATCHING</a><br>
<P>
When PCRE2 is built, a default newline convention is set; this is usually the
standard convention for the operating system. The default can be overridden in
@ -1940,7 +1958,7 @@ the characters that it matches).
Notwithstanding the above, anomalous effects may still occur when CRLF is a
valid newline sequence and explicit \r or \n escapes appear in the pattern.
<a name="matchedstrings"></a></P>
<br><a name="SEC23" href="#TOC1">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a><br>
<br><a name="SEC24" href="#TOC1">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a><br>
<P>
<b>uint32_t pcre2_get_ovector_count(pcre2_match_data *<i>match_data</i>);</b>
<br>
@ -2167,7 +2185,7 @@ time.
</pre>
The internal recursion limit was reached.
<a name="extractbynumber"></a></P>
<br><a name="SEC24" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
<br><a name="SEC25" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
<P>
<b>int pcre2_substring_length_bynumber(pcre2_match_data *<i>match_data</i>,</b>
<b> unsigned int <i>number</i>, PCRE2_SIZE *<i>length</i>);</b>
@ -2239,7 +2257,7 @@ no capturing group of that number in the pattern, or because the group with
that number did not participate in the match, or because the ovector was too
small to capture that group.
</P>
<br><a name="SEC25" href="#TOC1">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a><br>
<br><a name="SEC26" href="#TOC1">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a><br>
<P>
<b>int pcre2_substring_list_get(pcre2_match_data *<i>match_data</i>,</b>
<b>" PCRE2_UCHAR ***<i>listptr</i>, PCRE2_SIZE **<i>lengthsptr</i>);</b>
@ -2273,7 +2291,7 @@ can be distinguished from a genuine zero-length substring by inspecting the
appropriate offset in the ovector, which contains PCRE2_UNSET for unset
substrings.
<a name="extractbynname"></a></P>
<br><a name="SEC26" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
<br><a name="SEC27" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
<P>
<b>int pcre2_substring_number_from_name(const pcre2_code *<i>code</i>,</b>
<b> PCRE2_SPTR <i>name</i>);</b>
@ -2326,7 +2344,67 @@ names are not included in the compiled code. The matching process uses only
numbers. For this reason, the use of different names for subpatterns of the
same number causes an error at compile time.
</P>
<br><a name="SEC27" href="#TOC1">DUPLICATE SUBPATTERN NAMES</a><br>
<br><a name="SEC28" href="#TOC1">CREATING A NEW STRING WITH SUBSTITUTIONS</a><br>
<P>
<b>int pcre2_substitute(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
<b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
<b> pcre2_match_context *<i>mcontext</i>, PCRE2_SPTR \fIreplacementzfP,</b>
<b> PCRE2_SIZE <i>rlength</i>, PCRE2_UCHAR *\fIoutputbuffer\zfP,</b>
<b> PCRE2_SIZE *<i>outlengthptr</i>);</b>
This function calls <b>pcre2_match()</b> and then makes a copy of the subject
string in <i>outputbuffer</i>, replacing the part that was matched with the
<i>replacement</i> string, whose length is supplied in <b>rlength</b>. This can
be given as PCRE2_ZERO_TERMINATED for a zero-terminated string.
</P>
<P>
In the replacement string, which is interpreted as a UTF string in UTF mode, a
dollar character is an escape character that can specify the insertion of
characters from capturing groups in the pattern. The following forms are
recognized:
<pre>
$$ insert a dollar character
$&#60;n&#62; insert the contents of group &#60;n&#62;
${&#60;n&#62;} insert the contents of group &#60;n&#62;
</pre>
Either a group number or a group name can be given for &#60;n&#62;. Curly brackets are
required only if the following character would be interpreted as part of the
number or name. The number may be zero to include the entire matched string.
For example, if the pattern a(b)c is matched with "[abc]" and the replacement
string "+$1$0$1+", the result is "[+babcb+]". Group insertion is done by
calling <b>pcre2_copy_byname()</b> or <b>pcre2_copy_bynumber()</b> as
appropriate.
</P>
<P>
The first seven arguments of <b>pcre2_substitute()</b> are the same as for
<b>pcre2_match()</b>, except that the partial matching options are not
permitted, and <i>match_data</i> may be passed as NULL, in which case a match
data block is obtained and freed within this function, using memory management
functions from the match context, if provided, or else those that were used to
allocate memory for the compiled code.
</P>
<P>
There is one additional option, PCRE2_SUBSTITUTE_GLOBAL, which causes the
function to iterate over the subject string, replacing every matching
substring. If this is not set, only the first matching substring is replaced.
</P>
<P>
The <i>outlengthptr</i> argument must point to a variable that contains the
length, in code units, of the output buffer. It is updated to contain the
length of the new string, excluding the trailing zero that is automatically
added.
</P>
<P>
The function returns the number of replacements that were made. This may be
zero if no matches were found, and is never greater than 1 unless
PCRE2_SUBSTITUTE_GLOBAL is set. In the event of an error, a negative error code
is returned. Except for PCRE2_ERROR_NOMATCH (which is never returned), any
errors from <b>pcre2_match()</b> or the substring copying functions are passed
straight back. PCRE2_ERROR_BADREPLACEMENT is returned for an invalid
replacement string (unrecognized sequence following a dollar sign), and
PCRE2_ERROR_NOMEMORY is returned if the output buffer is not big enough.
</P>
<br><a name="SEC29" href="#TOC1">DUPLICATE SUBPATTERN NAMES</a><br>
<P>
<b>int pcre2_substring_nametable_scan(const pcre2_code *<i>code</i>,</b>
<b> PCRE2_SPTR <i>name</i>, PCRE2_SPTR *<i>first</i>, PCRE2_SPTR *<i>last</i>);</b>
@ -2369,7 +2447,7 @@ The format of the name table is described above in the section entitled
Given all the relevant entries for the name, you can extract each of their
numbers, and hence the captured data.
</P>
<br><a name="SEC28" href="#TOC1">FINDING ALL POSSIBLE MATCHES</a><br>
<br><a name="SEC30" href="#TOC1">FINDING ALL POSSIBLE MATCHES</a><br>
<P>
The traditional matching function uses a similar algorithm to Perl, which stops
when it finds the first match, starting at a given point in the subject. If you
@ -2387,7 +2465,7 @@ substring. Then return 1, which forces <b>pcre2_match()</b> to backtrack and try
other alternatives. Ultimately, when it runs out of matches,
<b>pcre2_match()</b> will yield PCRE2_ERROR_NOMATCH.
<a name="dfamatch"></a></P>
<br><a name="SEC29" href="#TOC1">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a><br>
<br><a name="SEC31" href="#TOC1">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a><br>
<P>
<b>int pcre2_dfa_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
<b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
@ -2562,13 +2640,13 @@ some plausibility checks are made on the contents of the workspace, which
should contain data about the previous partial match. If any of these checks
fail, this error is given.
</P>
<br><a name="SEC30" href="#TOC1">SEE ALSO</a><br>
<br><a name="SEC32" href="#TOC1">SEE ALSO</a><br>
<P>
<b>pcre2build</b>(3), <b>pcre2libs</b>(3), <b>pcre2callout</b>(3),
<b>pcre2matching</b>(3), <b>pcre2partial</b>(3), <b>pcre2posix</b>(3),
<b>pcre2demo(3)</b>, <b>pcre2sample</b>(3), <b>pcre2stack</b>(3).
</P>
<br><a name="SEC31" href="#TOC1">AUTHOR</a><br>
<br><a name="SEC33" href="#TOC1">AUTHOR</a><br>
<P>
Philip Hazel
<br>
@ -2577,9 +2655,9 @@ University Computing Service
Cambridge CB2 3QH, England.
<br>
</P>
<br><a name="SEC32" href="#TOC1">REVISION</a><br>
<br><a name="SEC34" href="#TOC1">REVISION</a><br>
<P>
Last updated: 03 November 2014
Last updated: 11 November 2014
<br>
Copyright &copy; 1997-2014 University of Cambridge.
<br>

View File

@ -178,19 +178,20 @@ pattern.
The <b>pcre2_jit_stack_assign()</b> function specifies which stack JIT code
should use. Its arguments are as follows:
<pre>
pcre2_code *code
pcre2_match_context *mcontext
pcre2_jit_callback callback
void *data
</pre>
The <i>code</i> argument is a pointer to a compiled pattern, after it has been
processed by <b>pcre2_jit_compile()</b>. There are three cases for the values of
the other two options:
The first argument is a pointer to a match context. When this is subsequently
passed to a matching function, its information determines which JIT stack is
used. There are three cases for the values of the other two options:
<pre>
(1) If <i>callback</i> is NULL and <i>data</i> is NULL, an internal 32K block
on the machine stack is used.
(2) If <i>callback</i> is NULL and <i>data</i> is not NULL, <i>data</i> must be
a valid JIT stack, the result of calling <b>pcre2_jit_stack_create()</b>.
a pointer to a valid JIT stack, the result of calling
<b>pcre2_jit_stack_create()</b>.
(3) If <i>callback</i> is not NULL, it must point to a function that is
called with <i>data</i> as an argument at the start of matching, in
@ -215,11 +216,11 @@ each thread so that the application is thread-safe.
</P>
<P>
Strictly speaking, even more is allowed. You can assign the same non-NULL stack
to any number of patterns as long as they are not used for matching by multiple
threads at the same time. For example, you can assign the same stack to all
compiled patterns, and use a global mutex in the callback to wait until the
stack is available for use. However, this is an inefficient solution, and not
recommended.
to a match context that is used by any number of patterns, as long as they are
not used for matching by multiple threads at the same time. For example, you
could use the same stack in all compiled patterns, with a global mutex in the
callback to wait until the stack is available for use. However, this is an
inefficient solution, and not recommended.
</P>
<P>
This is a suggestion for how a multithreaded program that needs to set up
@ -234,10 +235,7 @@ non-default JIT stacks might operate:
Use a one-line callback function
return thread_local_var
</pre>
All the functions described in this section do nothing if JIT is not available,
and <b>pcre2_jit_stack_assign()</b> does nothing unless the <b>code</b> argument
is non-NULL and points to a <b>pcre2_code</b> block that has been successfully
processed by <b>pcre2_jit_compile()</b>.
All the functions described in this section do nothing if JIT is not available.
<a name="stackfaq"></a></P>
<br><a name="SEC7" href="#TOC1">JIT STACK FAQ</a><br>
<P>
@ -267,26 +265,26 @@ grow up to 1M anytime if needed.
<br>
<br>
The owner of the stack is the user program, not the JIT studied pattern or
anything else. The user program must ensure that if a stack is used by
<b>pcre2_match()</b>, (that is, it is assigned to the pattern currently
running), that stack must not be used by any other threads (to avoid
overwriting the same memory area). The best practice for multithreaded programs
is to allocate a stack for each thread, and return this stack through the JIT
callback function.
anything else. The user program must ensure that if a stack is being used by
<b>pcre2_match()</b>, (that is, it is assigned to a match context that is passed
to the pattern currently running), that stack must not be used by any other
threads (to avoid overwriting the same memory area). The best practice for
multithreaded programs is to allocate a stack for each thread, and return this
stack through the JIT callback function.
</P>
<P>
(4) When should a JIT stack be freed?
<br>
<br>
You can free a JIT stack at any time, as long as it will not be used by
<b>pcre2_match()</b> again. When you assign the stack to a pattern, only a
<b>pcre2_match()</b> again. When you assign the stack to a match context, only a
pointer is set. There is no reference counting or any other magic. You can free
the patterns and stacks in any order, anytime. Just <i>do not</i> call
<b>pcre2_match()</b> with a pattern pointing to an already freed stack, as that
will cause SEGFAULT. (Also, do not free a stack currently used by
<b>pcre2_match()</b> in another thread). You can also replace the stack for a
pattern at any time. You can even free the previous stack before assigning a
replacement.
compiled patterns, contexts, and stacks in any order, anytime. Just \fIdo
not\fP call <b>pcre2_match()</b> with a match context pointing to an already
freed stack, as that will cause SEGFAULT. (Also, do not free a stack currently
used by <b>pcre2_match()</b> in another thread). You can also replace the stack
in a context at any time when it is not in use. You can also free the previous
stack before assigning a replacement.
</P>
<P>
(5) Should I allocate/free a stack every time before/after calling
@ -296,7 +294,7 @@ replacement.
No, because this is too costly in terms of resources. However, you could
implement some clever idea which release the stack if it is not used in let's
say two minutes. The JIT callback can help to achieve this without keeping a
list of the currently JIT studied patterns.
list of patterns.
</P>
<P>
(6) OK, the stack is for long term memory allocation. But what happens if a
@ -333,25 +331,28 @@ memory management, or NULL for standard memory management.
<br><a name="SEC9" href="#TOC1">EXAMPLE CODE</a><br>
<P>
This is a single-threaded example that specifies a JIT stack without using a
callback.
callback. A real program should include error checking after all the function
calls.
<pre>
int rc;
pcre2_code *re;
pcre2_match_data *match_data;
pcre2_match_context *mcontext;
pcre2_jit_stack *jit_stack;
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0,
&errornumber, &erroffset, NULL);
/* Check for errors */
rc = pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
/* Check for errors */
mcontext = pcre2_match_context_create(NULL);
jit_stack = pcre2_jit_stack_create(NULL, 32*1024, 512*1024);
/* Check for error (NULL) */
pcre2_jit_stack_assign(re, NULL, jit_stack);
pcre2_jit_stack_assign(mcontext, NULL, jit_stack);
match_data = pcre2_match_data_create(re, 10);
rc = pcre2_match(re, subject, length, 0, 0, match_data, NULL);
/* Check results */
pcre2_free(re);
rc = pcre2_match(re, subject, length, 0, 0, match_data, mcontext);
/* Process result */
pcre2_code_free(re);
pcre2_match_data_free(match_data);
pcre2_match_context_free(mcontext);
pcre2_jit_stack_free(jit_stack);
</PRE>
@ -369,13 +370,10 @@ processed by <b>pcre2_jit_compile()</b>).
</P>
<P>
The fast path function is called <b>pcre2_jit_match()</b>, and it takes exactly
the same arguments as <b>pcre2_match()</b>, plus one additional argument that
must either point to a JIT stack or be NULL. In the latter case, if a callback
function has been set up by <b>pcre2_jit_stack_assign()</b>, it is called.
Otherwise the system stack is used. The return values are the same as for
<b>pcre2_match()</b>, plus PCRE2_ERROR_JIT_BADOPTION if a matching mode (partial
or complete) is requested that was not compiled. Unsupported option bits (for
example, PCRE2_ANCHORED) are ignored.
the same arguments as <b>pcre2_match()</b>. The return values are also the same,
plus PCRE2_ERROR_JIT_BADOPTION if a matching mode (partial or complete) is
requested that was not compiled. Unsupported option bits (for example,
PCRE2_ANCHORED) are ignored.
</P>
<P>
When you call <b>pcre2_match()</b>, as well as testing for invalid options, a
@ -404,7 +402,7 @@ Cambridge CB2 3QH, England.
</P>
<br><a name="SEC13" href="#TOC1">REVISION</a><br>
<P>
Last updated: 03 November 2014
Last updated: 08 November 2014
<br>
Copyright &copy; 1997-2014 University of Cambridge.
<br>

View File

@ -488,7 +488,6 @@ about the pattern:
posix use the POSIX API
stackguard=&#60;number&#62; test the stackguard feature
tables=[0|1|2] select internal tables
use_length use the pattern's length
</pre>
The effects of these modifiers are described in the following sections.
FIXME: Give more examples.
@ -542,15 +541,10 @@ pairs. For example:
/ab 32 59/hex
</pre>
This feature is provided as a way of creating patterns that contain binary zero
characters. When <b>hex</b> is set, it implies <b>use_length</b>.
</P>
<br><b>
Using the pattern's length
</b><br>
<P>
By default, <b>pcre2test</b> passes patterns as zero-terminated strings to
<b>pcre2_compile()</b>, giving the length as -1. If <b>use_length</b> is set, the
length of the pattern is passed. This is implied if <b>hex</b> is set.
characters. By default, <b>pcre2test</b> passes patterns as zero-terminated
strings to <b>pcre2_compile()</b>, giving the length as PCRE2_ZERO_TERMINATED.
However, for patterns specified in hexadecimal, the length of the pattern is
passed.
</P>
<br><b>
JIT compilation
@ -766,6 +760,7 @@ pattern.
ovector=&#60;n&#62; set size of output vector
recursion_limit=&#60;n&#62; set a recursion limit
startchar show startchar when relevant
zero_terminate pass the subject as zero-terminated
</pre>
The effects of these modifiers are described in the following sections.
FIXME: Give more examples.
@ -979,6 +974,20 @@ match block of exactly the right size for the pattern. (It is not possible to
create a match block with a zero-length ovector; there is always one pair of
offsets.)
</P>
<br><b>
Passing the subject as zero-terminated
</b><br>
<P>
By default, the subject string is passed to a native API matching function with
its correct length. In order to test the facility for passing a zero-terminated
string, the <b>zero_terminate</b> modifier is provided. It causes the length to
be passed as PCRE2_ZERO_TERMINATED. (When matching via the POSIX interface,
this modifier has no effect, as there is no facility for passing a length.)
</P>
<P>
When testing <b>pcre2_substitute</b>, this modifier also has the effect of
passing the replacement string as zero-terminated.
</P>
<br><a name="SEC12" href="#TOC1">THE ALTERNATIVE MATCHING FUNCTION</a><br>
<P>
By default, <b>pcre2test</b> uses the standard PCRE2 matching function,
@ -1224,7 +1233,7 @@ Cambridge CB2 3QH, England.
</P>
<br><a name="SEC20" href="#TOC1">REVISION</a><br>
<P>
Last updated: 02 November 2014
Last updated: 09 November 2014
<br>
Copyright &copy; 1997-2014 University of Cambridge.
<br>

View File

@ -204,6 +204,10 @@ in the library.
<tr><td><a href="pcre2_set_recursion_memory_management.html">pcre2_set_recursion_memory_management</a></td>
<td>&nbsp;&nbsp;Set match recursion memory management</td></tr>
<tr><td><a href="pcre2_substitute.html">pcre2_substitute</a></td>
<td>&nbsp;&nbsp;Match a compiled pattern to a subject string and do
substitutions</td></tr>
<tr><td><a href="pcre2_substring_copy_byname.html">pcre2_substring_copy_byname</a></td>
<td>&nbsp;&nbsp;Extract named substring into given buffer</td></tr>

View File

@ -186,7 +186,7 @@ PCRE2 NATIVE API BASIC FUNCTIONS
pcre2_match_data_create(uint32_t ovecsize,
pcre2_general_context *gcontext);
pcre2_match_data_create_from_pattern(pcre2_code *code,
pcre2_match_data_create_from_pattern(const pcre2_code *code,
pcre2_general_context *gcontext);
int pcre2_match(const pcre2_code *code, PCRE2_SPTR subject,
@ -314,6 +314,16 @@ PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS
PCRE2_UCHAR ***listptr, PCRE2_SIZE **lengthsptr);
PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION
int pcre2_substitute(const pcre2_code *code, PCRE2_SPTR subject,
PCRE2_SIZE length, PCRE2_SIZE startoffset,
uint32_t options, pcre2_match_data *match_data,
pcre2_match_context *mcontext, PCRE2_SPTR replacementzfP,
PCRE2_SIZE rlength, PCRE2_UCHAR *outputbuffer,
PCRE2_SIZE *outlengthptr);
PCRE2 NATIVE API JIT FUNCTIONS
int pcre2_jit_compile(pcre2_code *code, uint32_t options);
@ -321,14 +331,14 @@ PCRE2 NATIVE API JIT FUNCTIONS
int pcre2_jit_match(const pcre2_code *code, PCRE2_SPTR subject,
PCRE2_SIZE length, PCRE2_SIZE startoffset,
uint32_t options, pcre2_match_data *match_data,
pcre2_match_context *mcontext, pcre2_jit_stack *jit_stack);
pcre2_match_context *mcontext);
void pcre2_jit_free_unused_memory(pcre2_general_context *gcontext);
pcre2_jit_stack *pcre2_jit_stack_create(pcre2_general_context *gcontext,
PCRE2_SIZE startsize, PCRE2_SIZE maxsize);
void pcre2_jit_stack_assign(const pcre2_code *code,
void pcre2_jit_stack_assign(pcre2_match_context *mcontext,
pcre2_jit_callback callback_function, void *callback_data);
void pcre2_jit_stack_free(pcre2_jit_stack *jit_stack);
@ -459,7 +469,7 @@ PCRE2 API OVERVIEW
In addition to the main compiling and matching functions, there are
convenience functions for extracting captured substrings from a subject
string that is matched by pcre2_match(). They are:
string that has been matched by pcre2_match(). They are:
pcre2_substring_copy_byname()
pcre2_substring_copy_bynumber()
@ -474,9 +484,13 @@ PCRE2 API OVERVIEW
pcre2_substring_free() and pcre2_substring_list_free() are also pro-
vided, to free the memory used for extracted strings.
There are functions for finding out information about a compiled pat-
tern (pcre2_pattern_info()) and about the configuration with which
PCRE2 was built (pcre2_config()).
The function pcre2_substitute() can be called to match a pattern and
return a copy of the subject string with substitutions for parts that
were matched.
Finally, there are functions for finding out information about a com-
piled pattern (pcre2_pattern_info()) and about the configuration with
which PCRE2 was built (pcre2_config()).
NEWLINES
@ -862,7 +876,8 @@ CHECKING BUILD-TIME OPTIONS
contains the name of the architecture for which the JIT compiler is
configured, for example "x86 32bit (little endian + unaligned)". If JIT
support is not available, PCRE2_ERROR_BADOPTION is returned, otherwise
the length of the string, in code units, is returned.
the number of code units used is returned. This is the length of the
string, plus one unit for the terminating zero.
PCRE2_CONFIG_LINKSIZE
@ -932,8 +947,8 @@ CHECKING BUILD-TIME OPTIONS
fig() with where set to NULL.) If PCRE2 has been compiled without Uni-
code support, the buffer is filled with the text "Unicode not sup-
ported". Otherwise, the Unicode version string (for example, "7.0.0")
is inserted. The string is zero-terminated. The function returns the
length of the string in code units.
is inserted. The number of code units used is returned. This is the
length of the string plus one unit for the terminating zero.
PCRE2_CONFIG_UNICODE
@ -946,8 +961,9 @@ CHECKING BUILD-TIME OPTIONS
The where argument should point to a buffer that is at least 12 code
units long. (The exact length needed can be found by calling pcre2_con-
fig() with where set to NULL.) The buffer is filled with the PCRE2 ver-
sion string, zero-terminated. The length of the string (in code units)
is returned.
sion string, zero-terminated. The number of code units used is
returned. This is the length of the string plus one unit for the termi-
nating zero.
COMPILING A PATTERN
@ -1300,14 +1316,14 @@ JUST-IN-TIME (JIT) COMPILATION
int pcre2_jit_match(const pcre2_code *code, PCRE2_SPTR subject,
PCRE2_SIZE length, PCRE2_SIZE startoffset,
uint32_t options, pcre2_match_data *match_data,
pcre2_match_context *mcontext, pcre2_jit_stack *jit_stack);
pcre2_match_context *mcontext);
void pcre2_jit_free_unused_memory(pcre2_general_context *gcontext);
pcre2_jit_stack *pcre2_jit_stack_create(pcre2_general_context *gcontext,
PCRE2_SIZE startsize, PCRE2_SIZE maxsize);
void pcre2_jit_stack_assign(const pcre2_code *code,
void pcre2_jit_stack_assign(pcre2_match_context *mcontext,
pcre2_jit_callback callback_function, void *callback_data);
void pcre2_jit_stack_free(pcre2_jit_stack *jit_stack);
@ -1674,7 +1690,7 @@ THE MATCH DATA BLOCK
pcre2_match_data_create(uint32_t ovecsize,
pcre2_general_context *gcontext);
pcre2_match_data_create_from_pattern(pcre2_code *code,
pcre2_match_data_create_from_pattern(const pcre2_code *code,
pcre2_general_context *gcontext);
void pcre2_match_data_free(pcre2_match_data *match_data);
@ -2290,6 +2306,64 @@ EXTRACTING CAPTURED SUBSTRINGS BY NAME
causes an error at compile time.
CREATING A NEW STRING WITH SUBSTITUTIONS
int pcre2_substitute(const pcre2_code *code, PCRE2_SPTR subject,
PCRE2_SIZE length, PCRE2_SIZE startoffset,
uint32_t options, pcre2_match_data *match_data,
pcre2_match_context *mcontext, PCRE2_SPTR replacementzfP,
PCRE2_SIZE rlength, PCRE2_UCHAR *outputbufferP,
PCRE2_SIZE *outlengthptr);
This function calls pcre2_match() and then makes a copy of the subject
string in outputbuffer, replacing the part that was matched with the
replacement string, whose length is supplied in rlength. This can be
given as PCRE2_ZERO_TERMINATED for a zero-terminated string.
In the replacement string, which is interpreted as a UTF string in UTF
mode, a dollar character is an escape character that can specify the
insertion of characters from capturing groups in the pattern. The fol-
lowing forms are recognized:
$$ insert a dollar character
$<n> insert the contents of group <n>
${<n>} insert the contents of group <n>
Either a group number or a group name can be given for <n>. Curly
brackets are required only if the following character would be inter-
preted as part of the number or name. The number may be zero to include
the entire matched string. For example, if the pattern a(b)c is
matched with "[abc]" and the replacement string "+$1$0$1+", the result
is "[+babcb+]". Group insertion is done by calling pcre2_copy_byname()
or pcre2_copy_bynumber() as appropriate.
The first seven arguments of pcre2_substitute() are the same as for
pcre2_match(), except that the partial matching options are not permit-
ted, and match_data may be passed as NULL, in which case a match data
block is obtained and freed within this function, using memory manage-
ment functions from the match context, if provided, or else those that
were used to allocate memory for the compiled code.
There is one additional option, PCRE2_SUBSTITUTE_GLOBAL, which causes
the function to iterate over the subject string, replacing every match-
ing substring. If this is not set, only the first matching substring is
replaced.
The outlengthptr argument must point to a variable that contains the
length, in code units, of the output buffer. It is updated to contain
the length of the new string, excluding the trailing zero that is auto-
matically added.
The function returns the number of replacements that were made. This
may be zero if no matches were found, and is never greater than 1
unless PCRE2_SUBSTITUTE_GLOBAL is set. In the event of an error, a neg-
ative error code is returned. Except for PCRE2_ERROR_NOMATCH (which is
never returned), any errors from pcre2_match() or the substring copying
functions are passed straight back. PCRE2_ERROR_BADREPLACEMENT is
returned for an invalid replacement string (unrecognized sequence fol-
lowing a dollar sign), and PCRE2_ERROR_NOMEMORY is returned if the out-
put buffer is not big enough.
DUPLICATE SUBPATTERN NAMES
int pcre2_substring_nametable_scan(const pcre2_code *code,
@ -2527,7 +2601,7 @@ AUTHOR
REVISION
Last updated: 03 November 2014
Last updated: 11 November 2014
Copyright (c) 1997-2014 University of Cambridge.
------------------------------------------------------------------------------
@ -3553,20 +3627,21 @@ CONTROLLING THE JIT STACK
The pcre2_jit_stack_assign() function specifies which stack JIT code
should use. Its arguments are as follows:
pcre2_code *code
pcre2_match_context *mcontext
pcre2_jit_callback callback
void *data
The code argument is a pointer to a compiled pattern, after it has been
processed by pcre2_jit_compile(). There are three cases for the values
of the other two options:
The first argument is a pointer to a match context. When this is subse-
quently passed to a matching function, its information determines which
JIT stack is used. There are three cases for the values of the other
two options:
(1) If callback is NULL and data is NULL, an internal 32K block
on the machine stack is used.
(2) If callback is NULL and data is not NULL, data must be
a valid JIT stack, the result of calling pcre2_jit_stack_cre-
ate().
a pointer to a valid JIT stack, the result of calling
pcre2_jit_stack_create().
(3) If callback is not NULL, it must point to a function that is
called with data as an argument at the start of matching, in
@ -3591,11 +3666,12 @@ CONTROLLING THE JIT STACK
application is thread-safe.
Strictly speaking, even more is allowed. You can assign the same non-
NULL stack to any number of patterns as long as they are not used for
matching by multiple threads at the same time. For example, you can
assign the same stack to all compiled patterns, and use a global mutex
in the callback to wait until the stack is available for use. However,
this is an inefficient solution, and not recommended.
NULL stack to a match context that is used by any number of patterns,
as long as they are not used for matching by multiple threads at the
same time. For example, you could use the same stack in all compiled
patterns, with a global mutex in the callback to wait until the stack
is available for use. However, this is an inefficient solution, and not
recommended.
This is a suggestion for how a multithreaded program that needs to set
up non-default JIT stacks might operate:
@ -3610,9 +3686,7 @@ CONTROLLING THE JIT STACK
return thread_local_var
All the functions described in this section do nothing if JIT is not
available, and pcre2_jit_stack_assign() does nothing unless the code
argument is non-NULL and points to a pcre2_code block that has been
successfully processed by pcre2_jit_compile().
available.
JIT STACK FAQ
@ -3639,24 +3713,24 @@ JIT STACK FAQ
(3) Who "owns" a JIT stack?
The owner of the stack is the user program, not the JIT studied pattern
or anything else. The user program must ensure that if a stack is used
by pcre2_match(), (that is, it is assigned to the pattern currently
running), that stack must not be used by any other threads (to avoid
overwriting the same memory area). The best practice for multithreaded
programs is to allocate a stack for each thread, and return this stack
through the JIT callback function.
or anything else. The user program must ensure that if a stack is being
used by pcre2_match(), (that is, it is assigned to a match context that
is passed to the pattern currently running), that stack must not be
used by any other threads (to avoid overwriting the same memory area).
The best practice for multithreaded programs is to allocate a stack for
each thread, and return this stack through the JIT callback function.
(4) When should a JIT stack be freed?
You can free a JIT stack at any time, as long as it will not be used by
pcre2_match() again. When you assign the stack to a pattern, only a
pointer is set. There is no reference counting or any other magic. You
can free the patterns and stacks in any order, anytime. Just do not
call pcre2_match() with a pattern pointing to an already freed stack,
as that will cause SEGFAULT. (Also, do not free a stack currently used
by pcre2_match() in another thread). You can also replace the stack for
a pattern at any time. You can even free the previous stack before
assigning a replacement.
pcre2_match() again. When you assign the stack to a match context, only
a pointer is set. There is no reference counting or any other magic.
You can free compiled patterns, contexts, and stacks in any order, any-
time. Just do not call pcre2_match() with a match context pointing to
an already freed stack, as that will cause SEGFAULT. (Also, do not free
a stack currently used by pcre2_match() in another thread). You can
also replace the stack in a context at any time when it is not in use.
You can also free the previous stack before assigning a replacement.
(5) Should I allocate/free a stack every time before/after calling
pcre2_match()?
@ -3664,7 +3738,7 @@ JIT STACK FAQ
No, because this is too costly in terms of resources. However, you
could implement some clever idea which release the stack if it is not
used in let's say two minutes. The JIT callback can help to achieve
this without keeping a list of the currently JIT studied patterns.
this without keeping a list of patterns.
(6) OK, the stack is for long term memory allocation. But what happens
if a pattern causes stack overflow with a stack of 1M? Is that 1M kept
@ -3699,25 +3773,28 @@ FREEING JIT SPECULATIVE MEMORY
EXAMPLE CODE
This is a single-threaded example that specifies a JIT stack without
using a callback.
using a callback. A real program should include error checking after
all the function calls.
int rc;
pcre2_code *re;
pcre2_match_data *match_data;
pcre2_match_context *mcontext;
pcre2_jit_stack *jit_stack;
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0,
&errornumber, &erroffset, NULL);
/* Check for errors */
rc = pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
/* Check for errors */
mcontext = pcre2_match_context_create(NULL);
jit_stack = pcre2_jit_stack_create(NULL, 32*1024, 512*1024);
/* Check for error (NULL) */
pcre2_jit_stack_assign(re, NULL, jit_stack);
pcre2_jit_stack_assign(mcontext, NULL, jit_stack);
match_data = pcre2_match_data_create(re, 10);
rc = pcre2_match(re, subject, length, 0, 0, match_data, NULL);
/* Check results */
pcre2_free(re);
rc = pcre2_match(re, subject, length, 0, 0, match_data, mcontext);
/* Process result */
pcre2_code_free(re);
pcre2_match_data_free(match_data);
pcre2_match_context_free(mcontext);
pcre2_jit_stack_free(jit_stack);
@ -3733,14 +3810,10 @@ JIT FAST PATH API
patterns that have been successfully processed by pcre2_jit_compile()).
The fast path function is called pcre2_jit_match(), and it takes
exactly the same arguments as pcre2_match(), plus one additional argu-
ment that must either point to a JIT stack or be NULL. In the latter
case, if a callback function has been set up by
pcre2_jit_stack_assign(), it is called. Otherwise the system stack is
used. The return values are the same as for pcre2_match(), plus
PCRE2_ERROR_JIT_BADOPTION if a matching mode (partial or complete) is
requested that was not compiled. Unsupported option bits (for example,
PCRE2_ANCHORED) are ignored.
exactly the same arguments as pcre2_match(). The return values are also
the same, plus PCRE2_ERROR_JIT_BADOPTION if a matching mode (partial or
complete) is requested that was not compiled. Unsupported option bits
(for example, PCRE2_ANCHORED) are ignored.
When you call pcre2_match(), as well as testing for invalid options, a
number of other sanity checks are performed on the arguments. For exam-
@ -3767,7 +3840,7 @@ AUTHOR
REVISION
Last updated: 03 November 2014
Last updated: 08 November 2014
Copyright (c) 1997-2014 University of Cambridge.
------------------------------------------------------------------------------

73
doc/pcre2_substitute.3 Normal file
View File

@ -0,0 +1,73 @@
.TH PCRE2_SUBSTITUTE 3 "11 November 2014" "PCRE2 10.00"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.SH SYNOPSIS
.rs
.sp
.B #include <pcre2.h>
.PP
.nf
.B int pcre2_substitute(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
.B " PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
.B " uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
.B " pcre2_match_context *\fImcontext\fP, PCRE2_SPTR \fIreplacementzfP,"
.B " PCRE2_SIZE \fIrlength\fP, PCRE2_UCHAR *\fIoutputbuffer\fP,"
.B " PCRE2_SIZE *\fIoutlengthptr\fP);"
.fi
.
.SH DESCRIPTION
.rs
.sp
This function matches a compiled regular expression against a given subject
string, using a matching algorithm that is similar to Perl's. It then makes a
copy of the subject, substituting a replacement string for what was matched.
Its arguments are:
.sp
\fIcode\fP Points to the compiled pattern
\fIsubject\fP Points to the subject string
\fIlength\fP Length of the subject string
\fIstartoffset\fP Offset in the subject at which to start matching
\fIoptions\fP Option bits
\fImatch_data\fP Points to a match data block, or is NULL
\fImcontext\fP Points to a match context, or is NULL
\fIreplacement\fP Points to the replacement string
\fIrlength\fP Length of the replacement string
\fIoutputbuffer\fP Points to the output buffer
\fIoutlengthptr\fP Points to the length of the output buffer
.sp
A match context is needed only if you want to:
.sp
Set up a callout function
Change the limit for calling the internal function \fImatch()\fP
Change the limit for calling \fImatch()\fP recursively
Set custom memory management when the heap is used for recursion
.sp
The \fIlength\fP, \fIstartoffset\fP and \fIrlength\fP values are code
units, not characters, as is the contents of the variable pointed at by
\fIoutlengthptr\fP, which is updated to the actual length of the new string.
The options are:
.sp
PCRE2_ANCHORED Match only at the first position
PCRE2_NOTBOL Subject string is not the beginning of a line
PCRE2_NOTEOL Subject string is not the end of a line
PCRE2_NOTEMPTY An empty string is not a valid match
PCRE2_NOTEMPTY_ATSTART An empty string at the start of the subject
is not a valid match
PCRE2_NO_UTF_CHECK Do not check the subject or replacement for
UTF validity (only relevant if PCRE2_UTF
was set at compile time)
PCRE2_SUBSTITUTE_GLOBAL Replace all occurrences in the subject
.sp
The function returns the number of substitutions, which may be zero if there
were no matches. The result can be greater than one only when
PCRE2_SUBSTITUTE_GLOBAL is set.
.P
There is a complete description of the PCRE2 native API in the
.\" HREF
\fBpcre2api\fP
.\"
page and a description of the POSIX API in the
.\" HREF
\fBpcre2posix\fP
.\"
page.

View File

@ -1,4 +1,4 @@
.TH PCRE2API 3 "08 November 2014" "PCRE2 10.00"
.TH PCRE2API 3 "11 November 2014" "PCRE2 10.00"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.sp
@ -169,6 +169,19 @@ document for an overview of all the PCRE2 documentation.
.fi
.
.
.SH "PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION"
.rs
.sp
.nf
.B int pcre2_substitute(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
.B " PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
.B " uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
.B " pcre2_match_context *\fImcontext\fP, PCRE2_SPTR \fIreplacementzfP,"
.B " PCRE2_SIZE \fIrlength\fP, PCRE2_UCHAR *\fIoutputbuffer\fP,"
.B " PCRE2_SIZE *\fIoutlengthptr\fP);"
.fi
.
.
.SH "PCRE2 NATIVE API JIT FUNCTIONS"
.rs
.sp
@ -332,8 +345,8 @@ and disadvantages is given in the
documentation. There is no JIT support for \fBpcre2_dfa_match()\fP.
.P
In addition to the main compiling and matching functions, there are convenience
functions for extracting captured substrings from a subject string that is
matched by \fBpcre2_match()\fP. They are:
functions for extracting captured substrings from a subject string that has
been matched by \fBpcre2_match()\fP. They are:
.sp
\fBpcre2_substring_copy_byname()\fP
\fBpcre2_substring_copy_bynumber()\fP
@ -348,9 +361,13 @@ matched by \fBpcre2_match()\fP. They are:
\fBpcre2_substring_free()\fP and \fBpcre2_substring_list_free()\fP are also
provided, to free the memory used for extracted strings.
.P
There are functions for finding out information about a compiled pattern
(\fBpcre2_pattern_info()\fP) and about the configuration with which PCRE2 was
built (\fBpcre2_config()\fP).
The function \fBpcre2_substitute()\fP can be called to match a pattern and
return a copy of the subject string with substitutions for parts that were
matched.
.P
Finally, there are functions for finding out information about a compiled
pattern (\fBpcre2_pattern_info()\fP) and about the configuration with which
PCRE2 was built (\fBpcre2_config()\fP).
.
.
.\" HTML <a name="newlines"></a>
@ -2361,6 +2378,66 @@ numbers. For this reason, the use of different names for subpatterns of the
same number causes an error at compile time.
.
.
.SH "CREATING A NEW STRING WITH SUBSTITUTIONS"
.rs
.sp
.nf
.B int pcre2_substitute(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
.B " PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
.B " uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
.B " pcre2_match_context *\fImcontext\fP, PCRE2_SPTR \fIreplacementzfP,"
.B " PCRE2_SIZE \fIrlength\fP, PCRE2_UCHAR *\fIoutputbuffer\zfP,"
.B " PCRE2_SIZE *\fIoutlengthptr\fP);"
.fi
This function calls \fBpcre2_match()\fP and then makes a copy of the subject
string in \fIoutputbuffer\fP, replacing the part that was matched with the
\fIreplacement\fP string, whose length is supplied in \fBrlength\fP. This can
be given as PCRE2_ZERO_TERMINATED for a zero-terminated string.
.P
In the replacement string, which is interpreted as a UTF string in UTF mode,
and is checked for UTF validity unless the PCRE2_NO_UTF_CHECK option is set, a
dollar character is an escape character that can specify the insertion of
characters from capturing groups in the pattern. The following forms are
recognized:
.sp
$$ insert a dollar character
$<n> insert the contents of group <n>
${<n>} insert the contents of group <n>
.sp
Either a group number or a group name can be given for <n>. Curly brackets are
required only if the following character would be interpreted as part of the
number or name. The number may be zero to include the entire matched string.
For example, if the pattern a(b)c is matched with "[abc]" and the replacement
string "+$1$0$1+", the result is "[+babcb+]". Group insertion is done by
calling \fBpcre2_copy_byname()\fP or \fBpcre2_copy_bynumber()\fP as
appropriate.
.P
The first seven arguments of \fBpcre2_substitute()\fP are the same as for
\fBpcre2_match()\fP, except that the partial matching options are not
permitted, and \fImatch_data\fP may be passed as NULL, in which case a match
data block is obtained and freed within this function, using memory management
functions from the match context, if provided, or else those that were used to
allocate memory for the compiled code.
.P
There is one additional option, PCRE2_SUBSTITUTE_GLOBAL, which causes the
function to iterate over the subject string, replacing every matching
substring. If this is not set, only the first matching substring is replaced.
.P
The \fIoutlengthptr\fP argument must point to a variable that contains the
length, in code units, of the output buffer. It is updated to contain the
length of the new string, excluding the trailing zero that is automatically
added.
.P
The function returns the number of replacements that were made. This may be
zero if no matches were found, and is never greater than 1 unless
PCRE2_SUBSTITUTE_GLOBAL is set. In the event of an error, a negative error code
is returned. Except for PCRE2_ERROR_NOMATCH (which is never returned), any
errors from \fBpcre2_match()\fP or the substring copying functions are passed
straight back. PCRE2_ERROR_BADREPLACEMENT is returned for an invalid
replacement string (unrecognized sequence following a dollar sign), and
PCRE2_ERROR_NOMEMORY is returned if the output buffer is not big enough.
.
.
.SH "DUPLICATE SUBPATTERN NAMES"
.rs
.sp
@ -2633,6 +2710,6 @@ Cambridge CB2 3QH, England.
.rs
.sp
.nf
Last updated: 08 November 2014
Last updated: 11 November 2014
Copyright (c) 1997-2014 University of Cambridge.
.fi

View File

@ -435,7 +435,6 @@ PATTERN MODIFIERS
posix use the POSIX API
stackguard=<number> test the stackguard feature
tables=[0|1|2] select internal tables
use_length use the pattern's length
The effects of these modifiers are described in the following sections.
FIXME: Give more examples.
@ -480,13 +479,10 @@ PATTERN MODIFIERS
/ab 32 59/hex
This feature is provided as a way of creating patterns that contain
binary zero characters. When hex is set, it implies use_length.
Using the pattern's length
By default, pcre2test passes patterns as zero-terminated strings to
pcre2_compile(), giving the length as -1. If use_length is set, the
length of the pattern is passed. This is implied if hex is set.
binary zero characters. By default, pcre2test passes patterns as zero-
terminated strings to pcre2_compile(), giving the length as
PCRE2_ZERO_TERMINATED. However, for patterns specified in hexadecimal,
the length of the pattern is passed.
JIT compilation
@ -676,6 +672,7 @@ SUBJECT MODIFIERS
ovector=<n> set size of output vector
recursion_limit=<n> set a recursion limit
startchar show startchar when relevant
zero_terminate pass the subject as zero-terminated
The effects of these modifiers are described in the following sections.
FIXME: Give more examples.
@ -861,6 +858,18 @@ SUBJECT MODIFIERS
match block with a zero-length ovector; there is always one pair of
offsets.)
Passing the subject as zero-terminated
By default, the subject string is passed to a native API matching func-
tion with its correct length. In order to test the facility for passing
a zero-terminated string, the zero_terminate modifier is provided. It
causes the length to be passed as PCRE2_ZERO_TERMINATED. (When matching
via the POSIX interface, this modifier has no effect, as there is no
facility for passing a length.)
When testing pcre2_substitute, this modifier also has the effect of
passing the replacement string as zero-terminated.
THE ALTERNATIVE MATCHING FUNCTION
@ -1103,5 +1112,5 @@ AUTHOR
REVISION
Last updated: 02 November 2014
Last updated: 09 November 2014
Copyright (c) 1997-2014 University of Cambridge.

View File

@ -67,8 +67,8 @@ Arguments:
buffer where to put the substituted string
blength points to length of buffer; updated to length of string
Returns: > 0 number of substitutions made
< 0 an error code, including PCRE2_ERROR_NOMATCH if no match
Returns: >= 0 number of substitutions made
< 0 an error code
PCRE2_ERROR_BADREPLACEMENT means invalid use of $
*/
@ -78,8 +78,8 @@ pcre2_substitute(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
pcre2_match_context *mcontext, PCRE2_SPTR replacement, PCRE2_SIZE rlength,
PCRE2_UCHAR *buffer, PCRE2_SIZE *blength)
{
int rc = 0;
int subs = 0;
int rc;
int subs;
uint32_t ovector_count;
uint32_t goptions = 0;
BOOL match_data_created = FALSE;
@ -106,6 +106,21 @@ if (match_data == NULL)
ovector = pcre2_get_ovector_pointer(match_data);
ovector_count = pcre2_get_ovector_count(match_data);
/* Check UTF replacement string if necessary. */
#ifdef SUPPORT_UNICODE
if ((code->overall_options & PCRE2_UTF) != 0 &&
(options & PCRE2_NO_UTF_CHECK) == 0)
{
rc = PRIV(valid_utf)(replacement, rlength, &(match_data->rightchar));
if (rc != 0)
{
match_data->leftchar = 0;
goto EXIT;
}
}
#endif /* SUPPORT_UNICODE */
/* Notice the global option and remove it from the options that are passed to
pcre2_match(). */
@ -129,6 +144,7 @@ lengthleft = *blength - start_offset;
/* Loop for global substituting. */
subs = 0;
do
{
PCRE2_SIZE i;
@ -273,6 +289,7 @@ buffer[buff_offset] = 0;
EXIT:
if (match_data_created) pcre2_match_data_free(match_data);
else match_data->rc = rc;
return rc;
NOROOM:

View File

@ -1547,7 +1547,7 @@ the three different cases. */
#define PCRE2_GET_STARTCHAR(a,b) a = pcre2_get_startchar_16(G(b,16))
#define PCRE2_JIT_COMPILE(a,b) pcre2_jit_compile_16(G(a,16),b)
#define PCRE2_JIT_FREE_UNUSED_MEMORY(a) pcre2_jit_free_unused_memory_16(G(a,16))
#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h,i) \
#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h) \
a = pcre2_jit_match_16(G(b,16),(PCRE2_SPTR16)c,d,e,f,G(g,16),G(h,16))
#define PCRE2_JIT_STACK_CREATE(a,b,c,d) \
a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_16(b,c,d);
@ -1628,7 +1628,7 @@ the three different cases. */
#define PCRE2_GET_STARTCHAR(a,b) a = pcre2_get_startchar_32(G(b,32))
#define PCRE2_JIT_COMPILE(a,b) pcre2_jit_compile_32(G(a,32),b)
#define PCRE2_JIT_FREE_UNUSED_MEMORY(a) pcre2_jit_free_unused_memory_32(G(a,32))
#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h,i) \
#define PCRE2_JIT_MATCH(a,b,c,d,e,f,g,h) \
a = pcre2_jit_match_32(G(b,32),(PCRE2_SPTR32)c,d,e,f,G(g,32),G(h,32))
#define PCRE2_JIT_STACK_CREATE(a,b,c,d) \
a = (PCRE2_JIT_STACK *)pcre2_jit_stack_create_32(b,c,d);