aboutsummaryrefslogtreecommitdiff
path: root/regex.c
blob: 8d46a31131a37ffc568a87ccd69c20ea40ef24ae (plain) (blame)
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
#include "regex.h"
#include <stdio.h>
#include <string.h>

char* clstocs(CleanString* cs) {
    char* s = calloc(CleanStringLength(cs) + 1, 1);
    uint8_t i;
    for (i = 0; i < CleanStringLength(cs); i++)
        s[i] = CleanStringCharacters(cs)[i];
    s[i] = 0;
    return s;
}

pcre2_code* cleanregex_pcre2_compile(CleanString* cs, int64_t flags) {
    uint8_t* s = (uint8_t*) clstocs(cs);
    int error; PCRE2_SIZE offset;
    pcre2_code* code = pcre2_compile(s, PCRE2_ZERO_TERMINATED, flags, 
            &error, &offset, NULL);
    if (code)
        return code;
    else
        return NULL;
}

int64_t cleanregex_match(pcre2_code* re, CleanString* sub) {
    pcre2_match_data *data = pcre2_match_data_create_from_pattern(re, NULL);
    uint8_t* csub = (uint8_t*) clstocs(sub);
    unsigned long len = CleanStringLength(sub); 
    int match = pcre2_match(re, csub, len, 0, 0, data, NULL);
    if (match < 0) {
        pcre2_match_data_free(data);
        return match;
    }
    PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(data);
    pcre2_match_data_free(data);
    return ovector[0];
}