[epiphany/mwleeds/webapp-dbus-api: 65/69] Fix ephy_string_find_and_replace
- From: Phaedrus Leeds <mwleeds src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/mwleeds/webapp-dbus-api: 65/69] Fix ephy_string_find_and_replace
- Date: Mon, 31 Jan 2022 18:58:51 +0000 (UTC)
commit 8df5b9d215118a25ee3743e4a209199cd5ab6ce2
Author: Phaedrus Leeds <mwleeds protonmail com>
Date: Mon Jan 24 19:36:28 2022 -0800
Fix ephy_string_find_and_replace
This replaces the implementation with the one from GLib
g_string_replace() because I thought it might help a bug I was seeing.
It doesn't help; the bug was elsewhere. But it still seems like a good
idea to do this.
lib/ephy-string.c | 28 +++++++++++++++++++++-------
1 file changed, 21 insertions(+), 7 deletions(-)
---
diff --git a/lib/ephy-string.c b/lib/ephy-string.c
index dd434e1b8..1606a5af0 100644
--- a/lib/ephy-string.c
+++ b/lib/ephy-string.c
@@ -244,8 +244,9 @@ ephy_string_find_and_replace (const char *haystack,
const char *to_repl)
{
GString *str;
- const char *tmp;
+ gchar *cur, *next;
gsize to_find_len;
+ gsize to_repl_len;
gsize pos;
g_assert (haystack);
@@ -254,12 +255,25 @@ ephy_string_find_and_replace (const char *haystack,
str = g_string_new (haystack);
to_find_len = strlen (to_find);
-
- while ((tmp = strstr (str->str, to_find)) != NULL) {
- pos = tmp - str->str;
- g_string_erase (str, pos, to_find_len);
- g_string_insert (str, pos, to_repl);
- }
+ to_repl_len = strlen (to_repl);
+ cur = str->str;
+
+ while ((next = strstr (cur, to_find)) != NULL)
+ {
+ pos = next - str->str;
+ g_string_erase (str, pos, to_find_len);
+ g_string_insert (str, pos, to_repl);
+ cur = str->str + pos + to_repl_len;
+ /* Only match the empty string once at any given position, to
+ * avoid infinite loops */
+ if (to_find_len == 0)
+ {
+ if (cur[0] == '\0')
+ break;
+ else
+ cur++;
+ }
+ }
return g_string_free (str, FALSE);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]