Refactor regex and implement UTF8 mode for GRegex

- with non-unicode byte to Private Use Area A mapping
- move all ifdefs to iregex.h file only
This commit is contained in:
ailin-nemui 2017-02-16 22:48:13 +01:00
commit 79bbca4644
12 changed files with 327 additions and 132 deletions

View file

@ -24,13 +24,10 @@
#include "misc.h"
#include "formats.h"
#include "utf8.h"
#include "iregex.h"
#include "textbuffer.h"
#ifndef USE_GREGEX
# include <regex.h>
#endif
#define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
TEXT_BUFFER_REC *textbuffer_create(void)
@ -545,11 +542,7 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
int before, int after,
int regexp, int fullword, int case_sensitive)
{
#ifdef USE_GREGEX
GRegex *preg;
#else
regex_t preg;
#endif
Regex *preg;
LINE_REC *line, *pre_line;
GList *matches;
GString *str;
@ -559,23 +552,14 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
g_return_val_if_fail(buffer != NULL, NULL);
g_return_val_if_fail(text != NULL, NULL);
#ifdef USE_GREGEX
preg = NULL;
if (regexp) {
preg = g_regex_new(text, G_REGEX_RAW | (case_sensitive ? 0 : G_REGEX_CASELESS), 0, NULL);
preg = i_regex_new(text, case_sensitive ? 0 : G_REGEX_CASELESS, 0, NULL);
if (preg == NULL)
return NULL;
}
#else
if (regexp) {
int flags = REG_EXTENDED | REG_NOSUB |
(case_sensitive ? 0 : REG_ICASE);
if (regcomp(&preg, text, flags) != 0)
return NULL;
}
#endif
matches = NULL; match_after = 0;
str = g_string_new(NULL);
@ -592,17 +576,16 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
(line->info.level & nolevel) == 0;
if (*text != '\0') {
const char *tmp = NULL;
textbuffer_line2text(line, FALSE, str);
if (line_matched) {
line_matched = regexp ?
#ifdef USE_GREGEX
g_regex_match(preg, str->str, 0, NULL)
#else
regexec(&preg, str->str, 0, NULL, 0) == 0
#endif
i_regex_match(preg, str->str, 0, NULL, &tmp)
: match_func(str->str, text) != NULL;
}
if (tmp && tmp != str->str)
g_free_not_null((char *)tmp);
}
if (line_matched) {
@ -631,12 +614,8 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
}
}
#ifdef USE_GREGEX
if (preg != NULL)
g_regex_unref(preg);
#else
if (regexp) regfree(&preg);
#endif
i_regex_unref(preg);
g_string_free(str, TRUE);
return matches;
}