GNOME Bugzilla – Bug 119678
GMarkup and printf
Last modified: 2011-02-18 16:07:08 UTC
It would be very nice to have a function that could be used in situations like gtk_dialog_new_with_markup() that automatically escaped all string and character arguments with g_markup_escape_text() The problem here is implementing it. Problems with doing the printf first: * Hard to implement - we need to be able to do something with only the parts of the string that are printf output. Problems with parsing the markup first, then doing the printf. * What does Should %<b>s</b> work as a string format? What is the right result? * Should #x2f;s work as a string format? * Hard to implement; how do we know how to adjust the attrlist for the effects of the expansion. A trick we can use to implement this described as follows: Say, we have printf ("Jane %s the %s\N", verb, noun); If we compare the results of: printf ("%sX%sX", verb, noun); printf ("%sX%sY", verb, noun); And we get: "ateXcakeX" "ateYcakeY" then by finding the next position at which the two strings vary we can determine that the two formatted segments were "ate" and "cake". The attached patch implements this, using a heavily stripped down form of gnulib's printf-parse.c to find the conversion specifiers. It's not especially efficient way of doing things but should be quite portable. Perhaps g_markup_escaped_printf() / g_markup_escaped_printfv() for names for the functions. The patch could use some more tests and some comments, and needs documentation.
Created attachment 19131 [details] Implementation of the idea
Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor@redhat.com> * glib/gmarkup.c: Add g_markup_printf_escaped(), g_markup_vprintf_escaped(). * tests/markup-escape-test.c (main): Test for g_markup_escape_text(), g_markup_printf_escaped().