diff options
Diffstat (limited to 'xs_regex.h')
| -rw-r--r-- | xs_regex.h | 19 |
1 files changed, 10 insertions, 9 deletions
| @@ -4,22 +4,22 @@ | |||
| 4 | 4 | ||
| 5 | #define _XS_REGEX_H | 5 | #define _XS_REGEX_H |
| 6 | 6 | ||
| 7 | d_char *xs_regex_split_n(const char *str, const char *rx, int count); | 7 | xs_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) |
| 9 | d_char *xs_regex_match_n(const char *str, const char *rx, int count); | 9 | xs_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 | ||
| 16 | d_char *xs_regex_split_n(const char *str, const char *rx, int count) | 16 | xs_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 | ||
| 54 | d_char *xs_regex_match_n(const char *str, const char *rx, int count) | 54 | xs_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 */ |