2.65 xs_regex.h
  1/* copyright (c) 2022 - 2024 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        list = xs_list_append(list, s1);
 47
 48        /* add now the matched text as the separator */
 49        xs *s2 = xs_str_new_sz(p + rm.rm_so, rm.rm_eo - rm.rm_so);
 50        list = xs_list_append(list, s2);
 51
 52        /* move forward */
 53        offset += rm.rm_eo;
 54
 55        count--;
 56    }
 57
 58    /* add the rest of the string */
 59    list = xs_list_append(list, p);
 60
 61    regfree(&re);
 62
 63    return list;
 64}
 65
 66
 67xs_list *xs_regex_select_n(const char *str, const char *rx, int count)
 68/* selects all matches and return them as a list */
 69{
 70    xs_list *list = xs_list_new();
 71    xs *split = NULL;
 72    const xs_val *v;
 73    int n = 0;
 74
 75    /* split */
 76    split = xs_regex_split_n(str, rx, count);
 77
 78    /* now iterate to get only the 'separators' (odd ones) */
 79    xs_list_foreach(split, v) {
 80        if (n & 0x1)
 81            list = xs_list_append(list, v);
 82
 83        n++;
 84    }
 85
 86    return list;
 87}
 88
 89
 90xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int count)
 91/* replaces all matches with the rep string. If it contains unescaped &,
 92   they are replaced with the match */
 93{
 94    xs_str *s = xs_str_new(NULL);
 95    xs *split = xs_regex_split_n(str, rx, count);
 96    const xs_val *v;
 97    int n = 0;
 98    int pholder = !!strchr(rep, '&');
 99
100    xs_list_foreach(split, v) {
101        if (n & 0x1) {
102            if (pholder) {
103                /* rep has a placeholder; process char by char */
104                const char *p = rep;
105
106                while (*p) {
107                    if (*p == '&')
108                        s = xs_str_cat(s, v);
109                    else {
110                        if (*p == '\\')
111                            p++;
112
113                        if (!*p)
114                            break;
115
116                        s = xs_append_m(s, p, 1);
117                    }
118
119                    p++;
120                }
121            }
122            else
123                s = xs_str_cat(s, rep);
124        }
125        else
126            s = xs_str_cat(s, v);
127
128        n++;
129    }
130
131    xs_free(str);
132
133    return s;
134}
135
136
137int xs_regex_match(const char *str, const char *rx)
138/* returns if str matches the regex at least once */
139{
140    xs *l = xs_regex_select_n(str, rx, 1);
141
142    return xs_list_len(l) == 1;
143}
144
145
146#endif /* XS_IMPLEMENTATION */
147
148#endif /* XS_REGEX_H */