Revision: 5730 http://geany.svn.sourceforge.net/geany/?rev=5730&view=rev Author: ntrel Date: 2011-04-19 16:36:29 +0000 (Tue, 19 Apr 2011)
Log Message: ----------- Add utils_string_find() to search in a fixed range. Change utils_string_replace() to just replace a fixed number of characters.
Modified Paths: -------------- trunk/ChangeLog trunk/src/editor.c trunk/src/utils.c trunk/src/utils.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2011-04-19 16:04:31 UTC (rev 5729) +++ trunk/ChangeLog 2011-04-19 16:36:29 UTC (rev 5730) @@ -4,6 +4,10 @@ Fix multiple snippet cursor positions for Tabs + Spaces mode. Simplify editor_insert_snippet() code now we use cursor marker strings. + * src/utils.c, src/utils.h, src/editor.c: + Add utils_string_find() to search in a fixed range. + Change utils_string_replace() to just replace a fixed number of + characters.
2011-04-17 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
Modified: trunk/src/editor.c =================================================================== --- trunk/src/editor.c 2011-04-19 16:04:31 UTC (rev 5729) +++ trunk/src/editor.c 2011-04-19 16:36:29 UTC (rev 5730) @@ -2347,9 +2347,7 @@
g_return_val_if_fail(match->rm_so >= 0, FALSE); pos += match->rm_so; - g_string_erase(haystack, pos, match->rm_eo - match->rm_so); - g_string_insert(haystack, pos, replace); - pos += strlen(replace); + pos = utils_string_replace(haystack, pos, match->rm_eo - match->rm_so, replace); ret++; } return ret; @@ -2534,10 +2532,12 @@ i = 0; while (1) { - cursor_steps = utils_string_replace(pattern, cursor_steps, -1, geany_cursor_marker, NULL); + cursor_steps = utils_string_find(pattern, cursor_steps, -1, geany_cursor_marker); if (cursor_steps == -1) break;
+ g_string_erase(pattern, cursor_steps, strlen(geany_cursor_marker)); + if (i++ > 0) { /* save the relative offset to each cursor position */
Modified: trunk/src/utils.c =================================================================== --- trunk/src/utils.c 2011-04-19 16:04:31 UTC (rev 5729) +++ trunk/src/utils.c 2011-04-19 16:36:29 UTC (rev 5730) @@ -1530,11 +1530,9 @@ }
-/* 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) +/* end can be -1 for haystack->len. + * returns: position of found text or -1. */ +gint utils_string_find(GString *haystack, gint start, gint end, const gchar *needle) { gint pos;
@@ -1558,14 +1556,22 @@ 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; }
+/* Replaces @len characters from offset @a pos. + * len can be -1 for str->len. + * returns: pos + strlen(replace). */ +gint utils_string_replace(GString *str, gint pos, gint len, const gchar *replace) +{ + g_string_erase(str, pos, len); + g_string_insert(str, pos, replace); + + return pos + strlen(replace); +} + + /** * Replaces all occurrences of @a needle in @a haystack with @a replace. * As of Geany 0.16, @a replace can match @a needle, so the following will work: @@ -1584,13 +1590,12 @@
while (1) { - pos = utils_string_replace(haystack, pos, -1, needle, replace); + pos = utils_string_find(haystack, pos, -1, needle);
if (pos == -1) break;
- if (replace) - pos += strlen(replace); + pos = utils_string_replace(haystack, pos, strlen(needle), replace); count++; } return count; @@ -1612,7 +1617,13 @@ */ guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace) { - return utils_string_replace(haystack, 0, -1, needle, replace) == -1 ? 0 : 1; + gint pos = utils_string_find(haystack, 0, -1, needle); + + if (pos == -1) + return 0; + + utils_string_replace(haystack, pos, strlen(needle), replace); + return 1; }
Modified: trunk/src/utils.h =================================================================== --- trunk/src/utils.h 2011-04-19 16:04:31 UTC (rev 5729) +++ trunk/src/utils.h 2011-04-19 16:36:29 UTC (rev 5730) @@ -168,9 +168,10 @@
gchar *utils_get_hostname(void);
-gint utils_string_replace(GString *haystack, gint start, gint end, - const gchar *needle, const gchar *replace); +gint utils_string_find(GString *haystack, gint start, gint end, const gchar *needle);
+gint utils_string_replace(GString *str, gint pos, gint len, 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.