| 6 |
and semantics are as close as possible to those of the Perl 5 language. |
and semantics are as close as possible to those of the Perl 5 language. |
| 7 |
|
|
| 8 |
Written by Philip Hazel |
Written by Philip Hazel |
| 9 |
Copyright (c) 1997-2005 University of Cambridge |
Copyright (c) 1997-2006 University of Cambridge |
| 10 |
|
|
| 11 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
| 12 |
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
| 131 |
* Translate error code to string * |
* Translate error code to string * |
| 132 |
*************************************************/ |
*************************************************/ |
| 133 |
|
|
| 134 |
PCRE_EXPORT size_t |
PCRE_DATA_SCOPE size_t |
| 135 |
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) |
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) |
| 136 |
{ |
{ |
| 137 |
const char *message, *addmessage; |
const char *message, *addmessage; |
| 166 |
* Free store held by a regex * |
* Free store held by a regex * |
| 167 |
*************************************************/ |
*************************************************/ |
| 168 |
|
|
| 169 |
PCRE_EXPORT void |
PCRE_DATA_SCOPE void |
| 170 |
regfree(regex_t *preg) |
regfree(regex_t *preg) |
| 171 |
{ |
{ |
| 172 |
(pcre_free)(preg->re_pcre); |
(pcre_free)(preg->re_pcre); |
| 189 |
various non-zero codes on failure |
various non-zero codes on failure |
| 190 |
*/ |
*/ |
| 191 |
|
|
| 192 |
PCRE_EXPORT int |
PCRE_DATA_SCOPE int |
| 193 |
regcomp(regex_t *preg, const char *pattern, int cflags) |
regcomp(regex_t *preg, const char *pattern, int cflags) |
| 194 |
{ |
{ |
| 195 |
const char *errorptr; |
const char *errorptr; |
| 197 |
int errorcode; |
int errorcode; |
| 198 |
int options = 0; |
int options = 0; |
| 199 |
|
|
| 200 |
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS; |
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS; |
| 201 |
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE; |
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE; |
| 202 |
if ((cflags & REG_DOTALL) != 0) options |= PCRE_DOTALL; |
if ((cflags & REG_DOTALL) != 0) options |= PCRE_DOTALL; |
| 203 |
|
if ((cflags & REG_NOSUB) != 0) options |= PCRE_NO_AUTO_CAPTURE; |
| 204 |
|
if ((cflags & REG_UTF8) != 0) options |= PCRE_UTF8; |
| 205 |
|
|
| 206 |
preg->re_pcre = pcre_compile2(pattern, options, &errorcode, &errorptr, |
preg->re_pcre = pcre_compile2(pattern, options, &errorcode, &errorptr, |
| 207 |
&erroffset, NULL); |
&erroffset, NULL); |
| 225 |
the POSIX structures as was done in earlier releases when PCRE needed only 2 |
the POSIX structures as was done in earlier releases when PCRE needed only 2 |
| 226 |
ints. However, if the number of possible capturing brackets is small, use a |
ints. However, if the number of possible capturing brackets is small, use a |
| 227 |
block of store on the stack, to reduce the use of malloc/free. The threshold is |
block of store on the stack, to reduce the use of malloc/free. The threshold is |
| 228 |
in a macro that can be changed at configure time. */ |
in a macro that can be changed at configure time. |
| 229 |
|
|
| 230 |
PCRE_EXPORT int |
If REG_NOSUB was specified at compile time, the PCRE_NO_AUTO_CAPTURE flag will |
| 231 |
|
be set. When this is the case, the nmatch and pmatch arguments are ignored, and |
| 232 |
|
the only result is yes/no/error. */ |
| 233 |
|
|
| 234 |
|
PCRE_DATA_SCOPE int |
| 235 |
regexec(const regex_t *preg, const char *string, size_t nmatch, |
regexec(const regex_t *preg, const char *string, size_t nmatch, |
| 236 |
regmatch_t pmatch[], int eflags) |
regmatch_t pmatch[], int eflags) |
| 237 |
{ |
{ |
| 240 |
int *ovector = NULL; |
int *ovector = NULL; |
| 241 |
int small_ovector[POSIX_MALLOC_THRESHOLD * 3]; |
int small_ovector[POSIX_MALLOC_THRESHOLD * 3]; |
| 242 |
BOOL allocated_ovector = FALSE; |
BOOL allocated_ovector = FALSE; |
| 243 |
|
BOOL nosub = |
| 244 |
|
(((const pcre *)preg->re_pcre)->options & PCRE_NO_AUTO_CAPTURE) != 0; |
| 245 |
|
|
| 246 |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
| 247 |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
| 248 |
|
|
| 249 |
((regex_t *)preg)->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
((regex_t *)preg)->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
| 250 |
|
|
| 251 |
if (nmatch > 0) |
/* When no string data is being returned, ensure that nmatch is zero. |
| 252 |
|
Otherwise, ensure the vector for holding the return data is large enough. */ |
| 253 |
|
|
| 254 |
|
if (nosub) nmatch = 0; |
| 255 |
|
|
| 256 |
|
else if (nmatch > 0) |
| 257 |
{ |
{ |
| 258 |
if (nmatch <= POSIX_MALLOC_THRESHOLD) |
if (nmatch <= POSIX_MALLOC_THRESHOLD) |
| 259 |
{ |
{ |
| 261 |
} |
} |
| 262 |
else |
else |
| 263 |
{ |
{ |
| 264 |
|
if (nmatch > INT_MAX/(sizeof(int) * 3)) return REG_ESPACE; |
| 265 |
ovector = (int *)malloc(sizeof(int) * nmatch * 3); |
ovector = (int *)malloc(sizeof(int) * nmatch * 3); |
| 266 |
if (ovector == NULL) return REG_ESPACE; |
if (ovector == NULL) return REG_ESPACE; |
| 267 |
allocated_ovector = TRUE; |
allocated_ovector = TRUE; |
| 276 |
if (rc >= 0) |
if (rc >= 0) |
| 277 |
{ |
{ |
| 278 |
size_t i; |
size_t i; |
| 279 |
for (i = 0; i < (size_t)rc; i++) |
if (!nosub) |
| 280 |
{ |
{ |
| 281 |
pmatch[i].rm_so = ovector[i*2]; |
for (i = 0; i < (size_t)rc; i++) |
| 282 |
pmatch[i].rm_eo = ovector[i*2+1]; |
{ |
| 283 |
|
pmatch[i].rm_so = ovector[i*2]; |
| 284 |
|
pmatch[i].rm_eo = ovector[i*2+1]; |
| 285 |
|
} |
| 286 |
|
if (allocated_ovector) free(ovector); |
| 287 |
|
for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
| 288 |
} |
} |
|
if (allocated_ovector) free(ovector); |
|
|
for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
|
| 289 |
return 0; |
return 0; |
| 290 |
} |
} |
| 291 |
|
|