aboutsummaryrefslogtreecommitdiff
path: root/regex.c
diff options
context:
space:
mode:
authorCamil Staps2016-03-21 19:31:24 +0100
committerCamil Staps2016-03-21 19:42:59 +0100
commit58ea61a19cb9bd0f6c600ebbb643e209fdf9d7cb (patch)
tree864264245f5f1ec8d8af0162830afd9bb5a63473 /regex.c
parentInitial commit (diff)
Matching works
Diffstat (limited to 'regex.c')
-rw-r--r--regex.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/regex.c b/regex.c
new file mode 100644
index 0000000..8d46a31
--- /dev/null
+++ b/regex.c
@@ -0,0 +1,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];
+}
+