Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_pcre.c

Nilorea Library pcre api test.

Nilorea Library pcre api test

Author
Castagnier Mickael
Version
1.0
Date
04/12/2019
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_log.h"
#include "nilorea/n_pcre.h"
#include <string.h>
/* Headless self-test of npcre_replace ($N capture refs, global substitution). */
static int test_replace(const char* pattern, const char* subject, const char* repl, const char* want) {
N_PCRE* re = npcre_new((char*)pattern, 0);
N_STR* out = re ? npcre_replace(re, subject, strlen(subject), repl) : NULL;
int ok = (out && out->data && strcmp(out->data, want) == 0);
if (!ok)
n_log(LOG_ERR, "npcre_replace(/%s/, '%s', '%s') -> '%s', expected '%s'",
pattern, subject, repl, (out && out->data) ? out->data : "(null)", want);
if (out)
free_nstr(&out);
if (re)
return ok ? 0 : 1;
}
int main(int argc, char** argv) {
int failures = 0;
n_log(LOG_INFO, "Starting !");
/* always-on regression for npcre_replace (no CLI args required) */
failures += test_replace("(\\w+)@(\\w+)", "a@b and c@d", "$2.$1", "b.a and d.c");
failures += test_replace("foo", "no match here", "bar", "no match here");
failures += test_replace("[0-9]+", "id=42&n=7", "#", "id=#&n=#");
failures += test_replace("a", "", "X", "");
if (failures) {
n_log(LOG_ERR, "ex_pcre: %d npcre_replace failure(s)", failures);
exit(1);
}
n_log(LOG_NOTICE, "ex_pcre: npcre_replace self-tests passed");
if (argc < 3) {
n_log(LOG_INFO, "ex_pcre: pass \"regexp\" \"string\" to also run the match demo");
exit(0);
}
N_PCRE* pcre = npcre_new(argv[1], 0);
if (npcre_match_capture(argv[2], pcre) == TRUE) {
n_log(LOG_INFO, "MATCHED !");
if (pcre->captured > 1) {
n_log(LOG_INFO, "CAPTURED !");
int it = 0;
while (pcre->match_list[it]) {
n_log(LOG_INFO, "Match[%d]:%s", it, pcre->match_list[it]);
it++;
}
}
} else {
n_log(LOG_INFO, "NO MATCH !");
}
/* test npcre_clean_match to clear previous match data */
n_log(LOG_INFO, "npcre_clean_match done, captured: %d", pcre->captured);
/* reuse the same pattern with a second match */
if (npcre_match_capture("another test string", pcre) == TRUE) {
n_log(LOG_INFO, "Second match succeeded");
} else {
n_log(LOG_INFO, "Second match: no match");
}
npcre_delete(&pcre);
exit(0);
}
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.