diff options
author | Mole Shang <[email protected]> | 2023-08-08 21:10:36 +0800 |
---|---|---|
committer | Mole Shang <[email protected]> | 2023-08-08 21:10:36 +0800 |
commit | cee1e6cc9bfb25b6630719045e15fbf0a3044069 (patch) | |
tree | 245eebefaeb973aa6211473fac1ab70e9d06e469 /src/utils | |
parent | b865a491b39293611546f1b5461bcfac37ff9377 (diff) | |
download | hinata-cee1e6cc9bfb25b6630719045e15fbf0a3044069.tar.gz hinata-cee1e6cc9bfb25b6630719045e15fbf0a3044069.tar.bz2 hinata-cee1e6cc9bfb25b6630719045e15fbf0a3044069.zip |
utils: fix multi-patterns matching
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/utils.c | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/utils/utils.c b/src/utils/utils.c index 16d39d9..f3ebbb4 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -34,14 +34,14 @@ int regex_match(const char *subject, str_array_t patterns, match_data = pcre2_match_data_create_from_pattern(re, NULL); - unsigned char i = 0; while (offset < subject_len && (rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)subject_len, offset, 0, match_data, NULL)) > 0) { // results->str = realloc(results->str, sizeof(char *) * (rc + // results->n)); - resize_str_array(results, rc + i); + int valid_n = results->n; + resize_str_array(results, rc - 1 + results->n); ovector = pcre2_get_ovector_pointer(match_data); DEBUG_PRINT("Get %d captures.\n", rc - 1); @@ -53,14 +53,14 @@ int regex_match(const char *subject, str_array_t patterns, /* Here we need to manually control the str array, * as PCRE2_SPTR == const unsigned char * (which cannot be directly casted) */ - results->str[j] = malloc(substring_length + 1); - sprintf(results->str[j], "%.*s", (int)substring_length, substring); + results->str[valid_n + j - 1] = malloc(substring_length + 1); + sprintf(results->str[valid_n + j - 1], "%.*s", (int)substring_length, + substring); DEBUG_PRINT("index: %2d, substring_length: %d\n", j, (int)substring_length); } offset = ovector[1]; DEBUG_PRINT("offset: %zu, subject_len: %zu\n", offset, subject_len); - i++; } pcre2_match_data_free(match_data); pcre2_code_free(re); @@ -69,7 +69,6 @@ int regex_match(const char *subject, str_array_t patterns, switch (rc) { case PCRE2_ERROR_NOMATCH: DEBUG_PRINT("No match found.\n"); - return 0; break; case 0: LOG("PCRE2", @@ -95,7 +94,6 @@ int repchr(char *str, char t, char r) { return c; } - void free_and_nullify(void *p) { if (p) { free(p); |