-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
37 lines (33 loc) · 1.19 KB
/
utils.h
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
#include <stddef.h>
#include <k.h>
#include <oniguruma.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int init_regex(OnigRegex *regex, OnigErrorInfo *einfo, const K pattern, const K str, UChar **pattr, UChar **inp) {
*pattr = malloc(NK(pattern) + 1);
if (*pattr == NULL) return ONIGERR_MEMORY;
memcpy(*pattr, (const void *)pattern, NK(pattern));
(*pattr)[NK(pattern)] = '\0';
*inp = malloc(NK(str) + 1);
if (*inp == NULL) { free(*pattr); return ONIGERR_MEMORY; }
memcpy(*inp, (const void *)str, NK(str));
(*inp)[NK(str)] = '\0';
return onig_new(regex, *pattr, *pattr + strlen((char *)*pattr), ONIG_OPTION_DEFAULT, ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, einfo);
}
#ifndef COMMON_INCLUDE_FILE
#define COMMON_INCLUDE_FILE
#define INIT_REGEX(pattern, str, regex, einfo, region, pattr, inp) \
OnigRegex regex; \
OnigErrorInfo einfo; \
OnigRegion *region; \
UChar *pattr, *inp; \
if (init_regex(®ex, &einfo, pattern, str, &pattr, &inp) != ONIG_NORMAL) { \
return Ki(-1); \
} \
region = onig_region_new();
void cleanup_regex(OnigRegex regex, OnigRegion *region) {
onig_region_free(region, 1);
onig_free(regex);
}
#endif