aboutsummaryrefslogtreecommitdiff
path: root/cleanregex.c
blob: 20f315457d89f5c251b7eba3eb45d1ce23305816 (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
39
40
#include "cleanregex.h"
#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;
}

void cleanrgx_compile(
		CleanString* cs, int64_t flags,
		int64_t* re_code, int64_t* re_regex) {
    char* s = (char*) clstocs(cs);
	regex_t* regex = malloc(sizeof(regex_t*));
	*re_code = regcomp(regex, s, flags);
	*re_regex = (int64_t) regex;
	free(s);
}

int64_t cleanrgx_exec(int64_t* rgx, CleanString* cs) {
	char* s = (char*) clstocs(cs);
	int64_t result = regexec((regex_t*) rgx, s, 0, NULL, 0);
	free(s);
	if (!result) {
		return 1;
	} else if (result == REG_NOMATCH) {
		return 0;
	} else {
		return -1;
	}
}

void cleanrgx_free(regex_t* rgx) {
	regfree(rgx);
}