using System; namespace printf { /// /// Contains the printf family of functions. /// It supports the following formats: 'diufeExXoscp'; /// The following flags: '-+ #0'; /// Width and precision; /// Length (h, l) for hexadecimal output (for other formats, integers are treated as Int64 or UInt64) /// /// Decimal separator is not localized, it is always a '.' character. /// %g and %G does not remove trailing zeroes. /// NaN and infinities are not guaranteed to have a fixed representation accross platforms. /// /// At the moment, it does NOT support %n (number of chars printed, through a prointer), /// and the 1$..n$ position specifier. /// public static class Printf { /// /// Formats the arguments according to the format string. /// Returns the result as a string. /// /// The format string /// The objects to format /// The formatted output /// The format string is invalid or too few arguments provided. /// Format string is null public static string sprintf(string format, params object[] args) { if (format == null) throw new ArgumentNullException("format"); try { FormatObject f = new FormatObject(format); f.SetArgs(args); return f.ToString(); } catch(InvalidOperationException ex) { throw new ArgumentException("Error in format string or arguments, see inner exception for details", ex); } } /// /// Formats the arguments according to the format string, /// and writes the formatted string to the standard output. /// /// The format string /// The objects to format /// The format string is invalid or too few arguments provided. /// Format string is null public static void printf(string format, params object[] args) { if (format == null) throw new ArgumentNullException("format"); Console.Write(sprintf(format, args)); } /// /// Formats the arguments according to the format string, /// and writes the formatted string to the specified TextWriter. /// /// The target TextWriter /// The format string /// The objects to format /// The format string is invalid or too few arguments provided. /// Format string or TextWriter is null public static void fprintf(System.IO.TextWriter writer, string format, params object[] args) { if (format == null) throw new ArgumentNullException("format"); if (writer == null) throw new ArgumentNullException("writer"); writer.Write(sprintf(format, args)); } } }