9 |
|
|
10 |
Written by: Philip Hazel <ph10@cam.ac.uk> |
Written by: Philip Hazel <ph10@cam.ac.uk> |
11 |
|
|
12 |
Copyright (c) 1997-2000 University of Cambridge |
Copyright (c) 1997-2003 University of Cambridge |
13 |
|
|
14 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
15 |
Permission is granted to anyone to use this software for any purpose on any |
Permission is granted to anyone to use this software for any purpose on any |
43 |
#include "internal.h" |
#include "internal.h" |
44 |
|
|
45 |
|
|
46 |
|
/************************************************* |
47 |
|
* Find number for named string * |
48 |
|
*************************************************/ |
49 |
|
|
50 |
|
/* This function is used by the two extraction functions below, as well |
51 |
|
as being generally available. |
52 |
|
|
53 |
|
Arguments: |
54 |
|
code the compiled regex |
55 |
|
stringname the name whose number is required |
56 |
|
|
57 |
|
Returns: the number of the named parentheses, or a negative number |
58 |
|
(PCRE_ERROR_NOSUBSTRING) if not found |
59 |
|
*/ |
60 |
|
|
61 |
|
int |
62 |
|
pcre_get_stringnumber(const pcre *code, const char *stringname) |
63 |
|
{ |
64 |
|
int rc; |
65 |
|
int entrysize; |
66 |
|
int top, bot; |
67 |
|
uschar *nametable; |
68 |
|
|
69 |
|
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0) |
70 |
|
return rc; |
71 |
|
if (top <= 0) return PCRE_ERROR_NOSUBSTRING; |
72 |
|
|
73 |
|
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0) |
74 |
|
return rc; |
75 |
|
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0) |
76 |
|
return rc; |
77 |
|
|
78 |
|
bot = 0; |
79 |
|
while (top > bot) |
80 |
|
{ |
81 |
|
int mid = (top + bot) / 2; |
82 |
|
uschar *entry = nametable + entrysize*mid; |
83 |
|
int c = strcmp(stringname, (char *)(entry + 2)); |
84 |
|
if (c == 0) return (entry[0] << 8) + entry[1]; |
85 |
|
if (c > 0) bot = mid + 1; else top = mid; |
86 |
|
} |
87 |
|
|
88 |
|
return PCRE_ERROR_NOSUBSTRING; |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
/************************************************* |
/************************************************* |
94 |
* Copy captured string to given buffer * |
* Copy captured string to given buffer * |
135 |
|
|
136 |
|
|
137 |
/************************************************* |
/************************************************* |
138 |
|
* Copy named captured string to given buffer * |
139 |
|
*************************************************/ |
140 |
|
|
141 |
|
/* This function copies a single captured substring into a given buffer, |
142 |
|
identifying it by name. |
143 |
|
|
144 |
|
Arguments: |
145 |
|
code the compiled regex |
146 |
|
subject the subject string that was matched |
147 |
|
ovector pointer to the offsets table |
148 |
|
stringcount the number of substrings that were captured |
149 |
|
(i.e. the yield of the pcre_exec call, unless |
150 |
|
that was zero, in which case it should be 1/3 |
151 |
|
of the offset table size) |
152 |
|
stringname the name of the required substring |
153 |
|
buffer where to put the substring |
154 |
|
size the size of the buffer |
155 |
|
|
156 |
|
Returns: if successful: |
157 |
|
the length of the copied string, not including the zero |
158 |
|
that is put on the end; can be zero |
159 |
|
if not successful: |
160 |
|
PCRE_ERROR_NOMEMORY (-6) buffer too small |
161 |
|
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring |
162 |
|
*/ |
163 |
|
|
164 |
|
int |
165 |
|
pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector, |
166 |
|
int stringcount, const char *stringname, char *buffer, int size) |
167 |
|
{ |
168 |
|
int n = pcre_get_stringnumber(code, stringname); |
169 |
|
if (n <= 0) return n; |
170 |
|
return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
/************************************************* |
176 |
* Copy all captured strings to new store * |
* Copy all captured strings to new store * |
177 |
*************************************************/ |
*************************************************/ |
178 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
/************************************************* |
295 |
|
* Copy named captured string to new store * |
296 |
|
*************************************************/ |
297 |
|
|
298 |
|
/* This function copies a single captured substring, identified by name, into |
299 |
|
new store. |
300 |
|
|
301 |
|
Arguments: |
302 |
|
code the compiled regex |
303 |
|
subject the subject string that was matched |
304 |
|
ovector pointer to the offsets table |
305 |
|
stringcount the number of substrings that were captured |
306 |
|
(i.e. the yield of the pcre_exec call, unless |
307 |
|
that was zero, in which case it should be 1/3 |
308 |
|
of the offset table size) |
309 |
|
stringname the name of the required substring |
310 |
|
stringptr where to put the pointer |
311 |
|
|
312 |
|
Returns: if successful: |
313 |
|
the length of the copied string, not including the zero |
314 |
|
that is put on the end; can be zero |
315 |
|
if not successful: |
316 |
|
PCRE_ERROR_NOMEMORY (-6) couldn't get memory |
317 |
|
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring |
318 |
|
*/ |
319 |
|
|
320 |
|
int |
321 |
|
pcre_get_named_substring(const pcre *code, const char *subject, int *ovector, |
322 |
|
int stringcount, const char *stringname, const char **stringptr) |
323 |
|
{ |
324 |
|
int n = pcre_get_stringnumber(code, stringname); |
325 |
|
if (n <= 0) return n; |
326 |
|
return pcre_get_substring(subject, ovector, stringcount, n, stringptr); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
/************************************************* |
/************************************************* |
333 |
* Free store obtained by get_substring * |
* Free store obtained by get_substring * |
334 |
*************************************************/ |
*************************************************/ |