Skip to content

ustring: Add sprintf(), wrapping g_strdup_printf()

Daniel Boles requested to merge wip/dboles/ustring-sprintf into master

This is for master; should it be wanted in glibmm-2.4 also, then it should cherry-pick trivially.

This includes a pretty simplistic test, but I don't make anywhere else use it, since I'm not sure if it'll get merged (see #21 (closed) for pitfalls, but they should be pretty obvious given that we need to delegate to printf().)

Add another way to produce formatted ustrings, this time using printf
syntax, by forwarding arguments to g_strdup_printf() and then copying
the result into the returned ustring.

This includes a private ustring::sprintify() function that by default
just forward its argument but can be overloaded to do something else.
In this commit, that is overloaded for ustring and std::string so that
their .c_str() is passed to printf instead, avoiding the ugliness of
users always having to write .c_str() in their own lists of arguments.

Note that the same lack of type safety as plagues printf() and all its
variants (in both C and GLib) applies here: the arguments are just
forwarded on, so if you include too few or the wrong types for the
placeholders you specify, you invoke undefined behaviour just as in C.

https://gitlab.gnome.org/GNOME/glibmm/issues/21

Merge request reports