Merge branch 'add_g_prefix_error_literal' into 'master'

Add g_prefix_error_literal()

Closes #663

See merge request GNOME/glib!2121
This commit is contained in:
Philip Withnall 2021-06-01 19:08:16 +00:00
commit bc0d9376b7
4 changed files with 52 additions and 0 deletions

View File

@ -786,6 +786,7 @@ g_set_error_literal
g_propagate_error
g_clear_error
g_prefix_error
g_prefix_error_literal
g_propagate_prefixed_error
<SUBSECTION>
GErrorInitFunc

View File

@ -1100,6 +1100,30 @@ g_prefix_error (GError **err,
}
}
/**
* g_prefix_error_literal:
* @err: (allow-none): a return location for a #GError, or %NULL
* @prefix: string to prefix @err with
*
* Prefixes @prefix to an existing error message. If @err or *@err is
* %NULL (i.e.: no error variable) then do nothing.
*
* Since: 2.70
*/
void
g_prefix_error_literal (GError **err,
const gchar *prefix)
{
if (err && *err)
{
gchar *oldstring;
oldstring = (*err)->message;
(*err)->message = g_strconcat (prefix, oldstring, NULL);
g_free (oldstring);
}
}
/**
* g_propagate_prefixed_error:
* @dest: error return location

View File

@ -244,6 +244,11 @@ void g_prefix_error (GError **err,
const gchar *format,
...) G_GNUC_PRINTF (2, 3);
/* if (err) prefix the string to the ->message */
GLIB_AVAILABLE_IN_2_70
void g_prefix_error_literal (GError **err,
const gchar *prefix);
/* g_propagate_error then g_error_prefix on dest */
GLIB_AVAILABLE_IN_ALL
void g_propagate_prefixed_error (GError **dest,

View File

@ -69,6 +69,27 @@ test_prefix (void)
g_propagate_prefixed_error (NULL, src, "foo %d %s: ", 1, "two");
}
static void
test_prefix_literal (void)
{
GError *error = NULL;
g_prefix_error_literal (NULL, "foo: ");
g_prefix_error_literal (&error, "foo: ");
g_assert_null (error);
error = NULL;
g_prefix_error_literal (&error, "foo: ");
g_assert_null (error);
error = g_error_new_literal (G_MARKUP_ERROR, G_MARKUP_ERROR_EMPTY, "bla");
g_assert_nonnull (error);
g_prefix_error_literal (&error, "foo: ");
g_assert_cmpstr (error->message, ==, "foo: bla");
g_error_free (error);
}
static void
test_literal (void)
{
@ -374,6 +395,7 @@ main (int argc, char *argv[])
g_test_add_func ("/error/overwrite", test_overwrite);
g_test_add_func ("/error/prefix", test_prefix);
g_test_add_func ("/error/prefix-literal", test_prefix_literal);
g_test_add_func ("/error/literal", test_literal);
g_test_add_func ("/error/copy", test_copy);
g_test_add_func ("/error/matches", test_matches);