summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xs_openssl.h2
-rw-r--r--xs_regex.h47
2 files changed, 48 insertions, 1 deletions
diff --git a/xs_openssl.h b/xs_openssl.h
index d172444..fd57c86 100644
--- a/xs_openssl.h
+++ b/xs_openssl.h
@@ -194,7 +194,7 @@ d_char *xs_evp_sign(char *secret, char *mem, int size)
/* I've learnt all these magical incantations by watching
the Python module code and the OpenSSL manual pages */
- /* Well, "learnt" may be an exaggeration */
+ /* Well, "learnt" may be an overstatement */
md = EVP_get_digestbyname("sha256");
diff --git a/xs_regex.h b/xs_regex.h
new file mode 100644
index 0000000..75ccfc3
--- /dev/null
+++ b/xs_regex.h
@@ -0,0 +1,47 @@
+/* copyright (c) 2022 grunfink - MIT license */
+
+#ifndef _XS_REGEX_H
+
+#define _XS_REGEX_H
+
+d_char *xs_regex_match(char *str, char *rx, int count);
+#define xs_regex_matchall(str, rx) xs_regex_match(str, rx, 0xfffffff)
+
+#ifdef XS_IMPLEMENTATION
+
+#include <regex.h>
+
+d_char *xs_regex_match(char *str, char *rx, int count)
+/* returns a list with upto count matches */
+{
+ regex_t re;
+ regmatch_t rm;
+ d_char *list = NULL;
+ int offset = 0;
+ char *p;
+
+ if (regcomp(&re, rx, REG_EXTENDED))
+ return NULL;
+
+ list = xs_list_new();
+
+ while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
+ /* add the first part */
+ list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so);
+
+ /* add the asciiz */
+ list = xs_str_cat(list, "");
+
+ offset += rm.rm_eo;
+
+ count--;
+ }
+
+ regfree(&re);
+
+ return list;
+}
+
+#endif /* XS_IMPLEMENTATION */
+
+#endif /* XS_REGEX_H */