g_string_replace: Backport from GLib
Note that this implementation does not cope gracefully with zero-length patterns (https://gitlab.gnome.org/GNOME/glib/-/issues/2452) so we should be careful not to use it like that. For the use-case that I have in mind, that won't be a problem.
Suggested by @denittis for !337 (merged).