From 323f818a6ce33d021bc0a6d34064598917e68c91 Mon Sep 17 00:00:00 2001 From: Ben Fuhrmannek Date: Sat, 5 Feb 2022 12:22:13 +0100 Subject: introduced sp_regexp / store original regex --- src/sp_pcre_compat.h | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'src/sp_pcre_compat.h') diff --git a/src/sp_pcre_compat.h b/src/sp_pcre_compat.h index 725004d..6e9d91a 100644 --- a/src/sp_pcre_compat.h +++ b/src/sp_pcre_compat.h @@ -17,12 +17,36 @@ #endif sp_pcre* sp_pcre_compile(const char* str); -void sp_pcre_free(sp_pcre* regexp); -#define sp_is_regexp_matching_zend(regexp, zstr) \ - sp_is_regexp_matching_len(regexp, ZSTR_VAL(zstr), ZSTR_LEN(zstr)) -#define sp_is_regexp_matching(regexp, str) \ - sp_is_regexp_matching_len(regexp, str, strlen(str)) -bool sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str, - size_t len); +static inline void sp_pcre_free(sp_pcre* regexp) { +#ifdef SP_HAS_PCRE2 + pcre2_code_free(regexp); +#endif +} +bool sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str, size_t len); + + +typedef struct { + sp_pcre *re; + zend_string *pattern; +} sp_regexp; + +#define sp_is_regexp_matching_zstr(regexp, zstr) sp_is_regexp_matching_len(regexp->re, ZSTR_VAL(zstr), ZSTR_LEN(zstr)) +#define sp_is_regexp_matching(regexp, str) sp_is_regexp_matching_len(regexp->re, str, strlen(str)) +static inline sp_regexp* sp_regexp_compile(zend_string *zstr) { + sp_pcre *re = sp_pcre_compile(ZSTR_VAL(zstr)); + if (!re) { return NULL; } + sp_regexp *ret = pecalloc(sizeof(sp_regexp), 1, 1); + ret->re = re; + ret->pattern = zstr; + return ret; +} +static inline void sp_regexp_free(sp_regexp *regexp) { + if (regexp) { + if (regexp->re) { sp_pcre_free(regexp->re); } + if (regexp->pattern) { zend_string_release(regexp->pattern); } + pefree(regexp, 1); + } +} + #endif // SP_PCRE_COMPAT_H -- cgit v1.3