aboutsummaryrefslogtreecommitdiff
path: root/cleanregex.c
diff options
context:
space:
mode:
Diffstat (limited to 'cleanregex.c')
-rw-r--r--cleanregex.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/cleanregex.c b/cleanregex.c
new file mode 100644
index 0000000..20f3154
--- /dev/null
+++ b/cleanregex.c
@@ -0,0 +1,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);
+}