Skip to content

Instantly share code, notes, and snippets.

@Maggie262
Forked from ehabkost/reverse_fprintf.c
Created December 9, 2020 06:02
Show Gist options
  • Save Maggie262/94dec102a5244b46515403b4e238bc2a to your computer and use it in GitHub Desktop.
Save Maggie262/94dec102a5244b46515403b4e238bc2a to your computer and use it in GitHub Desktop.
/* Copyright (C) 2019 Red Hat Inc.
*
* Authors:
* Eduardo Habkost <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
char *reverse_fprintf(const char *format, const char *str)
{
char *r = NULL;
char *pattern = NULL;
GRegex *re = NULL;
GMatchInfo *match = NULL;
pattern = g_strdup_printf(format, "(.*)");
re = g_regex_new(pattern, G_REGEX_ANCHORED, 0, NULL);
if (!re) {
goto out;
}
if (!g_regex_match(re, str, 0, &match)) {
goto out;
}
r = g_strdup(g_match_info_fetch(match, 1));
out:
g_match_info_free(match);
if (re) {
g_regex_unref(re);
}
g_free(pattern);
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment