[gnome-builder] search: add fuzzy highlighter
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] search: add fuzzy highlighter
- Date: Fri, 7 Apr 2017 21:56:23 +0000 (UTC)
commit 3161c487494cb064a05d7439dc244d33fe75df89
Author: Christian Hergert <chergert redhat com>
Date: Fri Apr 7 14:44:43 2017 -0700
search: add fuzzy highlighter
Just a simple routine for when we need access when using fuzzy searching.
contrib/search/fuzzy.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
contrib/search/fuzzy.h | 3 ++
2 files changed, 57 insertions(+), 0 deletions(-)
---
diff --git a/contrib/search/fuzzy.c b/contrib/search/fuzzy.c
index cacf7e0..73c3c58 100644
--- a/contrib/search/fuzzy.c
+++ b/contrib/search/fuzzy.c
@@ -561,3 +561,57 @@ fuzzy_remove (Fuzzy *fuzzy,
g_clear_pointer (&ar, g_array_unref);
}
+
+gchar *
+fuzzy_highlight (Fuzzy *fuzzy,
+ const gchar *str,
+ const gchar *match)
+{
+ static const gchar *begin = "<b>";
+ static const gchar *end = "</b>";
+ GString *ret;
+ gunichar str_ch;
+ gunichar match_ch;
+ gboolean element_open = FALSE;
+
+ if (str == NULL || match == NULL)
+ return g_strdup (str);
+
+ ret = g_string_new (NULL);
+
+ for (; *str; str = g_utf8_next_char (str))
+ {
+ str_ch = g_utf8_get_char (str);
+ match_ch = g_utf8_get_char (match);
+
+ if ((str_ch == match_ch) ||
+ (!fuzzy->case_sensitive && g_unichar_tolower (str_ch) == g_unichar_tolower (match_ch)))
+ {
+ if (!element_open)
+ {
+ g_string_append (ret, begin);
+ element_open = TRUE;
+ }
+
+ g_string_append_unichar (ret, str_ch);
+
+ /* 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/contrib/search/fuzzy.h b/contrib/search/fuzzy.h
index cc6330c..1c213f3 100644
--- a/contrib/search/fuzzy.h
+++ b/contrib/search/fuzzy.h
@@ -53,6 +53,9 @@ void fuzzy_remove (Fuzzy *fuzzy,
const gchar *key);
Fuzzy *fuzzy_ref (Fuzzy *fuzzy);
void fuzzy_unref (Fuzzy *fuzzy);
+gchar *fuzzy_highlight (Fuzzy *fuzzy,
+ const gchar *str,
+ const gchar *query);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]