[gtksourceview/wip/search] Add utils functions to escape/unescape search text



commit f624708e60228d27069cd45d433ae8b4f1f2038f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Jun 23 18:06:02 2013 +0200

    Add utils functions to escape/unescape search text
    
    Code copied from gedit (copyright Paolo Borelli).
    License: GPL -> LGPL.

 docs/reference/gtksourceview-3.0-sections.txt |    7 +
 docs/reference/gtksourceview-docs.xml         |    1 +
 gtksourceview/Makefile.am                     |    2 +
 gtksourceview/gtksource.h                     |    1 +
 gtksourceview/gtksourcebuffer.c               |    6 +
 gtksourceview/gtksourceutils.c                |  189 +++++++++++++++++++++++++
 gtksourceview/gtksourceutils.h                |   34 +++++
 po/POTFILES.in                                |    1 +
 8 files changed, 241 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index 43f06e3..3f3ef74 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -605,3 +605,10 @@ GtkSourceCompletionWordsClass
 GtkSourceCompletionWordsPrivate
 gtk_source_completion_words_get_type
 </SECTION>
+
+<SECTION>
+<FILE>utils</FILE>
+<TITLE>GtkSourceUtils</TITLE>
+gtk_source_utils_unescape_search_text
+gtk_source_utils_escape_search_text
+</SECTION>
diff --git a/docs/reference/gtksourceview-docs.xml b/docs/reference/gtksourceview-docs.xml
index abcdc61..90fee63 100644
--- a/docs/reference/gtksourceview-docs.xml
+++ b/docs/reference/gtksourceview-docs.xml
@@ -37,6 +37,7 @@
     <xi:include href="xml/stylescheme.xml"/>
     <xi:include href="xml/styleschememanager.xml"/>
     <xi:include href="xml/undomanager.xml"/>
+    <xi:include href="xml/utils.xml"/>
     <xi:include href="xml/view.xml"/>
   </chapter>
 
diff --git a/gtksourceview/Makefile.am b/gtksourceview/Makefile.am
index 1d3278c..067c380 100644
--- a/gtksourceview/Makefile.am
+++ b/gtksourceview/Makefile.am
@@ -43,6 +43,7 @@ libgtksourceview_headers =                    \
        gtksourcestyleschememanager.h           \
        gtksourcetypes.h                        \
        gtksourceundomanager.h                  \
+       gtksourceutils.h                        \
        gtksourceview.h
 
 libgtksourceview_private_headers = \
@@ -104,6 +105,7 @@ libgtksourceview_c_files = \
        gtksourcestyle.c                \
        gtksourcestylescheme.c          \
        gtksourcestyleschememanager.c   \
+       gtksourceutils.c                \
        gtksourceview.c
 
 # Split in a helper library for unit tests
diff --git a/gtksourceview/gtksource.h b/gtksourceview/gtksource.h
index 2ca35bc..f2afe9f 100644
--- a/gtksourceview/gtksource.h
+++ b/gtksourceview/gtksource.h
@@ -41,6 +41,7 @@
 #include <gtksourceview/gtksourcestylescheme.h>
 #include <gtksourceview/gtksourcestyleschememanager.h>
 #include <gtksourceview/gtksourceundomanager.h>
+#include <gtksourceview/gtksourceutils.h>
 #include <gtksourceview/gtksourceview.h>
 
 #endif /* __GTK_SOURCE_H__ */
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index a8f3e01..5231656 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -2573,6 +2573,9 @@ gtk_source_buffer_get_undo_manager (GtkSourceBuffer *buffer)
  * disabled. A copy of @text will be made, so you can safely free @text after
  * a call to this function.
  *
+ * You may be interested to call gtk_source_utils_unescape_search_text() before
+ * this function.
+ *
  * Since: 3.10
  */
 void
@@ -2603,6 +2606,9 @@ gtk_source_buffer_set_search_text (GtkSourceBuffer *buffer,
  *
  * Gets the text to search. The return value must not be freed.
  *
+ * You may be interested to call gtk_source_utils_escape_search_text() after
+ * this function.
+ *
  * Returns: the text to search, or %NULL if the search is disabled.
  * Since: 3.10
  */
diff --git a/gtksourceview/gtksourceutils.c b/gtksourceview/gtksourceutils.c
new file mode 100644
index 0000000..3347dd1
--- /dev/null
+++ b/gtksourceview/gtksourceutils.c
@@ -0,0 +1,189 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* gtksourceutils.c
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2005 - Paolo Borelli
+ * Copyright (C) 2013 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * SECTION:utils
+ * @title: GtkSourceUtils
+ * @short_description: Utilities functions
+ *
+ * Utilities functions.
+ */
+
+#include "gtksourceutils.h"
+#include <string.h>
+
+/**
+ * gtk_source_utils_unescape_search_text:
+ * @text: the text to unescape.
+ *
+ * Use this function before gtk_source_buffer_set_search_text(), to unescape
+ * certain sequences of characters: \n, \r, \t and \\. The purpose is to easily
+ * write those characters in a search entry.
+ *
+ * See also: gtk_source_utils_escape_search_text().
+ *
+ * Since: 3.10
+ */
+gchar *
+gtk_source_utils_unescape_search_text (const gchar *text)
+{
+       GString *str;
+       gint length;
+       gboolean drop_prev = FALSE;
+       const gchar *cur;
+       const gchar *end;
+       const gchar *prev;
+
+       if (text == NULL)
+       {
+               return NULL;
+       }
+
+       length = strlen (text);
+
+       str = g_string_new ("");
+
+       cur = text;
+       end = text + length;
+       prev = NULL;
+
+       while (cur != end)
+       {
+               const gchar *next;
+               next = g_utf8_next_char (cur);
+
+               if (prev && (*prev == '\\'))
+               {
+                       switch (*cur)
+                       {
+                               case 'n':
+                                       str = g_string_append (str, "\n");
+                                       break;
+                               case 'r':
+                                       str = g_string_append (str, "\r");
+                                       break;
+                               case 't':
+                                       str = g_string_append (str, "\t");
+                                       break;
+                               case '\\':
+                                       str = g_string_append (str, "\\");
+                                       drop_prev = TRUE;
+                                       break;
+                               default:
+                                       str = g_string_append (str, "\\");
+                                       str = g_string_append_len (str, cur, next - cur);
+                                       break;
+                       }
+               }
+               else if (*cur != '\\')
+               {
+                       str = g_string_append_len (str, cur, next - cur);
+               }
+               else if ((next == end) && (*cur == '\\'))
+               {
+                       str = g_string_append (str, "\\");
+               }
+
+               if (!drop_prev)
+               {
+                       prev = cur;
+               }
+               else
+               {
+                       prev = NULL;
+                       drop_prev = FALSE;
+               }
+
+               cur = next;
+       }
+
+       return g_string_free (str, FALSE);
+}
+
+/**
+ * gtk_source_utils_escape_search_text:
+ * @text: the text to escape.
+ *
+ * Use this function after gtk_source_buffer_get_search_text(), to escape
+ * certain characters: \n, \r, \t and \.
+ *
+ * See also: gtk_source_utils_unescape_search_text().
+ *
+ * Since: 3.10
+ */
+gchar *
+gtk_source_utils_escape_search_text (const gchar* text)
+{
+       GString *str;
+       gint length;
+       const gchar *p;
+       const gchar *end;
+
+       if (text == NULL)
+       {
+               return NULL;
+       }
+
+       length = strlen (text);
+
+       /* no escape when typing.
+        * The short circuit works only for ascii, but we only
+        * care about not escaping a single '\' */
+       if (length == 1)
+       {
+               return g_strdup (text);
+       }
+
+       str = g_string_new ("");
+
+       p = text;
+       end = text + length;
+
+       while (p != end)
+       {
+               const gchar *next;
+               next = g_utf8_next_char (p);
+
+               switch (*p)
+               {
+                       case '\n':
+                               g_string_append (str, "\\n");
+                               break;
+                       case '\r':
+                               g_string_append (str, "\\r");
+                               break;
+                       case '\t':
+                               g_string_append (str, "\\t");
+                               break;
+                       case '\\':
+                               g_string_append (str, "\\\\");
+                               break;
+                       default:
+                               g_string_append_len (str, p, next - p);
+                               break;
+               }
+
+               p = next;
+       }
+
+       return g_string_free (str, FALSE);
+}
diff --git a/gtksourceview/gtksourceutils.h b/gtksourceview/gtksourceutils.h
new file mode 100644
index 0000000..2a39ed9
--- /dev/null
+++ b/gtksourceview/gtksourceutils.h
@@ -0,0 +1,34 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*-
+ * gtksourceutils.h
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2013 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GTK_SOURCE_UTILS_H__
+#define __GTK_SOURCE_UTILS_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+gchar          *gtk_source_utils_unescape_search_text          (const gchar    *text);
+gchar          *gtk_source_utils_escape_search_text            (const gchar    *text);
+
+G_END_DECLS
+
+#endif /* __GTK_SOURCE_UTILS_H__ */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 32a6455..c20b581 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -136,5 +136,6 @@ gtksourceview/gtksourcestyle.c
 gtksourceview/gtksourcestylescheme.c
 gtksourceview/gtksourcestyleschememanager.c
 gtksourceview/gtksourceundomanagerdefault.c
+gtksourceview/gtksourceutils.c
 gtksourceview/gtksourceview.c
 gtksourceview/gtksourceview-i18n.c


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