Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_pcre.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#ifndef __NILOREA_PCRE_HELPERS__
29#define __NILOREA_PCRE_HELPERS__
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/* PCRE2 uses a different API than legacy PCRE (pcre.h).
36 * We use the 8-bit library (char / UTF-8 style strings). */
37#ifndef PCRE2_CODE_UNIT_WIDTH
38#define PCRE2_CODE_UNIT_WIDTH 8
39#endif
40#include <pcre2.h>
41
42#include <nilorea/n_common.h>
43#include <nilorea/n_str.h>
44
45/* Compatibility aliases.
46 * The legacy PCRE (pcre.h) used PCRE_* option names. PCRE2 uses PCRE2_*.
47 * If your existing code still passes PCRE_* flags to npcre_new(), these
48 * aliases let it keep compiling.
49 */
50#ifndef PCRE_CASELESS
51#define PCRE_CASELESS PCRE2_CASELESS
52#endif
53#ifndef PCRE_MULTILINE
54#define PCRE_MULTILINE PCRE2_MULTILINE
55#endif
56#ifndef PCRE_DOTALL
57#define PCRE_DOTALL PCRE2_DOTALL
58#endif
59#ifndef PCRE_EXTENDED
60#define PCRE_EXTENDED PCRE2_EXTENDED
61#endif
62#ifndef PCRE_ANCHORED
63#define PCRE_ANCHORED PCRE2_ANCHORED
64#endif
65#ifndef PCRE_UTF8
66#define PCRE_UTF8 PCRE2_UTF
67#endif
68#ifndef PCRE_UCP
69#define PCRE_UCP PCRE2_UCP
70#endif
71
72/* pcre2_substring_list_free() had a const PCRE2_UCHAR** argument up to 10.41,
73 * then it was changed to PCRE2_UCHAR** in 10.42 to fix the mismatch with
74 * pcre2_substring_list(). Use void* as intermediate to satisfy both. */
75#ifdef PCRE2_LIST_FREE_CONST
76#define PCRE2_LIST_FREE_CAST(x) ((const PCRE2_UCHAR8**)(void*)(x))
77#else
78#define PCRE2_LIST_FREE_CAST(x) ((PCRE2_UCHAR8**)(void*)(x))
79#endif
80
82#define npcre_print_error(__LEVEL__, __rc__) \
83 do { \
84 _npcre_print_error(__LEVEL__, __FILE__, __func__, __LINE__, __rc__); \
85 } while (0)
86
93typedef struct N_PCRE {
97 pcre2_code* regexp;
99 pcre2_match_data* match_data;
104 PCRE2_UCHAR8** match_list;
108 pthread_rwlock_t rwlock;
109} N_PCRE;
110
112N_PCRE* npcre_new(char* str, int flags);
114int npcre_delete(N_PCRE** pcre);
116int npcre_clean_match(N_PCRE* pcre);
118int npcre_match(char* str, N_PCRE* pcre);
120int npcre_match_capture(char* str, N_PCRE* pcre);
125N_STR* npcre_replace(N_PCRE* pcre, const char* subject, size_t subject_len, const char* replacement);
127int _npcre_print_error(int LOG_LEVEL, const char* file, const char* func, int line, int rc);
128
133#ifdef __cplusplus
134}
135#endif
136/* #ifndef __NILOREA_PCRE_HELPERS__ */
137#endif
A box including a string and his lenght.
Definition n_str.h:61
PCRE2_UCHAR8 ** match_list
populated match list (NULL-terminated) after npcre_match_capture() Allocated by PCRE2 via pcre2_subst...
Definition n_pcre.h:104
pcre2_code * regexp
regexp
Definition n_pcre.h:97
pcre2_match_data * match_data
match data storage (ovector, etc.)
Definition n_pcre.h:99
char * regexp_str
original regexp string
Definition n_pcre.h:95
pthread_rwlock_t rwlock
rwlock protecting match_data, match_list, and captured
Definition n_pcre.h:108
int captured
flag for match_list cleaning
Definition n_pcre.h:106
N_PCRE * npcre_new(char *str, int flags)
pcre helper, compile and optimize regexp
Definition n_pcre.c:82
int _npcre_print_error(int LOG_LEVEL, const char *file, const char *func, int line, int rc)
print error helper
Definition n_pcre.c:189
int npcre_match_capture(char *str, N_PCRE *pcre)
thread-safe pcre match with captures stored in pcre->match_list
Definition n_pcre.c:323
int npcre_match(char *str, N_PCRE *pcre)
thread-safe pcre match, only returns TRUE/FALSE, no captures stored
Definition n_pcre.c:228
int npcre_clean_match(N_PCRE *pcre)
clean match results from a previous npcre_match_capture
Definition n_pcre.c:166
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...
Definition n_pcre.c:253
int npcre_delete(N_PCRE **pcre)
pcre helper, delete/free a compiled regexp
Definition n_pcre.c:136
N_PCRE structure.
Definition n_pcre.h:93
Common headers and low-level functions & define.
static int LOG_LEVEL
static global maximum wanted log level value
Definition n_log.c:78
N_STR and string function declaration.