2.82 xs_regex.h
  1/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  2
  3#ifndef _XS_REGEX_H
  4
  5#define _XS_REGEX_H
  6
  7int xs_regex_match(const char *str, const char *rx);
  8xs_list *xs_regex_split_n(const char *str, const char *rx, int count);
  9#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL)
 10xs_list *xs_regex_select_n(const char *str, const char *rx, int count);
 11#define xs_regex_select(str, rx) xs_regex_select_n(str, rx, XS_ALL)
 12xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int count);
 13#define xs_regex_replace_i(str, rx, rep) xs_regex_replace_in(str, rx, rep, XS_ALL)
 14#define xs_regex_replace_n(str, rx, rep, count) xs_regex_replace_in(xs_dup(str), rx, rep, count)
 15#define xs_regex_replace(str, rx, rep) xs_regex_replace_in(xs_dup(str), rx, rep, XS_ALL)
 16
 17#ifdef XS_IMPLEMENTATION
 18
 19#ifdef __TINYC__
 20/* fix a compilation error in tcc */
 21#define _REGEX_NELTS(n)
 22#endif
 23
 24#include <regex.h>
 25
 26xs_list *xs_regex_split_n(const char *str, const char *rx, int count)
 27/* splits str using regex as a separator, at most count times.
 28    Always returns a list:
 29    len == 0: regcomp error
 30    len == 1: full string (no matches)
 31    len == odd: first part [ separator / next part ]...
 32*/
 33{
 34    regex_t re;
 35    regmatch_t rm;
 36    int offset = 0;
 37    xs_list *list = xs_list_new();
 38    const char *p;
 39
 40    if (regcomp(&re, rx, REG_EXTENDED))
 41        return list;
 42
 43    while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
 44        /* add first the leading part of the string */
 45        xs *s1 = xs_str_new_sz(p, rm.rm_so);
 46
 47        list = xs_list_append(list, xs_is_string(s1) ? s1 : "");
 48
 49        /* add now the matched text as the separator */
 50        xs *s2 = xs_str_new_sz(p + rm.rm_so, rm.rm_eo - rm.rm_so);
 51
 52        list = xs_list_append(list, xs_is_string(s2) ? s2 : "");
 53
 54        /* move forward */
 55        offset += rm.rm_eo;
 56
 57        count--;
 58    }
 59
 60    /* add the rest of the string */
 61    list = xs_list_append(list, p);
 62
 63    regfree(&re);
 64
 65    return list;
 66}
 67
 68
 69xs_list *xs_regex_select_n(const char *str, const char *rx, int count)
 70/* selects all matches and return them as a list */
 71{
 72    xs_list *list = xs_list_new();
 73    xs *split = NULL;
 74    const xs_val *v;
 75    int n = 0;
 76
 77    /* split */
 78    split = xs_regex_split_n(str, rx, count);
 79
 80    /* now iterate to get only the 'separators' (odd ones) */
 81    xs_list_foreach(split, v) {
 82        if (n & 0x1)
 83            list = xs_list_append(list, v);
 84
 85        n++;
 86    }
 87
 88    return list;
 89}
 90
 91
 92xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int count)
 93/* replaces all matches with the rep string. If it contains unescaped &,
 94   they are replaced with the match */
 95{
 96    xs_str *s = xs_str_new(NULL);
 97    xs *split = xs_regex_split_n(str, rx, count);
 98    const xs_val *v;
 99    int n = 0;
100    int pholder = !!strchr(rep, '&');
101
102    xs_list_foreach(split, v) {
103        if (n & 0x1) {
104            if (pholder) {
105                /* rep has a placeholder; process char by char */
106                const char *p = rep;
107
108                while (*p) {
109                    if (*p == '&')
110                        s = xs_str_cat(s, v);
111                    else {
112                        if (*p == '\\')
113                            p++;
114
115                        if (!*p)
116                            break;
117
118                        s = xs_append_m(s, p, 1);
119                    }
120
121                    p++;
122                }
123            }
124            else
125                s = xs_str_cat(s, rep);
126        }
127        else
128            s = xs_str_cat(s, v);
129
130        n++;
131    }
132
133    xs_free(str);
134
135    return s;
136}
137
138
139int xs_regex_match(const char *str, const char *rx)
140/* returns if str matches the regex at least once */
141{
142    xs *l = xs_regex_select_n(str, rx, 1);
143
144    return xs_list_len(l) == 1;
145}
146
147
148#endif /* XS_IMPLEMENTATION */
149
150#endif /* XS_REGEX_H */