diff --git a/cmirror.c b/cmirror.c index 03fd620..95ed53c 100644 --- a/cmirror.c +++ b/cmirror.c @@ -23,6 +23,7 @@ #include #include #include +#include // https://www.manpagez.com/html/glib/glib-2.56.0/glib-Double-ended-Queues.php // http://owww.molgen.mpg.de/~buczek/glib-doc/ @@ -82,25 +83,15 @@ static char **match_re(GRegex *regex, char *string) { match_re(regex, string); \ }) -static gboolean fn_escape_cb(const GMatchInfo *match_info, GString *result, gpointer user_data) { - (void)user_data; - g_autofree char *c = g_match_info_fetch(match_info, 0); - assert(c && c[0] != '\0' && c[1] == '\0'); - g_string_append_printf(result, "\\x%02x", *c); - return FALSE; -} - static char *fn_escape(char *fn) { - static GRegex *regex = NULL; - if (!regex) { - regex = compile_pattern("([[:^graph:]\\\\])"); + GString *out = g_string_sized_new(80); + for (char c = *fn++ ; c ; c = *fn++) { + if (isgraph(c)) + g_string_append_c(out, c); + else + g_string_append_printf(out, "\\x%02x", (unsigned char)c); } - GError *error; - - char *ret = g_regex_replace_eval(regex, fn, -1, 0, 0, fn_escape_cb, NULL, &error); - if (!ret) - die("%s\n", error->message); - return ret; + return g_string_free(out, FALSE); } static gboolean fn_unescape_cb(const GMatchInfo *match_info, GString *result, gpointer user_data) {