7 n__pcre_8h
gullradriel edited this page 2026-10-29 20:54:40 +01:00

include/nilorea/n_pcre.h

PCRE helpers for regex matching. More...

Structures

Name
struct N_PCRE
N_PCRE structure.

Defines

Name
npcre_print_error(LEVEL, rc)
PCRE error logging function wrapper to get line and func.
PCRE_UTF8
PCRE_UCP
PCRE_MULTILINE
PCRE_EXTENDED
PCRE_DOTALL
PCRE_CASELESS
PCRE_ANCHORED
PCRE2_LIST_FREE_CAST(x)
PCRE2_CODE_UNIT_WIDTH

Detailed Description

PCRE helpers for regex matching.

Author: Castagnier Mickael

Version: 2.0

Date: 04/12/2019

Macros Documentation

define npcre_print_error

#define npcre_print_error(
    __LEVEL__,
    __rc__
)
do {                                                                     \
    _npcre_print_error(__LEVEL__, __FILE__, __func__, __LINE__, __rc__); \
} while (0)

PCRE error logging function wrapper to get line and func.

define PCRE_UTF8

#define PCRE_UTF8 PCRE2_UTF

define PCRE_UCP

#define PCRE_UCP PCRE2_UCP

define PCRE_MULTILINE

#define PCRE_MULTILINE PCRE2_MULTILINE

define PCRE_EXTENDED

#define PCRE_EXTENDED PCRE2_EXTENDED

define PCRE_DOTALL

#define PCRE_DOTALL PCRE2_DOTALL

define PCRE_CASELESS

#define PCRE_CASELESS PCRE2_CASELESS

define PCRE_ANCHORED

#define PCRE_ANCHORED PCRE2_ANCHORED

define PCRE2_LIST_FREE_CAST

#define PCRE2_LIST_FREE_CAST(
    x
)
((PCRE2_UCHAR8**)(void*)(x))

define PCRE2_CODE_UNIT_WIDTH

#define PCRE2_CODE_UNIT_WIDTH 8

Source code

/*
 * 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
 */


#ifndef __NILOREA_PCRE_HELPERS__
#define __NILOREA_PCRE_HELPERS__

#ifdef __cplusplus
extern "C" {
#endif

/* PCRE2 uses a different API than legacy PCRE (pcre.h).
 * We use the 8-bit library (char / UTF-8 style strings). */
#ifndef PCRE2_CODE_UNIT_WIDTH
#define PCRE2_CODE_UNIT_WIDTH 8
#endif
#include <pcre2.h>

#include <nilorea/n_common.h>
#include <nilorea/n_str.h>

/* Compatibility aliases.
 * The legacy PCRE (pcre.h) used PCRE_* option names. PCRE2 uses PCRE2_*.
 * If your existing code still passes PCRE_* flags to npcre_new(), these
 * aliases let it keep compiling.
 */
#ifndef PCRE_CASELESS
#define PCRE_CASELESS PCRE2_CASELESS
#endif
#ifndef PCRE_MULTILINE
#define PCRE_MULTILINE PCRE2_MULTILINE
#endif
#ifndef PCRE_DOTALL
#define PCRE_DOTALL PCRE2_DOTALL
#endif
#ifndef PCRE_EXTENDED
#define PCRE_EXTENDED PCRE2_EXTENDED
#endif
#ifndef PCRE_ANCHORED
#define PCRE_ANCHORED PCRE2_ANCHORED
#endif
#ifndef PCRE_UTF8
#define PCRE_UTF8 PCRE2_UTF
#endif
#ifndef PCRE_UCP
#define PCRE_UCP PCRE2_UCP
#endif

/* pcre2_substring_list_free() had a const PCRE2_UCHAR** argument up to 10.41,
 * then it was changed to PCRE2_UCHAR** in 10.42 to fix the mismatch with
 * pcre2_substring_list(). Use void* as intermediate to satisfy both. */
#ifdef PCRE2_LIST_FREE_CONST
#define PCRE2_LIST_FREE_CAST(x) ((const PCRE2_UCHAR8**)(void*)(x))
#else
#define PCRE2_LIST_FREE_CAST(x) ((PCRE2_UCHAR8**)(void*)(x))
#endif

#define npcre_print_error(__LEVEL__, __rc__)                                 \
    do {                                                                     \
        _npcre_print_error(__LEVEL__, __FILE__, __func__, __LINE__, __rc__); \
    } while (0)


typedef struct N_PCRE {
    char* regexp_str;
    pcre2_code* regexp;
    pcre2_match_data* match_data;
    PCRE2_UCHAR8** match_list;
    int captured;
    pthread_rwlock_t rwlock;
} N_PCRE;

N_PCRE* npcre_new(char* str, int flags);
int npcre_delete(N_PCRE** pcre);
int npcre_clean_match(N_PCRE* pcre);
int npcre_match(char* str, N_PCRE* pcre);
int npcre_match_capture(char* str, N_PCRE* pcre);
N_STR* npcre_replace(N_PCRE* pcre, const char* subject, size_t subject_len, const char* replacement);
int _npcre_print_error(int LOG_LEVEL, const char* file, const char* func, int line, int rc);


#ifdef __cplusplus
}
#endif
/* #ifndef __NILOREA_PCRE_HELPERS__ */
#endif