Add function utils_string_replace() to replace in a fixed range.
Remove utils_string_replace_helper() and update cursor marker code. git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5726 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
parent
69c5d1f15e
commit
0b2d16a41a
@ -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:
|
||||
|
23
src/editor.c
23
src/editor.c
@ -2436,9 +2436,7 @@ void editor_insert_text_block(GeanyEditor *editor, const gchar *text, gint inser
|
||||
|
||||
if (cursor_index >= 0)
|
||||
{
|
||||
gint idx = utils_strpos(buf->str, geany_cursor_marker);
|
||||
|
||||
g_string_erase(buf, idx, strlen(geany_cursor_marker));
|
||||
gint idx = utils_string_replace(buf, 0, -1, geany_cursor_marker, NULL);
|
||||
|
||||
sci_insert_text(sci, insert_pos, buf->str);
|
||||
sci_set_current_position(sci, insert_pos + idx, FALSE);
|
||||
@ -2530,7 +2528,7 @@ static gssize snippets_make_replacements(GeanyEditor *editor, GString *pattern,
|
||||
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 @@ static gssize replace_cursor_markers(GeanyEditor *editor, GString *pattern, gsiz
|
||||
{
|
||||
/* 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 @@ static gssize replace_cursor_markers(GeanyEditor *editor, GString *pattern, gsiz
|
||||
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);
|
||||
}
|
||||
|
||||
|
70
src/utils.c
70
src/utils.c
@ -1530,39 +1530,39 @@ gboolean utils_str_has_upper(const gchar *str)
|
||||
}
|
||||
|
||||
|
||||
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 @@ static guint utils_string_replace_helper(GString *haystack, const gchar *needle,
|
||||
**/
|
||||
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_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_helper(haystack, needle, replace, 1);
|
||||
return utils_string_replace(haystack, 0, -1, needle, replace) == -1 ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
|
@ -168,6 +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);
|
||||
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user