[gnome-builder] string: add more highlight capabilities



commit b365253e1d9bbd8e8e4064613f0ca017942d8a4f
Author: Christian Hergert <christian hergert me>
Date:   Thu Sep 24 04:43:54 2015 -0700

    string: add more highlight capabilities

 src/util/gb-string.c |   32 +++++++++++++++++++++++++++-----
 src/util/gb-string.h |   14 ++++++++++++--
 2 files changed, 39 insertions(+), 7 deletions(-)
---
diff --git a/src/util/gb-string.c b/src/util/gb-string.c
index ffb96db..a3718fe 100644
--- a/src/util/gb-string.c
+++ b/src/util/gb-string.c
@@ -19,9 +19,13 @@
 #include "gb-string.h"
 
 gchar *
-gb_str_highlight (const gchar *str,
-                  const gchar *match)
+gb_str_highlight_full (const gchar     *str,
+                       const gchar     *match,
+                       gboolean         insensitive,
+                       GbHighlightType  type)
 {
+  const gchar *begin = "<u>";
+  const gchar *end = "</u>";
   GString *ret;
   gunichar str_ch;
   gunichar match_ch;
@@ -29,6 +33,12 @@ gb_str_highlight (const gchar *str,
   g_return_val_if_fail (str, NULL);
   g_return_val_if_fail (match, NULL);
 
+  if (type == GB_HIGHLIGHT_BOLD)
+    {
+      begin = "<b>";
+      end = "</b>";
+    }
+
   ret = g_string_new (NULL);
 
   for (; *str; str = g_utf8_next_char (str))
@@ -36,12 +46,15 @@ gb_str_highlight (const gchar *str,
       str_ch = g_utf8_get_char (str);
       match_ch = g_utf8_get_char (match);
 
-      if (str_ch == match_ch)
+      if ((str_ch == match_ch) || (insensitive && (g_unichar_tolower (str_ch) == g_unichar_tolower 
(match_ch))))
         {
-          g_string_append (ret, "<u>");
+          g_string_append (ret, begin);
           g_string_append_unichar (ret, str_ch);
-          g_string_append (ret, "</u>");
+          g_string_append (ret, end);
 
+          /*
+           * TODO: We could seek to the next char and append in a batch.
+           */
           match = g_utf8_next_char (match);
         }
       else
@@ -50,5 +63,14 @@ gb_str_highlight (const gchar *str,
         }
     }
 
+#undef TOGGLE
+
   return g_string_free (ret, FALSE);
 }
+
+gchar *
+gb_str_highlight (const gchar *str,
+                  const gchar *match)
+{
+  return gb_str_highlight_full (str, match, FALSE, GB_HIGHLIGHT_UNDERLINE);
+}
diff --git a/src/util/gb-string.h b/src/util/gb-string.h
index 682a435..11ae6a6 100644
--- a/src/util/gb-string.h
+++ b/src/util/gb-string.h
@@ -28,8 +28,18 @@ G_BEGIN_DECLS
 #define gb_str_equal0(s1,s2) \
   (((s1) == (s2)) || ((s1) && (s2) && g_str_equal(s1,s2)))
 
-gchar *gb_str_highlight (const gchar *src,
-                         const gchar *match);
+typedef enum
+{
+  GB_HIGHLIGHT_UNDERLINE,
+  GB_HIGHLIGHT_BOLD,
+} GbHighlightType;
+
+gchar *gb_str_highlight      (const gchar     *src,
+                              const gchar     *match);
+gchar *gb_str_highlight_full (const gchar     *str,
+                              const gchar     *match,
+                              gboolean         insensitive,
+                              GbHighlightType  type);
 
 G_END_DECLS
 


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