1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
6 This is a library of functions to support regular expressions whose syntax
7 and semantics are as close as possible to those of the Perl 5 language. See
8 the file Tech.Notes for some information on the internals.
10 Written by: Philip Hazel <ph10@cam.ac.uk>
12 Copyright (c) 1997-2000 University of Cambridge
14 -----------------------------------------------------------------------------
15 Permission is granted to anyone to use this software for any purpose on any
16 computer system, and to redistribute it freely, subject to the following
19 1. This software is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 2. The origin of this software must not be misrepresented, either by
24 explicit claim or by omission.
26 3. Altered versions must be plainly marked as such, and must not be
27 misrepresented as being the original software.
29 4. If PCRE is embedded in any software that is released under the GNU
30 General Purpose Licence (GPL), then the terms of that licence shall
31 supersede any condition above with which it is incompatible.
32 -----------------------------------------------------------------------------
35 /* This module contains some convenience functions for extracting substrings
36 from the subject string after a regex match has succeeded. The original idea
37 for these functions came from Scott Wimer <scottw@cgibuilder.com>. */
40 /* Include the internals header, which itself includes Standard C headers plus
41 the external pcre header. */
47 /*************************************************
48 * Copy captured string to given buffer *
49 *************************************************/
51 /* This function copies a single captured substring into a given buffer.
52 Note that we use memcpy() rather than strncpy() in case there are binary zeros
56 subject the subject string that was matched
57 ovector pointer to the offsets table
58 stringcount the number of substrings that were captured
59 (i.e. the yield of the pcre_exec call, unless
60 that was zero, in which case it should be 1/3
61 of the offset table size)
62 stringnumber the number of the required substring
63 buffer where to put the substring
64 size the size of the buffer
66 Returns: if successful:
67 the length of the copied string, not including the zero
68 that is put on the end; can be zero
70 PCRE_ERROR_NOMEMORY (-6) buffer too small
71 PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
75 pcre_copy_substring(const char *subject, int *ovector, int stringcount,
76 int stringnumber, char *buffer, int size)
79 if (stringnumber < 0 || stringnumber >= stringcount)
80 return PCRE_ERROR_NOSUBSTRING;
82 yield = ovector[stringnumber+1] - ovector[stringnumber];
83 if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
84 memcpy(buffer, subject + ovector[stringnumber], yield);
91 /*************************************************
92 * Copy all captured strings to new store *
93 *************************************************/
95 /* This function gets one chunk of store and builds a list of pointers and all
96 of the captured substrings in it. A NULL pointer is put on the end of the list.
99 subject the subject string that was matched
100 ovector pointer to the offsets table
101 stringcount the number of substrings that were captured
102 (i.e. the yield of the pcre_exec call, unless
103 that was zero, in which case it should be 1/3
104 of the offset table size)
105 listptr set to point to the list of pointers
107 Returns: if successful: 0
109 PCRE_ERROR_NOMEMORY (-6) failed to get store
113 pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
114 const char ***listptr)
117 int size = sizeof(char *);
118 int double_count = stringcount * 2;
122 for (i = 0; i < double_count; i += 2)
123 size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;
125 stringlist = (char **)(pcre_malloc)(size);
126 if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
128 *listptr = (const char **)stringlist;
129 p = (char *)(stringlist + stringcount + 1);
131 for (i = 0; i < double_count; i += 2)
133 int len = ovector[i+1] - ovector[i];
134 memcpy(p, subject + ovector[i], len);
146 /*************************************************
147 * Free store obtained by get_substring_list *
148 *************************************************/
150 /* This function exists for the benefit of people calling PCRE from non-C
151 programs that can call its functions, but not free() or (pcre_free)() directly.
153 Argument: the result of a previous pcre_get_substring_list()
158 pcre_free_substring_list(const char **pointer)
160 (pcre_free)((void *)pointer);
165 /*************************************************
166 * Copy captured string to new store *
167 *************************************************/
169 /* This function copies a single captured substring into a piece of new
173 subject the subject string that was matched
174 ovector pointer to the offsets table
175 stringcount the number of substrings that were captured
176 (i.e. the yield of the pcre_exec call, unless
177 that was zero, in which case it should be 1/3
178 of the offset table size)
179 stringnumber the number of the required substring
180 stringptr where to put a pointer to the substring
182 Returns: if successful:
183 the length of the string, not including the zero that
184 is put on the end; can be zero
186 PCRE_ERROR_NOMEMORY (-6) failed to get store
187 PCRE_ERROR_NOSUBSTRING (-7) substring not present
191 pcre_get_substring(const char *subject, int *ovector, int stringcount,
192 int stringnumber, const char **stringptr)
196 if (stringnumber < 0 || stringnumber >= stringcount)
197 return PCRE_ERROR_NOSUBSTRING;
199 yield = ovector[stringnumber+1] - ovector[stringnumber];
200 substring = (char *)(pcre_malloc)(yield + 1);
201 if (substring == NULL) return PCRE_ERROR_NOMEMORY;
202 memcpy(substring, subject + ovector[stringnumber], yield);
203 substring[yield] = 0;
204 *stringptr = substring;
210 /*************************************************
211 * Free store obtained by get_substring *
212 *************************************************/
214 /* This function exists for the benefit of people calling PCRE from non-C
215 programs that can call its functions, but not free() or (pcre_free)() directly.
217 Argument: the result of a previous pcre_get_substring()
222 pcre_free_substring(const char *pointer)
224 (pcre_free)((void *)pointer);