97 PCRE2_SIZE erroroffset = 0;
99 pcre->
regexp = pcre2_compile(
101 PCRE2_ZERO_TERMINATED,
108 PCRE2_UCHAR errmsg[256];
109 (void)pcre2_get_error_message(errorcode, errmsg,
sizeof(errmsg));
110 n_log(
LOG_ERR,
"pcre2 compilation of %s failed at offset %zu : %s", str, (
size_t)erroroffset, (
char*)errmsg);
116 pcre->
match_data = pcre2_match_data_create_from_pattern(pcre->
regexp, NULL);
118 n_log(
LOG_ERR,
"pcre2_match_data_create_from_pattern failed (OOM?)");
124 (void)pcre2_jit_compile(pcre->
regexp, PCRE2_JIT_COMPLETE);
140 if ((*pcre)->captured > 0 && (*pcre)->match_list) {
142 (*pcre)->match_list = NULL;
143 (*pcre)->captured = 0;
146 if ((*pcre)->match_data) {
147 pcre2_match_data_free((*pcre)->match_data);
148 (*pcre)->match_data = NULL;
150 if ((*pcre)->regexp) {
151 pcre2_code_free((*pcre)->regexp);
152 (*pcre)->regexp = NULL;
198 _n_log(
LOG_DEBUG, file, func, line,
"no pcre error, match: %d elements in ovector", rc);
203 _n_log(
LOG_LEVEL, file, func, line,
"match OK, but not enough space in ovector to store all elements");
207 PCRE2_UCHAR errmsg[256];
208 PCRE2_SIZE cap =
sizeof(errmsg) /
sizeof(errmsg[0]);
210 int mrc = pcre2_get_error_message(rc, errmsg, cap);
212 _n_log(
LOG_LEVEL, file, func, line,
"PCRE2 error %d: %s", rc, (
const char*)errmsg);
214 _n_log(
LOG_LEVEL, file, func, line,
"Unknown PCRE2 error code: %d", rc);
233 pcre2_match_data* local_match_data = pcre2_match_data_create_from_pattern(pcre->
regexp, NULL);
234 if (!local_match_data) {
235 n_log(
LOG_ERR,
"npcre_match: pcre2_match_data_create_from_pattern failed");
239 int rc = (int)pcre2_match(
242 (PCRE2_SIZE)strlen(str),
248 pcre2_match_data_free(local_match_data);
250 return (rc > 0) ? TRUE : FALSE;
254 pcre2_match_data* md = NULL;
255 PCRE2_UCHAR8* outbuf = NULL;
256 PCRE2_SIZE slen, rlen, cap;
257 N_STR* result = NULL;
264 md = pcre2_match_data_create_from_pattern(pcre->
regexp, NULL);
266 n_log(
LOG_ERR,
"npcre_replace: pcre2_match_data_create_from_pattern failed");
270 slen = (PCRE2_SIZE)subject_len;
271 rlen = (PCRE2_SIZE)strlen(replacement);
275 PCRE2_SIZE used = cap;
277 Malloc(outbuf, PCRE2_UCHAR8, cap);
279 pcre2_match_data_free(md);
282 rc = pcre2_substitute(
284 (PCRE2_SPTR)subject, slen,
286 PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH,
288 (PCRE2_SPTR)replacement, rlen,
290 if (rc == PCRE2_ERROR_NOMEMORY) {
300 pcre2_match_data_free(md);
309 pcre2_match_data_free(md);
332 size_t len = strlen(str);
334 rc = (int)pcre2_match(
346 case PCRE2_ERROR_NOMATCH:
348 case PCRE2_ERROR_NULL:
351 case PCRE2_ERROR_BADOPTION:
354 case PCRE2_ERROR_BADMAGIC:
357 case PCRE2_ERROR_NOMEMORY:
375 PCRE2_SIZE* lens = NULL;
#define init_lock(__rwlock_mutex)
Macro for initializing a rwlock.
#define FreeNoLog(__ptr)
Free Handler without log.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define rw_lock_destroy(__rwlock_mutex)
Macro to destroy rwlock mutex.
#define unlock(__rwlock_mutex)
Macro for releasing read/write lock a rwlock mutex.
#define write_lock(__rwlock_mutex)
Macro for acquiring a write lock on a rwlock mutex.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void _n_log(int level, const char *file, const char *func, int line, const char *format,...)
Logging function.
size_t NSTRBYTE
N_STR base unit.
int char_to_nstr_ex(const char *from, NSTRBYTE nboct, N_STR **to)
Convert a char into a N_STR, extended version.
A box including a string and his lenght.
PCRE2_UCHAR8 ** match_list
populated match list (NULL-terminated) after npcre_match_capture() Allocated by PCRE2 via pcre2_subst...
pcre2_code * regexp
regexp
pcre2_match_data * match_data
match data storage (ovector, etc.)
char * regexp_str
original regexp string
pthread_rwlock_t rwlock
rwlock protecting match_data, match_list, and captured
int captured
flag for match_list cleaning
N_PCRE * npcre_new(char *str, int flags)
From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile aut...
int _npcre_print_error(int LOG_LEVEL, const char *file, const char *func, int line, int rc)
print error associated to pcre error code
int npcre_match_capture(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures.
int npcre_match(char *str, N_PCRE *pcre)
Thread-safe match: returns TRUE/FALSE without modifying the N_PCRE struct.
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
N_STR * npcre_replace(N_PCRE *pcre, const char *subject, size_t subject_len, const char *replacement)
thread-safe regex substitute: replace every match in subject (binary-safe, subject_len bytes) using a...
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
static int LOG_LEVEL
static global maximum wanted log level value
PCRE helpers for regex matching.
#define PCRE2_LIST_FREE_CAST(x)
#define npcre_print_error(__LEVEL__, __rc__)
PCRE error logging function wrapper to get line and func.
N_STR and string function declaration.