Add utils_string_find() to search in a fixed range.
Change utils_string_replace() to just replace a fixed number of characters. git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5730 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
parent
06f9d7e068
commit
a89ba90b15
@ -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>
|
||||
|
@ -2347,9 +2347,7 @@ static guint utils_string_regex_replace_all(GString *haystack,
|
||||
|
||||
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 @@ static gssize replace_cursor_markers(GeanyEditor *editor, GString *pattern)
|
||||
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 */
|
||||
|
37
src/utils.c
37
src/utils.c
@ -1530,11 +1530,9 @@ gboolean utils_str_has_upper(const gchar *str)
|
||||
}
|
||||
|
||||
|
||||
/* 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 @@ gint utils_string_replace(GString *haystack, gint start, gint end,
|
||||
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 @@ guint utils_string_replace_all(GString *haystack, const gchar *needle, const gch
|
||||
|
||||
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_all(GString *haystack, const gchar *needle, const gch
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -168,8 +168,9 @@ gchar utils_brace_opposite(gchar ch);
|
||||
|
||||
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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user