SF.net SVN: geany:[3223] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Thu Nov 13 17:19:52 UTC 2008


Revision: 3223
          http://geany.svn.sourceforge.net/geany/?rev=3223&view=rev
Author:   ntrel
Date:     2008-11-13 17:19:52 +0000 (Thu, 13 Nov 2008)

Log Message:
-----------
Make API function utils_string_replace_all() able to make
replacements that match the search string.

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

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-11-13 16:42:51 UTC (rev 3222)
+++ trunk/ChangeLog	2008-11-13 17:19:52 UTC (rev 3223)
@@ -27,6 +27,9 @@
  * src/editor.c:
    Make function editor_insert_text_block() interpret any \t tab chars
    as indent widths when inserting text.
+ * src/utils.c:
+   Make API function utils_string_replace_all() able to make
+   replacements that match the search string.
 
 
 2008-11-12  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2008-11-13 16:42:51 UTC (rev 3222)
+++ trunk/src/utils.c	2008-11-13 17:19:52 UTC (rev 3223)
@@ -1347,37 +1347,44 @@
 
 
 /**
- *  Replaces all occurrences of @c needle in @c haystack with @c replace.
- *  Currently this is not safe if @c replace matches @c needle, so @c needle and @c replace
- *  must not be equal.
+ * Replaces all occurrences of @c needle in @c haystack with @c replace.
+ * As of Geany 0.16, @a replace can match @a needle, so the following will work:
+ * @code utils_string_replace_all(text, "\n", "\r\n"); @endcode
  *
- *  @param haystack The input string to operate on. This string is modified in place.
- *  @param needle The string which should be replaced.
- *  @param replace The replacement for @c needle.
+ * @param haystack The input string to operate on. This string is modified in place.
+ * @param needle The string which should be replaced.
+ * @param replace The replacement for @c needle.
  *
- *  @return @a TRUE if @c needle was found, else @a FALSE.
+ * @return @a TRUE if @c needle was found, else @a FALSE.
  **/
 gboolean utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
 {
-	const gchar *c;
+	const gchar *stack, *match;
 	gssize pos = -1;
 
 	if (haystack->len == 0)
 		return FALSE;
 	g_return_val_if_fail(NZV(needle), FALSE);
-	g_return_val_if_fail(! NZV(replace) || strstr(replace, needle) == NULL, FALSE);
 
+	stack = haystack->str;
 	while (1)
 	{
-		c = strstr(haystack->str, needle);
-		if (c == NULL)
+		match = strstr(stack, needle);
+		if (match == NULL)
 			break;
 		else
 		{
-			pos = c - haystack->str;
+			pos = match - haystack->str;
 			g_string_erase(haystack, pos, strlen(needle));
+
+			/* next search is after removed matching text */
+			stack = match;
+
 			if (replace)
+			{
 				g_string_insert(haystack, pos, replace);
+				stack += strlen(replace);	/* don't replace replacements */
+			}
 		}
 	}
 	return (pos != -1);


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