snac2/xs_regex.h

81 lines
1.9 KiB
C
Raw Normal View History

2023-07-28 09:34:18 +00:00
/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
2022-09-27 08:03:41 +00:00
#ifndef _XS_REGEX_H
#define _XS_REGEX_H
2023-01-28 16:49:02 +00:00
xs_list *xs_regex_split_n(const char *str, const char *rx, int count);
2022-11-24 07:47:02 +00:00
#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL)
2023-01-28 16:49:02 +00:00
xs_list *xs_regex_match_n(const char *str, const char *rx, int count);
2022-11-24 07:47:02 +00:00
#define xs_regex_match(str, rx) xs_regex_match_n(str, rx, XS_ALL)
2022-09-27 08:03:41 +00:00
#ifdef XS_IMPLEMENTATION
#include <regex.h>
2023-01-28 16:49:02 +00:00
xs_list *xs_regex_split_n(const char *str, const char *rx, int count)
2022-09-27 15:19:59 +00:00
/* splits str by regex */
2022-09-27 08:03:41 +00:00
{
regex_t re;
regmatch_t rm;
int offset = 0;
2023-01-28 16:49:02 +00:00
xs_list *list = NULL;
2022-09-27 15:19:59 +00:00
const char *p;
2022-09-27 08:03:41 +00:00
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)) {
2022-09-27 15:19:59 +00:00
/* add first the leading part of the string */
list = xs_list_append_m(list, p, rm.rm_so);
2023-01-28 16:49:02 +00:00
list = xs_insert_m(list, xs_size(list) - 1, "", 1);
2022-09-27 08:03:41 +00:00
2022-09-27 15:19:59 +00:00
/* add now the matched text as the separator */
list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so);
2023-01-28 16:49:02 +00:00
list = xs_insert_m(list, xs_size(list) - 1, "", 1);
2022-09-27 08:03:41 +00:00
2022-09-27 15:19:59 +00:00
/* move forward */
2022-09-27 08:03:41 +00:00
offset += rm.rm_eo;
count--;
}
2022-09-27 15:19:59 +00:00
/* add the rest of the string */
list = xs_list_append(list, p);
2022-09-27 08:03:41 +00:00
regfree(&re);
return list;
}
2022-09-27 15:19:59 +00:00
2023-01-28 16:49:02 +00:00
xs_list *xs_regex_match_n(const char *str, const char *rx, int count)
2022-09-27 15:19:59 +00:00
/* returns a list with upto count matches */
{
2023-01-28 16:49:02 +00:00
xs_list *list = xs_list_new();
2022-09-27 15:19:59 +00:00
xs *split = NULL;
2023-01-28 16:49:02 +00:00
xs_list *p;
xs_val *v;
2022-09-27 15:19:59 +00:00
int n = 0;
/* split */
2022-10-07 11:48:53 +00:00
split = xs_regex_split_n(str, rx, count);
2022-09-27 15:19:59 +00:00
/* now iterate to get only the 'separators' (odd ones) */
2022-10-07 11:48:53 +00:00
p = split;
2022-09-27 15:19:59 +00:00
while (xs_list_iter(&p, &v)) {
if (n & 0x1)
list = xs_list_append(list, v);
n++;
}
return list;
}
2022-09-27 08:03:41 +00:00
#endif /* XS_IMPLEMENTATION */
#endif /* XS_REGEX_H */