view th_regex.h @ 639:8c957ad9d4c3

Some more work on regex stuff.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Jan 2020 11:38:28 +0200
parents d191ded8a790
children 9e1f9e1d1487
line wrap: on
line source

/*
 * Simple regular expression matching functionality
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2020 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#ifdef TH_EXPERIMENTAL_REGEX

#ifndef TH_REGEX_H
#define TH_REGEX_H

#include "th_types.h"
#include "th_datastruct.h"


#ifdef __cplusplus
extern "C" {
#endif


//
// Definitions
//
// Character type
typedef char th_regex_char;


enum
{
    TH_REF_CASEFOLD          = 0x0001,
    TH_REF_ANCHORED          = 0x0002,
};


typedef struct
{
    int type;
    th_regex_char start, end;

    size_t nchars;
    th_regex_char *chars;
} th_regex_list_item;


typedef struct
{
    size_t nitems, itemssize;
    th_regex_list_item *items;
} th_regex_list;


struct th_regex_ctx;

typedef struct
{
    int mode, type;
    ssize_t repeatMin, repeatMax;

    union
    {
        th_regex_char chr;

        struct
        {
            size_t nchars;
            th_regex_char *chars;
        } list;

        struct th_regex_ctx *expr;
    } match;
} th_regex_node;


typedef struct th_regex_ctx
{
    size_t nnodes, nodessize;
    th_regex_node *nodes;
} th_regex_ctx;


typedef struct
{
    th_llist_t node;
    size_t start, len;
} th_regex_match_node;


#ifdef TH_EXPERIMENTAL_REGEX_DEBUG
enum
{
    TH_DBG_RE_COMPILE = 0x0001,
    TH_DBG_RE_FREE    = 0x0002,
    TH_DBG_RE_MATCH   = 0x0004,
};

extern int th_dbg_re_flags;
#endif


//
// Functions
//
int      th_regex_compile(th_regex_ctx **pexpr, const th_regex_char *pattern);
void     th_regex_free(th_regex_ctx *expr);

int      th_regex_match(const th_regex_ctx *expr, const th_regex_char *haystack,
         size_t *pnmatches, th_regex_match_node **pmatches, const size_t maxmatches,
         const int flags);
void     th_regex_free_matches(th_regex_match_node *matches);


#ifdef __cplusplus
}
#endif
#endif // TH_REGEX_H

#endif // TH_EXPERIMENTAL_REGEX