[Xfce4-commits] [panel-plugins/xfce4-whiskermenu-plugin] 50/473: Improve search algorithm.

noreply at xfce.org noreply at xfce.org
Mon Feb 16 23:53:40 CET 2015


This is an automated email from the git hooks/post-receive script.

gottcode pushed a commit to branch master
in repository panel-plugins/xfce4-whiskermenu-plugin.

commit 29470a1a307fbec4215b2725a9930e7009d72b87
Author: Graeme Gott <graeme at gottcode.org>
Date:   Mon Jul 1 16:10:49 2013 -0400

    Improve search algorithm.
---
 src/launcher.cpp |   71 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 3 deletions(-)

diff --git a/src/launcher.cpp b/src/launcher.cpp
index b0a09ae..79707bb 100644
--- a/src/launcher.cpp
+++ b/src/launcher.cpp
@@ -16,6 +16,8 @@
 
 #include "launcher.hpp"
 
+#include <vector>
+
 extern "C"
 {
 #include <exo/exo.h>
@@ -243,17 +245,80 @@ void Launcher::run(GdkScreen* screen) const
 
 unsigned int Launcher::search(const std::string& filter_string)
 {
+	// Check if search has been done before
 	std::map<std::string, unsigned int>::const_iterator i = m_searches.find(filter_string);
 	if (i != m_searches.end())
 	{
 		return i->second;
 	}
 
+	// Check if search will fail because a shorter version has failed before
+
+	// Perform search
 	unsigned int index = UINT_MAX;
-	gchar* result = g_strstr_len(get_search_text(), -1, filter_string.c_str());
-	if (result != NULL)
+	std::vector<unsigned int> spaces;
+
+	const gchar* filter_string_c = filter_string.c_str();
+	const gchar* filter_string_ind = filter_string_c;
+	size_t filter_len = filter_string.length();
+
+	const gchar* search_text = get_search_text();
+	size_t len = strlen(search_text);
+	for (const gchar* pos = search_text; *pos; pos = g_utf8_next_char(pos))
+	{
+		gunichar c = g_utf8_get_char(pos);
+		len -= (pos - search_text);
+		if ((len >= filter_len) && (memcmp(pos, filter_string_c, filter_len) == 0))
+		{
+			index = pos - search_text;
+			break;
+		}
+		else if (c == g_utf8_get_char(filter_string_ind))
+		{
+			filter_string_ind = g_utf8_next_char(filter_string_ind);
+		}
+		else if (g_unichar_isspace(c))
+		{
+			spaces.push_back(pos - search_text);
+		}
+		else if ((c == '\n') && (*filter_string_ind != 0))
+		{
+			filter_string_ind = filter_string_c;
+		}
+	}
+
+	// Check if search text starts with filter string
+	if (index == 0)
+	{
+		// Do nothing
+	}
+	// Check if search text contains filter string
+	else if (index != UINT_MAX)
+	{
+		// Check if a word in search text starts with filter string
+		unsigned int space_index = 0;
+		for (std::vector<unsigned int>::const_reverse_iterator i = spaces.rbegin(), end = spaces.rend(); i != end; ++i)
+		{
+			if (*i < index)
+			{
+				space_index = *i;
+				break;
+			}
+		}
+		unsigned int delta = index - space_index;
+		if (delta == 1)
+		{
+			index += 0x10000000;
+		}
+		else
+		{
+			index += 0x20000000 + delta;
+		}
+	}
+	// Check if search text contains characters of string
+	else if (*filter_string_ind == 0)
 	{
-		index = result - get_search_text();
+		index = UINT_MAX - 1;
 	}
 
 	m_searches.insert(std::make_pair(filter_string, index));

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list