[glibmm/wip/dboles/ustring-sprintf: 4/4] ustring: Add overloads for const char* fmt . . .



commit 7e0eff2af16d7d23929319741be1a57eb4168640
Author: Daniel Boles <dboles src gnome org>
Date:   Fri Jun 21 19:03:35 2019 +0100

    ustring: Add overloads for const char* fmt . . .
    
    . . . in the hope that doing so can help compilers catch some mistakes
    that users might make. However, point out they shouldn't rely on that.
    We only provide this overload in case it catches such users in the act
    of making a mistake, but they should still be careful when writing code.
    
    details:
     (1) This means we can implement sprintf(ustring const&) in terms of
         ustring(char const*) instead of repeating ourselves.
     (2) I provide a 2nd overload of the zero-args case just for symmetry,
         and to avoid creating another ustring for the argument just to copy
         it on return (if not that I expect this to be performance-critical)
    
    https://gitlab.gnome.org/GNOME/glibmm/issues/21#note_537941

 glib/glibmm/ustring.h | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
---
diff --git a/glib/glibmm/ustring.h b/glib/glibmm/ustring.h
index 0cbf21e1..0bd0e701 100644
--- a/glib/glibmm/ustring.h
+++ b/glib/glibmm/ustring.h
@@ -764,6 +764,24 @@ public:
   template <class... Ts>
   static inline ustring sprintf(const ustring& fmt, const Ts&... args);
 
+  /* Overload of sprintf() taking a string literal.
+   *
+   * The main benefit of this is not constructing a temporary ustring if @p fmt
+   * is a string literal. A secondary effect is that it might encourage compilers
+   * to check if the given format @p fmt matches the variadic arguments @p args.
+   * The latter effect is a convenience at best; you must not rely on it to find
+   * errors in your code, as your compiler might not always be able to do so.
+   *
+   * @param fmt The template string, in the format used by <tt>printf()</tt> et al.
+   * @param args A set of arguments having the count/types/order required by @a fmt.
+   *
+   * @return The substituted string.
+   *
+   * @newin{2,62}
+   */
+  template <class... Ts>
+  static inline ustring sprintf(const char* fmt, const Ts&... args);
+
   /*! Overload of sprintf() for a format string only, which returns it unchanged.
    *
    * If no @p args to be substituted are given, there is nothing to do, so the
@@ -778,6 +796,16 @@ public:
    */
   static inline ustring sprintf(const ustring& fmt);
 
+  /*! Overload of sprintf() for a format string only, which returns it unchanged
+   * and avoids creating a temporary ustring as the argument.
+   *
+   * @param fmt The string
+   * @return The same string, as a ustring.
+   *
+   * @newin{2,62}
+   */
+  static inline ustring sprintf(const char* fmt);
+
   //! @}
 
 private:
@@ -1274,7 +1302,15 @@ inline // static
   ustring
   ustring::sprintf(const ustring& fmt, const Ts&... args)
 {
-  auto c_str = g_strdup_printf(fmt.c_str(), sprintify(args)...);
+  return sprintf(fmt.c_str(), args...);
+}
+
+template <class... Ts>
+inline // static
+  ustring
+  ustring::sprintf(const char* fmt, const Ts&... args)
+{
+  auto c_str = g_strdup_printf(fmt, sprintify(args)...);
   Glib::ustring ustr(c_str);
   g_free(c_str);
 
@@ -1288,6 +1324,13 @@ inline // static
   return fmt;
 }
 
+inline // static
+  ustring
+  ustring::sprintf(const char* fmt)
+{
+  return ustring(fmt);
+}
+
 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
 
 /** @relates Glib::ustring */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]