[gnome-builder] string: allow for both bold and underline highlights



commit 86af4a640baf030c04d211afb4f69a52f0bb1e9b
Author: Christian Hergert <christian hergert me>
Date:   Fri Oct 16 12:37:32 2015 -0700

    string: allow for both bold and underline highlights
    
    This also allows us to join multiple characters into a single attribute
    rather than creating a new attribute per character.

 src/util/gb-string.c |   45 ++++++++++++++++++++++++++++++++++-----------
 src/util/gb-string.h |    4 ++--
 2 files changed, 36 insertions(+), 13 deletions(-)
---
diff --git a/src/util/gb-string.c b/src/util/gb-string.c
index 0c78f2f..2710672 100644
--- a/src/util/gb-string.c
+++ b/src/util/gb-string.c
@@ -27,20 +27,31 @@ gb_str_highlight_full (const gchar     *str,
                        gboolean         insensitive,
                        GbHighlightType  type)
 {
-  const gchar *begin = "<u>";
-  const gchar *end = "</u>";
+  const gchar *begin = NULL;
+  const gchar *end = NULL;
   GString *ret;
   gunichar str_ch;
   gunichar match_ch;
-
-  g_return_val_if_fail (str, NULL);
-  g_return_val_if_fail (match, NULL);
+  gboolean element_open = FALSE;
 
   if (type == GB_HIGHLIGHT_BOLD)
     {
       begin = "<b>";
       end = "</b>";
     }
+  else if (type == GB_HIGHLIGHT_UNDERLINE)
+    {
+      begin = "<u>";
+      end = "</u>";
+    }
+  else if (type == (GB_HIGHLIGHT_UNDERLINE | GB_HIGHLIGHT_BOLD))
+    {
+      begin = "<b><u>";
+      end = "</u></b>";
+    }
+
+  if (str == NULL || match == NULL || begin == NULL || end == NULL)
+    return g_strdup (str);
 
   ret = g_string_new (NULL);
 
@@ -49,23 +60,35 @@ gb_str_highlight_full (const gchar     *str,
       str_ch = g_utf8_get_char (str);
       match_ch = g_utf8_get_char (match);
 
-      if ((str_ch == match_ch) || (insensitive && (g_unichar_tolower (str_ch) == g_unichar_tolower 
(match_ch))))
+      if ((str_ch == match_ch) ||
+          (insensitive && (g_unichar_tolower (str_ch) == g_unichar_tolower (match_ch))))
         {
-          g_string_append (ret, begin);
+          if (!element_open)
+            {
+              g_string_append (ret, begin);
+              element_open = TRUE;
+            }
+
           g_string_append_unichar (ret, str_ch);
-          g_string_append (ret, end);
 
-          /*
-           * TODO: We could seek to the next char and append in a batch.
-           */
+          /* TODO: We could seek to the next char and append in a batch. */
           match = g_utf8_next_char (match);
         }
       else
         {
+          if (element_open)
+            {
+              g_string_append (ret, end);
+              element_open = FALSE;
+            }
+
           g_string_append_unichar (ret, str_ch);
         }
     }
 
+  if (element_open)
+    g_string_append (ret, end);
+
   return g_string_free (ret, FALSE);
 }
 
diff --git a/src/util/gb-string.h b/src/util/gb-string.h
index f8336b7..9370ea2 100644
--- a/src/util/gb-string.h
+++ b/src/util/gb-string.h
@@ -25,8 +25,8 @@ G_BEGIN_DECLS
 
 typedef enum
 {
-  GB_HIGHLIGHT_UNDERLINE,
-  GB_HIGHLIGHT_BOLD,
+  GB_HIGHLIGHT_UNDERLINE = 1,
+  GB_HIGHLIGHT_BOLD      = 1 << 1,
 } GbHighlightType;
 
 gchar *gb_str_highlight      (const gchar     *src,


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