Skip to content
  • Daniel Boles's avatar
    ustring: Add sprintf(), wrapping g_strdup_printf() · 34c666c4
    Daniel Boles authored
    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.
    
    For reasons like that, C++'s preference of streams over stdio, and the
    hope that we'll eventually get an actual nice string-formatting solution
    in the C++ Standard, I don't go out of my way to shout about this in the
    documentation. Users who really want sprintf() will find it, without us
    having to shout too loudly about it and risk being seen as recommending
    it more than anything else. It's here for those who know they need it.
    
    #21
    34c666c4