summaryrefslogtreecommitdiff
path: root/xs_regex.h
diff options
context:
space:
mode:
authorLouis Brauer <louis77@noreply.codeberg.org>2024-05-25 08:05:36 +0000
committerLouis Brauer <louis77@noreply.codeberg.org>2024-05-25 08:05:36 +0000
commit84a767dd0878013194ed7551b5ae6ef715e841a6 (patch)
tree9fb1b2b89e0bfbb4b8bf1e85d840c8653e646bb7 /xs_regex.h
parentcf5718bf4dedb85d2e1a1495f05bfc7e66124022 (diff)
parenta2920800007c291bdf2b5264622cbc713d4961ee (diff)
Merge pull request 'master' (#1) from grunfink/snac2:master into master
Reviewed-on: https://codeberg.org/louis77/snac2/pulls/1
Diffstat (limited to 'xs_regex.h')
-rw-r--r--xs_regex.h43
1 files changed, 30 insertions, 13 deletions
diff --git a/xs_regex.h b/xs_regex.h
index 1adbcf8..d8d2d1d 100644
--- a/xs_regex.h
+++ b/xs_regex.h
@@ -4,6 +4,7 @@
#define _XS_REGEX_H
+int xs_regex_match(const char *str, const char *rx);
xs_list *xs_regex_split_n(const char *str, const char *rx, int count);
#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL)
xs_list *xs_regex_select_n(const char *str, const char *rx, int count);
@@ -15,21 +16,29 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
#ifdef XS_IMPLEMENTATION
+#ifdef __TINYC__
+/* fix a compilation error in tcc */
+#define _REGEX_NELTS(n)
+#endif
+
#include <regex.h>
xs_list *xs_regex_split_n(const char *str, const char *rx, int count)
-/* splits str by regex */
+/* splits str using regex as a separator, at most count times.
+ Always returns a list:
+ len == 0: regcomp error
+ len == 1: full string (no matches)
+ len == odd: first part [ separator / next part ]...
+*/
{
regex_t re;
regmatch_t rm;
int offset = 0;
- xs_list *list = NULL;
+ xs_list *list = xs_list_new();
const char *p;
if (regcomp(&re, rx, REG_EXTENDED))
- return NULL;
-
- list = xs_list_new();
+ return list;
while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
/* add first the leading part of the string */
@@ -60,16 +69,15 @@ xs_list *xs_regex_select_n(const char *str, const char *rx, int count)
{
xs_list *list = xs_list_new();
xs *split = NULL;
- xs_list *p;
- xs_val *v;
+ const xs_val *v;
int n = 0;
+ int c = 0;
/* split */
split = xs_regex_split_n(str, rx, count);
/* now iterate to get only the 'separators' (odd ones) */
- p = split;
- while (xs_list_iter(&p, &v)) {
+ while (xs_list_next(split, &v, &c)) {
if (n & 0x1)
list = xs_list_append(list, v);
@@ -86,13 +94,12 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
{
xs_str *s = xs_str_new(NULL);
xs *split = xs_regex_split_n(str, rx, count);
- xs_list *p;
- xs_val *v;
+ const xs_val *v;
int n = 0;
+ int c = 0;
int pholder = !!strchr(rep, '&');
- p = split;
- while (xs_list_iter(&p, &v)) {
+ while (xs_list_next(split, &v, &c)) {
if (n & 0x1) {
if (pholder) {
/* rep has a placeholder; process char by char */
@@ -128,6 +135,16 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
return s;
}
+
+int xs_regex_match(const char *str, const char *rx)
+/* returns if str matches the regex at least once */
+{
+ xs *l = xs_regex_select_n(str, rx, 1);
+
+ return xs_list_len(l) == 1;
+}
+
+
#endif /* XS_IMPLEMENTATION */
#endif /* XS_REGEX_H */