]> jfr.im git - irc/irssi/irssi.git/commitdiff
Use GLib's regexp interface (backed by PCRE)
authorLemonBoy <redacted>
Thu, 14 Jan 2016 13:10:00 +0000 (14:10 +0100)
committerAilin Nemui <redacted>
Mon, 2 Jan 2017 16:50:14 +0000 (17:50 +0100)
src/core/ignore.c
src/core/ignore.h
src/core/misc.c
src/fe-common/core/fe-ignore.c
src/fe-common/core/hilight-text.c
src/fe-common/core/hilight-text.h
src/fe-text/textbuffer.c

index 2047dc9d28fc959913013f6a3412ec27eb9b8ee2..2b8299bda0736f9e3640c4bd755e011bd6b06db0 100644 (file)
@@ -67,12 +67,8 @@ static int ignore_match_pattern(IGNORE_REC *rec, const char *text)
                return FALSE;
 
        if (rec->regexp) {
-#ifdef HAVE_REGEX_H
                return rec->regexp_compiled &&
-                       regexec(&rec->preg, text, 0, NULL, 0) == 0;
-#else
-                return FALSE;
-#endif
+                   g_regex_match(rec->preg, text, 0, NULL);
        }
 
        return rec->fullword ?
@@ -326,26 +322,23 @@ static void ignore_remove_config(IGNORE_REC *rec)
 
 static void ignore_init_rec(IGNORE_REC *rec)
 {
-#ifdef HAVE_REGEX_H
-       char *errbuf;
-       int errcode, errbuf_len;
+       if (rec->regexp_compiled) {
+               g_regex_unref(rec->preg);
+               rec->regexp_compiled = FALSE;
+       }
 
-       if (rec->regexp_compiled) regfree(&rec->preg);
-       rec->regexp_compiled = FALSE;
        if (rec->regexp && rec->pattern != NULL) {
-               errcode = regcomp(&rec->preg, rec->pattern,
-                               REG_EXTENDED|REG_ICASE|REG_NOSUB);
-               if (errcode != 0) {
-                       errbuf_len = regerror(errcode, &rec->preg, 0, 0);
-                       errbuf = g_malloc(errbuf_len);
-                       regerror(errcode, &rec->preg, errbuf, errbuf_len);
-                       g_warning("Failed to compile regexp '%s': %s", rec->pattern, errbuf);
-                       g_free(errbuf);
+               GError *re_error;
+
+               rec->preg = g_regex_new(rec->pattern, G_REGEX_CASELESS, 0, &re_error);
+
+               if (rec->preg == NULL) {
+                       g_warning("Failed to compile regexp '%s': %s", rec->pattern, re_error->message);
+                       g_error_free(re_error);
                } else {
                        rec->regexp_compiled = TRUE;
                }
        }
-#endif
 }
 
 void ignore_add_rec(IGNORE_REC *rec)
@@ -365,9 +358,7 @@ static void ignore_destroy(IGNORE_REC *rec, int send_signal)
        if (send_signal)
                signal_emit("ignore destroyed", 1, rec);
 
-#ifdef HAVE_REGEX_H
-       if (rec->regexp_compiled) regfree(&rec->preg);
-#endif
+       if (rec->regexp_compiled) g_regex_unref(rec->preg);
        if (rec->channels != NULL) g_strfreev(rec->channels);
        g_free_not_null(rec->mask);
        g_free_not_null(rec->servertag);
index f889740fbf8f9ccde16111b9eb00ca404f5a2285..6c9797f5ad2533206a6e8f5783ee0ac37f32afcb 100644 (file)
@@ -1,10 +1,6 @@
 #ifndef __IGNORE_H
 #define __IGNORE_H
 
-#ifdef HAVE_REGEX_H
-#  include <regex.h>
-#endif
-
 typedef struct _IGNORE_REC IGNORE_REC;
 
 struct _IGNORE_REC {
@@ -20,10 +16,8 @@ struct _IGNORE_REC {
        unsigned int regexp:1;
        unsigned int fullword:1;
        unsigned int replies:1; /* ignore replies to nick in channel */
-#ifdef HAVE_REGEX_H
        unsigned int regexp_compiled:1; /* should always be TRUE, unless regexp is invalid */
-       regex_t preg;
-#endif
+       GRegex *preg;
 };
 
 extern GSList *ignores;
index 0bb1f7e6293eb3105cf869fdd9632517be81a37d..c59eb12678da5ca28a1abcae659824bef16d5a19 100644 (file)
 #include "misc.h"
 #include "commands.h"
 
-#ifdef HAVE_REGEX_H
-#  include <regex.h>
-#endif
-
 typedef struct {
        int condition;
        GInputFunction function;
index 52b11e6b6788bb6bd764e9673450cc3abcde1288..addfa0b80da987978a3284b05cf01151c0b61be3 100644 (file)
@@ -58,10 +58,8 @@ static void ignore_print(int index, IGNORE_REC *rec)
                g_string_append(options, "-regexp ");
                if (rec->pattern == NULL)
                        g_string_append(options, "[INVALID! -pattern missing] ");
-#ifdef HAVE_REGEX_H
                else if (!rec->regexp_compiled)
                        g_string_append(options, "[INVALID!] ");
-#endif
        }
        if (rec->fullword) g_string_append(options, "-full ");
        if (rec->replies) g_string_append(options, "-replies ");
index 4691a7081c022b33435f1c0c1345283723df668a..83b6f67e443f9d748ed8997318e899b04acfeb77 100644 (file)
@@ -101,9 +101,7 @@ static void hilight_destroy(HILIGHT_REC *rec)
 {
        g_return_if_fail(rec != NULL);
 
-#ifdef HAVE_REGEX_H
-       if (rec->regexp_compiled) regfree(&rec->preg);
-#endif
+       if (rec->regexp_compiled) g_regex_unref(rec->preg);
        if (rec->channels != NULL) g_strfreev(rec->channels);
        g_free_not_null(rec->color);
        g_free_not_null(rec->act_color);
@@ -120,14 +118,15 @@ static void hilights_destroy_all(void)
 
 static void hilight_init_rec(HILIGHT_REC *rec)
 {
-#ifdef HAVE_REGEX_H
-       if (rec->regexp_compiled) regfree(&rec->preg);
-       if (!rec->regexp)
+       if (rec->regexp_compiled) {
+               g_regex_unref(rec->preg);
                rec->regexp_compiled = FALSE;
-       else
-               rec->regexp_compiled = regcomp(&rec->preg, rec->text,
-                               rec->case_sensitive ? REG_EXTENDED : (REG_EXTENDED|REG_ICASE)) == 0;
-#endif
+       }
+
+       rec->preg = g_regex_new(rec->text, G_REGEX_CASELESS, 0, NULL);
+
+       if (rec->preg != NULL)
+               rec->regexp_compiled = TRUE;
 }
 
 void hilight_create(HILIGHT_REC *rec)
@@ -200,19 +199,15 @@ static int hilight_match_text(HILIGHT_REC *rec, const char *text,
        char *match;
 
        if (rec->regexp) {
-#ifdef HAVE_REGEX_H
-               regmatch_t rmatch[1];
-
-               if (rec->regexp_compiled &&
-                       regexec(&rec->preg, text, 1, rmatch, 0) == 0) {
-                       if (rmatch[0].rm_so > 0 &&
-                               match_beg != NULL && match_end != NULL) {
-                               *match_beg = rmatch[0].rm_so;
-                               *match_end = rmatch[0].rm_eo;
+               GMatchInfo *match;
+
+               if (rec->regexp_compiled) {
+                       g_regex_match (rec->preg, text, 0, &match);
+
+                       if (g_match_info_matches(match)) {
+                               return g_match_info_fetch_pos(match, 0, match_beg, match_end);
                        }
-                       return TRUE;
                }
-#endif
        } else {
                if (rec->case_sensitive) {
                        match = rec->fullword ?
@@ -509,10 +504,8 @@ static void hilight_print(int index, HILIGHT_REC *rec)
        if (rec->case_sensitive) g_string_append(options, "-matchcase ");
        if (rec->regexp) {
                g_string_append(options, "-regexp ");
-#ifdef HAVE_REGEX_H
                if (!rec->regexp_compiled)
                        g_string_append(options, "[INVALID!] ");
-#endif
        }
 
        if (rec->priority != 0)
index ae05e1ca7a5dfc93b8bd7523775b80cf7a7a729e..a74c38b0c4b8f7daa43615dafb7281d61431c414 100644 (file)
@@ -1,10 +1,6 @@
 #ifndef __HILIGHT_TEXT_H
 #define __HILIGHT_TEXT_H
 
-#ifdef HAVE_REGEX_H
-#  include <regex.h>
-#endif
-
 #include "formats.h"
 
 struct _HILIGHT_REC {
@@ -24,10 +20,8 @@ struct _HILIGHT_REC {
        unsigned int fullword:1; /* match `text' only for full words */
        unsigned int regexp:1; /* `text' is a regular expression */
        unsigned int case_sensitive:1;/* `text' must match case */
-#ifdef HAVE_REGEX_H
        unsigned int regexp_compiled:1; /* should always be TRUE, unless regexp is invalid */
-       regex_t preg;
-#endif
+       GRegex *preg;
        char *servertag;
 };
 
index 24ee62bccd41f1b8949b8b0895125ee067c04fe5..979b2a463e3eafbeec40b48d45727c65cbd69fa8 100644 (file)
 
 #include "textbuffer.h"
 
-#ifdef HAVE_REGEX_H
-#  include <regex.h>
-#endif
-
 #define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
 
 TEXT_BUFFER_REC *textbuffer_create(void)
@@ -537,9 +533,7 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
                            int before, int after,
                            int regexp, int fullword, int case_sensitive)
 {
-#ifdef HAVE_REGEX_H
-       regex_t preg;
-#endif
+       GRegex *preg;
         LINE_REC *line, *pre_line;
        GList *matches;
        GString *str;
@@ -550,14 +544,10 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
        g_return_val_if_fail(text != NULL, NULL);
 
        if (regexp) {
-#ifdef HAVE_REGEX_H
-               int flags = REG_EXTENDED | REG_NOSUB |
-                       (case_sensitive ? 0 : REG_ICASE);
-               if (regcomp(&preg, text, flags) != 0)
+               preg = g_regex_new(text, (case_sensitive ? 0 : G_REGEX_CASELESS), 0, NULL);
+
+               if (preg == NULL)
                        return NULL;
-#else
-               return NULL;
-#endif
        }
 
        matches = NULL; match_after = 0;
@@ -577,12 +567,11 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
                if (*text != '\0') {
                        textbuffer_line2text(line, FALSE, str);
 
-                       if (line_matched)
-                       line_matched =
-#ifdef HAVE_REGEX_H
-                       regexp ? regexec(&preg, str->str, 0, NULL, 0) == 0 :
-#endif
-                       match_func(str->str, text) != NULL;
+                       if (line_matched) {
+                               line_matched = regexp ? 
+                                   g_regex_match(preg, str->str, 0, NULL) :
+                                   match_func(str->str, text) != NULL;
+                       }
                }
 
                if (line_matched) {
@@ -610,9 +599,9 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
                                matches = g_list_append(matches, NULL);
                }
        }
-#ifdef HAVE_REGEX_H
-       if (regexp) regfree(&preg);
-#endif
+
+       if (regexp)
+               g_regex_unref(preg);
         g_string_free(str, TRUE);
        return matches;
 }