php-src/ext/pcre/pcrelib/pcre_get.c

661 lines
22 KiB
C
Raw Normal View History

2000-04-12 02:40:47 +08:00
/*************************************************
* Perl-Compatible Regular Expressions *
*************************************************/
/* PCRE is a library of functions to support regular expressions whose syntax
and semantics are as close as possible to those of the Perl 5 language.
2000-04-12 02:40:47 +08:00
Written by Philip Hazel
2012-10-19 15:49:28 +08:00
Copyright (c) 1997-2012 University of Cambridge
2000-04-12 02:40:47 +08:00
-----------------------------------------------------------------------------
2005-05-28 01:46:43 +08:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the University of Cambridge nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
2000-04-12 02:40:47 +08:00
-----------------------------------------------------------------------------
*/
2000-04-12 02:40:47 +08:00
/* This module contains some convenience functions for extracting substrings
from the subject string after a regex match has succeeded. The original idea
2005-05-28 01:46:43 +08:00
for these functions came from Scott Wimer. */
2000-04-12 02:40:47 +08:00
2007-10-07 18:35:03 +08:00
#include "config.h"
2007-09-02 01:48:43 +08:00
#include "pcre_internal.h"
2000-04-12 02:40:47 +08:00
2003-06-28 00:31:24 +08:00
/*************************************************
* Find number for named string *
*************************************************/
2006-08-31 04:00:23 +08:00
/* This function is used by the get_first_set() function below, as well
as being generally available. It assumes that names are unique.
2003-06-28 00:31:24 +08:00
Arguments:
code the compiled regex
stringname the name whose number is required
Returns: the number of the named parentheses, or a negative number
(PCRE_ERROR_NOSUBSTRING) if not found
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2003-06-28 00:31:24 +08:00
pcre_get_stringnumber(const pcre *code, const char *stringname)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre32_get_stringnumber(const pcre32 *code, PCRE_SPTR32 stringname)
2012-10-19 15:49:28 +08:00
#endif
2003-06-28 00:31:24 +08:00
{
int rc;
int entrysize;
int top, bot;
2012-10-19 15:49:28 +08:00
pcre_uchar *nametable;
2003-06-28 00:31:24 +08:00
2012-10-19 15:49:28 +08:00
#ifdef COMPILE_PCRE8
2003-06-28 00:31:24 +08:00
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
return rc;
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
return rc;
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
return rc;
2012-10-19 15:49:28 +08:00
#endif
#ifdef COMPILE_PCRE16
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
return rc;
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
return rc;
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
return rc;
#endif
#ifdef COMPILE_PCRE32
if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
return rc;
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
return rc;
if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
return rc;
#endif
2003-06-28 00:31:24 +08:00
bot = 0;
while (top > bot)
{
int mid = (top + bot) / 2;
2012-10-19 15:49:28 +08:00
pcre_uchar *entry = nametable + entrysize*mid;
int c = STRCMP_UC_UC((pcre_uchar *)stringname,
(pcre_uchar *)(entry + IMM2_SIZE));
if (c == 0) return GET2(entry, 0);
2003-06-28 00:31:24 +08:00
if (c > 0) bot = mid + 1; else top = mid;
}
return PCRE_ERROR_NOSUBSTRING;
}
2000-04-12 02:40:47 +08:00
2006-08-31 04:00:23 +08:00
/*************************************************
* Find (multiple) entries for named string *
*************************************************/
/* This is used by the get_first_set() function below, as well as being
generally available. It is used when duplicated names are permitted.
Arguments:
code the compiled regex
stringname the name whose entries required
firstptr where to put the pointer to the first entry
lastptr where to put the pointer to the last entry
Returns: the length of each entry, or a negative number
(PCRE_ERROR_NOSUBSTRING) if not found
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2006-08-31 04:00:23 +08:00
pcre_get_stringtable_entries(const pcre *code, const char *stringname,
char **firstptr, char **lastptr)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname,
PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre32_get_stringtable_entries(const pcre32 *code, PCRE_SPTR32 stringname,
PCRE_UCHAR32 **firstptr, PCRE_UCHAR32 **lastptr)
2012-10-19 15:49:28 +08:00
#endif
2006-08-31 04:00:23 +08:00
{
int rc;
int entrysize;
int top, bot;
2012-10-19 15:49:28 +08:00
pcre_uchar *nametable, *lastentry;
2006-08-31 04:00:23 +08:00
2012-10-19 15:49:28 +08:00
#ifdef COMPILE_PCRE8
2006-08-31 04:00:23 +08:00
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
return rc;
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
return rc;
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
return rc;
2012-10-19 15:49:28 +08:00
#endif
#ifdef COMPILE_PCRE16
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
return rc;
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
return rc;
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
return rc;
#endif
#ifdef COMPILE_PCRE32
if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
return rc;
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
return rc;
if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
return rc;
#endif
2006-08-31 04:00:23 +08:00
lastentry = nametable + entrysize * (top - 1);
bot = 0;
while (top > bot)
{
int mid = (top + bot) / 2;
2012-10-19 15:49:28 +08:00
pcre_uchar *entry = nametable + entrysize*mid;
int c = STRCMP_UC_UC((pcre_uchar *)stringname,
(pcre_uchar *)(entry + IMM2_SIZE));
2006-08-31 04:00:23 +08:00
if (c == 0)
{
2012-10-19 15:49:28 +08:00
pcre_uchar *first = entry;
pcre_uchar *last = entry;
2006-08-31 04:00:23 +08:00
while (first > nametable)
{
2012-10-19 15:49:28 +08:00
if (STRCMP_UC_UC((pcre_uchar *)stringname,
(pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break;
2006-08-31 04:00:23 +08:00
first -= entrysize;
}
while (last < lastentry)
{
2012-10-19 15:49:28 +08:00
if (STRCMP_UC_UC((pcre_uchar *)stringname,
(pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break;
2006-08-31 04:00:23 +08:00
last += entrysize;
}
#if defined COMPILE_PCRE8
2006-08-31 04:00:23 +08:00
*firstptr = (char *)first;
*lastptr = (char *)last;
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
*firstptr = (PCRE_UCHAR16 *)first;
*lastptr = (PCRE_UCHAR16 *)last;
#elif defined COMPILE_PCRE32
*firstptr = (PCRE_UCHAR32 *)first;
*lastptr = (PCRE_UCHAR32 *)last;
2012-10-19 15:49:28 +08:00
#endif
2006-08-31 04:00:23 +08:00
return entrysize;
}
if (c > 0) bot = mid + 1; else top = mid;
}
return PCRE_ERROR_NOSUBSTRING;
}
/*************************************************
* Find first set of multiple named strings *
*************************************************/
/* This function allows for duplicate names in the table of named substrings.
It returns the number of the first one that was set in a pattern match.
Arguments:
code the compiled regex
stringname the name of the capturing substring
ovector the vector of matched substrings
Returns: the number of the first that is set,
or the number of the last one if none are set,
or a negative number on error
*/
#if defined COMPILE_PCRE8
2006-08-31 04:00:23 +08:00
static int
get_first_set(const pcre *code, const char *stringname, int *ovector)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
static int
get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector)
#elif defined COMPILE_PCRE32
static int
get_first_set(const pcre32 *code, PCRE_SPTR32 stringname, int *ovector)
2012-10-19 15:49:28 +08:00
#endif
2006-08-31 04:00:23 +08:00
{
2012-10-19 15:49:28 +08:00
const REAL_PCRE *re = (const REAL_PCRE *)code;
2006-08-31 04:00:23 +08:00
int entrysize;
2012-10-19 15:49:28 +08:00
pcre_uchar *entry;
#if defined COMPILE_PCRE8
2006-08-31 04:00:23 +08:00
char *first, *last;
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_UCHAR16 *first, *last;
#elif defined COMPILE_PCRE32
PCRE_UCHAR32 *first, *last;
2012-10-19 15:49:28 +08:00
#endif
#if defined COMPILE_PCRE8
2007-10-07 18:35:03 +08:00
if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
2006-08-31 04:00:23 +08:00
return pcre_get_stringnumber(code, stringname);
entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
return pcre16_get_stringnumber(code, stringname);
entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last);
#elif defined COMPILE_PCRE32
if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
return pcre32_get_stringnumber(code, stringname);
entrysize = pcre32_get_stringtable_entries(code, stringname, &first, &last);
2012-10-19 15:49:28 +08:00
#endif
2006-08-31 04:00:23 +08:00
if (entrysize <= 0) return entrysize;
2012-10-19 15:49:28 +08:00
for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize)
2006-08-31 04:00:23 +08:00
{
2012-10-19 15:49:28 +08:00
int n = GET2(entry, 0);
2006-08-31 04:00:23 +08:00
if (ovector[n*2] >= 0) return n;
}
2012-10-19 15:49:28 +08:00
return GET2(entry, 0);
2006-08-31 04:00:23 +08:00
}
2000-04-12 02:40:47 +08:00
/*************************************************
* Copy captured string to given buffer *
*************************************************/
/* This function copies a single captured substring into a given buffer.
Note that we use memcpy() rather than strncpy() in case there are binary zeros
in the string.
Arguments:
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringnumber the number of the required substring
buffer where to put the substring
size the size of the buffer
Returns: if successful:
the length of the copied string, not including the zero
that is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) buffer too small
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2000-04-12 02:40:47 +08:00
pcre_copy_substring(const char *subject, int *ovector, int stringcount,
int stringnumber, char *buffer, int size)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
int stringnumber, PCRE_UCHAR16 *buffer, int size)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre32_copy_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
int stringnumber, PCRE_UCHAR32 *buffer, int size)
2012-10-19 15:49:28 +08:00
#endif
2000-04-12 02:40:47 +08:00
{
int yield;
if (stringnumber < 0 || stringnumber >= stringcount)
return PCRE_ERROR_NOSUBSTRING;
stringnumber *= 2;
yield = ovector[stringnumber+1] - ovector[stringnumber];
if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
2012-10-19 15:49:28 +08:00
memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield));
2000-04-12 02:40:47 +08:00
buffer[yield] = 0;
return yield;
}
2003-06-28 00:31:24 +08:00
/*************************************************
* Copy named captured string to given buffer *
*************************************************/
/* This function copies a single captured substring into a given buffer,
2006-08-31 04:00:23 +08:00
identifying it by name. If the regex permits duplicate names, the first
substring that is set is chosen.
2003-06-28 00:31:24 +08:00
Arguments:
code the compiled regex
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringname the name of the required substring
buffer where to put the substring
size the size of the buffer
Returns: if successful:
the length of the copied string, not including the zero
that is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) buffer too small
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
*/
#if defined COMPILE_PCRE8
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre_copy_named_substring(const pcre *code, const char *subject,
int *ovector, int stringcount, const char *stringname,
char *buffer, int size)
#elif defined COMPILE_PCRE16
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2012-10-19 15:49:28 +08:00
pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
int *ovector, int stringcount, PCRE_SPTR16 stringname,
PCRE_UCHAR16 *buffer, int size)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre32_copy_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
int *ovector, int stringcount, PCRE_SPTR32 stringname,
PCRE_UCHAR32 *buffer, int size)
2012-10-19 15:49:28 +08:00
#endif
2003-06-28 00:31:24 +08:00
{
2006-08-31 04:00:23 +08:00
int n = get_first_set(code, stringname, ovector);
2003-06-28 00:31:24 +08:00
if (n <= 0) return n;
#if defined COMPILE_PCRE8
2003-06-28 00:31:24 +08:00
return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size);
#elif defined COMPILE_PCRE32
return pcre32_copy_substring(subject, ovector, stringcount, n, buffer, size);
2012-10-19 15:49:28 +08:00
#endif
2003-06-28 00:31:24 +08:00
}
2000-04-12 02:40:47 +08:00
/*************************************************
* Copy all captured strings to new store *
*************************************************/
/* This function gets one chunk of store and builds a list of pointers and all
of the captured substrings in it. A NULL pointer is put on the end of the list.
Arguments:
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
listptr set to point to the list of pointers
Returns: if successful: 0
if not successful:
PCRE_ERROR_NOMEMORY (-6) failed to get store
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2000-04-12 02:40:47 +08:00
pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
const char ***listptr)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount,
PCRE_SPTR16 **listptr)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre32_get_substring_list(PCRE_SPTR32 subject, int *ovector, int stringcount,
PCRE_SPTR32 **listptr)
2012-10-19 15:49:28 +08:00
#endif
2000-04-12 02:40:47 +08:00
{
int i;
2012-10-19 15:49:28 +08:00
int size = sizeof(pcre_uchar *);
2000-04-12 02:40:47 +08:00
int double_count = stringcount * 2;
2012-10-19 15:49:28 +08:00
pcre_uchar **stringlist;
pcre_uchar *p;
2000-04-12 02:40:47 +08:00
for (i = 0; i < double_count; i += 2)
2012-10-19 15:49:28 +08:00
size += sizeof(pcre_uchar *) + IN_UCHARS(ovector[i+1] - ovector[i] + 1);
2000-04-12 02:40:47 +08:00
2012-10-19 15:49:28 +08:00
stringlist = (pcre_uchar **)(PUBL(malloc))(size);
2000-04-12 02:40:47 +08:00
if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
#if defined COMPILE_PCRE8
2000-04-12 02:40:47 +08:00
*listptr = (const char **)stringlist;
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
*listptr = (PCRE_SPTR16 *)stringlist;
#elif defined COMPILE_PCRE32
*listptr = (PCRE_SPTR32 *)stringlist;
2012-10-19 15:49:28 +08:00
#endif
p = (pcre_uchar *)(stringlist + stringcount + 1);
2000-04-12 02:40:47 +08:00
for (i = 0; i < double_count; i += 2)
{
int len = ovector[i+1] - ovector[i];
2012-10-19 15:49:28 +08:00
memcpy(p, subject + ovector[i], IN_UCHARS(len));
2000-04-12 02:40:47 +08:00
*stringlist++ = p;
p += len;
*p++ = 0;
}
*stringlist = NULL;
return 0;
}
2001-02-21 06:00:33 +08:00
/*************************************************
* Free store obtained by get_substring_list *
*************************************************/
/* This function exists for the benefit of people calling PCRE from non-C
2012-10-19 15:49:28 +08:00
programs that can call its functions, but not free() or (PUBL(free))()
directly.
2001-02-21 06:00:33 +08:00
Argument: the result of a previous pcre_get_substring_list()
Returns: nothing
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
2001-02-21 06:00:33 +08:00
pcre_free_substring_list(const char **pointer)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
pcre16_free_substring_list(PCRE_SPTR16 *pointer)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
pcre32_free_substring_list(PCRE_SPTR32 *pointer)
2012-10-19 15:49:28 +08:00
#endif
2001-02-21 06:00:33 +08:00
{
2012-10-19 15:49:28 +08:00
(PUBL(free))((void *)pointer);
2001-02-21 06:00:33 +08:00
}
2000-04-12 02:40:47 +08:00
/*************************************************
* Copy captured string to new store *
*************************************************/
/* This function copies a single captured substring into a piece of new
store
Arguments:
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringnumber the number of the required substring
stringptr where to put a pointer to the substring
Returns: if successful:
the length of the string, not including the zero that
is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) failed to get store
PCRE_ERROR_NOSUBSTRING (-7) substring not present
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2000-04-12 02:40:47 +08:00
pcre_get_substring(const char *subject, int *ovector, int stringcount,
int stringnumber, const char **stringptr)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
int stringnumber, PCRE_SPTR16 *stringptr)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre32_get_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
int stringnumber, PCRE_SPTR32 *stringptr)
2012-10-19 15:49:28 +08:00
#endif
2000-04-12 02:40:47 +08:00
{
int yield;
2012-10-19 15:49:28 +08:00
pcre_uchar *substring;
2000-04-12 02:40:47 +08:00
if (stringnumber < 0 || stringnumber >= stringcount)
return PCRE_ERROR_NOSUBSTRING;
stringnumber *= 2;
yield = ovector[stringnumber+1] - ovector[stringnumber];
2012-10-19 15:49:28 +08:00
substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1));
2000-04-12 02:40:47 +08:00
if (substring == NULL) return PCRE_ERROR_NOMEMORY;
2012-10-19 15:49:28 +08:00
memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield));
2000-04-12 02:40:47 +08:00
substring[yield] = 0;
#if defined COMPILE_PCRE8
2012-10-19 15:49:28 +08:00
*stringptr = (const char *)substring;
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
*stringptr = (PCRE_SPTR16)substring;
#elif defined COMPILE_PCRE32
*stringptr = (PCRE_SPTR32)substring;
2012-10-19 15:49:28 +08:00
#endif
2000-04-12 02:40:47 +08:00
return yield;
}
2001-02-21 06:00:33 +08:00
2003-06-28 00:31:24 +08:00
/*************************************************
* Copy named captured string to new store *
*************************************************/
/* This function copies a single captured substring, identified by name, into
2006-08-31 04:00:23 +08:00
new store. If the regex permits duplicate names, the first substring that is
set is chosen.
2003-06-28 00:31:24 +08:00
Arguments:
code the compiled regex
subject the subject string that was matched
ovector pointer to the offsets table
stringcount the number of substrings that were captured
(i.e. the yield of the pcre_exec call, unless
that was zero, in which case it should be 1/3
of the offset table size)
stringname the name of the required substring
stringptr where to put the pointer
Returns: if successful:
the length of the copied string, not including the zero
that is put on the end; can be zero
if not successful:
PCRE_ERROR_NOMEMORY (-6) couldn't get memory
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2012-10-19 15:49:28 +08:00
pcre_get_named_substring(const pcre *code, const char *subject,
int *ovector, int stringcount, const char *stringname,
const char **stringptr)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
int *ovector, int stringcount, PCRE_SPTR16 stringname,
PCRE_SPTR16 *stringptr)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre32_get_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
int *ovector, int stringcount, PCRE_SPTR32 stringname,
PCRE_SPTR32 *stringptr)
2012-10-19 15:49:28 +08:00
#endif
2003-06-28 00:31:24 +08:00
{
2006-08-31 04:00:23 +08:00
int n = get_first_set(code, stringname, ovector);
2003-06-28 00:31:24 +08:00
if (n <= 0) return n;
#if defined COMPILE_PCRE8
2003-06-28 00:31:24 +08:00
return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
return pcre16_get_substring(subject, ovector, stringcount, n, stringptr);
#elif defined COMPILE_PCRE32
return pcre32_get_substring(subject, ovector, stringcount, n, stringptr);
2012-10-19 15:49:28 +08:00
#endif
2003-06-28 00:31:24 +08:00
}
2001-02-21 06:00:33 +08:00
/*************************************************
* Free store obtained by get_substring *
*************************************************/
/* This function exists for the benefit of people calling PCRE from non-C
2012-10-19 15:49:28 +08:00
programs that can call its functions, but not free() or (PUBL(free))()
directly.
2001-02-21 06:00:33 +08:00
Argument: the result of a previous pcre_get_substring()
Returns: nothing
*/
#if defined COMPILE_PCRE8
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
2001-02-21 06:00:33 +08:00
pcre_free_substring(const char *pointer)
#elif defined COMPILE_PCRE16
2012-10-19 15:49:28 +08:00
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
pcre16_free_substring(PCRE_SPTR16 pointer)
#elif defined COMPILE_PCRE32
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
pcre32_free_substring(PCRE_SPTR32 pointer)
2012-10-19 15:49:28 +08:00
#endif
2001-02-21 06:00:33 +08:00
{
2012-10-19 15:49:28 +08:00
(PUBL(free))((void *)pointer);
2001-02-21 06:00:33 +08:00
}
/* End of pcre_get.c */