[polari] room: Split out nick matching into a utility function



commit 1bde9e9409f8fd0f48bfaa8524533cdd02dbdc9c
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Jul 12 12:52:57 2018 +0200

    room: Split out nick matching into a utility function
    
    Nick highlighting is important to get right, which is why we try to
    be smarter than simply matching for the current nick. As that makes
    the code more complex and thus more prune to bugs, it is worth cove-
    ring in unit testing. Splitting out the matching logic into a utility
    function will allow us to do so without setting up a telepathy test
    environment.
    
    https://gitlab.gnome.org/GNOME/polari/issues/53

 src/lib/polari-room.c | 35 +----------------------------------
 src/lib/polari-util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/polari-util.h |  3 +++
 3 files changed, 50 insertions(+), 34 deletions(-)
---
diff --git a/src/lib/polari-room.c b/src/lib/polari-room.c
index 2181faa..a0ed414 100644
--- a/src/lib/polari-room.c
+++ b/src/lib/polari-room.c
@@ -121,46 +121,13 @@ polari_create_room_id (TpAccount    *account,
   return id;
 }
 
-#ifdef HAVE_STRCASESTR
-#  define FOLDFUNC(text) ((char *)(text))
-#  define MATCHFUNC(haystick,needle) strcasestr (haystick, needle)
-#else
-#  define FOLDFUNC(text) g_utf8_casefold (text, -1)
-#  define MATCHFUNC(haystick,needle) strstr (haystick, needle)
-#endif
-
 static gboolean
 match_self_nick (PolariRoom *room,
                  const char *text)
 {
   PolariRoomPrivate *priv = room->priv;
-  g_autofree char *folded_text = NULL;
-  char *match;
-  gboolean result = FALSE;
-  int len;
-
-  len = strlen (priv->self_nick);
-  if (len == 0)
-    return FALSE;
-
-  folded_text = FOLDFUNC (text);
-  match = MATCHFUNC (folded_text, priv->self_nick);
-
-  while (match != NULL)
-    {
-      gboolean starts_word, ends_word;
-
-      /* assume ASCII nicknames, so no complex pango-style breaks */
-      starts_word = (match == folded_text || !g_ascii_isalnum (*(match - 1)));
-      ends_word = !g_ascii_isalnum (*(match + len));
-
-      result = starts_word && ends_word;
-      if (result)
-        break;
-      match = MATCHFUNC (match + len, priv->self_nick);
-    }
 
-  return result;
+  return polari_util_match_nick (text, priv->self_nick);
 }
 
 gboolean
diff --git a/src/lib/polari-util.c b/src/lib/polari-util.c
index 2c21c1f..fc82474 100644
--- a/src/lib/polari-util.c
+++ b/src/lib/polari-util.c
@@ -16,6 +16,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.";
  */
 
+#include <string.h>
+
 #include "polari-util.h"
 
 #include <glib.h>
@@ -42,6 +44,50 @@ polari_util_get_basenick (const char *nick)
     return g_utf8_casefold (nick, -1);
 }
 
+#ifdef HAVE_STRCASESTR
+#  define FOLDFUNC(text) ((char *)(text))
+#  define MATCHFUNC(haystick,needle) strcasestr (haystick, needle)
+#else
+#  define FOLDFUNC(text) g_utf8_casefold (text, -1)
+#  define MATCHFUNC(haystick,needle) strstr (haystick, needle)
+#endif
+
+gboolean
+polari_util_match_nick (const char *text,
+                        const char *nick)
+{
+  g_autofree char *folded_text = NULL;
+  g_autofree char *folded_nick = NULL;
+  char *match;
+  gboolean result = FALSE;
+  int len;
+
+  len = strlen (nick);
+  if (len == 0)
+    return FALSE;
+
+  folded_text = FOLDFUNC (text);
+  folded_nick = FOLDFUNC (nick);
+
+  match = MATCHFUNC (folded_text, folded_nick);
+
+  while (match != NULL)
+    {
+      gboolean starts_word, ends_word;
+
+      /* assume ASCII nicknames, so no complex pango-style breaks */
+      starts_word = (match == folded_text || !g_ascii_isalnum (*(match - 1)));
+      ends_word = !g_ascii_isalnum (*(match + len));
+
+      result = starts_word && ends_word;
+      if (result)
+        break;
+      match = MATCHFUNC (match + len, folded_nick);
+    }
+
+  return result;
+}
+
 /**
  * polari_util_match_identify_message:
  * @message: a text message
diff --git a/src/lib/polari-util.h b/src/lib/polari-util.h
index 5d0f46b..0ece7a3 100644
--- a/src/lib/polari-util.h
+++ b/src/lib/polari-util.h
@@ -22,6 +22,9 @@
 
 char *polari_util_get_basenick (const char *nick);
 
+gboolean polari_util_match_nick (const char *text,
+                                const char *nick);
+
 gboolean polari_util_match_identify_message (const char  *message,
                                              char       **command,
                                              char       **username,


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