summaryrefslogtreecommitdiff
path: root/xs_regex.h
diff options
context:
space:
mode:
authorGravatar default2023-01-28 17:49:02 +0100
committerGravatar default2023-01-28 17:49:02 +0100
commit876bebd9ac904ca930117237edaf8c3dcae7a922 (patch)
tree7e91e26c49e18fd80c7de93ff275ffce83fb14df /xs_regex.h
parentBumped version. (diff)
downloadsnac2-876bebd9ac904ca930117237edaf8c3dcae7a922.tar.gz
snac2-876bebd9ac904ca930117237edaf8c3dcae7a922.tar.xz
snac2-876bebd9ac904ca930117237edaf8c3dcae7a922.zip
Backport from xs.
Diffstat (limited to '')
-rw-r--r--xs_regex.h19
1 files changed, 10 insertions, 9 deletions
diff --git a/xs_regex.h b/xs_regex.h
index c01a19a..79b1b3a 100644
--- a/xs_regex.h
+++ b/xs_regex.h
@@ -4,22 +4,22 @@
4 4
5#define _XS_REGEX_H 5#define _XS_REGEX_H
6 6
7d_char *xs_regex_split_n(const char *str, const char *rx, int count); 7xs_list *xs_regex_split_n(const char *str, const char *rx, int count);
8#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL) 8#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL)
9d_char *xs_regex_match_n(const char *str, const char *rx, int count); 9xs_list *xs_regex_match_n(const char *str, const char *rx, int count);
10#define xs_regex_match(str, rx) xs_regex_match_n(str, rx, XS_ALL) 10#define xs_regex_match(str, rx) xs_regex_match_n(str, rx, XS_ALL)
11 11
12#ifdef XS_IMPLEMENTATION 12#ifdef XS_IMPLEMENTATION
13 13
14#include <regex.h> 14#include <regex.h>
15 15
16d_char *xs_regex_split_n(const char *str, const char *rx, int count) 16xs_list *xs_regex_split_n(const char *str, const char *rx, int count)
17/* splits str by regex */ 17/* splits str by regex */
18{ 18{
19 regex_t re; 19 regex_t re;
20 regmatch_t rm; 20 regmatch_t rm;
21 int offset = 0; 21 int offset = 0;
22 d_char *list = NULL; 22 xs_list *list = NULL;
23 const char *p; 23 const char *p;
24 24
25 if (regcomp(&re, rx, REG_EXTENDED)) 25 if (regcomp(&re, rx, REG_EXTENDED))
@@ -30,11 +30,11 @@ d_char *xs_regex_split_n(const char *str, const char *rx, int count)
30 while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) { 30 while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
31 /* add first the leading part of the string */ 31 /* add first the leading part of the string */
32 list = xs_list_append_m(list, p, rm.rm_so); 32 list = xs_list_append_m(list, p, rm.rm_so);
33 list = xs_str_cat(list, ""); 33 list = xs_insert_m(list, xs_size(list) - 1, "", 1);
34 34
35 /* add now the matched text as the separator */ 35 /* add now the matched text as the separator */
36 list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so); 36 list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so);
37 list = xs_str_cat(list, ""); 37 list = xs_insert_m(list, xs_size(list) - 1, "", 1);
38 38
39 /* move forward */ 39 /* move forward */
40 offset += rm.rm_eo; 40 offset += rm.rm_eo;
@@ -51,12 +51,13 @@ d_char *xs_regex_split_n(const char *str, const char *rx, int count)
51} 51}
52 52
53 53
54d_char *xs_regex_match_n(const char *str, const char *rx, int count) 54xs_list *xs_regex_match_n(const char *str, const char *rx, int count)
55/* returns a list with upto count matches */ 55/* returns a list with upto count matches */
56{ 56{
57 d_char *list = xs_list_new(); 57 xs_list *list = xs_list_new();
58 xs *split = NULL; 58 xs *split = NULL;
59 char *p, *v; 59 xs_list *p;
60 xs_val *v;
60 int n = 0; 61 int n = 0;
61 62
62 /* split */ 63 /* split */