SF.net SVN: geany:[5726] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Fri Apr 15 16:59:16 UTC 2011


Revision: 5726
          http://geany.svn.sourceforge.net/geany/?rev=5726&view=rev
Author:   ntrel
Date:     2011-04-15 16:59:15 +0000 (Fri, 15 Apr 2011)

Log Message:
-----------
Add function utils_string_replace() to replace in a fixed range.
Remove utils_string_replace_helper() and update cursor marker code.

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

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2011-04-14 17:37:46 UTC (rev 5725)
+++ trunk/ChangeLog	2011-04-15 16:59:15 UTC (rev 5726)
@@ -1,3 +1,10 @@
+2011-04-15  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
+
+ * src/utils.c, src/utils.h, src/editor.c:
+   Add function utils_string_replace() to replace in a fixed range.
+   Remove utils_string_replace_helper() and update cursor marker code.
+
+
 2011-04-14  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
 
  * src/editor.c:

Modified: trunk/src/editor.c
===================================================================
--- trunk/src/editor.c	2011-04-14 17:37:46 UTC (rev 5725)
+++ trunk/src/editor.c	2011-04-15 16:59:15 UTC (rev 5726)
@@ -2436,10 +2436,8 @@
 
 	if (cursor_index >= 0)
 	{
-		gint idx = utils_strpos(buf->str, geany_cursor_marker);
+		gint idx = utils_string_replace(buf, 0, -1, geany_cursor_marker, NULL);
 
-		g_string_erase(buf, idx, strlen(geany_cursor_marker));
-
 		sci_insert_text(sci, insert_pos, buf->str);
 		sci_set_current_position(sci, insert_pos + idx, FALSE);
 	}
@@ -2530,7 +2528,7 @@
 static gssize replace_cursor_markers(GeanyEditor *editor, GString *pattern, gsize indent_size)
 {
 	gssize cur_index = -1;
-	gint i, pos, idx, nl_count = 0;
+	gint i, idx, nl_count = 0;
 	GList *temp_list = NULL;
 	gint cursor_steps, old_cursor = 0;
 
@@ -2540,15 +2538,13 @@
 	{
 		/* replace every newline (up to next cursor) with EOL,
 		 * count newlines and update cursor_steps after */
-		while ((pos = utils_strpos(pattern->str + idx, "\n")) != -1)
+		while (1)
 		{
-			idx += pos;
-			if (idx >= cursor_steps)
+			idx = utils_string_replace(pattern, idx, cursor_steps, "\n", editor_get_eol_char(editor));
+			if (idx == -1)
 				break;
 
 			nl_count++;
-			g_string_erase(pattern, idx, 1);
-			g_string_insert(pattern, idx, editor_get_eol_char(editor));
 			idx += editor_get_eol_char_len(editor);
 
 			cursor_steps = utils_strpos(pattern->str, geany_cursor_marker);
@@ -2571,11 +2567,12 @@
 		old_cursor = cursor_steps;
 	}
 	/* replace remaining \n which may occur after the last cursor */
-	while ((pos = utils_strpos(pattern->str + idx, "\n")) != -1)
+	while (1)
 	{
-		idx += pos;
-		g_string_erase(pattern, idx, 1);
-		g_string_insert(pattern, idx, editor_get_eol_char(editor));
+		idx = utils_string_replace(pattern, idx, -1, "\n", editor_get_eol_char(editor));
+		if (idx == -1)
+			break;
+
 		idx += editor_get_eol_char_len(editor);
 	}
 

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2011-04-14 17:37:46 UTC (rev 5725)
+++ trunk/src/utils.c	2011-04-15 16:59:15 UTC (rev 5726)
@@ -1530,39 +1530,39 @@
 }
 
 
-static guint utils_string_replace_helper(GString *haystack, const gchar *needle,
-										 const gchar *replace, const guint max_replaces)
+/* Replaces needle if in range.
+ * end can be -1 for haystack->len.
+ * returns: position of replaced text or -1. */
+gint utils_string_replace(GString *haystack, gint start, gint end,
+		const gchar *needle, const gchar *replace)
 {
-	const gchar *stack, *match;
-	guint ret = 0;
-	gssize pos;
+	gint pos;
 
-	g_return_val_if_fail(haystack != NULL, 0);
+	g_return_val_if_fail(haystack != NULL, -1);
 	if (haystack->len == 0)
-		return FALSE;
-	g_return_val_if_fail(NZV(needle), 0);
+		return -1;
 
-	stack = haystack->str;
-	if (! (match = strstr(stack, needle)))
-		return 0;
-	do
-	{
-		pos = match - haystack->str;
-		g_string_erase(haystack, pos, strlen(needle));
+	g_return_val_if_fail(start >= 0, -1);
+	if (start >= (gint)haystack->len)
+		return -1;
 
-		/* 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;
+	g_return_val_if_fail(NZV(needle), -1);
 
-		if (G_LIKELY(replace))
-		{
-			g_string_insert(haystack, pos, replace);
-			stack = haystack->str + pos + strlen(replace);	/* skip past replacement */
-		}
-	}
-	while (++ret != max_replaces && (match = strstr(stack, needle)));
+	if (end < 0)
+		end = haystack->len;
 
-	return ret;
+	pos = utils_strpos(haystack->str + start, needle);
+	if (pos == -1)
+		return -1;
+
+	pos += start;
+	if (pos >= end)
+		return -1;
+
+	g_string_erase(haystack, pos, strlen(needle));
+	if (G_LIKELY(replace))
+		g_string_insert(haystack, pos, replace);
+	return pos;
 }
 
 
@@ -1579,7 +1579,21 @@
  **/
 guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
 {
-	return utils_string_replace_helper(haystack, needle, replace, 0);
+	guint count = 0;
+	gint pos = 0;
+
+	while (1)
+	{
+		pos = utils_string_replace(haystack, pos, -1, needle, replace);
+
+		if (pos == -1)
+			break;
+
+		if (replace)
+			pos += strlen(replace);
+		count++;
+	}
+	return count;
 }
 
 
@@ -1598,7 +1612,7 @@
  */
 guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace)
 {
-	return utils_string_replace_helper(haystack, needle, replace, 1);
+	return utils_string_replace(haystack, 0, -1, needle, replace) == -1 ? 0 : 1;
 }
 
 

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2011-04-14 17:37:46 UTC (rev 5725)
+++ trunk/src/utils.h	2011-04-15 16:59:15 UTC (rev 5726)
@@ -168,6 +168,9 @@
 
 gchar *utils_get_hostname(void);
 
+gint utils_string_replace(GString *haystack, gint start, gint end,
+		const gchar *needle, const gchar *replace);
+
 guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace);
 
 guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace);


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