mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-05-30 02:20:11 +00:00
(FSCORE-192) expose api for partial regex matching
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9623 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
parent
221fbdb0f3
commit
870a3ef8ff
@ -61,11 +61,22 @@ SWITCH_DECLARE(void) switch_perform_substitution(switch_regex_t *re, int match_c
|
|||||||
*/
|
*/
|
||||||
SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const char *expression);
|
SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const char *expression);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief Function to evaluate an expression against a string
|
||||||
|
\param target The string to find a match in
|
||||||
|
\param expression The regular expression to run against the string
|
||||||
|
\param partial If non-zero returns SUCCESS if the target is a partial match, on successful return, this is set to non-zero if the match was partial and zero if it was a full match
|
||||||
|
\return Boolean if a match was found or not
|
||||||
|
*/
|
||||||
|
SWITCH_DECLARE(switch_status_t) switch_regex_match_partial(const char *target, const char *expression, int * partial_match);
|
||||||
|
|
||||||
|
|
||||||
#define switch_regex_safe_free(re) if (re) {\
|
#define switch_regex_safe_free(re) if (re) {\
|
||||||
switch_regex_free(re);\
|
switch_regex_free(re);\
|
||||||
re = NULL;\
|
re = NULL;\
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
SWITCH_END_EXTERN_C
|
SWITCH_END_EXTERN_C
|
||||||
|
@ -161,14 +161,15 @@ SWITCH_DECLARE(void) switch_perform_substitution(switch_regex_t *re, int match_c
|
|||||||
substituted[y++] = '\0';
|
substituted[y++] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const char *expression)
|
SWITCH_DECLARE(switch_status_t) switch_regex_match_partial(const char *target, const char *expression, int * partial)
|
||||||
{
|
{
|
||||||
const char *error = NULL; /* Used to hold any errors */
|
const char *error = NULL; /* Used to hold any errors */
|
||||||
int error_offset = 0; /* Holds the offset of an error */
|
int error_offset = 0; /* Holds the offset of an error */
|
||||||
pcre *pcre_prepared = NULL; /* Holds the compiled regex */
|
pcre *pcre_prepared = NULL; /* Holds the compiled regex */
|
||||||
int match_count = 0; /* Number of times the regex was matched */
|
int match_count = 0; /* Number of times the regex was matched */
|
||||||
int offset_vectors[2]; /* not used, but has to exist or pcre won't even try to find a match */
|
int offset_vectors[2]; /* not used, but has to exist or pcre won't even try to find a match */
|
||||||
|
int pcre_flags = 0;
|
||||||
|
|
||||||
/* Compile the expression */
|
/* Compile the expression */
|
||||||
pcre_prepared = pcre_compile(expression, 0, &error, &error_offset, NULL);
|
pcre_prepared = pcre_compile(expression, 0, &error, &error_offset, NULL);
|
||||||
|
|
||||||
@ -186,8 +187,13 @@ SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const cha
|
|||||||
/* We definitely didn't match anything */
|
/* We definitely didn't match anything */
|
||||||
return SWITCH_STATUS_FALSE;
|
return SWITCH_STATUS_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (*partial) {
|
||||||
|
pcre_flags = PCRE_PARTIAL;
|
||||||
|
}
|
||||||
|
|
||||||
/* So far so good, run the regex */
|
/* So far so good, run the regex */
|
||||||
match_count = pcre_exec(pcre_prepared, NULL, target, (int) strlen(target), 0, 0, offset_vectors, sizeof(offset_vectors) / sizeof(offset_vectors[0]));
|
match_count = pcre_exec(pcre_prepared, NULL, target, (int) strlen(target), 0, pcre_flags, offset_vectors, sizeof(offset_vectors) / sizeof(offset_vectors[0]));
|
||||||
|
|
||||||
/* Clean up */
|
/* Clean up */
|
||||||
if (pcre_prepared) {
|
if (pcre_prepared) {
|
||||||
@ -199,12 +205,23 @@ SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const cha
|
|||||||
|
|
||||||
/* Was it a match made in heaven? */
|
/* Was it a match made in heaven? */
|
||||||
if (match_count > 0) {
|
if (match_count > 0) {
|
||||||
|
*partial = 0;
|
||||||
|
return SWITCH_STATUS_SUCCESS;
|
||||||
|
} else if (match_count == PCRE_ERROR_PARTIAL) {
|
||||||
|
/* yes it is already set, but the code is clearer this way */
|
||||||
|
*partial = 1;
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
return SWITCH_STATUS_FALSE;
|
return SWITCH_STATUS_FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const char *expression)
|
||||||
|
{
|
||||||
|
int partial = 0;
|
||||||
|
return switch_regex_match_partial(target, expression, &partial);
|
||||||
|
}
|
||||||
|
|
||||||
/* For Emacs:
|
/* For Emacs:
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* mode:c
|
* mode:c
|
||||||
|
Loading…
x
Reference in New Issue
Block a user