# HG changeset patch # User Teemu Piippo # Date 1529667189 -10800 # Node ID bf373bd83c1fa32ccdb273b2b7f949432e233a78 # Parent 25584737611cb4c855a8e565b39c1a90e21220b9 simplified format() diff -r 25584737611c -r bf373bd83c1f src/format.h --- a/src/format.h Fri Jun 22 14:15:27 2018 +0300 +++ b/src/format.h Fri Jun 22 14:33:09 2018 +0300 @@ -101,47 +101,33 @@ QString m_text; }; - -// Helper function for format() -template -void formatHelper (QString& str, Arg1 arg1, Rest... rest) -{ - str = str.arg (StringFormatArg (arg1).text()); - formatHelper (str, rest...); -} - - -static void formatHelper (QString& str) __attribute__ ((unused)); -static void formatHelper (QString& str) -{ - (void) str; -} - - // Format the message with the given args. // // The formatting ultimately uses String's arg() method to actually format the args so the format string should be // prepared accordingly, with %1 referring to the first arg, %2 to the second, etc. -template -QString format (QString fmtstr, Args... args) +template +QString format(const QString& formatString, const T& arg, Rest... rest) { - formatHelper (fmtstr, args...); - return fmtstr; + return format(formatString.arg(StringFormatArg(arg).text()), rest...); +} + +// Recursion base +inline QString format(const QString& formatString) +{ + return formatString; } template -void fprint (FILE* fp, QString fmtstr, Args... args) +void fprint(FILE* fp, const QString& formatString, Args... args) { - formatHelper (fmtstr, args...); - fprintf (fp, "%s", qPrintable (fmtstr)); + fprintf(fp, "%s", qPrintable(format(formatString, args...))); } template -void fprint (QIODevice& dev, QString fmtstr, Args... args) +void fprint (QIODevice& dev, const QString& formatString, Args... args) { - formatHelper (fmtstr, args...); - dev.write (fmtstr.toUtf8()); + dev.write(format(formatString, args...).toUtf8()); } class Printer : public QObject