SF.net SVN: geany:[3226] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Fri Nov 14 14:15:32 UTC 2008


Revision: 3226
          http://geany.svn.sourceforge.net/geany/?rev=3226&view=rev
Author:   ntrel
Date:     2008-11-14 14:15:32 +0000 (Fri, 14 Nov 2008)

Log Message:
-----------
Fix bug with utils_string_replace_all().
Make utils_str_replace() call utils_string_replace_all() internally
(for better memory management and allowing replacements to match
search string).

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

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-11-14 13:38:59 UTC (rev 3225)
+++ trunk/ChangeLog	2008-11-14 14:15:32 UTC (rev 3226)
@@ -4,6 +4,11 @@
    Prompt the user for whether to move the configuration directory or
    just quit instead. This is useful if the user is already running an
    older binary of Geany and the second instance is newer.
+ * src/utils.c, src/editor.c:
+   Fix bug with utils_string_replace_all().
+   Make utils_str_replace() call utils_string_replace_all() internally
+   (for better memory management and allowing replacements to match
+   search string).
 
 
 2008-11-13  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>

Modified: trunk/src/editor.c
===================================================================
--- trunk/src/editor.c	2008-11-14 13:38:59 UTC (rev 3225)
+++ trunk/src/editor.c	2008-11-14 14:15:32 UTC (rev 3226)
@@ -1813,9 +1813,8 @@
 		pattern = snippets_global_pattern;
 	}
 
-	/* replace line breaks and whitespaces */
-	pattern = utils_str_replace(pattern, "\n", "%newline%"); /* to avoid endless replacing of \n */
-	pattern = utils_str_replace(pattern, "%newline%", lindent);
+	/* add line indentation */
+	pattern = utils_str_replace(pattern, "\n", lindent);
 
 	/* replace any %template% wildcards */
 	pattern = snippets_replace_wildcards(editor, pattern);

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2008-11-14 13:38:59 UTC (rev 3225)
+++ trunk/src/utils.c	2008-11-14 14:15:32 UTC (rev 3226)
@@ -497,45 +497,19 @@
 
 
 /* Replaces all occurrences of needle in haystack with replacement.
- * New code should use utils_string_replace_all() instead.
- * All strings have to be NULL-terminated and needle and replacement have to be different,
- * e.g. needle "%" and replacement "%%" causes an endless loop */
+ * Warning: haystack will be freed.
+ * New code should use utils_string_replace_all() instead (freeing arguments
+ * is unusual behaviour).
+ * All strings have to be NULL-terminated.
+ * See utils_string_replace_all() for details. */
 gchar *utils_str_replace(gchar *haystack, const gchar *needle, const gchar *replacement)
 {
-	gint i;
-	gchar *start;
-	gint lt_pos;
-	gchar *result;
-	GString *str;
+	GString *str = g_string_new(haystack);
 
-	if (haystack == NULL)
-		return NULL;
-
-	if (needle == NULL || replacement == NULL)
-		return haystack;
-
-	if (utils_str_equal(needle, replacement))
-		return haystack;
-
-	start = strstr(haystack, needle);
-	lt_pos = utils_strpos(haystack, needle);
-
-	if (start == NULL || lt_pos == -1)
-		return haystack;
-
-	/* substitute by copying */
-	str = g_string_sized_new(strlen(haystack));
-	for (i = 0; i < lt_pos; i++)
-	{
-		g_string_append_c(str, haystack[i]);
-	}
-	g_string_append(str, replacement);
-	g_string_append(str, haystack + lt_pos + strlen(needle));
-
-	result = str->str;
 	g_free(haystack);
-	g_string_free(str, FALSE);
-	return utils_str_replace(result, needle, replacement);
+	utils_string_replace_all(str, needle, replacement);
+
+	return g_string_free(str, FALSE);
 }
 
 
@@ -1377,13 +1351,14 @@
 			pos = match - haystack->str;
 			g_string_erase(haystack, pos, strlen(needle));
 
-			/* next search is after removed matching text */
-			stack = match;
+			/* make next search after removed matching text.
+			 * (we have to be careful to only use haystack->str as its address may change) */
+			stack = haystack->str + pos;
 
 			if (replace)
 			{
 				g_string_insert(haystack, pos, replace);
-				stack += strlen(replace);	/* don't replace replacements */
+				stack = haystack->str + pos + strlen(replace);	/* skip past replacement */
 			}
 		}
 	}


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