From b3ac0ffb322f7f3bb36dfb31adc52e10f828566b Mon Sep 17 00:00:00 2001 From: "Philip.Hazel" Date: Tue, 11 Nov 2014 16:51:07 +0000 Subject: [PATCH] Substitute code update and documentation. --- Makefile.am | 2 + doc/html/index.html | 4 + doc/html/pcre2_config.html | 4 +- doc/html/pcre2_jit_match.html | 8 +- doc/html/pcre2_jit_stack_assign.html | 14 +- .../pcre2_match_data_create_from_pattern.html | 2 +- doc/html/pcre2_substitute.html | 85 ++++ doc/html/pcre2api.html | 218 ++++++--- doc/html/pcre2jit.html | 90 ++-- doc/html/pcre2test.html | 31 +- doc/index.html.src | 4 + doc/pcre2.txt | 459 ++++++++++-------- doc/pcre2_jit_stack_assign.3 | 2 +- doc/pcre2_substitute.3 | 73 +++ doc/pcre2api.3 | 95 +++- doc/pcre2jit.3 | 14 +- doc/pcre2test.1 | 4 +- doc/pcre2test.txt | 387 +++++++-------- src/pcre2_substitute.c | 27 +- src/pcre2_substring.c | 2 +- src/pcre2test.c | 4 +- 21 files changed, 978 insertions(+), 551 deletions(-) create mode 100644 doc/html/pcre2_substitute.html create mode 100644 doc/pcre2_substitute.3 diff --git a/Makefile.am b/Makefile.am index 8e558d1..7766365 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/doc/html/index.html b/doc/html/index.html index 13eeef6..f825066 100644 --- a/doc/html/index.html +++ b/doc/html/index.html @@ -204,6 +204,10 @@ in the library. pcre2_set_recursion_memory_management   Set match recursion memory management +pcre2_substitute +   Match a compiled pattern to a subject string and do + substitutions + pcre2_substring_copy_byname   Extract named substring into given buffer diff --git a/doc/html/pcre2_config.html b/doc/html/pcre2_config.html index cf5320d..d65d70d 100644 --- a/doc/html/pcre2_config.html +++ b/doc/html/pcre2_config.html @@ -70,8 +70,8 @@ available codes are: 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.

diff --git a/doc/html/pcre2_jit_match.html b/doc/html/pcre2_jit_match.html index ab58854..1d59667 100644 --- a/doc/html/pcre2_jit_match.html +++ b/doc/html/pcre2_jit_match.html @@ -22,7 +22,7 @@ SYNOPSIS 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);


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 pcre2_match() applies. Its arguments are exactly the same as for -pcre2_match() -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 -pcre2_jit_stack_create(), it is called. Otherwise the system stack is -used. +pcre2_match().

The supported options are PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY, diff --git a/doc/html/pcre2_jit_stack_assign.html b/doc/html/pcre2_jit_stack_assign.html index 95e81b3..e7e3a6a 100644 --- a/doc/html/pcre2_jit_stack_assign.html +++ b/doc/html/pcre2_jit_stack_assign.html @@ -19,18 +19,20 @@ SYNOPSIS #include <pcre2.h>

-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);


DESCRIPTION

-This function provides control over the memory used as a stack at run-time by a -call to pcre2_match() or pcre2_jit_match() 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 pcre2_match() or pcre2_jit_match() 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:

-  code           the pointer returned by pcre2_compile()
+  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
 
@@ -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 pcre2_jit_stack_create().

-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 pcre2jit diff --git a/doc/html/pcre2_match_data_create_from_pattern.html b/doc/html/pcre2_match_data_create_from_pattern.html index 2d58232..993da70 100644 --- a/doc/html/pcre2_match_data_create_from_pattern.html +++ b/doc/html/pcre2_match_data_create_from_pattern.html @@ -19,7 +19,7 @@ SYNOPSIS #include <pcre2.h>

-pcre2_match_data_create_from_pattern(pcre2_code *code, +pcre2_match_data_create_from_pattern(const pcre2_code *code, pcre2_general_context *gcontext);


diff --git a/doc/html/pcre2_substitute.html b/doc/html/pcre2_substitute.html new file mode 100644 index 0000000..6e0433d --- /dev/null +++ b/doc/html/pcre2_substitute.html @@ -0,0 +1,85 @@ + + +pcre2_substitute specification + + +

pcre2_substitute man page

+

+Return to the PCRE2 index page. +

+

+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. +
+
+SYNOPSIS +
+

+#include <pcre2.h> +

+

+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 \fIreplacementzfP, + PCRE2_SIZE rlength, PCRE2_UCHAR *outputbuffer, + PCRE2_SIZE *outlengthptr); +

+
+DESCRIPTION +
+

+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: +

+  code          Points to the compiled pattern
+  subject       Points to the subject string
+  length        Length of the subject string
+  startoffset   Offset in the subject at which to start matching
+  options       Option bits
+  match_data    Points to a match data block, or is NULL
+  mcontext      Points to a match context, or is NULL
+  replacement   Points to the replacement string
+  rlength       Length of the replacement string
+  outputbuffer  Points to the output buffer
+  outlengthptr  Points to the length of the output buffer
+
+A match context is needed only if you want to: +
+  Set up a callout function
+  Change the limit for calling the internal function match()
+  Change the limit for calling match() recursively
+  Set custom memory management when the heap is used for recursion
+
+The length, startoffset and rlength values are code +units, not characters, as is the contents of the variable pointed at by +outlengthptr, which is updated to the actual length of the new string. +The options are: +
+  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
+
+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. +

+

+There is a complete description of the PCRE2 native API in the +pcre2api +page and a description of the POSIX API in the +pcre2posix +page. +

+Return to the PCRE2 index page. +

diff --git a/doc/html/pcre2api.html b/doc/html/pcre2api.html index 4545948..e3fb95f 100644 --- a/doc/html/pcre2api.html +++ b/doc/html/pcre2api.html @@ -19,32 +19,34 @@ please consult the man page, in case the conversion went wrong.
  • PCRE2 NATIVE API COMPILE CONTEXT FUNCTIONS
  • PCRE2 NATIVE API MATCH CONTEXT FUNCTIONS
  • PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS -
  • PCRE2 NATIVE API JIT FUNCTIONS -
  • PCRE2 NATIVE API AUXILIARY FUNCTIONS -
  • PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES -
  • PCRE2 API OVERVIEW -
  • NEWLINES -
  • MULTITHREADING -
  • PCRE2 CONTEXTS -
  • CHECKING BUILD-TIME OPTIONS -
  • COMPILING A PATTERN -
  • COMPILATION ERROR CODES -
  • JUST-IN-TIME (JIT) COMPILATION -
  • LOCALE SUPPORT -
  • INFORMATION ABOUT A COMPILED PATTERN -
  • THE MATCH DATA BLOCK -
  • MATCHING A PATTERN: THE TRADITIONAL FUNCTION -
  • NEWLINE HANDLING WHEN MATCHING -
  • HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS -
  • EXTRACTING CAPTURED SUBSTRINGS BY NUMBER -
  • EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS -
  • EXTRACTING CAPTURED SUBSTRINGS BY NAME -
  • DUPLICATE SUBPATTERN NAMES -
  • FINDING ALL POSSIBLE MATCHES -
  • MATCHING A PATTERN: THE ALTERNATIVE FUNCTION -
  • SEE ALSO -
  • AUTHOR -
  • REVISION +
  • PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION +
  • PCRE2 NATIVE API JIT FUNCTIONS +
  • PCRE2 NATIVE API AUXILIARY FUNCTIONS +
  • PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES +
  • PCRE2 API OVERVIEW +
  • NEWLINES +
  • MULTITHREADING +
  • PCRE2 CONTEXTS +
  • CHECKING BUILD-TIME OPTIONS +
  • COMPILING A PATTERN +
  • COMPILATION ERROR CODES +
  • JUST-IN-TIME (JIT) COMPILATION +
  • LOCALE SUPPORT +
  • INFORMATION ABOUT A COMPILED PATTERN +
  • THE MATCH DATA BLOCK +
  • MATCHING A PATTERN: THE TRADITIONAL FUNCTION +
  • NEWLINE HANDLING WHEN MATCHING +
  • HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS +
  • EXTRACTING CAPTURED SUBSTRINGS BY NUMBER +
  • EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS +
  • EXTRACTING CAPTURED SUBSTRINGS BY NAME +
  • CREATING A NEW STRING WITH SUBSTITUTIONS +
  • DUPLICATE SUBPATTERN NAMES +
  • FINDING ALL POSSIBLE MATCHES +
  • MATCHING A PATTERN: THE ALTERNATIVE FUNCTION +
  • SEE ALSO +
  • AUTHOR +
  • REVISION

    #include <pcre2.h> @@ -69,7 +71,7 @@ document for an overview of all the PCRE2 documentation. 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);

    @@ -222,7 +224,16 @@ document for an overview of all the PCRE2 documentation. int pcre2_substring_list_get(pcre2_match_data *match_data, " PCRE2_UCHAR ***listptr, PCRE2_SIZE **lengthsptr);

    -
    PCRE2 NATIVE API JIT FUNCTIONS
    +
    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 \fIreplacementzfP, + PCRE2_SIZE rlength, PCRE2_UCHAR *outputbuffer, + PCRE2_SIZE *outlengthptr); +

    +
    PCRE2 NATIVE API JIT FUNCTIONS

    int pcre2_jit_compile(pcre2_code *code, uint32_t options);
    @@ -230,7 +241,7 @@ document for an overview of all the PCRE2 documentation. 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); @@ -240,13 +251,13 @@ document for an overview of all the PCRE2 documentation. 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);

    -
    PCRE2 NATIVE API AUXILIARY FUNCTIONS
    +
    PCRE2 NATIVE API AUXILIARY FUNCTIONS

    int pcre2_get_error_message(int errorcode, PCRE2_UCHAR *buffer, PCRE2_SIZE bufflen); @@ -260,7 +271,7 @@ document for an overview of all the PCRE2 documentation.
    int pcre2_config(uint32_t what, void *where);

    -
    PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES
    +
    PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES

    There are three PCRE2 libraries, supporting 8-bit, 16-bit, and 32-bit code units, respectively. However, there is just one header file, pcre2.h. @@ -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.

    -
    PCRE2 API OVERVIEW
    +
    PCRE2 API OVERVIEW

    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 pcre2_dfa_match().

    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: +functions for extracting captured substrings from a subject string that has +been matched by pcre2_match(). They are:

       pcre2_substring_copy_byname()
       pcre2_substring_copy_bynumber()
    @@ -403,11 +414,16 @@ matched by pcre2_match(). They are:
     provided, to free the memory used for extracted strings.
     

    -There are functions for finding out information about a compiled pattern -(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 compiled +pattern (pcre2_pattern_info()) and about the configuration with which +PCRE2 was built (pcre2_config()).

    -
    NEWLINES
    +
    NEWLINES

    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.

    -
    MULTITHREADING
    +
    MULTITHREADING

    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.

    -
    PCRE2 CONTEXTS
    +
    PCRE2 CONTEXTS

    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.

    -
    CHECKING BUILD-TIME OPTIONS
    +
    CHECKING BUILD-TIME OPTIONS

    int pcre2_config(uint32_t what, void *where);

    @@ -809,8 +825,9 @@ units long. (The exact length needed can be found by calling pcre2_config() with where 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.
       PCRE2_CONFIG_LINKSIZE
     
    @@ -878,8 +895,8 @@ units long. (The exact length needed can be found by calling pcre2_config() with where 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.
       PCRE2_CONFIG_UNICODE
     
    @@ -891,10 +908,11 @@ otherwise it is set to zero. Unicode support implies UTF support. 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_config() with where 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.

    -
    COMPILING A PATTERN
    +
    COMPILING A PATTERN

    pcre2_code *pcre2_compile(PCRE2_SPTR pattern, PCRE2_SIZE length, uint32_t options, int *errorcode, PCRE2_SIZE *erroroffset, @@ -1248,7 +1266,7 @@ of how this option changes the behaviour of PCRE2 are given in the pcre2unicode page.

    -
    COMPILATION ERROR CODES
    +
    COMPILATION ERROR CODES

    There are over 80 positive error codes that pcre2_compile() 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 pcre2_get_error_message() function can be called to obtain a textual error message from any error code.

    -
    JUST-IN-TIME (JIT) COMPILATION
    +
    JUST-IN-TIME (JIT) COMPILATION

    int pcre2_jit_compile(pcre2_code *code, uint32_t options);
    @@ -1266,7 +1284,7 @@ textual error message from any error code. 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); @@ -1276,7 +1294,7 @@ textual error message from any error code. 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);

    @@ -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.

    -
    LOCALE SUPPORT
    +
    LOCALE SUPPORT

    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.

    -
    INFORMATION ABOUT A COMPILED PATTERN
    +
    INFORMATION ABOUT A COMPILED PATTERN

    int pcre2_pattern_info(const pcre2 *code, uint32_t what, void *where);

    @@ -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 pcre2_code structure. Processing a pattern with the JIT compiler does not alter the value returned by this option.

    -
    THE MATCH DATA BLOCK
    +
    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);

    @@ -1690,7 +1708,7 @@ and other match data below.

    -
    MATCHING A PATTERN: THE TRADITIONAL FUNCTION
    +
    MATCHING A PATTERN: THE TRADITIONAL FUNCTION

    int pcre2_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length, PCRE2_SIZE startoffset, @@ -1902,7 +1920,7 @@ examples, in the pcre2partial documentation.

    -
    NEWLINE HANDLING WHEN MATCHING
    +
    NEWLINE HANDLING WHEN MATCHING

    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.

    -
    HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS
    +
    HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS

    uint32_t pcre2_get_ovector_count(pcre2_match_data *match_data);
    @@ -2167,7 +2185,7 @@ time.

    The internal recursion limit was reached.

    -
    EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
    +
    EXTRACTING CAPTURED SUBSTRINGS BY NUMBER

    int pcre2_substring_length_bynumber(pcre2_match_data *match_data, unsigned int number, PCRE2_SIZE *length); @@ -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.

    -
    EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS
    +
    EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS

    int pcre2_substring_list_get(pcre2_match_data *match_data, " PCRE2_UCHAR ***listptr, PCRE2_SIZE **lengthsptr); @@ -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.

    -
    EXTRACTING CAPTURED SUBSTRINGS BY NAME
    +
    EXTRACTING CAPTURED SUBSTRINGS BY NAME

    int pcre2_substring_number_from_name(const pcre2_code *code, PCRE2_SPTR name); @@ -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.

    -
    DUPLICATE SUBPATTERN NAMES
    +
    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 \fIreplacementzfP, + PCRE2_SIZE rlength, PCRE2_UCHAR *\fIoutputbuffer\zfP, + 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 following 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 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 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 +permitted, and match_data 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. +

    +

    +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. +

    +

    +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 automatically +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 negative 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 following a dollar sign), and +PCRE2_ERROR_NOMEMORY is returned if the output buffer is not big enough. +

    +
    DUPLICATE SUBPATTERN NAMES

    int pcre2_substring_nametable_scan(const pcre2_code *code, PCRE2_SPTR name, PCRE2_SPTR *first, PCRE2_SPTR *last); @@ -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.

    -
    FINDING ALL POSSIBLE MATCHES
    +
    FINDING ALL POSSIBLE MATCHES

    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 pcre2_match() to backtrack and try other alternatives. Ultimately, when it runs out of matches, pcre2_match() will yield PCRE2_ERROR_NOMATCH.

    -
    MATCHING A PATTERN: THE ALTERNATIVE FUNCTION
    +
    MATCHING A PATTERN: THE ALTERNATIVE FUNCTION

    int pcre2_dfa_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length, PCRE2_SIZE startoffset, @@ -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.

    -
    SEE ALSO
    +
    SEE ALSO

    pcre2build(3), pcre2libs(3), pcre2callout(3), pcre2matching(3), pcre2partial(3), pcre2posix(3), pcre2demo(3), pcre2sample(3), pcre2stack(3).

    -
    AUTHOR
    +
    AUTHOR

    Philip Hazel
    @@ -2577,9 +2655,9 @@ University Computing Service Cambridge CB2 3QH, England.

    -
    REVISION
    +
    REVISION

    -Last updated: 03 November 2014 +Last updated: 11 November 2014
    Copyright © 1997-2014 University of Cambridge.
    diff --git a/doc/html/pcre2jit.html b/doc/html/pcre2jit.html index fcd7cb6..0ab3876 100644 --- a/doc/html/pcre2jit.html +++ b/doc/html/pcre2jit.html @@ -178,19 +178,20 @@ pattern. The pcre2_jit_stack_assign() function specifies which stack JIT code should use. Its arguments are as follows:

    -  pcre2_code         *code
    -  pcre2_jit_callback  callback
    -  void               *data
    +  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 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:
       (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_create().
    +      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
    @@ -215,11 +216,11 @@ each thread so that the 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. +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 @@ -234,10 +235,7 @@ non-default JIT stacks might operate: Use a one-line callback function 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(). +All the functions described in this section do nothing if JIT is not available.


    JIT STACK FAQ

    @@ -267,26 +265,26 @@ grow up to 1M anytime if needed.

    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. +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 +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 -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. +compiled patterns, contexts, and stacks in any order, anytime. Just \fIdo +not\fP 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 @@ -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.

    (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.
    EXAMPLE CODE

    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.

       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);
     
     
    @@ -369,13 +370,10 @@ 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 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 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. +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 @@ -404,7 +402,7 @@ Cambridge CB2 3QH, England.


    REVISION

    -Last updated: 03 November 2014 +Last updated: 08 November 2014
    Copyright © 1997-2014 University of Cambridge.
    diff --git a/doc/html/pcre2test.html b/doc/html/pcre2test.html index 527a609..0cae3b2 100644 --- a/doc/html/pcre2test.html +++ b/doc/html/pcre2test.html @@ -488,7 +488,6 @@ about the pattern: 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. @@ -542,15 +541,10 @@ pairs. For example: /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. +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 @@ -766,6 +760,7 @@ pattern. 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. @@ -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.)

    +
    +Passing the subject as zero-terminated +
    +

    +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 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

    By default, pcre2test uses the standard PCRE2 matching function, @@ -1224,7 +1233,7 @@ Cambridge CB2 3QH, England.


    REVISION

    -Last updated: 02 November 2014 +Last updated: 09 November 2014
    Copyright © 1997-2014 University of Cambridge.
    diff --git a/doc/index.html.src b/doc/index.html.src index 355cf34..d3ba963 100644 --- a/doc/index.html.src +++ b/doc/index.html.src @@ -204,6 +204,10 @@ in the library. pcre2_set_recursion_memory_management   Set match recursion memory management +pcre2_substitute +   Match a compiled pattern to a subject string and do + substitutions + pcre2_substring_copy_byname   Extract named substring into given buffer diff --git a/doc/pcre2.txt b/doc/pcre2.txt index fd12375..e433cb0 100644 --- a/doc/pcre2.txt +++ b/doc/pcre2.txt @@ -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,33 +876,34 @@ 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 - The output is an integer that contains the number of bytes used for + The output is an integer that contains the number of bytes used for internal linkage in compiled regular expressions. When PCRE2 is config- - ured, the value can be set to 2, 3, or 4, with the default being 2. + ured, the value can be set to 2, 3, or 4, with the default being 2. This is the value that is returned by pcre2_config(). However, when the - 16-bit library is compiled, a value of 3 is rounded up to 4, and when - the 32-bit library is compiled, internal linkages always use 4 bytes, + 16-bit library is compiled, a value of 3 is rounded up to 4, and when + the 32-bit library is compiled, internal linkages always use 4 bytes, so the configured value is not relevant. The default value of 2 for the 8-bit and 16-bit libraries is sufficient - for all but the most massive patterns, since it allows the size of the + for all but the most massive patterns, since it allows the size of the compiled pattern to be up to 64K code units. Larger values allow larger - regular expressions to be compiled by those two libraries, but at the + regular expressions to be compiled by those two libraries, but at the expense of slower matching. PCRE2_CONFIG_MATCHLIMIT The output is an unsigned long integer that gives the default limit for - the number of internal matching function calls in a pcre2_match() exe- + the number of internal matching function calls in a pcre2_match() exe- cution. Further details are given with pcre2_match() below. PCRE2_CONFIG_NEWLINE - The output is an integer whose value specifies the default character + The output is an integer whose value specifies the default character sequence that is recognized as meaning "newline". The values are: 1 Carriage return (CR) @@ -897,57 +912,58 @@ CHECKING BUILD-TIME OPTIONS 4 Any Unicode line ending 5 Any of CR, LF, or CRLF - The default should normally correspond to the standard sequence for + The default should normally correspond to the standard sequence for your operating system. PCRE2_CONFIG_PARENSLIMIT - The output is an unsigned long integer that gives the maximum depth of - nesting of parentheses (of any kind) in a pattern. This limit is - imposed to cap the amount of system stack used when a pattern is com- - piled. It is specified when PCRE2 is built; the default is 250. This - limit does not take into account the stack that may already be used by - the calling application. For finer control over compilation stack + The output is an unsigned long integer that gives the maximum depth of + nesting of parentheses (of any kind) in a pattern. This limit is + imposed to cap the amount of system stack used when a pattern is com- + piled. It is specified when PCRE2 is built; the default is 250. This + limit does not take into account the stack that may already be used by + the calling application. For finer control over compilation stack usage, see pcre2_set_compile_recursion_guard(). PCRE2_CONFIG_RECURSIONLIMIT The output is an unsigned long integer that gives the default limit for the depth of recursion when calling the internal matching function in a - pcre2_match() execution. Further details are given with pcre2_match() + pcre2_match() execution. Further details are given with pcre2_match() below. PCRE2_CONFIG_STACKRECURSE - The output is an integer that is set to one if internal recursion when - running pcre2_match() is implemented by recursive function calls that - use the system stack to remember their state. This is the usual way + The output is an integer that is set to one if internal recursion when + running pcre2_match() is implemented by recursive function calls that + use the system stack to remember their state. This is the usual way that PCRE2 is compiled. The output is zero if PCRE2 was compiled to use blocks of data on the heap instead of recursive function calls. PCRE2_CONFIG_UNICODE_VERSION - The where argument should point to a buffer that is at least 24 code + The where argument should point to a buffer that is at least 24 code units long. (The exact length needed can be found by calling pcre2_con- - 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. + 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 number of code units used is returned. This is the + length of the string plus one unit for the terminating zero. PCRE2_CONFIG_UNICODE - The output is an integer that is set to one if Unicode support is - available; otherwise it is set to zero. Unicode support implies UTF + The output is an integer that is set to one if Unicode support is + available; otherwise it is set to zero. Unicode support implies UTF support. PCRE2_CONFIG_VERSION - The where argument should point to a buffer that is at least 12 code + 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,59 +2306,117 @@ 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 + $ insert the contents of group + ${} insert the contents of group + + Either a group number or a group name can be given for . 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, PCRE2_SPTR name, PCRE2_SPTR *first, PCRE2_SPTR *last); - When a pattern is compiled with the PCRE2_DUPNAMES option, names for - subpatterns are not required to be unique. Duplicate names are always - allowed for subpatterns with the same number, created by using the (?| - feature. Indeed, if such subpatterns are named, they are required to + When a pattern is compiled with the PCRE2_DUPNAMES option, names for + subpatterns are not required to be unique. Duplicate names are always + allowed for subpatterns with the same number, created by using the (?| + feature. Indeed, if such subpatterns are named, they are required to use the same names. Normally, patterns with duplicate names are such that in any one match, - only one of the named subpatterns participates. An example is shown in + only one of the named subpatterns participates. An example is shown in the pcre2pattern documentation. - When duplicates are present, pcre2_substring_copy_byname() and - pcre2_substring_get_byname() return the first substring corresponding + When duplicates are present, pcre2_substring_copy_byname() and + pcre2_substring_get_byname() return the first substring corresponding to the given name that is set. If none are set, PCRE2_ERROR_NOSUBSTRING - is returned. The pcre2_substring_number_from_name() function returns - one of the numbers that are associated with the name, but it is not + is returned. The pcre2_substring_number_from_name() function returns + one of the numbers that are associated with the name, but it is not defined which it is. - If you want to get full details of all captured substrings for a given - name, you must use the pcre2_substring_nametable_scan() function. The - first argument is the compiled pattern, and the second is the name. If - the third and fourth arguments are NULL, the function returns a group + If you want to get full details of all captured substrings for a given + name, you must use the pcre2_substring_nametable_scan() function. The + first argument is the compiled pattern, and the second is the name. If + the third and fourth arguments are NULL, the function returns a group number (it is not defined which). Otherwise, the third and fourth argu- - ments must be pointers to variables that are updated by the function. + ments must be pointers to variables that are updated by the function. After it has run, they point to the first and last entries in the name- to-number table for the given name, and the function returns the length - of each entry. In both cases, PCRE2_ERROR_NOSUBSTRING is returned if + of each entry. In both cases, PCRE2_ERROR_NOSUBSTRING is returned if there are no entries for the given name. The format of the name table is described above in the section entitled - Information about a pattern above. Given all the relevant entries for + Information about a pattern above. Given all the relevant entries for the name, you can extract each of their numbers, and hence the captured data. FINDING ALL POSSIBLE MATCHES - The traditional matching function uses a similar algorithm to Perl, + 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 want to find all possible matches, or the longest - possible match at a given position, consider using the alternative - matching function (see below) instead. If you cannot use the alterna- + the subject. If you want to find all possible matches, or the longest + possible match at a given position, consider using the alternative + matching function (see below) instead. If you cannot use the alterna- tive function, you can kludge it up by making use of the callout facil- ity, which is described in the pcre2callout documentation. What you have to do is to insert a callout right at the end of the pat- - tern. When your callout function is called, extract and save the cur- - rent matched substring. Then return 1, which forces pcre2_match() to - backtrack and try other alternatives. Ultimately, when it runs out of + tern. When your callout function is called, extract and save the cur- + rent matched substring. Then return 1, which forces pcre2_match() to + backtrack and try other alternatives. Ultimately, when it runs out of matches, pcre2_match() will yield PCRE2_ERROR_NOMATCH. @@ -2354,26 +2428,26 @@ MATCHING A PATTERN: THE ALTERNATIVE FUNCTION pcre2_match_context *mcontext, int *workspace, PCRE2_SIZE wscount); - The function pcre2_dfa_match() is called to match a subject string - against a compiled pattern, using a matching algorithm that scans the - subject string just once, and does not backtrack. This has different - characteristics to the normal algorithm, and is not compatible with - Perl. Some of the features of PCRE2 patterns are not supported. Never- - theless, there are times when this kind of matching can be useful. For - a discussion of the two matching algorithms, and a list of features + The function pcre2_dfa_match() is called to match a subject string + against a compiled pattern, using a matching algorithm that scans the + subject string just once, and does not backtrack. This has different + characteristics to the normal algorithm, and is not compatible with + Perl. Some of the features of PCRE2 patterns are not supported. Never- + theless, there are times when this kind of matching can be useful. For + a discussion of the two matching algorithms, and a list of features that pcre2_dfa_match() does not support, see the pcre2matching documen- tation. - The arguments for the pcre2_dfa_match() function are the same as for + The arguments for the pcre2_dfa_match() function are the same as for pcre2_match(), plus two extras. The ovector within the match data block is used in a different way, and this is described below. The other com- - mon arguments are used in the same way as for pcre2_match(), so their + mon arguments are used in the same way as for pcre2_match(), so their description is not repeated here. - The two additional arguments provide workspace for the function. The - workspace vector should contain at least 20 elements. It is used for + The two additional arguments provide workspace for the function. The + workspace vector should contain at least 20 elements. It is used for keeping track of multiple paths through the pattern tree. More - workspace is needed for patterns and subjects where there are a lot of + workspace is needed for patterns and subjects where there are a lot of potential matches. Here is an example of a simple call to pcre2_dfa_match(): @@ -2393,45 +2467,45 @@ MATCHING A PATTERN: THE ALTERNATIVE FUNCTION Option bits for pcre_dfa_match() - The unused bits of the options argument for pcre2_dfa_match() must be - zero. The only bits that may be set are PCRE2_ANCHORED, PCRE2_NOTBOL, + The unused bits of the options argument for pcre2_dfa_match() must be + zero. The only bits that may be set are PCRE2_ANCHORED, PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART, PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD, PCRE2_PARTIAL_SOFT, - PCRE2_DFA_SHORTEST, and PCRE2_DFA_RESTART. All but the last four of - these are exactly the same as for pcre2_match(), so their description + PCRE2_DFA_SHORTEST, and PCRE2_DFA_RESTART. All but the last four of + these are exactly the same as for pcre2_match(), so their description is not repeated here. PCRE2_PARTIAL_HARD PCRE2_PARTIAL_SOFT - These have the same general effect as they do for pcre2_match(), but - the details are slightly different. When PCRE2_PARTIAL_HARD is set for - pcre2_dfa_match(), it returns PCRE2_ERROR_PARTIAL if the end of the + These have the same general effect as they do for pcre2_match(), but + the details are slightly different. When PCRE2_PARTIAL_HARD is set for + pcre2_dfa_match(), it returns PCRE2_ERROR_PARTIAL if the end of the subject is reached and there is still at least one matching possibility that requires additional characters. This happens even if some complete - matches have already been found. When PCRE2_PARTIAL_SOFT is set, the - return code PCRE2_ERROR_NOMATCH is converted into PCRE2_ERROR_PARTIAL - if the end of the subject is reached, there have been no complete + matches have already been found. When PCRE2_PARTIAL_SOFT is set, the + return code PCRE2_ERROR_NOMATCH is converted into PCRE2_ERROR_PARTIAL + if the end of the subject is reached, there have been no complete matches, but there is still at least one matching possibility. The por- - tion of the string that was inspected when the longest partial match + tion of the string that was inspected when the longest partial match was found is set as the first matching string in both cases. There is a - more detailed discussion of partial and multi-segment matching, with + more detailed discussion of partial and multi-segment matching, with examples, in the pcre2partial documentation. PCRE2_DFA_SHORTEST - Setting the PCRE2_DFA_SHORTEST option causes the matching algorithm to + Setting the PCRE2_DFA_SHORTEST option causes the matching algorithm to stop as soon as it has found one match. Because of the way the alterna- - tive algorithm works, this is necessarily the shortest possible match + tive algorithm works, this is necessarily the shortest possible match at the first possible matching point in the subject string. PCRE2_DFA_RESTART - When pcre2_dfa_match() returns a partial match, it is possible to call + When pcre2_dfa_match() returns a partial match, it is possible to call it again, with additional subject characters, and have it continue with the same match. The PCRE2_DFA_RESTART option requests this action; when - it is set, the workspace and wscount options must reference the same - vector as before because data about the match so far is left in them + it is set, the workspace and wscount options must reference the same + vector as before because data about the match so far is left in them after a partial match. There is more discussion of this facility in the pcre2partial documentation. @@ -2439,8 +2513,8 @@ MATCHING A PATTERN: THE ALTERNATIVE FUNCTION When pcre2_dfa_match() succeeds, it may have matched more than one sub- string in the subject. Note, however, that all the matches from one run - of the function start at the same point in the subject. The shorter - matches are all initial substrings of the longer matches. For example, + of the function start at the same point in the subject. The shorter + matches are all initial substrings of the longer matches. For example, if the pattern <.*> @@ -2455,66 +2529,66 @@ MATCHING A PATTERN: THE ALTERNATIVE FUNCTION - On success, the yield of the function is a number greater than zero, - which is the number of matched substrings. The offsets of the sub- - strings are returned in the ovector, and can be extracted in the same - way as for pcre2_match(). They are returned in reverse order of - length; that is, the longest matching string is given first. If there - were too many matches to fit into the ovector, the yield of the func- + On success, the yield of the function is a number greater than zero, + which is the number of matched substrings. The offsets of the sub- + strings are returned in the ovector, and can be extracted in the same + way as for pcre2_match(). They are returned in reverse order of + length; that is, the longest matching string is given first. If there + were too many matches to fit into the ovector, the yield of the func- tion is zero, and the vector is filled with the longest matches. - NOTE: PCRE2's "auto-possessification" optimization usually applies to - character repeats at the end of a pattern (as well as internally). For - example, the pattern "a\d+" is compiled as if it were "a\d++" because - there is no point in backtracking into the repeated digits. For DFA - matching, this means that only one possible match is found. If you - really do want multiple matches in such cases, either use an ungreedy - repeat ("a\d+?") or set the PCRE2_NO_AUTO_POSSESS option when compil- + NOTE: PCRE2's "auto-possessification" optimization usually applies to + character repeats at the end of a pattern (as well as internally). For + example, the pattern "a\d+" is compiled as if it were "a\d++" because + there is no point in backtracking into the repeated digits. For DFA + matching, this means that only one possible match is found. If you + really do want multiple matches in such cases, either use an ungreedy + repeat ("a\d+?") or set the PCRE2_NO_AUTO_POSSESS option when compil- ing. Error returns from pcre2_dfa_match() The pcre2_dfa_match() function returns a negative number when it fails. - Many of the errors are the same as for pcre2_match(), as described + Many of the errors are the same as for pcre2_match(), as described above. There are in addition the following errors that are specific to pcre2_dfa_match(): PCRE2_ERROR_DFA_UITEM - This return is given if pcre2_dfa_match() encounters an item in the + This return is given if pcre2_dfa_match() encounters an item in the pattern that it does not support, for instance, the use of \C or a back reference. PCRE2_ERROR_DFA_UCOND - This return is given if pcre2_dfa_match() encounters a condition item - that uses a back reference for the condition, or a test for recursion + This return is given if pcre2_dfa_match() encounters a condition item + that uses a back reference for the condition, or a test for recursion in a specific group. These are not supported. PCRE2_ERROR_DFA_WSSIZE - This return is given if pcre2_dfa_match() runs out of space in the + This return is given if pcre2_dfa_match() runs out of space in the workspace vector. PCRE2_ERROR_DFA_RECURSE - When a recursive subpattern is processed, the matching function calls + When a recursive subpattern is processed, the matching function calls itself recursively, using private memory for the ovector and workspace. - This error is given if the internal ovector is not large enough. This + This error is given if the internal ovector is not large enough. This should be extremely rare, as a vector of size 1000 is used. PCRE2_ERROR_DFA_BADRESTART - When pcre2_dfa_match() is called with the pcre2_dfa_RESTART option, - some plausibility checks are made on the contents of the workspace, - which should contain data about the previous partial match. If any of + When pcre2_dfa_match() is called with the pcre2_dfa_RESTART option, + 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. SEE ALSO - pcre2build(3), pcre2libs(3), pcre2callout(3), pcre2matching(3), - pcre2partial(3), pcre2posix(3), pcre2demo(3), pcre2sample(3), + pcre2build(3), pcre2libs(3), pcre2callout(3), pcre2matching(3), + pcre2partial(3), pcre2posix(3), pcre2demo(3), pcre2sample(3), pcre2stack(3). @@ -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_jit_callback callback - void *data + 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,13 +3666,14 @@ 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 + This is a suggestion for how a multithreaded program that needs to set up non-default JIT stacks might operate: During thread initalization @@ -3609,10 +3685,8 @@ CONTROLLING THE JIT STACK Use a one-line callback function 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(). + All the functions described in this section do nothing if JIT is not + available. JIT STACK FAQ @@ -3620,66 +3694,66 @@ JIT STACK FAQ (1) Why do we need JIT stacks? PCRE2 (and JIT) is a recursive, depth-first engine, so it needs a stack - where the local data of the current node is pushed before checking its + where the local data of the current node is pushed before checking its child nodes. Allocating real machine stack on some platforms is diffi- cult. For example, the stack chain needs to be updated every time if we - extend the stack on PowerPC. Although it is possible, its updating + extend the stack on PowerPC. Although it is possible, its updating time overhead decreases performance. So we do the recursion in memory. (2) Why don't we simply allocate blocks of memory with malloc()? - Modern operating systems have a nice feature: they can reserve an + Modern operating systems have a nice feature: they can reserve an address space instead of allocating memory. We can safely allocate mem- - ory pages inside this address space, so the stack could grow without + ory pages inside this address space, so the stack could grow without moving memory data (this is important because of pointers). Thus we can - allocate 1M address space, and use only a single memory page (usually - 4K) if that is enough. However, we can still grow up to 1M anytime if + allocate 1M address space, and use only a single memory page (usually + 4K) if that is enough. However, we can still grow up to 1M anytime if needed. (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 + (5) Should I allocate/free a stack every time before/after calling pcre2_match()? - 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. + 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 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 + (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 until the stack is freed? - Especially on embedded sytems, it might be a good idea to release mem- - ory sometimes without freeing the stack. There is no API for this at - the moment. Probably a function call which returns with the currently - allocated memory for any stack and another which allows releasing mem- + Especially on embedded sytems, it might be a good idea to release mem- + ory sometimes without freeing the stack. There is no API for this at + the moment. Probably a function call which returns with the currently + allocated memory for any stack and another which allows releasing mem- ory (shrinking the stack) would be a good idea if someone needs this. (7) This is too much of a headache. Isn't there any better solution for JIT stack handling? - No, thanks to Windows. If POSIX threads were used everywhere, we could + No, thanks to Windows. If POSIX threads were used everywhere, we could throw out this complicated API. @@ -3688,36 +3762,39 @@ FREEING JIT SPECULATIVE MEMORY void pcre2_jit_free_unused_memory(pcre2_general_context *gcontext); The JIT executable allocator does not free all memory when it is possi- - ble. It expects new allocations, and keeps some free memory around to - improve allocation speed. However, in low memory conditions, it might - be better to free all possible memory. You can cause this to happen by - calling pcre2_jit_free_unused_memory(). Its argument is a general con- + ble. It expects new allocations, and keeps some free memory around to + improve allocation speed. However, in low memory conditions, it might + be better to free all possible memory. You can cause this to happen by + calling pcre2_jit_free_unused_memory(). Its argument is a general con- text, for custom memory management, or NULL for standard memory manage- ment. EXAMPLE CODE - This is a single-threaded example that specifies a JIT stack without - using a callback. + This is a single-threaded example that specifies a JIT stack without + 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. ------------------------------------------------------------------------------ diff --git a/doc/pcre2_jit_stack_assign.3 b/doc/pcre2_jit_stack_assign.3 index 09ca70f..66b8095 100644 --- a/doc/pcre2_jit_stack_assign.3 +++ b/doc/pcre2_jit_stack_assign.3 @@ -14,7 +14,7 @@ PCRE2 - Perl-compatible regular expressions (revised API) .SH DESCRIPTION .rs .sp -This function provides control over the memory used by JIT as a run-time stack +This function provides control over the memory used by JIT as a run-time stack when \fBpcre2_match()\fP or \fBpcre2_jit_match()\fP 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 diff --git a/doc/pcre2_substitute.3 b/doc/pcre2_substitute.3 new file mode 100644 index 0000000..6139f8f --- /dev/null +++ b/doc/pcre2_substitute.3 @@ -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 +.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. diff --git a/doc/pcre2api.3 b/doc/pcre2api.3 index 8b016b9..74d210e 100644 --- a/doc/pcre2api.3 +++ b/doc/pcre2api.3 @@ -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 @@ -865,8 +882,8 @@ otherwise it is set to zero. Unicode support implies UTF support. The \fIwhere\fP argument should point to a buffer that is at least 12 code units long. (The exact length needed can be found by calling \fBpcre2_config()\fP with \fBwhere\fP set to NULL.) The buffer is filled with -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 +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. . . @@ -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 + $ insert the contents of group + ${} insert the contents of group +.sp +Either a group number or a group name can be given for . 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 diff --git a/doc/pcre2jit.3 b/doc/pcre2jit.3 index ac82892..6451bde 100644 --- a/doc/pcre2jit.3 +++ b/doc/pcre2jit.3 @@ -169,7 +169,7 @@ should use. Its arguments are as follows: void *data .sp 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 +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: .sp (1) If \fIcallback\fP is NULL and \fIdata\fP is NULL, an internal 32K block @@ -309,27 +309,27 @@ memory management, or NULL for standard memory management. .rs .sp This is a single-threaded example that specifies a JIT stack without using a -callback. A real program should include error checking after all the function +callback. A real program should include error checking after all the function calls. .sp int rc; pcre2_code *re; pcre2_match_data *match_data; - pcre2_match_context *mcontext; + pcre2_match_context *mcontext; pcre2_jit_stack *jit_stack; .sp - re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, + re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroffset, NULL); rc = pcre2_jit_compile(re, PCRE2_JIT_COMPLETE); - mcontext = pcre2_match_context_create(NULL); + mcontext = pcre2_match_context_create(NULL); jit_stack = pcre2_jit_stack_create(NULL, 32*1024, 512*1024); 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, mcontext); - /* Process result */ + /* Process result */ .sp pcre2_code_free(re); - pcre2_match_data_free(match_data); + pcre2_match_data_free(match_data); pcre2_match_context_free(mcontext); pcre2_jit_stack_free(jit_stack); .sp diff --git a/doc/pcre2test.1 b/doc/pcre2test.1 index 2dbe5d3..d616210 100644 --- a/doc/pcre2test.1 +++ b/doc/pcre2test.1 @@ -720,7 +720,7 @@ pattern. ovector= set size of output vector recursion_limit= set a recursion limit startchar show startchar when relevant - zero_terminate pass the subject as zero-terminated + zero_terminate pass the subject as zero-terminated .sp The effects of these modifiers are described in the following sections. FIXME: Give more examples. @@ -932,7 +932,7 @@ offsets.) 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 \fBzero_terminate\fP modifier is provided. It causes the length to -be passed as PCRE2_ZERO_TERMINATED. (When matching via the POSIX interface, +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 When testing \fBpcre2_substitute\fP, this modifier also has the effect of diff --git a/doc/pcre2test.txt b/doc/pcre2test.txt index 0172c49..63e3884 100644 --- a/doc/pcre2test.txt +++ b/doc/pcre2test.txt @@ -435,7 +435,6 @@ PATTERN MODIFIERS posix use the POSIX API stackguard= 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,17 +479,14 @@ 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 - The /jit modifier may optionally be followed by and equals sign and a + The /jit modifier may optionally be followed by and equals sign and a number in the range 0 to 7: 0 disable JIT @@ -501,23 +497,23 @@ PATTERN MODIFIERS 6 use JIT for soft and hard partial match 7 all three modes - If no number is given, 7 is assumed. If JIT compilation is successful, - the compiled JIT code will automatically be used when pcre2_match() is - run for the appropriate type of match, except when incompatible run- + If no number is given, 7 is assumed. If JIT compilation is successful, + the compiled JIT code will automatically be used when pcre2_match() is + run for the appropriate type of match, except when incompatible run- time options are specified. For more details, see the pcre2jit documen- - tation. See also the jitstack modifier below for a way of setting the + tation. See also the jitstack modifier below for a way of setting the size of the JIT stack. - If the jitfast modifier is specified, matching is done using the JIT + If the jitfast modifier is specified, matching is done using the JIT "fast path" interface (pcre2_jit_match()), which skips some of the san- - ity checks that are done by pcre2_match(), and of course does not work - when JIT is not supported. If jitfast is specified without jit, jit=7 + ity checks that are done by pcre2_match(), and of course does not work + when JIT is not supported. If jitfast is specified without jit, jit=7 is assumed. - If the jitverify modifier is specified, information about the compiled - pattern shows whether JIT compilation was or was not successful. If - jitverify is specified without jit, jit=7 is assumed. If JIT compila- - tion is successful when jitverify is set, the text "(JIT)" is added to + If the jitverify modifier is specified, information about the compiled + pattern shows whether JIT compilation was or was not successful. If + jitverify is specified without jit, jit=7 is assumed. If JIT compila- + tion is successful when jitverify is set, the text "(JIT)" is added to the first output line after a match or non match when JIT-compiled code was actually used. @@ -528,33 +524,33 @@ PATTERN MODIFIERS /pattern/locale=fr_FR The given locale is set, pcre2_maketables() is called to build a set of - character tables for the locale, and this is then passed to pcre2_com- - pile() when compiling the regular expression. The same tables are used + character tables for the locale, and this is then passed to pcre2_com- + pile() when compiling the regular expression. The same tables are used when matching the following subject lines. The /locale modifier applies only to the pattern on which it appears, but can be given in a #pattern - command if a default is needed. Setting a locale and alternate charac- + command if a default is needed. Setting a locale and alternate charac- ter tables are mutually exclusive. Showing pattern memory - The /memory modifier causes the size in bytes of the memory block used - to hold the compiled pattern to be output. This does not include the - size of the pcre2_code block; it is just the actual compiled data. If + The /memory modifier causes the size in bytes of the memory block used + to hold the compiled pattern to be output. This does not include the + size of the pcre2_code block; it is just the actual compiled data. If the pattern is subsequently passed to the JIT compiler, the size of the JIT compiled code is also output. Limiting nested parentheses - The parens_nest_limit modifier sets a limit on the depth of nested - parentheses in a pattern. Breaching the limit causes a compilation - error. The default for the library is set when PCRE2 is built, but - pcre2test sets its own default of 220, which is required for running + The parens_nest_limit modifier sets a limit on the depth of nested + parentheses in a pattern. Breaching the limit causes a compilation + error. The default for the library is set when PCRE2 is built, but + pcre2test sets its own default of 220, which is required for running the standard test suite. Using the POSIX wrapper API - The /posix modifier causes pcre2test to call PCRE2 via the POSIX wrap- - per API rather than its native API. This supports only the 8-bit + The /posix modifier causes pcre2test to call PCRE2 via the POSIX wrap- + per API rather than its native API. This supports only the 8-bit library. When the POSIX API is being used, the following pattern modi- fiers set options for the regcomp() function: @@ -566,25 +562,25 @@ PATTERN MODIFIERS ucp REG_UCP ) the POSIX standard utf REG_UTF8 ) - The aftertext and allaftertext subject modifiers work as described + The aftertext and allaftertext subject modifiers work as described below. All other modifiers cause an error. Testing the stack guard feature - The /stackguard modifier is used to test the use of pcre2_set_com- - pile_recursion_guard(), a function that is provided to enable stack - availability to be checked during compilation (see the pcre2api docu- - mentation for details). If the number specified by the modifier is + The /stackguard modifier is used to test the use of pcre2_set_com- + pile_recursion_guard(), a function that is provided to enable stack + availability to be checked during compilation (see the pcre2api docu- + mentation for details). If the number specified by the modifier is greater than zero, pcre2_set_compile_recursion_guard() is called to set - up callback from pcre2_compile() to a local function. The argument it - is passed is the current nesting parenthesis depth; if this is greater + up callback from pcre2_compile() to a local function. The argument it + is passed is the current nesting parenthesis depth; if this is greater than the value given by the modifier, non-zero is returned, causing the compilation to be aborted. Using alternative character tables - The /tables modifier must be followed by a single digit. It causes a - specific set of built-in character tables to be passed to pcre2_com- + The /tables modifier must be followed by a single digit. It causes a + specific set of built-in character tables to be passed to pcre2_com- pile(). This is used in the PCRE2 tests to check behaviour with differ- ent character tables. The digit specifies the tables as follows: @@ -593,15 +589,15 @@ PATTERN MODIFIERS pcre2_chartables.c.dist 2 a set of tables defining ISO 8859 characters - In table 2, some characters whose codes are greater than 128 are iden- - tified as letters, digits, spaces, etc. Setting alternate character + In table 2, some characters whose codes are greater than 128 are iden- + tified as letters, digits, spaces, etc. Setting alternate character tables and a locale are mutually exclusive. Setting certain match controls The following modifiers are really subject modifiers, and are described - below. However, they may be included in a pattern's modifier list, in - which case they are applied to every subject line that is processed + below. However, they may be included in a pattern's modifier list, in + which case they are applied to every subject line that is processed with that pattern. They do not affect the compilation process. aftertext show text after match @@ -612,7 +608,7 @@ PATTERN MODIFIERS mark show mark values startchar show starting character when relevant - These modifiers may not appear in a #pattern command. If you want them + These modifiers may not appear in a #pattern command. If you want them as defaults, set them in a #subject command. @@ -623,7 +619,7 @@ SUBJECT MODIFIERS Setting match options - The following modifiers set options for pcre2_match() or + The following modifiers set options for pcre2_match() or pcre2_dfa_match(). See pcreapi for a description of their effects. anchored set PCRE2_ANCHORED @@ -637,20 +633,20 @@ SUBJECT MODIFIERS partial_hard (or ph) set PCRE2_PARTIAL_HARD partial_soft (or ps) set PCRE2_PARTIAL_SOFT - The partial matching modifiers are provided with abbreviations because + The partial matching modifiers are provided with abbreviations because they appear frequently in tests. - If the /posix modifier was present on the pattern, causing the POSIX + If the /posix modifier was present on the pattern, causing the POSIX wrapper API to be used, the only option-setting modifiers that have any - effect are notbol, notempty, and noteol, causing REG_NOTBOL, - REG_NOTEMPTY, and REG_NOTEOL, respectively, to be passed to regexec(). + effect are notbol, notempty, and noteol, causing REG_NOTBOL, + REG_NOTEMPTY, and REG_NOTEOL, respectively, to be passed to regexec(). Any other modifiers cause an error. Setting match controls - The following modifiers affect the matching process or request addi- - tional information. Some of them may also be specified on a pattern - line (see above), in which case they apply to every subject line that + The following modifiers affect the matching process or request addi- + tional information. Some of them may also be specified on a pattern + line (see above), in which case they apply to every subject line that is matched against that pattern. aftertext show text after match @@ -676,29 +672,30 @@ SUBJECT MODIFIERS ovector= set size of output vector recursion_limit= 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. Showing more text - The aftertext modifier requests that as well as outputting the sub- - string that matched the entire pattern, pcre2test should in addition - output the remainder of the subject string. This is useful for tests - where the subject contains multiple copies of the same substring. The - allaftertext modifier requests the same action for captured substrings - as well as the main matched substring. In each case the remainder is - output on the following line with a plus character following the cap- + The aftertext modifier requests that as well as outputting the sub- + string that matched the entire pattern, pcre2test should in addition + output the remainder of the subject string. This is useful for tests + where the subject contains multiple copies of the same substring. The + allaftertext modifier requests the same action for captured substrings + as well as the main matched substring. In each case the remainder is + output on the following line with a plus character following the cap- ture number. - The allusedtext modifier requests that all the text that was consulted - during a successful pattern match by the interpreter should be shown. - This feature is not supported for JIT matching, and if requested with - JIT it is ignored (with a warning message). Setting this modifier + The allusedtext modifier requests that all the text that was consulted + during a successful pattern match by the interpreter should be shown. + This feature is not supported for JIT matching, and if requested with + JIT it is ignored (with a warning message). Setting this modifier affects the output if there is a lookbehind at the start of a match, or - a lookahead at the end, or if \K is used in the pattern. Characters - that precede or follow the start and end of the actual match are indi- - cated in the output by '<' or '>' characters underneath them. Here is + a lookahead at the end, or if \K is used in the pattern. Characters + that precede or follow the start and end of the actual match are indi- + cated in the output by '<' or '>' characters underneath them. Here is an example: re> /(?<=pqr)abc(?=xyz)/ @@ -706,15 +703,15 @@ SUBJECT MODIFIERS 0: pqrabcxyz <<< >>> - This shows that the matched string is "abc", with the preceding and + This shows that the matched string is "abc", with the preceding and following strings "pqr" and "xyz" also consulted during the match. - The startchar modifier requests that the starting character for the - match be indicated, if it is different to the start of the matched + The startchar modifier requests that the starting character for the + match be indicated, if it is different to the start of the matched string. The only time when this occurs is when \K has been processed as part of the match. In this situation, the output for the matched string - is displayed from the starting character instead of from the match - point, with circumflex characters under the earlier characters. For + is displayed from the starting character instead of from the match + point, with circumflex characters under the earlier characters. For example: re> /abc\Kxyz/ @@ -722,7 +719,7 @@ SUBJECT MODIFIERS 0: abcxyz ^^^ - Unlike allusedtext, the startchar modifier can be used with JIT. How- + Unlike allusedtext, the startchar modifier can be used with JIT. How- ever, these two modifiers are mutually exclusive. Showing the value of all capture groups @@ -730,171 +727,183 @@ SUBJECT MODIFIERS The allcaptures modifier requests that the values of all potential cap- tured parentheses be output after a match. By default, only those up to the highest one actually used in the match are output (corresponding to - the return code from pcre2_match()). Groups that did not take part in + the return code from pcre2_match()). Groups that did not take part in the match are output as "". Testing callouts - A callout function is supplied when pcre2test calls the library match- - ing functions, unless callout_none is specified. If callout_capture is + A callout function is supplied when pcre2test calls the library match- + ing functions, unless callout_none is specified. If callout_capture is set, the current captured groups are output when a callout occurs. - The callout_fail modifier can be given one or two numbers. If there is + The callout_fail modifier can be given one or two numbers. If there is only one number, 1 is returned instead of 0 when a callout of that num- - ber is reached. If two numbers are given, 1 is returned when callout + ber is reached. If two numbers are given, 1 is returned when callout is reached for the th time. - The callout_data modifier can be given an unsigned or a negative num- - ber. Any value other than zero is used as a return from pcre2test's + The callout_data modifier can be given an unsigned or a negative num- + ber. Any value other than zero is used as a return from pcre2test's callout function. Testing substring extraction functions - The copy and get modifiers can be used to test the pcre2_sub- + The copy and get modifiers can be used to test the pcre2_sub- string_copy_xxx() and pcre2_substring_get_xxx() functions. They can be - given more than once, and each can specify a group name or number, for + given more than once, and each can specify a group name or number, for example: abcd\=copy=1,copy=3,get=G1 - If the #subject command is used to set default copy and get lists, - these can be unset by specifying a negative number for numbered groups + If the #subject command is used to set default copy and get lists, + these can be unset by specifying a negative number for numbered groups and an empty name for named groups. - The getall modifier tests pcre2_substring_list_get(), which extracts + The getall modifier tests pcre2_substring_list_get(), which extracts all captured substrings. - If the subject line is successfully matched, the substrings extracted - by the convenience functions are output with C, G, or L after the - string number instead of a colon. This is in addition to the normal - full list. The string length (that is, the return from the extraction + If the subject line is successfully matched, the substrings extracted + by the convenience functions are output with C, G, or L after the + string number instead of a colon. This is in addition to the normal + full list. The string length (that is, the return from the extraction function) is given in parentheses after each substring. Finding all matches in a string Searching for all possible matches within a subject can be requested by - the global or /altglobal modifier. After finding a match, the matching - function is called again to search the remainder of the subject. The - difference between global and altglobal is that the former uses the - start_offset argument to pcre2_match() or pcre2_dfa_match() to start - searching at a new point within the entire string (which is what Perl + the global or /altglobal modifier. After finding a match, the matching + function is called again to search the remainder of the subject. The + difference between global and altglobal is that the former uses the + start_offset argument to pcre2_match() or pcre2_dfa_match() to start + searching at a new point within the entire string (which is what Perl does), whereas the latter passes over a shortened substring. This makes a difference to the matching process if the pattern begins with a look- behind assertion (including \b or \B). - If an empty string is matched, the next match is done with the + If an empty string is matched, the next match is done with the PCRE2_NOTEMPTY_ATSTART and PCRE2_ANCHORED flags set, in order to search for another, non-empty, match at the same point in the subject. If this - match fails, the start offset is advanced, and the normal match is - retried. This imitates the way Perl handles such cases when using the - /g modifier or the split() function. Normally, the start offset is - advanced by one character, but if the newline convention recognizes - CRLF as a newline, and the current character is CR followed by LF, an + match fails, the start offset is advanced, and the normal match is + retried. This imitates the way Perl handles such cases when using the + /g modifier or the split() function. Normally, the start offset is + advanced by one character, but if the newline convention recognizes + CRLF as a newline, and the current character is CR followed by LF, an advance of two is used. Setting the JIT stack size - The jitstack modifier provides a way of setting the maximum stack size - that is used by the just-in-time optimization code. It is ignored if + The jitstack modifier provides a way of setting the maximum stack size + that is used by the just-in-time optimization code. It is ignored if JIT optimization is not being used. The value is a number of kilobytes. Providing a stack that is larger than the default 32K is necessary only for very complicated patterns. Setting match and recursion limits - The match_limit and recursion_limit modifiers set the appropriate lim- + The match_limit and recursion_limit modifiers set the appropriate lim- its in the match context. These values are ignored when the find_limits modifier is specified. Finding minimum limits - If the find_limits modifier is present, pcre2test calls pcre2_match() - several times, setting different values in the match context via - pcre2_set_match_limit() and pcre2_set_recursion_limit() until it finds - the minimum values for each parameter that allow pcre2_match() to com- + If the find_limits modifier is present, pcre2test calls pcre2_match() + several times, setting different values in the match context via + pcre2_set_match_limit() and pcre2_set_recursion_limit() until it finds + the minimum values for each parameter that allow pcre2_match() to com- plete without error. If JIT is being used, only the match limit is relevant. If DFA matching - is being used, neither limit is relevant, and this modifier is ignored + is being used, neither limit is relevant, and this modifier is ignored (with a warning message). - The match_limit number is a measure of the amount of backtracking that - takes place, and learning the minimum value can be instructive. For - most simple matches, the number is quite small, but for patterns with - very large numbers of matching possibilities, it can become large very - quickly with increasing length of subject string. The - match_limit_recursion number is a measure of how much stack (or, if - PCRE2 is compiled with NO_RECURSE, how much heap) memory is needed to + The match_limit number is a measure of the amount of backtracking that + takes place, and learning the minimum value can be instructive. For + most simple matches, the number is quite small, but for patterns with + very large numbers of matching possibilities, it can become large very + quickly with increasing length of subject string. The + match_limit_recursion number is a measure of how much stack (or, if + PCRE2 is compiled with NO_RECURSE, how much heap) memory is needed to complete the match attempt. Showing MARK names The mark modifier causes the names from backtracking control verbs that - are returned from calls to pcre2_match() to be displayed. If a mark is - returned for a match, non-match, or partial match, pcre2test shows it. - For a match, it is on a line by itself, tagged with "MK:". Otherwise, + are returned from calls to pcre2_match() to be displayed. If a mark is + returned for a match, non-match, or partial match, pcre2test shows it. + For a match, it is on a line by itself, tagged with "MK:". Otherwise, it is added to the non-match message. Showing memory usage - The memory modifier causes pcre2test to log all memory allocation and + The memory modifier causes pcre2test to log all memory allocation and freeing calls that occur during a match operation. Setting a starting offset - The offset modifier sets an offset in the subject string at which + The offset modifier sets an offset in the subject string at which matching starts. Its value is a number of code units, not characters. Setting the size of the output vector - The ovector modifier applies only to the subject line in which it - appears, though of course it can also be used to set a default in a - #subject command. It specifies the number of pairs of offsets that are + The ovector modifier applies only to the subject line in which it + appears, though of course it can also be used to set a default in a + #subject command. It specifies the number of pairs of offsets that are available for storing matching information. The default is 15. - A value of zero is useful when testing the POSIX API because it causes + A value of zero is useful when testing the POSIX API because it causes regexec() to be called with a NULL capture vector. When not testing the - POSIX API, a value of zero is used to cause pcre2_match_data_cre- - ate_from_pattern to be called, in order to create a match block of + POSIX API, a value of zero is used to cause pcre2_match_data_cre- + ate_from_pattern to be called, in order to create a 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 + 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 - By default, pcre2test uses the standard PCRE2 matching function, + By default, pcre2test uses the standard PCRE2 matching function, pcre2_match() to match each subject line. PCRE2 also supports an alter- - native matching function, pcre2_dfa_match(), which operates in a dif- - ferent way, and has some restrictions. The differences between the two + native matching function, pcre2_dfa_match(), which operates in a dif- + ferent way, and has some restrictions. The differences between the two functions are described in the pcre2matching documentation. - If the dfa modifier is set, the alternative matching function is used. - This function finds all possible matches at a given point in the sub- - ject. If, however, the dfa_shortest modifier is set, processing stops - after the first match is found. This is always the shortest possible + If the dfa modifier is set, the alternative matching function is used. + This function finds all possible matches at a given point in the sub- + ject. If, however, the dfa_shortest modifier is set, processing stops + after the first match is found. This is always the shortest possible match. DEFAULT OUTPUT FROM pcre2test - This section describes the output when the normal matching function, + This section describes the output when the normal matching function, pcre2_match(), is being used. - When a match succeeds, pcre2test outputs the list of captured sub- - strings, starting with number 0 for the string that matched the whole - pattern. Otherwise, it outputs "No match" when the return is - PCRE2_ERROR_NOMATCH, or "Partial match:" followed by the partially - matching substring when the return is PCRE2_ERROR_PARTIAL. (Note that - this is the entire substring that was inspected during the partial - match; it may include characters before the actual match start if a + When a match succeeds, pcre2test outputs the list of captured sub- + strings, starting with number 0 for the string that matched the whole + pattern. Otherwise, it outputs "No match" when the return is + PCRE2_ERROR_NOMATCH, or "Partial match:" followed by the partially + matching substring when the return is PCRE2_ERROR_PARTIAL. (Note that + this is the entire substring that was inspected during the partial + match; it may include characters before the actual match start if a lookbehind assertion, \K, \b, or \B was involved.) For any other return, pcre2test outputs the PCRE2 negative error number - and a short descriptive phrase. If the error is a failed UTF string - check, the offset of the start of the failing character and the reason - code are also output. Here is an example of an interactive pcre2test + and a short descriptive phrase. If the error is a failed UTF string + check, the offset of the start of the failing character and the reason + code are also output. Here is an example of an interactive pcre2test run. $ pcre2test @@ -908,10 +917,10 @@ DEFAULT OUTPUT FROM pcre2test No match Unset capturing substrings that are not followed by one that is set are - not returned by pcre2_match(), and are not shown by pcre2test. In the - following example, there are two capturing substrings, but when the - first data line is matched, the second, unset substring is not shown. - An "internal" unset substring is shown as "", as for the second + not returned by pcre2_match(), and are not shown by pcre2test. In the + following example, there are two capturing substrings, but when the + first data line is matched, the second, unset substring is not shown. + An "internal" unset substring is shown as "", as for the second data line. re> /(a)|(b)/ @@ -923,11 +932,11 @@ DEFAULT OUTPUT FROM pcre2test 1: 2: b - If the strings contain any non-printing characters, they are output as - \xhh escapes if the value is less than 256 and UTF mode is not set. + If the strings contain any non-printing characters, they are output as + \xhh escapes if the value is less than 256 and UTF mode is not set. Otherwise they are output as \x{hh...} escapes. See below for the defi- - nition of non-printing characters. If the /aftertext modifier is set, - the output for substring 0 is followed by the the rest of the subject + nition of non-printing characters. If the /aftertext modifier is set, + the output for substring 0 is followed by the the rest of the subject string, identified by "0+" like this: re> /cat/aftertext @@ -935,7 +944,7 @@ DEFAULT OUTPUT FROM pcre2test 0: cat 0+ aract - If global matching is requested, the results of successive matching + If global matching is requested, the results of successive matching attempts are output in sequence, like this: re> /\Bi(\w\w)/g @@ -947,8 +956,8 @@ DEFAULT OUTPUT FROM pcre2test 0: ipp 1: pp - "No match" is output only if the first match attempt fails. Here is an - example of a failure message (the offset 4 that is specified by \>4 is + "No match" is output only if the first match attempt fails. Here is an + example of a failure message (the offset 4 that is specified by \>4 is past the end of the subject string): re> /xyz/ @@ -956,7 +965,7 @@ DEFAULT OUTPUT FROM pcre2test Error -24 (bad offset value) Note that whereas patterns can be continued over several lines (a plain - ">" prompt is used for continuations), subject lines may not. However + ">" prompt is used for continuations), subject lines may not. However newlines can be included in a subject by means of the \n escape (or \r, \r\n, etc., depending on the newline sequence setting). @@ -964,7 +973,7 @@ DEFAULT OUTPUT FROM pcre2test OUTPUT FROM THE ALTERNATIVE MATCHING FUNCTION When the alternative matching function, pcre2_dfa_match(), is used, the - output consists of a list of all the matches that start at the first + output consists of a list of all the matches that start at the first point in the subject where there is at least one match. For example: re> /(tang|tangerine|tan)/ @@ -973,11 +982,11 @@ OUTPUT FROM THE ALTERNATIVE MATCHING FUNCTION 1: tang 2: tan - (Using the normal matching function on this data finds only "tang".) - The longest matching string is always given first (and numbered zero). - After a PCRE2_ERROR_PARTIAL return, the output is "Partial match:", - followed by the partially matching substring. (Note that this is the - entire substring that was inspected during the partial match; it may + (Using the normal matching function on this data finds only "tang".) + The longest matching string is always given first (and numbered zero). + After a PCRE2_ERROR_PARTIAL return, the output is "Partial match:", + followed by the partially matching substring. (Note that this is the + entire substring that was inspected during the partial match; it may include characters before the actual match start if a lookbehind asser- tion, \K, \b, or \B was involved.) @@ -993,16 +1002,16 @@ OUTPUT FROM THE ALTERNATIVE MATCHING FUNCTION 1: tan 0: tan - The alternative matching function does not support substring capture, - so the modifiers that are concerned with captured substrings are not + The alternative matching function does not support substring capture, + so the modifiers that are concerned with captured substrings are not relevant. RESTARTING AFTER A PARTIAL MATCH - When the alternative matching function has given the PCRE2_ERROR_PAR- + When the alternative matching function has given the PCRE2_ERROR_PAR- TIAL return, indicating that the subject partially matched the pattern, - you can restart the match with additional subject data by means of the + you can restart the match with additional subject data by means of the dfa_restart modifier. For example: re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/ @@ -1011,29 +1020,29 @@ RESTARTING AFTER A PARTIAL MATCH data> n05\=dfa,dfa_restart 0: n05 - For further information about partial matching, see the pcre2partial + For further information about partial matching, see the pcre2partial documentation. CALLOUTS If the pattern contains any callout requests, pcre2test's callout func- - tion is called during matching. This works with both matching func- + tion is called during matching. This works with both matching func- tions. By default, the called function displays the callout number, the - start and current positions in the text at the callout time, and the + start and current positions in the text at the callout time, and the next pattern item to be tested. For example: --->pqrabcdef 0 ^ ^ \d - This output indicates that callout number 0 occurred for a match - attempt starting at the fourth character of the subject string, when - the pointer was at the seventh character, and when the next pattern - item was \d. Just one circumflex is output if the start and current + This output indicates that callout number 0 occurred for a match + attempt starting at the fourth character of the subject string, when + the pointer was at the seventh character, and when the next pattern + item was \d. Just one circumflex is output if the start and current positions are the same. Callouts numbered 255 are assumed to be automatic callouts, inserted as - a result of the /auto_callout pattern modifier. In this case, instead + a result of the /auto_callout pattern modifier. In this case, instead of showing the callout number, the offset in the pattern, preceded by a plus, is output. For example: @@ -1047,7 +1056,7 @@ CALLOUTS 0: E* If a pattern contains (*MARK) items, an additional line is output when- - ever a change of latest mark is passed to the callout function. For + ever a change of latest mark is passed to the callout function. For example: re> /a(*MARK:X)bc/auto_callout @@ -1061,30 +1070,30 @@ CALLOUTS +12 ^ ^ 0: abc - The mark changes between matching "a" and "b", but stays the same for - the rest of the match, so nothing more is output. If, as a result of - backtracking, the mark reverts to being unset, the text "" is + The mark changes between matching "a" and "b", but stays the same for + the rest of the match, so nothing more is output. If, as a result of + backtracking, the mark reverts to being unset, the text "" is output. - The callout function in pcre2test returns zero (carry on matching) by - default, but you can use a callout_fail modifier in a subject line (as + The callout function in pcre2test returns zero (carry on matching) by + default, but you can use a callout_fail modifier in a subject line (as described above) to change this and other parameters of the callout. Inserting callouts can be helpful when using pcre2test to check compli- - cated regular expressions. For further information about callouts, see + cated regular expressions. For further information about callouts, see the pcre2callout documentation. NON-PRINTING CHARACTERS When pcre2test is outputting text in the compiled version of a pattern, - bytes other than 32-126 are always treated as non-printing characters + bytes other than 32-126 are always treated as non-printing characters and are therefore shown as hex escapes. - When pcre2test is outputting text that is a matched part of a subject - string, it behaves in the same way, unless a different locale has been - set for the pattern (using the /locale modifier). In this case, the - isprint() function is used to distinguish printing and non-printing + When pcre2test is outputting text that is a matched part of a subject + string, it behaves in the same way, unless a different locale has been + set for the pattern (using the /locale modifier). In this case, the + isprint() function is used to distinguish printing and non-printing characters. @@ -1103,5 +1112,5 @@ AUTHOR REVISION - Last updated: 02 November 2014 + Last updated: 09 November 2014 Copyright (c) 1997-2014 University of Cambridge. diff --git a/src/pcre2_substitute.c b/src/pcre2_substitute.c index 1100c93..341a9a9 100644 --- a/src/pcre2_substitute.c +++ b/src/pcre2_substitute.c @@ -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; @@ -91,7 +91,7 @@ PCRE2_SIZE *ovector; if ((options & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0) return PCRE2_ERROR_BADOPTION; - + /* If no match data block is provided, create one. */ if (match_data == NULL) @@ -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: diff --git a/src/pcre2_substring.c b/src/pcre2_substring.c index 05548ad..92adbac 100644 --- a/src/pcre2_substring.c +++ b/src/pcre2_substring.c @@ -473,7 +473,7 @@ Arguments: Returns: the number of the named parenthesis, or a negative number PCRE2_ERROR_NOSUBSTRING if not found - PCRE2_ERROR_NOUNIQUESUBSTRING if not unique + PCRE2_ERROR_NOUNIQUESUBSTRING if not unique */ PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION diff --git a/src/pcre2test.c b/src/pcre2test.c index 9bd7a4d..b73b03c 100644 --- a/src/pcre2test.c +++ b/src/pcre2test.c @@ -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);