Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_pcre.c
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#include "nilorea/n_log.h"
29#include "nilorea/n_pcre.h"
30
31#include <string.h>
32
33/* Headless self-test of npcre_replace ($N capture refs, global substitution). */
34static int test_replace(const char* pattern, const char* subject, const char* repl, const char* want) {
35 N_PCRE* re = npcre_new((char*)pattern, 0);
36 N_STR* out = re ? npcre_replace(re, subject, strlen(subject), repl) : NULL;
37 int ok = (out && out->data && strcmp(out->data, want) == 0);
38 if (!ok)
39 n_log(LOG_ERR, "npcre_replace(/%s/, '%s', '%s') -> '%s', expected '%s'",
40 pattern, subject, repl, (out && out->data) ? out->data : "(null)", want);
41 if (out)
42 free_nstr(&out);
43 if (re)
44 npcre_delete(&re);
45 return ok ? 0 : 1;
46}
47
48int main(int argc, char** argv) {
49 int failures = 0;
52
53 n_log(LOG_INFO, "Starting !");
54
55 /* always-on regression for npcre_replace (no CLI args required) */
56 failures += test_replace("(\\w+)@(\\w+)", "a@b and c@d", "$2.$1", "b.a and d.c");
57 failures += test_replace("foo", "no match here", "bar", "no match here");
58 failures += test_replace("[0-9]+", "id=42&n=7", "#", "id=#&n=#");
59 failures += test_replace("a", "", "X", "");
60 if (failures) {
61 n_log(LOG_ERR, "ex_pcre: %d npcre_replace failure(s)", failures);
62 exit(1);
63 }
64 n_log(LOG_NOTICE, "ex_pcre: npcre_replace self-tests passed");
65
66 if (argc < 3) {
67 n_log(LOG_INFO, "ex_pcre: pass \"regexp\" \"string\" to also run the match demo");
68 exit(0);
69 }
70 N_PCRE* pcre = npcre_new(argv[1], 0);
71 if (npcre_match_capture(argv[2], pcre) == TRUE) {
72 n_log(LOG_INFO, "MATCHED !");
73 if (pcre->captured > 1) {
74 n_log(LOG_INFO, "CAPTURED !");
75 int it = 0;
76 while (pcre->match_list[it]) {
77 n_log(LOG_INFO, "Match[%d]:%s", it, pcre->match_list[it]);
78 it++;
79 }
80 }
81 } else {
82 n_log(LOG_INFO, "NO MATCH !");
83 }
84
85 /* test npcre_clean_match to clear previous match data */
87 n_log(LOG_INFO, "npcre_clean_match done, captured: %d", pcre->captured);
88
89 /* reuse the same pattern with a second match */
90 if (npcre_match_capture("another test string", pcre) == TRUE) {
91 n_log(LOG_INFO, "Second match succeeded");
92 } else {
93 n_log(LOG_INFO, "Second match: no match");
94 }
95
96 npcre_delete(&pcre);
97
98 exit(0);
99}
static int failures
int main(void)
static int test_replace(const char *pattern, const char *subject, const char *repl, const char *want)
Definition ex_pcre.c:34
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
#define LOG_STDERR
internal, default LOG_TYPE
Definition n_log.h:50
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_INFO
informational
Definition n_log.h:82
char * data
the string
Definition n_str.h:63
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
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
int captured
flag for match_list cleaning
Definition n_pcre.h:106
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...
Definition n_pcre.c:82
int npcre_match_capture(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures.
Definition n_pcre.c:323
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
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)
Free a N_PCRE pointer.
Definition n_pcre.c:136
N_PCRE structure.
Definition n_pcre.h:93
Generic log system.
PCRE helpers for regex matching.