SF.net SVN: geany:[5994] trunk/src/utils.c

colombanw at users.sourceforge.net colombanw at xxxxx
Mon Oct 3 17:06:12 UTC 2011


Revision: 5994
          http://geany.svn.sourceforge.net/geany/?rev=5994&view=rev
Author:   colombanw
Date:     2011-10-03 17:06:12 +0000 (Mon, 03 Oct 2011)
Log Message:
-----------
Cleanup utils_strpos()

Use strstr() to find the substring, which makes the code simpler and
is way faster (new version is nearly 2 times faster on my machine).

Modified Paths:
--------------
    trunk/src/utils.c

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2011-10-03 17:05:57 UTC (rev 5993)
+++ trunk/src/utils.c	2011-10-03 17:06:12 UTC (rev 5994)
@@ -697,39 +697,16 @@
 
 gint utils_strpos(const gchar *haystack, const gchar *needle)
 {
-	gint haystack_length = strlen(haystack);
-	gint needle_length = strlen(needle);
-	gint i, j, pos = -1;
+	const gchar *sub;
 
-	if (needle_length > haystack_length)
-	{
+	if (! *needle)
 		return -1;
-	}
-	else
-	{
-		for (i = 0; (i < haystack_length) && pos == -1; i++)
-		{
-			if (haystack[i] == needle[0] && needle_length == 1)
-				return i;
-			else if (haystack[i] == needle[0])
-			{
-				for (j = 1; (j < needle_length); j++)
-				{
-					if (haystack[i + j] == needle[j])
-					{
-						if (pos == -1)
-							pos = i;
-					}
-					else
-					{
-						pos = -1;
-						break;
-					}
-				}
-			}
-		}
-		return pos;
-	}
+
+	sub = strstr(haystack, needle);
+	if (! sub)
+		return -1;
+
+	return sub - haystack;
 }
 
 

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Commits mailing list