[gnome-builder] ctags: improve regex escaping



commit 21946d4628c570e0a777afb0494d4cb50ec1cf4f
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jan 28 14:23:05 2018 -0800

    ctags: improve regex escaping
    
    When consuming ctags files that use a regex to specify the location, we
    have to convert the ctags regex into something GRegex can use. The regex
    provided in the ctags file does not perform escaping on various features
    like groups (), or repeats * and therefore we must do it for it.
    
    Fixes #250

 src/plugins/ctags/ide-ctags-symbol-resolver.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/ctags/ide-ctags-symbol-resolver.c b/src/plugins/ctags/ide-ctags-symbol-resolver.c
index d4d72492a..bf6e91c24 100644
--- a/src/plugins/ctags/ide-ctags-symbol-resolver.c
+++ b/src/plugins/ctags/ide-ctags-symbol-resolver.c
@@ -97,6 +97,7 @@ static gchar *
 extract_regex (const gchar *pattern)
 {
   const gchar *input = pattern;
+  GString *str;
   const gchar *endptr;
 
   if (!pattern || *pattern != '/')
@@ -116,10 +117,30 @@ extract_regex (const gchar *pattern)
   if (endptr < pattern)
     goto failure;
 
-  return g_strdelimit (g_strndup (pattern, endptr - pattern), "()", '.');
+  str = g_string_new (NULL);
+
+  for (const gchar *iter = pattern; iter < endptr; iter = g_utf8_next_char (iter))
+    {
+      gunichar ch = g_utf8_get_char (iter);
+
+      switch (ch)
+        {
+        case '(':
+        case ')':
+        case '*':
+          g_string_append_printf (str, "\\%c", ch);
+          break;
+
+        default:
+          g_string_append_unichar (str, ch);
+          break;
+        }
+    }
+
+  return g_string_free (str, FALSE);
 
 failure:
-  return g_strdup (input);
+  return g_regex_escape_string (input, -1);
 }
 
 static void
@@ -197,6 +218,8 @@ regex_worker (GTask        *task,
 
   pattern = extract_regex (lookup->entry->pattern);
 
+  IDE_TRACE_MSG ("Looking for regex pattern: %s", pattern);
+
   if (!(regex = g_regex_new (pattern, G_REGEX_MULTILINE, 0, &error)))
     {
       g_task_return_error (task, g_steal_pointer (&error));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]