summaryrefslogtreecommitdiff
path: root/xs_regex.h
blob: 75ccfc32055696c45198107207fd5a70764fbbc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* copyright (c) 2022 grunfink - MIT license */

#ifndef _XS_REGEX_H

#define _XS_REGEX_H

d_char *xs_regex_match(char *str, char *rx, int count);
#define xs_regex_matchall(str, rx) xs_regex_match(str, rx, 0xfffffff)

#ifdef XS_IMPLEMENTATION

#include <regex.h>

d_char *xs_regex_match(char *str, char *rx, int count)
/* returns a list with upto count matches */
{
    regex_t re;
    regmatch_t rm;
    d_char *list = NULL;
    int offset = 0;
    char *p;

    if (regcomp(&re, rx, REG_EXTENDED))
        return NULL;

    list = xs_list_new();

    while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
        /* add the first part */
        list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so);

        /* add the asciiz */
        list = xs_str_cat(list, "");

        offset += rm.rm_eo;

        count--;
    }

    regfree(&re);

    return list;
}

#endif /* XS_IMPLEMENTATION */

#endif /* XS_REGEX_H */