diff options
| author | 2022-09-27 17:19:59 +0200 | |
|---|---|---|
| committer | 2022-09-27 17:19:59 +0200 | |
| commit | bd2a07691df6beb2bbe480f856c3801a67f16d68 (patch) | |
| tree | 670e003c571b6de1a563e0f85627d7c96eae7d65 /xs_regex.h | |
| parent | 'Accept' messages are processed. (diff) | |
| download | penes-snac2-bd2a07691df6beb2bbe480f856c3801a67f16d68.tar.gz penes-snac2-bd2a07691df6beb2bbe480f856c3801a67f16d68.tar.xz penes-snac2-bd2a07691df6beb2bbe480f856c3801a67f16d68.zip | |
Backport from xs.
Diffstat (limited to '')
| -rw-r--r-- | xs_regex.h | 49 |
1 files changed, 40 insertions, 9 deletions
| @@ -4,21 +4,23 @@ | |||
| 4 | 4 | ||
| 5 | #define _XS_REGEX_H | 5 | #define _XS_REGEX_H |
| 6 | 6 | ||
| 7 | d_char *xs_regex_match(char *str, char *rx, int count); | 7 | d_char *xs_regex_split_n(const char *str, const char *rx, int count); |
| 8 | #define xs_regex_matchall(str, rx) xs_regex_match(str, rx, 0xfffffff) | 8 | #define xs_regex_split(str, rx) xs_regex_split_n(str, rx, 0xfffffff) |
| 9 | d_char *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, 0xfffffff) | ||
| 9 | 11 | ||
| 10 | #ifdef XS_IMPLEMENTATION | 12 | #ifdef XS_IMPLEMENTATION |
| 11 | 13 | ||
| 12 | #include <regex.h> | 14 | #include <regex.h> |
| 13 | 15 | ||
| 14 | d_char *xs_regex_match(char *str, char *rx, int count) | 16 | d_char *xs_regex_split_n(const char *str, const char *rx, int count) |
| 15 | /* returns a list with upto count matches */ | 17 | /* splits str by regex */ |
| 16 | { | 18 | { |
| 17 | regex_t re; | 19 | regex_t re; |
| 18 | regmatch_t rm; | 20 | regmatch_t rm; |
| 19 | d_char *list = NULL; | ||
| 20 | int offset = 0; | 21 | int offset = 0; |
| 21 | char *p; | 22 | d_char *list = NULL; |
| 23 | const char *p; | ||
| 22 | 24 | ||
| 23 | if (regcomp(&re, rx, REG_EXTENDED)) | 25 | if (regcomp(&re, rx, REG_EXTENDED)) |
| 24 | return NULL; | 26 | return NULL; |
| @@ -26,22 +28,51 @@ d_char *xs_regex_match(char *str, char *rx, int count) | |||
| 26 | list = xs_list_new(); | 28 | list = xs_list_new(); |
| 27 | 29 | ||
| 28 | 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)) { |
| 29 | /* add the first part */ | 31 | /* add first the leading part of the string */ |
| 30 | list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so); | 32 | list = xs_list_append_m(list, p, rm.rm_so); |
| 33 | list = xs_str_cat(list, ""); | ||
| 31 | 34 | ||
| 32 | /* add the asciiz */ | 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); | ||
| 33 | list = xs_str_cat(list, ""); | 37 | list = xs_str_cat(list, ""); |
| 34 | 38 | ||
| 39 | /* move forward */ | ||
| 35 | offset += rm.rm_eo; | 40 | offset += rm.rm_eo; |
| 36 | 41 | ||
| 37 | count--; | 42 | count--; |
| 38 | } | 43 | } |
| 39 | 44 | ||
| 45 | /* add the rest of the string */ | ||
| 46 | list = xs_list_append(list, p); | ||
| 47 | |||
| 40 | regfree(&re); | 48 | regfree(&re); |
| 41 | 49 | ||
| 42 | return list; | 50 | return list; |
| 43 | } | 51 | } |
| 44 | 52 | ||
| 53 | |||
| 54 | d_char *xs_regex_match_n(const char *str, const char *rx, int count) | ||
| 55 | /* returns a list with upto count matches */ | ||
| 56 | { | ||
| 57 | d_char *list = xs_list_new(); | ||
| 58 | xs *split = NULL; | ||
| 59 | char *p, *v; | ||
| 60 | int n = 0; | ||
| 61 | |||
| 62 | /* split */ | ||
| 63 | p = split = xs_regex_split_n(str, rx, count); | ||
| 64 | |||
| 65 | /* now iterate to get only the 'separators' (odd ones) */ | ||
| 66 | while (xs_list_iter(&p, &v)) { | ||
| 67 | if (n & 0x1) | ||
| 68 | list = xs_list_append(list, v); | ||
| 69 | |||
| 70 | n++; | ||
| 71 | } | ||
| 72 | |||
| 73 | return list; | ||
| 74 | } | ||
| 75 | |||
| 45 | #endif /* XS_IMPLEMENTATION */ | 76 | #endif /* XS_IMPLEMENTATION */ |
| 46 | 77 | ||
| 47 | #endif /* XS_REGEX_H */ | 78 | #endif /* XS_REGEX_H */ |